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