2 * Copyright 2019 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
10 #include <openssl/opensslconf.h> /* SIXTY_FOUR_BIT_LONG, ... */
11 #include <openssl/err.h>
12 #include <openssl/pem.h> /* PEM_BUFSIZE */
13 #include <openssl/pkcs12.h> /* PKCS8_encrypt() */
14 #include <openssl/types.h>
15 #include <openssl/x509.h> /* i2d_X509_PUBKEY_bio() */
16 #include "crypto/bn.h" /* bn_get_words() */
17 #include "prov/bio.h" /* ossl_prov_bio_printf() */
18 #include "prov/implementations.h"
19 #include "prov/providercommonerr.h" /* PROV_R_READ_KEY */
20 #include "serializer_local.h"
22 static PKCS8_PRIV_KEY_INFO *
23 ossl_prov_p8info_from_obj(const void *obj, int obj_nid,
26 int (*k2d)(const void *obj,
27 unsigned char **pder))
29 /* der, derlen store the key DER output and its length */
30 unsigned char *der = NULL;
32 /* The final PKCS#8 info */
33 PKCS8_PRIV_KEY_INFO *p8info = NULL;
36 if ((p8info = PKCS8_PRIV_KEY_INFO_new()) == NULL
37 || (derlen = k2d(obj, &der)) <= 0
38 || !PKCS8_pkey_set0(p8info, OBJ_nid2obj(obj_nid), 0,
39 params_type, params, der, derlen)) {
40 ERR_raise(ERR_LIB_PROV, ERR_R_MALLOC_FAILURE);
41 PKCS8_PRIV_KEY_INFO_free(p8info);
49 static X509_SIG *ossl_prov_encp8_from_p8info(PKCS8_PRIV_KEY_INFO *p8info,
50 struct pkcs8_encrypt_ctx_st *ctx)
53 char buf[PEM_BUFSIZE];
54 const void *kstr = ctx->cipher_pass;
55 size_t klen = ctx->cipher_pass_length;
57 if (ctx->cipher == NULL)
61 if (!ctx->cb(buf, sizeof(buf), &klen, NULL, ctx->cbarg)) {
62 ERR_raise(ERR_LIB_PROV, PROV_R_READ_KEY);
67 /* NID == -1 means "standard" */
68 p8 = PKCS8_encrypt(-1, ctx->cipher, kstr, klen, NULL, 0, 0, p8info);
70 OPENSSL_cleanse(buf, klen);
74 static X509_SIG *ossl_prov_encp8_from_obj(const void *obj, int obj_nid,
77 int (*k2d)(const void *obj,
78 unsigned char **pder),
79 struct pkcs8_encrypt_ctx_st *ctx)
81 PKCS8_PRIV_KEY_INFO *p8info =
82 ossl_prov_p8info_from_obj(obj, obj_nid, params, params_type, k2d);
83 X509_SIG *p8 = ossl_prov_encp8_from_p8info(p8info, ctx);
85 PKCS8_PRIV_KEY_INFO_free(p8info);
89 static X509_PUBKEY *ossl_prov_pubkey_from_obj(const void *obj, int obj_nid,
92 int (*k2d)(const void *obj,
93 unsigned char **pder))
95 /* der, derlen store the key DER output and its length */
96 unsigned char *der = NULL;
98 /* The final X509_PUBKEY */
99 X509_PUBKEY *xpk = NULL;
102 if ((xpk = X509_PUBKEY_new()) == NULL
103 || (derlen = k2d(obj, &der)) <= 0
104 || !X509_PUBKEY_set0_param(xpk, OBJ_nid2obj(obj_nid),
105 params_type, params, der, derlen)) {
106 ERR_raise(ERR_LIB_PROV, ERR_R_MALLOC_FAILURE);
107 X509_PUBKEY_free(xpk);
115 OSSL_OP_keymgmt_importkey_fn *ossl_prov_get_importkey(const OSSL_DISPATCH *fns)
117 /* Pilfer the keymgmt dispatch table */
118 for (; fns->function_id != 0; fns++)
119 if (fns->function_id == OSSL_FUNC_KEYMGMT_IMPORTKEY)
120 return OSSL_get_OP_keymgmt_importkey(fns);
125 # ifdef SIXTY_FOUR_BIT_LONG
126 # define BN_FMTu "%lu"
127 # define BN_FMTx "%lx"
130 # ifdef SIXTY_FOUR_BIT
131 # define BN_FMTu "%llu"
132 # define BN_FMTx "%llx"
135 # ifdef THIRTY_TWO_BIT
136 # define BN_FMTu "%u"
137 # define BN_FMTx "%x"
140 int ossl_prov_print_labeled_bignum(BIO *out, const char *label,
144 const char *post_label_spc = " ";
156 bytes = BN_num_bytes(bn);
157 words = bn_get_words(bn);
158 neg = BN_is_negative(bn) ? "-" : "";
161 return ossl_prov_bio_printf(out, "%s%s0\n", label, post_label_spc);
163 if (BN_num_bytes(bn) <= BN_BYTES)
164 return ossl_prov_bio_printf(out,
165 "%s%s%s" BN_FMTu " (%s0x" BN_FMTx ")\n",
166 label, post_label_spc, neg, words[0],
172 if (ossl_prov_bio_printf(out, "%s%s\n", label, neg) <= 0)
175 /* Keep track of how many bytes we have printed out so far */
179 * OpenSSL BIGNUMs are little endian limbs, so we print them last to
181 * i is used as limb index, j is used as the "byte index" in the limb
183 for (i = bytes / BN_BYTES - 1; i >= 0; i--) {
184 BN_ULONG l = words[i];
187 for (j = BN_BYTES - 1; j >= 0; j--) {
189 int b = ((l & (0xffLU << o)) >> o) & 0xff;
191 /* Indent every new line with 4 spaces */
194 if (ossl_prov_bio_printf(out, "\n") <= 0)
196 if (ossl_prov_bio_printf(out, " ") <= 0)
201 * Upper bit set, then we print an extra zero and pretend the
202 * BIGNUM was one byte longer
204 if (n == 0 && b > 127) {
205 if (ossl_prov_bio_printf(out, "%02x:", 0) <= 0)
212 if (ossl_prov_bio_printf(out, "%02x:", b) <= 0)
215 if (ossl_prov_bio_printf(out, "%02x", b) <= 0)
220 if (ossl_prov_bio_printf(out, "\n") <= 0)
226 /* p2s = param to asn1_string, k2d = key to der */
227 int ossl_prov_write_priv_der_from_obj(BIO *out, const void *obj, int obj_nid,
228 int (*p2s)(const void *obj, int nid,
231 int (*k2d)(const void *obj,
232 unsigned char **pder),
233 struct pkcs8_encrypt_ctx_st *ctx)
236 ASN1_STRING *str = NULL;
239 if (p2s != NULL && !p2s(obj, obj_nid, &str, &strtype))
242 if (ctx->cipher_intent) {
244 ossl_prov_encp8_from_obj(obj, obj_nid, str, strtype, k2d, ctx);
247 ret = i2d_PKCS8_bio(out, p8);
251 PKCS8_PRIV_KEY_INFO *p8info =
252 ossl_prov_p8info_from_obj(obj, obj_nid, str, strtype, k2d);
255 ret = i2d_PKCS8_PRIV_KEY_INFO_bio(out, p8info);
257 PKCS8_PRIV_KEY_INFO_free(p8info);
263 int ossl_prov_write_priv_pem_from_obj(BIO *out, const void *obj, int obj_nid,
264 int (*p2s)(const void *obj, int nid,
267 int (*k2d)(const void *obj,
268 unsigned char **pder),
269 struct pkcs8_encrypt_ctx_st *ctx)
272 ASN1_STRING *str = NULL;
275 if (p2s != NULL && !p2s(obj, obj_nid, &str, &strtype))
278 if (ctx->cipher_intent) {
279 X509_SIG *p8 = ossl_prov_encp8_from_obj(obj, obj_nid, str, strtype,
283 ret = PEM_write_bio_PKCS8(out, p8);
287 PKCS8_PRIV_KEY_INFO *p8info =
288 ossl_prov_p8info_from_obj(obj, obj_nid, str, strtype, k2d);
291 ret = PEM_write_bio_PKCS8_PRIV_KEY_INFO(out, p8info);
293 PKCS8_PRIV_KEY_INFO_free(p8info);
299 int ossl_prov_write_pub_der_from_obj(BIO *out, const void *obj, int obj_nid,
300 int (*p2s)(const void *obj, int nid,
303 int (*k2d)(const void *obj,
304 unsigned char **pder))
307 ASN1_STRING *str = NULL;
309 X509_PUBKEY *xpk = NULL;
311 if (p2s != NULL && !p2s(obj, obj_nid, &str, &strtype))
314 xpk = ossl_prov_pubkey_from_obj(obj, obj_nid, str, strtype, k2d);
317 ret = i2d_X509_PUBKEY_bio(out, xpk);
319 /* Also frees |str| */
320 X509_PUBKEY_free(xpk);
324 int ossl_prov_write_pub_pem_from_obj(BIO *out, const void *obj, int obj_nid,
325 int (*p2s)(const void *obj, int nid,
328 int (*k2d)(const void *obj,
329 unsigned char **pder))
332 ASN1_STRING *str = NULL;
334 X509_PUBKEY *xpk = NULL;
336 if (p2s != NULL && !p2s(obj, obj_nid, &str, &strtype))
339 xpk = ossl_prov_pubkey_from_obj(obj, obj_nid, str, strtype, k2d);
342 ret = PEM_write_bio_X509_PUBKEY(out, xpk);
344 /* Also frees |str| */
345 X509_PUBKEY_free(xpk);