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