wtls_statesupport.c
来自「mms client」· C语言 代码 · 共 874 行 · 第 1/2 页
C
874 行
decryptedData = wtls_decrypt(payload->data, wtls_machine); /* replace the data */ octstr_destroy(payload->data); payload->data = decryptedData; } else { debug("wtls", 0, "PDU %d is not encrypted.", i); } }}RSAPublicKey* wtls_get_rsapublickey(void){ RSA* rsaStructure=0; EVP_PKEY* publicKey=0; BIGNUM *modulus=0,*exponent=0; unsigned char* tempModulusStorage=0,*tempExponentStorage=0; int numbytes=0; RSAPublicKey* returnStructure=0; Octstr *octstrModulus=0, *octstrExponent=0; /* First, we need to extract the RSA structure from the X509 Cert */ /* Get the EVP_PKEY structure from the X509 cert */ publicKey = X509_PUBKEY_get(x509_cert->cert_info->key); /* Take said EVP_PKEY structure and get the RSA component */ if (EVP_PKEY_type(publicKey->type) != EVP_PKEY_RSA) { return NULL; } else { rsaStructure = publicKey->pkey.rsa; } /* Then we need to grab the exponent component from the cert */ exponent = rsaStructure->e; /* We need to allocate sufficient memory to hold the exponent */ numbytes = BN_num_bytes(exponent); tempExponentStorage = gw_malloc(numbytes); /* Then we get the exponent */ numbytes = BN_bn2bin(exponent, tempExponentStorage); /* And finally we convert the exponent to an Octstr */ octstrExponent = octstr_create_from_data(tempExponentStorage,numbytes); /* Then we need to grab the modulus component from the cert */ modulus = rsaStructure->n; /* We need to allocate sufficient memory to hold the modulus */ numbytes = BN_num_bytes(modulus); tempModulusStorage = gw_malloc(numbytes); /* Then we get the modulus */ numbytes = BN_bn2bin(modulus, tempModulusStorage); /* And finally we convert the modulus to an Octstr */ octstrModulus = octstr_create_from_data(tempModulusStorage,numbytes); /* Put the components into our return structure */ returnStructure = gw_malloc(sizeof(RSAPublicKey)); returnStructure->rsa_exponent = octstrExponent; returnStructure->rsa_modulus = octstrModulus; /* And deallocate the memory allocated for holding the modulus */ gw_free(tempModulusStorage); gw_free(tempExponentStorage); tempModulusStorage = NULL; tempExponentStorage = NULL; return returnStructure;}Octstr* wtls_get_certificate(void){ unsigned char** pp; unsigned char* ppStart; int amountWritten = 1260; Octstr* returnOctstr; debug("wtls_get_certificate",0,"x509_cert : %x", x509_cert); /* Convert the x509 certificate to DER-encoding */ amountWritten =i2d_X509(x509_cert, NULL); debug("wtls_get_certificate",0,"amountWritten : %d", amountWritten); /* Allocate some memory for *pp */ pp = (unsigned char**) gw_malloc(sizeof(unsigned char**)); /* Allocate the memory and call the same function again?!!? What an original idea :-/ */ ppStart = (unsigned char *) gw_malloc (sizeof(unsigned char)*amountWritten); debug("wtls_get_certificate",0,"x509_cert_DER_pre : %x", *pp); *pp = ppStart; amountWritten =i2d_X509(x509_cert, pp); /* And we do this, because otherwise *pp is pointing to the end of the buffer. Yay */ *pp = ppStart; debug("wtls_get_certificate",0,"x509_cert_DER_post : %x", *pp); /* Convert the DER-encoded char string to an octstr */ returnOctstr = octstr_create_from_data(*pp,amountWritten); /* Destroy the memory allocated temporarily above */ gw_free(*pp); *pp = NULL; /* Destroy the memory allocated for pp as well */ gw_free(pp); pp = NULL; /* Return the octstr */ return returnOctstr;}/* Chooses a CipherSuite from the list provided by the client. Returns NULL if none is acceptable. */CipherSuite* wtls_choose_ciphersuite(List* ciphersuites) { CipherSuite* returnSuite = NULL; CipherSuite* currentCS = NULL; int i = 0; int listLen; listLen = list_len(ciphersuites); //returnSuite = gw_malloc(sizeof(CipherSuite)); /* the first CS in the list */ do { /* the next CS in the list */ currentCS = list_get(ciphersuites, i); /* Check if we support this BulkCipher */ if(currentCS->bulk_cipher_algo >= RC5_CBC_40 && currentCS->bulk_cipher_algo <= IDEA_CBC) { /* Check if we support this MAC algsorithm */ if(currentCS->mac_algo >= SHA_0 && currentCS->mac_algo <= MD5_NOLIMIT) { /* We can use this CipherSuite then */ returnSuite = currentCS; } } i++; } while(returnSuite == NULL && i < listLen); return returnSuite;}int isSupportedKeyEx(int keyExId) { int maxSupported; int i; int retCode = 0; maxSupported = sizeof(supportedKeyExSuite) / sizeof(KeyExchangeSuite); for(i = 0; i<maxSupported; i++) { if(keyExId == supportedKeyExSuite[i]) { retCode = 1; } } return retCode;}int wtls_choose_clientkeyid(List* clientKeyIds) { int returnKey = 0; KeyExchangeId *currentKeyId = NULL; int i = 0; int listLen; listLen = list_len(clientKeyIds); debug("wtls", 0, "listLen = %d", listLen); do { currentKeyId = list_get(clientKeyIds, i); debug("wtls", 0, "Key %d", i); dump_key_exchange_id("wtls", 0, currentKeyId); /* check if the current key suite is supported */ if(isSupportedKeyEx(currentKeyId->key_exchange_suite)) { returnKey = i+1; } i++; } while(returnKey == 0 && i < listLen); return returnKey;}int wtls_choose_snmode(int snmode){ return 2;}int wtls_choose_krefresh(int krefresh){ return 2;}Random* wtls_get_random(void){ Random* randomData; randomData = gw_malloc(sizeof(Random)); randomData->gmt_unix_time = 0x0000; /* Yeah, I know, it's not very random */ randomData->random_bytes = octstr_create("000000000000"); return randomData;}int clienthellos_are_identical (List* pdu_list, List* last_received_packet){ return 0;}int certifcateverifys_are_identical (List* pdu_list, List* last_received_packet){ return 0;}int certificates_are_identical (List* pdu_list, List* last_received_packet){ return 0;}int clientkeyexchanges_are_identical (List* pdu_list, List* last_received_packet){ return 0;}int changecipherspecs_are_identical (List* pdu_list, List* last_received_packet){ return 0;}int finisheds_are_indentical (List* pdu_list, List* last_received_packet){ return 0;}int packet_contains_changecipherspec (List* pdu_list){ return 0;}int packet_contains_finished (List* pdu_list){ return 0;}int packet_contains_optional_stuff (List* pdu_list){ return 0;}int packet_contains_userdata (List* pdu_list){ /* FIXME: need to check if it is really Userdata !! */ return 1;}int packet_contains_clienthello (List* pdu_list){ return 0;}int is_critical_alert (List* pdu_list){ return 0;}int is_warning_alert (List* pdu_list){ return 0;}/* go through the list of wtls_Payloads and add the data of any handshake message to wtls_machine->handshake_data */void add_all_handshake_data(WTLSMachine *wtls_machine, List *pdu_list){ long i, listlen; wtls_Payload *payload; gw_assert(pdu_list != NULL); listlen = list_len(pdu_list); debug("wtls", 0,"adding handshake data from %d PDU(s)", listlen); for(i=0; i<listlen; i++) { payload = (wtls_Payload *)list_get(pdu_list, i); if(payload->type == Handshake_PDU) { octstr_insert(wtls_machine->handshake_data, payload->data, octstr_len(wtls_machine->handshake_data)); debug("wtls", 0, "Data from PDU %d:", i); octstr_dump(payload->data, 2); } }}void calculate_server_key_block(WTLSMachine *wtls_machine){ Octstr* concatenatedRandoms=0; Octstr* labelMaster=0; Octstr* key_block; Octstr* final_server_write_enc_key = NULL; Octstr* final_server_write_IV = NULL; Octstr* emptySecret = NULL; /* Concatenate our random data */ concatenatedRandoms = octstr_create(""); pack_int16(concatenatedRandoms, 0, wtls_machine->server_seq_num); octstr_append(concatenatedRandoms, wtls_machine->server_random); octstr_append(concatenatedRandoms, wtls_machine->client_random); /* Calculate the key_block */ labelMaster = octstr_create("server expansion"); key_block = wtls_calculate_prf(wtls_machine->master_secret, labelMaster, concatenatedRandoms, hash_table[wtls_machine->mac_algorithm].key_size + bulk_table[wtls_machine->bulk_cipher_algorithm].key_material + bulk_table[wtls_machine->bulk_cipher_algorithm].iv_size, wtls_machine ); octstr_destroy(labelMaster); labelMaster = NULL; octstr_destroy(concatenatedRandoms); concatenatedRandoms = NULL; /* Break the key_block in its 3 parts */ wtls_machine->server_write_MAC_secret = octstr_copy(key_block, 0, hash_table[wtls_machine->mac_algorithm].key_size); octstr_delete(key_block, 0, hash_table[wtls_machine->mac_algorithm].key_size); wtls_machine->server_write_enc_key = octstr_copy(key_block, 0, bulk_table[wtls_machine->bulk_cipher_algorithm].key_material); octstr_delete(key_block, 0, bulk_table[wtls_machine->bulk_cipher_algorithm].key_material); wtls_machine->server_write_IV = octstr_copy(key_block, 0, bulk_table[wtls_machine->bulk_cipher_algorithm].iv_size); /* Additional calculations for exportable encryption algos */ if(bulk_table[wtls_machine->bulk_cipher_algorithm].is_exportable == EXPORTABLE) { concatenatedRandoms = octstr_cat(wtls_machine->client_random, wtls_machine->server_random); labelMaster = octstr_create("server write key"); final_server_write_enc_key = wtls_calculate_prf(wtls_machine->server_write_enc_key, labelMaster, concatenatedRandoms, bulk_table[wtls_machine->bulk_cipher_algorithm].key_material, wtls_machine); octstr_destroy(labelMaster); labelMaster = NULL; octstr_destroy(concatenatedRandoms); concatenatedRandoms = NULL; octstr_destroy(wtls_machine->server_write_enc_key); wtls_machine->server_write_enc_key = final_server_write_enc_key; final_server_write_enc_key = NULL; concatenatedRandoms = octstr_create(""); octstr_append_char(concatenatedRandoms, wtls_machine->server_seq_num); octstr_append(concatenatedRandoms, wtls_machine->client_random); octstr_append(concatenatedRandoms, wtls_machine->server_random); emptySecret = octstr_create(""); final_server_write_IV = wtls_calculate_prf(emptySecret, labelMaster, concatenatedRandoms, bulk_table[wtls_machine->bulk_cipher_algorithm].iv_size, wtls_machine); octstr_destroy(labelMaster); labelMaster = NULL; octstr_destroy(concatenatedRandoms); concatenatedRandoms = NULL; }}void calculate_client_key_block(WTLSMachine *wtls_machine) { Octstr* concatenatedRandoms=0; Octstr* key_block; Octstr* final_client_write_enc_key = NULL; Octstr* final_client_write_IV = NULL; Octstr* emptySecret = NULL; Octstr* labelMaster=0; /* Concatenate our random data */ concatenatedRandoms = octstr_create(""); pack_int16(concatenatedRandoms, 0,wtls_machine->client_seq_num); octstr_append(concatenatedRandoms, wtls_machine->server_random); octstr_append(concatenatedRandoms, wtls_machine->client_random); /* Calculate the key_block */ labelMaster = octstr_create("client expansion"); key_block = wtls_calculate_prf(wtls_machine->master_secret, labelMaster, concatenatedRandoms, hash_table[wtls_machine->mac_algorithm].key_size + bulk_table[wtls_machine->bulk_cipher_algorithm].key_material + bulk_table[wtls_machine->bulk_cipher_algorithm].iv_size, wtls_machine ); octstr_destroy(labelMaster); labelMaster = NULL; octstr_destroy(concatenatedRandoms); concatenatedRandoms = NULL; /* Break the key_block in its 3 parts */ wtls_machine->client_write_MAC_secret = octstr_copy(key_block, 0, hash_table[wtls_machine->mac_algorithm].key_size); octstr_delete(key_block, 0, hash_table[wtls_machine->mac_algorithm].key_size); wtls_machine->client_write_enc_key = octstr_copy(key_block, 0, bulk_table[wtls_machine->bulk_cipher_algorithm].key_material); octstr_delete(key_block, 0, bulk_table[wtls_machine->bulk_cipher_algorithm].key_material); wtls_machine->client_write_IV = octstr_copy(key_block, 0, bulk_table[wtls_machine->bulk_cipher_algorithm].iv_size); /* Additional calculations for exportable encryption algos */ if(bulk_table[wtls_machine->bulk_cipher_algorithm].is_exportable == EXPORTABLE) { concatenatedRandoms = octstr_cat(wtls_machine->client_random, wtls_machine->server_random); labelMaster = octstr_create("client write key"); final_client_write_enc_key = wtls_calculate_prf(wtls_machine->client_write_enc_key, labelMaster, concatenatedRandoms, bulk_table[wtls_machine->bulk_cipher_algorithm].key_material, wtls_machine); octstr_destroy(labelMaster); labelMaster = NULL; octstr_destroy(wtls_machine->client_write_enc_key); wtls_machine->client_write_enc_key = final_client_write_enc_key; final_client_write_enc_key = NULL; octstr_destroy(labelMaster); labelMaster = NULL; octstr_destroy(concatenatedRandoms); concatenatedRandoms = NULL; emptySecret = octstr_create(""); final_client_write_IV = wtls_calculate_prf(emptySecret, labelMaster, concatenatedRandoms, bulk_table[wtls_machine->bulk_cipher_algorithm].iv_size, wtls_machine); octstr_destroy(labelMaster); labelMaster = NULL; octstr_destroy(concatenatedRandoms); concatenatedRandoms = NULL; }}#endif
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?