]> git.ipfire.org Git - thirdparty/openssl.git/blame - providers/implementations/encode_decode/decode_ms2key.c
Update copyright year
[thirdparty/openssl.git] / providers / implementations / encode_decode / decode_ms2key.c
CommitLineData
37d398c1 1/*
8020d79b 2 * Copyright 2020-2021 The OpenSSL Project Authors. All Rights Reserved.
37d398c1
RL
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
8ae40cf5
RL
16#include <string.h>
17
37d398c1
RL
18#include <openssl/core_dispatch.h>
19#include <openssl/core_names.h>
14c8a3d1 20#include <openssl/core_object.h>
37d398c1
RL
21#include <openssl/crypto.h>
22#include <openssl/params.h>
8ae40cf5 23#include <openssl/pem.h> /* For public PVK functions */
37d398c1 24#include <openssl/x509.h>
8ae40cf5 25#include "internal/passphrase.h"
0934cf48 26#include "crypto/pem.h" /* For internal PVK and "blob" headers */
37d398c1
RL
27#include "prov/bio.h"
28#include "prov/implementations.h"
8ae40cf5
RL
29#include "endecoder_local.h"
30
8ae40cf5
RL
31static EVP_PKEY *read_msblob(PROV_CTX *provctx, OSSL_CORE_BIO *cin, int *ispub)
32{
33 BIO *in = bio_new_from_core_bio(provctx, cin);
34 EVP_PKEY *pkey = ossl_b2i_bio(in, ispub);
35
36 BIO_free(in);
37 return pkey;
38}
39
8ae40cf5
RL
40static EVP_PKEY *read_pvk(PROV_CTX *provctx, OSSL_CORE_BIO *cin,
41 OSSL_PASSPHRASE_CALLBACK *pw_cb, void *pw_cbarg)
42{
43 BIO *in = NULL;
44 EVP_PKEY *pkey = NULL;
45 struct ossl_passphrase_data_st pwdata;
46
47 memset(&pwdata, 0, sizeof(pwdata));
48 if (!ossl_pw_set_ossl_passphrase_cb(&pwdata, pw_cb, pw_cbarg))
49 return NULL;
50
51 in = bio_new_from_core_bio(provctx, cin);
52 pkey = b2i_PVK_bio(in, ossl_pw_pem_password, &pwdata);
53 BIO_free(in);
54
55 return pkey;
56}
37d398c1 57
ece9304c
RL
58static OSSL_FUNC_decoder_freectx_fn ms2key_freectx;
59static OSSL_FUNC_decoder_gettable_params_fn ms2key_gettable_params;
60static OSSL_FUNC_decoder_get_params_fn msblob2key_get_params;
ece9304c 61static OSSL_FUNC_decoder_get_params_fn pvk2key_get_params;
ece9304c 62static OSSL_FUNC_decoder_decode_fn msblob2key_decode;
ece9304c 63static OSSL_FUNC_decoder_decode_fn pvk2key_decode;
ece9304c 64static OSSL_FUNC_decoder_export_object_fn ms2key_export_object;
37d398c1
RL
65
66typedef void *(extract_key_fn)(EVP_PKEY *);
67typedef void (free_key_fn)(void *);
68struct keytype_desc_st {
69 int type; /* EVP key type */
70 const char *name; /* Keytype */
71 const OSSL_DISPATCH *fns; /* Keymgmt (to pilfer functions from) */
72
73 /*
74 * These must be the correct EVP_PKEY_get1_{TYPE}() and {TYPE}_free()
75 * function for the key.
76 */
77 extract_key_fn *extract_key;
78 free_key_fn *free_key;
79};
80
81/*
ece9304c 82 * Context used for DER to key decoding.
37d398c1
RL
83 */
84struct ms2key_ctx_st {
85 PROV_CTX *provctx;
86 const struct keytype_desc_st *desc;
87};
88
89static struct ms2key_ctx_st *
90ms2key_newctx(void *provctx, const struct keytype_desc_st *desc)
91{
92 struct ms2key_ctx_st *ctx = OPENSSL_zalloc(sizeof(*ctx));
93
94 if (ctx != NULL) {
95 ctx->provctx = provctx;
96 ctx->desc = desc;
97 }
98 return ctx;
99}
100
101static void ms2key_freectx(void *vctx)
102{
103 struct ms2key_ctx_st *ctx = vctx;
104
105 OPENSSL_free(ctx);
106}
107
4df0d37f 108static const OSSL_PARAM *ms2key_gettable_params(ossl_unused void *provctx)
37d398c1
RL
109{
110 static const OSSL_PARAM gettables[] = {
ece9304c 111 { OSSL_DECODER_PARAM_INPUT_TYPE, OSSL_PARAM_UTF8_PTR, NULL, 0, 0 },
37d398c1
RL
112 OSSL_PARAM_END,
113 };
114
115 return gettables;
116}
117
118static int msblob2key_get_params(OSSL_PARAM params[])
119{
120 OSSL_PARAM *p;
121
ece9304c 122 p = OSSL_PARAM_locate(params, OSSL_DECODER_PARAM_INPUT_TYPE);
37d398c1
RL
123 if (p != NULL && !OSSL_PARAM_set_utf8_ptr(p, "MSBLOB"))
124 return 0;
125
126 return 1;
127}
128
37d398c1
RL
129static int pvk2key_get_params(OSSL_PARAM params[])
130{
131 OSSL_PARAM *p;
132
ece9304c 133 p = OSSL_PARAM_locate(params, OSSL_DECODER_PARAM_INPUT_TYPE);
37d398c1
RL
134 if (p != NULL && !OSSL_PARAM_set_utf8_ptr(p, "PVK"))
135 return 0;
136
137 return 1;
138}
37d398c1
RL
139
140static int ms2key_post(struct ms2key_ctx_st *ctx, EVP_PKEY *pkey,
141 OSSL_CALLBACK *data_cb, void *data_cbarg)
142{
143 void *key = NULL;
144 int ok = 0;
145
146 if (pkey != NULL) {
147 /*
148 * Tear out the low-level key pointer from the pkey,
149 * but only if it matches the expected key type.
150 *
81f9af34 151 * The check should be done with EVP_PKEY_is_a(), but
37d398c1
RL
152 * as long as we still have #legacy internal keys, it's safer to
153 * use the type numbers in side the provider.
154 */
155 if (EVP_PKEY_id(pkey) == ctx->desc->type)
156 key = ctx->desc->extract_key(pkey);
157 }
158
159 if (key != NULL) {
14c8a3d1
RL
160 OSSL_PARAM params[4];
161 int object_type = OSSL_OBJECT_PKEY;
37d398c1
RL
162
163 params[0] =
14c8a3d1
RL
164 OSSL_PARAM_construct_int(OSSL_OBJECT_PARAM_TYPE, &object_type);
165 params[1] =
166 OSSL_PARAM_construct_utf8_string(OSSL_OBJECT_PARAM_DATA_TYPE,
37d398c1
RL
167 (char *)ctx->desc->name, 0);
168 /* The address of the key becomes the octet string */
14c8a3d1
RL
169 params[2] =
170 OSSL_PARAM_construct_octet_string(OSSL_OBJECT_PARAM_REFERENCE,
37d398c1 171 &key, sizeof(key));
14c8a3d1 172 params[3] = OSSL_PARAM_construct_end();
37d398c1
RL
173
174 ok = data_cb(params, data_cbarg);
175 }
176 ctx->desc->free_key(key);
177
178 return ok;
179}
180
2c090c1d 181static int msblob2key_decode(void *vctx, OSSL_CORE_BIO *cin, int selection,
ece9304c
RL
182 OSSL_CALLBACK *data_cb, void *data_cbarg,
183 OSSL_PASSPHRASE_CALLBACK *pw_cb, void *pw_cbarg)
37d398c1
RL
184{
185 struct ms2key_ctx_st *ctx = vctx;
186 int ispub = -1;
8ae40cf5 187 EVP_PKEY *pkey = read_msblob(ctx->provctx, cin, &ispub);
2c090c1d
RL
188 int ok = 0;
189
190 if (selection == 0
191 || (ispub
192 ? (selection & OSSL_KEYMGMT_SELECT_PUBLIC_KEY) != 0
193 : (selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0))
194 ok = ms2key_post(ctx, pkey, data_cb, data_cbarg);
37d398c1
RL
195
196 EVP_PKEY_free(pkey);
197 return ok;
198}
199
2c090c1d 200static int pvk2key_decode(void *vctx, OSSL_CORE_BIO *cin, int selection,
ece9304c
RL
201 OSSL_CALLBACK *data_cb, void *data_cbarg,
202 OSSL_PASSPHRASE_CALLBACK *pw_cb, void *pw_cbarg)
37d398c1
RL
203{
204 struct ms2key_ctx_st *ctx = vctx;
8ae40cf5 205 EVP_PKEY *pkey = read_pvk(ctx->provctx, cin, pw_cb, pw_cbarg);
2c090c1d
RL
206 int ok = 0;
207
208 if (selection == 0
209 || (selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0)
210 ok = ms2key_post(ctx, pkey, data_cb, data_cbarg);
37d398c1
RL
211
212 EVP_PKEY_free(pkey);
213 return ok;
214}
37d398c1
RL
215
216static int ms2key_export_object(void *vctx,
ece9304c
RL
217 const void *reference, size_t reference_sz,
218 OSSL_CALLBACK *export_cb, void *export_cbarg)
37d398c1
RL
219{
220 struct ms2key_ctx_st *ctx = vctx;
221 OSSL_FUNC_keymgmt_export_fn *export =
222 ossl_prov_get_keymgmt_export(ctx->desc->fns);
223 void *keydata;
224
225 if (reference_sz == sizeof(keydata) && export != NULL) {
226 /* The contents of the reference is the address to our object */
227 keydata = *(void **)reference;
228
229 return export(keydata, OSSL_KEYMGMT_SELECT_ALL,
230 export_cb, export_cbarg);
231 }
232 return 0;
233}
234
235#define IMPLEMENT_TYPE(KEYTYPEstr, KEYTYPE, keytype, extract, free) \
236 static const struct keytype_desc_st keytype##_desc; \
ece9304c 237 static OSSL_FUNC_decoder_newctx_fn ms2##keytype##_newctx; \
37d398c1
RL
238 static void *ms2##keytype##_newctx(void *provctx) \
239 { \
240 return ms2key_newctx(provctx, &keytype##_desc); \
241 } \
242 static const struct keytype_desc_st keytype##_desc = \
1be63951
P
243 { EVP_PKEY_##KEYTYPE, KEYTYPEstr, \
244 ossl_##keytype##_keymgmt_functions, \
37d398c1
RL
245 (extract_key_fn *)extract, \
246 (free_key_fn *)free }
247
248#define IMPLEMENT_MS(mstype, keytype) \
249 const OSSL_DISPATCH \
1be63951 250 ossl_##mstype##_to_##keytype##_decoder_functions[] = { \
ece9304c 251 { OSSL_FUNC_DECODER_NEWCTX, \
37d398c1 252 (void (*)(void))ms2##keytype##_newctx }, \
ece9304c 253 { OSSL_FUNC_DECODER_FREECTX, \
37d398c1 254 (void (*)(void))ms2key_freectx }, \
ece9304c 255 { OSSL_FUNC_DECODER_GETTABLE_PARAMS, \
37d398c1 256 (void (*)(void))ms2key_gettable_params }, \
ece9304c 257 { OSSL_FUNC_DECODER_GET_PARAMS, \
37d398c1 258 (void (*)(void))mstype##2key_get_params }, \
ece9304c
RL
259 { OSSL_FUNC_DECODER_DECODE, \
260 (void (*)(void))mstype##2key_decode }, \
261 { OSSL_FUNC_DECODER_EXPORT_OBJECT, \
37d398c1
RL
262 (void (*)(void))ms2key_export_object }, \
263 { 0, NULL } \
264 }
265
266#ifndef OPENSSL_NO_DSA
267IMPLEMENT_TYPE("DSA", DSA, dsa, EVP_PKEY_get1_DSA, DSA_free);
268IMPLEMENT_MS(msblob, dsa);
37d398c1 269IMPLEMENT_MS(pvk, dsa);
37d398c1
RL
270#endif
271IMPLEMENT_TYPE("RSA", RSA, rsa, EVP_PKEY_get1_RSA, RSA_free);
272IMPLEMENT_MS(msblob, rsa);
37d398c1 273IMPLEMENT_MS(pvk, rsa);