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