]>
Commit | Line | Data |
---|---|---|
0f113f3e | 1 | /* |
33388b44 | 2 | * Copyright 2000-2020 The OpenSSL Project Authors. All Rights Reserved. |
5da2f69f | 3 | * |
4a8b0c55 | 4 | * Licensed under the Apache License 2.0 (the "License"). You may not use |
aa6bb135 RS |
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 | |
5da2f69f DSH |
8 | */ |
9 | ||
7638370c RL |
10 | /* EVP_MD_CTX related stuff */ |
11 | ||
23c48d94 | 12 | #include <openssl/core_dispatch.h> |
15dfa092 | 13 | #include "internal/refcount.h" |
ff64702b | 14 | |
e870791a SL |
15 | #define EVP_CTRL_RET_UNSUPPORTED -1 |
16 | ||
17 | ||
7638370c | 18 | struct evp_md_ctx_st { |
b7c913c8 | 19 | const EVP_MD *reqdigest; /* The original requested digest */ |
7638370c RL |
20 | const EVP_MD *digest; |
21 | ENGINE *engine; /* functional reference if 'digest' is | |
22 | * ENGINE-provided */ | |
23 | unsigned long flags; | |
24 | void *md_data; | |
25 | /* Public key context for sign/verify */ | |
26 | EVP_PKEY_CTX *pctx; | |
27 | /* Update function: usually copied from EVP_MD */ | |
28 | int (*update) (EVP_MD_CTX *ctx, const void *data, size_t count); | |
8c8cf0d9 MC |
29 | |
30 | /* Provider ctx */ | |
31 | void *provctx; | |
32 | EVP_MD *fetched_digest; | |
7638370c RL |
33 | } /* EVP_MD_CTX */ ; |
34 | ||
8baf9968 RL |
35 | struct evp_cipher_ctx_st { |
36 | const EVP_CIPHER *cipher; | |
37 | ENGINE *engine; /* functional reference if 'cipher' is | |
38 | * ENGINE-provided */ | |
39 | int encrypt; /* encrypt or decrypt */ | |
40 | int buf_len; /* number we have left */ | |
41 | unsigned char oiv[EVP_MAX_IV_LENGTH]; /* original iv */ | |
42 | unsigned char iv[EVP_MAX_IV_LENGTH]; /* working iv */ | |
43 | unsigned char buf[EVP_MAX_BLOCK_LENGTH]; /* saved partial block */ | |
44 | int num; /* used by cfb/ofb/ctr mode */ | |
45 | /* FIXME: Should this even exist? It appears unused */ | |
46 | void *app_data; /* application stuff */ | |
47 | int key_len; /* May change for variable length cipher */ | |
48 | unsigned long flags; /* Various flags */ | |
49 | void *cipher_data; /* per EVP data */ | |
50 | int final_used; | |
51 | int block_mask; | |
52 | unsigned char final[EVP_MAX_BLOCK_LENGTH]; /* possible final block */ | |
df05f2ce MC |
53 | |
54 | /* Provider ctx */ | |
55 | void *provctx; | |
56 | EVP_CIPHER *fetched_cipher; | |
8baf9968 RL |
57 | } /* EVP_CIPHER_CTX */ ; |
58 | ||
567db2c1 | 59 | struct evp_mac_ctx_st { |
e74bd290 | 60 | EVP_MAC *meth; /* Method structure */ |
567db2c1 RL |
61 | void *data; /* Individual method data */ |
62 | } /* EVP_MAC_CTX */; | |
63 | ||
5a285add | 64 | struct evp_kdf_ctx_st { |
fb9e6dd6 P |
65 | EVP_KDF *meth; /* Method structure */ |
66 | void *data; /* Algorithm-specific data */ | |
5a285add DM |
67 | } /* EVP_KDF_CTX */ ; |
68 | ||
15dfa092 P |
69 | struct evp_rand_ctx_st { |
70 | EVP_RAND *meth; /* Method structure */ | |
71 | void *data; /* Algorithm-specific data */ | |
15dfa092 P |
72 | } /* EVP_RAND_CTX */ ; |
73 | ||
a94a3e0d RL |
74 | struct evp_keymgmt_st { |
75 | int id; /* libcrypto internal */ | |
76 | ||
f7c16d48 | 77 | int name_id; |
a94a3e0d RL |
78 | OSSL_PROVIDER *prov; |
79 | CRYPTO_REF_COUNT refcnt; | |
80 | CRYPTO_RWLOCK *lock; | |
81 | ||
b305452f | 82 | /* Constructor(s), destructor, information */ |
363b1e5d DMSP |
83 | OSSL_FUNC_keymgmt_new_fn *new; |
84 | OSSL_FUNC_keymgmt_free_fn *free; | |
85 | OSSL_FUNC_keymgmt_get_params_fn *get_params; | |
86 | OSSL_FUNC_keymgmt_gettable_params_fn *gettable_params; | |
87 | OSSL_FUNC_keymgmt_set_params_fn *set_params; | |
88 | OSSL_FUNC_keymgmt_settable_params_fn *settable_params; | |
6508e858 | 89 | |
1a5632e0 | 90 | /* Generation, a complex constructor */ |
363b1e5d DMSP |
91 | OSSL_FUNC_keymgmt_gen_init_fn *gen_init; |
92 | OSSL_FUNC_keymgmt_gen_set_template_fn *gen_set_template; | |
93 | OSSL_FUNC_keymgmt_gen_set_params_fn *gen_set_params; | |
94 | OSSL_FUNC_keymgmt_gen_settable_params_fn *gen_settable_params; | |
95 | OSSL_FUNC_keymgmt_gen_fn *gen; | |
96 | OSSL_FUNC_keymgmt_gen_cleanup_fn *gen_cleanup; | |
1a5632e0 | 97 | |
5dacb38c RL |
98 | OSSL_FUNC_keymgmt_load_fn *load; |
99 | ||
b305452f | 100 | /* Key object checking */ |
363b1e5d DMSP |
101 | OSSL_FUNC_keymgmt_query_operation_name_fn *query_operation_name; |
102 | OSSL_FUNC_keymgmt_has_fn *has; | |
103 | OSSL_FUNC_keymgmt_validate_fn *validate; | |
104 | OSSL_FUNC_keymgmt_match_fn *match; | |
b305452f RL |
105 | |
106 | /* Import and export routines */ | |
363b1e5d DMSP |
107 | OSSL_FUNC_keymgmt_import_fn *import; |
108 | OSSL_FUNC_keymgmt_import_types_fn *import_types; | |
109 | OSSL_FUNC_keymgmt_export_fn *export; | |
110 | OSSL_FUNC_keymgmt_export_types_fn *export_types; | |
111 | OSSL_FUNC_keymgmt_copy_fn *copy; | |
a94a3e0d RL |
112 | } /* EVP_KEYMGMT */ ; |
113 | ||
ff64702b | 114 | struct evp_keyexch_st { |
f7c16d48 | 115 | int name_id; |
ff64702b MC |
116 | OSSL_PROVIDER *prov; |
117 | CRYPTO_REF_COUNT refcnt; | |
118 | CRYPTO_RWLOCK *lock; | |
119 | ||
363b1e5d DMSP |
120 | OSSL_FUNC_keyexch_newctx_fn *newctx; |
121 | OSSL_FUNC_keyexch_init_fn *init; | |
122 | OSSL_FUNC_keyexch_set_peer_fn *set_peer; | |
123 | OSSL_FUNC_keyexch_derive_fn *derive; | |
124 | OSSL_FUNC_keyexch_freectx_fn *freectx; | |
125 | OSSL_FUNC_keyexch_dupctx_fn *dupctx; | |
126 | OSSL_FUNC_keyexch_set_ctx_params_fn *set_ctx_params; | |
127 | OSSL_FUNC_keyexch_settable_ctx_params_fn *settable_ctx_params; | |
128 | OSSL_FUNC_keyexch_get_ctx_params_fn *get_ctx_params; | |
129 | OSSL_FUNC_keyexch_gettable_ctx_params_fn *gettable_ctx_params; | |
ff64702b MC |
130 | } /* EVP_KEYEXCH */; |
131 | ||
dfcb5d29 | 132 | struct evp_signature_st { |
f7c16d48 | 133 | int name_id; |
dfcb5d29 MC |
134 | OSSL_PROVIDER *prov; |
135 | CRYPTO_REF_COUNT refcnt; | |
136 | CRYPTO_RWLOCK *lock; | |
137 | ||
363b1e5d DMSP |
138 | OSSL_FUNC_signature_newctx_fn *newctx; |
139 | OSSL_FUNC_signature_sign_init_fn *sign_init; | |
140 | OSSL_FUNC_signature_sign_fn *sign; | |
141 | OSSL_FUNC_signature_verify_init_fn *verify_init; | |
142 | OSSL_FUNC_signature_verify_fn *verify; | |
143 | OSSL_FUNC_signature_verify_recover_init_fn *verify_recover_init; | |
144 | OSSL_FUNC_signature_verify_recover_fn *verify_recover; | |
145 | OSSL_FUNC_signature_digest_sign_init_fn *digest_sign_init; | |
146 | OSSL_FUNC_signature_digest_sign_update_fn *digest_sign_update; | |
147 | OSSL_FUNC_signature_digest_sign_final_fn *digest_sign_final; | |
148 | OSSL_FUNC_signature_digest_sign_fn *digest_sign; | |
149 | OSSL_FUNC_signature_digest_verify_init_fn *digest_verify_init; | |
150 | OSSL_FUNC_signature_digest_verify_update_fn *digest_verify_update; | |
151 | OSSL_FUNC_signature_digest_verify_final_fn *digest_verify_final; | |
152 | OSSL_FUNC_signature_digest_verify_fn *digest_verify; | |
153 | OSSL_FUNC_signature_freectx_fn *freectx; | |
154 | OSSL_FUNC_signature_dupctx_fn *dupctx; | |
155 | OSSL_FUNC_signature_get_ctx_params_fn *get_ctx_params; | |
156 | OSSL_FUNC_signature_gettable_ctx_params_fn *gettable_ctx_params; | |
157 | OSSL_FUNC_signature_set_ctx_params_fn *set_ctx_params; | |
158 | OSSL_FUNC_signature_settable_ctx_params_fn *settable_ctx_params; | |
159 | OSSL_FUNC_signature_get_ctx_md_params_fn *get_ctx_md_params; | |
160 | OSSL_FUNC_signature_gettable_ctx_md_params_fn *gettable_ctx_md_params; | |
161 | OSSL_FUNC_signature_set_ctx_md_params_fn *set_ctx_md_params; | |
162 | OSSL_FUNC_signature_settable_ctx_md_params_fn *settable_ctx_md_params; | |
dfcb5d29 MC |
163 | } /* EVP_SIGNATURE */; |
164 | ||
2c938e2e MC |
165 | struct evp_asym_cipher_st { |
166 | int name_id; | |
167 | OSSL_PROVIDER *prov; | |
168 | CRYPTO_REF_COUNT refcnt; | |
169 | CRYPTO_RWLOCK *lock; | |
170 | ||
363b1e5d DMSP |
171 | OSSL_FUNC_asym_cipher_newctx_fn *newctx; |
172 | OSSL_FUNC_asym_cipher_encrypt_init_fn *encrypt_init; | |
173 | OSSL_FUNC_asym_cipher_encrypt_fn *encrypt; | |
174 | OSSL_FUNC_asym_cipher_decrypt_init_fn *decrypt_init; | |
175 | OSSL_FUNC_asym_cipher_decrypt_fn *decrypt; | |
176 | OSSL_FUNC_asym_cipher_freectx_fn *freectx; | |
177 | OSSL_FUNC_asym_cipher_dupctx_fn *dupctx; | |
178 | OSSL_FUNC_asym_cipher_get_ctx_params_fn *get_ctx_params; | |
179 | OSSL_FUNC_asym_cipher_gettable_ctx_params_fn *gettable_ctx_params; | |
180 | OSSL_FUNC_asym_cipher_set_ctx_params_fn *set_ctx_params; | |
181 | OSSL_FUNC_asym_cipher_settable_ctx_params_fn *settable_ctx_params; | |
2c938e2e MC |
182 | } /* EVP_ASYM_CIPHER */; |
183 | ||
0f113f3e MC |
184 | int PKCS5_v2_PBKDF2_keyivgen(EVP_CIPHER_CTX *ctx, const char *pass, |
185 | int passlen, ASN1_TYPE *param, | |
186 | const EVP_CIPHER *c, const EVP_MD *md, | |
187 | int en_de); | |
a0be4fd1 RL |
188 | |
189 | struct evp_Encode_Ctx_st { | |
190 | /* number saved in a partial encode/decode */ | |
191 | int num; | |
192 | /* | |
193 | * The length is either the output line length (in input bytes) or the | |
194 | * shortest input line length that is ok. Once decoding begins, the | |
195 | * length is adjusted up each time a longer line is decoded | |
196 | */ | |
197 | int length; | |
198 | /* data to encode */ | |
199 | unsigned char enc_data[80]; | |
200 | /* number read on current line */ | |
201 | int line_num; | |
c0804614 | 202 | unsigned int flags; |
a0be4fd1 | 203 | }; |
4a1f3f27 DSH |
204 | |
205 | typedef struct evp_pbe_st EVP_PBE_CTL; | |
206 | DEFINE_STACK_OF(EVP_PBE_CTL) | |
7141ba31 MC |
207 | |
208 | int is_partially_overlapping(const void *ptr1, const void *ptr2, int len); | |
c13d2ab4 | 209 | |
50cd4768 | 210 | #include <openssl/types.h> |
c13d2ab4 RL |
211 | #include <openssl/core.h> |
212 | ||
213 | void *evp_generic_fetch(OPENSSL_CTX *ctx, int operation_id, | |
f7c16d48 RL |
214 | const char *name, const char *properties, |
215 | void *(*new_method)(int name_id, | |
6b9e3724 | 216 | const OSSL_DISPATCH *fns, |
0ddf74bf | 217 | OSSL_PROVIDER *prov), |
7c95390e | 218 | int (*up_ref_method)(void *), |
0211740f | 219 | void (*free_method)(void *)); |
f7c16d48 RL |
220 | void *evp_generic_fetch_by_number(OPENSSL_CTX *ctx, int operation_id, |
221 | int name_id, const char *properties, | |
222 | void *(*new_method)(int name_id, | |
223 | const OSSL_DISPATCH *fns, | |
0ddf74bf | 224 | OSSL_PROVIDER *prov), |
f7c16d48 RL |
225 | int (*up_ref_method)(void *), |
226 | void (*free_method)(void *)); | |
3d96a51c RL |
227 | void evp_generic_do_all(OPENSSL_CTX *libctx, int operation_id, |
228 | void (*user_fn)(void *method, void *arg), | |
229 | void *user_arg, | |
f7c16d48 | 230 | void *(*new_method)(int name_id, |
3d96a51c | 231 | const OSSL_DISPATCH *fns, |
0ddf74bf | 232 | OSSL_PROVIDER *prov), |
3d96a51c | 233 | void (*free_method)(void *)); |
13273237 | 234 | |
f7c16d48 RL |
235 | /* Internal fetchers for method types that are to be combined with others */ |
236 | EVP_KEYMGMT *evp_keymgmt_fetch_by_number(OPENSSL_CTX *ctx, int name_id, | |
237 | const char *properties); | |
238 | ||
3fd70262 RL |
239 | /* Internal structure constructors for fetched methods */ |
240 | EVP_MD *evp_md_new(void); | |
550f974a | 241 | EVP_CIPHER *evp_cipher_new(void); |
3fd70262 | 242 | |
13273237 RL |
243 | /* Helper functions to avoid duplicating code */ |
244 | ||
245 | /* | |
459b15d4 | 246 | * These methods implement different ways to pass a params array to the |
13273237 RL |
247 | * provider. They will return one of these values: |
248 | * | |
249 | * -2 if the method doesn't come from a provider | |
250 | * (evp_do_param will return this to the called) | |
251 | * -1 if the provider doesn't offer the desired function | |
252 | * (evp_do_param will raise an error and return 0) | |
253 | * or the return value from the desired function | |
254 | * (evp_do_param will return it to the caller) | |
255 | */ | |
459b15d4 SL |
256 | int evp_do_ciph_getparams(const EVP_CIPHER *ciph, OSSL_PARAM params[]); |
257 | int evp_do_ciph_ctx_getparams(const EVP_CIPHER *ciph, void *provctx, | |
13273237 | 258 | OSSL_PARAM params[]); |
459b15d4 | 259 | int evp_do_ciph_ctx_setparams(const EVP_CIPHER *ciph, void *provctx, |
13273237 | 260 | OSSL_PARAM params[]); |
6a3b7c68 RL |
261 | int evp_do_md_getparams(const EVP_MD *md, OSSL_PARAM params[]); |
262 | int evp_do_md_ctx_getparams(const EVP_MD *md, void *provctx, | |
263 | OSSL_PARAM params[]); | |
264 | int evp_do_md_ctx_setparams(const EVP_MD *md, void *provctx, | |
265 | OSSL_PARAM params[]); | |
ff64702b MC |
266 | |
267 | OSSL_PARAM *evp_pkey_to_param(EVP_PKEY *pkey, size_t *sz); | |
268 | ||
269 | #define M_check_autoarg(ctx, arg, arglen, err) \ | |
270 | if (ctx->pmeth->flags & EVP_PKEY_FLAG_AUTOARGLEN) { \ | |
271 | size_t pksize = (size_t)EVP_PKEY_size(ctx->pkey); \ | |
272 | \ | |
273 | if (pksize == 0) { \ | |
51ba9ebd | 274 | ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_KEY); /*ckerr_ignore*/ \ |
ff64702b MC |
275 | return 0; \ |
276 | } \ | |
277 | if (arg == NULL) { \ | |
278 | *arglen = pksize; \ | |
279 | return 1; \ | |
280 | } \ | |
281 | if (*arglen < pksize) { \ | |
51ba9ebd | 282 | ERR_raise(ERR_LIB_EVP, EVP_R_BUFFER_TOO_SMALL); /*ckerr_ignore*/ \ |
ff64702b MC |
283 | return 0; \ |
284 | } \ | |
285 | } | |
864b89ce MC |
286 | |
287 | void evp_pkey_ctx_free_old_ops(EVP_PKEY_CTX *ctx); | |
f7c16d48 RL |
288 | |
289 | /* OSSL_PROVIDER * is only used to get the library context */ | |
3f7ce7f1 | 290 | const char *evp_first_name(const OSSL_PROVIDER *prov, int name_id); |
e4a1d023 RL |
291 | int evp_is_a(OSSL_PROVIDER *prov, int number, |
292 | const char *legacy_name, const char *name); | |
f651c727 RL |
293 | void evp_names_do_all(OSSL_PROVIDER *prov, int number, |
294 | void (*fn)(const char *name, void *data), | |
295 | void *data); | |
3c957bcd | 296 | int evp_cipher_cache_constants(EVP_CIPHER *cipher); |