Implement the ECX Serializers
[oweals/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 int ossl_prov_prepare_dh_params(const void *dh, int nid,
54                                 ASN1_STRING **pstr, int *pstrtype);
55 int ossl_prov_dh_pub_to_der(const void *dh, unsigned char **pder);
56 int ossl_prov_dh_priv_to_der(const void *dh, unsigned char **pder);
57
58 void ecx_get_new_free_import(ECX_KEY_TYPE type,
59                              OSSL_OP_keymgmt_new_fn **ecx_new,
60                              OSSL_OP_keymgmt_free_fn **ecx_free,
61                              OSSL_OP_keymgmt_import_fn **ecx_import);
62 int ossl_prov_ecx_pub_to_der(const void *ecxkey, unsigned char **pder);
63 int ossl_prov_ecx_priv_to_der(const void *ecxkey, unsigned char **pder);
64
65 int ossl_prov_prepare_dsa_params(const void *dsa, int nid,
66                                  ASN1_STRING **pstr, int *pstrtype);
67 /*
68  * Special variant of ossl_prov_prepare_dsa_params() that requires all
69  * three parameters (P, Q and G) to be set.  This is used when serializing
70  * the public key.
71  */
72 int ossl_prov_prepare_all_dsa_params(const void *dsa, int nid,
73                                      ASN1_STRING **pstr, int *pstrtype);
74 int ossl_prov_dsa_pub_to_der(const void *dsa, unsigned char **pder);
75 int ossl_prov_dsa_priv_to_der(const void *dsa, unsigned char **pder);
76
77 int ossl_prov_print_labeled_bignum(BIO *out, const char *label,
78                                    const BIGNUM *bn);
79 int ossl_prov_print_labeled_buf(BIO *out, const char *label,
80                                 const unsigned char *buf, size_t buflen);
81 int ossl_prov_print_rsa(BIO *out, RSA *rsa, int priv);
82
83 enum dh_print_type {
84     dh_print_priv,
85     dh_print_pub,
86     dh_print_params
87 };
88
89 int ossl_prov_print_dh(BIO *out, DH *dh, enum dh_print_type type);
90
91 enum dsa_print_type {
92     dsa_print_priv,
93     dsa_print_pub,
94     dsa_print_params
95 };
96
97 int ossl_prov_print_dsa(BIO *out, DSA *dsa, enum dsa_print_type type);
98
99 enum ecx_print_type {
100     ecx_print_priv,
101     ecx_print_pub
102 };
103
104 #ifndef OPENSSL_NO_EC
105 int ossl_prov_print_ecx(BIO *out, ECX_KEY *ecxkey, enum ecx_print_type type);
106 #endif
107
108 int ossl_prov_write_priv_der_from_obj(BIO *out, const void *obj, int obj_nid,
109                                       int (*p2s)(const void *obj, int nid,
110                                                  ASN1_STRING **str,
111                                                  int *strtype),
112                                       int (*k2d)(const void *obj,
113                                                  unsigned char **pder),
114                                       struct pkcs8_encrypt_ctx_st *ctx);
115 int ossl_prov_write_priv_pem_from_obj(BIO *out, const void *obj, int obj_nid,
116                                       int (*p2s)(const void *obj, int nid,
117                                                  ASN1_STRING **str,
118                                                  int *strtype),
119                                       int (*k2d)(const void *obj,
120                                                  unsigned char **pder),
121                                       struct pkcs8_encrypt_ctx_st *ctx);
122 int ossl_prov_write_pub_der_from_obj(BIO *out, const void *obj, int obj_nid,
123                                      int (*p2s)(const void *obj, int nid,
124                                                 ASN1_STRING **str,
125                                                 int *strtype),
126                                      int (*k2d)(const void *obj,
127                                                 unsigned char **pder));
128 int ossl_prov_write_pub_pem_from_obj(BIO *out, const void *obj, int obj_nid,
129                                      int (*p2s)(const void *obj, int nid,
130                                                 ASN1_STRING **str,
131                                                 int *strtype),
132                                      int (*k2d)(const void *obj,
133                                                 unsigned char **pder));