/src/openssl32/providers/implementations/encode_decode/decode_pem2der.c
Line  | Count  | Source (jump to first uncovered line)  | 
1  |  | /*  | 
2  |  |  * Copyright 2020-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  |  | /*  | 
11  |  |  * RSA low level APIs are deprecated for public use, but still ok for  | 
12  |  |  * internal use.  | 
13  |  |  */  | 
14  |  | #include "internal/deprecated.h"  | 
15  |  |  | 
16  |  | #include <string.h>  | 
17  |  |  | 
18  |  | #include <openssl/core_dispatch.h>  | 
19  |  | #include <openssl/core_names.h>  | 
20  |  | #include <openssl/core_object.h>  | 
21  |  | #include <openssl/crypto.h>  | 
22  |  | #include <openssl/err.h>  | 
23  |  | #include <openssl/params.h>  | 
24  |  | #include <openssl/pem.h>  | 
25  |  | #include <openssl/proverr.h>  | 
26  |  | #include "internal/nelem.h"  | 
27  |  | #include "prov/bio.h"  | 
28  |  | #include "prov/implementations.h"  | 
29  |  | #include "endecoder_local.h"  | 
30  |  |  | 
31  |  | static int read_pem(PROV_CTX *provctx, OSSL_CORE_BIO *cin,  | 
32  |  |                     char **pem_name, char **pem_header,  | 
33  |  |                     unsigned char **data, long *len)  | 
34  | 71.8k  | { | 
35  | 71.8k  |     BIO *in = ossl_bio_new_from_core_bio(provctx, cin);  | 
36  | 71.8k  |     int ok;  | 
37  |  |  | 
38  | 71.8k  |     if (in == NULL)  | 
39  | 0  |         return 0;  | 
40  | 71.8k  |     ok = (PEM_read_bio(in, pem_name, pem_header, data, len) > 0);  | 
41  |  |  | 
42  | 71.8k  |     BIO_free(in);  | 
43  | 71.8k  |     return ok;  | 
44  | 71.8k  | }  | 
45  |  |  | 
46  |  | static OSSL_FUNC_decoder_newctx_fn pem2der_newctx;  | 
47  |  | static OSSL_FUNC_decoder_freectx_fn pem2der_freectx;  | 
48  |  | static OSSL_FUNC_decoder_decode_fn pem2der_decode;  | 
49  |  |  | 
50  |  | /*  | 
51  |  |  * Context used for PEM to DER decoding.  | 
52  |  |  */  | 
53  |  | struct pem2der_ctx_st { | 
54  |  |     PROV_CTX *provctx;  | 
55  |  | };  | 
56  |  |  | 
57  |  | static void *pem2der_newctx(void *provctx)  | 
58  | 2.89M  | { | 
59  | 2.89M  |     struct pem2der_ctx_st *ctx = OPENSSL_zalloc(sizeof(*ctx));  | 
60  |  |  | 
61  | 2.89M  |     if (ctx != NULL)  | 
62  | 2.89M  |         ctx->provctx = provctx;  | 
63  | 2.89M  |     return ctx;  | 
64  | 2.89M  | }  | 
65  |  |  | 
66  |  | static void pem2der_freectx(void *vctx)  | 
67  | 2.89M  | { | 
68  | 2.89M  |     struct pem2der_ctx_st *ctx = vctx;  | 
69  |  |  | 
70  | 2.89M  |     OPENSSL_free(ctx);  | 
71  | 2.89M  | }  | 
72  |  |  | 
73  |  | /* pem_password_cb compatible function */  | 
74  |  | struct pem2der_pass_data_st { | 
75  |  |     OSSL_PASSPHRASE_CALLBACK *cb;  | 
76  |  |     void *cbarg;  | 
77  |  | };  | 
78  |  |  | 
79  |  | static int pem2der_pass_helper(char *buf, int num, int w, void *data)  | 
80  | 2  | { | 
81  | 2  |     struct pem2der_pass_data_st *pass_data = data;  | 
82  | 2  |     size_t plen;  | 
83  |  |  | 
84  | 2  |     if (pass_data == NULL  | 
85  | 2  |         || pass_data->cb == NULL  | 
86  | 2  |         || !pass_data->cb(buf, num, &plen, NULL, pass_data->cbarg))  | 
87  | 2  |         return -1;  | 
88  | 0  |     return (int)plen;  | 
89  | 2  | }  | 
90  |  |  | 
91  |  | /*  | 
92  |  |  * The selection parameter in pem2der_decode() is not used by this function  | 
93  |  |  * because it's not relevant just to decode PEM to DER.  | 
94  |  |  */  | 
95  |  | static int pem2der_decode(void *vctx, OSSL_CORE_BIO *cin, int selection,  | 
96  |  |                           OSSL_CALLBACK *data_cb, void *data_cbarg,  | 
97  |  |                           OSSL_PASSPHRASE_CALLBACK *pw_cb, void *pw_cbarg)  | 
98  | 39.0k  | { | 
99  |  |     /*  | 
100  |  |      * PEM names we recognise.  Other PEM names should be recognised by  | 
101  |  |      * other decoder implementations.  | 
102  |  |      */  | 
103  | 39.0k  |     static struct pem_name_map_st { | 
104  | 39.0k  |         const char *pem_name;  | 
105  | 39.0k  |         int object_type;  | 
106  | 39.0k  |         const char *data_type;  | 
107  | 39.0k  |         const char *data_structure;  | 
108  | 39.0k  |     } pem_name_map[] = { | 
109  |  |         /* PKCS#8 and SubjectPublicKeyInfo */  | 
110  | 39.0k  |         { PEM_STRING_PKCS8, OSSL_OBJECT_PKEY, NULL, "EncryptedPrivateKeyInfo" }, | 
111  | 39.0k  |         { PEM_STRING_PKCS8INF, OSSL_OBJECT_PKEY, NULL, "PrivateKeyInfo" }, | 
112  | 39.0k  |         { PEM_STRING_PUBLIC, OSSL_OBJECT_PKEY, NULL, "SubjectPublicKeyInfo" }, | 
113  |  |  | 
114  |  |         /* Our set of type specific PEM types */  | 
115  | 39.0k  |         { PEM_STRING_DHPARAMS, OSSL_OBJECT_PKEY, "DH", "type-specific" }, | 
116  | 39.0k  |         { PEM_STRING_DHXPARAMS, OSSL_OBJECT_PKEY, "X9.42 DH", "type-specific" }, | 
117  | 39.0k  |         { PEM_STRING_DSA, OSSL_OBJECT_PKEY, "DSA", "type-specific" }, | 
118  | 39.0k  |         { PEM_STRING_DSA_PUBLIC, OSSL_OBJECT_PKEY, "DSA", "type-specific" }, | 
119  | 39.0k  |         { PEM_STRING_DSAPARAMS, OSSL_OBJECT_PKEY, "DSA", "type-specific" }, | 
120  | 39.0k  |         { PEM_STRING_ECPRIVATEKEY, OSSL_OBJECT_PKEY, "EC", "type-specific" }, | 
121  | 39.0k  |         { PEM_STRING_ECPARAMETERS, OSSL_OBJECT_PKEY, "EC", "type-specific" }, | 
122  | 39.0k  |         { PEM_STRING_SM2PARAMETERS, OSSL_OBJECT_PKEY, "SM2", "type-specific" }, | 
123  | 39.0k  |         { PEM_STRING_RSA, OSSL_OBJECT_PKEY, "RSA", "type-specific" }, | 
124  | 39.0k  |         { PEM_STRING_RSA_PUBLIC, OSSL_OBJECT_PKEY, "RSA", "type-specific" }, | 
125  |  |  | 
126  |  |         /*  | 
127  |  |          * A few others that there is at least have an object type for, even  | 
128  |  |          * though there is no provider interface to handle such objects, yet.  | 
129  |  |          * However, this is beneficial for the OSSL_STORE result handler.  | 
130  |  |          */  | 
131  | 39.0k  |         { PEM_STRING_X509, OSSL_OBJECT_CERT, NULL, "Certificate" }, | 
132  | 39.0k  |         { PEM_STRING_X509_TRUSTED, OSSL_OBJECT_CERT, NULL, "Certificate" }, | 
133  | 39.0k  |         { PEM_STRING_X509_OLD, OSSL_OBJECT_CERT, NULL, "Certificate" }, | 
134  | 39.0k  |         { PEM_STRING_X509_CRL, OSSL_OBJECT_CRL, NULL, "CertificateList" } | 
135  | 39.0k  |     };  | 
136  | 39.0k  |     struct pem2der_ctx_st *ctx = vctx;  | 
137  | 39.0k  |     char *pem_name = NULL, *pem_header = NULL;  | 
138  | 39.0k  |     size_t i;  | 
139  | 39.0k  |     unsigned char *der = NULL;  | 
140  | 39.0k  |     long der_len = 0;  | 
141  | 39.0k  |     int ok = 0;  | 
142  | 39.0k  |     int objtype = OSSL_OBJECT_UNKNOWN;  | 
143  |  |  | 
144  | 39.0k  |     ok = read_pem(ctx->provctx, cin, &pem_name, &pem_header,  | 
145  | 39.0k  |                   &der, &der_len) > 0;  | 
146  |  |     /* We return "empty handed".  This is not an error. */  | 
147  | 39.0k  |     if (!ok)  | 
148  | 7.90k  |         return 1;  | 
149  |  |  | 
150  |  |     /*  | 
151  |  |      * 10 is the number of characters in "Proc-Type:", which  | 
152  |  |      * PEM_get_EVP_CIPHER_INFO() requires to be present.  | 
153  |  |      * If the PEM header has less characters than that, it's  | 
154  |  |      * not worth spending cycles on it.  | 
155  |  |      */  | 
156  | 31.1k  |     if (strlen(pem_header) > 10) { | 
157  | 136  |         EVP_CIPHER_INFO cipher;  | 
158  | 136  |         struct pem2der_pass_data_st pass_data;  | 
159  |  |  | 
160  | 136  |         ok = 0;                  /* Assume that we fail */  | 
161  | 136  |         pass_data.cb = pw_cb;  | 
162  | 136  |         pass_data.cbarg = pw_cbarg;  | 
163  | 136  |         if (!PEM_get_EVP_CIPHER_INFO(pem_header, &cipher)  | 
164  | 136  |             || !PEM_do_header(&cipher, der, &der_len,  | 
165  | 0  |                               pem2der_pass_helper, &pass_data))  | 
166  | 136  |             goto end;  | 
167  | 136  |     }  | 
168  |  |  | 
169  |  |     /*  | 
170  |  |      * Indicated that we successfully decoded something, or not at all.  | 
171  |  |      * Ending up "empty handed" is not an error.  | 
172  |  |      */  | 
173  | 31.0k  |     ok = 1;  | 
174  |  |  | 
175  |  |     /* Have a look to see if we recognise anything */  | 
176  | 233k  |     for (i = 0; i < OSSL_NELEM(pem_name_map); i++)  | 
177  | 233k  |         if (strcmp(pem_name, pem_name_map[i].pem_name) == 0)  | 
178  | 30.9k  |             break;  | 
179  |  |  | 
180  | 31.0k  |     if (i < OSSL_NELEM(pem_name_map)) { | 
181  | 30.9k  |         OSSL_PARAM params[5], *p = params;  | 
182  |  |         /* We expect these to be read only so casting away the const is ok */  | 
183  | 30.9k  |         char *data_type = (char *)pem_name_map[i].data_type;  | 
184  | 30.9k  |         char *data_structure = (char *)pem_name_map[i].data_structure;  | 
185  |  |  | 
186  | 30.9k  |         objtype = pem_name_map[i].object_type;  | 
187  | 30.9k  |         if (data_type != NULL)  | 
188  | 30.9k  |             *p++ =  | 
189  | 30.9k  |                 OSSL_PARAM_construct_utf8_string(OSSL_OBJECT_PARAM_DATA_TYPE,  | 
190  | 30.9k  |                                                  data_type, 0);  | 
191  |  |  | 
192  |  |         /* We expect this to be read only so casting away the const is ok */  | 
193  | 30.9k  |         if (data_structure != NULL)  | 
194  | 30.9k  |             *p++ =  | 
195  | 30.9k  |                 OSSL_PARAM_construct_utf8_string(OSSL_OBJECT_PARAM_DATA_STRUCTURE,  | 
196  | 30.9k  |                                                  data_structure, 0);  | 
197  | 30.9k  |         *p++ =  | 
198  | 30.9k  |             OSSL_PARAM_construct_octet_string(OSSL_OBJECT_PARAM_DATA,  | 
199  | 30.9k  |                                               der, der_len);  | 
200  | 30.9k  |         *p++ =  | 
201  | 30.9k  |             OSSL_PARAM_construct_int(OSSL_OBJECT_PARAM_TYPE, &objtype);  | 
202  |  |  | 
203  | 30.9k  |         *p = OSSL_PARAM_construct_end();  | 
204  |  |  | 
205  | 30.9k  |         ok = data_cb(params, data_cbarg);  | 
206  | 30.9k  |     }  | 
207  |  |  | 
208  | 31.1k  |  end:  | 
209  | 31.1k  |     OPENSSL_free(pem_name);  | 
210  | 31.1k  |     OPENSSL_free(pem_header);  | 
211  | 31.1k  |     OPENSSL_free(der);  | 
212  | 31.1k  |     return ok;  | 
213  | 31.0k  | }  | 
214  |  |  | 
215  |  | const OSSL_DISPATCH ossl_pem_to_der_decoder_functions[] = { | 
216  |  |     { OSSL_FUNC_DECODER_NEWCTX, (void (*)(void))pem2der_newctx }, | 
217  |  |     { OSSL_FUNC_DECODER_FREECTX, (void (*)(void))pem2der_freectx }, | 
218  |  |     { OSSL_FUNC_DECODER_DECODE, (void (*)(void))pem2der_decode }, | 
219  |  |     OSSL_DISPATCH_END  | 
220  |  | };  |