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 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 #3
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 #4
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 #5
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 #6
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 #7
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)
}