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