[RESOLVED] Conversion Problem
I need some help with a Crypto call. According to MSDN, the following C++ call sets the random string from the client:
Code:
Data.pbData = pClientRandom;
Data.cbData = cbClientRandom;
CryptSetKeyParam(
hMasterKey,
KP_CLIENT_RANDOM,
(PBYTE)&Data,
0);
Converting to VB, the function is declared as:
Code:
Private Declare Function CryptSetKeyParam Lib "advapi32.dll" (ByVal hKey As Long, ByVal dwParam As Long, ByVal pbData As Long, ByVal dwFlags As Long) As Long
The call:
Code:
CryptSetKeyParam(hMasterKey, KP_CLIENT_RANDOM, hClientRandom, 0)
returns an error. However, the call works when using KP_SCHANNEL_ALG (20) instead of KP_CLIENT_RANDOM (21) or KP_CLIENT_SERVER (22). According to Microsoft, the structure of "pbData" will vary, depending on the value of "dwParam". In the case of KP_CLIENT_RANDOM, "pbData" is a handle to a byte string containing 32 bytes of random data from the client followed by whatever cbClientRandom is. Some sources suggest that this is a BLOB structure, which means that cbClientRandom should be a 4 byte long integer (DWORD) containing the length of pClientRandom.
I have tried every combination I can think of, but the call always returns an error. Any help would be appreciated.
J.A. Coutts
Re: [RESOLVED] Conversion Problem
I think I know why the the order is reversed. In C++ code Microsoft lists it as:
Data.pbData = pClientRandom 'Handle to byte string
Data.cbData = cbClientRandom 'handle to length of byte string
In order to get the call to work, we have to reverse the order:
Data.cbData = cbClientRandom 'handle to length of byte string
Data.pbData = pClientRandom 'Handle to byte string
When I export a BLOB, I have to reverse the order of the resulting byte string to get the correct value, and I assume that the Microsoft compliler automatically handles the reversal by declaring it as a BLOB structure. Does this mean that I should reverse the string itself before sending it to the call? At this point in time I have no idea if Secure Channel is getting the correct information or not, as I have not tested the complete code yet.
J.A. Coutts
Re: [RESOLVED] Conversion Problem
Quote:
Originally Posted by
couttsj
I think I know why the the order is reversed. In C++ code Microsoft lists it as:
Data.pbData = pClientRandom 'Handle to byte string
Data.cbData = cbClientRandom 'handle to length of byte string
In order to get the call to work, we have to reverse the order:
Data.cbData = cbClientRandom 'handle to length of byte string
Data.pbData = pClientRandom 'Handle to byte string
I doubt the order of assignment has any effect. However, the CRYPT_DATA_BLOB Type declaration must conform to the documentation.
Quote:
Originally Posted by
couttsj
Does this mean that I should reverse the string itself before sending it to the call?
Sorry, but I don't know the answer to that. I haven't messed that much with the Crypto API. Testing should clear things up though.
Re: [RESOLVED] Conversion Problem
Of course you are correct Bonnie. I assumed that the order they were listed was the order in which they were defined (one should never assume). When I checked the Wincrypt.h file, I find the type definition has them in the correct order, along with many alias names.
Code:
typedef struct _CRYPTOAPI_BLOB {
DWORD cbData;
BYTE* pbData;
} CRYPT_INTEGER_BLOB, *PCRYPT_INTEGER_BLOB,
CRYPT_UINT_BLOB, *PCRYPT_UINT_BLOB,
CRYPT_OBJID_BLOB, *PCRYPT_OBJID_BLOB,
CERT_NAME_BLOB, *PCERT_NAME_BLOB,
CERT_RDN_VALUE_BLOB,*PCERT_RDN_VALUE_BLOB,
CERT_BLOB, *PCERT_BLOB,
CRL_BLOB, *PCRL_BLOB,
DATA_BLOB, *PDATA_BLOB,
CRYPT_DATA_BLOB, *PCRYPT_DATA_BLOB,
CRYPT_HASH_BLOB, *PCRYPT_HASH_BLOB,
CRYPT_DIGEST_BLOB, *PCRYPT_DIGEST_BLOB,
CRYPT_DER_BLOB, *PCRYPT_DER_BLOB,
CRYPT_ATTR_BLOB, *PCRYPT_ATTR_BLOB;
I don't know of any easy way of testing TLS handshakes. The very nature of the beast means that each session is different. I have used a packet sniffer to capture various sessions, but I cannot even duplicate these because I do not know the random string that the client used. It was sent encrypted using the Server Certificate Public key, and only the Private key can be used to decrypt it.
J.A. Coutts
Re: [RESOLVED] Conversion Problem
Quote:
Originally Posted by
couttsj
I don't know of any easy way of testing TLS handshakes. The very nature of the beast means that each session is different. I have used a packet sniffer to capture various sessions, but I cannot even duplicate these because I do not know the random string that the client used. It was sent encrypted using the Server Certificate Public key, and only the Private key can be used to decrypt it.
I understand now what we're dealing with! Previously, I had no idea of what you were talking about, so I apologize for my earlier statement. Well now, it looks like we can only rely on whatever documentation is available. Unfortunately, I have only scratched the surface of the Crypto API; again I apologize if I can't be of much help!