Add DH key exchange to fips provider
[openssl.git] / providers / implementations / serializers / serializer_common.c
1 /*
2  * Copyright 2019 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 #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"
21
22 static PKCS8_PRIV_KEY_INFO *
23 ossl_prov_p8info_from_obj(const void *obj, int obj_nid,
24                           ASN1_STRING *params,
25                           int params_type,
26                           int (*k2d)(const void *obj,
27                                      unsigned char **pder))
28 {
29     /* der, derlen store the key DER output and its length */
30     unsigned char *der = NULL;
31     int derlen;
32     /* The final PKCS#8 info */
33     PKCS8_PRIV_KEY_INFO *p8info = NULL;
34
35
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);
42         OPENSSL_free(der);
43         p8info = NULL;
44     }
45
46     return p8info;
47 }
48
49 static X509_SIG *ossl_prov_encp8_from_p8info(PKCS8_PRIV_KEY_INFO *p8info,
50                                              struct pkcs8_encrypt_ctx_st *ctx)
51 {
52     X509_SIG *p8 = NULL;
53     char buf[PEM_BUFSIZE];
54     const void *kstr = ctx->cipher_pass;
55     size_t klen = ctx->cipher_pass_length;
56
57     if (ctx->cipher == NULL)
58         return NULL;
59
60     if (kstr == NULL) {
61         if (!ctx->cb(buf, sizeof(buf), &klen, NULL, ctx->cbarg)) {
62             ERR_raise(ERR_LIB_PROV, PROV_R_READ_KEY);
63             return NULL;
64         }
65         kstr = buf;
66     }
67     /* NID == -1 means "standard" */
68     p8 = PKCS8_encrypt(-1, ctx->cipher, kstr, klen, NULL, 0, 0, p8info);
69     if (kstr == buf)
70         OPENSSL_cleanse(buf, klen);
71     return p8;
72 }
73
74 static X509_SIG *ossl_prov_encp8_from_obj(const void *obj, int obj_nid,
75                                           ASN1_STRING *params,
76                                           int params_type,
77                                           int (*k2d)(const void *obj,
78                                                      unsigned char **pder),
79                                           struct pkcs8_encrypt_ctx_st *ctx)
80 {
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);
84
85     PKCS8_PRIV_KEY_INFO_free(p8info);
86     return p8;
87 }
88
89 static X509_PUBKEY *ossl_prov_pubkey_from_obj(const void *obj, int obj_nid,
90                                               ASN1_STRING *params,
91                                               int params_type,
92                                               int (*k2d)(const void *obj,
93                                                          unsigned char **pder))
94 {
95     /* der, derlen store the key DER output and its length */
96     unsigned char *der = NULL;
97     int derlen;
98     /* The final X509_PUBKEY */
99     X509_PUBKEY *xpk = NULL;
100
101
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);
108         OPENSSL_free(der);
109         xpk = NULL;
110     }
111
112     return xpk;
113 }
114
115 OSSL_OP_keymgmt_importkey_fn *ossl_prov_get_importkey(const OSSL_DISPATCH *fns)
116 {
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);
121
122     return NULL;
123 }
124
125 # ifdef SIXTY_FOUR_BIT_LONG
126 #  define BN_FMTu "%lu"
127 #  define BN_FMTx "%lx"
128 # endif
129
130 # ifdef SIXTY_FOUR_BIT
131 #  define BN_FMTu "%llu"
132 #  define BN_FMTx "%llx"
133 # endif
134
135 # ifdef THIRTY_TWO_BIT
136 #  define BN_FMTu "%u"
137 #  define BN_FMTx "%x"
138 # endif
139
140 int ossl_prov_print_labeled_bignum(BIO *out, const char *label,
141                                    const BIGNUM *n)
142 {
143     const char *neg;
144     const char *post_label_spc = " ";
145     int bytes;
146     BN_ULONG *words;
147     int i, off;
148
149     if (n == NULL)
150         return 0;
151     if (label == NULL) {
152         label = "";
153         post_label_spc = "";
154     }
155
156     bytes = BN_num_bytes(n);
157     words = bn_get_words(n);
158     neg = BN_is_negative(n) ? "-" : "";
159
160     if (BN_is_zero(n))
161         return ossl_prov_bio_printf(out, "%s%s0\n", label, post_label_spc);
162
163     if (BN_num_bytes(n) <= 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],
167                                     neg, words[0]);
168
169     if (neg[0] == '-')
170         neg = " (Negative)";
171
172     if (ossl_prov_bio_printf(out, "%s%s\n", label, neg) <= 0)
173         return 0;
174
175     /* Skip past the zero bytes in the first word */
176     for (off = 0; off < BN_BYTES; off++) {
177         BN_ULONG l = words[0];
178         int o = 8 * (BN_BYTES - off - 1);
179         int b = ((l & (0xffLU << o)) >> o) & 0xff;
180
181         if (b != 0)
182             break;
183     }
184     /* print 16 hex digits per line, indented with 4 spaces */
185     for (i = 0; i < bytes; i += 16) {
186         int j, step;
187
188         if (ossl_prov_bio_printf(out, "    ") <= 0)
189             return 0;
190
191         for (j = 0; i + j < bytes && j < 16; j += BN_BYTES - step) {
192             int k;
193             BN_ULONG l = words[(i + j + off) / BN_BYTES];
194
195             step = (i + j + off) % BN_BYTES;
196
197             for (k = step; k < BN_BYTES; k++) {
198                 int o = 8 * (BN_BYTES - k - 1);
199                 int b = ((l & (0xffLU << o)) >> o) & 0xff;
200
201                 /* We may have reached the number of bytes prematurely */
202                 if (i + j + k - off >= bytes)
203                     break;
204
205                 if (ossl_prov_bio_printf(out, "%02x:", b) <= 0)
206                     return 0;
207             }
208         }
209
210         if (ossl_prov_bio_printf(out, "\n") <= 0)
211             return 0;
212     }
213
214     return 1;
215 }
216
217 /* p2s = param to asn1_string, k2d = key to der */
218 int ossl_prov_write_priv_der_from_obj(BIO *out, const void *obj, int obj_nid,
219                                       int (*p2s)(const void *obj, int nid,
220                                                  ASN1_STRING **str,
221                                                  int *strtype),
222                                       int (*k2d)(const void *obj,
223                                                  unsigned char **pder),
224                                       struct pkcs8_encrypt_ctx_st *ctx)
225 {
226     int ret = 0;
227     ASN1_STRING *str = NULL;
228     int strtype = 0;
229
230     if (p2s != NULL && !p2s(obj, obj_nid, &str, &strtype))
231         return 0;
232
233     if (ctx->cipher_intent) {
234         X509_SIG *p8 =
235             ossl_prov_encp8_from_obj(obj, obj_nid, str, strtype, k2d, ctx);
236
237         if (p8 != NULL)
238             ret = i2d_PKCS8_bio(out, p8);
239
240         X509_SIG_free(p8);
241     } else {
242         PKCS8_PRIV_KEY_INFO *p8info =
243             ossl_prov_p8info_from_obj(obj, obj_nid, str, strtype, k2d);
244
245         if (p8info != NULL)
246             ret = i2d_PKCS8_PRIV_KEY_INFO_bio(out, p8info);
247
248         PKCS8_PRIV_KEY_INFO_free(p8info);
249     }
250
251     return ret;
252 }
253
254 int ossl_prov_write_priv_pem_from_obj(BIO *out, const void *obj, int obj_nid,
255                                       int (*p2s)(const void *obj, int nid,
256                                                  ASN1_STRING **str,
257                                                  int *strtype),
258                                       int (*k2d)(const void *obj,
259                                                  unsigned char **pder),
260                                       struct pkcs8_encrypt_ctx_st *ctx)
261 {
262     int ret = 0;
263     ASN1_STRING *str = NULL;
264     int strtype = 0;
265
266     if (p2s != NULL && !p2s(obj, obj_nid, &str, &strtype))
267         return 0;
268
269     if (ctx->cipher_intent) {
270         X509_SIG *p8 = ossl_prov_encp8_from_obj(obj, obj_nid, str, strtype,
271                                                 k2d, ctx);
272
273         if (p8 != NULL)
274             ret = PEM_write_bio_PKCS8(out, p8);
275
276         X509_SIG_free(p8);
277     } else {
278         PKCS8_PRIV_KEY_INFO *p8info =
279             ossl_prov_p8info_from_obj(obj, obj_nid, str, strtype, k2d);
280
281         if (p8info != NULL)
282             ret = PEM_write_bio_PKCS8_PRIV_KEY_INFO(out, p8info);
283
284         PKCS8_PRIV_KEY_INFO_free(p8info);
285     }
286
287     return ret;
288 }
289
290 int ossl_prov_write_pub_der_from_obj(BIO *out, const void *obj, int obj_nid,
291                                      int (*p2s)(const void *obj, int nid,
292                                                 ASN1_STRING **str,
293                                                 int *strtype),
294                                      int (*k2d)(const void *obj,
295                                                 unsigned char **pder))
296 {
297     int ret = 0;
298     ASN1_STRING *str = NULL;
299     int strtype = 0;
300     X509_PUBKEY *xpk = NULL;
301
302     if (p2s != NULL && !p2s(obj, obj_nid, &str, &strtype))
303         return 0;
304
305     xpk = ossl_prov_pubkey_from_obj(obj, obj_nid, str, strtype, k2d);
306
307     if (xpk != NULL)
308         ret = i2d_X509_PUBKEY_bio(out, xpk);
309
310     /* Also frees |str| */
311     X509_PUBKEY_free(xpk);
312     return ret;
313 }
314
315 int ossl_prov_write_pub_pem_from_obj(BIO *out, const void *obj, int obj_nid,
316                                      int (*p2s)(const void *obj, int nid,
317                                                 ASN1_STRING **str,
318                                                 int *strtype),
319                                      int (*k2d)(const void *obj,
320                                                 unsigned char **pder))
321 {
322     int ret = 0;
323     ASN1_STRING *str = NULL;
324     int strtype = 0;
325     X509_PUBKEY *xpk = NULL;
326
327     if (p2s != NULL && !p2s(obj, obj_nid, &str, &strtype))
328         return 0;
329
330     xpk = ossl_prov_pubkey_from_obj(obj, obj_nid, str, strtype, k2d);
331
332     if (xpk != NULL)
333         ret = PEM_write_bio_X509_PUBKEY(out, xpk);
334
335     /* Also frees |str| */
336     X509_PUBKEY_free(xpk);
337     return ret;
338 }
339