]> git.ipfire.org Git - thirdparty/openssl.git/blame - providers/implementations/encode_decode/decode_der2key.c
Fix up issue on AIX caused by broken compiler handling of macro expansion
[thirdparty/openssl.git] / providers / implementations / encode_decode / decode_der2key.c
CommitLineData
7c664b1f
RL
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 * low level APIs are deprecated for public use, but still ok for
12 * internal use.
13 */
14#include "internal/deprecated.h"
15
16#include <openssl/core_dispatch.h>
17#include <openssl/core_names.h>
14c8a3d1 18#include <openssl/core_object.h>
7c664b1f
RL
19#include <openssl/crypto.h>
20#include <openssl/params.h>
21#include <openssl/x509.h>
22#include "prov/bio.h"
23#include "prov/implementations.h"
ece9304c 24#include "encoder_local.h"
7c664b1f 25
ece9304c 26static OSSL_FUNC_decoder_newctx_fn der2rsa_newctx;
7c664b1f 27
ece9304c
RL
28static OSSL_FUNC_decoder_freectx_fn der2key_freectx;
29static OSSL_FUNC_decoder_gettable_params_fn der2key_gettable_params;
30static OSSL_FUNC_decoder_get_params_fn der2key_get_params;
31static OSSL_FUNC_decoder_decode_fn der2key_decode;
32static OSSL_FUNC_decoder_export_object_fn der2key_export_object;
7c664b1f
RL
33
34typedef void *(extract_key_fn)(EVP_PKEY *);
35typedef void (free_key_fn)(void *);
36struct keytype_desc_st {
37 int type; /* EVP key type */
38 const char *name; /* Keytype */
39 const OSSL_DISPATCH *fns; /* Keymgmt (to pilfer functions from) */
40
41 /*
42 * These must be the correct EVP_PKEY_get1_{TYPE}() and {TYPE}_free()
43 * function for the key.
44 */
45 extract_key_fn *extract_key;
46 free_key_fn *free_key;
47};
48
49/*
ece9304c 50 * Context used for DER to key decoding.
7c664b1f
RL
51 */
52struct der2key_ctx_st {
53 PROV_CTX *provctx;
54 const struct keytype_desc_st *desc;
55};
56
57static struct der2key_ctx_st *
58der2key_newctx(void *provctx, const struct keytype_desc_st *desc)
59{
60 struct der2key_ctx_st *ctx = OPENSSL_zalloc(sizeof(*ctx));
61
62 if (ctx != NULL) {
63 ctx->provctx = provctx;
64 ctx->desc = desc;
65 }
66 return ctx;
67}
68
69static void der2key_freectx(void *vctx)
70{
71 struct der2key_ctx_st *ctx = vctx;
72
73 OPENSSL_free(ctx);
74}
75
af5e1e85 76static const OSSL_PARAM *der2key_gettable_params(void *provctx)
7c664b1f
RL
77{
78 static const OSSL_PARAM gettables[] = {
ece9304c 79 { OSSL_DECODER_PARAM_INPUT_TYPE, OSSL_PARAM_UTF8_PTR, NULL, 0, 0 },
7c664b1f
RL
80 OSSL_PARAM_END,
81 };
82
83 return gettables;
84}
85
86static int der2key_get_params(OSSL_PARAM params[])
87{
88 OSSL_PARAM *p;
89
ece9304c 90 p = OSSL_PARAM_locate(params, OSSL_DECODER_PARAM_INPUT_TYPE);
7c664b1f
RL
91 if (p != NULL && !OSSL_PARAM_set_utf8_ptr(p, "DER"))
92 return 0;
93
94 return 1;
95}
96
ece9304c
RL
97static int der2key_decode(void *vctx, OSSL_CORE_BIO *cin,
98 OSSL_CALLBACK *data_cb, void *data_cbarg,
99 OSSL_PASSPHRASE_CALLBACK *pw_cb, void *pw_cbarg)
7c664b1f
RL
100{
101 struct der2key_ctx_st *ctx = vctx;
102 void *libctx = PROV_LIBRARY_CONTEXT_OF(ctx->provctx);
103 unsigned char *der = NULL;
104 const unsigned char *derp;
105 long der_len = 0;
106 unsigned char *new_der = NULL;
107 long new_der_len;
108 EVP_PKEY *pkey = NULL;
109 void *key = NULL;
110 int ok = 0;
111
112 if (!ossl_prov_read_der(ctx->provctx, cin, &der, &der_len))
113 return 0;
114
115 /*
116 * Opportunistic attempt to decrypt. If it doesn't work, we try to
117 * decode our input unencrypted.
118 */
119 if (ossl_prov_der_from_p8(&new_der, &new_der_len, der, der_len,
120 pw_cb, pw_cbarg)) {
121 OPENSSL_free(der);
122 der = new_der;
123 der_len = new_der_len;
124 }
125
126 derp = der;
127 pkey = d2i_PrivateKey_ex(ctx->desc->type, NULL, &derp, der_len,
128 libctx, NULL);
129 if (pkey == NULL) {
130 derp = der;
22b81444 131 pkey = d2i_PUBKEY_ex(NULL, &derp, der_len, libctx, NULL);
7c664b1f
RL
132 }
133
b5b6669f
RL
134 if (pkey == NULL) {
135 derp = der;
136 pkey = d2i_KeyParams(ctx->desc->type, NULL, &derp, der_len);
137 }
138
7c664b1f
RL
139 if (pkey != NULL) {
140 /*
141 * Tear out the low-level key pointer from the pkey,
142 * but only if it matches the expected key type.
143 *
144 * TODO(3.0): The check should be done with EVP_PKEY_is_a(), but
145 * as long as we still have #legacy internal keys, it's safer to
22b81444 146 * use the type numbers inside the provider.
7c664b1f
RL
147 */
148 if (EVP_PKEY_id(pkey) == ctx->desc->type)
149 key = ctx->desc->extract_key(pkey);
150
151 /*
152 * ctx->desc->extract_key() is expected to have incremented |key|'s
153 * reference count, so it should be safe to free |pkey| now.
154 */
155 EVP_PKEY_free(pkey);
156 }
157
158 OPENSSL_free(der);
159
160 if (key != NULL) {
14c8a3d1
RL
161 OSSL_PARAM params[4];
162 int object_type = OSSL_OBJECT_PKEY;
7c664b1f
RL
163
164 params[0] =
14c8a3d1
RL
165 OSSL_PARAM_construct_int(OSSL_OBJECT_PARAM_TYPE, &object_type);
166 params[1] =
167 OSSL_PARAM_construct_utf8_string(OSSL_OBJECT_PARAM_DATA_TYPE,
7c664b1f
RL
168 (char *)ctx->desc->name, 0);
169 /* The address of the key becomes the octet string */
14c8a3d1
RL
170 params[2] =
171 OSSL_PARAM_construct_octet_string(OSSL_OBJECT_PARAM_REFERENCE,
7c664b1f 172 &key, sizeof(key));
14c8a3d1 173 params[3] = OSSL_PARAM_construct_end();
7c664b1f
RL
174
175 ok = data_cb(params, data_cbarg);
176 }
177 ctx->desc->free_key(key);
178
179 return ok;
180}
181
182static int der2key_export_object(void *vctx,
183 const void *reference, size_t reference_sz,
184 OSSL_CALLBACK *export_cb, void *export_cbarg)
185{
186 struct der2key_ctx_st *ctx = vctx;
187 OSSL_FUNC_keymgmt_export_fn *export =
188 ossl_prov_get_keymgmt_export(ctx->desc->fns);
189 void *keydata;
190
191 if (reference_sz == sizeof(keydata) && export != NULL) {
192 /* The contents of the reference is the address to our object */
193 keydata = *(void **)reference;
194
195 return export(keydata, OSSL_KEYMGMT_SELECT_ALL,
196 export_cb, export_cbarg);
197 }
198 return 0;
199}
200
201#define IMPLEMENT_NEWCTX(KEYTYPEstr, KEYTYPE, keytype, extract, free) \
202 static const struct keytype_desc_st keytype##_desc = \
203 { EVP_PKEY_##KEYTYPE, KEYTYPEstr, keytype##_keymgmt_functions, \
204 (extract_key_fn *)extract, \
205 (free_key_fn *)free }; \
206 static void *der2##keytype##_newctx(void *provctx) \
207 { \
208 return der2key_newctx(provctx, &keytype##_desc); \
209 } \
ece9304c
RL
210 const OSSL_DISPATCH der_to_##keytype##_decoder_functions[] = { \
211 { OSSL_FUNC_DECODER_NEWCTX, \
7c664b1f 212 (void (*)(void))der2##keytype##_newctx }, \
ece9304c 213 { OSSL_FUNC_DECODER_FREECTX, \
7c664b1f 214 (void (*)(void))der2key_freectx }, \
ece9304c 215 { OSSL_FUNC_DECODER_GETTABLE_PARAMS, \
7c664b1f 216 (void (*)(void))der2key_gettable_params }, \
ece9304c 217 { OSSL_FUNC_DECODER_GET_PARAMS, \
7c664b1f 218 (void (*)(void))der2key_get_params }, \
ece9304c
RL
219 { OSSL_FUNC_DECODER_DECODE, \
220 (void (*)(void))der2key_decode }, \
221 { OSSL_FUNC_DECODER_EXPORT_OBJECT, \
7c664b1f
RL
222 (void (*)(void))der2key_export_object }, \
223 { 0, NULL } \
224 }
225
226#ifndef OPENSSL_NO_DH
227IMPLEMENT_NEWCTX("DH", DH, dh, EVP_PKEY_get1_DH, DH_free);
31d2daec 228IMPLEMENT_NEWCTX("DHX", DHX, dhx, EVP_PKEY_get1_DH, DH_free);
7c664b1f
RL
229#endif
230#ifndef OPENSSL_NO_DSA
231IMPLEMENT_NEWCTX("DSA", DSA, dsa, EVP_PKEY_get1_DSA, DSA_free);
232#endif
233#ifndef OPENSSL_NO_EC
234IMPLEMENT_NEWCTX("EC", EC, ec, EVP_PKEY_get1_EC_KEY, EC_KEY_free);
235IMPLEMENT_NEWCTX("X25519", X25519, x25519,
236 EVP_PKEY_get1_X25519, ecx_key_free);
237IMPLEMENT_NEWCTX("X448", X448, x448,
238 EVP_PKEY_get1_X448, ecx_key_free);
239IMPLEMENT_NEWCTX("ED25519", ED25519, ed25519,
240 EVP_PKEY_get1_ED25519, ecx_key_free);
241IMPLEMENT_NEWCTX("ED448", ED448, ed448, EVP_PKEY_get1_ED448, ecx_key_free);
242#endif
243IMPLEMENT_NEWCTX("RSA", RSA, rsa, EVP_PKEY_get1_RSA, RSA_free);
244IMPLEMENT_NEWCTX("RSA-PSS", RSA_PSS, rsapss, EVP_PKEY_get1_RSA, RSA_free);