Add libctx and propq param to ASN.1 sign/verify/HMAC/decrypt
[openssl.git] / include / crypto / asn1.h
1 /*
2  * Copyright 2015-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 /* Internal ASN1 structures and functions: not for application use */
11
12 /* ASN1 public key method structure */
13
14 #include <openssl/core.h>
15
16 struct evp_pkey_asn1_method_st {
17     int pkey_id;
18     int pkey_base_id;
19     unsigned long pkey_flags;
20     char *pem_str;
21     char *info;
22     int (*pub_decode) (EVP_PKEY *pk, const X509_PUBKEY *pub);
23     int (*pub_encode) (X509_PUBKEY *pub, const EVP_PKEY *pk);
24     int (*pub_cmp) (const EVP_PKEY *a, const EVP_PKEY *b);
25     int (*pub_print) (BIO *out, const EVP_PKEY *pkey, int indent,
26                       ASN1_PCTX *pctx);
27     int (*priv_decode) (EVP_PKEY *pk, const PKCS8_PRIV_KEY_INFO *p8inf);
28     int (*priv_encode) (PKCS8_PRIV_KEY_INFO *p8, const EVP_PKEY *pk);
29     int (*priv_print) (BIO *out, const EVP_PKEY *pkey, int indent,
30                        ASN1_PCTX *pctx);
31     int (*pkey_size) (const EVP_PKEY *pk);
32     int (*pkey_bits) (const EVP_PKEY *pk);
33     int (*pkey_security_bits) (const EVP_PKEY *pk);
34     int (*param_decode) (EVP_PKEY *pkey,
35                          const unsigned char **pder, int derlen);
36     int (*param_encode) (const EVP_PKEY *pkey, unsigned char **pder);
37     int (*param_missing) (const EVP_PKEY *pk);
38     int (*param_copy) (EVP_PKEY *to, const EVP_PKEY *from);
39     int (*param_cmp) (const EVP_PKEY *a, const EVP_PKEY *b);
40     int (*param_print) (BIO *out, const EVP_PKEY *pkey, int indent,
41                         ASN1_PCTX *pctx);
42     int (*sig_print) (BIO *out,
43                       const X509_ALGOR *sigalg, const ASN1_STRING *sig,
44                       int indent, ASN1_PCTX *pctx);
45     void (*pkey_free) (EVP_PKEY *pkey);
46     int (*pkey_ctrl) (EVP_PKEY *pkey, int op, long arg1, void *arg2);
47     /* Legacy functions for old PEM */
48     int (*old_priv_decode) (EVP_PKEY *pkey,
49                             const unsigned char **pder, int derlen);
50     int (*old_priv_encode) (const EVP_PKEY *pkey, unsigned char **pder);
51     /* Custom ASN1 signature verification */
52     int (*item_verify) (EVP_MD_CTX *ctx, const ASN1_ITEM *it, const void *data,
53                         const X509_ALGOR *a, const ASN1_BIT_STRING *sig,
54                         EVP_PKEY *pkey);
55     int (*item_sign) (EVP_MD_CTX *ctx, const ASN1_ITEM *it, const void *data,
56                       X509_ALGOR *alg1, X509_ALGOR *alg2,
57                       ASN1_BIT_STRING *sig);
58     int (*siginf_set) (X509_SIG_INFO *siginf, const X509_ALGOR *alg,
59                        const ASN1_STRING *sig);
60     /* Check */
61     int (*pkey_check) (const EVP_PKEY *pk);
62     int (*pkey_public_check) (const EVP_PKEY *pk);
63     int (*pkey_param_check) (const EVP_PKEY *pk);
64     /* Get/set raw private/public key data */
65     int (*set_priv_key) (EVP_PKEY *pk, const unsigned char *priv, size_t len);
66     int (*set_pub_key) (EVP_PKEY *pk, const unsigned char *pub, size_t len);
67     int (*get_priv_key) (const EVP_PKEY *pk, unsigned char *priv, size_t *len);
68     int (*get_pub_key) (const EVP_PKEY *pk, unsigned char *pub, size_t *len);
69
70     /*
71      * TODO: Make sure these functions are defined for key types that are
72      * implemented in providers.
73      */
74     /* Exports and imports to / from providers */
75     size_t (*dirty_cnt) (const EVP_PKEY *pk);
76     int (*export_to) (const EVP_PKEY *pk, void *to_keydata,
77                       EVP_KEYMGMT *to_keymgmt, OPENSSL_CTX *libctx,
78                       const char *propq);
79     OSSL_CALLBACK *import_from;
80
81     int (*priv_decode_with_libctx) (EVP_PKEY *pk,
82                                     const PKCS8_PRIV_KEY_INFO *p8inf,
83                                     OPENSSL_CTX *libctx,
84                                     const char *propq);
85 } /* EVP_PKEY_ASN1_METHOD */ ;
86
87 DEFINE_STACK_OF_CONST(EVP_PKEY_ASN1_METHOD)
88
89 extern const EVP_PKEY_ASN1_METHOD cmac_asn1_meth;
90 extern const EVP_PKEY_ASN1_METHOD dh_asn1_meth;
91 extern const EVP_PKEY_ASN1_METHOD dhx_asn1_meth;
92 extern const EVP_PKEY_ASN1_METHOD dsa_asn1_meths[5];
93 extern const EVP_PKEY_ASN1_METHOD eckey_asn1_meth;
94 extern const EVP_PKEY_ASN1_METHOD ecx25519_asn1_meth;
95 extern const EVP_PKEY_ASN1_METHOD ecx448_asn1_meth;
96 extern const EVP_PKEY_ASN1_METHOD ed25519_asn1_meth;
97 extern const EVP_PKEY_ASN1_METHOD ed448_asn1_meth;
98 extern const EVP_PKEY_ASN1_METHOD sm2_asn1_meth;
99 extern const EVP_PKEY_ASN1_METHOD poly1305_asn1_meth;
100
101 extern const EVP_PKEY_ASN1_METHOD hmac_asn1_meth;
102 extern const EVP_PKEY_ASN1_METHOD rsa_asn1_meths[2];
103 extern const EVP_PKEY_ASN1_METHOD rsa_pss_asn1_meth;
104 extern const EVP_PKEY_ASN1_METHOD siphash_asn1_meth;
105
106 /*
107  * These are used internally in the ASN1_OBJECT to keep track of whether the
108  * names and data need to be free()ed
109  */
110 # define ASN1_OBJECT_FLAG_DYNAMIC         0x01/* internal use */
111 # define ASN1_OBJECT_FLAG_CRITICAL        0x02/* critical x509v3 object id */
112 # define ASN1_OBJECT_FLAG_DYNAMIC_STRINGS 0x04/* internal use */
113 # define ASN1_OBJECT_FLAG_DYNAMIC_DATA    0x08/* internal use */
114 struct asn1_object_st {
115     const char *sn, *ln;
116     int nid;
117     int length;
118     const unsigned char *data;  /* data remains const after init */
119     int flags;                  /* Should we free this one */
120 };
121
122 /* ASN1 print context structure */
123
124 struct asn1_pctx_st {
125     unsigned long flags;
126     unsigned long nm_flags;
127     unsigned long cert_flags;
128     unsigned long oid_flags;
129     unsigned long str_flags;
130 } /* ASN1_PCTX */ ;
131
132 int asn1_d2i_read_bio(BIO *in, BUF_MEM **pb);