Fix migration guide mappings for i2o/o2i_ECPublicKey
[openssl.git] / providers / implementations / encode_decode / decode_pem2der.c
1 /*
2  * Copyright 2020-2021 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 {
35     BIO *in = bio_new_from_core_bio(provctx, cin);
36     int ok = (PEM_read_bio(in, pem_name, pem_header, data, len) > 0);
37
38     BIO_free(in);
39     return ok;
40 }
41
42 static OSSL_FUNC_decoder_newctx_fn pem2der_newctx;
43 static OSSL_FUNC_decoder_freectx_fn pem2der_freectx;
44 static OSSL_FUNC_decoder_gettable_params_fn pem2der_gettable_params;
45 static OSSL_FUNC_decoder_get_params_fn pem2der_get_params;
46 static OSSL_FUNC_decoder_decode_fn pem2der_decode;
47
48 /*
49  * Context used for PEM to DER decoding.
50  */
51 struct pem2der_ctx_st {
52     PROV_CTX *provctx;
53 };
54
55 static void *pem2der_newctx(void *provctx)
56 {
57     struct pem2der_ctx_st *ctx = OPENSSL_zalloc(sizeof(*ctx));
58
59     if (ctx != NULL)
60         ctx->provctx = provctx;
61     return ctx;
62 }
63
64 static void pem2der_freectx(void *vctx)
65 {
66     struct pem2der_ctx_st *ctx = vctx;
67
68     OPENSSL_free(ctx);
69 }
70
71 static const OSSL_PARAM *pem2der_gettable_params(void *provctx)
72 {
73     static const OSSL_PARAM gettables[] = {
74         { OSSL_DECODER_PARAM_INPUT_TYPE, OSSL_PARAM_UTF8_PTR, NULL, 0, 0 },
75         OSSL_PARAM_END,
76     };
77
78     return gettables;
79 }
80
81 static int pem2der_get_params(OSSL_PARAM params[])
82 {
83     OSSL_PARAM *p;
84
85     p = OSSL_PARAM_locate(params, OSSL_DECODER_PARAM_INPUT_TYPE);
86     if (p != NULL && !OSSL_PARAM_set_utf8_ptr(p, "PEM"))
87         return 0;
88
89     return 1;
90 }
91
92 /* pem_password_cb compatible function */
93 struct pem2der_pass_data_st {
94     OSSL_PASSPHRASE_CALLBACK *cb;
95     void *cbarg;
96 };
97
98 static int pem2der_pass_helper(char *buf, int num, int w, void *data)
99 {
100     struct pem2der_pass_data_st *pass_data = data;
101     size_t plen;
102
103     if (pass_data == NULL
104         || pass_data->cb == NULL
105         || !pass_data->cb(buf, num, &plen, NULL, pass_data->cbarg))
106         return -1;
107     return (int)plen;
108 }
109
110 /*
111  * The selection parameter in pem2der_decode() is not used by this function
112  * because it's not relevant just to decode PEM to DER.
113  */
114 static int pem2der_decode(void *vctx, OSSL_CORE_BIO *cin, int selection,
115                           OSSL_CALLBACK *data_cb, void *data_cbarg,
116                           OSSL_PASSPHRASE_CALLBACK *pw_cb, void *pw_cbarg)
117 {
118     /* Strings to peel off the pem name */
119     static struct peelablee_pem_name_endings_st {
120         const char *ending;
121         const char *data_structure;
122     } peelable_pem_name_endings[] = {
123         /*
124          * These entries should be in longest to shortest order to avoid
125          * mixups.
126          */
127         { "ENCRYPTED PRIVATE KEY", "pkcs8" },
128         { "PRIVATE KEY", "pkcs8" },
129         { "PUBLIC KEY", "SubjectPublicKeyInfo" },
130         { "PARAMETERS", NULL }
131
132         /*
133          * Libcrypto currently only supports decoding keys with provider side
134          * decoders, so we don't try to peel any other PEM name.  That's an
135          * exercise for when libcrypto starts to treat other types of objects
136          * via providers.
137          */
138     };
139     struct pem2der_ctx_st *ctx = vctx;
140     char *pem_name = NULL, *pem_header = NULL;
141     size_t pem_name_len, i;
142     unsigned char *der = NULL;
143     long der_len = 0;
144     int ok = 0;
145     int objtype = OSSL_OBJECT_UNKNOWN;
146     const char *data_structure = NULL;
147
148     if (read_pem(ctx->provctx, cin, &pem_name, &pem_header,
149                  &der, &der_len) <= 0)
150         return 0;
151
152     /*
153      * 10 is the number of characters in "Proc-Type:", which
154      * PEM_get_EVP_CIPHER_INFO() requires to be present.
155      * If the PEM header has less characters than that, it's
156      * not worth spending cycles on it.
157      */
158     if (strlen(pem_header) > 10) {
159         EVP_CIPHER_INFO cipher;
160         struct pem2der_pass_data_st pass_data;
161
162         pass_data.cb = pw_cb;
163         pass_data.cbarg = pw_cbarg;
164         if (!PEM_get_EVP_CIPHER_INFO(pem_header, &cipher)
165             || !PEM_do_header(&cipher, der, &der_len,
166                               pem2der_pass_helper, &pass_data))
167             goto end;
168     }
169
170     /*
171      * Peal off certain strings from the end of |pem_name|, as they serve
172      * no further purpose.
173      */
174     for (i = 0, pem_name_len = strlen(pem_name);
175          i < OSSL_NELEM(peelable_pem_name_endings);
176          i++) {
177         size_t peel_len = strlen(peelable_pem_name_endings[i].ending);
178         size_t pem_name_offset;
179
180         if (peel_len <= pem_name_len) {
181             pem_name_offset = pem_name_len - peel_len;
182             if (strcmp(pem_name + pem_name_offset,
183                        peelable_pem_name_endings[i].ending) == 0) {
184
185                 do {
186                     pem_name[pem_name_offset] = '\0';
187                 } while (pem_name_offset > 0
188                          && pem_name[--pem_name_offset] == ' ');
189
190                 if (pem_name[0] == '\0') {
191                     OPENSSL_free(pem_name);
192                     pem_name = NULL;
193                 }
194                 /* All of these peelable endings are for EVP_PKEYs */
195                 objtype = OSSL_OBJECT_PKEY;
196                 if (pem_name == NULL) {
197                     data_structure = peelable_pem_name_endings[i].data_structure;
198                     if (data_structure == NULL)
199                         goto end;
200                 } else {
201                     /*
202                      * If there is an algorithm name prefix then it is a
203                      * type-specific data structure
204                      */
205                     data_structure = "type-specific";
206                 }
207                 break;
208             }
209         }
210     }
211
212     /* If we don't know the object type yet check if it's one we know about */
213     if (objtype == OSSL_OBJECT_UNKNOWN) {
214         if (strcmp(pem_name, PEM_STRING_X509) == 0
215                 || strcmp(pem_name, PEM_STRING_X509_TRUSTED) == 0
216                 || strcmp(pem_name, PEM_STRING_X509_OLD) == 0)
217             objtype = OSSL_OBJECT_CERT;
218         else if (strcmp(pem_name, PEM_STRING_X509_CRL) == 0)
219             objtype = OSSL_OBJECT_CRL;
220     }
221
222     {
223         OSSL_PARAM params[5], *p = params;
224
225         if (pem_name != NULL)
226             *p++ =
227                 OSSL_PARAM_construct_utf8_string(OSSL_OBJECT_PARAM_DATA_TYPE,
228                                                  pem_name, 0);
229
230         /* We expect this to be read only so casting away the const is ok */
231         if (data_structure != NULL)
232             *p++ =
233                 OSSL_PARAM_construct_utf8_string(OSSL_OBJECT_PARAM_DATA_STRUCTURE,
234                                                  (char *)data_structure, 0);
235         *p++ =
236             OSSL_PARAM_construct_octet_string(OSSL_OBJECT_PARAM_DATA,
237                                               der, der_len);
238         *p++ =
239             OSSL_PARAM_construct_int(OSSL_OBJECT_PARAM_TYPE, &objtype);
240
241         *p = OSSL_PARAM_construct_end();
242
243         ok = data_cb(params, data_cbarg);
244     }
245
246  end:
247     OPENSSL_free(pem_name);
248     OPENSSL_free(pem_header);
249     OPENSSL_free(der);
250     return ok;
251 }
252
253 const OSSL_DISPATCH ossl_pem_to_der_decoder_functions[] = {
254     { OSSL_FUNC_DECODER_NEWCTX, (void (*)(void))pem2der_newctx },
255     { OSSL_FUNC_DECODER_FREECTX, (void (*)(void))pem2der_freectx },
256     { OSSL_FUNC_DECODER_GETTABLE_PARAMS,
257       (void (*)(void))pem2der_gettable_params },
258     { OSSL_FUNC_DECODER_GET_PARAMS,
259       (void (*)(void))pem2der_get_params },
260     { OSSL_FUNC_DECODER_DECODE, (void (*)(void))pem2der_decode },
261     { 0, NULL }
262 };