| 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 "azure_c_shared_utility/x509_openssl.h" |
| 5 |
#include "azure_c_shared_utility/optimize_size.h" |
| 6 |
#include "azure_c_shared_utility/xlogging.h" |
| 7 |
#include "azure_c_shared_utility/const_defines.h" |
| 8 |
#include "azure_c_shared_utility/shared_util_options.h" |
| 9 |
#include "openssl/bio.h" |
| 10 |
#include "openssl/rsa.h" |
| 11 |
#include "openssl/x509.h" |
| 12 |
#include "openssl/pem.h" |
| 13 |
#include "openssl/err.h" |
| 14 |
#ifndef OPENSSL_NO_ENGINE |
| 15 |
#include "openssl/engine.h" |
| 16 |
#endif // OPENSSL_NO_ENGINE |
| 17 |
|
| 18 |
#ifdef __APPLE__ |
| 19 |
#ifndef EVP_PKEY_id |
| 20 |
#define EVP_PKEY_id(evp_key) evp_key->type |
| 21 |
#endif // EVP_PKEY_id |
| 22 |
#endif // __APPLE__ |
| 23 |
|
| 24 |
static void log_ERR_get_error(const char* message) |
| 25 |
{ |
| 26 |
char buf[128]; |
| 27 |
AZURE_UNREFERENCED_PARAMETER(buf); |
| 28 |
unsigned long error; |
| 29 |
int i; |
| 30 |
|
| 31 |
if (message != NULL) |
| 32 |
{ |
| 33 |
LogError("%s", message); |
| 34 |
} |
| 35 |
|
| 36 |
error = ERR_get_error(); |
| 37 |
|
| 38 |
for (i = 0; 0 != error; i++) |
| 39 |
{ |
| 40 |
LogError(" [%d] %s", i, ERR_error_string(error, buf)); |
| 41 |
error = ERR_get_error(); |
| 42 |
} |
| 43 |
} |
| 44 |
|
| 45 |
static int load_certificate_chain(SSL_CTX* ssl_ctx, const char* certificate) |
| 46 |
{ |
| 47 |
int result; |
| 48 |
BIO* bio_cert; |
| 49 |
X509* x509_value; |
| 50 |
|
| 51 |
if ((bio_cert = BIO_new_mem_buf((char*)certificate, -1)) == NULL) |
| 52 |
{ |
| 53 |
log_ERR_get_error("cannot create BIO"); |
| 54 |
result = MU_FAILURE; |
| 55 |
} |
| 56 |
else |
| 57 |
{ |
| 58 |
/* Codes_SRS_X509_OPENSSL_07_005: [ x509_openssl_add_ecc_credentials shall load the cert chain by calling PEM_read_bio_X509_AUX and SSL_CTX_use_certification. ] */ |
| 59 |
if ((x509_value = PEM_read_bio_X509_AUX(bio_cert, NULL, NULL, NULL)) == NULL) |
| 60 |
{ |
| 61 |
log_ERR_get_error("Failure PEM_read_bio_X509_AUX"); |
| 62 |
result = MU_FAILURE; |
| 63 |
} |
| 64 |
else |
| 65 |
{ |
| 66 |
if (SSL_CTX_use_certificate(ssl_ctx, x509_value) != 1) |
| 67 |
{ |
| 68 |
log_ERR_get_error("Failure PEM_read_bio_X509_AUX"); |
| 69 |
result = MU_FAILURE; |
| 70 |
} |
| 71 |
else |
| 72 |
{ |
| 73 |
X509* ca_chain; |
| 74 |
|
| 75 |
result = 0; |
| 76 |
// If we could set up our certificate, now proceed to the CA |
| 77 |
// certificates. |
| 78 |
|
| 79 |
/* Codes_SRS_X509_OPENSSL_07_006: [ If successful x509_openssl_add_ecc_credentials shall to import each certificate in the cert chain. ] */ |
| 80 |
#if (OPENSSL_VERSION_NUMBER >= 0x10100000L) || defined(LIBRESSL_VERSION_NUMBER) |
| 81 |
SSL_CTX_clear_extra_chain_certs(ssl_ctx); |
| 82 |
#else |
| 83 |
if (ssl_ctx->extra_certs != NULL) |
| 84 |
{ |
| 85 |
sk_X509_pop_free(ssl_ctx->extra_certs, X509_free); |
| 86 |
ssl_ctx->extra_certs = NULL; |
| 87 |
} |
| 88 |
#endif |
| 89 |
while ((ca_chain = PEM_read_bio_X509(bio_cert, NULL, NULL, NULL)) != NULL) |
| 90 |
{ |
| 91 |
if (SSL_CTX_add_extra_chain_cert(ssl_ctx, ca_chain) != 1) |
| 92 |
{ |
| 93 |
X509_free(ca_chain); |
| 94 |
result = MU_FAILURE; |
| 95 |
break; |
| 96 |
} |
| 97 |
} |
| 98 |
if (result != 0) |
| 99 |
{ |
| 100 |
// When the while loop ends, it's usually just EOF. |
| 101 |
unsigned long err_value = ERR_peek_last_error(); |
| 102 |
if (ERR_GET_LIB(err_value) == ERR_LIB_PEM && ERR_GET_REASON(err_value) == PEM_R_NO_START_LINE) |
| 103 |
{ |
| 104 |
ERR_clear_error(); |
| 105 |
result = 0; |
| 106 |
} |
| 107 |
else |
| 108 |
{ |
| 109 |
result = MU_FAILURE; |
| 110 |
} |
| 111 |
} |
| 112 |
} |
| 113 |
X509_free(x509_value); |
| 114 |
} |
| 115 |
BIO_free(bio_cert); |
| 116 |
} |
| 117 |
/* Codes_SRS_X509_OPENSSL_07_007: [ If any failure is encountered x509_openssl_add_ecc_credentials shall return a non-zero value. ] */ |
| 118 |
return result; |
| 119 |
} |
| 120 |
|
| 121 |
static int load_ecc_key(SSL_CTX* ssl_ctx, EVP_PKEY* evp_key) |
| 122 |
{ |
| 123 |
int result; |
| 124 |
/* Codes_SRS_X509_OPENSSL_07_004: [ x509_openssl_add_ecc_credentials shall import the certification using by the EVP_PKEY. ] */ |
| 125 |
if (SSL_CTX_use_PrivateKey(ssl_ctx, evp_key) != 1) |
| 126 |
{ |
| 127 |
LogError("Failed SSL_CTX_use_PrivateKey"); |
| 128 |
result = MU_FAILURE; |
| 129 |
} |
| 130 |
else |
| 131 |
{ |
| 132 |
result = 0; |
| 133 |
} |
| 134 |
/* Codes_SRS_X509_OPENSSL_07_007: [ If any failure is encountered x509_openssl_add_ecc_credentials shall return a non-zero value. ] */ |
| 135 |
return result; |
| 136 |
} |
| 137 |
|
| 138 |
static int load_key_RSA(SSL_CTX* ssl_ctx, EVP_PKEY* evp_key) |
| 139 |
{ |
| 140 |
int result; |
| 141 |
/*Codes_SRS_X509_OPENSSL_02_005: [ x509_openssl_add_credentials shall use PEM_read_bio_RSAPrivateKey to read the x509 private key. ]*/ |
| 142 |
RSA* privatekey = EVP_PKEY_get1_RSA(evp_key); |
| 143 |
if (privatekey == NULL) |
| 144 |
{ |
| 145 |
/*Codes_SRS_X509_OPENSSL_02_009: [ Otherwise x509_openssl_add_credentials shall fail and return a non-zero number. ]*/ |
| 146 |
log_ERR_get_error("Failure reading RSA private key"); |
| 147 |
result = MU_FAILURE; |
| 148 |
} |
| 149 |
else |
| 150 |
{ |
| 151 |
/*Codes_SRS_X509_OPENSSL_02_007: [ x509_openssl_add_credentials shall use SSL_CTX_use_RSAPrivateKey to load the private key into the SSL context. ]*/ |
| 152 |
if (SSL_CTX_use_RSAPrivateKey(ssl_ctx, privatekey) != 1) |
| 153 |
{ |
| 154 |
/*Codes_SRS_X509_OPENSSL_02_009: [ Otherwise x509_openssl_add_credentials shall fail and return a non-zero number. ]*/ |
| 155 |
log_ERR_get_error("Failure calling SSL_CTX_use_RSAPrivateKey"); |
| 156 |
result = MU_FAILURE; |
| 157 |
} |
| 158 |
else |
| 159 |
{ |
| 160 |
/*all is fine*/ |
| 161 |
/*Codes_SRS_X509_OPENSSL_02_008: [ If no error occurs, then x509_openssl_add_credentials shall succeed and return 0. ]*/ |
| 162 |
result = 0; |
| 163 |
} |
| 164 |
RSA_free(privatekey); |
| 165 |
} |
| 166 |
return result; |
| 167 |
} |
| 168 |
|
| 169 |
int x509_openssl_add_pem_file_key(SSL_CTX* ssl_ctx, const char* x509privatekey) |
| 170 |
{ |
| 171 |
int result; |
| 172 |
|
| 173 |
BIO* bio_key = BIO_new_mem_buf((char*)x509privatekey, -1); /*taking off the const from the pointer is needed on older versions of OPENSSL*/ |
| 174 |
if (bio_key == NULL) |
| 175 |
{ |
| 176 |
log_ERR_get_error("cannot create private key BIO"); |
| 177 |
result = MU_FAILURE; |
| 178 |
} |
| 179 |
else |
| 180 |
{ |
| 181 |
// Get the Private Key type |
| 182 |
EVP_PKEY* evp_key = PEM_read_bio_PrivateKey(bio_key, NULL, NULL, NULL); |
| 183 |
if (evp_key == NULL) |
| 184 |
{ |
| 185 |
log_ERR_get_error("failure creating private key evp_key"); |
| 186 |
result = MU_FAILURE; |
| 187 |
} |
| 188 |
else |
| 189 |
{ |
| 190 |
// Check the type for the EVP key |
| 191 |
int evp_type = EVP_PKEY_id(evp_key); |
| 192 |
if (evp_type == EVP_PKEY_RSA || evp_type == EVP_PKEY_RSA2) |
| 193 |
{ |
| 194 |
if (load_key_RSA(ssl_ctx, evp_key) != 0) |
| 195 |
{ |
| 196 |
LogError("failure loading RSA private key cert"); |
| 197 |
result = MU_FAILURE; |
| 198 |
} |
| 199 |
else |
| 200 |
{ |
| 201 |
result = 0; |
| 202 |
} |
| 203 |
} |
| 204 |
else |
| 205 |
{ |
| 206 |
if (load_ecc_key(ssl_ctx, evp_key) != 0) |
| 207 |
{ |
| 208 |
LogError("failure loading ECC private key cert"); |
| 209 |
result = MU_FAILURE; |
| 210 |
} |
| 211 |
else |
| 212 |
{ |
| 213 |
result = 0; |
| 214 |
} |
| 215 |
} |
| 216 |
EVP_PKEY_free(evp_key); |
| 217 |
} |
| 218 |
BIO_free(bio_key); |
| 219 |
} |
| 220 |
|
| 221 |
return result; |
| 222 |
} |
| 223 |
|
| 224 |
#ifndef OPENSSL_NO_ENGINE |
| 225 |
int x509_openssl_add_engine_key(SSL_CTX* ssl_ctx, const char* x509privatekey_id, ENGINE* engine) |
| 226 |
{ |
| 227 |
int result; |
| 228 |
|
| 229 |
// Engine functional reference is short-lived, only within this function. |
| 230 |
if (!ENGINE_init(engine)) |
| 231 |
{ |
| 232 |
log_ERR_get_error("unable to initialize ENGINE."); |
| 233 |
result = MU_FAILURE; |
| 234 |
} |
| 235 |
else |
| 236 |
{ |
| 237 |
if (!ENGINE_set_default(engine, ENGINE_METHOD_ALL)) |
| 238 |
{ |
| 239 |
log_ERR_get_error("unable to configure ENGINE."); |
| 240 |
result = MU_FAILURE; |
| 241 |
} |
| 242 |
else |
| 243 |
{ |
| 244 |
EVP_PKEY* evp_key = ENGINE_load_private_key(engine, x509privatekey_id, NULL, NULL); |
| 245 |
|
| 246 |
if (evp_key == NULL) |
| 247 |
{ |
| 248 |
log_ERR_get_error("unable to load private key from ENGINE."); |
| 249 |
result = MU_FAILURE; |
| 250 |
} |
| 251 |
else |
| 252 |
{ |
| 253 |
if (load_ecc_key(ssl_ctx, evp_key) != 0) |
| 254 |
{ |
| 255 |
LogError("unable to configure private key from ENGINE."); |
| 256 |
result = MU_FAILURE; |
| 257 |
} |
| 258 |
else |
| 259 |
{ |
| 260 |
result = 0; |
| 261 |
} |
| 262 |
|
| 263 |
EVP_PKEY_free(evp_key); |
| 264 |
} |
| 265 |
} |
| 266 |
|
| 267 |
if (!ENGINE_finish(engine)) // Release functional reference. |
| 268 |
{ |
| 269 |
log_ERR_get_error("unable to release ENGINE functional reference."); |
| 270 |
result = MU_FAILURE; |
| 271 |
} |
| 272 |
} |
| 273 |
|
| 274 |
return result; |
| 275 |
} |
| 276 |
#endif // OPENSSL_NO_ENGINE |
| 277 |
|
| 278 |
#ifndef OPENSSL_NO_ENGINE |
| 279 |
int x509_openssl_add_credentials( |
| 280 |
SSL_CTX* ssl_ctx, |
| 281 |
const char* x509certificate, |
| 282 |
const char* x509privatekey, |
| 283 |
OPTION_OPENSSL_KEY_TYPE x509privatekeytype, |
| 284 |
ENGINE* engine) |
| 285 |
#else // OPENSSL_NO_ENGINE |
| 286 |
int x509_openssl_add_credentials( |
| 287 |
SSL_CTX* ssl_ctx, |
| 288 |
const char* x509certificate, |
| 289 |
const char* x509privatekey, |
| 290 |
OPTION_OPENSSL_KEY_TYPE x509privatekeytype) |
| 291 |
#endif // OPENSSL_NO_ENGINE |
| 292 |
{ |
| 293 |
int result; |
| 294 |
if (ssl_ctx == NULL || x509certificate == NULL || x509privatekey == NULL) |
| 295 |
{ |
| 296 |
/*Codes_SRS_X509_OPENSSL_02_009: [ Otherwise x509_openssl_add_credentials shall fail and return a non-zero number. ]*/ |
| 297 |
LogError("invalid parameter detected: ssl_ctx=%p, x509certificate=%p, x509privatekey=%p", ssl_ctx, x509certificate, x509privatekey); |
| 298 |
result = MU_FAILURE; |
| 299 |
} |
| 300 |
#ifndef OPENSSL_NO_ENGINE |
| 301 |
else if ((x509privatekeytype == KEY_TYPE_ENGINE) && (engine == NULL)) |
| 302 |
{ |
| 303 |
LogError("OpenSSL Engine must be configured when KEY_TYPE_ENGINE is used."); |
| 304 |
result = MU_FAILURE; |
| 305 |
} |
| 306 |
#endif // OPENSSL_NO_ENGINE |
| 307 |
else |
| 308 |
{ |
| 309 |
// Configure private key. |
| 310 |
if (x509privatekeytype == KEY_TYPE_DEFAULT) |
| 311 |
{ |
| 312 |
result = x509_openssl_add_pem_file_key(ssl_ctx, x509privatekey); |
| 313 |
} |
| 314 |
#ifndef OPENSSL_NO_ENGINE |
| 315 |
else if (x509privatekeytype == KEY_TYPE_ENGINE) |
| 316 |
{ |
| 317 |
result = x509_openssl_add_engine_key(ssl_ctx, x509privatekey, engine); |
| 318 |
} |
| 319 |
#endif // OPENSSL_NO_ENGINE |
| 320 |
else |
| 321 |
{ |
| 322 |
LogError("Unexpected value of OPTION_OPENSSL_KEY_TYPE (%d)", x509privatekeytype); |
| 323 |
result = MU_FAILURE; |
| 324 |
} |
| 325 |
|
| 326 |
if (result == 0) |
| 327 |
{ |
| 328 |
if (load_certificate_chain(ssl_ctx, x509certificate) != 0) |
| 329 |
{ |
| 330 |
LogError("failure loading public cert or chain"); |
| 331 |
result = MU_FAILURE; |
| 332 |
} |
| 333 |
} |
| 334 |
} |
| 335 |
|
| 336 |
return result; |
| 337 |
} |
| 338 |
|
| 339 |
/*return 0 if everything was ok, any other number to signal an error*/ |
| 340 |
/*this function inserts a x509certificate+x509privatekey to a SSL_CTX (ssl context) in order to authenticate the device with the service*/ |
| 341 |
int x509_openssl_add_certificates(SSL_CTX* ssl_ctx, const char* certificates) |
| 342 |
{ |
| 343 |
int result; |
| 344 |
|
| 345 |
/*Codes_SRS_X509_OPENSSL_02_010: [ If ssl_ctx is NULL then x509_openssl_add_certificates shall fail and return a non-zero value. ]*/ |
| 346 |
/*Codes_SRS_X509_OPENSSL_02_011: [ If certificates is NULL then x509_openssl_add_certificates shall fail and return a non-zero value. ]*/ |
| 347 |
if ((certificates == NULL) || (ssl_ctx == NULL)) |
| 348 |
{ |
| 349 |
/*Codes_SRS_X509_OPENSSL_02_018: [ In case of any failure x509_openssl_add_certificates shall fail and return a non-zero value. ]*/ |
| 350 |
LogError("invalid argument SSL_CTX* ssl_ctx=%p, const char* certificates=%s", ssl_ctx, MU_P_OR_NULL(certificates)); |
| 351 |
result = MU_FAILURE; |
| 352 |
} |
| 353 |
else |
| 354 |
{ |
| 355 |
X509_STORE* cert_store = SSL_CTX_get_cert_store(ssl_ctx); |
| 356 |
if (cert_store == NULL) |
| 357 |
{ |
| 358 |
/*Codes_SRS_X509_OPENSSL_02_018: [ In case of any failure x509_openssl_add_certificates shall fail and return a non-zero value. ]*/ |
| 359 |
log_ERR_get_error("failure in SSL_CTX_get_cert_store."); |
| 360 |
result = MU_FAILURE; |
| 361 |
} |
| 362 |
else |
| 363 |
{ |
| 364 |
/*Codes_SRS_X509_OPENSSL_02_012: [ x509_openssl_add_certificates shall get the memory BIO method function by calling BIO_s_mem. ]*/ |
| 365 |
#if (OPENSSL_VERSION_NUMBER >= 0x10100000L) || defined(LIBRESSL_VERSION_NUMBER) |
| 366 |
const BIO_METHOD* bio_method; |
| 367 |
#else |
| 368 |
BIO_METHOD* bio_method; |
| 369 |
#endif |
| 370 |
bio_method = BIO_s_mem(); |
| 371 |
if (bio_method == NULL) |
| 372 |
{ |
| 373 |
/*Codes_SRS_X509_OPENSSL_02_018: [ In case of any failure x509_openssl_add_certificates shall fail and return a non-zero value. ]*/ |
| 374 |
log_ERR_get_error("failure in BIO_s_mem"); |
| 375 |
result = MU_FAILURE; |
| 376 |
} |
| 377 |
else |
| 378 |
{ |
| 379 |
/*Codes_SRS_X509_OPENSSL_02_013: [ x509_openssl_add_certificates shall create a new memory BIO by calling BIO_new. ]*/ |
| 380 |
BIO* cert_memory_bio = BIO_new(bio_method); |
| 381 |
if (cert_memory_bio == NULL) |
| 382 |
{ |
| 383 |
/*Codes_SRS_X509_OPENSSL_02_018: [ In case of any failure x509_openssl_add_certificates shall fail and return a non-zero value. ]*/ |
| 384 |
log_ERR_get_error("failure in BIO_new"); |
| 385 |
result = MU_FAILURE; |
| 386 |
} |
| 387 |
else |
| 388 |
{ |
| 389 |
/*Codes_SRS_X509_OPENSSL_02_014: [ x509_openssl_add_certificates shall load certificates into the memory BIO by a call to BIO_puts. ]*/ |
| 390 |
int puts_result = BIO_puts(cert_memory_bio, certificates); |
| 391 |
if ((puts_result<0) || (puts_result != (int)strlen(certificates))) |
| 392 |
{ |
| 393 |
/*Codes_SRS_X509_OPENSSL_02_018: [ In case of any failure x509_openssl_add_certificates shall fail and return a non-zero value. ]*/ |
| 394 |
log_ERR_get_error("failure in BIO_puts"); |
| 395 |
result = MU_FAILURE; |
| 396 |
} |
| 397 |
else |
| 398 |
{ |
| 399 |
X509* certificate; |
| 400 |
/*Codes_SRS_X509_OPENSSL_02_015: [ x509_openssl_add_certificates shall retrieve each certificate by a call to PEM_read_bio_X509. ]*/ |
| 401 |
while ((certificate = PEM_read_bio_X509(cert_memory_bio, NULL, NULL, NULL)) != NULL) |
| 402 |
{ |
| 403 |
/*Codes_SRS_X509_OPENSSL_02_016: [ x509_openssl_add_certificates shall add the certificate to the store by a call to X509_STORE_add_cert. ]*/ |
| 404 |
if (!X509_STORE_add_cert(cert_store, certificate)) |
| 405 |
{ |
| 406 |
/*Codes_SRS_X509_OPENSSL_02_017: [ If X509_STORE_add_cert returns with error and that error is X509_R_CERT_ALREADY_IN_HASH_TABLE then x509_openssl_add_certificates shall ignore it as the certificate is already in the store. ]*/ |
| 407 |
/*if the certificate is already in the store, then ERR_peek_error would return REASON == X509_R_CERT_ALREADY_IN_HASH_TABLE, so that's no a "real" error*/ |
| 408 |
unsigned long error = ERR_peek_error(); |
| 409 |
if (ERR_GET_REASON(error) == X509_R_CERT_ALREADY_IN_HASH_TABLE) |
| 410 |
{ |
| 411 |
/*not an error*/ |
| 412 |
} |
| 413 |
else |
| 414 |
{ |
| 415 |
/*Codes_SRS_X509_OPENSSL_02_018: [ In case of any failure x509_openssl_add_certificates shall fail and return a non-zero value. ]*/ |
| 416 |
log_ERR_get_error("failure in X509_STORE_add_cert"); |
| 417 |
X509_free(certificate); |
| 418 |
break; |
| 419 |
} |
| 420 |
|
| 421 |
} |
| 422 |
X509_free(certificate); |
| 423 |
} |
| 424 |
|
| 425 |
if (certificate == NULL) |
| 426 |
{ |
| 427 |
/*Codes_SRS_X509_OPENSSL_02_019: [ Otherwise, x509_openssl_add_certificates shall succeed and return 0. ]*/ |
| 428 |
result = 0;/*all is fine*/ |
| 429 |
} |
| 430 |
else |
| 431 |
{ |
| 432 |
/*previous while loop terminated unfortunately*/ |
| 433 |
/*Codes_SRS_X509_OPENSSL_02_018: [ In case of any failure x509_openssl_add_certificates shall fail and return a non-zero value. ]*/ |
| 434 |
result = MU_FAILURE; |
| 435 |
} |
| 436 |
} |
| 437 |
BIO_free(cert_memory_bio); |
| 438 |
} |
| 439 |
} |
| 440 |
} |
| 441 |
} |
| 442 |
return result; |
| 443 |
} |