]> git.ipfire.org Git - thirdparty/openssl.git/blob - providers/implementations/serializers/deserialize_pem2der.c
ea43bd83190d0a03f13d7d739e7e22835ccafee6
[thirdparty/openssl.git] / providers / implementations / serializers / deserialize_pem2der.c
1 /*
2 * Copyright 2020 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/crypto.h>
21 #include <openssl/err.h>
22 #include <openssl/params.h>
23 #include <openssl/pem.h>
24 #include "prov/bio.h"
25 #include "prov/implementations.h"
26 #include "prov/providercommonerr.h"
27 #include "serializer_local.h"
28
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;
34
35 /*
36 * Context used for PEM to DER deserialization.
37 */
38 struct pem2der_ctx_st {
39 PROV_CTX *provctx;
40 };
41
42 static void *pem2der_newctx(void *provctx)
43 {
44 struct pem2der_ctx_st *ctx = OPENSSL_zalloc(sizeof(*ctx));
45
46 if (ctx != NULL)
47 ctx->provctx = provctx;
48 return ctx;
49 }
50
51 static void pem2der_freectx(void *vctx)
52 {
53 struct pem2der_ctx_st *ctx = vctx;
54
55 OPENSSL_free(ctx);
56 }
57
58 static const OSSL_PARAM *pem2der_gettable_params(void *provctx)
59 {
60 static const OSSL_PARAM gettables[] = {
61 { OSSL_DESERIALIZER_PARAM_INPUT_TYPE, OSSL_PARAM_UTF8_PTR, NULL, 0, 0 },
62 OSSL_PARAM_END,
63 };
64
65 return gettables;
66 }
67
68 static int pem2der_get_params(OSSL_PARAM params[])
69 {
70 OSSL_PARAM *p;
71
72 p = OSSL_PARAM_locate(params, OSSL_DESERIALIZER_PARAM_INPUT_TYPE);
73 if (p != NULL && !OSSL_PARAM_set_utf8_ptr(p, "PEM"))
74 return 0;
75
76 return 1;
77 }
78
79 /* pem_password_cb compatible function */
80 struct pem2der_pass_data_st {
81 OSSL_PASSPHRASE_CALLBACK *cb;
82 void *cbarg;
83 };
84
85 static int pem2der_pass_helper(char *buf, int num, int w, void *data)
86 {
87 struct pem2der_pass_data_st *pass_data = data;
88 size_t plen;
89
90 if (pass_data == NULL
91 || pass_data->cb == NULL
92 || !pass_data->cb(buf, num, &plen, NULL, pass_data->cbarg))
93 return -1;
94 return (int)plen;
95 }
96
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)
100 {
101 struct pem2der_ctx_st *ctx = vctx;
102 char *pem_name = NULL, *pem_header = NULL;
103 unsigned char *der = NULL;
104 long der_len = 0;
105 int ok = 0;
106
107 if (ossl_prov_read_pem(ctx->provctx, cin, &pem_name, &pem_header,
108 &der, &der_len) <= 0)
109 return 0;
110
111 /*
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.
116 */
117 if (strlen(pem_header) > 10) {
118 EVP_CIPHER_INFO cipher;
119 struct pem2der_pass_data_st pass_data;
120
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))
126 goto end;
127 }
128
129 {
130 OSSL_PARAM params[3];
131
132 params[0] =
133 OSSL_PARAM_construct_utf8_string(OSSL_DESERIALIZER_PARAM_DATA_TYPE,
134 pem_name, 0);
135 params[1] =
136 OSSL_PARAM_construct_octet_string(OSSL_DESERIALIZER_PARAM_DATA,
137 der, der_len);
138 params[2] = OSSL_PARAM_construct_end();
139
140 ok = data_cb(params, data_cbarg);
141 }
142
143 end:
144 OPENSSL_free(pem_name);
145 OPENSSL_free(pem_header);
146 OPENSSL_free(der);
147 return ok;
148 }
149
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 },
158 { 0, NULL }
159 };