-
Notifications
You must be signed in to change notification settings - Fork 35
Expand file tree
/
Copy pathinternal.h
More file actions
280 lines (244 loc) · 8.63 KB
/
Copy pathinternal.h
File metadata and controls
280 lines (244 loc) · 8.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
/* internal.h
*
* Copyright (C) 2006-2025 wolfSSL Inc.
*
* This file is part of wolfProvider.
*
* wolfProvider is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* wolfProvider is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with wolfProvider. If not, see <http://www.gnu.org/licenses/>.
*/
#include <openssl/core.h>
#include <openssl/core_names.h>
#include <openssl/types.h>
#include <openssl/bio.h>
#ifdef WOLFENGINE_USER_SETTINGS
#include "user_settings.h"
#endif
#include <wolfssl/options.h>
#include <wolfssl/version.h>
#include <wolfssl/wolfcrypt/random.h>
#include <wolfssl/wolfcrypt/hash.h>
#include <wolfssl/wolfcrypt/integer.h>
#include <wolfssl/wolfcrypt/error-crypt.h>
#include <wolfssl/wolfcrypt/wc_encrypt.h>
#include <wolfssl/wolfcrypt/coding.h>
#include <wolfssl/wolfcrypt/asn_public.h>
#include "wp_params.h"
#ifndef AES_BLOCK_SIZE
#define AES_BLOCK_SIZE 16
#endif
#ifndef WP_INTERNAL_H
#define WP_INTERNAL_H
/** Maximum value of a size_t. */
#define MAX_SIZE_T ((size_t)-1)
/** Maximum supported digest name size. */
#define WP_MAX_MD_NAME_SIZE 15
/** Maximum supported cipher name size. */
#define WP_MAX_CIPH_NAME_SIZE 15
/** Maximum supported cipher name size. */
#define WP_MAX_MAC_NAME_SIZE 6
/** Maximum supported digest name size. */
#define WP_MAX_PROPS_SIZE 80
/* DER key encoding/decoding formats. */
/** SubjectPublicKeyInfo encoding format. */
#define WP_ENC_FORMAT_SPKI 1
/** PrivateKeyInfo encoding format. */
#define WP_ENC_FORMAT_PKI 2
/** EncryptedPrivateKeyInfo encoding format. */
#define WP_ENC_FORMAT_EPKI 3
/** Type-specific encoding format. */
#define WP_ENC_FORMAT_TYPE_SPECIFIC 4
/** X9_62 encoding format. */
#define WP_ENC_FORMAT_X9_62 5
/** Text encoding format. */
#define WP_ENC_FORMAT_TEXT 6
/* Data format. */
/** DER - Binary encoding. */
#define WP_FORMAT_DER 0
/** PEM - Text encoding. */
#define WP_FORMAT_PEM 1
/** Human readable text encoding. */
#define WP_FORMAT_TEXT 2
/* MAC key types. */
/** HMAC key type. */
#define WP_MAC_TYPE_HMAC 1
/** CMAC key type. */
#define WP_MAC_TYPE_CMAC 2
/** PKCS5 encoding */
#define WP_PKCS5 5
/** PBES2 encoding */
#define WP_PBES2 13
/** Default iterations for PKCS#12 PBKDF2. */
#define WP_PKCS12_ITERATIONS_DEFAULT 2048
/** Maximum salt length for PKCS. */
#define WP_MAX_SALT_SIZE 64
/** Default salt length for PSS. */
#define WP_RSA_DEFAULT_SALT_LEN 20
/* These values are taken from ssl.h.
* Can't include this header as it redeclares OpenSSL types.
*/
/* Named Groups */
enum {
WOLFSSL_NAMED_GROUP_INVALID = 0,
WOLFSSL_ECC_SECP160K1 = 15,
WOLFSSL_ECC_SECP160R1 = 16,
WOLFSSL_ECC_SECP160R2 = 17,
WOLFSSL_ECC_SECP192K1 = 18,
WOLFSSL_ECC_SECP192R1 = 19,
WOLFSSL_ECC_SECP224K1 = 20,
WOLFSSL_ECC_SECP224R1 = 21,
WOLFSSL_ECC_SECP256K1 = 22,
WOLFSSL_ECC_SECP256R1 = 23,
WOLFSSL_ECC_SECP384R1 = 24,
WOLFSSL_ECC_SECP521R1 = 25,
WOLFSSL_ECC_BRAINPOOLP256R1 = 26,
WOLFSSL_ECC_BRAINPOOLP384R1 = 27,
WOLFSSL_ECC_BRAINPOOLP512R1 = 28,
WOLFSSL_ECC_X25519 = 29,
WOLFSSL_ECC_X448 = 30,
WOLFSSL_ECC_MAX = 30,
WOLFSSL_FFDHE_2048 = 256,
WOLFSSL_FFDHE_3072 = 257,
WOLFSSL_FFDHE_4096 = 258,
WOLFSSL_FFDHE_6144 = 259,
WOLFSSL_FFDHE_8192 = 260
};
/**
* wolfSSL provider context.
*/
typedef struct WOLFPROV_CTX {
/** Cached handle to a resource. */
const OSSL_CORE_HANDLE *handle;
/** Library context to use in all cases. */
OSSL_LIB_CTX *libCtx;
/** Random number generator. */
WC_RNG rng;
#ifndef WP_SINGLE_THREADED
/** Mutex for use of random number generator. */
wolfSSL_Mutex rng_mutex;
#endif
BIO_METHOD *coreBioMethod;
} WOLFPROV_CTX;
#if defined(WP_HAVE_SEED_SRC) && defined(WP_HAVE_RANDOM)
/*
* Global /dev/urandom subsystem functions.
*
* These manage a cached file handle to /dev/urandom that is opened lazily
* on first entropy request (matching OpenSSL's default provider behavior).
* The file stays open so child processes inherit it and can read even
* in sandboxed environments that block openat().
*/
int wp_urandom_init(void);
void wp_urandom_cleanup(void);
int wp_urandom_read(unsigned char* buf, size_t len);
#ifndef WP_SINGLE_THREADED
wolfSSL_Mutex *wp_get_urandom_mutex(void);
#endif
#endif /* WP_HAVE_SEED_SRC */
WC_RNG* wp_provctx_get_rng(WOLFPROV_CTX* provCtx);
#ifndef WP_SINGLE_THREADED
int wp_provctx_lock_rng(WOLFPROV_CTX* provCtx);
void wp_provctx_unlock_rng(WOLFPROV_CTX* provCtx);
#ifdef HAVE_FIPS
/* CAST self-test algorithm categories */
#define WP_CAST_ALGO_AES 0
#define WP_CAST_ALGO_HMAC 1
#define WP_CAST_ALGO_DRBG 2
#define WP_CAST_ALGO_RSA 3
#define WP_CAST_ALGO_ECDSA 4
#define WP_CAST_ALGO_ECDH 5
#define WP_CAST_ALGO_DH 6
#define WP_CAST_ALGO_COUNT 7
int wp_init_cast(int algo);
/**
* Check FIPS CAST for algorithm. Returns 0 on failure.
* Use at function entry points that return int (1=success, 0=failure).
*/
#define WP_CHECK_FIPS_ALGO(algo) \
do { \
if (wp_init_cast(algo) != 1) { \
return 0; \
} \
} while (0)
/**
* Check FIPS CAST for algorithm. Returns NULL on failure.
* Use at function entry points that return pointers (NULL=failure).
*/
#define WP_CHECK_FIPS_ALGO_PTR(algo) \
do { \
if (wp_init_cast(algo) != 1) { \
return NULL; \
} \
} while (0)
#else
/* Non-FIPS: no-op */
#define WP_CHECK_FIPS_ALGO(algo) do { } while (0)
#define WP_CHECK_FIPS_ALGO_PTR(algo) do { } while (0)
#endif
#endif
int wolfssl_prov_get_capabilities(void *provctx, const char *capability,
OSSL_CALLBACK *cb, void *arg);
int wp_name_to_nid(OSSL_LIB_CTX* libCtx, const char* name, const char* propQ);
enum wc_HashType wp_name_to_wc_hash_type(OSSL_LIB_CTX* libCtx, const char* name,
const char* propQ);
enum wc_HashType wp_nid_to_wc_hash_type(int nid);
int wp_name_to_wc_mgf(OSSL_LIB_CTX* libCtx, const char* name,
const char* propQ);
int wp_mgf1_from_hash(int nid);
#if LIBWOLFSSL_VERSION_HEX >= 0x05007004
int wp_hash_copy(wc_HashAlg* src, wc_HashAlg* dst);
#else
int wp_hash_copy(wc_HashAlg* src, wc_HashAlg* dst, enum wc_HashType hashType);
#endif
int wp_cipher_from_params(const OSSL_PARAM params[], int* cipher,
const char** cipherName);
int wp_encrypt_key_pkcs8_size(WOLFPROV_CTX* provCtx, int cipher,
word32 plainLen, size_t* outLen);
int wp_encrypt_key_pkcs8(WOLFPROV_CTX* provCtx, int cipher,
const unsigned char* plain, word32 plainLen,
unsigned char* out, size_t* outLen,
OSSL_PASSPHRASE_CALLBACK* pwCb, void* pwCbArg);
int wp_decrypt_key_pkcs8(unsigned char* data, word32* len,
OSSL_PASSPHRASE_CALLBACK* pwCb, void* pwCbArg);
int wp_read_der_bio(WOLFPROV_CTX* provCtx, OSSL_CORE_BIO *coreBio, unsigned char** data, word32* len);
int wp_read_pem_bio(WOLFPROV_CTX *provctx, OSSL_CORE_BIO *coreBio,
unsigned char** data, word32* len);
BIO* wp_corebio_get_bio(WOLFPROV_CTX* provCtx, OSSL_CORE_BIO *coreBio);
#ifdef HAVE_FIPS
/**
* Decide whether a decoder should skip key-object instantiation (FIPS only).
*
* Cold-CAST optimization: if the wrapped key's (SPKI/PKCS#8) AlgorithmIdentifier
* OID is recognized but not in allowedNids, return 1 so the caller bails before
* instantiating a key (which would fire the lazy CAST). The OID is read from the
* DER directly, so the decoder's format label is not needed.
*
* @param [in] castType FIPS CAST id for this decoder's algorithm.
* @param [in] der DER-encoded key bytes.
* @param [in] len Length of der in bytes.
* @param [in] allowedNids NIDs owned by this decoder.
* @param [in] nAllowed Number of entries in allowedNids.
* @return 1 to skip instantiation, 0 to proceed.
*/
int wp_decode_should_skip(int castType, const unsigned char* der, word32 len,
const int* allowedNids, size_t nAllowed);
#endif /* HAVE_FIPS */
byte wp_ct_byte_mask_eq(byte a, byte b);
byte wp_ct_byte_mask_ne(byte a, byte b);
byte wp_ct_int_mask_gte(int a, int b);
byte wp_ct_int_mask_eq(int a, int b);
byte wp_ct_int_mask_lt(int a, int b);
byte wp_ct_byte_mask_sel(byte mask, byte a, byte b);
void wp_c32toa(word32 wc_u32, byte* c);
word32 wp_atoc32(const byte* c);
#endif /* WP_INTERNAL_H */