Beispiel #1
0
func (msg *MessageV3) Marshal() (b []byte, err error) {
	var buf []byte
	raw := asn1.RawValue{Class: asn1.ClassUniversal, Tag: asn1.TagSequence, IsCompound: true}

	buf, err = asn1.Marshal(msg.version)
	if err != nil {
		return
	}
	raw.Bytes = buf

	buf, err = msg.globalDataV3.Marshal()
	if err != nil {
		return
	}
	raw.Bytes = append(raw.Bytes, buf...)

	buf, err = msg.securityParameterV3.Marshal()
	if err != nil {
		return
	}
	raw.Bytes = append(raw.Bytes, buf...)

	// if 0 == len(msg.pduBytes) {
	// 	panic("pdu bytes is empty.")
	// }
	raw.Bytes = append(raw.Bytes, msg.pduBytes...)
	return asn1.Marshal(raw)
}
Beispiel #2
0
func (sec *securityParameterV3) Marshal() ([]byte, error) {
	raw := asn1.RawValue{Class: asn1.ClassUniversal, Tag: asn1.TagOctetString, IsCompound: false}

	buf, err := asn1.Marshal(*sec)
	if err != nil {
		return nil, err
	}
	raw.Bytes = buf

	return asn1.Marshal(raw)
}
Beispiel #3
0
func (v *TimeTicks) Marshal() ([]byte, error) {
	b, err := asn1.Marshal(int64(v.Value))
	if err == nil {
		b[0] = asn1.TagTimeticks
	}
	return b, err
}
Beispiel #4
0
func (v *Opaque) Marshal() ([]byte, error) {
	b, err := asn1.Marshal(v.Value)
	if err == nil {
		b[0] = asn1.TagOpaque
	}
	return b, err
}
Beispiel #5
0
func (v *Ipaddress) Marshal() ([]byte, error) {
	b, err := asn1.Marshal(v.Value)
	if err == nil {
		b[0] = asn1.TagIPAddress
	}
	return b, err
}
Beispiel #6
0
func (v *Gauge32) Marshal() ([]byte, error) {
	b, err := asn1.Marshal(int64(v.Value))
	if err == nil {
		b[0] = asn1.TagGauge32
	}
	return b, err
}
Beispiel #7
0
func encrypt(msg *MessageV3, proto PrivProtocol, key []byte) (err error) {
	var dst, priv []byte
	src := msg.PduBytes()

	switch proto {
	case Des:
		dst, priv, err = EncryptDES(src, key, int32(msg.AuthEngineBoots), genSalt32())
	case Aes:
		dst, priv, err = EncryptAES(
			src, key, int32(msg.AuthEngineBoots), int32(msg.AuthEngineTime), genSalt64())
	default:
		err = errors.New("'" + fmt.Sprint(proto) + "' is unsupported crypto.")
	}
	if err != nil {
		return
	}

	raw := asn1.RawValue{Class: asn1.ClassUniversal, Tag: asn1.TagOctetString, IsCompound: false}
	raw.Bytes = dst
	dst, err = asn1.Marshal(raw)
	if err == nil {
		msg.SetPduBytes(dst)
		msg.PrivParameter = priv
	}
	return
}
Beispiel #8
0
func (v *Counter64) Marshal() ([]byte, error) {
	i := big.NewInt(0).SetUint64(v.Value)
	b, err := asn1.Marshal(i)
	if err == nil {
		b[0] = asn1.TagCounter64
	}
	return b, err
}
Beispiel #9
0
func (msg *MessageV1) Marshal() (b []byte, err error) {
	var buf []byte
	raw := asn1.RawValue{Class: asn1.ClassUniversal, Tag: asn1.TagSequence, IsCompound: true}

	buf, err = asn1.Marshal(msg.version)
	if err != nil {
		return
	}
	raw.Bytes = append(raw.Bytes, buf...)

	buf, err = asn1.Marshal(msg.Community)
	if err != nil {
		return
	}
	raw.Bytes = append(raw.Bytes, buf...)

	raw.Bytes = append(raw.Bytes, msg.pduBytes...)
	return asn1.Marshal(raw)
}
Beispiel #10
0
func (v *VariableBinding) Marshal() (b []byte, err error) {
	var buf []byte
	raw := asn1.RawValue{Class: asn1.ClassUniversal, Tag: asn1.TagSequence, IsCompound: true}

	if v.Variable == nil {
		return asn1.Marshal(raw)
	}

	buf, err = v.Oid.Marshal()
	if err != nil {
		return
	}
	raw.Bytes = buf

	buf, err = v.Variable.Marshal()
	if err != nil {
		return
	}
	raw.Bytes = append(raw.Bytes, buf...)

	return asn1.Marshal(raw)
}
Beispiel #11
0
func (pdu *ScopedPdu) Marshal() (b []byte, err error) {
	var buf []byte
	raw := asn1.RawValue{Class: asn1.ClassUniversal, Tag: asn1.TagSequence, IsCompound: true}

	buf, err = asn1.Marshal(pdu.ContextEngineId)
	if err != nil {
		return
	}
	raw.Bytes = buf

	buf, err = asn1.Marshal(pdu.ContextName)
	if err != nil {
		return
	}
	raw.Bytes = append(raw.Bytes, buf...)

	buf, err = pdu.PduV1.Marshal()
	if err != nil {
		return
	}
	raw.Bytes = append(raw.Bytes, buf...)

	return asn1.Marshal(raw)
}
Beispiel #12
0
func (h *globalDataV3) Marshal() (b []byte, err error) {
	return asn1.Marshal(*h)
}
Beispiel #13
0
func (v *Oid) Marshal() ([]byte, error) {
	return asn1.Marshal(asn1.ObjectIdentifier(v.Value))
}
Beispiel #14
0
func (v *OctetString) Marshal() ([]byte, error) {
	return asn1.Marshal(v.Value)
}
Beispiel #15
0
func (v *Integer) Marshal() ([]byte, error) {
	return asn1.Marshal(v.Value)
}
Beispiel #16
0
func (pdu *PduV1) Marshal() (b []byte, err error) {
	var buf []byte
	raw := asn1.RawValue{Class: asn1.ClassContextSpecific, Tag: int(pdu.pduType), IsCompound: true}

	if Trap == pdu.pduType {
		buf, err = pdu.Enterprise.Marshal()
		if err != nil {
			return
		}
		raw.Bytes = buf

		buf, err = pdu.AgentAddress.Marshal()
		if err != nil {
			return
		}
		raw.Bytes = append(raw.Bytes, buf...)

		buf, err = asn1.Marshal(pdu.GenericTrap)
		if err != nil {
			return
		}
		raw.Bytes = append(raw.Bytes, buf...)

		buf, err = asn1.Marshal(pdu.SpecificTrap)
		if err != nil {
			return
		}
		raw.Bytes = append(raw.Bytes, buf...)

		buf, err = asn1.Marshal(pdu.Timestamp)
		if err != nil {
			return
		}
		raw.Bytes = append(raw.Bytes, buf...)
	} else {
		buf, err = asn1.Marshal(pdu.requestId)
		if err != nil {
			return
		}
		raw.Bytes = buf

		buf, err = asn1.Marshal(pdu.errorStatus)
		if err != nil {
			return
		}
		raw.Bytes = append(raw.Bytes, buf...)

		buf, err = asn1.Marshal(pdu.errorIndex)
		if err != nil {
			return
		}
		raw.Bytes = append(raw.Bytes, buf...)
	}

	VariableBindings := asn1.RawValue{Class: asn1.ClassUniversal, Tag: asn1.TagSequence, IsCompound: true}
	for i := 0; i < len(pdu.variableBindings); i++ {
		buf, err = pdu.variableBindings[i].Marshal()
		if err != nil {
			return
		}
		VariableBindings.Bytes = append(VariableBindings.Bytes, buf...)
	}

	buf, err = asn1.Marshal(VariableBindings)
	if err != nil {
		return
	}
	raw.Bytes = append(raw.Bytes, buf...)
	return asn1.Marshal(raw)
}