📄 snmpusm.c
字号:
DEBUGMSGTL(("usm", "Encryption successful.\n")); free(ciphertext); } else { /* * theSecLevel != SNMP_SEC_LEVEL_AUTHPRIV */ } /* * Start encoding the msgSecurityParameters. */ sp_offset = *offset; DEBUGDUMPHEADER("send", "msgPrivacyParameters"); /* * msgPrivacyParameters (warning: assumes DES salt). */ rc = asn_realloc_rbuild_string(wholeMsg, wholeMsgLen, offset, 1, (u_char) (ASN_UNIVERSAL | ASN_PRIMITIVE | ASN_OCTET_STR), iv, save_salt_length); DEBUGINDENTLESS(); if (rc == 0) { DEBUGMSGTL(("usm", "building privParams failed.\n")); usm_free_usmStateReference(secStateRef); return SNMPERR_TOO_LONG; } DEBUGDUMPHEADER("send", "msgAuthenticationParameters"); /* * msgAuthenticationParameters (warnings assumes 0x00 by 12). */ if (theSecLevel == SNMP_SEC_LEVEL_AUTHNOPRIV || theSecLevel == SNMP_SEC_LEVEL_AUTHPRIV) { memset(authParams, 0, USM_MD5_AND_SHA_AUTH_LEN); msgAuthParmLen = USM_MD5_AND_SHA_AUTH_LEN; } rc = asn_realloc_rbuild_string(wholeMsg, wholeMsgLen, offset, 1, (u_char) (ASN_UNIVERSAL | ASN_PRIMITIVE | ASN_OCTET_STR), authParams, msgAuthParmLen); DEBUGINDENTLESS(); if (rc == 0) { DEBUGMSGTL(("usm", "building authParams failed.\n")); usm_free_usmStateReference(secStateRef); return SNMPERR_TOO_LONG; } /* * Remember where to put the actual HMAC we calculate later on. An * encoded OCTET STRING of length USM_MD5_AND_SHA_AUTH_LEN has an ASN.1 * header of length 2, hence the fudge factor. */ mac_offset = *offset - 2; /* * msgUserName. */ DEBUGDUMPHEADER("send", "msgUserName"); rc = asn_realloc_rbuild_string(wholeMsg, wholeMsgLen, offset, 1, (u_char) (ASN_UNIVERSAL | ASN_PRIMITIVE | ASN_OCTET_STR), (u_char *) theName, theNameLength); DEBUGINDENTLESS(); if (rc == 0) { DEBUGMSGTL(("usm", "building authParams failed.\n")); usm_free_usmStateReference(secStateRef); return SNMPERR_TOO_LONG; } /* * msgAuthoritativeEngineTime. */ DEBUGDUMPHEADER("send", "msgAuthoritativeEngineTime"); rc = asn_realloc_rbuild_int(wholeMsg, wholeMsgLen, offset, 1, (u_char) (ASN_UNIVERSAL | ASN_PRIMITIVE | ASN_INTEGER), &time_long, sizeof(long)); DEBUGINDENTLESS(); if (rc == 0) { DEBUGMSGTL(("usm", "building msgAuthoritativeEngineTime failed.\n")); usm_free_usmStateReference(secStateRef); return SNMPERR_TOO_LONG; } /* * msgAuthoritativeEngineBoots. */ DEBUGDUMPHEADER("send", "msgAuthoritativeEngineBoots"); rc = asn_realloc_rbuild_int(wholeMsg, wholeMsgLen, offset, 1, (u_char) (ASN_UNIVERSAL | ASN_PRIMITIVE | ASN_INTEGER), &boots_long, sizeof(long)); DEBUGINDENTLESS(); if (rc == 0) { DEBUGMSGTL(("usm", "building msgAuthoritativeEngineBoots failed.\n")); usm_free_usmStateReference(secStateRef); return SNMPERR_TOO_LONG; } DEBUGDUMPHEADER("send", "msgAuthoritativeEngineID"); rc = asn_realloc_rbuild_string(wholeMsg, wholeMsgLen, offset, 1, (u_char) (ASN_UNIVERSAL | ASN_PRIMITIVE | ASN_OCTET_STR), theEngineID, theEngineIDLength); DEBUGINDENTLESS(); if (rc == 0) { DEBUGMSGTL(("usm", "building msgAuthoritativeEngineID failed.\n")); usm_free_usmStateReference(secStateRef); return SNMPERR_TOO_LONG; } /* * USM msgSecurityParameters sequence header */ rc = asn_realloc_rbuild_sequence(wholeMsg, wholeMsgLen, offset, 1, (u_char) (ASN_SEQUENCE | ASN_CONSTRUCTOR), *offset - sp_offset); if (rc == 0) { DEBUGMSGTL(("usm", "building usm security parameters failed.\n")); usm_free_usmStateReference(secStateRef); return SNMPERR_TOO_LONG; } /* * msgSecurityParameters OCTET STRING wrapper. */ rc = asn_realloc_rbuild_header(wholeMsg, wholeMsgLen, offset, 1, (u_char) (ASN_UNIVERSAL | ASN_PRIMITIVE | ASN_OCTET_STR), *offset - sp_offset); if (rc == 0) { DEBUGMSGTL(("usm", "building msgSecurityParameters failed.\n")); usm_free_usmStateReference(secStateRef); return SNMPERR_TOO_LONG; } /* * Copy in the msgGlobalData and msgVersion. */ while ((*wholeMsgLen - *offset) < globalDataLen) { if (!asn_realloc(wholeMsg, wholeMsgLen)) { DEBUGMSGTL(("usm", "building global data failed.\n")); usm_free_usmStateReference(secStateRef); return SNMPERR_TOO_LONG; } } *offset += globalDataLen; memcpy(*wholeMsg + *wholeMsgLen - *offset, globalData, globalDataLen); /* * Total packet sequence. */ rc = asn_realloc_rbuild_sequence(wholeMsg, wholeMsgLen, offset, 1, (u_char) (ASN_SEQUENCE | ASN_CONSTRUCTOR), *offset); if (rc == 0) { DEBUGMSGTL(("usm", "building master packet sequence failed.\n")); usm_free_usmStateReference(secStateRef); return SNMPERR_TOO_LONG; } /* * Now consider / do authentication. */ if (theSecLevel == SNMP_SEC_LEVEL_AUTHNOPRIV || theSecLevel == SNMP_SEC_LEVEL_AUTHPRIV) { size_t temp_sig_len = msgAuthParmLen; u_char *temp_sig = (u_char *) malloc(temp_sig_len); u_char *proto_msg = *wholeMsg + *wholeMsgLen - *offset; size_t proto_msg_len = *offset; if (temp_sig == NULL) { DEBUGMSGTL(("usm", "Out of memory.\n")); usm_free_usmStateReference(secStateRef); return SNMPERR_USM_GENERICERROR; } if (sc_generate_keyed_hash(theAuthProtocol, theAuthProtocolLength, theAuthKey, theAuthKeyLength, proto_msg, proto_msg_len, temp_sig, &temp_sig_len) != SNMP_ERR_NOERROR) { SNMP_FREE(temp_sig); DEBUGMSGTL(("usm", "Signing failed.\n")); usm_free_usmStateReference(secStateRef); return SNMPERR_USM_AUTHENTICATIONFAILURE; } if (temp_sig_len != msgAuthParmLen) { SNMP_FREE(temp_sig); DEBUGMSGTL(("usm", "Signing lengths failed.\n")); usm_free_usmStateReference(secStateRef); return SNMPERR_USM_AUTHENTICATIONFAILURE; } memcpy(*wholeMsg + *wholeMsgLen - mac_offset, temp_sig, msgAuthParmLen); SNMP_FREE(temp_sig); } /* * endif -- create keyed hash */ usm_free_usmStateReference(secStateRef); DEBUGMSGTL(("usm", "USM processing completed.\n")); return SNMPERR_SUCCESS;} /* end usm_rgenerate_out_msg() */#endif /* *//*******************************************************************-o-****** * usm_parse_security_parameters * * Parameters: * (See list below...) * * Returns: * 0 On success, * -1 Otherwise. * * tab stop 4 * * Extracts values from the security header and data portions of the * incoming buffer. */intusm_parse_security_parameters(u_char * secParams, size_t remaining, u_char * secEngineID, size_t * secEngineIDLen, u_int * boots_uint, u_int * time_uint, char *secName, size_t * secNameLen, u_char * signature, size_t * signature_length, u_char * salt, size_t * salt_length, u_char ** data_ptr){ u_char *parse_ptr = secParams; u_char *value_ptr; u_char *next_ptr; u_char type_value; size_t octet_string_length = remaining; size_t sequence_length; size_t remaining_bytes; long boots_long; long time_long; u_int origNameLen; /* * Eat the first octet header. */ if ((value_ptr = asn_parse_sequence(parse_ptr, &octet_string_length, &type_value, (ASN_UNIVERSAL | ASN_PRIMITIVE | ASN_OCTET_STR), "usm first octet")) == NULL) { /* * RETURN parse error */ return -1; } /* * Eat the sequence header. */ parse_ptr = value_ptr; sequence_length = octet_string_length; if ((value_ptr = asn_parse_sequence(parse_ptr, &sequence_length, &type_value, (ASN_SEQUENCE | ASN_CONSTRUCTOR), "usm sequence")) == NULL) { /* * RETURN parse error */ return -1; } /* * Retrieve the engineID. */ parse_ptr = value_ptr; remaining_bytes = sequence_length; DEBUGDUMPHEADER("recv", "msgAuthoritativeEngineID"); if ((next_ptr = asn_parse_string(parse_ptr, &remaining_bytes, &type_value, secEngineID, secEngineIDLen)) == NULL) { DEBUGINDENTLESS(); /* * RETURN parse error */ return -1; } DEBUGINDENTLESS(); if (type_value != (u_char) (ASN_UNIVERSAL | ASN_PRIMITIVE | ASN_OCTET_STR)) { /* * RETURN parse error */ return -1; } /* * Retrieve the engine boots, notice switch in the way next_ptr and * remaining_bytes are used (to accomodate the asn code). */ DEBUGDUMPHEADER("recv", "msgAuthoritativeEngineBoots"); if ((next_ptr = asn_parse_int(next_ptr, &remaining_bytes, &type_value, &boots_long, sizeof(long))) == NULL) { DEBUGINDENTLESS(); /* * RETURN parse error */ return -1; } DEBUGINDENTLESS(); if (type_value != (u_char) (ASN_UNIVERSAL | ASN_PRIMITIVE | ASN_INTEGER)) { DEBUGINDENTLESS(); /* * RETURN parse error */ return -1; } *boots_uint = (u_int) boots_long; /* * Retrieve the time value. */ DEBUGDUMPHEADER("recv", "msgAuthoritativeEngineTime"); if ((next_ptr = asn_parse_int(next_ptr, &remaining_bytes, &type_value, &time_long, sizeof(long))) == NULL) { /* * RETURN parse error */ return -1; } DEBUGINDENTLESS(); if (type_value != (u_char) (ASN_UNIVERSAL | ASN_PRIMITIVE | ASN_INTEGER)) { /* * RETURN parse error */ return -1; } *time_uint = (u_int) time_long; /* * Retrieve the secName. */ origNameLen = *secNameLen; DEBUGDUMPHEADER("recv", "msgUserName"); if ((next_ptr = asn_parse_string(next_ptr, &remaining_bytes, &type_value, (u_char *) secName, secNameLen)) == NULL) { DEBUGINDENTLESS(); /* * RETURN parse error */ return -1;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -