Add Serializers for EC
[openssl.git] / providers / implementations / serializers / serializer_ec.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 #include <openssl/err.h>
11 #include "crypto/ec.h"
12 #include "prov/bio.h"             /* ossl_prov_bio_printf() */
13 #include "prov/implementations.h" /* ec_keymgmt_functions */
14 #include "serializer_local.h"
15
16 void ec_get_new_free_import(OSSL_OP_keymgmt_new_fn **ec_new,
17                             OSSL_OP_keymgmt_free_fn **ec_free,
18                             OSSL_OP_keymgmt_import_fn **ec_import)
19 {
20     *ec_new = ossl_prov_get_keymgmt_new(ec_keymgmt_functions);
21     *ec_free = ossl_prov_get_keymgmt_free(ec_keymgmt_functions);
22     *ec_import = ossl_prov_get_keymgmt_import(ec_keymgmt_functions);
23 }
24
25 static int ossl_prov_print_ec_param(BIO *out, const EC_GROUP *group)
26 {
27     const char *curve_name;
28     int curve_nid = EC_GROUP_get_curve_name(group);
29
30     /* TODO(3.0): Explicit parameters are currently not supported */
31     if (curve_nid == NID_undef)
32         return 0;
33
34     if (ossl_prov_bio_printf(out, "%s: %s\n", "ASN1 OID",
35                              OBJ_nid2sn(curve_nid)) <= 0)
36         return 0;
37
38     /* TODO(3.0): Only named curves are currently supported */
39     curve_name = EC_curve_nid2nist(curve_nid);
40     return (curve_name == NULL
41             || ossl_prov_bio_printf(out, "%s: %s\n", "NIST CURVE",
42                                     curve_name) > 0);
43 }
44
45 int ossl_prov_print_eckey(BIO *out, EC_KEY *eckey, enum ec_print_type type)
46 {
47     int ret = 0;
48     const char *type_label = NULL;
49     unsigned char *priv = NULL, *pub = NULL;
50     size_t priv_len = 0, pub_len = 0;
51     const EC_GROUP *group;
52
53     if (eckey == NULL || (group = EC_KEY_get0_group(eckey)) == NULL)
54         goto null_err;
55
56     switch (type) {
57     case ec_print_priv:
58         type_label = "Private-Key";
59         break;
60     case ec_print_pub:
61         type_label = "Public-Key";
62         break;
63     case ec_print_params:
64         type_label = "EC-Parameters";
65         break;
66     }
67
68     if (type == ec_print_priv) {
69         const BIGNUM *priv_key = EC_KEY_get0_private_key(eckey);
70
71         if (priv_key == NULL)
72             goto null_err;
73         priv_len = EC_KEY_priv2buf(eckey, &priv);
74         if (priv_len == 0)
75             goto err;
76     }
77
78     if (type == ec_print_priv || type == ec_print_pub) {
79         const EC_POINT *pub_pt = EC_KEY_get0_public_key(eckey);
80
81         if (pub_pt == NULL)
82             goto null_err;
83
84         pub_len = EC_KEY_key2buf(eckey, EC_KEY_get_conv_form(eckey), &pub, NULL);
85         if (pub_len == 0)
86             goto err;
87     }
88
89     if (ossl_prov_bio_printf(out, "%s: (%d bit)\n", type_label,
90                              EC_GROUP_order_bits(group)) <= 0)
91         goto err;
92     if (priv != NULL
93         && !ossl_prov_print_labeled_buf(out, "priv:", priv, priv_len))
94         goto err;
95     if (pub != NULL
96         && !ossl_prov_print_labeled_buf(out, "pub:", pub, pub_len))
97         goto err;
98     ret = ossl_prov_print_ec_param(out, group);
99 err:
100     OPENSSL_clear_free(priv, priv_len);
101     OPENSSL_free(pub);
102     return ret;
103 null_err:
104     ERR_raise(ERR_LIB_PROV, ERR_R_PASSED_NULL_PARAMETER);
105     goto err;
106 }
107
108 int ossl_prov_prepare_ec_params(const void *eckey, int nid,
109                                 void **pstr, int *pstrtype)
110 {
111     int curve_nid;
112     const EC_GROUP *group = EC_KEY_get0_group(eckey);
113     ASN1_OBJECT *params;
114
115     if (group == NULL
116         || ((curve_nid = EC_GROUP_get_curve_name(group)) == NID_undef)
117         || ((params = OBJ_nid2obj(curve_nid)) == NULL)) {
118         /* TODO(3.0): Explicit curves are not supported */
119         return 0;
120     }
121
122     *pstr = params;
123     *pstrtype = V_ASN1_OBJECT;
124     return 1;
125 }
126
127 int ossl_prov_ec_pub_to_der(const void *eckey, unsigned char **pder)
128 {
129     return i2o_ECPublicKey(eckey, pder);
130 }
131
132 int ossl_prov_ec_priv_to_der(const void *veckey, unsigned char **pder)
133 {
134     EC_KEY *eckey = (EC_KEY *)veckey;
135     unsigned int old_flags;
136     int ret = 0;
137
138     /*
139      * For PKCS8 the curve name appears in the PKCS8_PRIV_KEY_INFO object
140      * as the pkeyalg->parameter field. (For a named curve this is an OID)
141      * The pkey field is an octet string that holds the encoded
142      * ECPrivateKey SEQUENCE with the optional parameters field omitted.
143      * We omit this by setting the EC_PKEY_NO_PARAMETERS flag.
144      */
145     old_flags = EC_KEY_get_enc_flags(eckey); /* save old flags */
146     EC_KEY_set_enc_flags(eckey, old_flags | EC_PKEY_NO_PARAMETERS);
147     ret = i2d_ECPrivateKey(eckey, pder);
148     EC_KEY_set_enc_flags(eckey, old_flags); /* restore old flags */
149     return ret; /* return the length of the der encoded data */
150 }