1 1.1.1.7 christos /* 2 1.1.1.11 christos * Copyright 1995-2022 The OpenSSL Project Authors. All Rights Reserved. 3 1.1.1.4 spz * 4 1.1.1.12 christos * Licensed under the Apache License 2.0 (the "License"). You may not use 5 1.1.1.7 christos * this file except in compliance with the License. You can obtain a copy 6 1.1.1.7 christos * in the file LICENSE in the source distribution or at 7 1.1.1.7 christos * https://www.openssl.org/source/license.html 8 1.1 christos */ 9 1.1 christos 10 1.1 christos #include <stdio.h> 11 1.1.1.9 christos #include "ssl_local.h" 12 1.1.1.12 christos #include "internal/packet.h" 13 1.1 christos #include <openssl/bio.h> 14 1.1 christos #include <openssl/objects.h> 15 1.1 christos #include <openssl/evp.h> 16 1.1 christos #include <openssl/x509.h> 17 1.1.1.12 christos #include <openssl/x509v3.h> 18 1.1 christos #include <openssl/pem.h> 19 1.1 christos 20 1.1 christos static int ssl_set_cert(CERT *c, X509 *x509); 21 1.1 christos static int ssl_set_pkey(CERT *c, EVP_PKEY *pkey); 22 1.1.1.8 christos 23 1.1.1.8 christos #define SYNTHV1CONTEXT (SSL_EXT_TLS1_2_AND_BELOW_ONLY \ 24 1.1.1.8 christos | SSL_EXT_CLIENT_HELLO \ 25 1.1.1.8 christos | SSL_EXT_TLS1_2_SERVER_HELLO \ 26 1.1.1.8 christos | SSL_EXT_IGNORE_ON_RESUMPTION) 27 1.1.1.8 christos 28 1.1 christos int SSL_use_certificate(SSL *ssl, X509 *x) 29 1.1.1.4 spz { 30 1.1.1.7 christos int rv; 31 1.1.1.4 spz if (x == NULL) { 32 1.1.1.12 christos ERR_raise(ERR_LIB_SSL, ERR_R_PASSED_NULL_PARAMETER); 33 1.1.1.8 christos return 0; 34 1.1.1.4 spz } 35 1.1.1.12 christos 36 1.1.1.7 christos rv = ssl_security_cert(ssl, NULL, x, 0, 1); 37 1.1.1.7 christos if (rv != 1) { 38 1.1.1.12 christos ERR_raise(ERR_LIB_SSL, rv); 39 1.1.1.7 christos return 0; 40 1.1.1.4 spz } 41 1.1.1.7 christos 42 1.1.1.8 christos return ssl_set_cert(ssl->cert, x); 43 1.1.1.4 spz } 44 1.1 christos 45 1.1 christos int SSL_use_certificate_file(SSL *ssl, const char *file, int type) 46 1.1.1.4 spz { 47 1.1.1.4 spz int j; 48 1.1.1.4 spz BIO *in; 49 1.1.1.4 spz int ret = 0; 50 1.1.1.12 christos X509 *cert = NULL, *x = NULL; 51 1.1.1.4 spz 52 1.1.1.7 christos in = BIO_new(BIO_s_file()); 53 1.1.1.4 spz if (in == NULL) { 54 1.1.1.12 christos ERR_raise(ERR_LIB_SSL, ERR_R_BUF_LIB); 55 1.1.1.4 spz goto end; 56 1.1.1.4 spz } 57 1.1.1.4 spz 58 1.1.1.4 spz if (BIO_read_filename(in, file) <= 0) { 59 1.1.1.12 christos ERR_raise(ERR_LIB_SSL, ERR_R_SYS_LIB); 60 1.1.1.12 christos goto end; 61 1.1.1.12 christos } 62 1.1.1.12 christos 63 1.1.1.12 christos if (type != SSL_FILETYPE_ASN1 && type != SSL_FILETYPE_PEM) { 64 1.1.1.12 christos ERR_raise(ERR_LIB_SSL, SSL_R_BAD_SSL_FILETYPE); 65 1.1.1.12 christos goto end; 66 1.1.1.12 christos } 67 1.1.1.12 christos x = X509_new_ex(ssl->ctx->libctx, ssl->ctx->propq); 68 1.1.1.12 christos if (x == NULL) { 69 1.1.1.12 christos ERR_raise(ERR_LIB_SSL, ERR_R_MALLOC_FAILURE); 70 1.1.1.4 spz goto end; 71 1.1.1.4 spz } 72 1.1.1.4 spz if (type == SSL_FILETYPE_ASN1) { 73 1.1.1.4 spz j = ERR_R_ASN1_LIB; 74 1.1.1.12 christos cert = d2i_X509_bio(in, &x); 75 1.1.1.4 spz } else if (type == SSL_FILETYPE_PEM) { 76 1.1.1.4 spz j = ERR_R_PEM_LIB; 77 1.1.1.12 christos cert = PEM_read_bio_X509(in, &x, ssl->default_passwd_callback, 78 1.1.1.12 christos ssl->default_passwd_callback_userdata); 79 1.1.1.4 spz } else { 80 1.1.1.12 christos ERR_raise(ERR_LIB_SSL, SSL_R_BAD_SSL_FILETYPE); 81 1.1.1.4 spz goto end; 82 1.1.1.4 spz } 83 1.1.1.4 spz 84 1.1.1.12 christos if (cert == NULL) { 85 1.1.1.12 christos ERR_raise(ERR_LIB_SSL, j); 86 1.1.1.4 spz goto end; 87 1.1.1.4 spz } 88 1.1.1.4 spz 89 1.1.1.4 spz ret = SSL_use_certificate(ssl, x); 90 1.1.1.4 spz end: 91 1.1.1.7 christos X509_free(x); 92 1.1.1.7 christos BIO_free(in); 93 1.1.1.8 christos return ret; 94 1.1.1.4 spz } 95 1.1 christos 96 1.1 christos int SSL_use_certificate_ASN1(SSL *ssl, const unsigned char *d, int len) 97 1.1.1.4 spz { 98 1.1.1.4 spz X509 *x; 99 1.1.1.4 spz int ret; 100 1.1.1.4 spz 101 1.1.1.12 christos x = X509_new_ex(ssl->ctx->libctx, ssl->ctx->propq); 102 1.1.1.4 spz if (x == NULL) { 103 1.1.1.12 christos ERR_raise(ERR_LIB_SSL, ERR_R_MALLOC_FAILURE); 104 1.1.1.8 christos return 0; 105 1.1.1.4 spz } 106 1.1.1.4 spz 107 1.1.1.12 christos if (d2i_X509(&x, &d, (long)len)== NULL) { 108 1.1.1.12 christos X509_free(x); 109 1.1.1.12 christos ERR_raise(ERR_LIB_SSL, ERR_R_ASN1_LIB); 110 1.1.1.8 christos return 0; 111 1.1.1.4 spz } 112 1.1.1.4 spz 113 1.1.1.12 christos ret = SSL_use_certificate(ssl, x); 114 1.1.1.12 christos X509_free(x); 115 1.1.1.8 christos return ret; 116 1.1.1.4 spz } 117 1.1 christos 118 1.1 christos static int ssl_set_pkey(CERT *c, EVP_PKEY *pkey) 119 1.1.1.4 spz { 120 1.1.1.8 christos size_t i; 121 1.1.1.8 christos 122 1.1.1.8 christos if (ssl_cert_lookup_by_pkey(pkey, &i) == NULL) { 123 1.1.1.12 christos ERR_raise(ERR_LIB_SSL, SSL_R_UNKNOWN_CERTIFICATE_TYPE); 124 1.1.1.8 christos return 0; 125 1.1.1.4 spz } 126 1.1.1.4 spz 127 1.1.1.12 christos if (c->pkeys[i].x509 != NULL 128 1.1.1.12 christos && !X509_check_private_key(c->pkeys[i].x509, pkey)) 129 1.1.1.12 christos return 0; 130 1.1.1.4 spz 131 1.1.1.7 christos EVP_PKEY_free(c->pkeys[i].privatekey); 132 1.1.1.7 christos EVP_PKEY_up_ref(pkey); 133 1.1.1.4 spz c->pkeys[i].privatekey = pkey; 134 1.1.1.8 christos c->key = &c->pkeys[i]; 135 1.1.1.8 christos return 1; 136 1.1.1.4 spz } 137 1.1 christos 138 1.1 christos int SSL_use_PrivateKey(SSL *ssl, EVP_PKEY *pkey) 139 1.1.1.4 spz { 140 1.1.1.4 spz int ret; 141 1.1 christos 142 1.1.1.4 spz if (pkey == NULL) { 143 1.1.1.12 christos ERR_raise(ERR_LIB_SSL, ERR_R_PASSED_NULL_PARAMETER); 144 1.1.1.8 christos return 0; 145 1.1.1.4 spz } 146 1.1.1.4 spz ret = ssl_set_pkey(ssl->cert, pkey); 147 1.1.1.8 christos return ret; 148 1.1.1.4 spz } 149 1.1 christos 150 1.1 christos int SSL_use_PrivateKey_file(SSL *ssl, const char *file, int type) 151 1.1.1.4 spz { 152 1.1.1.4 spz int j, ret = 0; 153 1.1.1.4 spz BIO *in; 154 1.1.1.4 spz EVP_PKEY *pkey = NULL; 155 1.1.1.4 spz 156 1.1.1.7 christos in = BIO_new(BIO_s_file()); 157 1.1.1.4 spz if (in == NULL) { 158 1.1.1.12 christos ERR_raise(ERR_LIB_SSL, ERR_R_BUF_LIB); 159 1.1.1.4 spz goto end; 160 1.1.1.4 spz } 161 1.1.1.4 spz 162 1.1.1.4 spz if (BIO_read_filename(in, file) <= 0) { 163 1.1.1.12 christos ERR_raise(ERR_LIB_SSL, ERR_R_SYS_LIB); 164 1.1.1.4 spz goto end; 165 1.1.1.4 spz } 166 1.1.1.4 spz if (type == SSL_FILETYPE_PEM) { 167 1.1.1.4 spz j = ERR_R_PEM_LIB; 168 1.1.1.12 christos pkey = PEM_read_bio_PrivateKey_ex(in, NULL, 169 1.1.1.12 christos ssl->default_passwd_callback, 170 1.1.1.12 christos ssl->default_passwd_callback_userdata, 171 1.1.1.12 christos ssl->ctx->libctx, 172 1.1.1.12 christos ssl->ctx->propq); 173 1.1.1.4 spz } else if (type == SSL_FILETYPE_ASN1) { 174 1.1.1.4 spz j = ERR_R_ASN1_LIB; 175 1.1.1.12 christos pkey = d2i_PrivateKey_ex_bio(in, NULL, ssl->ctx->libctx, 176 1.1.1.12 christos ssl->ctx->propq); 177 1.1.1.4 spz } else { 178 1.1.1.12 christos ERR_raise(ERR_LIB_SSL, SSL_R_BAD_SSL_FILETYPE); 179 1.1.1.4 spz goto end; 180 1.1.1.4 spz } 181 1.1.1.4 spz if (pkey == NULL) { 182 1.1.1.12 christos ERR_raise(ERR_LIB_SSL, j); 183 1.1.1.4 spz goto end; 184 1.1.1.4 spz } 185 1.1.1.4 spz ret = SSL_use_PrivateKey(ssl, pkey); 186 1.1.1.4 spz EVP_PKEY_free(pkey); 187 1.1.1.4 spz end: 188 1.1.1.7 christos BIO_free(in); 189 1.1.1.8 christos return ret; 190 1.1.1.4 spz } 191 1.1 christos 192 1.1.1.4 spz int SSL_use_PrivateKey_ASN1(int type, SSL *ssl, const unsigned char *d, 193 1.1.1.4 spz long len) 194 1.1.1.4 spz { 195 1.1.1.4 spz int ret; 196 1.1.1.4 spz const unsigned char *p; 197 1.1.1.4 spz EVP_PKEY *pkey; 198 1.1.1.4 spz 199 1.1.1.4 spz p = d; 200 1.1.1.12 christos if ((pkey = d2i_PrivateKey_ex(type, NULL, &p, (long)len, ssl->ctx->libctx, 201 1.1.1.12 christos ssl->ctx->propq)) == NULL) { 202 1.1.1.12 christos ERR_raise(ERR_LIB_SSL, ERR_R_ASN1_LIB); 203 1.1.1.8 christos return 0; 204 1.1.1.4 spz } 205 1.1.1.4 spz 206 1.1.1.4 spz ret = SSL_use_PrivateKey(ssl, pkey); 207 1.1.1.4 spz EVP_PKEY_free(pkey); 208 1.1.1.8 christos return ret; 209 1.1.1.4 spz } 210 1.1 christos 211 1.1 christos int SSL_CTX_use_certificate(SSL_CTX *ctx, X509 *x) 212 1.1.1.4 spz { 213 1.1.1.7 christos int rv; 214 1.1.1.4 spz if (x == NULL) { 215 1.1.1.12 christos ERR_raise(ERR_LIB_SSL, ERR_R_PASSED_NULL_PARAMETER); 216 1.1.1.8 christos return 0; 217 1.1.1.4 spz } 218 1.1.1.12 christos 219 1.1.1.7 christos rv = ssl_security_cert(NULL, ctx, x, 0, 1); 220 1.1.1.7 christos if (rv != 1) { 221 1.1.1.12 christos ERR_raise(ERR_LIB_SSL, rv); 222 1.1.1.7 christos return 0; 223 1.1.1.4 spz } 224 1.1.1.8 christos return ssl_set_cert(ctx->cert, x); 225 1.1.1.4 spz } 226 1.1 christos 227 1.1 christos static int ssl_set_cert(CERT *c, X509 *x) 228 1.1.1.4 spz { 229 1.1.1.4 spz EVP_PKEY *pkey; 230 1.1.1.8 christos size_t i; 231 1.1.1.4 spz 232 1.1.1.7 christos pkey = X509_get0_pubkey(x); 233 1.1.1.4 spz if (pkey == NULL) { 234 1.1.1.12 christos ERR_raise(ERR_LIB_SSL, SSL_R_X509_LIB); 235 1.1.1.8 christos return 0; 236 1.1.1.4 spz } 237 1.1.1.4 spz 238 1.1.1.8 christos if (ssl_cert_lookup_by_pkey(pkey, &i) == NULL) { 239 1.1.1.12 christos ERR_raise(ERR_LIB_SSL, SSL_R_UNKNOWN_CERTIFICATE_TYPE); 240 1.1.1.7 christos return 0; 241 1.1.1.4 spz } 242 1.1.1.12 christos 243 1.1.1.12 christos if (i == SSL_PKEY_ECC && !EVP_PKEY_can_sign(pkey)) { 244 1.1.1.12 christos ERR_raise(ERR_LIB_SSL, SSL_R_ECC_CERT_NOT_FOR_SIGNING); 245 1.1.1.7 christos return 0; 246 1.1.1.7 christos } 247 1.1.1.12 christos 248 1.1.1.4 spz if (c->pkeys[i].privatekey != NULL) { 249 1.1.1.5 christos /* 250 1.1.1.5 christos * The return code from EVP_PKEY_copy_parameters is deliberately 251 1.1.1.5 christos * ignored. Some EVP_PKEY types cannot do this. 252 1.1.1.5 christos */ 253 1.1.1.4 spz EVP_PKEY_copy_parameters(pkey, c->pkeys[i].privatekey); 254 1.1.1.4 spz ERR_clear_error(); 255 1.1 christos 256 1.1.1.4 spz if (!X509_check_private_key(x, c->pkeys[i].privatekey)) { 257 1.1.1.4 spz /* 258 1.1.1.4 spz * don't fail for a cert/key mismatch, just free current private 259 1.1.1.4 spz * key (when switching to a different cert & key, first this 260 1.1.1.4 spz * function should be used, then ssl_set_pkey 261 1.1.1.4 spz */ 262 1.1.1.4 spz EVP_PKEY_free(c->pkeys[i].privatekey); 263 1.1.1.4 spz c->pkeys[i].privatekey = NULL; 264 1.1.1.4 spz /* clear error queue */ 265 1.1.1.4 spz ERR_clear_error(); 266 1.1.1.4 spz } 267 1.1.1.4 spz } 268 1.1.1.4 spz 269 1.1.1.7 christos X509_free(c->pkeys[i].x509); 270 1.1.1.7 christos X509_up_ref(x); 271 1.1.1.4 spz c->pkeys[i].x509 = x; 272 1.1.1.4 spz c->key = &(c->pkeys[i]); 273 1.1.1.4 spz 274 1.1.1.7 christos return 1; 275 1.1.1.4 spz } 276 1.1 christos 277 1.1 christos int SSL_CTX_use_certificate_file(SSL_CTX *ctx, const char *file, int type) 278 1.1.1.4 spz { 279 1.1.1.12 christos int j = SSL_R_BAD_VALUE; 280 1.1.1.4 spz BIO *in; 281 1.1.1.4 spz int ret = 0; 282 1.1.1.12 christos X509 *x = NULL, *cert = NULL; 283 1.1.1.4 spz 284 1.1.1.7 christos in = BIO_new(BIO_s_file()); 285 1.1.1.4 spz if (in == NULL) { 286 1.1.1.12 christos ERR_raise(ERR_LIB_SSL, ERR_R_BUF_LIB); 287 1.1.1.4 spz goto end; 288 1.1.1.4 spz } 289 1.1.1.4 spz 290 1.1.1.4 spz if (BIO_read_filename(in, file) <= 0) { 291 1.1.1.12 christos ERR_raise(ERR_LIB_SSL, ERR_R_SYS_LIB); 292 1.1.1.12 christos goto end; 293 1.1.1.12 christos } 294 1.1.1.12 christos if (type != SSL_FILETYPE_ASN1 && type != SSL_FILETYPE_PEM) { 295 1.1.1.12 christos ERR_raise(ERR_LIB_SSL, SSL_R_BAD_SSL_FILETYPE); 296 1.1.1.12 christos goto end; 297 1.1.1.12 christos } 298 1.1.1.12 christos x = X509_new_ex(ctx->libctx, ctx->propq); 299 1.1.1.12 christos if (x == NULL) { 300 1.1.1.12 christos ERR_raise(ERR_LIB_SSL, ERR_R_MALLOC_FAILURE); 301 1.1.1.4 spz goto end; 302 1.1.1.4 spz } 303 1.1.1.4 spz if (type == SSL_FILETYPE_ASN1) { 304 1.1.1.4 spz j = ERR_R_ASN1_LIB; 305 1.1.1.12 christos cert = d2i_X509_bio(in, &x); 306 1.1.1.4 spz } else if (type == SSL_FILETYPE_PEM) { 307 1.1.1.4 spz j = ERR_R_PEM_LIB; 308 1.1.1.12 christos cert = PEM_read_bio_X509(in, &x, ctx->default_passwd_callback, 309 1.1.1.12 christos ctx->default_passwd_callback_userdata); 310 1.1.1.4 spz } 311 1.1.1.12 christos if (cert == NULL) { 312 1.1.1.12 christos ERR_raise(ERR_LIB_SSL, j); 313 1.1.1.4 spz goto end; 314 1.1.1.4 spz } 315 1.1.1.4 spz 316 1.1.1.4 spz ret = SSL_CTX_use_certificate(ctx, x); 317 1.1.1.4 spz end: 318 1.1.1.7 christos X509_free(x); 319 1.1.1.7 christos BIO_free(in); 320 1.1.1.8 christos return ret; 321 1.1.1.4 spz } 322 1.1 christos 323 1.1.1.7 christos int SSL_CTX_use_certificate_ASN1(SSL_CTX *ctx, int len, const unsigned char *d) 324 1.1.1.4 spz { 325 1.1.1.4 spz X509 *x; 326 1.1.1.4 spz int ret; 327 1.1.1.4 spz 328 1.1.1.12 christos x = X509_new_ex(ctx->libctx, ctx->propq); 329 1.1.1.4 spz if (x == NULL) { 330 1.1.1.12 christos ERR_raise(ERR_LIB_SSL, ERR_R_MALLOC_FAILURE); 331 1.1.1.8 christos return 0; 332 1.1.1.4 spz } 333 1.1.1.4 spz 334 1.1.1.12 christos if (d2i_X509(&x, &d, (long)len) == NULL) { 335 1.1.1.12 christos X509_free(x); 336 1.1.1.12 christos ERR_raise(ERR_LIB_SSL, ERR_R_ASN1_LIB); 337 1.1.1.5 christos return 0; 338 1.1.1.5 christos } 339 1.1.1.4 spz 340 1.1.1.12 christos ret = SSL_CTX_use_certificate(ctx, x); 341 1.1.1.12 christos X509_free(x); 342 1.1.1.8 christos return ret; 343 1.1.1.4 spz } 344 1.1 christos 345 1.1 christos int SSL_CTX_use_PrivateKey(SSL_CTX *ctx, EVP_PKEY *pkey) 346 1.1.1.4 spz { 347 1.1.1.4 spz if (pkey == NULL) { 348 1.1.1.12 christos ERR_raise(ERR_LIB_SSL, ERR_R_PASSED_NULL_PARAMETER); 349 1.1.1.8 christos return 0; 350 1.1.1.4 spz } 351 1.1.1.8 christos return ssl_set_pkey(ctx->cert, pkey); 352 1.1.1.4 spz } 353 1.1 christos 354 1.1 christos int SSL_CTX_use_PrivateKey_file(SSL_CTX *ctx, const char *file, int type) 355 1.1.1.4 spz { 356 1.1.1.4 spz int j, ret = 0; 357 1.1.1.4 spz BIO *in; 358 1.1.1.4 spz EVP_PKEY *pkey = NULL; 359 1.1.1.4 spz 360 1.1.1.7 christos in = BIO_new(BIO_s_file()); 361 1.1.1.4 spz if (in == NULL) { 362 1.1.1.12 christos ERR_raise(ERR_LIB_SSL, ERR_R_BUF_LIB); 363 1.1.1.4 spz goto end; 364 1.1.1.4 spz } 365 1.1.1.4 spz 366 1.1.1.4 spz if (BIO_read_filename(in, file) <= 0) { 367 1.1.1.12 christos ERR_raise(ERR_LIB_SSL, ERR_R_SYS_LIB); 368 1.1.1.4 spz goto end; 369 1.1.1.4 spz } 370 1.1.1.4 spz if (type == SSL_FILETYPE_PEM) { 371 1.1.1.4 spz j = ERR_R_PEM_LIB; 372 1.1.1.12 christos pkey = PEM_read_bio_PrivateKey_ex(in, NULL, 373 1.1.1.4 spz ctx->default_passwd_callback, 374 1.1.1.12 christos ctx->default_passwd_callback_userdata, 375 1.1.1.12 christos ctx->libctx, ctx->propq); 376 1.1.1.4 spz } else if (type == SSL_FILETYPE_ASN1) { 377 1.1.1.4 spz j = ERR_R_ASN1_LIB; 378 1.1.1.12 christos pkey = d2i_PrivateKey_ex_bio(in, NULL, ctx->libctx, ctx->propq); 379 1.1.1.4 spz } else { 380 1.1.1.12 christos ERR_raise(ERR_LIB_SSL, SSL_R_BAD_SSL_FILETYPE); 381 1.1.1.4 spz goto end; 382 1.1.1.4 spz } 383 1.1.1.4 spz if (pkey == NULL) { 384 1.1.1.12 christos ERR_raise(ERR_LIB_SSL, j); 385 1.1.1.4 spz goto end; 386 1.1.1.4 spz } 387 1.1.1.4 spz ret = SSL_CTX_use_PrivateKey(ctx, pkey); 388 1.1.1.4 spz EVP_PKEY_free(pkey); 389 1.1.1.4 spz end: 390 1.1.1.7 christos BIO_free(in); 391 1.1.1.8 christos return ret; 392 1.1.1.4 spz } 393 1.1 christos 394 1.1.1.4 spz int SSL_CTX_use_PrivateKey_ASN1(int type, SSL_CTX *ctx, 395 1.1.1.4 spz const unsigned char *d, long len) 396 1.1.1.4 spz { 397 1.1.1.4 spz int ret; 398 1.1.1.4 spz const unsigned char *p; 399 1.1.1.4 spz EVP_PKEY *pkey; 400 1.1.1.4 spz 401 1.1.1.4 spz p = d; 402 1.1.1.12 christos if ((pkey = d2i_PrivateKey_ex(type, NULL, &p, (long)len, ctx->libctx, 403 1.1.1.12 christos ctx->propq)) == NULL) { 404 1.1.1.12 christos ERR_raise(ERR_LIB_SSL, ERR_R_ASN1_LIB); 405 1.1.1.8 christos return 0; 406 1.1.1.4 spz } 407 1.1.1.4 spz 408 1.1.1.4 spz ret = SSL_CTX_use_PrivateKey(ctx, pkey); 409 1.1.1.4 spz EVP_PKEY_free(pkey); 410 1.1.1.8 christos return ret; 411 1.1.1.4 spz } 412 1.1 christos 413 1.1.1.4 spz /* 414 1.1.1.4 spz * Read a file that contains our certificate in "PEM" format, possibly 415 1.1.1.4 spz * followed by a sequence of CA certificates that should be sent to the peer 416 1.1.1.4 spz * in the Certificate message. 417 1.1 christos */ 418 1.1.1.7 christos static int use_certificate_chain_file(SSL_CTX *ctx, SSL *ssl, const char *file) 419 1.1.1.4 spz { 420 1.1.1.4 spz BIO *in; 421 1.1.1.4 spz int ret = 0; 422 1.1.1.4 spz X509 *x = NULL; 423 1.1.1.7 christos pem_password_cb *passwd_callback; 424 1.1.1.7 christos void *passwd_callback_userdata; 425 1.1.1.12 christos SSL_CTX *real_ctx = (ssl == NULL) ? ctx : ssl->ctx; 426 1.1.1.12 christos 427 1.1.1.12 christos if (ctx == NULL && ssl == NULL) 428 1.1.1.12 christos return 0; 429 1.1.1.4 spz 430 1.1.1.4 spz ERR_clear_error(); /* clear error stack for 431 1.1.1.4 spz * SSL_CTX_use_certificate() */ 432 1.1.1.4 spz 433 1.1.1.7 christos if (ctx != NULL) { 434 1.1.1.7 christos passwd_callback = ctx->default_passwd_callback; 435 1.1.1.7 christos passwd_callback_userdata = ctx->default_passwd_callback_userdata; 436 1.1.1.7 christos } else { 437 1.1.1.7 christos passwd_callback = ssl->default_passwd_callback; 438 1.1.1.7 christos passwd_callback_userdata = ssl->default_passwd_callback_userdata; 439 1.1.1.7 christos } 440 1.1.1.7 christos 441 1.1.1.7 christos in = BIO_new(BIO_s_file()); 442 1.1.1.4 spz if (in == NULL) { 443 1.1.1.12 christos ERR_raise(ERR_LIB_SSL, ERR_R_BUF_LIB); 444 1.1.1.4 spz goto end; 445 1.1.1.4 spz } 446 1.1.1.4 spz 447 1.1.1.4 spz if (BIO_read_filename(in, file) <= 0) { 448 1.1.1.12 christos ERR_raise(ERR_LIB_SSL, ERR_R_SYS_LIB); 449 1.1.1.4 spz goto end; 450 1.1.1.4 spz } 451 1.1.1.4 spz 452 1.1.1.12 christos x = X509_new_ex(real_ctx->libctx, real_ctx->propq); 453 1.1.1.4 spz if (x == NULL) { 454 1.1.1.12 christos ERR_raise(ERR_LIB_SSL, ERR_R_MALLOC_FAILURE); 455 1.1.1.12 christos goto end; 456 1.1.1.12 christos } 457 1.1.1.12 christos if (PEM_read_bio_X509_AUX(in, &x, passwd_callback, 458 1.1.1.12 christos passwd_callback_userdata) == NULL) { 459 1.1.1.12 christos ERR_raise(ERR_LIB_SSL, ERR_R_PEM_LIB); 460 1.1.1.4 spz goto end; 461 1.1.1.4 spz } 462 1.1.1.4 spz 463 1.1.1.7 christos if (ctx) 464 1.1.1.7 christos ret = SSL_CTX_use_certificate(ctx, x); 465 1.1.1.7 christos else 466 1.1.1.7 christos ret = SSL_use_certificate(ssl, x); 467 1.1.1.4 spz 468 1.1.1.4 spz if (ERR_peek_error() != 0) 469 1.1.1.4 spz ret = 0; /* Key/certificate mismatch doesn't imply 470 1.1.1.4 spz * ret==0 ... */ 471 1.1.1.4 spz if (ret) { 472 1.1.1.4 spz /* 473 1.1.1.4 spz * If we could set up our certificate, now proceed to the CA 474 1.1.1.4 spz * certificates. 475 1.1.1.4 spz */ 476 1.1.1.4 spz X509 *ca; 477 1.1.1.4 spz int r; 478 1.1.1.4 spz unsigned long err; 479 1.1.1.4 spz 480 1.1.1.7 christos if (ctx) 481 1.1.1.7 christos r = SSL_CTX_clear_chain_certs(ctx); 482 1.1.1.7 christos else 483 1.1.1.7 christos r = SSL_clear_chain_certs(ssl); 484 1.1.1.4 spz 485 1.1.1.7 christos if (r == 0) { 486 1.1.1.7 christos ret = 0; 487 1.1.1.7 christos goto end; 488 1.1.1.7 christos } 489 1.1.1.7 christos 490 1.1.1.12 christos while (1) { 491 1.1.1.12 christos ca = X509_new_ex(real_ctx->libctx, real_ctx->propq); 492 1.1.1.12 christos if (ca == NULL) { 493 1.1.1.12 christos ERR_raise(ERR_LIB_SSL, ERR_R_MALLOC_FAILURE); 494 1.1.1.4 spz goto end; 495 1.1.1.4 spz } 496 1.1.1.12 christos if (PEM_read_bio_X509(in, &ca, passwd_callback, 497 1.1.1.12 christos passwd_callback_userdata) != NULL) { 498 1.1.1.12 christos if (ctx) 499 1.1.1.12 christos r = SSL_CTX_add0_chain_cert(ctx, ca); 500 1.1.1.12 christos else 501 1.1.1.12 christos r = SSL_add0_chain_cert(ssl, ca); 502 1.1.1.12 christos /* 503 1.1.1.12 christos * Note that we must not free ca if it was successfully added to 504 1.1.1.12 christos * the chain (while we must free the main certificate, since its 505 1.1.1.12 christos * reference count is increased by SSL_CTX_use_certificate). 506 1.1.1.12 christos */ 507 1.1.1.12 christos if (!r) { 508 1.1.1.12 christos X509_free(ca); 509 1.1.1.12 christos ret = 0; 510 1.1.1.12 christos goto end; 511 1.1.1.12 christos } 512 1.1.1.12 christos } else { 513 1.1.1.12 christos X509_free(ca); 514 1.1.1.12 christos break; 515 1.1.1.12 christos } 516 1.1.1.4 spz } 517 1.1.1.4 spz /* When the while loop ends, it's usually just EOF. */ 518 1.1.1.4 spz err = ERR_peek_last_error(); 519 1.1.1.4 spz if (ERR_GET_LIB(err) == ERR_LIB_PEM 520 1.1.1.4 spz && ERR_GET_REASON(err) == PEM_R_NO_START_LINE) 521 1.1.1.4 spz ERR_clear_error(); 522 1.1.1.4 spz else 523 1.1.1.4 spz ret = 0; /* some real error */ 524 1.1.1.4 spz } 525 1.1.1.4 spz 526 1.1.1.4 spz end: 527 1.1.1.7 christos X509_free(x); 528 1.1.1.7 christos BIO_free(in); 529 1.1.1.8 christos return ret; 530 1.1.1.4 spz } 531 1.1.1.6 spz 532 1.1.1.7 christos int SSL_CTX_use_certificate_chain_file(SSL_CTX *ctx, const char *file) 533 1.1.1.7 christos { 534 1.1.1.7 christos return use_certificate_chain_file(ctx, NULL, file); 535 1.1.1.7 christos } 536 1.1.1.7 christos 537 1.1.1.7 christos int SSL_use_certificate_chain_file(SSL *ssl, const char *file) 538 1.1.1.7 christos { 539 1.1.1.7 christos return use_certificate_chain_file(NULL, ssl, file); 540 1.1.1.7 christos } 541 1.1.1.7 christos 542 1.1.1.6 spz static int serverinfo_find_extension(const unsigned char *serverinfo, 543 1.1.1.6 spz size_t serverinfo_length, 544 1.1.1.6 spz unsigned int extension_type, 545 1.1.1.6 spz const unsigned char **extension_data, 546 1.1.1.6 spz size_t *extension_length) 547 1.1.1.6 spz { 548 1.1.1.8 christos PACKET pkt, data; 549 1.1.1.8 christos 550 1.1.1.6 spz *extension_data = NULL; 551 1.1.1.6 spz *extension_length = 0; 552 1.1.1.6 spz if (serverinfo == NULL || serverinfo_length == 0) 553 1.1.1.6 spz return -1; 554 1.1.1.8 christos 555 1.1.1.8 christos if (!PACKET_buf_init(&pkt, serverinfo, serverinfo_length)) 556 1.1.1.8 christos return -1; 557 1.1.1.8 christos 558 1.1.1.6 spz for (;;) { 559 1.1.1.6 spz unsigned int type = 0; 560 1.1.1.8 christos unsigned long context = 0; 561 1.1.1.6 spz 562 1.1.1.6 spz /* end of serverinfo */ 563 1.1.1.8 christos if (PACKET_remaining(&pkt) == 0) 564 1.1.1.6 spz return 0; /* Extension not found */ 565 1.1.1.6 spz 566 1.1.1.8 christos if (!PACKET_get_net_4(&pkt, &context) 567 1.1.1.8 christos || !PACKET_get_net_2(&pkt, &type) 568 1.1.1.8 christos || !PACKET_get_length_prefixed_2(&pkt, &data)) 569 1.1.1.8 christos return -1; 570 1.1.1.6 spz 571 1.1.1.6 spz if (type == extension_type) { 572 1.1.1.8 christos *extension_data = PACKET_data(&data); 573 1.1.1.8 christos *extension_length = PACKET_remaining(&data);; 574 1.1.1.6 spz return 1; /* Success */ 575 1.1.1.6 spz } 576 1.1.1.6 spz } 577 1.1.1.7 christos /* Unreachable */ 578 1.1.1.6 spz } 579 1.1.1.6 spz 580 1.1.1.8 christos static int serverinfoex_srv_parse_cb(SSL *s, unsigned int ext_type, 581 1.1.1.8 christos unsigned int context, 582 1.1.1.8 christos const unsigned char *in, 583 1.1.1.8 christos size_t inlen, X509 *x, size_t chainidx, 584 1.1.1.8 christos int *al, void *arg) 585 1.1.1.6 spz { 586 1.1.1.6 spz 587 1.1.1.6 spz if (inlen != 0) { 588 1.1.1.6 spz *al = SSL_AD_DECODE_ERROR; 589 1.1.1.6 spz return 0; 590 1.1.1.6 spz } 591 1.1.1.6 spz 592 1.1.1.6 spz return 1; 593 1.1.1.6 spz } 594 1.1.1.6 spz 595 1.1.1.8 christos static int serverinfo_srv_parse_cb(SSL *s, unsigned int ext_type, 596 1.1.1.8 christos const unsigned char *in, 597 1.1.1.8 christos size_t inlen, int *al, void *arg) 598 1.1.1.8 christos { 599 1.1.1.8 christos return serverinfoex_srv_parse_cb(s, ext_type, 0, in, inlen, NULL, 0, al, 600 1.1.1.8 christos arg); 601 1.1.1.8 christos } 602 1.1.1.8 christos 603 1.1.1.8 christos static int serverinfoex_srv_add_cb(SSL *s, unsigned int ext_type, 604 1.1.1.8 christos unsigned int context, 605 1.1.1.8 christos const unsigned char **out, 606 1.1.1.8 christos size_t *outlen, X509 *x, size_t chainidx, 607 1.1.1.8 christos int *al, void *arg) 608 1.1.1.6 spz { 609 1.1.1.6 spz const unsigned char *serverinfo = NULL; 610 1.1.1.6 spz size_t serverinfo_length = 0; 611 1.1.1.6 spz 612 1.1.1.8 christos /* We only support extensions for the first Certificate */ 613 1.1.1.8 christos if ((context & SSL_EXT_TLS1_3_CERTIFICATE) != 0 && chainidx > 0) 614 1.1.1.8 christos return 0; 615 1.1.1.8 christos 616 1.1.1.6 spz /* Is there serverinfo data for the chosen server cert? */ 617 1.1.1.6 spz if ((ssl_get_server_cert_serverinfo(s, &serverinfo, 618 1.1.1.6 spz &serverinfo_length)) != 0) { 619 1.1.1.6 spz /* Find the relevant extension from the serverinfo */ 620 1.1.1.6 spz int retval = serverinfo_find_extension(serverinfo, serverinfo_length, 621 1.1.1.6 spz ext_type, out, outlen); 622 1.1.1.6 spz if (retval == -1) { 623 1.1.1.8 christos *al = SSL_AD_INTERNAL_ERROR; 624 1.1.1.6 spz return -1; /* Error */ 625 1.1.1.6 spz } 626 1.1.1.6 spz if (retval == 0) 627 1.1.1.6 spz return 0; /* No extension found, don't send extension */ 628 1.1.1.6 spz return 1; /* Send extension */ 629 1.1.1.6 spz } 630 1.1.1.6 spz return 0; /* No serverinfo data found, don't send 631 1.1.1.6 spz * extension */ 632 1.1.1.6 spz } 633 1.1.1.6 spz 634 1.1.1.8 christos static int serverinfo_srv_add_cb(SSL *s, unsigned int ext_type, 635 1.1.1.8 christos const unsigned char **out, size_t *outlen, 636 1.1.1.8 christos int *al, void *arg) 637 1.1.1.8 christos { 638 1.1.1.8 christos return serverinfoex_srv_add_cb(s, ext_type, 0, out, outlen, NULL, 0, al, 639 1.1.1.8 christos arg); 640 1.1.1.8 christos } 641 1.1.1.8 christos 642 1.1.1.6 spz /* 643 1.1.1.6 spz * With a NULL context, this function just checks that the serverinfo data 644 1.1.1.6 spz * parses correctly. With a non-NULL context, it registers callbacks for 645 1.1.1.6 spz * the included extensions. 646 1.1.1.6 spz */ 647 1.1.1.8 christos static int serverinfo_process_buffer(unsigned int version, 648 1.1.1.8 christos const unsigned char *serverinfo, 649 1.1.1.6 spz size_t serverinfo_length, SSL_CTX *ctx) 650 1.1.1.6 spz { 651 1.1.1.8 christos PACKET pkt; 652 1.1.1.8 christos 653 1.1.1.6 spz if (serverinfo == NULL || serverinfo_length == 0) 654 1.1.1.6 spz return 0; 655 1.1.1.6 spz 656 1.1.1.8 christos if (version != SSL_SERVERINFOV1 && version != SSL_SERVERINFOV2) 657 1.1.1.8 christos return 0; 658 1.1.1.6 spz 659 1.1.1.8 christos if (!PACKET_buf_init(&pkt, serverinfo, serverinfo_length)) 660 1.1.1.8 christos return 0; 661 1.1.1.6 spz 662 1.1.1.8 christos while (PACKET_remaining(&pkt)) { 663 1.1.1.8 christos unsigned long context = 0; 664 1.1.1.8 christos unsigned int ext_type = 0; 665 1.1.1.8 christos PACKET data; 666 1.1.1.6 spz 667 1.1.1.8 christos if ((version == SSL_SERVERINFOV2 && !PACKET_get_net_4(&pkt, &context)) 668 1.1.1.8 christos || !PACKET_get_net_2(&pkt, &ext_type) 669 1.1.1.8 christos || !PACKET_get_length_prefixed_2(&pkt, &data)) 670 1.1.1.6 spz return 0; 671 1.1.1.6 spz 672 1.1.1.8 christos if (ctx == NULL) 673 1.1.1.8 christos continue; 674 1.1.1.6 spz 675 1.1.1.8 christos /* 676 1.1.1.8 christos * The old style custom extensions API could be set separately for 677 1.1.1.8 christos * server/client, i.e. you could set one custom extension for a client, 678 1.1.1.8 christos * and *for the same extension in the same SSL_CTX* you could set a 679 1.1.1.8 christos * custom extension for the server as well. It seems quite weird to be 680 1.1.1.8 christos * setting a custom extension for both client and server in a single 681 1.1.1.8 christos * SSL_CTX - but theoretically possible. This isn't possible in the 682 1.1.1.8 christos * new API. Therefore, if we have V1 serverinfo we use the old API. We 683 1.1.1.8 christos * also use the old API even if we have V2 serverinfo but the context 684 1.1.1.8 christos * looks like an old style <= TLSv1.2 extension. 685 1.1.1.8 christos */ 686 1.1.1.8 christos if (version == SSL_SERVERINFOV1 || context == SYNTHV1CONTEXT) { 687 1.1.1.8 christos if (!SSL_CTX_add_server_custom_ext(ctx, ext_type, 688 1.1.1.8 christos serverinfo_srv_add_cb, 689 1.1.1.8 christos NULL, NULL, 690 1.1.1.8 christos serverinfo_srv_parse_cb, 691 1.1.1.8 christos NULL)) 692 1.1.1.8 christos return 0; 693 1.1.1.8 christos } else { 694 1.1.1.8 christos if (!SSL_CTX_add_custom_ext(ctx, ext_type, context, 695 1.1.1.8 christos serverinfoex_srv_add_cb, 696 1.1.1.8 christos NULL, NULL, 697 1.1.1.8 christos serverinfoex_srv_parse_cb, 698 1.1.1.8 christos NULL)) 699 1.1.1.8 christos return 0; 700 1.1.1.8 christos } 701 1.1.1.6 spz } 702 1.1.1.8 christos 703 1.1.1.8 christos return 1; 704 1.1.1.6 spz } 705 1.1.1.6 spz 706 1.1.1.12 christos static size_t extension_contextoff(unsigned int version) 707 1.1.1.12 christos { 708 1.1.1.12 christos return version == SSL_SERVERINFOV1 ? 4 : 0; 709 1.1.1.12 christos } 710 1.1.1.12 christos 711 1.1.1.12 christos static size_t extension_append_length(unsigned int version, size_t extension_length) 712 1.1.1.12 christos { 713 1.1.1.12 christos return extension_length + extension_contextoff(version); 714 1.1.1.12 christos } 715 1.1.1.12 christos 716 1.1.1.12 christos static void extension_append(unsigned int version, 717 1.1.1.12 christos const unsigned char *extension, 718 1.1.1.12 christos const size_t extension_length, 719 1.1.1.12 christos unsigned char *serverinfo) 720 1.1.1.12 christos { 721 1.1.1.12 christos const size_t contextoff = extension_contextoff(version); 722 1.1.1.12 christos 723 1.1.1.12 christos if (contextoff > 0) { 724 1.1.1.12 christos /* We know this only uses the last 2 bytes */ 725 1.1.1.12 christos serverinfo[0] = 0; 726 1.1.1.12 christos serverinfo[1] = 0; 727 1.1.1.12 christos serverinfo[2] = (SYNTHV1CONTEXT >> 8) & 0xff; 728 1.1.1.12 christos serverinfo[3] = SYNTHV1CONTEXT & 0xff; 729 1.1.1.12 christos } 730 1.1.1.12 christos 731 1.1.1.12 christos memcpy(serverinfo + contextoff, extension, extension_length); 732 1.1.1.12 christos } 733 1.1.1.12 christos 734 1.1.1.8 christos int SSL_CTX_use_serverinfo_ex(SSL_CTX *ctx, unsigned int version, 735 1.1.1.8 christos const unsigned char *serverinfo, 736 1.1.1.8 christos size_t serverinfo_length) 737 1.1.1.6 spz { 738 1.1.1.11 christos unsigned char *new_serverinfo = NULL; 739 1.1.1.6 spz 740 1.1.1.6 spz if (ctx == NULL || serverinfo == NULL || serverinfo_length == 0) { 741 1.1.1.12 christos ERR_raise(ERR_LIB_SSL, ERR_R_PASSED_NULL_PARAMETER); 742 1.1.1.6 spz return 0; 743 1.1.1.6 spz } 744 1.1.1.11 christos if (version == SSL_SERVERINFOV1) { 745 1.1.1.11 christos /* 746 1.1.1.11 christos * Convert serverinfo version v1 to v2 and call yourself recursively 747 1.1.1.11 christos * over the converted serverinfo. 748 1.1.1.11 christos */ 749 1.1.1.11 christos const size_t sinfo_length = extension_append_length(SSL_SERVERINFOV1, 750 1.1.1.11 christos serverinfo_length); 751 1.1.1.11 christos unsigned char *sinfo; 752 1.1.1.11 christos int ret; 753 1.1.1.11 christos 754 1.1.1.11 christos sinfo = OPENSSL_malloc(sinfo_length); 755 1.1.1.11 christos if (sinfo == NULL) { 756 1.1.1.12 christos ERR_raise(ERR_LIB_SSL, ERR_R_MALLOC_FAILURE); 757 1.1.1.11 christos return 0; 758 1.1.1.11 christos } 759 1.1.1.11 christos 760 1.1.1.11 christos extension_append(SSL_SERVERINFOV1, serverinfo, serverinfo_length, sinfo); 761 1.1.1.11 christos 762 1.1.1.11 christos ret = SSL_CTX_use_serverinfo_ex(ctx, SSL_SERVERINFOV2, sinfo, 763 1.1.1.11 christos sinfo_length); 764 1.1.1.11 christos 765 1.1.1.11 christos OPENSSL_free(sinfo); 766 1.1.1.11 christos return ret; 767 1.1.1.11 christos } 768 1.1.1.8 christos if (!serverinfo_process_buffer(version, serverinfo, serverinfo_length, 769 1.1.1.8 christos NULL)) { 770 1.1.1.12 christos ERR_raise(ERR_LIB_SSL, SSL_R_INVALID_SERVERINFO_DATA); 771 1.1.1.6 spz return 0; 772 1.1.1.6 spz } 773 1.1.1.6 spz if (ctx->cert->key == NULL) { 774 1.1.1.12 christos ERR_raise(ERR_LIB_SSL, ERR_R_INTERNAL_ERROR); 775 1.1.1.6 spz return 0; 776 1.1.1.6 spz } 777 1.1.1.6 spz new_serverinfo = OPENSSL_realloc(ctx->cert->key->serverinfo, 778 1.1.1.6 spz serverinfo_length); 779 1.1.1.6 spz if (new_serverinfo == NULL) { 780 1.1.1.12 christos ERR_raise(ERR_LIB_SSL, ERR_R_MALLOC_FAILURE); 781 1.1.1.6 spz return 0; 782 1.1.1.6 spz } 783 1.1.1.6 spz ctx->cert->key->serverinfo = new_serverinfo; 784 1.1.1.6 spz memcpy(ctx->cert->key->serverinfo, serverinfo, serverinfo_length); 785 1.1.1.6 spz ctx->cert->key->serverinfo_length = serverinfo_length; 786 1.1.1.6 spz 787 1.1.1.6 spz /* 788 1.1.1.6 spz * Now that the serverinfo is validated and stored, go ahead and 789 1.1.1.6 spz * register callbacks. 790 1.1.1.6 spz */ 791 1.1.1.8 christos if (!serverinfo_process_buffer(version, serverinfo, serverinfo_length, 792 1.1.1.8 christos ctx)) { 793 1.1.1.12 christos ERR_raise(ERR_LIB_SSL, SSL_R_INVALID_SERVERINFO_DATA); 794 1.1.1.6 spz return 0; 795 1.1.1.6 spz } 796 1.1.1.6 spz return 1; 797 1.1.1.6 spz } 798 1.1.1.6 spz 799 1.1.1.8 christos int SSL_CTX_use_serverinfo(SSL_CTX *ctx, const unsigned char *serverinfo, 800 1.1.1.8 christos size_t serverinfo_length) 801 1.1.1.8 christos { 802 1.1.1.8 christos return SSL_CTX_use_serverinfo_ex(ctx, SSL_SERVERINFOV1, serverinfo, 803 1.1.1.8 christos serverinfo_length); 804 1.1.1.8 christos } 805 1.1.1.8 christos 806 1.1.1.6 spz int SSL_CTX_use_serverinfo_file(SSL_CTX *ctx, const char *file) 807 1.1.1.6 spz { 808 1.1.1.6 spz unsigned char *serverinfo = NULL; 809 1.1.1.7 christos unsigned char *tmp; 810 1.1.1.6 spz size_t serverinfo_length = 0; 811 1.1.1.6 spz unsigned char *extension = 0; 812 1.1.1.6 spz long extension_length = 0; 813 1.1.1.6 spz char *name = NULL; 814 1.1.1.6 spz char *header = NULL; 815 1.1.1.12 christos static const char namePrefix1[] = "SERVERINFO FOR "; 816 1.1.1.12 christos static const char namePrefix2[] = "SERVERINFOV2 FOR "; 817 1.1.1.12 christos unsigned int name_len; 818 1.1.1.6 spz int ret = 0; 819 1.1.1.6 spz BIO *bin = NULL; 820 1.1.1.11 christos size_t num_extensions = 0; 821 1.1.1.6 spz 822 1.1.1.6 spz if (ctx == NULL || file == NULL) { 823 1.1.1.12 christos ERR_raise(ERR_LIB_SSL, ERR_R_PASSED_NULL_PARAMETER); 824 1.1.1.6 spz goto end; 825 1.1.1.6 spz } 826 1.1.1.6 spz 827 1.1.1.7 christos bin = BIO_new(BIO_s_file()); 828 1.1.1.6 spz if (bin == NULL) { 829 1.1.1.12 christos ERR_raise(ERR_LIB_SSL, ERR_R_BUF_LIB); 830 1.1.1.6 spz goto end; 831 1.1.1.6 spz } 832 1.1.1.6 spz if (BIO_read_filename(bin, file) <= 0) { 833 1.1.1.12 christos ERR_raise(ERR_LIB_SSL, ERR_R_SYS_LIB); 834 1.1.1.6 spz goto end; 835 1.1.1.6 spz } 836 1.1.1.6 spz 837 1.1.1.6 spz for (num_extensions = 0;; num_extensions++) { 838 1.1.1.8 christos unsigned int version; 839 1.1.1.11 christos size_t append_length; 840 1.1.1.8 christos 841 1.1.1.6 spz if (PEM_read_bio(bin, &name, &header, &extension, &extension_length) 842 1.1.1.6 spz == 0) { 843 1.1.1.6 spz /* 844 1.1.1.6 spz * There must be at least one extension in this file 845 1.1.1.6 spz */ 846 1.1.1.6 spz if (num_extensions == 0) { 847 1.1.1.12 christos ERR_raise(ERR_LIB_SSL, SSL_R_NO_PEM_EXTENSIONS); 848 1.1.1.6 spz goto end; 849 1.1.1.6 spz } else /* End of file, we're done */ 850 1.1.1.6 spz break; 851 1.1.1.6 spz } 852 1.1.1.6 spz /* Check that PEM name starts with "BEGIN SERVERINFO FOR " */ 853 1.1.1.12 christos name_len = strlen(name); 854 1.1.1.12 christos if (name_len < sizeof(namePrefix1) - 1) { 855 1.1.1.12 christos ERR_raise(ERR_LIB_SSL, SSL_R_PEM_NAME_TOO_SHORT); 856 1.1.1.6 spz goto end; 857 1.1.1.6 spz } 858 1.1.1.12 christos if (strncmp(name, namePrefix1, sizeof(namePrefix1) - 1) == 0) { 859 1.1.1.8 christos version = SSL_SERVERINFOV1; 860 1.1.1.8 christos } else { 861 1.1.1.12 christos if (name_len < sizeof(namePrefix2) - 1) { 862 1.1.1.12 christos ERR_raise(ERR_LIB_SSL, SSL_R_PEM_NAME_TOO_SHORT); 863 1.1.1.8 christos goto end; 864 1.1.1.8 christos } 865 1.1.1.12 christos if (strncmp(name, namePrefix2, sizeof(namePrefix2) - 1) != 0) { 866 1.1.1.12 christos ERR_raise(ERR_LIB_SSL, SSL_R_PEM_NAME_BAD_PREFIX); 867 1.1.1.8 christos goto end; 868 1.1.1.8 christos } 869 1.1.1.8 christos version = SSL_SERVERINFOV2; 870 1.1.1.6 spz } 871 1.1.1.6 spz /* 872 1.1.1.6 spz * Check that the decoded PEM data is plausible (valid length field) 873 1.1.1.6 spz */ 874 1.1.1.8 christos if (version == SSL_SERVERINFOV1) { 875 1.1.1.8 christos /* 4 byte header: 2 bytes type, 2 bytes len */ 876 1.1.1.8 christos if (extension_length < 4 877 1.1.1.8 christos || (extension[2] << 8) + extension[3] 878 1.1.1.8 christos != extension_length - 4) { 879 1.1.1.12 christos ERR_raise(ERR_LIB_SSL, SSL_R_BAD_DATA); 880 1.1.1.8 christos goto end; 881 1.1.1.8 christos } 882 1.1.1.8 christos } else { 883 1.1.1.8 christos /* 8 byte header: 4 bytes context, 2 bytes type, 2 bytes len */ 884 1.1.1.8 christos if (extension_length < 8 885 1.1.1.8 christos || (extension[6] << 8) + extension[7] 886 1.1.1.8 christos != extension_length - 8) { 887 1.1.1.12 christos ERR_raise(ERR_LIB_SSL, SSL_R_BAD_DATA); 888 1.1.1.8 christos goto end; 889 1.1.1.8 christos } 890 1.1.1.6 spz } 891 1.1.1.6 spz /* Append the decoded extension to the serverinfo buffer */ 892 1.1.1.11 christos append_length = extension_append_length(version, extension_length); 893 1.1.1.11 christos tmp = OPENSSL_realloc(serverinfo, serverinfo_length + append_length); 894 1.1.1.7 christos if (tmp == NULL) { 895 1.1.1.12 christos ERR_raise(ERR_LIB_SSL, ERR_R_MALLOC_FAILURE); 896 1.1.1.6 spz goto end; 897 1.1.1.6 spz } 898 1.1.1.7 christos serverinfo = tmp; 899 1.1.1.11 christos extension_append(version, extension, extension_length, 900 1.1.1.11 christos serverinfo + serverinfo_length); 901 1.1.1.11 christos serverinfo_length += append_length; 902 1.1.1.6 spz 903 1.1.1.6 spz OPENSSL_free(name); 904 1.1.1.6 spz name = NULL; 905 1.1.1.6 spz OPENSSL_free(header); 906 1.1.1.6 spz header = NULL; 907 1.1.1.6 spz OPENSSL_free(extension); 908 1.1.1.6 spz extension = NULL; 909 1.1.1.6 spz } 910 1.1.1.6 spz 911 1.1.1.8 christos ret = SSL_CTX_use_serverinfo_ex(ctx, SSL_SERVERINFOV2, serverinfo, 912 1.1.1.8 christos serverinfo_length); 913 1.1.1.6 spz end: 914 1.1.1.6 spz /* SSL_CTX_use_serverinfo makes a local copy of the serverinfo. */ 915 1.1.1.6 spz OPENSSL_free(name); 916 1.1.1.6 spz OPENSSL_free(header); 917 1.1.1.6 spz OPENSSL_free(extension); 918 1.1.1.6 spz OPENSSL_free(serverinfo); 919 1.1.1.7 christos BIO_free(bin); 920 1.1.1.6 spz return ret; 921 1.1.1.6 spz } 922 1.1.1.8 christos 923 1.1.1.8 christos static int ssl_set_cert_and_key(SSL *ssl, SSL_CTX *ctx, X509 *x509, EVP_PKEY *privatekey, 924 1.1.1.8 christos STACK_OF(X509) *chain, int override) 925 1.1.1.8 christos { 926 1.1.1.8 christos int ret = 0; 927 1.1.1.8 christos size_t i; 928 1.1.1.8 christos int j; 929 1.1.1.8 christos int rv; 930 1.1.1.8 christos CERT *c = ssl != NULL ? ssl->cert : ctx->cert; 931 1.1.1.8 christos STACK_OF(X509) *dup_chain = NULL; 932 1.1.1.8 christos EVP_PKEY *pubkey = NULL; 933 1.1.1.8 christos 934 1.1.1.8 christos /* Do all security checks before anything else */ 935 1.1.1.8 christos rv = ssl_security_cert(ssl, ctx, x509, 0, 1); 936 1.1.1.8 christos if (rv != 1) { 937 1.1.1.12 christos ERR_raise(ERR_LIB_SSL, rv); 938 1.1.1.8 christos goto out; 939 1.1.1.8 christos } 940 1.1.1.8 christos for (j = 0; j < sk_X509_num(chain); j++) { 941 1.1.1.8 christos rv = ssl_security_cert(ssl, ctx, sk_X509_value(chain, j), 0, 0); 942 1.1.1.8 christos if (rv != 1) { 943 1.1.1.12 christos ERR_raise(ERR_LIB_SSL, rv); 944 1.1.1.8 christos goto out; 945 1.1.1.8 christos } 946 1.1.1.8 christos } 947 1.1.1.8 christos 948 1.1.1.8 christos pubkey = X509_get_pubkey(x509); /* bumps reference */ 949 1.1.1.8 christos if (pubkey == NULL) 950 1.1.1.8 christos goto out; 951 1.1.1.8 christos if (privatekey == NULL) { 952 1.1.1.8 christos privatekey = pubkey; 953 1.1.1.8 christos } else { 954 1.1.1.8 christos /* For RSA, which has no parameters, missing returns 0 */ 955 1.1.1.8 christos if (EVP_PKEY_missing_parameters(privatekey)) { 956 1.1.1.8 christos if (EVP_PKEY_missing_parameters(pubkey)) { 957 1.1.1.8 christos /* nobody has parameters? - error */ 958 1.1.1.12 christos ERR_raise(ERR_LIB_SSL, SSL_R_MISSING_PARAMETERS); 959 1.1.1.8 christos goto out; 960 1.1.1.8 christos } else { 961 1.1.1.8 christos /* copy to privatekey from pubkey */ 962 1.1.1.12 christos if (!EVP_PKEY_copy_parameters(privatekey, pubkey)) { 963 1.1.1.12 christos ERR_raise(ERR_LIB_SSL, SSL_R_COPY_PARAMETERS_FAILED); 964 1.1.1.12 christos goto out; 965 1.1.1.12 christos } 966 1.1.1.8 christos } 967 1.1.1.8 christos } else if (EVP_PKEY_missing_parameters(pubkey)) { 968 1.1.1.8 christos /* copy to pubkey from privatekey */ 969 1.1.1.12 christos if (!EVP_PKEY_copy_parameters(pubkey, privatekey)) { 970 1.1.1.12 christos ERR_raise(ERR_LIB_SSL, SSL_R_COPY_PARAMETERS_FAILED); 971 1.1.1.12 christos goto out; 972 1.1.1.12 christos } 973 1.1.1.8 christos } /* else both have parameters */ 974 1.1.1.8 christos 975 1.1.1.8 christos /* check that key <-> cert match */ 976 1.1.1.12 christos if (EVP_PKEY_eq(pubkey, privatekey) != 1) { 977 1.1.1.12 christos ERR_raise(ERR_LIB_SSL, SSL_R_PRIVATE_KEY_MISMATCH); 978 1.1.1.8 christos goto out; 979 1.1.1.8 christos } 980 1.1.1.8 christos } 981 1.1.1.8 christos if (ssl_cert_lookup_by_pkey(pubkey, &i) == NULL) { 982 1.1.1.12 christos ERR_raise(ERR_LIB_SSL, SSL_R_UNKNOWN_CERTIFICATE_TYPE); 983 1.1.1.8 christos goto out; 984 1.1.1.8 christos } 985 1.1.1.8 christos 986 1.1.1.8 christos if (!override && (c->pkeys[i].x509 != NULL 987 1.1.1.8 christos || c->pkeys[i].privatekey != NULL 988 1.1.1.8 christos || c->pkeys[i].chain != NULL)) { 989 1.1.1.8 christos /* No override, and something already there */ 990 1.1.1.12 christos ERR_raise(ERR_LIB_SSL, SSL_R_NOT_REPLACING_CERTIFICATE); 991 1.1.1.8 christos goto out; 992 1.1.1.8 christos } 993 1.1.1.8 christos 994 1.1.1.8 christos if (chain != NULL) { 995 1.1.1.8 christos dup_chain = X509_chain_up_ref(chain); 996 1.1.1.8 christos if (dup_chain == NULL) { 997 1.1.1.12 christos ERR_raise(ERR_LIB_SSL, ERR_R_MALLOC_FAILURE); 998 1.1.1.8 christos goto out; 999 1.1.1.8 christos } 1000 1.1.1.8 christos } 1001 1.1.1.8 christos 1002 1.1.1.8 christos sk_X509_pop_free(c->pkeys[i].chain, X509_free); 1003 1.1.1.8 christos c->pkeys[i].chain = dup_chain; 1004 1.1.1.8 christos 1005 1.1.1.8 christos X509_free(c->pkeys[i].x509); 1006 1.1.1.8 christos X509_up_ref(x509); 1007 1.1.1.8 christos c->pkeys[i].x509 = x509; 1008 1.1.1.8 christos 1009 1.1.1.8 christos EVP_PKEY_free(c->pkeys[i].privatekey); 1010 1.1.1.8 christos EVP_PKEY_up_ref(privatekey); 1011 1.1.1.8 christos c->pkeys[i].privatekey = privatekey; 1012 1.1.1.8 christos 1013 1.1.1.8 christos c->key = &(c->pkeys[i]); 1014 1.1.1.8 christos 1015 1.1.1.8 christos ret = 1; 1016 1.1.1.8 christos out: 1017 1.1.1.8 christos EVP_PKEY_free(pubkey); 1018 1.1.1.8 christos return ret; 1019 1.1.1.8 christos } 1020 1.1.1.8 christos 1021 1.1.1.8 christos int SSL_use_cert_and_key(SSL *ssl, X509 *x509, EVP_PKEY *privatekey, 1022 1.1.1.8 christos STACK_OF(X509) *chain, int override) 1023 1.1.1.8 christos { 1024 1.1.1.8 christos return ssl_set_cert_and_key(ssl, NULL, x509, privatekey, chain, override); 1025 1.1.1.8 christos } 1026 1.1.1.8 christos 1027 1.1.1.8 christos int SSL_CTX_use_cert_and_key(SSL_CTX *ctx, X509 *x509, EVP_PKEY *privatekey, 1028 1.1.1.8 christos STACK_OF(X509) *chain, int override) 1029 1.1.1.8 christos { 1030 1.1.1.8 christos return ssl_set_cert_and_key(NULL, ctx, x509, privatekey, chain, override); 1031 1.1.1.8 christos } 1032