/src/njs/external/njs_openssl.h
Line | Count | Source |
1 | | |
2 | | /* |
3 | | * Copyright (C) Dmitry Volyntsev |
4 | | * Copyright (C) NGINX, Inc. |
5 | | */ |
6 | | |
7 | | |
8 | | #ifndef _NJS_EXTERNAL_OPENSSL_H_INCLUDED_ |
9 | | #define _NJS_EXTERNAL_OPENSSL_H_INCLUDED_ |
10 | | |
11 | | |
12 | | #define OPENSSL_SUPPRESS_DEPRECATED |
13 | | |
14 | | #include <openssl/bn.h> |
15 | | #include <openssl/bio.h> |
16 | | #include <openssl/x509.h> |
17 | | #include <openssl/evp.h> |
18 | | #include <openssl/aes.h> |
19 | | #include <openssl/rsa.h> |
20 | | #include <openssl/err.h> |
21 | | #include <openssl/rand.h> |
22 | | #include <openssl/hmac.h> |
23 | | #include <openssl/crypto.h> |
24 | | |
25 | | #ifdef EVP_PKEY_HKDF |
26 | | #include <openssl/kdf.h> |
27 | | #endif |
28 | | |
29 | | |
30 | | #if (defined LIBRESSL_VERSION_NUMBER && OPENSSL_VERSION_NUMBER == 0x20000000L) |
31 | | #undef OPENSSL_VERSION_NUMBER |
32 | | #if (LIBRESSL_VERSION_NUMBER >= 0x2080000fL) |
33 | | #define OPENSSL_VERSION_NUMBER 0x1010000fL |
34 | | #else |
35 | | #define OPENSSL_VERSION_NUMBER 0x1000107fL |
36 | | #endif |
37 | | #endif |
38 | | |
39 | | |
40 | | #if (OPENSSL_VERSION_NUMBER >= 0x10002000L \ |
41 | | && (!defined LIBRESSL_VERSION_NUMBER \ |
42 | | || LIBRESSL_VERSION_NUMBER >= 0x3010000fL)) |
43 | | #define NJS_OPENSSL_HAS_RSA_OAEP_MD 1 |
44 | | #else |
45 | | #define NJS_OPENSSL_HAS_RSA_OAEP_MD 0 |
46 | | #endif |
47 | | |
48 | | |
49 | | #if (OPENSSL_VERSION_NUMBER >= 0x10100000L) |
50 | 0 | #define njs_evp_md_ctx_new() EVP_MD_CTX_new() |
51 | 0 | #define njs_evp_md_ctx_free(_ctx) EVP_MD_CTX_free(_ctx) |
52 | | #else |
53 | | #define njs_evp_md_ctx_new() EVP_MD_CTX_create() |
54 | | #define njs_evp_md_ctx_free(_ctx) EVP_MD_CTX_destroy(_ctx) |
55 | | #endif |
56 | | |
57 | | |
58 | | #if (OPENSSL_VERSION_NUMBER >= 0x10100000L) |
59 | 0 | #define njs_hmac_ctx_new() HMAC_CTX_new() |
60 | 0 | #define njs_hmac_ctx_free(_ctx) HMAC_CTX_free(_ctx) |
61 | | #else |
62 | | |
63 | | njs_inline HMAC_CTX * |
64 | | njs_hmac_ctx_new(void) |
65 | | { |
66 | | HMAC_CTX *ctx; |
67 | | |
68 | | ctx = OPENSSL_malloc(sizeof(HMAC_CTX)); |
69 | | if (ctx != NULL) { |
70 | | HMAC_CTX_init(ctx); |
71 | | } |
72 | | |
73 | | return ctx; |
74 | | } |
75 | | |
76 | | |
77 | | njs_inline void |
78 | | njs_hmac_ctx_free(HMAC_CTX *ctx) |
79 | | { |
80 | | if (ctx != NULL) { |
81 | | HMAC_CTX_cleanup(ctx); |
82 | | OPENSSL_free(ctx); |
83 | | } |
84 | | } |
85 | | |
86 | | #endif |
87 | | |
88 | | |
89 | 0 | #define njs_bio_new_mem_buf(b, len) BIO_new_mem_buf((void *) b, len) |
90 | | |
91 | | |
92 | | #if (OPENSSL_VERSION_NUMBER < 0x30000000L && !defined ERR_peek_error_data) |
93 | 0 | #define ERR_peek_error_data(d, f) ERR_peek_error_line_data(NULL, NULL, d, f) |
94 | | #endif |
95 | | |
96 | | |
97 | | njs_inline int |
98 | | njs_bn_bn2binpad(const BIGNUM *bn, unsigned char *to, int tolen) |
99 | 0 | { |
100 | 0 | #if (OPENSSL_VERSION_NUMBER >= 0x10100000L) |
101 | 0 | return BN_bn2binpad(bn, to, tolen); |
102 | | #else |
103 | | int len; |
104 | | |
105 | | len = BN_num_bytes(bn); |
106 | | |
107 | | if (tolen > len) { |
108 | | memset(to, 0, tolen - len); |
109 | | |
110 | | } else if (tolen < len) { |
111 | | return -1; |
112 | | } |
113 | | |
114 | | return BN_bn2bin(bn, &to[tolen - len]); |
115 | | #endif |
116 | 0 | } Unexecuted instantiation: njs_crypto_module.c:njs_bn_bn2binpad Unexecuted instantiation: njs_webcrypto_module.c:njs_bn_bn2binpad |
117 | | |
118 | | |
119 | | njs_inline int |
120 | | njs_pkey_up_ref(EVP_PKEY *pkey) |
121 | 0 | { |
122 | 0 | #if (OPENSSL_VERSION_NUMBER >= 0x10100000L) |
123 | 0 | return EVP_PKEY_up_ref(pkey); |
124 | | #else |
125 | | CRYPTO_add(&pkey->references, 1, CRYPTO_LOCK_EVP_PKEY); |
126 | | return 1; |
127 | | #endif |
128 | 0 | } Unexecuted instantiation: njs_crypto_module.c:njs_pkey_up_ref Unexecuted instantiation: njs_webcrypto_module.c:njs_pkey_up_ref |
129 | | |
130 | | |
131 | | njs_inline const RSA * |
132 | | njs_pkey_get_rsa_key(EVP_PKEY *pkey) |
133 | 0 | { |
134 | 0 | #if (OPENSSL_VERSION_NUMBER >= 0x10100000L) |
135 | 0 | return EVP_PKEY_get0_RSA(pkey); |
136 | | #else |
137 | | return EVP_PKEY_get0(pkey); |
138 | | #endif |
139 | 0 | } Unexecuted instantiation: njs_crypto_module.c:njs_pkey_get_rsa_key Unexecuted instantiation: njs_webcrypto_module.c:njs_pkey_get_rsa_key |
140 | | |
141 | | |
142 | | njs_inline void |
143 | | njs_rsa_get0_key(const RSA *rsa, const BIGNUM **n, const BIGNUM **e, |
144 | | const BIGNUM **d) |
145 | 0 | { |
146 | 0 | #if (OPENSSL_VERSION_NUMBER >= 0x10100000L) |
147 | 0 | RSA_get0_key(rsa, n, e, d); |
148 | | #else |
149 | | if (n != NULL) { |
150 | | *n = rsa->n; |
151 | | } |
152 | | |
153 | | if (e != NULL) { |
154 | | *e = rsa->e; |
155 | | } |
156 | | |
157 | | if (d != NULL) { |
158 | | *d = rsa->d; |
159 | | } |
160 | | #endif |
161 | 0 | } Unexecuted instantiation: njs_crypto_module.c:njs_rsa_get0_key Unexecuted instantiation: njs_webcrypto_module.c:njs_rsa_get0_key |
162 | | |
163 | | |
164 | | njs_inline void |
165 | | njs_rsa_get0_factors(const RSA *rsa, const BIGNUM **p, const BIGNUM **q) |
166 | 0 | { |
167 | 0 | #if (OPENSSL_VERSION_NUMBER >= 0x10100000L) |
168 | 0 | RSA_get0_factors(rsa, p, q); |
169 | | #else |
170 | | if (p != NULL) { |
171 | | *p = rsa->p; |
172 | | } |
173 | | |
174 | | if (q != NULL) { |
175 | | *q = rsa->q; |
176 | | } |
177 | | #endif |
178 | 0 | } Unexecuted instantiation: njs_crypto_module.c:njs_rsa_get0_factors Unexecuted instantiation: njs_webcrypto_module.c:njs_rsa_get0_factors |
179 | | |
180 | | |
181 | | |
182 | | njs_inline void |
183 | | njs_rsa_get0_ctr_params(const RSA *rsa, const BIGNUM **dp, const BIGNUM **dq, |
184 | | const BIGNUM **qi) |
185 | 0 | { |
186 | 0 | #if (OPENSSL_VERSION_NUMBER >= 0x10100000L) |
187 | 0 | RSA_get0_crt_params(rsa, dp, dq, qi); |
188 | | #else |
189 | | if (dp != NULL) { |
190 | | *dp = rsa->dmp1; |
191 | | } |
192 | | |
193 | | if (dq != NULL) { |
194 | | *dq = rsa->dmq1; |
195 | | } |
196 | | |
197 | | if (qi != NULL) { |
198 | | *qi = rsa->iqmp; |
199 | | } |
200 | | #endif |
201 | 0 | } Unexecuted instantiation: njs_crypto_module.c:njs_rsa_get0_ctr_params Unexecuted instantiation: njs_webcrypto_module.c:njs_rsa_get0_ctr_params |
202 | | |
203 | | |
204 | | njs_inline int |
205 | | njs_rsa_set0_key(RSA *rsa, BIGNUM *n, BIGNUM *e, BIGNUM *d) |
206 | 0 | { |
207 | 0 | #if (OPENSSL_VERSION_NUMBER >= 0x10100000L) |
208 | 0 | return RSA_set0_key(rsa, n, e, d); |
209 | | #else |
210 | | if ((rsa->n == NULL && n == NULL) || (rsa->e == NULL && e == NULL)) { |
211 | | return 0; |
212 | | } |
213 | | |
214 | | if (n != NULL) { |
215 | | BN_free(rsa->n); |
216 | | rsa->n = n; |
217 | | } |
218 | | |
219 | | if (e != NULL) { |
220 | | BN_free(rsa->e); |
221 | | rsa->e = e; |
222 | | } |
223 | | |
224 | | if (d != NULL) { |
225 | | BN_clear_free(rsa->d); |
226 | | rsa->d = d; |
227 | | BN_set_flags(rsa->d, BN_FLG_CONSTTIME); |
228 | | } |
229 | | |
230 | | return 1; |
231 | | #endif |
232 | 0 | } Unexecuted instantiation: njs_crypto_module.c:njs_rsa_set0_key Unexecuted instantiation: njs_webcrypto_module.c:njs_rsa_set0_key |
233 | | |
234 | | |
235 | | njs_inline int |
236 | | njs_rsa_set0_factors(RSA *rsa, BIGNUM *p, BIGNUM *q) |
237 | 0 | { |
238 | 0 | #if (OPENSSL_VERSION_NUMBER >= 0x10100000L) |
239 | 0 | return RSA_set0_factors(rsa, p, q); |
240 | | #else |
241 | | if ((rsa->p == NULL && p == NULL) || (rsa->q == NULL && q == NULL)) { |
242 | | return 0; |
243 | | } |
244 | | |
245 | | if (p != NULL) { |
246 | | BN_clear_free(rsa->p); |
247 | | rsa->p = p; |
248 | | BN_set_flags(rsa->p, BN_FLG_CONSTTIME); |
249 | | } |
250 | | |
251 | | if (q != NULL) { |
252 | | BN_clear_free(rsa->q); |
253 | | rsa->q = q; |
254 | | BN_set_flags(rsa->q, BN_FLG_CONSTTIME); |
255 | | } |
256 | | |
257 | | return 1; |
258 | | #endif |
259 | 0 | } Unexecuted instantiation: njs_crypto_module.c:njs_rsa_set0_factors Unexecuted instantiation: njs_webcrypto_module.c:njs_rsa_set0_factors |
260 | | |
261 | | |
262 | | njs_inline int |
263 | | njs_rsa_set0_ctr_params(RSA *rsa, BIGNUM *dp, BIGNUM *dq, BIGNUM *qi) |
264 | 0 | { |
265 | 0 | #if (OPENSSL_VERSION_NUMBER >= 0x10100000L) |
266 | 0 | return RSA_set0_crt_params(rsa, dp, dq, qi); |
267 | | #else |
268 | | if ((rsa->dmp1 == NULL && dp == NULL) |
269 | | || (rsa->dmq1 == NULL && dq == NULL) |
270 | | || (rsa->iqmp == NULL && qi == NULL)) |
271 | | { |
272 | | return 0; |
273 | | } |
274 | | |
275 | | if (dp != NULL) { |
276 | | BN_clear_free(rsa->dmp1); |
277 | | rsa->dmp1 = dp; |
278 | | BN_set_flags(rsa->dmp1, BN_FLG_CONSTTIME); |
279 | | } |
280 | | |
281 | | if (dq != NULL) { |
282 | | BN_clear_free(rsa->dmq1); |
283 | | rsa->dmq1 = dq; |
284 | | BN_set_flags(rsa->dmq1, BN_FLG_CONSTTIME); |
285 | | } |
286 | | |
287 | | if (qi != NULL) { |
288 | | BN_clear_free(rsa->iqmp); |
289 | | rsa->iqmp = qi; |
290 | | BN_set_flags(rsa->iqmp, BN_FLG_CONSTTIME); |
291 | | } |
292 | | |
293 | | return 1; |
294 | | #endif |
295 | 0 | } Unexecuted instantiation: njs_crypto_module.c:njs_rsa_set0_ctr_params Unexecuted instantiation: njs_webcrypto_module.c:njs_rsa_set0_ctr_params |
296 | | |
297 | | |
298 | | njs_inline const EC_KEY * |
299 | | njs_pkey_get_ec_key(EVP_PKEY *pkey) |
300 | 0 | { |
301 | 0 | #if (OPENSSL_VERSION_NUMBER >= 0x10100000L) |
302 | 0 | return EVP_PKEY_get0_EC_KEY(pkey); |
303 | | #else |
304 | | if (pkey->type != EVP_PKEY_EC) { |
305 | | return NULL; |
306 | | } |
307 | | |
308 | | return pkey->pkey.ec; |
309 | | #endif |
310 | 0 | } Unexecuted instantiation: njs_crypto_module.c:njs_pkey_get_ec_key Unexecuted instantiation: njs_webcrypto_module.c:njs_pkey_get_ec_key |
311 | | |
312 | | |
313 | | njs_inline int |
314 | | njs_ec_group_order_bits(const EC_GROUP *group) |
315 | 0 | { |
316 | 0 | #if (OPENSSL_VERSION_NUMBER >= 0x10100000L) |
317 | 0 | return EC_GROUP_order_bits(group); |
318 | | #else |
319 | | int bits; |
320 | | BIGNUM *order; |
321 | | |
322 | | order = BN_new(); |
323 | | if (order == NULL) { |
324 | | return 0; |
325 | | } |
326 | | |
327 | | if (EC_GROUP_get_order(group, order, NULL) == 0) { |
328 | | return 0; |
329 | | } |
330 | | |
331 | | bits = BN_num_bits(order); |
332 | | |
333 | | BN_free(order); |
334 | | |
335 | | return bits; |
336 | | #endif |
337 | 0 | } Unexecuted instantiation: njs_crypto_module.c:njs_ec_group_order_bits Unexecuted instantiation: njs_webcrypto_module.c:njs_ec_group_order_bits |
338 | | |
339 | | |
340 | | njs_inline int |
341 | | njs_ec_point_get_affine_coordinates(const EC_GROUP *group, const EC_POINT *p, |
342 | | BIGNUM *x, BIGNUM *y) |
343 | 0 | { |
344 | 0 | #if (OPENSSL_VERSION_NUMBER >= 0x10101001L) |
345 | 0 | return EC_POINT_get_affine_coordinates(group, p, x, y, NULL); |
346 | | #else |
347 | | return EC_POINT_get_affine_coordinates_GFp(group, p, x, y, NULL); |
348 | | #endif |
349 | 0 | } Unexecuted instantiation: njs_crypto_module.c:njs_ec_point_get_affine_coordinates Unexecuted instantiation: njs_webcrypto_module.c:njs_ec_point_get_affine_coordinates |
350 | | |
351 | | |
352 | | njs_inline int |
353 | | njs_ecdsa_sig_set0(ECDSA_SIG *sig, BIGNUM *r, BIGNUM *s) |
354 | 0 | { |
355 | 0 | #if (OPENSSL_VERSION_NUMBER >= 0x10100000L) |
356 | 0 | return ECDSA_SIG_set0(sig, r, s); |
357 | | #else |
358 | | if (r == NULL || s == NULL) { |
359 | | return 0; |
360 | | } |
361 | | |
362 | | BN_clear_free(sig->r); |
363 | | BN_clear_free(sig->s); |
364 | | |
365 | | sig->r = r; |
366 | | sig->s = s; |
367 | | |
368 | | return 1; |
369 | | #endif |
370 | 0 | } Unexecuted instantiation: njs_crypto_module.c:njs_ecdsa_sig_set0 Unexecuted instantiation: njs_webcrypto_module.c:njs_ecdsa_sig_set0 |
371 | | |
372 | | |
373 | | #endif /* _NJS_EXTERNAL_OPENSSL_H_INCLUDED_ */ |