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