2 * Copyright 2020 The OpenSSL Project Authors. All Rights Reserved.
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
11 * RSA low level APIs are deprecated for public use, but still ok for
14 #include "internal/deprecated.h"
18 #include <openssl/core_dispatch.h>
19 #include <openssl/core_names.h>
20 #include <openssl/crypto.h>
21 #include <openssl/err.h>
22 #include <openssl/params.h>
23 #include <openssl/pem.h>
25 #include "prov/implementations.h"
26 #include "prov/providercommonerr.h"
27 #include "serializer_local.h"
29 static OSSL_FUNC_deserializer_newctx_fn pem2der_newctx
;
30 static OSSL_FUNC_deserializer_freectx_fn pem2der_freectx
;
31 static OSSL_FUNC_deserializer_gettable_params_fn pem2der_gettable_params
;
32 static OSSL_FUNC_deserializer_get_params_fn pem2der_get_params
;
33 static OSSL_FUNC_deserializer_deserialize_fn pem2der_deserialize
;
36 * Context used for PEM to DER deserialization.
38 struct pem2der_ctx_st
{
42 static void *pem2der_newctx(void *provctx
)
44 struct pem2der_ctx_st
*ctx
= OPENSSL_zalloc(sizeof(*ctx
));
47 ctx
->provctx
= provctx
;
51 static void pem2der_freectx(void *vctx
)
53 struct pem2der_ctx_st
*ctx
= vctx
;
58 static const OSSL_PARAM
*pem2der_gettable_params(void *provctx
)
60 static const OSSL_PARAM gettables
[] = {
61 { OSSL_DESERIALIZER_PARAM_INPUT_TYPE
, OSSL_PARAM_UTF8_PTR
, NULL
, 0, 0 },
68 static int pem2der_get_params(OSSL_PARAM params
[])
72 p
= OSSL_PARAM_locate(params
, OSSL_DESERIALIZER_PARAM_INPUT_TYPE
);
73 if (p
!= NULL
&& !OSSL_PARAM_set_utf8_ptr(p
, "PEM"))
79 /* pem_password_cb compatible function */
80 struct pem2der_pass_data_st
{
81 OSSL_PASSPHRASE_CALLBACK
*cb
;
85 static int pem2der_pass_helper(char *buf
, int num
, int w
, void *data
)
87 struct pem2der_pass_data_st
*pass_data
= data
;
91 || pass_data
->cb
== NULL
92 || !pass_data
->cb(buf
, num
, &plen
, NULL
, pass_data
->cbarg
))
97 static int pem2der_deserialize(void *vctx
, OSSL_CORE_BIO
*cin
,
98 OSSL_CALLBACK
*data_cb
, void *data_cbarg
,
99 OSSL_PASSPHRASE_CALLBACK
*pw_cb
, void *pw_cbarg
)
101 struct pem2der_ctx_st
*ctx
= vctx
;
102 char *pem_name
= NULL
, *pem_header
= NULL
;
103 unsigned char *der
= NULL
;
107 if (ossl_prov_read_pem(ctx
->provctx
, cin
, &pem_name
, &pem_header
,
108 &der
, &der_len
) <= 0)
112 * 10 is the number of characters in "Proc-Type:", which
113 * PEM_get_EVP_CIPHER_INFO() requires to be present.
114 * If the PEM header has less characters than that, it's
115 * not worth spending cycles on it.
117 if (strlen(pem_header
) > 10) {
118 EVP_CIPHER_INFO cipher
;
119 struct pem2der_pass_data_st pass_data
;
121 pass_data
.cb
= pw_cb
;
122 pass_data
.cbarg
= pw_cbarg
;
123 if (!PEM_get_EVP_CIPHER_INFO(pem_header
, &cipher
)
124 || !PEM_do_header(&cipher
, der
, &der_len
,
125 pem2der_pass_helper
, &pass_data
))
130 OSSL_PARAM params
[3];
133 OSSL_PARAM_construct_utf8_string(OSSL_DESERIALIZER_PARAM_DATA_TYPE
,
136 OSSL_PARAM_construct_octet_string(OSSL_DESERIALIZER_PARAM_DATA
,
138 params
[2] = OSSL_PARAM_construct_end();
140 ok
= data_cb(params
, data_cbarg
);
144 OPENSSL_free(pem_name
);
145 OPENSSL_free(pem_header
);
150 const OSSL_DISPATCH pem_to_der_deserializer_functions
[] = {
151 { OSSL_FUNC_DESERIALIZER_NEWCTX
, (void (*)(void))pem2der_newctx
},
152 { OSSL_FUNC_DESERIALIZER_FREECTX
, (void (*)(void))pem2der_freectx
},
153 { OSSL_FUNC_DESERIALIZER_GETTABLE_PARAMS
,
154 (void (*)(void))pem2der_gettable_params
},
155 { OSSL_FUNC_DESERIALIZER_GET_PARAMS
,
156 (void (*)(void))pem2der_get_params
},
157 { OSSL_FUNC_DESERIALIZER_DESERIALIZE
, (void (*)(void))pem2der_deserialize
},