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