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