wtls_statesupport.c
来自「mms client」· C语言 代码 · 共 874 行 · 第 1/2 页
C
874 行
/* * wtls_statesupport.c * * 2001 Nick Clarey, Yann Muller for 3G LAB */#include "gwlib/gwlib.h"#if (HAVE_WTLS_OPENSSL)#include <openssl/x509.h>#ifndef NO_RC5#include <openssl/rc5.h>#else#error "your OpenSSL installation lacks RC5 algorithm support"#endif#include "wtls_statesupport.h"#define BLOCKLENGTH 64#define INNERPAD 0x36#define OUTERPAD 0x5Cextern X509* x509_cert;extern RSA* private_key;extern KeyExchangeSuite client_key_exchange_algo;extern PublicKeyAlgorithm public_key_algo;extern SignatureAlgorithm signature_algo;Octstr* wtls_hmac_hash(Octstr* key, Octstr* data, WTLSMachine* wtls_machine);Octstr* wtls_hash(Octstr* inputData, WTLSMachine* wtls_machine);Octstr* wtls_encrypt_rc5(Octstr* data, WTLSMachine* wtls_machine);Octstr* wtls_decrypt_rc5(Octstr* encryptedData, WTLSMachine* wtls_machine);/* Add here the supported KeyExchangeSuites used by wtls_choose_clientkeyid */KeyExchangeSuite supportedKeyExSuite[] = { rsa_anon };Octstr* wtls_decrypt(Octstr* buffer, WTLSMachine* wtls_machine){ return wtls_decrypt_rc5(buffer,wtls_machine);}/* This function will convert our buffer into a completed GenericBlockCipher */Octstr* wtls_encrypt(Octstr* buffer, WTLSMachine* wtls_machine, int recordType){ Octstr* bufferCopy; Octstr* encryptedContent; Octstr* contentMac; Octstr* padding; Octstr* tempData; unsigned char* tempPadding; int paddingLength, contentLength, macSize, blockLength, sequenceNumber, bufferLength; int i; /* Copy our buffer */ bufferCopy = octstr_duplicate(buffer); /* Get the MAC of the content */ sequenceNumber = wtls_machine->server_seq_num; bufferLength = octstr_len(buffer); /* Copy the buffer in preparation for MAC calculation */ tempData = octstr_create(""); pack_int16(tempData, 0, sequenceNumber); octstr_append_char(tempData, recordType); pack_int16(tempData, octstr_len(tempData), bufferLength); octstr_append(tempData, buffer); /* Calculate the MAC */ contentMac = wtls_hmac_hash(wtls_machine->server_write_MAC_secret, tempData ,wtls_machine); /* Calculate the padding length */ contentLength = octstr_len(bufferCopy); macSize = hash_table[wtls_machine->mac_algorithm].mac_size; blockLength = bulk_table[wtls_machine->bulk_cipher_algorithm].block_size; paddingLength = (contentLength + macSize + 1) % (blockLength); /* Append the MAC to the bufferCopy */ octstr_append(bufferCopy,contentMac); if (paddingLength > 0) { /* Pad with the paddingLength itself paddingLength times. Confused yet? */ tempPadding = gw_malloc(paddingLength); for (i=0;i < paddingLength; i++) { /* You're probably really spaced out around now... see section 9.2.3.3 for more details... */ tempPadding[i] = paddingLength; } octstr_append_data(bufferCopy, tempPadding, paddingLength); } /* Add the length byte */ octstr_append_char(bufferCopy, paddingLength); /* Encrypt the content */ encryptedContent = wtls_encrypt_rc5(bufferCopy,wtls_machine); return encryptedContent;}/* P_hash as described in WAP WTLS section 11.3.2 */Octstr* wtls_P_hash(Octstr* secret, Octstr* seed, int byteLength, WTLSMachine* wtls_machine){ Octstr *a; Octstr *aPrev; Octstr *aPlusSeed; Octstr *hashTemp; Octstr *hashedData; hashedData = octstr_create(""); /* start with A(1) = HMAC_hash(secret, seed) */ aPrev = octstr_duplicate(seed); do { /* A(i) */ a = wtls_hmac_hash(secret, aPrev, wtls_machine); aPlusSeed = octstr_cat(a, seed); /* HMAC */ hashTemp = wtls_hmac_hash(secret, aPlusSeed, wtls_machine); octstr_append(hashedData, hashTemp); octstr_destroy(hashTemp); /* Update a(i-1) */ octstr_destroy(aPrev); aPrev = a; } while(octstr_len(hashedData) < byteLength); gw_free(aPlusSeed); return hashedData;}/* Pseudo Random Function (PRF) as described in WAP WTLS section 11.3.2 */Octstr* wtls_calculate_prf(Octstr* secret, Octstr* label, Octstr* seed, int byteLength, WTLSMachine* wtls_machine){ Octstr* returnOctstr; Octstr *labelPlusSeed; /* Create label + seed */ labelPlusSeed = octstr_cat(label, seed); /* PRF(secret, label, seed) = P_hash(secret, label + seed) */ returnOctstr = wtls_P_hash(secret, labelPlusSeed, byteLength, wtls_machine); /* Return the first nbytes of the hashed data */ octstr_truncate(returnOctstr, byteLength); gw_free(labelPlusSeed); return returnOctstr; }/* MAC calculation */Octstr* wtls_hmac_hash(Octstr* key, Octstr* data, WTLSMachine* wtls_machine){ static unsigned char final_mac[1024]; unsigned char *mac, *buffer, *keyString; int mac_len, bufferlen, keylen; Octstr *returnOctstr; buffer = octstr_get_cstr(data); bufferlen = octstr_len(data); keyString = octstr_get_cstr(key); keylen = octstr_len(key); mac = final_mac; switch (wtls_machine->mac_algorithm) { case SHA_0: /* no keyed MAC is calculated */ /* So what do we return ? */ break; case SHA_40: case SHA_80: case SHA_NOLIMIT: HMAC(EVP_sha1(), keyString, keylen, buffer, bufferlen, mac, &mac_len); break; case SHA_XOR_40: // dunno yet break; case MD5_40: case MD5_80: case MD5_NOLIMIT: HMAC(EVP_md5(), keyString, keylen, buffer, bufferlen, mac, &mac_len); break; } returnOctstr = octstr_create_from_data(mac, mac_len);}/* Not to be confused with octstr_hash, this applies the currently set hashing algorithm from wtls_machine to the supplied input data, returning a hashed Octstr. If it fails, it will return a NULL pointer */Octstr* wtls_hash(Octstr* inputData, WTLSMachine* wtls_machine){ int inputDataLength; int outputDataLength; unsigned char* outputDataTemp; unsigned char* inputDataTemp; unsigned char* tempPointer; Octstr* outputData; inputDataLength = octstr_len(inputData); outputDataLength = hash_table[wtls_machine->mac_algorithm].key_size; inputDataTemp = gw_malloc(inputDataLength); outputDataTemp = gw_malloc(outputDataLength); /* Copy the contents of inputData into inputDataTemp, ready for hashing */ tempPointer = octstr_get_cstr(inputData); memcpy((void*) inputDataTemp, (void*)tempPointer, inputDataLength); /* Hash away! */ // Here's where we need to hash on the selected algorithm, not just the SHA-1 algorithm //debug("wtls", 0, "mac algo %d", wtls_machine->mac_algorithm); switch (wtls_machine->mac_algorithm) { case SHA_0: /* no keyed MAC is calculated */ // So what do we return ? break; case SHA_40: case SHA_80: case SHA_NOLIMIT: tempPointer = SHA1(inputDataTemp, inputDataLength, outputDataTemp); break; case SHA_XOR_40: // dunno yet break; case MD5_40: case MD5_80: case MD5_NOLIMIT: tempPointer = MD5(inputDataTemp, inputDataLength, outputDataTemp); break; } if (tempPointer == NULL){ /* Pop out an error */ } /* Get our output data setup */ outputData = octstr_create_from_data(outputDataTemp,outputDataLength); /* some algorithms don't use the full length of H */ octstr_truncate(outputData, hash_table[wtls_machine->mac_algorithm].mac_size); /* Delete our allocated memory */ gw_free(outputDataTemp); gw_free(inputDataTemp); outputDataTemp = NULL; inputDataTemp = NULL; /* Return the outputData */ return outputData;}Octstr* wtls_decrypt_rc5(Octstr* data, WTLSMachine* wtls_machine){ Octstr* encryptedData; Octstr* decryptedData; Octstr* duplicatedIv; unsigned char* output; unsigned char* input; unsigned char* iv; unsigned char* keyData; int keyLen; int ivLen; int dataLen; RC5_32_KEY* key = NULL; ivLen = bulk_table[wtls_machine->bulk_cipher_algorithm].iv_size; duplicatedIv = octstr_duplicate(wtls_machine->client_write_IV); iv = octstr_get_cstr(duplicatedIv); keyLen = bulk_table[wtls_machine->bulk_cipher_algorithm].key_material; keyData = octstr_get_cstr(wtls_machine->client_write_enc_key); dataLen = octstr_len(data); input = octstr_get_cstr(data); key = gw_malloc (sizeof(RC5_32_KEY)); /* Key generation */ RC5_32_set_key(key, keyLen, keyData, RC5_16_ROUNDS); /* Malloc our output */ output = gw_malloc (dataLen); /* Encrypt the string */ debug("wtls_statesupport",0,"About to decrypt: dataLen = %d, iv = %x", dataLen, iv); octstr_dump(data,0); RC5_32_cbc_encrypt(input, output, dataLen, key, iv, RC5_DECRYPT); debug("wtls_statesupport",0,"Decrypted"); decryptedData = octstr_create_from_data(output, dataLen); octstr_dump(decryptedData,0); /* Encrypt it just to test */ gw_free(output); output = NULL; output = gw_malloc (dataLen); /* Ensure that we preserve the iv */ octstr_destroy(duplicatedIv); duplicatedIv = octstr_duplicate(wtls_machine->client_write_IV); iv = octstr_get_cstr(duplicatedIv); octstr_get_many_chars(iv, wtls_machine->client_write_IV,0,ivLen); input = octstr_get_cstr(decryptedData); RC5_32_cbc_encrypt(input, output, dataLen, key, iv, RC5_ENCRYPT); encryptedData = octstr_create_from_data(output, dataLen); gw_free(output); output = NULL; octstr_destroy(duplicatedIv); return decryptedData;}Octstr* wtls_encrypt_rc5(Octstr* data, WTLSMachine* wtls_machine){ Octstr* encryptedData; Octstr* decryptedData; Octstr* duplicatedIv; unsigned char* output; unsigned char* input; unsigned char* iv; unsigned char* keyData; int keyLen; int ivLen; int dataLen; RC5_32_KEY* key = NULL; ivLen = bulk_table[wtls_machine->bulk_cipher_algorithm].iv_size; duplicatedIv = octstr_duplicate(wtls_machine->server_write_IV); iv = octstr_get_cstr(duplicatedIv); keyLen = bulk_table[wtls_machine->bulk_cipher_algorithm].key_material; keyData = octstr_get_cstr(wtls_machine->server_write_enc_key); dataLen = octstr_len(data); input = octstr_get_cstr(data); key = gw_malloc (sizeof(RC5_32_KEY)); /* Key generation */ debug("wtls_statesupport",0,"Key generation"); RC5_32_set_key(key, keyLen, keyData, RC5_16_ROUNDS); /* Malloc our output */ output = gw_malloc (dataLen); /* Encrypt the string */ RC5_32_cbc_encrypt(input, output, dataLen, key, iv, RC5_ENCRYPT); encryptedData = octstr_create_from_data(output, dataLen); /* Decrypt it just to test */ gw_free(output); output = NULL; output = gw_malloc (dataLen); /* Ensure that we preserve the iv */ octstr_destroy(duplicatedIv); duplicatedIv = octstr_duplicate(wtls_machine->server_write_IV); iv = octstr_get_cstr(duplicatedIv); octstr_get_many_chars(iv, wtls_machine->server_write_IV,0,ivLen); input = octstr_get_cstr(encryptedData); RC5_32_cbc_encrypt(input, output, dataLen, key, iv, RC5_DECRYPT); decryptedData = octstr_create_from_data(output, dataLen); gw_free(output); output = NULL; octstr_destroy(duplicatedIv); return encryptedData;}Octstr* wtls_decrypt_rsa(Octstr* encryptedData){ int numBytesWritten=0,numBytesToRead=0; Octstr *decryptedData=0; unsigned char* tempDecryptionBuffer=0; char* tempEncryptionPointer=0; /* Allocate some memory for our decryption buffer */ tempDecryptionBuffer = gw_malloc(RSA_size(private_key)); /* Calculate the number of bytes to read from encryptedData when decrypting */ numBytesToRead = octstr_len(encryptedData); /* Don't write to this pointer. Ever ever ever. */ tempEncryptionPointer = octstr_get_cstr(encryptedData); /* Decrypt the data in encryptedData */ numBytesWritten = RSA_private_decrypt(numBytesToRead, tempEncryptionPointer, tempDecryptionBuffer, private_key, RSA_PKCS1_PADDING); if(numBytesWritten == -1) { tempEncryptionPointer += 2; numBytesToRead -= 2; numBytesWritten = RSA_private_decrypt(numBytesToRead, tempEncryptionPointer, tempDecryptionBuffer, private_key, RSA_PKCS1_PADDING); } /* Move the tempDecryptionBuffer to an Octstr */ decryptedData = octstr_create_from_data(tempDecryptionBuffer,numBytesWritten); /* Deallocate the tempDecryptionBuffer */ gw_free(tempDecryptionBuffer); tempDecryptionBuffer = NULL; debug("wtls",0, "Decrypted secret"); octstr_dump( decryptedData, 0); /* Return the decrypted data */ return decryptedData;}void wtls_decrypt_pdu_list(WTLSMachine *wtls_machine, List *pdu_list){ int i, listlen; Octstr* decryptedData = NULL; wtls_Payload *payload; listlen = list_len(pdu_list); for( i=0; i<listlen; i++) { payload = (wtls_Payload *)list_get(pdu_list, i); if(payload->cipher) { debug("wtls", 0, "Decrypting PDU %d", i);
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?