| 1 |
// Copyright (C) 1995-1998 Eric Young ([email protected]) |
| 2 |
// Copyright (c) 1998-2007 The OpenSSL Project. All rights reserved. |
| 3 |
// SPDX-License-Identifier: Apache-2.0 |
| 4 |
|
| 5 |
#include <openssl/ssl.h> |
| 6 |
|
| 7 |
#include <errno.h> |
| 8 |
#include <string.h> |
| 9 |
|
| 10 |
#include <openssl/asn1.h> |
| 11 |
#include <openssl/bio.h> |
| 12 |
#include <openssl/err.h> |
| 13 |
#include <openssl/mem.h> |
| 14 |
#include <openssl/pem.h> |
| 15 |
#include <openssl/stack.h> |
| 16 |
#include <openssl/x509.h> |
| 17 |
|
| 18 |
#include "internal.h" |
| 19 |
|
| 20 |
|
| 21 |
static int xname_cmp(const X509_NAME *const *a, const X509_NAME *const *b) { |
| 22 |
return X509_NAME_cmp(*a, *b); |
| 23 |
} |
| 24 |
|
| 25 |
static int add_bio_cert_subjects_to_stack(STACK_OF(X509_NAME) *out, BIO *bio, |
| 26 |
bool allow_empty) { |
| 27 |
// This function historically sorted |out| after every addition and skipped |
| 28 |
// duplicates. This implementation preserves that behavior, but only sorts at |
| 29 |
// the end, to avoid a quadratic running time. Existing duplicates in |out| |
| 30 |
// are preserved, but do not introduce new duplicates. |
| 31 |
bssl::UniquePtr<STACK_OF(X509_NAME)> to_append(sk_X509_NAME_new(xname_cmp)); |
| 32 |
if (to_append == nullptr) { |
| 33 |
return 0; |
| 34 |
} |
| 35 |
|
| 36 |
// Temporarily switch the comparison function for |out|. |
| 37 |
struct RestoreCmpFunc { |
| 38 |
~RestoreCmpFunc() { sk_X509_NAME_set_cmp_func(stack, old_cmp); } |
| 39 |
STACK_OF(X509_NAME) *stack; |
| 40 |
int (*old_cmp)(const X509_NAME *const *, const X509_NAME *const *); |
| 41 |
}; |
| 42 |
RestoreCmpFunc restore = {out, sk_X509_NAME_set_cmp_func(out, xname_cmp)}; |
| 43 |
|
| 44 |
sk_X509_NAME_sort(out); |
| 45 |
bool first = true; |
| 46 |
for (;;) { |
| 47 |
bssl::UniquePtr<X509> x509( |
| 48 |
PEM_read_bio_X509(bio, nullptr, nullptr, nullptr)); |
| 49 |
if (x509 == nullptr) { |
| 50 |
if (first && !allow_empty) { |
| 51 |
return 0; |
| 52 |
} |
| 53 |
// TODO(davidben): This ignores PEM syntax errors. It should only succeed |
| 54 |
// on |PEM_R_NO_START_LINE|. |
| 55 |
ERR_clear_error(); |
| 56 |
break; |
| 57 |
} |
| 58 |
first = false; |
| 59 |
|
| 60 |
X509_NAME *subject = X509_get_subject_name(x509.get()); |
| 61 |
// Skip if already present in |out|. Duplicates in |to_append| will be |
| 62 |
// handled separately. |
| 63 |
if (sk_X509_NAME_find_awslc(out, nullptr, subject)) { |
| 64 |
continue; |
| 65 |
} |
| 66 |
|
| 67 |
bssl::UniquePtr<X509_NAME> copy(X509_NAME_dup(subject)); |
| 68 |
if (copy == nullptr || |
| 69 |
!bssl::PushToStack(to_append.get(), std::move(copy))) { |
| 70 |
return 0; |
| 71 |
} |
| 72 |
} |
| 73 |
|
| 74 |
// Append |to_append| to |stack|, skipping any duplicates. |
| 75 |
sk_X509_NAME_sort(to_append.get()); |
| 76 |
size_t num = sk_X509_NAME_num(to_append.get()); |
| 77 |
for (size_t i = 0; i < num; i++) { |
| 78 |
bssl::UniquePtr<X509_NAME> name(sk_X509_NAME_value(to_append.get(), i)); |
| 79 |
sk_X509_NAME_set(to_append.get(), i, nullptr); |
| 80 |
if (i + 1 < num && |
| 81 |
X509_NAME_cmp(name.get(), sk_X509_NAME_value(to_append.get(), i + 1)) == |
| 82 |
0) { |
| 83 |
continue; |
| 84 |
} |
| 85 |
if (!bssl::PushToStack(out, std::move(name))) { |
| 86 |
return 0; |
| 87 |
} |
| 88 |
} |
| 89 |
|
| 90 |
// Sort |out| one last time, to preserve the historical behavior of |
| 91 |
// maintaining the sorted list. |
| 92 |
sk_X509_NAME_sort(out); |
| 93 |
return 1; |
| 94 |
} |
| 95 |
|
| 96 |
int SSL_add_bio_cert_subjects_to_stack(STACK_OF(X509_NAME) *out, BIO *bio) { |
| 97 |
return add_bio_cert_subjects_to_stack(out, bio, /*allow_empty=*/true); |
| 98 |
} |
| 99 |
|
| 100 |
STACK_OF(X509_NAME) *SSL_load_client_CA_file(const char *file) { |
| 101 |
bssl::UniquePtr<BIO> in(BIO_new_file(file, "rb")); |
| 102 |
if (in == nullptr) { |
| 103 |
return nullptr; |
| 104 |
} |
| 105 |
bssl::UniquePtr<STACK_OF(X509_NAME)> ret(sk_X509_NAME_new_null()); |
| 106 |
if (ret == nullptr || // |
| 107 |
!add_bio_cert_subjects_to_stack(ret.get(), in.get(), |
| 108 |
/*allow_empty=*/false)) { |
| 109 |
return nullptr; |
| 110 |
} |
| 111 |
return ret.release(); |
| 112 |
} |
| 113 |
|
| 114 |
int SSL_add_file_cert_subjects_to_stack(STACK_OF(X509_NAME) *out, |
| 115 |
const char *file) { |
| 116 |
bssl::UniquePtr<BIO> in(BIO_new_file(file, "rb")); |
| 117 |
if (in == nullptr) { |
| 118 |
return 0; |
| 119 |
} |
| 120 |
return SSL_add_bio_cert_subjects_to_stack(out, in.get()); |
| 121 |
} |
| 122 |
|
| 123 |
int SSL_use_certificate_file(SSL *ssl, const char *file, int type) { |
| 124 |
int reason_code; |
| 125 |
BIO *in; |
| 126 |
int ret = 0; |
| 127 |
X509 *x = NULL; |
| 128 |
|
| 129 |
in = BIO_new(BIO_s_file()); |
| 130 |
if (in == NULL) { |
| 131 |
OPENSSL_PUT_ERROR(SSL, ERR_R_BUF_LIB); |
| 132 |
goto end; |
| 133 |
} |
| 134 |
|
| 135 |
if (BIO_read_filename(in, file) <= 0) { |
| 136 |
OPENSSL_PUT_ERROR(SSL, ERR_R_SYS_LIB); |
| 137 |
goto end; |
| 138 |
} |
| 139 |
|
| 140 |
if (type == SSL_FILETYPE_ASN1) { |
| 141 |
reason_code = ERR_R_ASN1_LIB; |
| 142 |
x = d2i_X509_bio(in, NULL); |
| 143 |
} else if (type == SSL_FILETYPE_PEM) { |
| 144 |
reason_code = ERR_R_PEM_LIB; |
| 145 |
x = PEM_read_bio_X509(in, NULL, ssl->ctx->default_passwd_callback, |
| 146 |
ssl->ctx->default_passwd_callback_userdata); |
| 147 |
} else { |
| 148 |
OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SSL_FILETYPE); |
| 149 |
goto end; |
| 150 |
} |
| 151 |
|
| 152 |
if (x == NULL) { |
| 153 |
OPENSSL_PUT_ERROR(SSL, reason_code); |
| 154 |
goto end; |
| 155 |
} |
| 156 |
|
| 157 |
ret = SSL_use_certificate(ssl, x); |
| 158 |
|
| 159 |
end: |
| 160 |
X509_free(x); |
| 161 |
BIO_free(in); |
| 162 |
|
| 163 |
return ret; |
| 164 |
} |
| 165 |
|
| 166 |
int SSL_use_RSAPrivateKey_file(SSL *ssl, const char *file, int type) { |
| 167 |
int reason_code, ret = 0; |
| 168 |
BIO *in; |
| 169 |
RSA *rsa = NULL; |
| 170 |
|
| 171 |
in = BIO_new(BIO_s_file()); |
| 172 |
if (in == NULL) { |
| 173 |
OPENSSL_PUT_ERROR(SSL, ERR_R_BUF_LIB); |
| 174 |
goto end; |
| 175 |
} |
| 176 |
|
| 177 |
if (BIO_read_filename(in, file) <= 0) { |
| 178 |
OPENSSL_PUT_ERROR(SSL, ERR_R_SYS_LIB); |
| 179 |
goto end; |
| 180 |
} |
| 181 |
|
| 182 |
if (type == SSL_FILETYPE_ASN1) { |
| 183 |
reason_code = ERR_R_ASN1_LIB; |
| 184 |
rsa = d2i_RSAPrivateKey_bio(in, NULL); |
| 185 |
} else if (type == SSL_FILETYPE_PEM) { |
| 186 |
reason_code = ERR_R_PEM_LIB; |
| 187 |
rsa = |
| 188 |
PEM_read_bio_RSAPrivateKey(in, NULL, ssl->ctx->default_passwd_callback, |
| 189 |
ssl->ctx->default_passwd_callback_userdata); |
| 190 |
} else { |
| 191 |
OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SSL_FILETYPE); |
| 192 |
goto end; |
| 193 |
} |
| 194 |
|
| 195 |
if (rsa == NULL) { |
| 196 |
OPENSSL_PUT_ERROR(SSL, reason_code); |
| 197 |
goto end; |
| 198 |
} |
| 199 |
ret = SSL_use_RSAPrivateKey(ssl, rsa); |
| 200 |
RSA_free(rsa); |
| 201 |
|
| 202 |
end: |
| 203 |
BIO_free(in); |
| 204 |
return ret; |
| 205 |
} |
| 206 |
|
| 207 |
int SSL_use_PrivateKey_file(SSL *ssl, const char *file, int type) { |
| 208 |
int reason_code, ret = 0; |
| 209 |
BIO *in; |
| 210 |
EVP_PKEY *pkey = NULL; |
| 211 |
|
| 212 |
in = BIO_new(BIO_s_file()); |
| 213 |
if (in == NULL) { |
| 214 |
OPENSSL_PUT_ERROR(SSL, ERR_R_BUF_LIB); |
| 215 |
goto end; |
| 216 |
} |
| 217 |
|
| 218 |
if (BIO_read_filename(in, file) <= 0) { |
| 219 |
OPENSSL_PUT_ERROR(SSL, ERR_R_SYS_LIB); |
| 220 |
goto end; |
| 221 |
} |
| 222 |
|
| 223 |
if (type == SSL_FILETYPE_PEM) { |
| 224 |
reason_code = ERR_R_PEM_LIB; |
| 225 |
pkey = PEM_read_bio_PrivateKey(in, NULL, ssl->ctx->default_passwd_callback, |
| 226 |
ssl->ctx->default_passwd_callback_userdata); |
| 227 |
} else if (type == SSL_FILETYPE_ASN1) { |
| 228 |
reason_code = ERR_R_ASN1_LIB; |
| 229 |
pkey = d2i_PrivateKey_bio(in, NULL); |
| 230 |
} else { |
| 231 |
OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SSL_FILETYPE); |
| 232 |
goto end; |
| 233 |
} |
| 234 |
|
| 235 |
if (pkey == NULL) { |
| 236 |
OPENSSL_PUT_ERROR(SSL, reason_code); |
| 237 |
goto end; |
| 238 |
} |
| 239 |
ret = SSL_use_PrivateKey(ssl, pkey); |
| 240 |
EVP_PKEY_free(pkey); |
| 241 |
|
| 242 |
end: |
| 243 |
BIO_free(in); |
| 244 |
return ret; |
| 245 |
} |
| 246 |
|
| 247 |
int SSL_CTX_use_certificate_file(SSL_CTX *ctx, const char *file, int type) { |
| 248 |
int reason_code; |
| 249 |
BIO *in; |
| 250 |
int ret = 0; |
| 251 |
X509 *x = NULL; |
| 252 |
|
| 253 |
in = BIO_new(BIO_s_file()); |
| 254 |
if (in == NULL) { |
| 255 |
OPENSSL_PUT_ERROR(SSL, ERR_R_BUF_LIB); |
| 256 |
goto end; |
| 257 |
} |
| 258 |
|
| 259 |
if (BIO_read_filename(in, file) <= 0) { |
| 260 |
OPENSSL_PUT_ERROR(SSL, ERR_R_SYS_LIB); |
| 261 |
goto end; |
| 262 |
} |
| 263 |
|
| 264 |
if (type == SSL_FILETYPE_ASN1) { |
| 265 |
reason_code = ERR_R_ASN1_LIB; |
| 266 |
x = d2i_X509_bio(in, NULL); |
| 267 |
} else if (type == SSL_FILETYPE_PEM) { |
| 268 |
reason_code = ERR_R_PEM_LIB; |
| 269 |
x = PEM_read_bio_X509(in, NULL, ctx->default_passwd_callback, |
| 270 |
ctx->default_passwd_callback_userdata); |
| 271 |
} else { |
| 272 |
OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SSL_FILETYPE); |
| 273 |
goto end; |
| 274 |
} |
| 275 |
|
| 276 |
if (x == NULL) { |
| 277 |
OPENSSL_PUT_ERROR(SSL, reason_code); |
| 278 |
goto end; |
| 279 |
} |
| 280 |
|
| 281 |
ret = SSL_CTX_use_certificate(ctx, x); |
| 282 |
|
| 283 |
end: |
| 284 |
X509_free(x); |
| 285 |
BIO_free(in); |
| 286 |
return ret; |
| 287 |
} |
| 288 |
|
| 289 |
int SSL_CTX_use_RSAPrivateKey_file(SSL_CTX *ctx, const char *file, int type) { |
| 290 |
int reason_code, ret = 0; |
| 291 |
BIO *in; |
| 292 |
RSA *rsa = NULL; |
| 293 |
|
| 294 |
in = BIO_new(BIO_s_file()); |
| 295 |
if (in == NULL) { |
| 296 |
OPENSSL_PUT_ERROR(SSL, ERR_R_BUF_LIB); |
| 297 |
goto end; |
| 298 |
} |
| 299 |
|
| 300 |
if (BIO_read_filename(in, file) <= 0) { |
| 301 |
OPENSSL_PUT_ERROR(SSL, ERR_R_SYS_LIB); |
| 302 |
goto end; |
| 303 |
} |
| 304 |
|
| 305 |
if (type == SSL_FILETYPE_ASN1) { |
| 306 |
reason_code = ERR_R_ASN1_LIB; |
| 307 |
rsa = d2i_RSAPrivateKey_bio(in, NULL); |
| 308 |
} else if (type == SSL_FILETYPE_PEM) { |
| 309 |
reason_code = ERR_R_PEM_LIB; |
| 310 |
rsa = PEM_read_bio_RSAPrivateKey(in, NULL, ctx->default_passwd_callback, |
| 311 |
ctx->default_passwd_callback_userdata); |
| 312 |
} else { |
| 313 |
OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SSL_FILETYPE); |
| 314 |
goto end; |
| 315 |
} |
| 316 |
|
| 317 |
if (rsa == NULL) { |
| 318 |
OPENSSL_PUT_ERROR(SSL, reason_code); |
| 319 |
goto end; |
| 320 |
} |
| 321 |
ret = SSL_CTX_use_RSAPrivateKey(ctx, rsa); |
| 322 |
RSA_free(rsa); |
| 323 |
|
| 324 |
end: |
| 325 |
BIO_free(in); |
| 326 |
return ret; |
| 327 |
} |
| 328 |
|
| 329 |
int SSL_CTX_use_PrivateKey_file(SSL_CTX *ctx, const char *file, int type) { |
| 330 |
int reason_code, ret = 0; |
| 331 |
BIO *in; |
| 332 |
EVP_PKEY *pkey = NULL; |
| 333 |
|
| 334 |
in = BIO_new(BIO_s_file()); |
| 335 |
if (in == NULL) { |
| 336 |
OPENSSL_PUT_ERROR(SSL, ERR_R_BUF_LIB); |
| 337 |
goto end; |
| 338 |
} |
| 339 |
|
| 340 |
if (BIO_read_filename(in, file) <= 0) { |
| 341 |
OPENSSL_PUT_ERROR(SSL, ERR_R_SYS_LIB); |
| 342 |
goto end; |
| 343 |
} |
| 344 |
|
| 345 |
if (type == SSL_FILETYPE_PEM) { |
| 346 |
reason_code = ERR_R_PEM_LIB; |
| 347 |
pkey = PEM_read_bio_PrivateKey(in, NULL, ctx->default_passwd_callback, |
| 348 |
ctx->default_passwd_callback_userdata); |
| 349 |
} else if (type == SSL_FILETYPE_ASN1) { |
| 350 |
reason_code = ERR_R_ASN1_LIB; |
| 351 |
pkey = d2i_PrivateKey_bio(in, NULL); |
| 352 |
} else { |
| 353 |
OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SSL_FILETYPE); |
| 354 |
goto end; |
| 355 |
} |
| 356 |
|
| 357 |
if (pkey == NULL) { |
| 358 |
OPENSSL_PUT_ERROR(SSL, reason_code); |
| 359 |
goto end; |
| 360 |
} |
| 361 |
ret = SSL_CTX_use_PrivateKey(ctx, pkey); |
| 362 |
EVP_PKEY_free(pkey); |
| 363 |
|
| 364 |
end: |
| 365 |
BIO_free(in); |
| 366 |
return ret; |
| 367 |
} |
| 368 |
|
| 369 |
// Read a file that contains our certificate in "PEM" format, possibly followed |
| 370 |
// by a sequence of CA certificates that should be sent to the peer in the |
| 371 |
// Certificate message. |
| 372 |
static int use_certificate_chain_file(SSL_CTX *ctx, SSL *ssl, const char *file) { |
| 373 |
if(ctx == nullptr && ssl == nullptr) { |
| 374 |
return 0; |
| 375 |
} |
| 376 |
|
| 377 |
int ret = 0; |
| 378 |
// |parent_ctx| is |ctx| unless |ssl| is defined. |
| 379 |
SSL_CTX *parent_ctx = ctx; |
| 380 |
if (ssl != nullptr) { |
| 381 |
parent_ctx = ssl->ctx.get(); |
| 382 |
} |
| 383 |
ERR_clear_error(); // clear error stack for SSL_CTX_use_certificate() |
| 384 |
|
| 385 |
bssl::UniquePtr<BIO> in(BIO_new(BIO_s_file())); |
| 386 |
if (in == nullptr) { |
| 387 |
OPENSSL_PUT_ERROR(SSL, ERR_R_BUF_LIB); |
| 388 |
return 0; |
| 389 |
} |
| 390 |
|
| 391 |
if (BIO_read_filename(in.get(), file) <= 0) { |
| 392 |
OPENSSL_PUT_ERROR(SSL, ERR_R_SYS_LIB); |
| 393 |
return 0; |
| 394 |
} |
| 395 |
|
| 396 |
bssl::UniquePtr<X509> x509(PEM_read_bio_X509_AUX( |
| 397 |
in.get(), nullptr, parent_ctx->default_passwd_callback, |
| 398 |
parent_ctx->default_passwd_callback_userdata)); |
| 399 |
if (x509 == nullptr) { |
| 400 |
OPENSSL_PUT_ERROR(SSL, ERR_R_PEM_LIB); |
| 401 |
return 0; |
| 402 |
} |
| 403 |
|
| 404 |
if (ctx != nullptr) { |
| 405 |
ret = SSL_CTX_use_certificate(ctx, x509.get()); |
| 406 |
} else { |
| 407 |
ret = SSL_use_certificate(ssl, x509.get()); |
| 408 |
} |
| 409 |
|
| 410 |
if (ERR_peek_error() != 0) { |
| 411 |
ret = 0; // Key/certificate mismatch doesn't imply ret==0 ... |
| 412 |
} |
| 413 |
|
| 414 |
if (ret) { |
| 415 |
// If we could set up our certificate, now proceed to the CA |
| 416 |
// certificates. |
| 417 |
X509 *ca; |
| 418 |
int temp_ret; |
| 419 |
uint32_t err; |
| 420 |
|
| 421 |
if (ctx != nullptr) { |
| 422 |
SSL_CTX_clear_chain_certs(ctx); |
| 423 |
} else { |
| 424 |
SSL_clear_chain_certs(ssl); |
| 425 |
} |
| 426 |
|
| 427 |
while ((ca = PEM_read_bio_X509( |
| 428 |
in.get(), nullptr, parent_ctx->default_passwd_callback, |
| 429 |
parent_ctx->default_passwd_callback_userdata)) != nullptr) { |
| 430 |
if (ctx != nullptr) { |
| 431 |
temp_ret = SSL_CTX_add0_chain_cert(ctx, ca); |
| 432 |
} else { |
| 433 |
temp_ret = SSL_add0_chain_cert(ssl, ca); |
| 434 |
} |
| 435 |
|
| 436 |
if (temp_ret == 0) { |
| 437 |
X509_free(ca); |
| 438 |
return 0; |
| 439 |
} |
| 440 |
// Note that we must not free |ca| if it was successfully added to the |
| 441 |
// chain (while we must free the main certificate, since its reference |
| 442 |
// count is increased by |SSL_CTX_use_certificate|). |
| 443 |
} |
| 444 |
|
| 445 |
// When the while loop ends, it's usually just EOF. |
| 446 |
err = ERR_peek_last_error(); |
| 447 |
if (ERR_GET_LIB(err) == ERR_LIB_PEM && |
| 448 |
ERR_GET_REASON(err) == PEM_R_NO_START_LINE) { |
| 449 |
ERR_clear_error(); |
| 450 |
} else { |
| 451 |
ret = 0; // some real error |
| 452 |
} |
| 453 |
} |
| 454 |
|
| 455 |
return ret; |
| 456 |
} |
| 457 |
|
| 458 |
|
| 459 |
int SSL_CTX_use_certificate_chain_file(SSL_CTX *ctx, const char *file) { |
| 460 |
if (ctx == nullptr || file == nullptr) { |
| 461 |
return 0; |
| 462 |
} |
| 463 |
return use_certificate_chain_file(ctx, nullptr, file); |
| 464 |
} |
| 465 |
|
| 466 |
int SSL_use_certificate_chain_file(SSL *ssl, const char *file) { |
| 467 |
if (ssl == nullptr || file == nullptr) { |
| 468 |
return 0; |
| 469 |
} |
| 470 |
return use_certificate_chain_file(nullptr, ssl, file); |
| 471 |
} |
| 472 |
|
| 473 |
void SSL_CTX_set_default_passwd_cb(SSL_CTX *ctx, pem_password_cb *cb) { |
| 474 |
ctx->default_passwd_callback = cb; |
| 475 |
} |
| 476 |
|
| 477 |
pem_password_cb *SSL_CTX_get_default_passwd_cb(const SSL_CTX *ctx) { |
| 478 |
return ctx->default_passwd_callback; |
| 479 |
} |
| 480 |
|
| 481 |
void SSL_CTX_set_default_passwd_cb_userdata(SSL_CTX *ctx, void *data) { |
| 482 |
ctx->default_passwd_callback_userdata = data; |
| 483 |
} |
| 484 |
|
| 485 |
void *SSL_CTX_get_default_passwd_cb_userdata(const SSL_CTX *ctx) { |
| 486 |
return ctx->default_passwd_callback_userdata; |
| 487 |
} |