Make EVP_PKEY_CTX_[get|set]_group_name work for DH too
[openssl.git] / crypto / evp / evp_local.h
1 /*
2  * Copyright 2000-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 /* EVP_MD_CTX related stuff */
11
12 #include <openssl/core_numbers.h>
13
14 #define EVP_CTRL_RET_UNSUPPORTED -1
15
16
17 struct evp_md_ctx_st {
18     const EVP_MD *reqdigest;    /* The original requested digest */
19     const EVP_MD *digest;
20     ENGINE *engine;             /* functional reference if 'digest' is
21                                  * ENGINE-provided */
22     unsigned long flags;
23     void *md_data;
24     /* Public key context for sign/verify */
25     EVP_PKEY_CTX *pctx;
26     /* Update function: usually copied from EVP_MD */
27     int (*update) (EVP_MD_CTX *ctx, const void *data, size_t count);
28
29     /* Provider ctx */
30     void *provctx;
31     EVP_MD *fetched_digest;
32 } /* EVP_MD_CTX */ ;
33
34 struct evp_cipher_ctx_st {
35     const EVP_CIPHER *cipher;
36     ENGINE *engine;             /* functional reference if 'cipher' is
37                                  * ENGINE-provided */
38     int encrypt;                /* encrypt or decrypt */
39     int buf_len;                /* number we have left */
40     unsigned char oiv[EVP_MAX_IV_LENGTH]; /* original iv */
41     unsigned char iv[EVP_MAX_IV_LENGTH]; /* working iv */
42     unsigned char buf[EVP_MAX_BLOCK_LENGTH]; /* saved partial block */
43     int num;                    /* used by cfb/ofb/ctr mode */
44     /* FIXME: Should this even exist? It appears unused */
45     void *app_data;             /* application stuff */
46     int key_len;                /* May change for variable length cipher */
47     unsigned long flags;        /* Various flags */
48     void *cipher_data;          /* per EVP data */
49     int final_used;
50     int block_mask;
51     unsigned char final[EVP_MAX_BLOCK_LENGTH]; /* possible final block */
52
53     /* Provider ctx */
54     void *provctx;
55     EVP_CIPHER *fetched_cipher;
56 } /* EVP_CIPHER_CTX */ ;
57
58 struct evp_mac_ctx_st {
59     EVP_MAC *meth;               /* Method structure */
60     void *data;                  /* Individual method data */
61 } /* EVP_MAC_CTX */;
62
63 struct evp_kdf_ctx_st {
64     EVP_KDF *meth;              /* Method structure */
65     void *data;                 /* Algorithm-specific data */
66 } /* EVP_KDF_CTX */ ;
67
68 struct evp_keymgmt_st {
69     int id;                      /* libcrypto internal */
70
71     int name_id;
72     OSSL_PROVIDER *prov;
73     CRYPTO_REF_COUNT refcnt;
74     CRYPTO_RWLOCK *lock;
75
76     /* Constructor(s), destructor, information */
77     OSSL_OP_keymgmt_new_fn *new;
78     OSSL_OP_keymgmt_free_fn *free;
79     OSSL_OP_keymgmt_get_params_fn *get_params;
80     OSSL_OP_keymgmt_gettable_params_fn *gettable_params;
81     OSSL_OP_keymgmt_set_params_fn *set_params;
82     OSSL_OP_keymgmt_settable_params_fn *settable_params;
83
84     /* Generation, a complex constructor */
85     OSSL_OP_keymgmt_gen_init_fn *gen_init;
86     OSSL_OP_keymgmt_gen_set_template_fn *gen_set_template;
87     OSSL_OP_keymgmt_gen_set_params_fn *gen_set_params;
88     OSSL_OP_keymgmt_gen_settable_params_fn *gen_settable_params;
89     OSSL_OP_keymgmt_gen_fn *gen;
90     OSSL_OP_keymgmt_gen_cleanup_fn *gen_cleanup;
91
92     /* Key object checking */
93     OSSL_OP_keymgmt_query_operation_name_fn *query_operation_name;
94     OSSL_OP_keymgmt_has_fn *has;
95     OSSL_OP_keymgmt_validate_fn *validate;
96     OSSL_OP_keymgmt_match_fn *match;
97
98     /* Import and export routines */
99     OSSL_OP_keymgmt_import_fn *import;
100     OSSL_OP_keymgmt_import_types_fn *import_types;
101     OSSL_OP_keymgmt_export_fn *export;
102     OSSL_OP_keymgmt_export_types_fn *export_types;
103     OSSL_OP_keymgmt_copy_fn *copy;
104 } /* EVP_KEYMGMT */ ;
105
106 struct evp_keyexch_st {
107     int name_id;
108     OSSL_PROVIDER *prov;
109     CRYPTO_REF_COUNT refcnt;
110     CRYPTO_RWLOCK *lock;
111
112     OSSL_OP_keyexch_newctx_fn *newctx;
113     OSSL_OP_keyexch_init_fn *init;
114     OSSL_OP_keyexch_set_peer_fn *set_peer;
115     OSSL_OP_keyexch_derive_fn *derive;
116     OSSL_OP_keyexch_freectx_fn *freectx;
117     OSSL_OP_keyexch_dupctx_fn *dupctx;
118     OSSL_OP_keyexch_set_ctx_params_fn *set_ctx_params;
119     OSSL_OP_keyexch_settable_ctx_params_fn *settable_ctx_params;
120     OSSL_OP_keyexch_get_ctx_params_fn *get_ctx_params;
121     OSSL_OP_keyexch_gettable_ctx_params_fn *gettable_ctx_params;
122 } /* EVP_KEYEXCH */;
123
124 struct evp_signature_st {
125     int name_id;
126     OSSL_PROVIDER *prov;
127     CRYPTO_REF_COUNT refcnt;
128     CRYPTO_RWLOCK *lock;
129
130     OSSL_OP_signature_newctx_fn *newctx;
131     OSSL_OP_signature_sign_init_fn *sign_init;
132     OSSL_OP_signature_sign_fn *sign;
133     OSSL_OP_signature_verify_init_fn *verify_init;
134     OSSL_OP_signature_verify_fn *verify;
135     OSSL_OP_signature_verify_recover_init_fn *verify_recover_init;
136     OSSL_OP_signature_verify_recover_fn *verify_recover;
137     OSSL_OP_signature_digest_sign_init_fn *digest_sign_init;
138     OSSL_OP_signature_digest_sign_update_fn *digest_sign_update;
139     OSSL_OP_signature_digest_sign_final_fn *digest_sign_final;
140     OSSL_OP_signature_digest_sign_fn *digest_sign;
141     OSSL_OP_signature_digest_verify_init_fn *digest_verify_init;
142     OSSL_OP_signature_digest_verify_update_fn *digest_verify_update;
143     OSSL_OP_signature_digest_verify_final_fn *digest_verify_final;
144     OSSL_OP_signature_digest_verify_fn *digest_verify;
145     OSSL_OP_signature_freectx_fn *freectx;
146     OSSL_OP_signature_dupctx_fn *dupctx;
147     OSSL_OP_signature_get_ctx_params_fn *get_ctx_params;
148     OSSL_OP_signature_gettable_ctx_params_fn *gettable_ctx_params;
149     OSSL_OP_signature_set_ctx_params_fn *set_ctx_params;
150     OSSL_OP_signature_settable_ctx_params_fn *settable_ctx_params;
151     OSSL_OP_signature_get_ctx_md_params_fn *get_ctx_md_params;
152     OSSL_OP_signature_gettable_ctx_md_params_fn *gettable_ctx_md_params;
153     OSSL_OP_signature_set_ctx_md_params_fn *set_ctx_md_params;
154     OSSL_OP_signature_settable_ctx_md_params_fn *settable_ctx_md_params;
155 } /* EVP_SIGNATURE */;
156
157 struct evp_asym_cipher_st {
158     int name_id;
159     OSSL_PROVIDER *prov;
160     CRYPTO_REF_COUNT refcnt;
161     CRYPTO_RWLOCK *lock;
162
163     OSSL_OP_asym_cipher_newctx_fn *newctx;
164     OSSL_OP_asym_cipher_encrypt_init_fn *encrypt_init;
165     OSSL_OP_asym_cipher_encrypt_fn *encrypt;
166     OSSL_OP_asym_cipher_decrypt_init_fn *decrypt_init;
167     OSSL_OP_asym_cipher_decrypt_fn *decrypt;
168     OSSL_OP_asym_cipher_freectx_fn *freectx;
169     OSSL_OP_asym_cipher_dupctx_fn *dupctx;
170     OSSL_OP_asym_cipher_get_ctx_params_fn *get_ctx_params;
171     OSSL_OP_asym_cipher_gettable_ctx_params_fn *gettable_ctx_params;
172     OSSL_OP_asym_cipher_set_ctx_params_fn *set_ctx_params;
173     OSSL_OP_asym_cipher_settable_ctx_params_fn *settable_ctx_params;
174 } /* EVP_ASYM_CIPHER */;
175
176 int PKCS5_v2_PBKDF2_keyivgen(EVP_CIPHER_CTX *ctx, const char *pass,
177                              int passlen, ASN1_TYPE *param,
178                              const EVP_CIPHER *c, const EVP_MD *md,
179                              int en_de);
180
181 struct evp_Encode_Ctx_st {
182     /* number saved in a partial encode/decode */
183     int num;
184     /*
185      * The length is either the output line length (in input bytes) or the
186      * shortest input line length that is ok.  Once decoding begins, the
187      * length is adjusted up each time a longer line is decoded
188      */
189     int length;
190     /* data to encode */
191     unsigned char enc_data[80];
192     /* number read on current line */
193     int line_num;
194     unsigned int flags;
195 };
196
197 typedef struct evp_pbe_st EVP_PBE_CTL;
198 DEFINE_STACK_OF(EVP_PBE_CTL)
199
200 int is_partially_overlapping(const void *ptr1, const void *ptr2, int len);
201
202 #include <openssl/types.h>
203 #include <openssl/core.h>
204
205 void *evp_generic_fetch(OPENSSL_CTX *ctx, int operation_id,
206                         const char *name, const char *properties,
207                         void *(*new_method)(int name_id,
208                                             const OSSL_DISPATCH *fns,
209                                             OSSL_PROVIDER *prov),
210                         int (*up_ref_method)(void *),
211                         void (*free_method)(void *));
212 void *evp_generic_fetch_by_number(OPENSSL_CTX *ctx, int operation_id,
213                                   int name_id, const char *properties,
214                                   void *(*new_method)(int name_id,
215                                                       const OSSL_DISPATCH *fns,
216                                                       OSSL_PROVIDER *prov),
217                                   int (*up_ref_method)(void *),
218                                   void (*free_method)(void *));
219 void evp_generic_do_all(OPENSSL_CTX *libctx, int operation_id,
220                         void (*user_fn)(void *method, void *arg),
221                         void *user_arg,
222                         void *(*new_method)(int name_id,
223                                             const OSSL_DISPATCH *fns,
224                                             OSSL_PROVIDER *prov),
225                         void (*free_method)(void *));
226
227 /* Internal fetchers for method types that are to be combined with others */
228 EVP_KEYMGMT *evp_keymgmt_fetch_by_number(OPENSSL_CTX *ctx, int name_id,
229                                          const char *properties);
230
231 /* Internal structure constructors for fetched methods */
232 EVP_MD *evp_md_new(void);
233 EVP_CIPHER *evp_cipher_new(void);
234
235 /* Helper functions to avoid duplicating code */
236
237 /*
238  * These methods implement different ways to pass a params array to the
239  * provider.  They will return one of these values:
240  *
241  * -2 if the method doesn't come from a provider
242  *    (evp_do_param will return this to the called)
243  * -1 if the provider doesn't offer the desired function
244  *    (evp_do_param will raise an error and return 0)
245  * or the return value from the desired function
246  *    (evp_do_param will return it to the caller)
247  */
248 int evp_do_ciph_getparams(const EVP_CIPHER *ciph, OSSL_PARAM params[]);
249 int evp_do_ciph_ctx_getparams(const EVP_CIPHER *ciph, void *provctx,
250                               OSSL_PARAM params[]);
251 int evp_do_ciph_ctx_setparams(const EVP_CIPHER *ciph, void *provctx,
252                               OSSL_PARAM params[]);
253 int evp_do_md_getparams(const EVP_MD *md, OSSL_PARAM params[]);
254 int evp_do_md_ctx_getparams(const EVP_MD *md, void *provctx,
255                             OSSL_PARAM params[]);
256 int evp_do_md_ctx_setparams(const EVP_MD *md, void *provctx,
257                             OSSL_PARAM params[]);
258
259 OSSL_PARAM *evp_pkey_to_param(EVP_PKEY *pkey, size_t *sz);
260
261 #define M_check_autoarg(ctx, arg, arglen, err) \
262     if (ctx->pmeth->flags & EVP_PKEY_FLAG_AUTOARGLEN) {           \
263         size_t pksize = (size_t)EVP_PKEY_size(ctx->pkey);         \
264                                                                   \
265         if (pksize == 0) {                                        \
266             ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_KEY); /*ckerr_ignore*/ \
267             return 0;                                             \
268         }                                                         \
269         if (arg == NULL) {                                        \
270             *arglen = pksize;                                     \
271             return 1;                                             \
272         }                                                         \
273         if (*arglen < pksize) {                                   \
274             ERR_raise(ERR_LIB_EVP, EVP_R_BUFFER_TOO_SMALL); /*ckerr_ignore*/ \
275             return 0;                                             \
276         }                                                         \
277     }
278
279 void evp_pkey_ctx_free_old_ops(EVP_PKEY_CTX *ctx);
280
281 /* OSSL_PROVIDER * is only used to get the library context */
282 const char *evp_first_name(const OSSL_PROVIDER *prov, int name_id);
283 int evp_is_a(OSSL_PROVIDER *prov, int number,
284              const char *legacy_name, const char *name);
285 void evp_names_do_all(OSSL_PROVIDER *prov, int number,
286                       void (*fn)(const char *name, void *data),
287                       void *data);
288 int evp_cipher_cache_constants(EVP_CIPHER *cipher);