/src/wolfssl-openssl-api/src/ssl_api_pk.c
Line | Count | Source |
1 | | /* ssl_api_pk.c |
2 | | * |
3 | | * Copyright (C) 2006-2026 wolfSSL Inc. |
4 | | * |
5 | | * This file is part of wolfSSL. |
6 | | * |
7 | | * wolfSSL is free software; you can redistribute it and/or modify |
8 | | * it under the terms of the GNU General Public License as published by |
9 | | * the Free Software Foundation; either version 3 of the License, or |
10 | | * (at your option) any later version. |
11 | | * |
12 | | * wolfSSL is distributed in the hope that it will be useful, |
13 | | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
14 | | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
15 | | * GNU General Public License for more details. |
16 | | * |
17 | | * You should have received a copy of the GNU General Public License |
18 | | * along with this program; if not, write to the Free Software |
19 | | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA |
20 | | */ |
21 | | |
22 | | #include <wolfssl/wolfcrypt/libwolfssl_sources.h> |
23 | | |
24 | | #if !defined(WOLFSSL_SSL_API_PK_INCLUDED) |
25 | | #ifndef WOLFSSL_IGNORE_FILE_WARN |
26 | | #warning ssl_api_pk.c is not compiled separately from ssl.c |
27 | | #endif |
28 | | #else |
29 | | |
30 | | #ifndef NO_CERTS |
31 | | |
32 | | #ifndef NO_CHECK_PRIVATE_KEY |
33 | | |
34 | | #ifdef WOLF_PRIVATE_KEY_ID |
35 | | /* Check priv against pub for match using external device with given devId |
36 | | * |
37 | | * @param [in] keyOID Public key OID. |
38 | | * @param [in] privKey Private key data. |
39 | | * @param [in] privSz Length of private key data in bytes. |
40 | | * @param [in] pubKey Public key data. |
41 | | * @param [in] pubSz Length of public key data in bytes. |
42 | | * @param [in] label Key data is a hardware label. |
43 | | * @param [in] id Key data is a hardware id. |
44 | | * @param [in] heap Heap hint for dynamic memory allocation. |
45 | | * @param [in] devId Device Id. |
46 | | * @return 0 on success. |
47 | | * @return MISSING_KEY when privKey is NULL. |
48 | | * @return Other negative value on error. |
49 | | */ |
50 | | static int check_cert_key_dev(word32 keyOID, byte* privKey, word32 privSz, |
51 | | const byte* pubKey, word32 pubSz, int label, int id, void* heap, int devId) |
52 | 0 | { |
53 | 0 | int ret = 0; |
54 | 0 | int type = 0; |
55 | 0 | void *pkey = NULL; |
56 | |
|
57 | 0 | if (privKey == NULL) { |
58 | 0 | ret = MISSING_KEY; |
59 | 0 | } |
60 | 0 | else { |
61 | 0 | switch (keyOID) { |
62 | 0 | #ifndef NO_RSA |
63 | 0 | case RSAk: |
64 | 0 | #ifdef WC_RSA_PSS |
65 | 0 | case RSAPSSk: |
66 | 0 | #endif |
67 | 0 | type = DYNAMIC_TYPE_RSA; |
68 | 0 | break; |
69 | 0 | #endif |
70 | 0 | #ifdef HAVE_ECC |
71 | 0 | case ECDSAk: |
72 | 0 | type = DYNAMIC_TYPE_ECC; |
73 | 0 | break; |
74 | 0 | #endif |
75 | | #if defined(HAVE_DILITHIUM) |
76 | | case ML_DSA_LEVEL2k: |
77 | | case ML_DSA_LEVEL3k: |
78 | | case ML_DSA_LEVEL5k: |
79 | | #ifdef WOLFSSL_DILITHIUM_FIPS204_DRAFT |
80 | | case DILITHIUM_LEVEL2k: |
81 | | case DILITHIUM_LEVEL3k: |
82 | | case DILITHIUM_LEVEL5k: |
83 | | #endif |
84 | | type = DYNAMIC_TYPE_DILITHIUM; |
85 | | break; |
86 | | #endif |
87 | | #if defined(HAVE_FALCON) |
88 | | case FALCON_LEVEL1k: |
89 | | case FALCON_LEVEL5k: |
90 | | type = DYNAMIC_TYPE_FALCON; |
91 | | break; |
92 | | #endif |
93 | 0 | } |
94 | | |
95 | 0 | ret = CreateDevPrivateKey(&pkey, privKey, privSz, type, label, id, heap, |
96 | 0 | devId); |
97 | 0 | } |
98 | 0 | #ifdef WOLF_CRYPTO_CB |
99 | 0 | if (ret == 0) { |
100 | 0 | switch (keyOID) { |
101 | 0 | #ifndef NO_RSA |
102 | 0 | case RSAk: |
103 | 0 | #ifdef WC_RSA_PSS |
104 | 0 | case RSAPSSk: |
105 | 0 | #endif |
106 | 0 | ret = wc_CryptoCb_RsaCheckPrivKey((RsaKey*)pkey, pubKey, pubSz); |
107 | 0 | break; |
108 | 0 | #endif |
109 | 0 | #ifdef HAVE_ECC |
110 | 0 | case ECDSAk: |
111 | 0 | ret = wc_CryptoCb_EccCheckPrivKey((ecc_key*)pkey, pubKey, |
112 | 0 | pubSz); |
113 | 0 | break; |
114 | 0 | #endif |
115 | | #if defined(HAVE_DILITHIUM) |
116 | | case ML_DSA_LEVEL2k: |
117 | | case ML_DSA_LEVEL3k: |
118 | | case ML_DSA_LEVEL5k: |
119 | | #ifdef WOLFSSL_DILITHIUM_FIPS204_DRAFT |
120 | | case DILITHIUM_LEVEL2k: |
121 | | case DILITHIUM_LEVEL3k: |
122 | | case DILITHIUM_LEVEL5k: |
123 | | #endif |
124 | | ret = wc_CryptoCb_PqcSignatureCheckPrivKey(pkey, |
125 | | WC_PQC_SIG_TYPE_DILITHIUM, pubKey, pubSz); |
126 | | break; |
127 | | #endif |
128 | | #if defined(HAVE_FALCON) |
129 | | case FALCON_LEVEL1k: |
130 | | case FALCON_LEVEL5k: |
131 | | ret = wc_CryptoCb_PqcSignatureCheckPrivKey(pkey, |
132 | | WC_PQC_SIG_TYPE_FALCON, pubKey, pubSz); |
133 | | break; |
134 | | #endif |
135 | 0 | default: |
136 | 0 | ret = 0; |
137 | 0 | } |
138 | 0 | } |
139 | | #else |
140 | | /* devId was set, don't check, for now */ |
141 | | /* TODO: Add callback for private key check? */ |
142 | | (void) pubKey; |
143 | | (void) pubSz; |
144 | | #endif |
145 | | |
146 | 0 | switch (keyOID) { |
147 | 0 | #ifndef NO_RSA |
148 | 0 | case RSAk: |
149 | 0 | #ifdef WC_RSA_PSS |
150 | 0 | case RSAPSSk: |
151 | 0 | #endif |
152 | 0 | wc_FreeRsaKey((RsaKey*)pkey); |
153 | 0 | break; |
154 | 0 | #endif |
155 | 0 | #ifdef HAVE_ECC |
156 | 0 | case ECDSAk: |
157 | 0 | wc_ecc_free((ecc_key*)pkey); |
158 | 0 | break; |
159 | 0 | #endif |
160 | | #if defined(HAVE_DILITHIUM) |
161 | | case ML_DSA_LEVEL2k: |
162 | | case ML_DSA_LEVEL3k: |
163 | | case ML_DSA_LEVEL5k: |
164 | | #ifdef WOLFSSL_DILITHIUM_FIPS204_DRAFT |
165 | | case DILITHIUM_LEVEL2k: |
166 | | case DILITHIUM_LEVEL3k: |
167 | | case DILITHIUM_LEVEL5k: |
168 | | #endif |
169 | | wc_dilithium_free((dilithium_key*)pkey); |
170 | | break; |
171 | | #endif |
172 | | #if defined(HAVE_FALCON) |
173 | | case FALCON_LEVEL1k: |
174 | | case FALCON_LEVEL5k: |
175 | | wc_falcon_free((falcon_key*)pkey); |
176 | | break; |
177 | | #endif |
178 | 0 | default: |
179 | 0 | WC_DO_NOTHING; |
180 | 0 | } |
181 | 0 | XFREE(pkey, heap, type); |
182 | |
|
183 | 0 | return ret; |
184 | 0 | } |
185 | | #endif /* WOLF_PRIVATE_KEY_ID */ |
186 | | |
187 | | /* Check private against public in certificate for match. |
188 | | * |
189 | | * @param [in] cert DER encoded certificate. |
190 | | * @param [in] key DER encoded private key. |
191 | | * @param [in] altKey Alternative DER encoded key. |
192 | | * @param [in] heap Heap hint for dynamic memory allocation. |
193 | | * @param [in] devId Device Id. |
194 | | * @param [in] isKeyLabel Whether key is label. |
195 | | * @param [in] isKeyId Whether key is an id. |
196 | | * @param [in] altDevId Alternative key's device id. |
197 | | * @param [in] isAltKeyLabel Is alternative key a label. |
198 | | * @param [in] isAltKeyId Is alternative key an id. |
199 | | * @return 1 on success. |
200 | | * @return 0 on failure. |
201 | | * @return MEMORY_E when memory allocation fails. |
202 | | */ |
203 | | static int check_cert_key(const DerBuffer* cert, const DerBuffer* key, |
204 | | const DerBuffer* altKey, void* heap, int devId, int isKeyLabel, int isKeyId, |
205 | | int altDevId, int isAltKeyLabel, int isAltKeyId) |
206 | 0 | { |
207 | 0 | WC_DECLARE_VAR(der, DecodedCert, 1, 0); |
208 | 0 | word32 size; |
209 | 0 | byte* buff; |
210 | 0 | int ret = 1; |
211 | |
|
212 | 0 | WOLFSSL_ENTER("check_cert_key"); |
213 | | |
214 | | /* Validate parameters. */ |
215 | 0 | if ((cert == NULL) || (key == NULL)) { |
216 | 0 | return 0; |
217 | 0 | } |
218 | 0 | if (ret == 1) { |
219 | | /* Make a decoded certificate object available. */ |
220 | 0 | WC_ALLOC_VAR_EX(der, DecodedCert, 1, heap, DYNAMIC_TYPE_DCERT, |
221 | 0 | return MEMORY_E); |
222 | 0 | } |
223 | | |
224 | 0 | if (ret == 1) { |
225 | | /* Decode certificate. */ |
226 | 0 | InitDecodedCert_ex(der, cert->buffer, cert->length, heap, devId); |
227 | | /* Parse certificate. */ |
228 | 0 | if (ParseCertRelative(der, CERT_TYPE, NO_VERIFY, NULL, NULL) != 0) { |
229 | 0 | ret = 0; |
230 | 0 | } |
231 | 0 | } |
232 | |
|
233 | 0 | if (ret == 1) { |
234 | 0 | buff = key->buffer; |
235 | 0 | size = key->length; |
236 | 0 | #ifdef WOLF_PRIVATE_KEY_ID |
237 | 0 | if (devId != INVALID_DEVID) { |
238 | 0 | ret = check_cert_key_dev(der->keyOID, buff, size, der->publicKey, |
239 | 0 | der->pubKeySize, isKeyLabel, isKeyId, heap, devId); |
240 | 0 | if (ret != WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE)) { |
241 | 0 | ret = (ret == 0) ? WOLFSSL_SUCCESS: WOLFSSL_FAILURE; |
242 | 0 | } |
243 | 0 | } |
244 | 0 | else { |
245 | | /* fall through if unavailable */ |
246 | 0 | ret = CRYPTOCB_UNAVAILABLE; |
247 | 0 | } |
248 | |
|
249 | 0 | if (ret == WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE)) |
250 | 0 | #endif /* WOLF_PRIVATE_KEY_ID */ |
251 | 0 | { |
252 | 0 | ret = wc_CheckPrivateKeyCert(buff, size, der, 0, heap); |
253 | 0 | if (ret != 1) { |
254 | 0 | ret = 0; |
255 | 0 | } |
256 | 0 | } |
257 | |
|
258 | | #ifdef WOLFSSL_DUAL_ALG_CERTS |
259 | | if ((ret == 1) && der->extSapkiSet && (der->sapkiDer != NULL)) { |
260 | | /* Certificate contains an alternative public key. Hence, we also |
261 | | * need an alternative private key. */ |
262 | | if (altKey == NULL) { |
263 | | ret = MISSING_KEY; |
264 | | buff = NULL; |
265 | | size = 0; |
266 | | } |
267 | | else { |
268 | | size = altKey->length; |
269 | | buff = altKey->buffer; |
270 | | } |
271 | | #ifdef WOLF_PRIVATE_KEY_ID |
272 | | if (altDevId != INVALID_DEVID) { |
273 | | /* We have to decode the public key first */ |
274 | | /* Default to max pub key size. */ |
275 | | word32 pubKeyLen = MAX_PUBLIC_KEY_SZ; |
276 | | byte* decodedPubKey = (byte*)XMALLOC(pubKeyLen, heap, |
277 | | DYNAMIC_TYPE_PUBLIC_KEY); |
278 | | if (decodedPubKey == NULL) { |
279 | | ret = MEMORY_E; |
280 | | } |
281 | | if (ret == WOLFSSL_SUCCESS) { |
282 | | if ((der->sapkiOID == RSAk) || (der->sapkiOID == ECDSAk)) { |
283 | | /* Simply copy the data */ |
284 | | XMEMCPY(decodedPubKey, der->sapkiDer, der->sapkiLen); |
285 | | pubKeyLen = der->sapkiLen; |
286 | | ret = 0; |
287 | | } |
288 | | else { |
289 | | #if defined(WC_ENABLE_ASYM_KEY_IMPORT) |
290 | | word32 idx = 0; |
291 | | ret = DecodeAsymKeyPublic(der->sapkiDer, &idx, |
292 | | der->sapkiLen, decodedPubKey, |
293 | | &pubKeyLen, der->sapkiOID); |
294 | | #else |
295 | | ret = NOT_COMPILED_IN; |
296 | | #endif /* WC_ENABLE_ASYM_KEY_IMPORT */ |
297 | | } |
298 | | } |
299 | | if (ret == 0) { |
300 | | ret = check_cert_key_dev(der->sapkiOID, buff, size, |
301 | | decodedPubKey, pubKeyLen, isAltKeyLabel, isAltKeyId, |
302 | | heap, altDevId); |
303 | | } |
304 | | XFREE(decodedPubKey, heap, DYNAMIC_TYPE_PUBLIC_KEY); |
305 | | if (ret != WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE)) { |
306 | | ret = (ret == 0) ? 1: 0; |
307 | | } |
308 | | } |
309 | | else { |
310 | | /* fall through if unavailable */ |
311 | | ret = CRYPTOCB_UNAVAILABLE; |
312 | | } |
313 | | |
314 | | if (ret == WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE)) |
315 | | #else |
316 | | if (ret == 1) |
317 | | #endif /* WOLF_PRIVATE_KEY_ID */ |
318 | | { |
319 | | ret = wc_CheckPrivateKeyCert(buff, size, der, 1, heap); |
320 | | if (ret != 1) { |
321 | | ret = 0; |
322 | | } |
323 | | } |
324 | | } |
325 | | #endif /* WOLFSSL_DUAL_ALG_CERTS */ |
326 | 0 | } |
327 | |
|
328 | 0 | FreeDecodedCert(der); |
329 | 0 | WC_FREE_VAR_EX(der, heap, DYNAMIC_TYPE_DCERT); |
330 | |
|
331 | 0 | (void)devId; |
332 | 0 | (void)isKeyLabel; |
333 | 0 | (void)isKeyId; |
334 | 0 | (void)altKey; |
335 | 0 | (void)altDevId; |
336 | 0 | (void)isAltKeyLabel; |
337 | 0 | (void)isAltKeyId; |
338 | |
|
339 | 0 | return ret; |
340 | 0 | } |
341 | | |
342 | | /* Check private against public in certificate for match |
343 | | * |
344 | | * @param [in] ctx SSL/TLS context with a private key and certificate. |
345 | | * |
346 | | * @return 1 on good private key |
347 | | * @return 0 if mismatched. |
348 | | */ |
349 | | int wolfSSL_CTX_check_private_key(const WOLFSSL_CTX* ctx) |
350 | 0 | { |
351 | 0 | int res = 1; |
352 | | #ifdef WOLFSSL_BLIND_PRIVATE_KEY |
353 | | DerBuffer *privateKey; |
354 | | #ifdef WOLFSSL_DUAL_ALG_CERTS |
355 | | DerBuffer *altPrivateKey; |
356 | | #endif |
357 | | #else |
358 | 0 | const DerBuffer *privateKey; |
359 | | #ifdef WOLFSSL_DUAL_ALG_CERTS |
360 | | const DerBuffer *altPrivateKey; |
361 | | #endif |
362 | 0 | #endif |
363 | | |
364 | | /* Validate parameter. */ |
365 | 0 | if (ctx == NULL) { |
366 | 0 | res = 0; |
367 | 0 | } |
368 | 0 | else { |
369 | | #ifdef WOLFSSL_DUAL_ALG_CERTS |
370 | | #ifdef WOLFSSL_BLIND_PRIVATE_KEY |
371 | | /* Unblind private keys. */ |
372 | | privateKey = wolfssl_priv_der_unblind(ctx->privateKey, |
373 | | ctx->privateKeyMask); |
374 | | if (privateKey == NULL) { |
375 | | res = 0; |
376 | | } |
377 | | if (ctx->altPrivateKey != NULL) { |
378 | | altPrivateKey = wolfssl_priv_der_unblind(ctx->altPrivateKey, |
379 | | ctx->altPrivateKeyMask); |
380 | | if (altPrivateKey == NULL) { |
381 | | res = 0; |
382 | | } |
383 | | } |
384 | | else { |
385 | | altPrivateKey = NULL; |
386 | | } |
387 | | #else |
388 | | privateKey = ctx->privateKey; |
389 | | altPrivateKey = ctx->altPrivateKey; |
390 | | #endif |
391 | | if (res == 1) { |
392 | | /* Check certificate and private keys. */ |
393 | | res = check_cert_key(ctx->certificate, privateKey, altPrivateKey, |
394 | | ctx->heap, ctx->privateKeyDevId, ctx->privateKeyLabel, |
395 | | ctx->privateKeyId, ctx->altPrivateKeyDevId, |
396 | | ctx->altPrivateKeyLabel, ctx->altPrivateKeyId) != 0; |
397 | | } |
398 | | #ifdef WOLFSSL_BLIND_PRIVATE_KEY |
399 | | /* Dispose of the unblinded buffers. */ |
400 | | wolfssl_priv_der_unblind_free(privateKey); |
401 | | wolfssl_priv_der_unblind_free(altPrivateKey); |
402 | | #endif |
403 | | #else |
404 | | #ifdef WOLFSSL_BLIND_PRIVATE_KEY |
405 | | /* Unblind private key. */ |
406 | | privateKey = wolfssl_priv_der_unblind(ctx->privateKey, |
407 | | ctx->privateKeyMask); |
408 | | if (privateKey == NULL) { |
409 | | res = 0; |
410 | | } |
411 | | #else |
412 | 0 | privateKey = ctx->privateKey; |
413 | 0 | #endif |
414 | 0 | if (res == WOLFSSL_SUCCESS) { |
415 | | /* Check certificate and private key. */ |
416 | 0 | res = check_cert_key(ctx->certificate, privateKey, NULL, ctx->heap, |
417 | 0 | ctx->privateKeyDevId, ctx->privateKeyLabel, ctx->privateKeyId, |
418 | 0 | INVALID_DEVID, 0, 0); |
419 | 0 | } |
420 | | #ifdef WOLFSSL_BLIND_PRIVATE_KEY |
421 | | /* Dispose of the unblinded buffer. */ |
422 | | wolfssl_priv_der_unblind_free(privateKey); |
423 | | #endif |
424 | 0 | #endif |
425 | 0 | } |
426 | | |
427 | | /* Place error into queue for Python port. */ |
428 | 0 | if (res != 1) { |
429 | 0 | WOLFSSL_ERROR(WC_KEY_MISMATCH_E); |
430 | 0 | } |
431 | |
|
432 | 0 | return res; |
433 | 0 | } |
434 | | |
435 | | #ifdef OPENSSL_EXTRA |
436 | | /* Check private against public in certificate for match. |
437 | | * |
438 | | * @param [in] ssl SSL/TLS object with a private key and certificate. |
439 | | * |
440 | | * @return 1 on good private key |
441 | | * @return 0 if mismatched. |
442 | | */ |
443 | | int wolfSSL_check_private_key(const WOLFSSL* ssl) |
444 | 0 | { |
445 | 0 | int res = 1; |
446 | | #ifdef WOLFSSL_BLIND_PRIVATE_KEY |
447 | | DerBuffer *privateKey; |
448 | | #ifdef WOLFSSL_DUAL_ALG_CERTS |
449 | | DerBuffer *altPrivateKey; |
450 | | #endif |
451 | | #else |
452 | 0 | const DerBuffer *privateKey; |
453 | | #ifdef WOLFSSL_DUAL_ALG_CERTS |
454 | | const DerBuffer *altPrivateKey; |
455 | | #endif |
456 | 0 | #endif |
457 | | |
458 | | /* Validate parameter. */ |
459 | 0 | if (ssl == NULL) { |
460 | 0 | res = 0; |
461 | 0 | } |
462 | 0 | else { |
463 | | #ifdef WOLFSSL_DUAL_ALG_CERTS |
464 | | #ifdef WOLFSSL_BLIND_PRIVATE_KEY |
465 | | /* Unblind private keys. */ |
466 | | privateKey = wolfssl_priv_der_unblind(ssl->buffers.key, |
467 | | ssl->buffers.keyMask); |
468 | | if (privateKey == NULL) { |
469 | | res = 0; |
470 | | } |
471 | | if (ssl->buffers.altKey != NULL) { |
472 | | altPrivateKey = wolfssl_priv_der_unblind(ssl->buffers.altKey, |
473 | | ssl->buffers.altKeyMask); |
474 | | if (altPrivateKey == NULL) { |
475 | | res = 0; |
476 | | } |
477 | | } |
478 | | else { |
479 | | altPrivateKey = NULL; |
480 | | } |
481 | | #else |
482 | | privateKey = ssl->buffers.key; |
483 | | altPrivateKey = ssl->buffers.altKey; |
484 | | #endif |
485 | | if (res == 1) { |
486 | | /* Check certificate and private keys. */ |
487 | | res = check_cert_key(ssl->buffers.certificate, privateKey, |
488 | | altPrivateKey, ssl->heap, ssl->buffers.keyDevId, |
489 | | ssl->buffers.keyLabel, ssl->buffers.keyId, |
490 | | ssl->buffers.altKeyDevId, ssl->buffers.altKeyLabel, |
491 | | ssl->buffers.altKeyId); |
492 | | } |
493 | | #ifdef WOLFSSL_BLIND_PRIVATE_KEY |
494 | | /* Dispose of the unblinded buffers. */ |
495 | | wolfssl_priv_der_unblind_free(privateKey); |
496 | | wolfssl_priv_der_unblind_free(altPrivateKey); |
497 | | #endif |
498 | | #else |
499 | | #ifdef WOLFSSL_BLIND_PRIVATE_KEY |
500 | | /* Unblind private key. */ |
501 | | privateKey = wolfssl_priv_der_unblind(ssl->buffers.key, |
502 | | ssl->buffers.keyMask); |
503 | | if (privateKey == NULL) { |
504 | | res = 0; |
505 | | } |
506 | | #else |
507 | 0 | privateKey = ssl->buffers.key; |
508 | 0 | #endif |
509 | 0 | if (res == 1) { |
510 | | /* Check certificate and private key. */ |
511 | 0 | res = check_cert_key(ssl->buffers.certificate, privateKey, NULL, |
512 | 0 | ssl->heap, ssl->buffers.keyDevId, ssl->buffers.keyLabel, |
513 | 0 | ssl->buffers.keyId, INVALID_DEVID, 0, 0); |
514 | 0 | } |
515 | | #ifdef WOLFSSL_BLIND_PRIVATE_KEY |
516 | | /* Dispose of the unblinded buffer. */ |
517 | | wolfssl_priv_der_unblind_free(privateKey); |
518 | | #endif |
519 | 0 | #endif |
520 | 0 | } |
521 | |
|
522 | 0 | return res; |
523 | 0 | } |
524 | | #endif /* OPENSSL_EXTRA */ |
525 | | #endif /* !NO_CHECK_PRIVATE_KEY */ |
526 | | |
527 | | |
528 | | #ifdef OPENSSL_ALL |
529 | | /** |
530 | | * Return the private key of the SSL/TLS context. |
531 | | * |
532 | | * The caller doesn *NOT*` free the returned object. |
533 | | * |
534 | | * Note, even though the supplied ctx pointer is designated const, on success |
535 | | * ctx->privateKeyPKey is changed by this call. The change is done safely using |
536 | | * a hardware-synchronized store. |
537 | | * |
538 | | * @param [in] ctx SSL/TLS context. |
539 | | * @return A WOFLSSL_EVP_PKEY on success. |
540 | | * @return NULL on error. |
541 | | */ |
542 | | WOLFSSL_EVP_PKEY* wolfSSL_CTX_get0_privatekey(const WOLFSSL_CTX* ctx) |
543 | 0 | { |
544 | 0 | WOLFSSL_EVP_PKEY* res = NULL; |
545 | 0 | const unsigned char *key; |
546 | 0 | int type = WC_EVP_PKEY_NONE; |
547 | |
|
548 | 0 | WOLFSSL_ENTER("wolfSSL_CTX_get0_privatekey"); |
549 | |
|
550 | 0 | if ((ctx == NULL) || (ctx->privateKey == NULL) || |
551 | 0 | (ctx->privateKey->buffer == NULL)) { |
552 | 0 | WOLFSSL_MSG("Bad parameter or key not set"); |
553 | 0 | } |
554 | 0 | else { |
555 | 0 | switch (ctx->privateKeyType) { |
556 | 0 | #ifndef NO_RSA |
557 | 0 | case rsa_sa_algo: |
558 | 0 | type = WC_EVP_PKEY_RSA; |
559 | 0 | break; |
560 | 0 | #endif |
561 | 0 | #ifdef HAVE_ECC |
562 | 0 | case ecc_dsa_sa_algo: |
563 | 0 | type = WC_EVP_PKEY_EC; |
564 | 0 | break; |
565 | 0 | #endif |
566 | 0 | #ifdef WOLFSSL_SM2 |
567 | 0 | case sm2_sa_algo: |
568 | 0 | type = WC_EVP_PKEY_EC; |
569 | 0 | break; |
570 | 0 | #endif |
571 | 0 | default: |
572 | | /* Other key types not supported either as ssl private keys |
573 | | * or in the EVP layer */ |
574 | 0 | WOLFSSL_MSG("Unsupported key type"); |
575 | 0 | } |
576 | 0 | } |
577 | | |
578 | 0 | if (type != WC_EVP_PKEY_NONE) { |
579 | 0 | if (ctx->privateKeyPKey != NULL) { |
580 | 0 | res = ctx->privateKeyPKey; |
581 | 0 | } |
582 | 0 | else { |
583 | | #ifdef WOLFSSL_BLIND_PRIVATE_KEY |
584 | | DerBuffer* unblinded_privateKey = wolfssl_priv_der_unblind( |
585 | | ctx->privateKey, ctx->privateKeyMask); |
586 | | if (unblinded_privateKey != NULL) { |
587 | | key = unblinded_privateKey->buffer; |
588 | | } |
589 | | else { |
590 | | key = NULL; |
591 | | } |
592 | | #else |
593 | 0 | key = ctx->privateKey->buffer; |
594 | 0 | #endif |
595 | 0 | if (key != NULL) { |
596 | 0 | res = wolfSSL_d2i_PrivateKey(type, NULL, &key, |
597 | 0 | (long)ctx->privateKey->length); |
598 | | #ifdef WOLFSSL_BLIND_PRIVATE_KEY |
599 | | wolfssl_priv_der_unblind_free(unblinded_privateKey); |
600 | | #endif |
601 | 0 | } |
602 | 0 | if (res != NULL) { |
603 | 0 | #ifdef WOLFSSL_ATOMIC_OPS |
604 | 0 | WOLFSSL_EVP_PKEY *current_pkey = NULL; |
605 | 0 | if (!wolfSSL_Atomic_Ptr_CompareExchange( |
606 | 0 | (void * volatile *)&ctx->privateKeyPKey, |
607 | 0 | (void **)¤t_pkey, res)) { |
608 | 0 | wolfSSL_EVP_PKEY_free(res); |
609 | 0 | res = current_pkey; |
610 | 0 | } |
611 | | #else |
612 | | ((WOLFSSL_CTX *)ctx)->privateKeyPKey = res; |
613 | | #endif |
614 | 0 | } |
615 | 0 | } |
616 | 0 | } |
617 | |
|
618 | 0 | return res; |
619 | 0 | } |
620 | | #endif /* OPENSSL_ALL */ |
621 | | |
622 | | #ifdef HAVE_ECC |
623 | | |
624 | | /* Set size, in bytes, of temporary ECDHE key into SSL/TLS context. |
625 | | * |
626 | | * Values can be: 14 - 66 (112 - 521 bit) |
627 | | * Uses the private key length if sz is 0. |
628 | | * |
629 | | * @param [in] ctx SSL/TLS context. |
630 | | * @param [in] sz Size of EC key in bytes. |
631 | | * @return 1 on success. |
632 | | * @return BAD_FUNC_ARG when ctx is NULL or sz is invalid. |
633 | | */ |
634 | | int wolfSSL_CTX_SetTmpEC_DHE_Sz(WOLFSSL_CTX* ctx, word16 sz) |
635 | 0 | { |
636 | 0 | int ret = 0; |
637 | |
|
638 | 0 | WOLFSSL_ENTER("wolfSSL_CTX_SetTmpEC_DHE_Sz"); |
639 | | |
640 | | /* Validate parameters. */ |
641 | 0 | if (ctx == NULL) { |
642 | 0 | ret = BAD_FUNC_ARG; |
643 | 0 | } |
644 | | /* If size is 0 then get value from loaded private key. */ |
645 | 0 | else if (sz == 0) { |
646 | | /* Applies only to ECDSA. */ |
647 | 0 | if (ctx->privateKeyType != ecc_dsa_sa_algo) { |
648 | 0 | ret = 1; |
649 | 0 | } |
650 | | /* Must have a key set. */ |
651 | 0 | else if (ctx->privateKeySz == 0) { |
652 | 0 | WOLFSSL_MSG("Must set private key/cert first"); |
653 | 0 | ret = BAD_FUNC_ARG; |
654 | 0 | } |
655 | 0 | else { |
656 | 0 | sz = (word16)ctx->privateKeySz; |
657 | 0 | } |
658 | 0 | } |
659 | 0 | if (ret == 0) { |
660 | | /* Check size against bounds. */ |
661 | | #if ECC_MIN_KEY_SZ > 0 |
662 | | if (sz < ECC_MINSIZE) { |
663 | | ret = BAD_FUNC_ARG; |
664 | | } |
665 | | else |
666 | | #endif |
667 | 0 | if (sz > ECC_MAXSIZE) { |
668 | 0 | ret = BAD_FUNC_ARG; |
669 | 0 | } |
670 | 0 | else { |
671 | | /* Store the size requested. */ |
672 | 0 | ctx->eccTempKeySz = sz; |
673 | 0 | ret = 1; |
674 | 0 | } |
675 | 0 | } |
676 | |
|
677 | 0 | return ret; |
678 | 0 | } |
679 | | |
680 | | |
681 | | /* Set size, in bytes, of temporary ECDHE key into SSL/TLS object. |
682 | | * |
683 | | * Values can be: 14 - 66 (112 - 521 bit) |
684 | | * Uses the private key length if sz is 0. |
685 | | * |
686 | | * @param [in] ssl SSL/TLS object. |
687 | | * @param [in] sz Size of EC key in bytes. |
688 | | * @return 1 on success. |
689 | | * @return BAD_FUNC_ARG when ssl is NULL or sz is invalid. |
690 | | */ |
691 | | int wolfSSL_SetTmpEC_DHE_Sz(WOLFSSL* ssl, word16 sz) |
692 | 0 | { |
693 | 0 | int ret = 1; |
694 | |
|
695 | 0 | WOLFSSL_ENTER("wolfSSL_SetTmpEC_DHE_Sz"); |
696 | | |
697 | | /* Validate parameters. */ |
698 | 0 | if (ssl == NULL) { |
699 | 0 | ret = BAD_FUNC_ARG; |
700 | 0 | } |
701 | | /* Check size against bounds. */ |
702 | | #if ECC_MIN_KEY_SZ > 0 |
703 | | else if (sz < ECC_MINSIZE) { |
704 | | ret = BAD_FUNC_ARG; |
705 | | } |
706 | | #endif |
707 | 0 | else if (sz > ECC_MAXSIZE) { |
708 | 0 | ret = BAD_FUNC_ARG; |
709 | 0 | } |
710 | 0 | else { |
711 | | /* Store the size requested. */ |
712 | 0 | ssl->eccTempKeySz = sz; |
713 | 0 | } |
714 | |
|
715 | 0 | return ret; |
716 | 0 | } |
717 | | |
718 | | #endif /* HAVE_ECC */ |
719 | | |
720 | | #ifdef HAVE_PK_CALLBACKS |
721 | | |
722 | | #ifdef HAVE_ECC |
723 | | /* Set the ECC key generation callback into the SSL/TLS context. |
724 | | * |
725 | | * @param [in] ctx SSL/TLS context. |
726 | | * @param [in] cb ECC key generation callback. |
727 | | */ |
728 | | void wolfSSL_CTX_SetEccKeyGenCb(WOLFSSL_CTX* ctx, CallbackEccKeyGen cb) |
729 | | { |
730 | | if (ctx != NULL) { |
731 | | ctx->EccKeyGenCb = cb; |
732 | | } |
733 | | } |
734 | | /* Set the context for ECC key generation callback into the SSL/TLS object. |
735 | | * |
736 | | * @param [in] ssl SSL/TLS object. |
737 | | * @param [in] ctx Context for ECC key generation callback. |
738 | | */ |
739 | | void wolfSSL_SetEccKeyGenCtx(WOLFSSL* ssl, void *ctx) |
740 | | { |
741 | | if (ssl != NULL) { |
742 | | ssl->EccKeyGenCtx = ctx; |
743 | | } |
744 | | } |
745 | | /* Get the context for ECC key generation callback from the SSL/TLS object. |
746 | | * |
747 | | * @param [in] ssl SSL/TLS object. |
748 | | * @return Context for ECC key generation callback. |
749 | | * @return NULL when ssl is NULL. |
750 | | */ |
751 | | void* wolfSSL_GetEccKeyGenCtx(WOLFSSL* ssl) |
752 | | { |
753 | | void* ret; |
754 | | |
755 | | if (ssl == NULL) { |
756 | | ret = NULL; |
757 | | } |
758 | | else { |
759 | | ret = ssl->EccKeyGenCtx; |
760 | | } |
761 | | |
762 | | return ret; |
763 | | } |
764 | | /* Set the context for ECC sign callback into the SSL/TLS context. |
765 | | * |
766 | | * @param [in] ctx SSL/TLS context. |
767 | | * @param [in] userCtx Context for ECC sign callback. |
768 | | */ |
769 | | void wolfSSL_CTX_SetEccSignCtx(WOLFSSL_CTX* ctx, void *userCtx) |
770 | | { |
771 | | if (ctx != NULL) { |
772 | | ctx->EccSignCtx = userCtx; |
773 | | } |
774 | | } |
775 | | /* Get the context for ECC sign callback from the SSL/TLS context. |
776 | | * |
777 | | * @param [in] ctx SSL/TLS context. |
778 | | * @return Context for ECC sign for callback. |
779 | | * @return NULL when ctx is NULL. |
780 | | */ |
781 | | void* wolfSSL_CTX_GetEccSignCtx(WOLFSSL_CTX* ctx) |
782 | | { |
783 | | void* ret; |
784 | | |
785 | | if (ctx == NULL) { |
786 | | ret = NULL; |
787 | | } |
788 | | else { |
789 | | ret = ctx->EccSignCtx; |
790 | | } |
791 | | |
792 | | return ret; |
793 | | } |
794 | | |
795 | | /* Set the ECC sign callback into the SSL/TLS context. |
796 | | * |
797 | | * @param [in] ctx SSL/TLS context. |
798 | | * @param [in] cb ECC sign callback. |
799 | | */ |
800 | | WOLFSSL_ABI void wolfSSL_CTX_SetEccSignCb(WOLFSSL_CTX* ctx, CallbackEccSign cb) |
801 | | { |
802 | | if (ctx != NULL) { |
803 | | ctx->EccSignCb = cb; |
804 | | } |
805 | | } |
806 | | /* Set the context for ECC sign callback into the SSL/TLS object. |
807 | | * |
808 | | * @param [in] ssl SSL/TLS object. |
809 | | * @param [in] ctx Context for ECC sign callback. |
810 | | */ |
811 | | void wolfSSL_SetEccSignCtx(WOLFSSL* ssl, void *ctx) |
812 | | { |
813 | | if (ssl != NULL) { |
814 | | ssl->EccSignCtx = ctx; |
815 | | } |
816 | | } |
817 | | /* Get the context for ECC sign callback from the SSL/TLS object. |
818 | | * |
819 | | * @param [in] ssl SSL/TLS object. |
820 | | * @return Context for ECC sign for callback. |
821 | | * @return NULL when ssl is NULL. |
822 | | */ |
823 | | void* wolfSSL_GetEccSignCtx(WOLFSSL* ssl) |
824 | | { |
825 | | void* ret; |
826 | | |
827 | | if (ssl == NULL) { |
828 | | ret = NULL; |
829 | | } |
830 | | else { |
831 | | ret = ssl->EccSignCtx; |
832 | | } |
833 | | |
834 | | return ret; |
835 | | } |
836 | | |
837 | | /* Set the ECC verify callback into the SSL/TLS context. |
838 | | * |
839 | | * @param [in] ctx SSL/TLS context. |
840 | | * @param [in] cb ECC verify callback. |
841 | | */ |
842 | | void wolfSSL_CTX_SetEccVerifyCb(WOLFSSL_CTX* ctx, CallbackEccVerify cb) |
843 | | { |
844 | | if (ctx != NULL) { |
845 | | ctx->EccVerifyCb = cb; |
846 | | } |
847 | | } |
848 | | /* Set the context for ECC verify callback into the SSL/TLS object. |
849 | | * |
850 | | * @param [in] ssl SSL/TLS object. |
851 | | * @param [in] ctx Context for ECC verify callback. |
852 | | */ |
853 | | void wolfSSL_SetEccVerifyCtx(WOLFSSL* ssl, void *ctx) |
854 | | { |
855 | | if (ssl != NULL) { |
856 | | ssl->EccVerifyCtx = ctx; |
857 | | } |
858 | | } |
859 | | /* Get the context for ECC verify callback from the SSL/TLS object. |
860 | | * |
861 | | * @param [in] ssl SSL/TLS object. |
862 | | * @return Context for ECC verify for callback. |
863 | | * @return NULL when ssl is NULL. |
864 | | */ |
865 | | void* wolfSSL_GetEccVerifyCtx(WOLFSSL* ssl) |
866 | | { |
867 | | void* ret; |
868 | | |
869 | | if (ssl == NULL) { |
870 | | ret = NULL; |
871 | | } |
872 | | else { |
873 | | ret = ssl->EccVerifyCtx; |
874 | | } |
875 | | |
876 | | return ret; |
877 | | } |
878 | | |
879 | | /* Set the ECC shared secret callback into the SSL/TLS context. |
880 | | * |
881 | | * @param [in] ctx SSL/TLS context. |
882 | | * @param [in] cb ECC shared secret callback. |
883 | | */ |
884 | | void wolfSSL_CTX_SetEccSharedSecretCb(WOLFSSL_CTX* ctx, |
885 | | CallbackEccSharedSecret cb) |
886 | | { |
887 | | if (ctx != NULL) { |
888 | | ctx->EccSharedSecretCb = cb; |
889 | | } |
890 | | } |
891 | | /* Set the context for ECC shared secret callback into the SSL/TLS object. |
892 | | * |
893 | | * @param [in] ssl SSL/TLS object. |
894 | | * @param [in] ctx Context for ECC shared secret callback. |
895 | | */ |
896 | | void wolfSSL_SetEccSharedSecretCtx(WOLFSSL* ssl, void *ctx) |
897 | | { |
898 | | if (ssl != NULL) { |
899 | | ssl->EccSharedSecretCtx = ctx; |
900 | | } |
901 | | } |
902 | | /* Get the context for ECC shared secret callback from the SSL/TLS object. |
903 | | * |
904 | | * @param [in] ssl SSL/TLS object. |
905 | | * @return Context for ECC shared secret callback. |
906 | | * @return NULL when ssl is NULL. |
907 | | */ |
908 | | void* wolfSSL_GetEccSharedSecretCtx(WOLFSSL* ssl) |
909 | | { |
910 | | void* ret; |
911 | | |
912 | | if (ssl == NULL) { |
913 | | ret = NULL; |
914 | | } |
915 | | else { |
916 | | ret = ssl->EccSharedSecretCtx; |
917 | | } |
918 | | |
919 | | return ret; |
920 | | } |
921 | | #endif /* HAVE_ECC */ |
922 | | |
923 | | #ifdef HAVE_ED25519 |
924 | | /* Set the Ed25519 sign callback into the SSL/TLS context. |
925 | | * |
926 | | * @param [in] ctx SSL/TLS context. |
927 | | * @param [in] cb Ed25519 sign callback. |
928 | | */ |
929 | | void wolfSSL_CTX_SetEd25519SignCb(WOLFSSL_CTX* ctx, CallbackEd25519Sign cb) |
930 | | { |
931 | | if (ctx != NULL) { |
932 | | ctx->Ed25519SignCb = cb; |
933 | | } |
934 | | } |
935 | | /* Set the context for Ed25519 sign callback into the SSL/TLS object. |
936 | | * |
937 | | * @param [in] ssl SSL/TLS object. |
938 | | * @param [in] ctx Context for Ed25519 sign callback. |
939 | | */ |
940 | | void wolfSSL_SetEd25519SignCtx(WOLFSSL* ssl, void *ctx) |
941 | | { |
942 | | if (ssl != NULL) { |
943 | | ssl->Ed25519SignCtx = ctx; |
944 | | } |
945 | | } |
946 | | /* Get the context for Ed25519 sign callback from the SSL/TLS object. |
947 | | * |
948 | | * @param [in] ssl SSL/TLS object. |
949 | | * @return Context for Ed25519 sign callback. |
950 | | * @return NULL when ssl is NULL. |
951 | | */ |
952 | | void* wolfSSL_GetEd25519SignCtx(WOLFSSL* ssl) |
953 | | { |
954 | | void* ret; |
955 | | |
956 | | if (ssl == NULL) { |
957 | | ret = NULL; |
958 | | } |
959 | | else { |
960 | | ret = ssl->Ed25519SignCtx; |
961 | | } |
962 | | |
963 | | return ret; |
964 | | } |
965 | | |
966 | | /* Set the Ed25519 verify callback into the SSL/TLS context. |
967 | | * |
968 | | * @param [in] ctx SSL/TLS context. |
969 | | * @param [in] cb Ed25519 verify callback. |
970 | | */ |
971 | | void wolfSSL_CTX_SetEd25519VerifyCb(WOLFSSL_CTX* ctx, CallbackEd25519Verify cb) |
972 | | { |
973 | | if (ctx != NULL) { |
974 | | ctx->Ed25519VerifyCb = cb; |
975 | | } |
976 | | } |
977 | | /* Set the context for Ed25519 verify callback into the SSL/TLS object. |
978 | | * |
979 | | * @param [in] ssl SSL/TLS object. |
980 | | * @param [in] ctx Context for Ed25519 verify callback. |
981 | | */ |
982 | | void wolfSSL_SetEd25519VerifyCtx(WOLFSSL* ssl, void *ctx) |
983 | | { |
984 | | if (ssl != NULL) { |
985 | | ssl->Ed25519VerifyCtx = ctx; |
986 | | } |
987 | | } |
988 | | /* Get the context for Ed25519 verify callback from the SSL/TLS object. |
989 | | * |
990 | | * @param [in] ssl SSL/TLS object. |
991 | | * @return Context for Ed25519 verify callback. |
992 | | * @return NULL when ssl is NULL. |
993 | | */ |
994 | | void* wolfSSL_GetEd25519VerifyCtx(WOLFSSL* ssl) |
995 | | { |
996 | | void* ret; |
997 | | |
998 | | if (ssl == NULL) { |
999 | | ret = NULL; |
1000 | | } |
1001 | | else { |
1002 | | ret = ssl->Ed25519VerifyCtx; |
1003 | | } |
1004 | | |
1005 | | return ret; |
1006 | | } |
1007 | | #endif /* HAVE_ED25519 */ |
1008 | | |
1009 | | #ifdef HAVE_CURVE25519 |
1010 | | /* Set the X25519 key generation callback into the SSL/TLS context. |
1011 | | * |
1012 | | * @param [in] ctx SSL/TLS context. |
1013 | | * @param [in] cb X25519 key generation callback. |
1014 | | */ |
1015 | | void wolfSSL_CTX_SetX25519KeyGenCb(WOLFSSL_CTX* ctx, CallbackX25519KeyGen cb) |
1016 | | { |
1017 | | if (ctx != NULL) { |
1018 | | ctx->X25519KeyGenCb = cb; |
1019 | | } |
1020 | | } |
1021 | | /* Set the context for X25519 key generation callback into the SSL/TLS object. |
1022 | | * |
1023 | | * @param [in] ssl SSL/TLS object. |
1024 | | * @param [in] ctx Context for X25519 key generation callback. |
1025 | | */ |
1026 | | void wolfSSL_SetX25519KeyGenCtx(WOLFSSL* ssl, void *ctx) |
1027 | | { |
1028 | | if (ssl != NULL) { |
1029 | | ssl->X25519KeyGenCtx = ctx; |
1030 | | } |
1031 | | } |
1032 | | /* Get the context for X25519 key generation callback from the SSL/TLS object. |
1033 | | * |
1034 | | * @param [in] ssl SSL/TLS object. |
1035 | | * @return Context for X25519 key generation callback. |
1036 | | * @return NULL when ssl is NULL. |
1037 | | */ |
1038 | | void* wolfSSL_GetX25519KeyGenCtx(WOLFSSL* ssl) |
1039 | | { |
1040 | | void* ret; |
1041 | | |
1042 | | if (ssl == NULL) { |
1043 | | ret = NULL; |
1044 | | } |
1045 | | else { |
1046 | | ret = ssl->X25519KeyGenCtx; |
1047 | | } |
1048 | | |
1049 | | return ret; |
1050 | | } |
1051 | | |
1052 | | /* Set the X25519 shared secret callback into the SSL/TLS context. |
1053 | | * |
1054 | | * @param [in] ctx SSL/TLS context. |
1055 | | * @param [in] cb X25519 shared secret callback. |
1056 | | */ |
1057 | | void wolfSSL_CTX_SetX25519SharedSecretCb(WOLFSSL_CTX* ctx, |
1058 | | CallbackX25519SharedSecret cb) |
1059 | | { |
1060 | | if (ctx != NULL) { |
1061 | | ctx->X25519SharedSecretCb = cb; |
1062 | | } |
1063 | | } |
1064 | | /* Set the context for X25519 shared secret callback into the SSL/TLS object. |
1065 | | * |
1066 | | * @param [in] ssl SSL/TLS object. |
1067 | | * @param [in] ctx Context for X25519 shared secret callback. |
1068 | | */ |
1069 | | void wolfSSL_SetX25519SharedSecretCtx(WOLFSSL* ssl, void *ctx) |
1070 | | { |
1071 | | if (ssl != NULL) { |
1072 | | ssl->X25519SharedSecretCtx = ctx; |
1073 | | } |
1074 | | } |
1075 | | /* Get the context for X25519 shared secret callback from the SSL/TLS object. |
1076 | | * |
1077 | | * @param [in] ssl SSL/TLS object. |
1078 | | * @return Context for X25519 shared secret callback. |
1079 | | * @return NULL when ssl is NULL. |
1080 | | */ |
1081 | | void* wolfSSL_GetX25519SharedSecretCtx(WOLFSSL* ssl) |
1082 | | { |
1083 | | void* ret; |
1084 | | |
1085 | | if (ssl == NULL) { |
1086 | | ret = NULL; |
1087 | | } |
1088 | | else { |
1089 | | ret = ssl->X25519SharedSecretCtx; |
1090 | | } |
1091 | | |
1092 | | return ret; |
1093 | | } |
1094 | | #endif /* HAVE_CURVE25519 */ |
1095 | | |
1096 | | #ifdef HAVE_ED448 |
1097 | | /* Set the Ed448 sign callback into the SSL/TLS context. |
1098 | | * |
1099 | | * @param [in] ctx SSL/TLS context. |
1100 | | * @param [in] cb Ed448 sign callback. |
1101 | | */ |
1102 | | void wolfSSL_CTX_SetEd448SignCb(WOLFSSL_CTX* ctx, CallbackEd448Sign cb) |
1103 | | { |
1104 | | if (ctx != NULL) { |
1105 | | ctx->Ed448SignCb = cb; |
1106 | | } |
1107 | | } |
1108 | | /* Set the context for Ed448 sign callback into the SSL/TLS object. |
1109 | | * |
1110 | | * @param [in] ssl SSL/TLS object. |
1111 | | * @param [in] ctx Context for Ed448 sign callback. |
1112 | | */ |
1113 | | void wolfSSL_SetEd448SignCtx(WOLFSSL* ssl, void *ctx) |
1114 | | { |
1115 | | if (ssl != NULL) { |
1116 | | ssl->Ed448SignCtx = ctx; |
1117 | | } |
1118 | | } |
1119 | | /* Get the context for Ed448 sign callback from the SSL/TLS object. |
1120 | | * |
1121 | | * @param [in] ssl SSL/TLS object. |
1122 | | * @return Context for Ed448 sign callback. |
1123 | | * @return NULL when ssl is NULL. |
1124 | | */ |
1125 | | void* wolfSSL_GetEd448SignCtx(WOLFSSL* ssl) |
1126 | | { |
1127 | | void* ret; |
1128 | | |
1129 | | if (ssl == NULL) { |
1130 | | ret = NULL; |
1131 | | } |
1132 | | else { |
1133 | | ret = ssl->Ed448SignCtx; |
1134 | | } |
1135 | | |
1136 | | return ret; |
1137 | | } |
1138 | | |
1139 | | /* Set the Ed448 verify callback into the SSL/TLS context. |
1140 | | * |
1141 | | * @param [in] ctx SSL/TLS context. |
1142 | | * @param [in] cb Ed448 verify callback. |
1143 | | */ |
1144 | | void wolfSSL_CTX_SetEd448VerifyCb(WOLFSSL_CTX* ctx, CallbackEd448Verify cb) |
1145 | | { |
1146 | | if (ctx != NULL) { |
1147 | | ctx->Ed448VerifyCb = cb; |
1148 | | } |
1149 | | } |
1150 | | /* Set the context for Ed448 verify callback into the SSL/TLS object. |
1151 | | * |
1152 | | * @param [in] ssl SSL/TLS object. |
1153 | | * @param [in] ctx Context for Ed448 verify callback. |
1154 | | */ |
1155 | | void wolfSSL_SetEd448VerifyCtx(WOLFSSL* ssl, void *ctx) |
1156 | | { |
1157 | | if (ssl != NULL) { |
1158 | | ssl->Ed448VerifyCtx = ctx; |
1159 | | } |
1160 | | } |
1161 | | /* Get the context for Ed448 verify callback from the SSL/TLS object. |
1162 | | * |
1163 | | * @param [in] ssl SSL/TLS object. |
1164 | | * @return Context for Ed448 verify callback. |
1165 | | * @return NULL when ssl is NULL. |
1166 | | */ |
1167 | | void* wolfSSL_GetEd448VerifyCtx(WOLFSSL* ssl) |
1168 | | { |
1169 | | void* ret; |
1170 | | |
1171 | | if (ssl == NULL) { |
1172 | | ret = NULL; |
1173 | | } |
1174 | | else { |
1175 | | ret = ssl->Ed448VerifyCtx; |
1176 | | } |
1177 | | |
1178 | | return ret; |
1179 | | } |
1180 | | #endif /* HAVE_ED448 */ |
1181 | | |
1182 | | #ifdef HAVE_CURVE448 |
1183 | | /* Set the X448 key generation callback into the SSL/TLS context. |
1184 | | * |
1185 | | * @param [in] ctx SSL/TLS context. |
1186 | | * @param [in] cb X448 key generation callback. |
1187 | | */ |
1188 | | void wolfSSL_CTX_SetX448KeyGenCb(WOLFSSL_CTX* ctx, |
1189 | | CallbackX448KeyGen cb) |
1190 | | { |
1191 | | if (ctx != NULL) { |
1192 | | ctx->X448KeyGenCb = cb; |
1193 | | } |
1194 | | } |
1195 | | /* Set the context for X448 key generation callback into the SSL/TLS object. |
1196 | | * |
1197 | | * @param [in] ssl SSL/TLS object. |
1198 | | * @param [in] ctx Context for X448 key generation callback. |
1199 | | */ |
1200 | | void wolfSSL_SetX448KeyGenCtx(WOLFSSL* ssl, void *ctx) |
1201 | | { |
1202 | | if (ssl != NULL) { |
1203 | | ssl->X448KeyGenCtx = ctx; |
1204 | | } |
1205 | | } |
1206 | | /* Get the context for X448 key generation callback from the SSL/TLS object. |
1207 | | * |
1208 | | * @param [in] ssl SSL/TLS object. |
1209 | | * @return Context for X448 key generation callback. |
1210 | | * @return NULL when ssl is NULL. |
1211 | | */ |
1212 | | void* wolfSSL_GetX448KeyGenCtx(WOLFSSL* ssl) |
1213 | | { |
1214 | | void* ret; |
1215 | | |
1216 | | if (ssl == NULL) { |
1217 | | ret = NULL; |
1218 | | } |
1219 | | else { |
1220 | | ret = ssl->X448KeyGenCtx; |
1221 | | } |
1222 | | |
1223 | | return ret; |
1224 | | } |
1225 | | |
1226 | | /* Set the X448 shared secret callback into the SSL/TLS context. |
1227 | | * |
1228 | | * @param [in] ctx SSL/TLS context. |
1229 | | * @param [in] cb X448 shared secret callback. |
1230 | | */ |
1231 | | void wolfSSL_CTX_SetX448SharedSecretCb(WOLFSSL_CTX* ctx, |
1232 | | CallbackX448SharedSecret cb) |
1233 | | { |
1234 | | if (ctx != NULL) { |
1235 | | ctx->X448SharedSecretCb = cb; |
1236 | | } |
1237 | | } |
1238 | | /* Set the context for X448 shared secret callback into the SSL/TLS object. |
1239 | | * |
1240 | | * @param [in] ssl SSL/TLS object. |
1241 | | * @param [in] ctx Context for X448 shared secret callback. |
1242 | | */ |
1243 | | void wolfSSL_SetX448SharedSecretCtx(WOLFSSL* ssl, void *ctx) |
1244 | | { |
1245 | | if (ssl != NULL) { |
1246 | | ssl->X448SharedSecretCtx = ctx; |
1247 | | } |
1248 | | } |
1249 | | /* Get the context for X448 shared secret callback from the SSL/TLS object. |
1250 | | * |
1251 | | * @param [in] ssl SSL/TLS object. |
1252 | | * @return Context for X448 shared secret callback. |
1253 | | * @return NULL when ssl is NULL. |
1254 | | */ |
1255 | | void* wolfSSL_GetX448SharedSecretCtx(WOLFSSL* ssl) |
1256 | | { |
1257 | | void* ret; |
1258 | | |
1259 | | if (ssl == NULL) { |
1260 | | ret = NULL; |
1261 | | } |
1262 | | else { |
1263 | | ret = ssl->X448SharedSecretCtx; |
1264 | | } |
1265 | | |
1266 | | return ret; |
1267 | | } |
1268 | | #endif /* HAVE_CURVE448 */ |
1269 | | |
1270 | | #ifndef NO_RSA |
1271 | | /* Set the RSA sign callback into the SSL/TLS context. |
1272 | | * |
1273 | | * @param [in] ctx SSL/TLS context. |
1274 | | * @param [in] cb RSA sign callback. |
1275 | | */ |
1276 | | void wolfSSL_CTX_SetRsaSignCb(WOLFSSL_CTX* ctx, CallbackRsaSign cb) |
1277 | | { |
1278 | | if (ctx != NULL) { |
1279 | | ctx->RsaSignCb = cb; |
1280 | | } |
1281 | | } |
1282 | | /* Set the RSA sign check callback into the SSL/TLS context. |
1283 | | * |
1284 | | * @param [in] ctx SSL/TLS context. |
1285 | | * @param [in] cb RSA sign check callback. |
1286 | | */ |
1287 | | void wolfSSL_CTX_SetRsaSignCheckCb(WOLFSSL_CTX* ctx, CallbackRsaVerify cb) |
1288 | | { |
1289 | | if (ctx != NULL) { |
1290 | | ctx->RsaSignCheckCb = cb; |
1291 | | } |
1292 | | } |
1293 | | /* Set the context for RSA sign callback into the SSL/TLS object. |
1294 | | * |
1295 | | * @param [in] ssl SSL/TLS object. |
1296 | | * @param [in] ctx Context for RSA sign callback. |
1297 | | */ |
1298 | | void wolfSSL_SetRsaSignCtx(WOLFSSL* ssl, void *ctx) |
1299 | | { |
1300 | | if (ssl != NULL) { |
1301 | | ssl->RsaSignCtx = ctx; |
1302 | | } |
1303 | | } |
1304 | | /* Get the context for RSA sign callback from the SSL/TLS object. |
1305 | | * |
1306 | | * @param [in] ssl SSL/TLS object. |
1307 | | * @return Context for RSA sign callback. |
1308 | | * @return NULL when ssl is NULL. |
1309 | | */ |
1310 | | void* wolfSSL_GetRsaSignCtx(WOLFSSL* ssl) |
1311 | | { |
1312 | | void* ret; |
1313 | | |
1314 | | if (ssl == NULL) { |
1315 | | ret = NULL; |
1316 | | } |
1317 | | else { |
1318 | | ret = ssl->RsaSignCtx; |
1319 | | } |
1320 | | |
1321 | | return ret; |
1322 | | } |
1323 | | |
1324 | | /* Set the RSA verify callback into the SSL/TLS context. |
1325 | | * |
1326 | | * @param [in] ctx SSL/TLS context. |
1327 | | * @param [in] cb RSA verify callback. |
1328 | | */ |
1329 | | void wolfSSL_CTX_SetRsaVerifyCb(WOLFSSL_CTX* ctx, CallbackRsaVerify cb) |
1330 | | { |
1331 | | if (ctx != NULL) { |
1332 | | ctx->RsaVerifyCb = cb; |
1333 | | } |
1334 | | } |
1335 | | /* Set the context for RSA verify callback into the SSL/TLS object. |
1336 | | * |
1337 | | * @param [in] ssl SSL/TLS object. |
1338 | | * @param [in] ctx Context for RSA verify callback. |
1339 | | */ |
1340 | | void wolfSSL_SetRsaVerifyCtx(WOLFSSL* ssl, void *ctx) |
1341 | | { |
1342 | | if (ssl != NULL) { |
1343 | | ssl->RsaVerifyCtx = ctx; |
1344 | | } |
1345 | | } |
1346 | | /* Get the context for RSA verify callback from the SSL/TLS object. |
1347 | | * |
1348 | | * @param [in] ssl SSL/TLS object. |
1349 | | * @return Context for RSA verify callback. |
1350 | | * @return NULL when ssl is NULL. |
1351 | | */ |
1352 | | void* wolfSSL_GetRsaVerifyCtx(WOLFSSL* ssl) |
1353 | | { |
1354 | | void* ret; |
1355 | | |
1356 | | if (ssl == NULL) { |
1357 | | ret = NULL; |
1358 | | } |
1359 | | else { |
1360 | | ret = ssl->RsaVerifyCtx; |
1361 | | } |
1362 | | |
1363 | | return ret; |
1364 | | } |
1365 | | |
1366 | | #ifdef WC_RSA_PSS |
1367 | | /* Set the RSA PSS sign callback into the SSL/TLS context. |
1368 | | * |
1369 | | * @param [in] ctx SSL/TLS context. |
1370 | | * @param [in] cb RSA PSS sign callback. |
1371 | | */ |
1372 | | void wolfSSL_CTX_SetRsaPssSignCb(WOLFSSL_CTX* ctx, CallbackRsaPssSign cb) |
1373 | | { |
1374 | | if (ctx != NULL) { |
1375 | | ctx->RsaPssSignCb = cb; |
1376 | | } |
1377 | | } |
1378 | | /* Set the RSA PSS sign check callback into the SSL/TLS context. |
1379 | | * |
1380 | | * @param [in] ctx SSL/TLS context. |
1381 | | * @param [in] cb RSA PSS sign check callback. |
1382 | | */ |
1383 | | void wolfSSL_CTX_SetRsaPssSignCheckCb(WOLFSSL_CTX* ctx, |
1384 | | CallbackRsaPssVerify cb) |
1385 | | { |
1386 | | if (ctx != NULL) { |
1387 | | ctx->RsaPssSignCheckCb = cb; |
1388 | | } |
1389 | | } |
1390 | | /* Set the context for RSA PSS sign callback into the SSL/TLS object. |
1391 | | * |
1392 | | * @param [in] ssl SSL/TLS object. |
1393 | | * @param [in] ctx Context for RSA PSS sign callback. |
1394 | | */ |
1395 | | void wolfSSL_SetRsaPssSignCtx(WOLFSSL* ssl, void *ctx) |
1396 | | { |
1397 | | if (ssl != NULL) { |
1398 | | ssl->RsaPssSignCtx = ctx; |
1399 | | } |
1400 | | } |
1401 | | /* Get the context for RSA PSS sign callback from the SSL/TLS object. |
1402 | | * |
1403 | | * @param [in] ssl SSL/TLS object. |
1404 | | * @return Context for RSA PSS sign callback. |
1405 | | * @return NULL when ssl is NULL. |
1406 | | */ |
1407 | | void* wolfSSL_GetRsaPssSignCtx(WOLFSSL* ssl) |
1408 | | { |
1409 | | void* ret; |
1410 | | |
1411 | | if (ssl == NULL) { |
1412 | | ret = NULL; |
1413 | | } |
1414 | | else { |
1415 | | ret = ssl->RsaPssSignCtx; |
1416 | | } |
1417 | | |
1418 | | return ret; |
1419 | | } |
1420 | | |
1421 | | /* Set the RSA PSS verify callback into the SSL/TLS context. |
1422 | | * |
1423 | | * @param [in] ctx SSL/TLS context. |
1424 | | * @param [in] cb RSA PSS verify callback. |
1425 | | */ |
1426 | | void wolfSSL_CTX_SetRsaPssVerifyCb(WOLFSSL_CTX* ctx, CallbackRsaPssVerify cb) |
1427 | | { |
1428 | | if (ctx != NULL) { |
1429 | | ctx->RsaPssVerifyCb = cb; |
1430 | | } |
1431 | | } |
1432 | | /* Set the context for RSA PSS verify callback into the SSL/TLS object. |
1433 | | * |
1434 | | * @param [in] ssl SSL/TLS object. |
1435 | | * @param [in] ctx Context for RSA PSS verify callback. |
1436 | | */ |
1437 | | void wolfSSL_SetRsaPssVerifyCtx(WOLFSSL* ssl, void *ctx) |
1438 | | { |
1439 | | if (ssl != NULL) { |
1440 | | ssl->RsaPssVerifyCtx = ctx; |
1441 | | } |
1442 | | } |
1443 | | /* Get the context for RSA PSS verify callback from the SSL/TLS object. |
1444 | | * |
1445 | | * @param [in] ssl SSL/TLS object. |
1446 | | * @return Context for RSA PSS verify callback. |
1447 | | * @return NULL when ssl is NULL. |
1448 | | */ |
1449 | | void* wolfSSL_GetRsaPssVerifyCtx(WOLFSSL* ssl) |
1450 | | { |
1451 | | void* ret; |
1452 | | |
1453 | | if (ssl == NULL) { |
1454 | | ret = NULL; |
1455 | | } |
1456 | | else { |
1457 | | ret = ssl->RsaPssVerifyCtx; |
1458 | | } |
1459 | | |
1460 | | return ret; |
1461 | | } |
1462 | | #endif /* WC_RSA_PSS */ |
1463 | | |
1464 | | /* Set the RSA encrypt callback into the SSL/TLS context. |
1465 | | * |
1466 | | * @param [in] ctx SSL/TLS context. |
1467 | | * @param [in] cb RSA encrypt callback. |
1468 | | */ |
1469 | | void wolfSSL_CTX_SetRsaEncCb(WOLFSSL_CTX* ctx, CallbackRsaEnc cb) |
1470 | | { |
1471 | | if (ctx != NULL) { |
1472 | | ctx->RsaEncCb = cb; |
1473 | | } |
1474 | | } |
1475 | | /* Set the context for RSA encrypt callback into the SSL/TLS object. |
1476 | | * |
1477 | | * @param [in] ssl SSL/TLS object. |
1478 | | * @param [in] ctx Context for RSA encrypt callback. |
1479 | | */ |
1480 | | void wolfSSL_SetRsaEncCtx(WOLFSSL* ssl, void *ctx) |
1481 | | { |
1482 | | if (ssl != NULL) { |
1483 | | ssl->RsaEncCtx = ctx; |
1484 | | } |
1485 | | } |
1486 | | /* Get the context for RSA encrypt callback from the SSL/TLS object. |
1487 | | * |
1488 | | * @param [in] ssl SSL/TLS object. |
1489 | | * @return Context for RSA encrypt callback. |
1490 | | * @return NULL when ssl is NULL. |
1491 | | */ |
1492 | | void* wolfSSL_GetRsaEncCtx(WOLFSSL* ssl) |
1493 | | { |
1494 | | void* ret; |
1495 | | |
1496 | | if (ssl == NULL) { |
1497 | | ret = NULL; |
1498 | | } |
1499 | | else { |
1500 | | ret = ssl->RsaEncCtx; |
1501 | | } |
1502 | | |
1503 | | return ret; |
1504 | | } |
1505 | | |
1506 | | /* Set the RSA decrypt callback into the SSL/TLS context. |
1507 | | * |
1508 | | * @param [in] ctx SSL/TLS context. |
1509 | | * @param [in] cb RSA decrypt callback. |
1510 | | */ |
1511 | | void wolfSSL_CTX_SetRsaDecCb(WOLFSSL_CTX* ctx, CallbackRsaDec cb) |
1512 | | { |
1513 | | if (ctx != NULL) { |
1514 | | ctx->RsaDecCb = cb; |
1515 | | } |
1516 | | } |
1517 | | /* Set the context for RSA decrypt callback into the SSL/TLS object. |
1518 | | * |
1519 | | * @param [in] ssl SSL/TLS object. |
1520 | | * @param [in] ctx Context for RSA decrypt callback. |
1521 | | */ |
1522 | | void wolfSSL_SetRsaDecCtx(WOLFSSL* ssl, void *ctx) |
1523 | | { |
1524 | | if (ssl != NULL) { |
1525 | | ssl->RsaDecCtx = ctx; |
1526 | | } |
1527 | | } |
1528 | | /* Get the context for RSA decrypt callback from the SSL/TLS object. |
1529 | | * |
1530 | | * @param [in] ssl SSL/TLS object. |
1531 | | * @return Context for RSA decrypt callback. |
1532 | | * @return NULL when ssl is NULL. |
1533 | | */ |
1534 | | void* wolfSSL_GetRsaDecCtx(WOLFSSL* ssl) |
1535 | | { |
1536 | | void* ret; |
1537 | | |
1538 | | if (ssl == NULL) { |
1539 | | ret = NULL; |
1540 | | } |
1541 | | else { |
1542 | | ret = ssl->RsaDecCtx; |
1543 | | } |
1544 | | |
1545 | | return ret; |
1546 | | } |
1547 | | #endif /* NO_RSA */ |
1548 | | |
1549 | | #endif /* HAVE_PK_CALLBACKS */ |
1550 | | |
1551 | | #endif /* !NO_CERTS */ |
1552 | | |
1553 | | #if defined(HAVE_PK_CALLBACKS) && !defined(NO_DH) |
1554 | | /* Set the DH key pair generation callback into the SSL/TLS context. |
1555 | | * |
1556 | | * @param [in] ctx SSL/TLS context. |
1557 | | * @param [in] cb DH key pair generation callback. |
1558 | | */ |
1559 | | void wolfSSL_CTX_SetDhGenerateKeyPair(WOLFSSL_CTX* ctx, |
1560 | | CallbackDhGenerateKeyPair cb) |
1561 | | { |
1562 | | if (ctx != NULL) { |
1563 | | ctx->DhGenerateKeyPairCb = cb; |
1564 | | } |
1565 | | } |
1566 | | /* Set the DH key agree callback into the SSL/TLS context. |
1567 | | * |
1568 | | * @param [in] ctx SSL/TLS context. |
1569 | | * @param [in] cb DH key agree callback. |
1570 | | */ |
1571 | | void wolfSSL_CTX_SetDhAgreeCb(WOLFSSL_CTX* ctx, CallbackDhAgree cb) |
1572 | | { |
1573 | | if (ctx != NULL) { |
1574 | | ctx->DhAgreeCb = cb; |
1575 | | } |
1576 | | } |
1577 | | /* Set the context for DH key agree callback into the SSL/TLS object. |
1578 | | * |
1579 | | * @param [in] ssl SSL/TLS object. |
1580 | | * @param [in] ctx Context for DH key agree callback. |
1581 | | */ |
1582 | | void wolfSSL_SetDhAgreeCtx(WOLFSSL* ssl, void *ctx) |
1583 | | { |
1584 | | if (ssl != NULL) { |
1585 | | ssl->DhAgreeCtx = ctx; |
1586 | | } |
1587 | | } |
1588 | | /* Get the context for DH key ageww callback from the SSL/TLS object. |
1589 | | * |
1590 | | * @param [in] ssl SSL/TLS object. |
1591 | | * @return Context for DH key agree callback. |
1592 | | * @return NULL when ssl is NULL. |
1593 | | */ |
1594 | | void* wolfSSL_GetDhAgreeCtx(WOLFSSL* ssl) |
1595 | | { |
1596 | | void* ret; |
1597 | | |
1598 | | if (ssl == NULL) { |
1599 | | ret = NULL; |
1600 | | } |
1601 | | else { |
1602 | | ret = ssl->DhAgreeCtx; |
1603 | | } |
1604 | | |
1605 | | return ret; |
1606 | | } |
1607 | | #endif /* HAVE_PK_CALLBACKS && !NO_DH */ |
1608 | | |
1609 | | #endif /* !WOLFSSL_SSL_API_PK_INCLUDED */ |