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