Add Serializers for EC
[openssl.git] / providers / implementations / serializers / serializer_local.h
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/core.h>
11 #include <openssl/core_numbers.h>
12 #include <openssl/bn.h>
13 #include <openssl/asn1.h>        /* i2d_of_void */
14 #include <openssl/x509.h>        /* X509_SIG */
15 #include <openssl/types.h>
16 #include <crypto/ecx.h>
17
18 struct pkcs8_encrypt_ctx_st {
19     /* Set to 1 if intending to encrypt/decrypt, otherwise 0 */
20     int cipher_intent;
21
22     EVP_CIPHER *cipher;
23     int pbe_nid;                 /* For future variation */
24
25     /* Passphrase that was passed by the caller */
26     void *cipher_pass;
27     size_t cipher_pass_length;
28
29     /* This callback is only used of |cipher_pass| is NULL */
30     OSSL_PASSPHRASE_CALLBACK *cb;
31     void *cbarg;
32 };
33
34 typedef enum {
35     ECX_KEY_TYPE_X25519,
36     ECX_KEY_TYPE_X448
37 } ECX_KEY_TYPE;
38
39 OSSL_OP_keymgmt_new_fn *ossl_prov_get_keymgmt_new(const OSSL_DISPATCH *fns);
40 OSSL_OP_keymgmt_free_fn *ossl_prov_get_keymgmt_free(const OSSL_DISPATCH *fns);
41 OSSL_OP_keymgmt_import_fn *ossl_prov_get_keymgmt_import(const OSSL_DISPATCH *fns);
42
43 OSSL_OP_keymgmt_new_fn *ossl_prov_get_keymgmt_rsa_new(void);
44 OSSL_OP_keymgmt_free_fn *ossl_prov_get_keymgmt_rsa_free(void);
45 OSSL_OP_keymgmt_import_fn *ossl_prov_get_keymgmt_rsa_import(void);
46 OSSL_OP_keymgmt_new_fn *ossl_prov_get_keymgmt_dh_new(void);
47 OSSL_OP_keymgmt_free_fn *ossl_prov_get_keymgmt_dh_free(void);
48 OSSL_OP_keymgmt_import_fn *ossl_prov_get_keymgmt_dh_import(void);
49 OSSL_OP_keymgmt_new_fn *ossl_prov_get_keymgmt_dsa_new(void);
50 OSSL_OP_keymgmt_free_fn *ossl_prov_get_keymgmt_dsa_free(void);
51 OSSL_OP_keymgmt_import_fn *ossl_prov_get_keymgmt_dsa_import(void);
52
53 void ec_get_new_free_import(OSSL_OP_keymgmt_new_fn **ec_new,
54                             OSSL_OP_keymgmt_free_fn **ec_free,
55                             OSSL_OP_keymgmt_import_fn **ec_import);
56
57 int ossl_prov_prepare_ec_params(const void *eckey, int nid,
58                                 void **pstr, int *pstrtype);
59 int ossl_prov_ec_pub_to_der(const void *eckey, unsigned char **pder);
60 int ossl_prov_ec_priv_to_der(const void *eckey, unsigned char **pder);
61
62 int ossl_prov_prepare_dh_params(const void *dh, int nid,
63                                 void **pstr, int *pstrtype);
64 int ossl_prov_dh_pub_to_der(const void *dh, unsigned char **pder);
65 int ossl_prov_dh_priv_to_der(const void *dh, unsigned char **pder);
66
67 void ecx_get_new_free_import(ECX_KEY_TYPE type,
68                              OSSL_OP_keymgmt_new_fn **ecx_new,
69                              OSSL_OP_keymgmt_free_fn **ecx_free,
70                              OSSL_OP_keymgmt_import_fn **ecx_import);
71 int ossl_prov_ecx_pub_to_der(const void *ecxkey, unsigned char **pder);
72 int ossl_prov_ecx_priv_to_der(const void *ecxkey, unsigned char **pder);
73
74 int ossl_prov_prepare_dsa_params(const void *dsa, int nid,
75                                 void **pstr, int *pstrtype);
76 /*
77  * Special variant of ossl_prov_prepare_dsa_params() that requires all
78  * three parameters (P, Q and G) to be set.  This is used when serializing
79  * the public key.
80  */
81 int ossl_prov_prepare_all_dsa_params(const void *dsa, int nid,
82                                      void **pstr, int *pstrtype);
83 int ossl_prov_dsa_pub_to_der(const void *dsa, unsigned char **pder);
84 int ossl_prov_dsa_priv_to_der(const void *dsa, unsigned char **pder);
85
86 int ossl_prov_print_labeled_bignum(BIO *out, const char *label,
87                                    const BIGNUM *bn);
88 int ossl_prov_print_labeled_buf(BIO *out, const char *label,
89                                 const unsigned char *buf, size_t buflen);
90 int ossl_prov_print_rsa(BIO *out, RSA *rsa, int priv);
91
92 enum dh_print_type {
93     dh_print_priv,
94     dh_print_pub,
95     dh_print_params
96 };
97
98 int ossl_prov_print_dh(BIO *out, DH *dh, enum dh_print_type type);
99
100 #ifndef OPENSSL_NO_EC
101 enum ec_print_type {
102     ec_print_priv,
103     ec_print_pub,
104     ec_print_params
105 };
106
107 int ossl_prov_print_eckey(BIO *out, EC_KEY *eckey, enum ec_print_type type);
108 #endif /*  OPENSSL_NO_EC */
109
110 enum dsa_print_type {
111     dsa_print_priv,
112     dsa_print_pub,
113     dsa_print_params
114 };
115
116 int ossl_prov_print_dsa(BIO *out, DSA *dsa, enum dsa_print_type type);
117
118 enum ecx_print_type {
119     ecx_print_priv,
120     ecx_print_pub
121 };
122
123 #ifndef OPENSSL_NO_EC
124 int ossl_prov_print_ecx(BIO *out, ECX_KEY *ecxkey, enum ecx_print_type type);
125 #endif
126
127 int ossl_prov_write_priv_der_from_obj(BIO *out, const void *obj, int obj_nid,
128                                       int (*p2s)(const void *obj, int nid,
129                                                  void **str,
130                                                  int *strtype),
131                                       int (*k2d)(const void *obj,
132                                                  unsigned char **pder),
133                                       struct pkcs8_encrypt_ctx_st *ctx);
134 int ossl_prov_write_priv_pem_from_obj(BIO *out, const void *obj, int obj_nid,
135                                       int (*p2s)(const void *obj, int nid,
136                                                  void **str,
137                                                  int *strtype),
138                                       int (*k2d)(const void *obj,
139                                                  unsigned char **pder),
140                                       struct pkcs8_encrypt_ctx_st *ctx);
141 int ossl_prov_write_pub_der_from_obj(BIO *out, const void *obj, int obj_nid,
142                                      int (*p2s)(const void *obj, int nid,
143                                                 void **str,
144                                                 int *strtype),
145                                      int (*k2d)(const void *obj,
146                                                 unsigned char **pder));
147 int ossl_prov_write_pub_pem_from_obj(BIO *out, const void *obj, int obj_nid,
148                                      int (*p2s)(const void *obj, int nid,
149                                                 void **str,
150                                                 int *strtype),
151                                      int (*k2d)(const void *obj,
152                                                 unsigned char **pder));