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