| 1 |
// Copyright (c) Microsoft. All rights reserved. |
| 2 |
// Licensed under the MIT license. See LICENSE file in the project root for full license information. |
| 3 |
|
| 4 |
#include "windows.h" |
| 5 |
|
| 6 |
#include "azure_c_shared_utility/gballoc.h" |
| 7 |
#include "azure_c_shared_utility/x509_schannel.h" |
| 8 |
#include "azure_c_shared_utility/xlogging.h" |
| 9 |
#include "azure_c_shared_utility/safe_math.h" |
| 10 |
|
| 11 |
#if _MSC_VER > 1500 |
| 12 |
#include <ncrypt.h> |
| 13 |
#endif |
| 14 |
|
| 15 |
#define KEY_NAME L"AzureAliasKey" |
| 16 |
#define ECC_256_MAGIC_NUMBER 0x20 |
| 17 |
#define ECC_384_MAGIC_NUMBER 0x30 |
| 18 |
|
| 19 |
typedef enum x509_CERT_TYPE_TAG |
| 20 |
{ |
| 21 |
x509_TYPE_RSA, |
| 22 |
x509_TYPE_ECC, |
| 23 |
x509_TYPE_UNKNOWN, |
| 24 |
} x509_CERT_TYPE; |
| 25 |
|
| 26 |
typedef struct X509_SCHANNEL_HANDLE_DATA_TAG |
| 27 |
{ |
| 28 |
HCRYPTPROV hProv; |
| 29 |
HCRYPTKEY x509hcryptkey; |
| 30 |
PCCERT_CONTEXT x509certificate_context; |
| 31 |
x509_CERT_TYPE cert_type; |
| 32 |
} X509_SCHANNEL_HANDLE_DATA; |
| 33 |
|
| 34 |
static const char end_certificate_in_pem[] = "-----END CERTIFICATE-----"; |
| 35 |
static const size_t end_certificate_in_pem_length = sizeof(end_certificate_in_pem) - 1; |
| 36 |
static const char pem_crlf_value[] = "\r\n"; |
| 37 |
static const size_t pem_crlf_value_length = sizeof(pem_crlf_value) - 1; |
| 38 |
|
| 39 |
|
| 40 |
static unsigned char* convert_cert_to_binary(const char* crypt_value, DWORD crypt_value_in_len, DWORD* crypt_length) |
| 41 |
{ |
| 42 |
unsigned char* result = NULL; |
| 43 |
DWORD result_length = 0; |
| 44 |
if (!CryptStringToBinaryA(crypt_value, crypt_value_in_len, CRYPT_STRING_ANY, NULL, &result_length, NULL, NULL)) |
| 45 |
{ |
| 46 |
/*Codes_SRS_X509_SCHANNEL_02_010: [ Otherwise, x509_schannel_create shall fail and return a NULL X509_SCHANNEL_HANDLE. ]*/ |
| 47 |
LogErrorWinHTTPWithGetLastErrorAsString("Failed determine crypt value size"); |
| 48 |
result = NULL; |
| 49 |
} |
| 50 |
else |
| 51 |
{ |
| 52 |
if ((result = (unsigned char*)malloc(result_length)) == NULL) |
| 53 |
{ |
| 54 |
/*Codes_SRS_X509_SCHANNEL_02_010: [ Otherwise, x509_schannel_create shall fail and return a NULL X509_SCHANNEL_HANDLE. ]*/ |
| 55 |
LogError("unable to allocate memory for crypt value"); |
| 56 |
} |
| 57 |
else |
| 58 |
{ |
| 59 |
if (!CryptStringToBinaryA(crypt_value, 0, CRYPT_STRING_ANY, result, &result_length, NULL, NULL)) |
| 60 |
{ |
| 61 |
/*Codes_SRS_X509_SCHANNEL_02_010: [ Otherwise, x509_schannel_create shall fail and return a NULL X509_SCHANNEL_HANDLE. ]*/ |
| 62 |
LogErrorWinHTTPWithGetLastErrorAsString("Failed convert crypt value to binary"); |
| 63 |
free(result); |
| 64 |
result = NULL; |
| 65 |
} |
| 66 |
else |
| 67 |
{ |
| 68 |
if (crypt_length != NULL) |
| 69 |
{ |
| 70 |
*crypt_length = result_length; |
| 71 |
} |
| 72 |
} |
| 73 |
} |
| 74 |
} |
| 75 |
return result; |
| 76 |
} |
| 77 |
|
| 78 |
static unsigned char* decode_crypt_object(unsigned char* private_key, DWORD key_length, DWORD* blob_size, x509_CERT_TYPE* cert_type) |
| 79 |
{ |
| 80 |
unsigned char* result; |
| 81 |
|
| 82 |
#ifdef _MSC_VER |
| 83 |
#pragma warning(push) |
| 84 |
#pragma warning(disable: 4306) |
| 85 |
#endif // _MSC_VER |
| 86 |
|
| 87 |
LPCSTR key_type = PKCS_RSA_PRIVATE_KEY; |
| 88 |
|
| 89 |
#ifdef _MSC_VER |
| 90 |
#pragma warning(pop) |
| 91 |
#endif // _MSC_VER |
| 92 |
|
| 93 |
DWORD private_key_blob_size = 0; |
| 94 |
|
| 95 |
/*Codes_SRS_X509_SCHANNEL_02_004: [ x509_schannel_create shall decode the private key by calling CryptDecodeObjectEx. ]*/ |
| 96 |
if (!CryptDecodeObjectEx(X509_ASN_ENCODING | PKCS_7_ASN_ENCODING, key_type, private_key, key_length, 0, NULL, NULL, &private_key_blob_size)) |
| 97 |
{ |
| 98 |
#if _MSC_VER > 1500 |
| 99 |
key_type = X509_ECC_PRIVATE_KEY; |
| 100 |
if (!CryptDecodeObjectEx(X509_ASN_ENCODING | PKCS_7_ASN_ENCODING, key_type, private_key, key_length, 0, NULL, NULL, &private_key_blob_size)) |
| 101 |
{ |
| 102 |
/*Codes_SRS_X509_SCHANNEL_02_010: [ Otherwise, x509_schannel_create shall fail and return a NULL X509_SCHANNEL_HANDLE. ]*/ |
| 103 |
LogErrorWinHTTPWithGetLastErrorAsString("Failed to CryptDecodeObjectEx x509 private key"); |
| 104 |
*cert_type = x509_TYPE_UNKNOWN; |
| 105 |
} |
| 106 |
else |
| 107 |
{ |
| 108 |
*cert_type = x509_TYPE_ECC; |
| 109 |
} |
| 110 |
#else |
| 111 |
/*Codes_SRS_X509_SCHANNEL_02_010: [ Otherwise, x509_schannel_create shall fail and return a NULL X509_SCHANNEL_HANDLE. ]*/ |
| 112 |
LogErrorWinHTTPWithGetLastErrorAsString("Failed to CryptDecodeObjectEx x509 private key"); |
| 113 |
*cert_type = x509_TYPE_UNKNOWN; |
| 114 |
#endif |
| 115 |
} |
| 116 |
else |
| 117 |
{ |
| 118 |
*cert_type = x509_TYPE_RSA; |
| 119 |
} |
| 120 |
|
| 121 |
if (*cert_type != x509_TYPE_UNKNOWN) |
| 122 |
{ |
| 123 |
if ((result = (unsigned char*)malloc(private_key_blob_size)) == NULL) |
| 124 |
{ |
| 125 |
/*Codes_SRS_X509_SCHANNEL_02_010: [ Otherwise, x509_schannel_create shall fail and return a NULL X509_SCHANNEL_HANDLE. ]*/ |
| 126 |
LogError("unable to malloc for x509 private key blob"); |
| 127 |
} |
| 128 |
else |
| 129 |
{ |
| 130 |
if (!CryptDecodeObjectEx(X509_ASN_ENCODING | PKCS_7_ASN_ENCODING, key_type, private_key, key_length, 0, NULL, result, &private_key_blob_size)) |
| 131 |
{ |
| 132 |
/*Codes_SRS_X509_SCHANNEL_02_010: [ Otherwise, x509_schannel_create shall fail and return a NULL X509_SCHANNEL_HANDLE. ]*/ |
| 133 |
LogErrorWinHTTPWithGetLastErrorAsString("Failed to CryptDecodeObjectEx x509 private key"); |
| 134 |
free(result); |
| 135 |
result = NULL; |
| 136 |
} |
| 137 |
else |
| 138 |
{ |
| 139 |
if (blob_size != NULL) |
| 140 |
{ |
| 141 |
*blob_size = private_key_blob_size; |
| 142 |
} |
| 143 |
} |
| 144 |
} |
| 145 |
} |
| 146 |
else |
| 147 |
{ |
| 148 |
result = NULL; |
| 149 |
} |
| 150 |
|
| 151 |
return result; |
| 152 |
} |
| 153 |
|
| 154 |
static int set_ecc_certificate_info(X509_SCHANNEL_HANDLE_DATA* x509_handle, unsigned char* x509privatekeyBlob) |
| 155 |
{ |
| 156 |
int result; |
| 157 |
#if _MSC_VER > 1500 |
| 158 |
BCRYPT_ECCKEY_BLOB* pKeyBlob; |
| 159 |
SECURITY_STATUS status; |
| 160 |
CRYPT_BIT_BLOB* pPubKeyBlob = &x509_handle->x509certificate_context->pCertInfo->SubjectPublicKeyInfo.PublicKey; |
| 161 |
CRYPT_ECC_PRIVATE_KEY_INFO* pPrivKeyInfo = (CRYPT_ECC_PRIVATE_KEY_INFO*)x509privatekeyBlob; |
| 162 |
DWORD pubSize = pPubKeyBlob->cbData - 1; |
| 163 |
DWORD privSize = pPrivKeyInfo->PrivateKey.cbData; |
| 164 |
size_t keyBlobSize = safe_add_size_t(safe_add_size_t(sizeof(BCRYPT_ECCKEY_BLOB), pubSize), privSize); |
| 165 |
BYTE* pubKeyBuf = pPubKeyBlob->pbData + 1; |
| 166 |
BYTE* privKeyBuf = pPrivKeyInfo->PrivateKey.pbData; |
| 167 |
|
| 168 |
if (keyBlobSize == SIZE_MAX || |
| 169 |
(pKeyBlob = (BCRYPT_ECCKEY_BLOB*)malloc(keyBlobSize)) == NULL) |
| 170 |
{ |
| 171 |
/*Codes_SRS_X509_SCHANNEL_02_010: [ Otherwise, x509_schannel_create shall fail and return a NULL X509_SCHANNEL_HANDLE. ]*/ |
| 172 |
LogError("Failed to malloc NCrypt private key blob, size:%zu", keyBlobSize); |
| 173 |
result = MU_FAILURE; |
| 174 |
} |
| 175 |
else |
| 176 |
{ |
| 177 |
pKeyBlob->dwMagic = privSize == ECC_256_MAGIC_NUMBER ? BCRYPT_ECDSA_PRIVATE_P256_MAGIC |
| 178 |
: privSize == ECC_384_MAGIC_NUMBER ? BCRYPT_ECDSA_PRIVATE_P384_MAGIC |
| 179 |
: BCRYPT_ECDSA_PRIVATE_P521_MAGIC; |
| 180 |
pKeyBlob->cbKey = privSize; |
| 181 |
memcpy((BYTE*)(pKeyBlob + 1), pubKeyBuf, pubSize); |
| 182 |
memcpy((BYTE*)(pKeyBlob + 1) + pubSize, privKeyBuf, privSize); |
| 183 |
|
| 184 |
/* Codes_SRS_X509_SCHANNEL_02_005: [ x509_schannel_create shall call CryptAcquireContext. ] */ |
| 185 |
/* at this moment, both the private key and the certificate are decoded for further usage */ |
| 186 |
/* NOTE: As no WinCrypt key storage provider supports ECC keys, NCrypt is used instead */ |
| 187 |
status = NCryptOpenStorageProvider(&x509_handle->hProv, MS_KEY_STORAGE_PROVIDER, 0); |
| 188 |
if (status != ERROR_SUCCESS) |
| 189 |
{ |
| 190 |
/* Codes_SRS_X509_SCHANNEL_02_010: [ Otherwise, x509_schannel_create shall fail and return a NULL X509_SCHANNEL_HANDLE. ]*/ |
| 191 |
LogError("NCryptOpenStorageProvider failed with error 0x%08X", status); |
| 192 |
result = MU_FAILURE; |
| 193 |
} |
| 194 |
else |
| 195 |
{ |
| 196 |
SECURITY_STATUS status2; |
| 197 |
NCryptBuffer ncBuf = { sizeof(KEY_NAME), NCRYPTBUFFER_PKCS_KEY_NAME, KEY_NAME }; |
| 198 |
NCryptBufferDesc ncBufDesc; |
| 199 |
ncBufDesc.ulVersion = 0; |
| 200 |
ncBufDesc.cBuffers = 1; |
| 201 |
ncBufDesc.pBuffers = &ncBuf; |
| 202 |
|
| 203 |
CRYPT_KEY_PROV_INFO keyProvInfo = { KEY_NAME, MS_KEY_STORAGE_PROVIDER, 0, 0, 0, NULL, 0 }; |
| 204 |
|
| 205 |
/*Codes_SRS_X509_SCHANNEL_02_006: [ x509_schannel_create shall import the private key by calling CryptImportKey. ] */ |
| 206 |
/*NOTE: As no WinCrypt key storage provider supports ECC keys, NCrypt is used instead*/ |
| 207 |
status = NCryptImportKey(x509_handle->hProv, 0, BCRYPT_ECCPRIVATE_BLOB, &ncBufDesc, &x509_handle->x509hcryptkey, (BYTE*)pKeyBlob, (DWORD)keyBlobSize, NCRYPT_OVERWRITE_KEY_FLAG); |
| 208 |
if (status == ERROR_SUCCESS) |
| 209 |
{ |
| 210 |
status2 = NCryptFreeObject(x509_handle->x509hcryptkey); |
| 211 |
if (status2 != ERROR_SUCCESS) |
| 212 |
{ |
| 213 |
LogError("NCryptFreeObject for key handle failed with error 0x%08X", status2); |
| 214 |
} |
| 215 |
else |
| 216 |
{ |
| 217 |
x509_handle->x509hcryptkey = 0; |
| 218 |
} |
| 219 |
} |
| 220 |
|
| 221 |
status2 = NCryptFreeObject(x509_handle->hProv); |
| 222 |
if (status2 != ERROR_SUCCESS) |
| 223 |
{ |
| 224 |
LogError("NCryptFreeObject for provider handle failed with error 0x%08X", status2); |
| 225 |
} |
| 226 |
else |
| 227 |
{ |
| 228 |
x509_handle->hProv = 0; |
| 229 |
} |
| 230 |
|
| 231 |
if (status != ERROR_SUCCESS) |
| 232 |
{ |
| 233 |
/*Codes_SRS_X509_SCHANNEL_02_010: [ Otherwise, x509_schannel_create shall fail and return a NULL X509_SCHANNEL_HANDLE. ]*/ |
| 234 |
LogError("NCryptImportKey failed with error 0x%08X", status); |
| 235 |
result = MU_FAILURE; |
| 236 |
} |
| 237 |
else if (!CertSetCertificateContextProperty(x509_handle->x509certificate_context, CERT_KEY_PROV_INFO_PROP_ID, 0, &keyProvInfo)) |
| 238 |
{ |
| 239 |
/*Codes_SRS_X509_SCHANNEL_02_010: [ Otherwise, x509_schannel_create shall fail and return a NULL X509_SCHANNEL_HANDLE. ]*/ |
| 240 |
LogErrorWinHTTPWithGetLastErrorAsString("CertSetCertificateContextProperty failed to set NCrypt key handle property"); |
| 241 |
result = MU_FAILURE; |
| 242 |
} |
| 243 |
else |
| 244 |
{ |
| 245 |
result = 0; |
| 246 |
} |
| 247 |
} |
| 248 |
free(pKeyBlob); |
| 249 |
} |
| 250 |
#else |
| 251 |
(void)x509_handle; |
| 252 |
(void)x509privatekeyBlob; |
| 253 |
LogError("SChannel ECC is not supported in this compliation"); |
| 254 |
result = MU_FAILURE; |
| 255 |
#endif |
| 256 |
return result; |
| 257 |
} |
| 258 |
|
| 259 |
static int set_rsa_certificate_info(X509_SCHANNEL_HANDLE_DATA* x509_handle, unsigned char* x509privatekeyBlob, DWORD x509privatekeyBlobSize) |
| 260 |
{ |
| 261 |
int result; |
| 262 |
/*Codes_SRS_X509_SCHANNEL_02_005: [ x509_schannel_create shall call CryptAcquireContext. ]*/ |
| 263 |
/*at this moment, both the private key and the certificate are decoded for further usage*/ |
| 264 |
if (!CryptAcquireContext(&(x509_handle->hProv), NULL, MS_ENH_RSA_AES_PROV, PROV_RSA_AES, CRYPT_VERIFYCONTEXT)) |
| 265 |
{ |
| 266 |
/*Codes_SRS_X509_SCHANNEL_02_010: [ Otherwise, x509_schannel_create shall fail and return a NULL X509_SCHANNEL_HANDLE. ]*/ |
| 267 |
LogErrorWinHTTPWithGetLastErrorAsString("CryptAcquireContext failed"); |
| 268 |
result = MU_FAILURE; |
| 269 |
} |
| 270 |
else |
| 271 |
{ |
| 272 |
/*Codes_SRS_X509_SCHANNEL_02_006: [ x509_schannel_create shall import the private key by calling CryptImportKey. ] */ |
| 273 |
if (!CryptImportKey(x509_handle->hProv, x509privatekeyBlob, x509privatekeyBlobSize, (HCRYPTKEY)NULL, 0, &(x509_handle->x509hcryptkey))) |
| 274 |
{ |
| 275 |
/*Codes_SRS_X509_SCHANNEL_02_010: [ Otherwise, x509_schannel_create shall fail and return a NULL X509_SCHANNEL_HANDLE. ]*/ |
| 276 |
LogErrorWinHTTPWithGetLastErrorAsString("CryptImportKey for private key failed"); |
| 277 |
if (!CryptReleaseContext(x509_handle->hProv, 0)) |
| 278 |
{ |
| 279 |
LogErrorWinHTTPWithGetLastErrorAsString("unable to CryptReleaseContext"); |
| 280 |
} |
| 281 |
result = MU_FAILURE; |
| 282 |
} |
| 283 |
else |
| 284 |
{ |
| 285 |
/*Codes_SRS_X509_SCHANNEL_02_008: [ x509_schannel_create shall call set the certificate private key by calling CertSetCertificateContextProperty. ]*/ |
| 286 |
if (!CertSetCertificateContextProperty(x509_handle->x509certificate_context, CERT_KEY_PROV_HANDLE_PROP_ID, 0, (void*)x509_handle->hProv)) |
| 287 |
{ |
| 288 |
/*Codes_SRS_X509_SCHANNEL_02_010: [ Otherwise, x509_schannel_create shall fail and return a NULL X509_SCHANNEL_HANDLE. ]*/ |
| 289 |
LogErrorWinHTTPWithGetLastErrorAsString("unable to CertSetCertificateContextProperty"); |
| 290 |
|
| 291 |
if (!CryptDestroyKey(x509_handle->x509hcryptkey)) |
| 292 |
{ |
| 293 |
LogErrorWinHTTPWithGetLastErrorAsString("unable to CryptDestroyKey"); |
| 294 |
} |
| 295 |
if (!CryptReleaseContext(x509_handle->hProv, 0)) |
| 296 |
{ |
| 297 |
LogErrorWinHTTPWithGetLastErrorAsString("unable to CryptReleaseContext"); |
| 298 |
} |
| 299 |
result = MU_FAILURE; |
| 300 |
} |
| 301 |
else |
| 302 |
{ |
| 303 |
result = 0; |
| 304 |
} |
| 305 |
} |
| 306 |
} |
| 307 |
return result; |
| 308 |
} |
| 309 |
|
| 310 |
X509_SCHANNEL_HANDLE x509_schannel_create(const char* x509certificate, const char* x509privatekey) |
| 311 |
{ |
| 312 |
X509_SCHANNEL_HANDLE_DATA* result; |
| 313 |
/*this is what happens with the x509 certificate and the x509 private key in this function*/ |
| 314 |
/* |
| 315 |
step 1: they are converted to binary form. |
| 316 |
step 1.1: the size of the binary form is computed |
| 317 |
step 1.2: the conversion happens |
| 318 |
step 2: the binary form is decoded |
| 319 |
step 2.1: the decoded form needed size is computed |
| 320 |
step 2.2: the decoded form is actually decoded |
| 321 |
step 3: a crypto provider is created |
| 322 |
step 4: the x509 private key is associated with the crypto provider |
| 323 |
step 5: a certificate context is created |
| 324 |
step 6: the certificate context is linked to the crypto provider |
| 325 |
*/ |
| 326 |
|
| 327 |
/*Codes_SRS_X509_SCHANNEL_02_001: [ If x509certificate or x509privatekey are NULL then x509_schannel_create shall fail and return NULL. ]*/ |
| 328 |
if ( |
| 329 |
(x509certificate == NULL) || |
| 330 |
(x509privatekey == NULL) |
| 331 |
) |
| 332 |
{ |
| 333 |
/*Codes_SRS_X509_SCHANNEL_02_010: [ Otherwise, x509_schannel_create shall fail and return a NULL X509_SCHANNEL_HANDLE. ]*/ |
| 334 |
LogError("invalid argument const char* x509certificate=%p, const char* x509privatekey=%p", x509certificate, x509privatekey); |
| 335 |
result = NULL; |
| 336 |
} |
| 337 |
else |
| 338 |
{ |
| 339 |
unsigned char* binaryx509Certificate; |
| 340 |
unsigned char* binaryx509privatekey; |
| 341 |
unsigned char* x509privatekeyBlob; |
| 342 |
DWORD binaryx509certificateSize; |
| 343 |
DWORD binaryx509privatekeySize; |
| 344 |
DWORD x509privatekeyBlobSize; |
| 345 |
|
| 346 |
result = (X509_SCHANNEL_HANDLE_DATA*)malloc(sizeof(X509_SCHANNEL_HANDLE_DATA)); |
| 347 |
if (result == NULL) |
| 348 |
{ |
| 349 |
LogError("unable to malloc"); |
| 350 |
/*Codes_SRS_X509_SCHANNEL_02_010: [ Otherwise, x509_schannel_create shall fail and return a NULL X509_SCHANNEL_HANDLE. ]*/ |
| 351 |
/*return as is*/ |
| 352 |
|
| 353 |
} |
| 354 |
else |
| 355 |
{ |
| 356 |
memset(result, 0, sizeof(X509_SCHANNEL_HANDLE_DATA)); |
| 357 |
/*Codes_SRS_X509_SCHANNEL_02_002: [ x509_schannel_create shall convert the certificate to binary form by calling CryptStringToBinaryA. ]*/ |
| 358 |
if ((binaryx509Certificate = convert_cert_to_binary(x509certificate, 0, &binaryx509certificateSize)) == NULL) |
| 359 |
{ |
| 360 |
LogError("Failure converting x509 certificate"); |
| 361 |
free(result); |
| 362 |
result = NULL; |
| 363 |
} |
| 364 |
/*Codes_SRS_X509_SCHANNEL_02_003: [ x509_schannel_create shall convert the private key to binary form by calling CryptStringToBinaryA. ]*/ |
| 365 |
/*at this moment x509 certificate is ready to be used in CertCreateCertificateContext*/ |
| 366 |
else if ((binaryx509privatekey = convert_cert_to_binary(x509privatekey, 0, &binaryx509privatekeySize)) == NULL) |
| 367 |
{ |
| 368 |
LogError("Failure converting x509 certificate"); |
| 369 |
free(binaryx509Certificate); |
| 370 |
free(result); |
| 371 |
result = NULL; |
| 372 |
} |
| 373 |
else if ((x509privatekeyBlob = decode_crypt_object(binaryx509privatekey, binaryx509privatekeySize, &x509privatekeyBlobSize, &result->cert_type)) == NULL) |
| 374 |
{ |
| 375 |
LogError("Failure decoding x509 private key"); |
| 376 |
free(binaryx509Certificate); |
| 377 |
free(binaryx509privatekey); |
| 378 |
free(result); |
| 379 |
result = NULL; |
| 380 |
} |
| 381 |
else |
| 382 |
{ |
| 383 |
/*Codes_SRS_X509_SCHANNEL_02_007: [ x509_schannel_create shall create a cerficate context by calling CertCreateCertificateContext. ]*/ |
| 384 |
PCCERT_CONTEXT cert_ctx = CertCreateCertificateContext(X509_ASN_ENCODING | PKCS_7_ASN_ENCODING, binaryx509Certificate, binaryx509certificateSize); |
| 385 |
if ((result->x509certificate_context = cert_ctx) == NULL) |
| 386 |
{ |
| 387 |
/*Codes_SRS_X509_SCHANNEL_02_010: [ Otherwise, x509_schannel_create shall fail and return a NULL X509_SCHANNEL_HANDLE. ]*/ |
| 388 |
LogErrorWinHTTPWithGetLastErrorAsString("unable to CertCreateCertificateContext"); |
| 389 |
free(result); |
| 390 |
result = NULL; |
| 391 |
} |
| 392 |
else |
| 393 |
{ |
| 394 |
/* Codes_SRS_X509_SCHANNEL_07_001: [ x509_schannel_create shall determine whether the certificate is of type RSA or ECC. ] */ |
| 395 |
if (result->cert_type == x509_TYPE_RSA) |
| 396 |
{ |
| 397 |
if (set_rsa_certificate_info(result, x509privatekeyBlob, x509privatekeyBlobSize) != 0) |
| 398 |
{ |
| 399 |
(void)CertFreeCertificateContext(result->x509certificate_context); |
| 400 |
free(result); |
| 401 |
result = NULL; |
| 402 |
} |
| 403 |
} |
| 404 |
else |
| 405 |
{ |
| 406 |
if (set_ecc_certificate_info(result, x509privatekeyBlob) != 0) |
| 407 |
{ |
| 408 |
(void)CertFreeCertificateContext(result->x509certificate_context); |
| 409 |
free(result); |
| 410 |
result = NULL; |
| 411 |
} |
| 412 |
} |
| 413 |
} |
| 414 |
free(x509privatekeyBlob); |
| 415 |
free(binaryx509privatekey); |
| 416 |
free(binaryx509Certificate); |
| 417 |
} |
| 418 |
} |
| 419 |
} |
| 420 |
return result; |
| 421 |
} |
| 422 |
|
| 423 |
void x509_schannel_destroy(X509_SCHANNEL_HANDLE x509_schannel_handle) |
| 424 |
{ |
| 425 |
/*Codes_SRS_X509_SCHANNEL_02_011: [ If parameter x509_schannel_handle is NULL then x509_schannel_destroy shall do nothing. ]*/ |
| 426 |
if (x509_schannel_handle != NULL) |
| 427 |
{ |
| 428 |
/*Codes_SRS_X509_SCHANNEL_02_012: [ Otherwise, x509_schannel_destroy shall free all used resources. ]*/ |
| 429 |
X509_SCHANNEL_HANDLE_DATA* x509crypto = (X509_SCHANNEL_HANDLE_DATA*)x509_schannel_handle; |
| 430 |
|
| 431 |
if (x509crypto->cert_type == x509_TYPE_RSA) |
| 432 |
{ |
| 433 |
if (!CryptDestroyKey(x509crypto->x509hcryptkey)) |
| 434 |
{ |
| 435 |
LogErrorWinHTTPWithGetLastErrorAsString("unable to CryptDestroyKey"); |
| 436 |
} |
| 437 |
if (!CryptReleaseContext(x509crypto->hProv, 0)) |
| 438 |
{ |
| 439 |
LogErrorWinHTTPWithGetLastErrorAsString("unable to CryptReleaseContext"); |
| 440 |
} |
| 441 |
} |
| 442 |
else |
| 443 |
{ |
| 444 |
#if _MSC_VER > 1500 |
| 445 |
if (x509crypto->x509hcryptkey != 0) |
| 446 |
{ |
| 447 |
(void)NCryptFreeObject(x509crypto->x509hcryptkey); |
| 448 |
} |
| 449 |
if (x509crypto->hProv != 0) |
| 450 |
{ |
| 451 |
(void)NCryptFreeObject(x509crypto->hProv); |
| 452 |
} |
| 453 |
#endif |
| 454 |
} |
| 455 |
if (!CertFreeCertificateContext(x509crypto->x509certificate_context)) |
| 456 |
{ |
| 457 |
LogErrorWinHTTPWithGetLastErrorAsString("unable to CertFreeCertificateContext"); |
| 458 |
} |
| 459 |
free(x509crypto); |
| 460 |
} |
| 461 |
} |
| 462 |
|
| 463 |
PCCERT_CONTEXT x509_schannel_get_certificate_context(X509_SCHANNEL_HANDLE x509_schannel_handle) |
| 464 |
{ |
| 465 |
PCCERT_CONTEXT result; |
| 466 |
if (x509_schannel_handle == NULL) |
| 467 |
{ |
| 468 |
/*Codes_SRS_X509_SCHANNEL_02_013: [ If parameter x509_schannel_handle is NULL then x509_schannel_get_certificate_context shall return NULL. ]*/ |
| 469 |
result = NULL; |
| 470 |
} |
| 471 |
else |
| 472 |
{ |
| 473 |
/*Codes_SRS_X509_SCHANNEL_02_014: [ Otherwise, x509_schannel_get_certificate_context shall return a non-NULL PCCERT_CONTEXT pointer. ]*/ |
| 474 |
X509_SCHANNEL_HANDLE_DATA* handleData = (X509_SCHANNEL_HANDLE_DATA*)x509_schannel_handle; |
| 475 |
result = handleData->x509certificate_context; |
| 476 |
} |
| 477 |
return result; |
| 478 |
} |
| 479 |
|
| 480 |
// For each certificate specified in trustedCertificate, add to hCertStore. Windows API's (namely |
| 481 |
// CryptStringToBinaryA & CertAddEncodedCertificateToStore) do not handle multiple certificates |
| 482 |
// at a time in a single call, so add_certificates_to_store() parses the PEM (delimited by "-----END CERTIFICATE-----") |
| 483 |
// to call Windows API a cert at a time. |
| 484 |
static int add_certificates_to_store(const char* trustedCertificate, HCERTSTORE hCertStore) |
| 485 |
{ |
| 486 |
int result = 0; |
| 487 |
int numCertificatesAdded = 0; |
| 488 |
const char* trustedCertCurrentRead = trustedCertificate; |
| 489 |
unsigned char* trustedCertificateEncoded = NULL; |
| 490 |
|
| 491 |
while (result == 0) |
| 492 |
{ |
| 493 |
const char* endCertificateCurrentRead; |
| 494 |
DWORD trustedCertificateEncodedLen; |
| 495 |
DWORD lastError; |
| 496 |
|
| 497 |
if ((endCertificateCurrentRead = strstr(trustedCertCurrentRead, end_certificate_in_pem)) == NULL) |
| 498 |
{ |
| 499 |
if (numCertificatesAdded == 0) |
| 500 |
{ |
| 501 |
LogError("Certificate missing closing %s. No certificates can be added to stare", end_certificate_in_pem); |
| 502 |
result = MU_FAILURE; |
| 503 |
} |
| 504 |
break; |
| 505 |
} |
| 506 |
|
| 507 |
endCertificateCurrentRead += end_certificate_in_pem_length; |
| 508 |
if (strncmp(endCertificateCurrentRead, pem_crlf_value, pem_crlf_value_length) == 0) |
| 509 |
{ |
| 510 |
endCertificateCurrentRead += pem_crlf_value_length; |
| 511 |
} |
| 512 |
|
| 513 |
if ((trustedCertificateEncoded = convert_cert_to_binary(trustedCertCurrentRead, (DWORD)(endCertificateCurrentRead - trustedCertCurrentRead), &trustedCertificateEncodedLen)) == NULL) |
| 514 |
{ |
| 515 |
LogError("Cannot encode trusted certificate"); |
| 516 |
result = MU_FAILURE; |
| 517 |
} |
| 518 |
else if (CertAddEncodedCertificateToStore(hCertStore, X509_ASN_ENCODING | PKCS_7_ASN_ENCODING, trustedCertificateEncoded, trustedCertificateEncodedLen, CERT_STORE_ADD_NEW, NULL) != TRUE) |
| 519 |
{ |
| 520 |
lastError = GetLastError(); |
| 521 |
LogError("CertAddEncodedCertificateToStore failed with error 0x%08x", lastError); |
| 522 |
result = MU_FAILURE; |
| 523 |
} |
| 524 |
|
| 525 |
if (trustedCertificateEncoded != NULL) |
| 526 |
{ |
| 527 |
free(trustedCertificateEncoded); |
| 528 |
} |
| 529 |
|
| 530 |
trustedCertCurrentRead = endCertificateCurrentRead; |
| 531 |
numCertificatesAdded++; |
| 532 |
} |
| 533 |
|
| 534 |
return result; |
| 535 |
} |
| 536 |
|
| 537 |
// x509_verify_certificate_in_chain determines whether the certificate in pCertContextToVerify |
| 538 |
// chains up to the PEM represented by trustedCertificate or not. |
| 539 |
int x509_verify_certificate_in_chain(const char* trustedCertificate, PCCERT_CONTEXT pCertContextToVerify) |
| 540 |
{ |
| 541 |
int result; |
| 542 |
HCERTSTORE hCertStore = NULL; |
| 543 |
HCERTCHAINENGINE hChainEngine = NULL; |
| 544 |
PCCERT_CHAIN_CONTEXT pChainContextToVerify = NULL; |
| 545 |
DWORD lastError; |
| 546 |
|
| 547 |
if ((trustedCertificate == NULL) || (pCertContextToVerify == NULL)) |
| 548 |
{ |
| 549 |
result = MU_FAILURE; |
| 550 |
} |
| 551 |
// Creates an in-memory certificate store that is destroyed at end of this function. |
| 552 |
else if (NULL == (hCertStore = CertOpenStore(CERT_STORE_PROV_MEMORY, X509_ASN_ENCODING | PKCS_7_ASN_ENCODING, 0, CERT_STORE_CREATE_NEW_FLAG, NULL))) |
| 553 |
{ |
| 554 |
lastError = GetLastError(); |
| 555 |
LogError("CertOpenStore failed with error 0x%08x", lastError); |
| 556 |
result = MU_FAILURE; |
| 557 |
} |
| 558 |
else if (add_certificates_to_store(trustedCertificate, hCertStore) != 0) |
| 559 |
{ |
| 560 |
LogError("Cannot add certificates to store"); |
| 561 |
result = MU_FAILURE; |
| 562 |
} |
| 563 |
else |
| 564 |
{ |
| 565 |
CERT_CHAIN_ENGINE_CONFIG EngineConfig; |
| 566 |
memset(&EngineConfig, 0, sizeof(EngineConfig)); |
| 567 |
EngineConfig.cbSize = sizeof(EngineConfig); |
| 568 |
EngineConfig.dwFlags = CERT_CHAIN_ENABLE_CACHE_AUTO_UPDATE | CERT_CHAIN_ENABLE_SHARE_STORE; |
| 569 |
EngineConfig.hExclusiveRoot = hCertStore; |
| 570 |
|
| 571 |
CERT_CHAIN_PARA ChainPara; |
| 572 |
memset(&ChainPara, 0, sizeof(ChainPara)); |
| 573 |
ChainPara.cbSize = sizeof(ChainPara); |
| 574 |
|
| 575 |
CERT_CHAIN_POLICY_PARA PolicyPara; |
| 576 |
memset(&PolicyPara, 0, sizeof(PolicyPara)); |
| 577 |
PolicyPara.cbSize = sizeof(PolicyPara); |
| 578 |
|
| 579 |
CERT_CHAIN_POLICY_STATUS PolicyStatus; |
| 580 |
memset(&PolicyStatus, 0, sizeof(PolicyStatus)); |
| 581 |
PolicyStatus.cbSize = sizeof(PolicyStatus); |
| 582 |
|
| 583 |
if (CertCreateCertificateChainEngine(&EngineConfig, &hChainEngine) != TRUE) |
| 584 |
{ |
| 585 |
lastError = GetLastError(); |
| 586 |
LogError("CertCreateCertificateChainEngine failed with error 0x%08x", lastError); |
| 587 |
result = MU_FAILURE; |
| 588 |
} |
| 589 |
else if (CertGetCertificateChain(hChainEngine, pCertContextToVerify, NULL, pCertContextToVerify->hCertStore, &ChainPara, 0, NULL, &pChainContextToVerify) != TRUE) |
| 590 |
{ |
| 591 |
lastError = GetLastError(); |
| 592 |
LogError("CertGetCertificateChain failed with error 0x%08x", lastError); |
| 593 |
result = MU_FAILURE; |
| 594 |
} |
| 595 |
else if (CertVerifyCertificateChainPolicy(CERT_CHAIN_POLICY_SSL, pChainContextToVerify, &PolicyPara, &PolicyStatus) != TRUE) |
| 596 |
{ |
| 597 |
lastError = GetLastError(); |
| 598 |
LogError("CertVerifyCertificateChainPolicy failed with error 0x%08x", lastError); |
| 599 |
result = MU_FAILURE; |
| 600 |
} |
| 601 |
else if (PolicyStatus.dwError != 0) |
| 602 |
{ |
| 603 |
LogError("CertVerifyCertificateChainPolicy sets certificateStatus = 0x%08x", PolicyStatus.dwError); |
| 604 |
result = MU_FAILURE; |
| 605 |
} |
| 606 |
else |
| 607 |
{ |
| 608 |
result = 0; |
| 609 |
} |
| 610 |
} |
| 611 |
|
| 612 |
if (pChainContextToVerify != NULL) |
| 613 |
{ |
| 614 |
CertFreeCertificateChain(pChainContextToVerify); |
| 615 |
} |
| 616 |
|
| 617 |
if (hChainEngine != NULL) |
| 618 |
{ |
| 619 |
CertFreeCertificateChainEngine(hChainEngine); |
| 620 |
} |
| 621 |
|
| 622 |
if (hCertStore != NULL) |
| 623 |
{ |
| 624 |
CertCloseStore(hCertStore, 0); |
| 625 |
} |
| 626 |
|
| 627 |
return result; |
| 628 |
} |