/src/wolfssl/src/ssl_load.c
Line | Count | Source |
1 | | /* ssl_load.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 | | /* |
25 | | * WOLFSSL_SYS_CA_CERTS |
26 | | * Enables ability to load system CA certs from the OS via |
27 | | * wolfSSL_CTX_load_system_CA_certs. |
28 | | */ |
29 | | |
30 | | #ifdef WOLFSSL_SYS_CA_CERTS |
31 | | /* Will be turned off automatically when NO_FILESYSTEM is defined |
32 | | * for non Mac/Windows systems */ |
33 | | |
34 | | #ifdef _WIN32 |
35 | | #define _WINSOCKAPI_ /* block inclusion of winsock.h header file */ |
36 | | #include <windows.h> |
37 | | #include <wincrypt.h> |
38 | | #undef _WINSOCKAPI_ /* undefine it for MINGW winsock2.h header file */ |
39 | | |
40 | | /* mingw gcc does not support pragma comment, and the |
41 | | * linking with crypt32 is handled in configure.ac */ |
42 | | #if !defined(__MINGW32__) && !defined(__MINGW64__) |
43 | | #pragma comment(lib, "crypt32") |
44 | | #endif |
45 | | #endif |
46 | | |
47 | | #if defined(__APPLE__) |
48 | | #if defined(HAVE_SECURITY_SECTRUSTSETTINGS_H) |
49 | | #include <Security/SecTrustSettings.h> |
50 | | #endif /* HAVE_SECURITY_SECTRUSTSETTINGS_H */ |
51 | | #ifdef WOLFSSL_TEST_APPLE_NATIVE_CERT_VALIDATION |
52 | | #include <CoreFoundation/CoreFoundation.h> |
53 | | #endif /* WOLFSSL_TEST_APPLE_NATIVE_CERT_VALIDATION */ |
54 | | #endif /* __APPLE__ */ |
55 | | |
56 | | #endif /* WOLFSSL_SYS_CA_CERTS */ |
57 | | |
58 | | #if !defined(WOLFSSL_SSL_LOAD_INCLUDED) |
59 | | #ifndef WOLFSSL_IGNORE_FILE_WARN |
60 | | #warning ssl_load.c does not need to be compiled separately from ssl.c |
61 | | #endif |
62 | | #else |
63 | | |
64 | | #include <wolfssl/wolfcrypt/logging.h> |
65 | | |
66 | | #if defined(HAVE_SESSION_TICKET) || !defined(NO_PSK) |
67 | | /* PSK field of context when it exists. */ |
68 | | #define CTX_HAVE_PSK(ctx) (ctx)->havePSK |
69 | | /* PSK field of ssl when it exists. */ |
70 | | #define SSL_HAVE_PSK(ssl) (ssl)->options.havePSK |
71 | | #else |
72 | | /* Have PSK value when no field. */ |
73 | 0 | #define CTX_HAVE_PSK(ctx) 0 |
74 | | /* Have PSK value when no field. */ |
75 | 0 | #define SSL_HAVE_PSK(ssl) 0 |
76 | | #endif |
77 | | #ifdef NO_RSA |
78 | | /* Boolean for RSA available. */ |
79 | | #define WOLFSSL_HAVE_RSA 0 |
80 | | #else |
81 | | /* Boolean for RSA available. */ |
82 | 0 | #define WOLFSSL_HAVE_RSA 1 |
83 | | #endif |
84 | | #ifndef NO_CERTS |
85 | | /* Private key size from ssl. */ |
86 | 0 | #define SSL_KEY_SZ(ssl) (ssl)->buffers.keySz |
87 | | #else |
88 | | /* Private key size not available. */ |
89 | | #define SSL_KEY_SZ(ssl) 0 |
90 | | #endif |
91 | | #ifdef HAVE_ANON |
92 | | /* Anonymous ciphersuite allowed field in context. */ |
93 | | #define CTX_USE_ANON(ctx) (ctx)->useAnon |
94 | | #else |
95 | | /* Anonymous ciphersuite allowed field not in context. */ |
96 | 0 | #define CTX_USE_ANON(ctx) 0 |
97 | | #endif |
98 | | |
99 | | #ifdef HAVE_PK_CALLBACKS |
100 | | #define WOLFSSL_IS_PRIV_PK_SET(ctx, ssl) \ |
101 | | wolfSSL_CTX_IsPrivatePkSet(((ssl) == NULL) ? (ctx) : (ssl)->ctx) |
102 | | #else |
103 | | #define WOLFSSL_IS_PRIV_PK_SET(ctx, ssl) 0 |
104 | | #endif |
105 | | |
106 | | /* Get the heap from the context or the ssl depending on which is available. */ |
107 | | #define WOLFSSL_HEAP(ctx, ssl) \ |
108 | 0 | (((ctx) != NULL) ? (ctx)->heap : (((ssl) != NULL) ? (ssl)->heap : NULL)) |
109 | | |
110 | | |
111 | | #ifndef NO_CERTS |
112 | | |
113 | | /* Get DER encoding from data in a buffer as a DerBuffer. |
114 | | * |
115 | | * @param [in] buff Buffer containing data. |
116 | | * @param [in] len Length of data in buffer. |
117 | | * @param [in] format Format of data: |
118 | | * WOLFSSL_FILETYPE_PEM or WOLFSSL_FILETYPE_ASN1. |
119 | | * @param [in] type Type of data: |
120 | | * CERT_TYPE, CA_TYPE, TRUSTED_PEER_TYPE, |
121 | | * PRIVATEKEY_TYPE or ALT_PRIVATEKEY_TYPE. |
122 | | * @param [in, out] info Info for encryption. |
123 | | * @param [in] heap Dynamic memory allocation hint. |
124 | | * @param [out] der Holds DER encoded data. |
125 | | * @param [out] algId Algorithm identifier for private keys. |
126 | | * @return 0 on success. |
127 | | * @return NOT_COMPILED_IN when format is PEM and PEM not supported. |
128 | | * @return ASN_PARSE_E when format is ASN.1 and invalid DER encoding. |
129 | | * @return MEMORY_E when dynamic memory allocation fails. |
130 | | */ |
131 | | static int DataToDerBuffer(const unsigned char* buff, word32 len, int format, |
132 | | int type, EncryptedInfo* info, void* heap, DerBuffer** der, int* algId) |
133 | 0 | { |
134 | 0 | int ret; |
135 | |
|
136 | 0 | info->consumed = 0; |
137 | | |
138 | | /* Data in buffer has PEM format - extract DER data. */ |
139 | 0 | if (format == WOLFSSL_FILETYPE_PEM) { |
140 | 0 | #ifdef WOLFSSL_PEM_TO_DER |
141 | 0 | ret = PemToDer(buff, (long)(len), type, der, heap, info, algId); |
142 | 0 | if (ret != 0) { |
143 | 0 | FreeDer(der); |
144 | 0 | } |
145 | | #else |
146 | | (void)algId; |
147 | | ret = NOT_COMPILED_IN; |
148 | | #endif |
149 | 0 | } |
150 | | /* Data in buffer is ASN.1 format - get first SEQ or OCT into der. */ |
151 | 0 | else { |
152 | | /* Get length of SEQ including header. */ |
153 | 0 | if ((info->consumed = wolfssl_der_length(buff, (int)len)) > 0) { |
154 | 0 | ret = 0; |
155 | 0 | } |
156 | 0 | else { |
157 | 0 | ret = ASN_PARSE_E; |
158 | 0 | } |
159 | |
|
160 | 0 | if (info->consumed > (int)len) { |
161 | 0 | ret = ASN_PARSE_E; |
162 | 0 | } |
163 | 0 | if (ret == 0) { |
164 | 0 | ret = AllocCopyDer(der, buff, (word32)info->consumed, type, heap); |
165 | 0 | } |
166 | 0 | } |
167 | |
|
168 | 0 | return ret; |
169 | 0 | } |
170 | | |
171 | | /* Process a user's certificate. |
172 | | * |
173 | | * Puts the 3-byte length before certificate data as required for TLS. |
174 | | * CA certificates are added to the certificate manager. |
175 | | * |
176 | | * @param [in] cm Certificate manager. |
177 | | * @param [in, out] pDer DER encoded data. |
178 | | * @param [in] type Type of data. Valid values: |
179 | | * CERT_TYPE, CA_TYPE or TRUSTED_PEER_TYPE. |
180 | | * @param [in] verify How to verify certificate. |
181 | | * @param [out] chainBuffer Buffer to hold chain of certificates. |
182 | | * @param [in, out] pIdx On in, current index into chainBuffer. |
183 | | * On out, index after certificate added. |
184 | | * @param [in] bufferSz Size of buffer in bytes. |
185 | | * @return 0 on success. |
186 | | * @return BUFFER_E if chain buffer not big enough to hold certificate. |
187 | | */ |
188 | | static int ProcessUserCert(WOLFSSL_CERT_MANAGER* cm, DerBuffer** pDer, |
189 | | int type, int verify, byte* chainBuffer, word32* pIdx, word32 bufferSz) |
190 | 0 | { |
191 | 0 | int ret = 0; |
192 | 0 | word32 idx = *pIdx; |
193 | 0 | DerBuffer* der = *pDer; |
194 | | |
195 | | /* Check there is space for certificate in chainBuffer. */ |
196 | 0 | if ((ret == 0) && ((idx + der->length + CERT_HEADER_SZ) > bufferSz)) { |
197 | 0 | WOLFSSL_MSG(" Cert Chain bigger than buffer. " |
198 | 0 | "Consider increasing MAX_CHAIN_DEPTH"); |
199 | 0 | ret = BUFFER_E; |
200 | 0 | } |
201 | 0 | if (ret == 0) { |
202 | | /* 3-byte length. */ |
203 | 0 | c32to24(der->length, &chainBuffer[idx]); |
204 | 0 | idx += CERT_HEADER_SZ; |
205 | | /* Add complete DER encoded certificate. */ |
206 | 0 | XMEMCPY(&chainBuffer[idx], der->buffer, der->length); |
207 | 0 | idx += der->length; |
208 | |
|
209 | 0 | if (type == CA_TYPE) { |
210 | | /* Add CA to certificate manager */ |
211 | 0 | ret = AddCA(cm, pDer, WOLFSSL_USER_CA, verify); |
212 | 0 | if (ret == 1) { |
213 | 0 | ret = 0; |
214 | 0 | } |
215 | 0 | } |
216 | 0 | } |
217 | | |
218 | | /* Update the index into chainBuffer. */ |
219 | 0 | *pIdx = idx; |
220 | 0 | return ret; |
221 | 0 | } |
222 | | |
223 | | /* Store the certificate chain buffer aganst WOLFSSL_CTX or WOLFSSL object. |
224 | | * |
225 | | * @param [in, out] ctx SSL context object. |
226 | | * @param [in, out] ssl SSL object. |
227 | | * @param [in] chainBuffer Buffer containing chain of certificates. |
228 | | * @param [in] len Length, in bytes, of data in buffer. |
229 | | * @param [in] cnt Number of certificates in chain. |
230 | | * @param [in] type Type of data. Valid values: |
231 | | * CERT_TYPE, CA_TYPE or CHAIN_CERT_TYPE. |
232 | | * @param [in] heap Dynamic memory allocation hint. |
233 | | * @return 0 on success. |
234 | | * @return MEMORY_E when dynamic memory allocation fails. |
235 | | */ |
236 | | static int ProcessUserChainRetain(WOLFSSL_CTX* ctx, WOLFSSL* ssl, |
237 | | const byte* chainBuffer, word32 len, int cnt, int type, void* heap) |
238 | 0 | { |
239 | 0 | int ret = 0; |
240 | |
|
241 | 0 | (void)cnt; |
242 | | |
243 | | /* Store in SSL object if available. */ |
244 | 0 | if (ssl != NULL) { |
245 | | /* Dispose of old chain if not reference to context's. */ |
246 | 0 | if (ssl->buffers.weOwnCertChain) { |
247 | 0 | FreeDer(&ssl->buffers.certChain); |
248 | 0 | } |
249 | | /* Allocate and copy the buffer into SSL object. */ |
250 | 0 | ret = AllocCopyDer(&ssl->buffers.certChain, chainBuffer, len, type, |
251 | 0 | heap); |
252 | 0 | ssl->buffers.weOwnCertChain = (ret == 0); |
253 | | /* Update count of certificates in chain. */ |
254 | 0 | ssl->buffers.certChainCnt = cnt; |
255 | 0 | } |
256 | | /* Store in SSL context object if available. */ |
257 | 0 | else if (ctx != NULL) { |
258 | | /* Dispose of old chain and allocate and copy in new chain. */ |
259 | 0 | FreeDer(&ctx->certChain); |
260 | | /* Allocate and copy the buffer into SSL context object. */ |
261 | 0 | ret = AllocCopyDer(&ctx->certChain, chainBuffer, len, type, heap); |
262 | | /* Update count of certificates in chain. */ |
263 | 0 | ctx->certChainCnt = cnt; |
264 | 0 | } |
265 | |
|
266 | 0 | return ret; |
267 | 0 | } |
268 | | |
269 | | /* Process user cert chain to pass during the TLS handshake. |
270 | | * |
271 | | * If not a certificate type then data is ignored. |
272 | | * |
273 | | * @param [in, out] ctx SSL context object. |
274 | | * @param [in, out] ssl SSL object. |
275 | | * @param [in] buff Buffer holding certificates. |
276 | | * @param [in] sz Length of data in buffer. |
277 | | * @param [in] format Format of the certificate: |
278 | | * WOLFSSL_FILETYPE_PEM or WOLFSSL_FILETYPE_ASN1 |
279 | | * @param [in] type Type of certificate: |
280 | | * CA_TYPE, CERT_TYPE or CHAIN_CERT_TYPE |
281 | | * @param [out] used Number of bytes from buff used. |
282 | | * @param [in, out] info Encryption information. |
283 | | * @param [in] verify How to verify certificate. |
284 | | * @return 0 on success. |
285 | | * @return BAD_FUNC_ARG when type is CA_TYPE and ctx is NULL. |
286 | | * @return MEMORY_E when dynamic memory allocation fails. |
287 | | */ |
288 | | static int ProcessUserChain(WOLFSSL_CTX* ctx, WOLFSSL* ssl, |
289 | | const unsigned char* buff, long sz, int format, int type, long* used, |
290 | | EncryptedInfo* info, int verify) |
291 | 0 | { |
292 | 0 | int ret = 0; |
293 | 0 | void* heap = WOLFSSL_HEAP(ctx, ssl); |
294 | |
|
295 | 0 | WOLFSSL_ENTER("ProcessUserChain"); |
296 | | |
297 | | /* Check we haven't consumed all the data. */ |
298 | 0 | if (info->consumed >= sz) { |
299 | 0 | WOLFSSL_MSG("Already consumed data"); |
300 | 0 | } |
301 | 0 | else { |
302 | 0 | #ifndef WOLFSSL_SMALL_STACK |
303 | 0 | byte stackBuffer[FILE_BUFFER_SIZE]; |
304 | 0 | #endif |
305 | 0 | StaticBuffer chain; |
306 | 0 | long consumed = info->consumed; |
307 | 0 | word32 idx = 0; |
308 | 0 | int gotOne = 0; |
309 | 0 | int cnt = 0; |
310 | | /* Calculate max possible size, including max headers */ |
311 | 0 | long maxSz = (sz - consumed) + (CERT_HEADER_SZ * MAX_CHAIN_DEPTH); |
312 | | |
313 | | /* Setup buffer to hold chain. */ |
314 | | #ifdef WOLFSSL_SMALL_STACK |
315 | | static_buffer_init(&chain); |
316 | | #else |
317 | 0 | static_buffer_init(&chain, stackBuffer, FILE_BUFFER_SIZE); |
318 | 0 | #endif |
319 | | /* Make buffer big enough to support maximum size. */ |
320 | 0 | ret = static_buffer_set_size(&chain, (word32)maxSz, heap, |
321 | 0 | DYNAMIC_TYPE_FILE); |
322 | |
|
323 | 0 | WOLFSSL_MSG("Processing Cert Chain"); |
324 | | /* Keep parsing certificates will data available. */ |
325 | 0 | while ((ret == 0) && (consumed < sz)) { |
326 | 0 | DerBuffer* part = NULL; |
327 | | |
328 | | /* Get a certificate as DER. */ |
329 | 0 | ret = DataToDerBuffer(buff + consumed, (word32)(sz - consumed), |
330 | 0 | format, type, info, heap, &part, NULL); |
331 | 0 | if (ret == 0) { |
332 | | /* Enforce maximum chain depth once a real cert is parsed. */ |
333 | 0 | if (cnt >= MAX_CHAIN_DEPTH) { |
334 | 0 | WOLFSSL_MSG("Chain depth limit reached"); |
335 | 0 | FreeDer(&part); |
336 | 0 | ret = MAX_CHAIN_ERROR; |
337 | 0 | break; |
338 | 0 | } |
339 | | /* Process the user certificate. */ |
340 | 0 | ret = ProcessUserCert(ctx->cm, &part, type, verify, |
341 | 0 | chain.buffer, &idx, (word32)maxSz); |
342 | 0 | } |
343 | | /* PEM may have trailing data that can be ignored. */ |
344 | 0 | if ((ret == WC_NO_ERR_TRACE(ASN_NO_PEM_HEADER)) && gotOne) { |
345 | 0 | WOLFSSL_MSG("We got one good cert, so stuff at end ok"); |
346 | 0 | ret = 0; |
347 | 0 | break; |
348 | 0 | } |
349 | | /* Certificate data handled. */ |
350 | 0 | FreeDer(&part); |
351 | |
|
352 | 0 | if (ret == 0) { |
353 | | /* Update consumed length. */ |
354 | 0 | consumed += info->consumed; |
355 | 0 | WOLFSSL_MSG(" Consumed another Cert in Chain"); |
356 | | /* Update whether we got a user certificate. */ |
357 | 0 | gotOne |= (type != CA_TYPE); |
358 | | /* Update count of certificates added to chain. */ |
359 | 0 | cnt++; |
360 | 0 | } |
361 | 0 | } |
362 | 0 | if (used != NULL) { |
363 | | /* Return the total consumed length. */ |
364 | 0 | *used = consumed; |
365 | 0 | } |
366 | | |
367 | | /* Check whether there is data in the chain buffer. */ |
368 | 0 | if ((ret == 0) && (idx > 0)) { |
369 | | /* Put the chain buffer against the SSL or SSL context object. */ |
370 | 0 | ret = ProcessUserChainRetain(ctx, ssl, chain.buffer, idx, cnt, type, |
371 | 0 | heap); |
372 | 0 | } |
373 | | |
374 | | /* Dispose of chain buffer. */ |
375 | 0 | static_buffer_free(&chain, heap, DYNAMIC_TYPE_FILE); |
376 | 0 | } |
377 | |
|
378 | 0 | WOLFSSL_LEAVE("ProcessUserChain", ret); |
379 | 0 | return ret; |
380 | 0 | } |
381 | | |
382 | | #ifndef NO_RSA |
383 | | #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && \ |
384 | | (HAVE_FIPS_VERSION > 2)) |
385 | | /* See if DER data is an RSA private key. |
386 | | * |
387 | | * Checks size meets minimum RSA key size. |
388 | | * This implementation uses less dynamic memory. |
389 | | * |
390 | | * @param [in, out] ctx SSL context object. |
391 | | * @param [in, out] ssl SSL object. |
392 | | * @param [in] der DER encoding. |
393 | | * @param [in, out] keyFormat On in, expected format. 0 means unknown. |
394 | | * @param [in] devId Device identifier. |
395 | | * @param [out] keyType Type of key. |
396 | | * @param [out] keySize Size of key. |
397 | | * @return 0 on success or not an RSA key and format unknown. |
398 | | * @return RSA_KEY_SIZE_E when key size doesn't meet minimum required. |
399 | | */ |
400 | | static int ProcessBufferTryDecodeRsa(WOLFSSL_CTX* ctx, WOLFSSL* ssl, |
401 | | DerBuffer* der, int* keyFormat, int devId, byte* keyType, int* keySize) |
402 | 0 | { |
403 | 0 | int ret; |
404 | 0 | word32 idx; |
405 | 0 | int keySz = 0; |
406 | |
|
407 | 0 | (void)devId; |
408 | | |
409 | | /* Validate we have an RSA private key and get key size. */ |
410 | 0 | idx = 0; |
411 | 0 | ret = wc_RsaPrivateKeyValidate(der->buffer, &idx, &keySz, der->length); |
412 | | #ifdef WOLF_PRIVATE_KEY_ID |
413 | | /* If that didn't work then maybe a public key if device ID or callback. */ |
414 | | if ((ret != 0) && ((devId != INVALID_DEVID) || |
415 | | WOLFSSL_IS_PRIV_PK_SET(ctx, ssl))) { |
416 | | word32 nSz; |
417 | | |
418 | | /* Decode as an RSA public key. */ |
419 | | idx = 0; |
420 | | ret = wc_RsaPublicKeyDecode_ex(der->buffer, &idx, der->length, NULL, |
421 | | &nSz, NULL, NULL); |
422 | | if (ret == 0) { |
423 | | keySz = (int)nSz; |
424 | | } |
425 | | } |
426 | | #endif |
427 | 0 | if (ret == 0) { |
428 | | /* Get the minimum RSA key size from SSL or SSL context object. */ |
429 | 0 | int minRsaSz = ssl ? ssl->options.minRsaKeySz : ctx->minRsaKeySz; |
430 | | |
431 | | /* Format, type and size are known. */ |
432 | 0 | *keyFormat = RSAk; |
433 | 0 | *keyType = rsa_sa_algo; |
434 | 0 | *keySize = keySz; |
435 | | |
436 | | /* Check that the size of the RSA key is enough. */ |
437 | 0 | if (keySz < minRsaSz) { |
438 | 0 | WOLFSSL_MSG("Private Key size too small"); |
439 | 0 | ret = RSA_KEY_SIZE_E; |
440 | 0 | } |
441 | | /* No static ECC key possible. */ |
442 | 0 | if ((ssl != NULL) && (ssl->options.side == WOLFSSL_SERVER_END)) { |
443 | 0 | ssl->options.haveStaticECC = 0; |
444 | 0 | } |
445 | 0 | } |
446 | | /* Not an RSA key but check whether we know what it is. */ |
447 | 0 | else if (*keyFormat == 0) { |
448 | 0 | WOLFSSL_MSG("Not an RSA key"); |
449 | | /* Format unknown so keep trying. */ |
450 | 0 | ret = 0; |
451 | 0 | } |
452 | |
|
453 | 0 | return ret; |
454 | 0 | } |
455 | | #else |
456 | | /* See if DER data is an RSA private key. |
457 | | * |
458 | | * Checks size meets minimum RSA key size. |
459 | | * This implementation uses more dynamic memory but supports older FIPS. |
460 | | * |
461 | | * @param [in, out] ctx SSL context object. |
462 | | * @param [in, out] ssl SSL object. |
463 | | * @param [in] der DER encoding. |
464 | | * @param [in, out] keyFormat On in, expected format. 0 means unknown. |
465 | | * @param [in] heap Dynamic memory allocation hint. |
466 | | * @param [in] devId Device identifier. |
467 | | * @param [out] keyType Type of key. |
468 | | * @param [out] keySize Size of key. |
469 | | * @return 0 on success or not an RSA key and format unknown. |
470 | | * @return RSA_KEY_SIZE_E when key size doesn't meet minimum required. |
471 | | */ |
472 | | static int ProcessBufferTryDecodeRsa(WOLFSSL_CTX* ctx, WOLFSSL* ssl, |
473 | | DerBuffer* der, int* keyFormat, void* heap, int devId, byte* keyType, |
474 | | int* keySize) |
475 | | { |
476 | | int ret; |
477 | | word32 idx; |
478 | | /* make sure RSA key can be used */ |
479 | | WC_DECLARE_VAR(key, RsaKey, 1, 0); |
480 | | |
481 | | /* Allocate an RSA key to parse into so we can get size. */ |
482 | | WC_ALLOC_VAR_EX(key, RsaKey, 1, heap, DYNAMIC_TYPE_RSA, |
483 | | return MEMORY_E); |
484 | | |
485 | | /* Initialize the RSA key. */ |
486 | | ret = wc_InitRsaKey_ex(key, heap, devId); |
487 | | if (ret == 0) { |
488 | | /* Check we have an RSA private key. */ |
489 | | idx = 0; |
490 | | ret = wc_RsaPrivateKeyDecode(der->buffer, &idx, key, der->length); |
491 | | #ifdef WOLF_PRIVATE_KEY_ID |
492 | | /* If that didn't work then maybe a public key if device ID or callback. |
493 | | */ |
494 | | if ((ret != 0) && ((devId != INVALID_DEVID) || |
495 | | WOLFSSL_IS_PRIV_PK_SET(ctx, ssl))) { |
496 | | /* If that didn't work then maybe a public key if device ID or |
497 | | * callback. */ |
498 | | idx = 0; |
499 | | ret = wc_RsaPublicKeyDecode(der->buffer, &idx, key, der->length); |
500 | | } |
501 | | #endif |
502 | | if (ret == 0) { |
503 | | /* Get the minimum RSA key size from SSL or SSL context object. */ |
504 | | int minRsaSz = ssl ? ssl->options.minRsaKeySz : ctx->minRsaKeySz; |
505 | | int keySz = wc_RsaEncryptSize((RsaKey*)key); |
506 | | |
507 | | /* Format is known. */ |
508 | | *keyFormat = RSAk; |
509 | | *keyType = rsa_sa_algo; |
510 | | *keySize = keySz; |
511 | | |
512 | | /* Check that the size of the RSA key is enough. */ |
513 | | if (keySz < minRsaSz) { |
514 | | WOLFSSL_MSG("Private Key size too small"); |
515 | | ret = RSA_KEY_SIZE_E; |
516 | | } |
517 | | /* No static ECC key possible. */ |
518 | | if ((ssl != NULL) && (ssl->options.side == WOLFSSL_SERVER_END)) { |
519 | | ssl->options.haveStaticECC = 0; |
520 | | } |
521 | | } |
522 | | /* Not an RSA key but check whether we know what it is. */ |
523 | | else if (*keyFormat == 0) { |
524 | | WOLFSSL_MSG("Not an RSA key"); |
525 | | /* Format unknown so keep trying. */ |
526 | | ret = 0; |
527 | | } |
528 | | |
529 | | /* Free dynamically allocated data in key. */ |
530 | | wc_FreeRsaKey(key); |
531 | | } |
532 | | |
533 | | WC_FREE_VAR_EX(key, heap, DYNAMIC_TYPE_RSA); |
534 | | |
535 | | return ret; |
536 | | } |
537 | | #endif |
538 | | #endif /* !NO_RSA */ |
539 | | |
540 | | #ifdef HAVE_ECC |
541 | | /* See if DER data is an ECC private key. |
542 | | * |
543 | | * Checks size meets minimum ECC key size. |
544 | | * |
545 | | * @param [in, out] ctx SSL context object. |
546 | | * @param [in, out] ssl SSL object. |
547 | | * @param [in] der DER encoding. |
548 | | * @param [in, out] keyFormat On in, expected format. 0 means unknown. |
549 | | * @param [in] heap Dynamic memory allocation hint. |
550 | | * @param [in] devId Device identifier. |
551 | | * @param [out] keyType Type of key. |
552 | | * @param [out] keySize Size of key. |
553 | | * @return 0 on success or not an ECC key and format unknown. |
554 | | * @return ECC_KEY_SIZE_E when ECC key size doesn't meet minimum required. |
555 | | */ |
556 | | static int ProcessBufferTryDecodeEcc(WOLFSSL_CTX* ctx, WOLFSSL* ssl, |
557 | | DerBuffer* der, int* keyFormat, void* heap, int devId, byte* keyType, |
558 | | int* keySize) |
559 | 0 | { |
560 | 0 | int ret = 0; |
561 | 0 | word32 idx; |
562 | | /* make sure ECC key can be used */ |
563 | 0 | WC_DECLARE_VAR(key, ecc_key, 1, 0); |
564 | | |
565 | | /* Allocate an ECC key to parse into. */ |
566 | 0 | WC_ALLOC_VAR_EX(key, ecc_key, 1, heap, DYNAMIC_TYPE_ECC, |
567 | 0 | return MEMORY_E); |
568 | | |
569 | | /* Initialize ECC key. */ |
570 | 0 | if (wc_ecc_init_ex(key, heap, devId) == 0) { |
571 | | /* Decode as an ECC private key. */ |
572 | 0 | idx = 0; |
573 | 0 | ret = wc_EccPrivateKeyDecode(der->buffer, &idx, key, der->length); |
574 | | #ifdef WOLF_PRIVATE_KEY_ID |
575 | | /* If that didn't work then maybe a public key if device ID or callback. |
576 | | */ |
577 | | if ((ret != 0) && ((devId != INVALID_DEVID) || |
578 | | WOLFSSL_IS_PRIV_PK_SET(ctx, ssl))) { |
579 | | /* Decode as an ECC public key. */ |
580 | | idx = 0; |
581 | | ret = wc_EccPublicKeyDecode(der->buffer, &idx, key, der->length); |
582 | | } |
583 | | #endif |
584 | | #ifdef WOLFSSL_SM2 |
585 | | if (*keyFormat == SM2k) { |
586 | | ret = wc_ecc_set_curve(key, WOLFSSL_SM2_KEY_BITS / 8, |
587 | | ECC_SM2P256V1); |
588 | | } |
589 | | #endif |
590 | 0 | if (ret == 0) { |
591 | | /* Get the minimum ECC key size from SSL or SSL context object. */ |
592 | 0 | int minKeySz = ssl ? ssl->options.minEccKeySz : ctx->minEccKeySz; |
593 | 0 | int keySz = wc_ecc_size(key); |
594 | | |
595 | | /* Format is known. */ |
596 | 0 | *keyFormat = ECDSAk; |
597 | | #ifdef WOLFSSL_SM2 |
598 | | if (key->dp->id == ECC_SM2P256V1) { |
599 | | *keyType = sm2_sa_algo; |
600 | | } |
601 | | else |
602 | | #endif |
603 | 0 | { |
604 | 0 | *keyType = ecc_dsa_sa_algo; |
605 | 0 | } |
606 | 0 | *keySize = keySz; |
607 | | |
608 | | /* Check that the size of the ECC key is enough. */ |
609 | 0 | if (keySz < minKeySz) { |
610 | 0 | WOLFSSL_MSG("ECC private key too small"); |
611 | 0 | ret = ECC_KEY_SIZE_E; |
612 | 0 | } |
613 | | /* Static ECC key possible. */ |
614 | 0 | if (ssl) { |
615 | 0 | ssl->options.haveStaticECC = 1; |
616 | 0 | } |
617 | 0 | else { |
618 | 0 | ctx->haveStaticECC = 1; |
619 | 0 | } |
620 | 0 | } |
621 | | /* Not an ECC key but check whether we know what it is. */ |
622 | 0 | else if (*keyFormat == 0) { |
623 | 0 | WOLFSSL_MSG("Not an ECC key"); |
624 | | /* Format unknown so keep trying. */ |
625 | 0 | ret = 0; |
626 | 0 | } |
627 | | |
628 | | /* Free dynamically allocated data in key. */ |
629 | 0 | wc_ecc_free(key); |
630 | 0 | } |
631 | |
|
632 | 0 | WC_FREE_VAR_EX(key, heap, DYNAMIC_TYPE_ECC); |
633 | 0 | return ret; |
634 | 0 | } |
635 | | #endif /* HAVE_ECC */ |
636 | | |
637 | | #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_IMPORT) |
638 | | /* See if DER data is an Ed25519 private key. |
639 | | * |
640 | | * Checks size meets minimum ECC key size. |
641 | | * |
642 | | * @param [in, out] ctx SSL context object. |
643 | | * @param [in, out] ssl SSL object. |
644 | | * @param [in] der DER encoding. |
645 | | * @param [in, out] keyFormat On in, expected format. 0 means unknown. |
646 | | * @param [in] heap Dynamic memory allocation hint. |
647 | | * @param [in] devId Device identifier. |
648 | | * @param [out] keyType Type of key. |
649 | | * @param [out] keySize Size of key. |
650 | | * @return 0 on success or not an Ed25519 key and format unknown. |
651 | | * @return ECC_KEY_SIZE_E when key size doesn't meet minimum required. |
652 | | */ |
653 | | static int ProcessBufferTryDecodeEd25519(WOLFSSL_CTX* ctx, WOLFSSL* ssl, |
654 | | DerBuffer* der, int* keyFormat, void* heap, int devId, byte* keyType, |
655 | | int* keySize) |
656 | | { |
657 | | int ret; |
658 | | word32 idx; |
659 | | /* make sure Ed25519 key can be used */ |
660 | | WC_DECLARE_VAR(key, ed25519_key, 1, 0); |
661 | | |
662 | | /* Allocate an Ed25519 key to parse into. */ |
663 | | WC_ALLOC_VAR_EX(key, ed25519_key, 1, heap, DYNAMIC_TYPE_ED25519, |
664 | | return MEMORY_E); |
665 | | |
666 | | /* Initialize Ed25519 key. */ |
667 | | ret = wc_ed25519_init_ex(key, heap, devId); |
668 | | if (ret == 0) { |
669 | | /* Decode as an Ed25519 private key. */ |
670 | | idx = 0; |
671 | | ret = wc_Ed25519PrivateKeyDecode(der->buffer, &idx, key, der->length); |
672 | | #ifdef WOLF_PRIVATE_KEY_ID |
673 | | /* If that didn't work then maybe a public key if device ID or callback. |
674 | | */ |
675 | | if ((ret != 0) && ((devId != INVALID_DEVID) || |
676 | | WOLFSSL_IS_PRIV_PK_SET(ctx, ssl))) { |
677 | | /* Decode as an Ed25519 public key. */ |
678 | | idx = 0; |
679 | | ret = wc_Ed25519PublicKeyDecode(der->buffer, &idx, key, |
680 | | der->length); |
681 | | } |
682 | | #endif |
683 | | if (ret == 0) { |
684 | | /* Get the minimum ECC key size from SSL or SSL context object. */ |
685 | | int minKeySz = ssl ? ssl->options.minEccKeySz : ctx->minEccKeySz; |
686 | | |
687 | | /* Format is known. */ |
688 | | *keyFormat = ED25519k; |
689 | | *keyType = ed25519_sa_algo; |
690 | | *keySize = ED25519_KEY_SIZE; |
691 | | |
692 | | /* Check that the size of the ECC key is enough. */ |
693 | | if (ED25519_KEY_SIZE < minKeySz) { |
694 | | WOLFSSL_MSG("ED25519 private key too small"); |
695 | | ret = ECC_KEY_SIZE_E; |
696 | | } |
697 | | if (ssl != NULL) { |
698 | | #if !defined(WOLFSSL_NO_CLIENT_AUTH) && !defined(NO_ED25519_CLIENT_AUTH) |
699 | | /* Ed25519 requires caching enabled for tracking message |
700 | | * hash used in EdDSA_Update for signing */ |
701 | | ssl->options.cacheMessages = 1; |
702 | | #endif |
703 | | } |
704 | | } |
705 | | /* Not an Ed25519 key but check whether we know what it is. */ |
706 | | else if (*keyFormat == 0) { |
707 | | WOLFSSL_MSG("Not an Ed25519 key"); |
708 | | /* Format unknown so keep trying. */ |
709 | | ret = 0; |
710 | | } |
711 | | |
712 | | /* Free dynamically allocated data in key. */ |
713 | | wc_ed25519_free(key); |
714 | | } |
715 | | |
716 | | WC_FREE_VAR_EX(key, heap, DYNAMIC_TYPE_ED25519); |
717 | | return ret; |
718 | | } |
719 | | #endif /* HAVE_ED25519 && HAVE_ED25519_KEY_IMPORT */ |
720 | | |
721 | | #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_IMPORT) |
722 | | /* See if DER data is an Ed448 private key. |
723 | | * |
724 | | * Checks size meets minimum ECC key size. |
725 | | * |
726 | | * @param [in, out] ctx SSL context object. |
727 | | * @param [in, out] ssl SSL object. |
728 | | * @param [in] der DER encoding. |
729 | | * @param [in, out] keyFormat On in, expected format. 0 means unknown. |
730 | | * @param [in] heap Dynamic memory allocation hint. |
731 | | * @param [in] devId Device identifier. |
732 | | * @param [out] keyType Type of key. |
733 | | * @param [out] keySize Size of key. |
734 | | * @return 0 on success or not an Ed448 key and format unknown. |
735 | | * @return ECC_KEY_SIZE_E when key size doesn't meet minimum required. |
736 | | */ |
737 | | static int ProcessBufferTryDecodeEd448(WOLFSSL_CTX* ctx, WOLFSSL* ssl, |
738 | | DerBuffer* der, int* keyFormat, void* heap, int devId, byte* keyType, |
739 | | int* keySize) |
740 | | { |
741 | | int ret; |
742 | | word32 idx; |
743 | | /* make sure Ed448 key can be used */ |
744 | | WC_DECLARE_VAR(key, ed448_key, 1, 0); |
745 | | |
746 | | /* Allocate an Ed448 key to parse into. */ |
747 | | WC_ALLOC_VAR_EX(key, ed448_key, 1, heap, DYNAMIC_TYPE_ED448, |
748 | | return MEMORY_E); |
749 | | |
750 | | /* Initialize Ed448 key. */ |
751 | | ret = wc_ed448_init_ex(key, heap, devId); |
752 | | if (ret == 0) { |
753 | | /* Decode as an Ed448 private key. */ |
754 | | idx = 0; |
755 | | ret = wc_Ed448PrivateKeyDecode(der->buffer, &idx, key, der->length); |
756 | | #ifdef WOLF_PRIVATE_KEY_ID |
757 | | /* If that didn't work then maybe a public key if device ID or callback. |
758 | | */ |
759 | | if ((ret != 0) && ((devId != INVALID_DEVID) || |
760 | | WOLFSSL_IS_PRIV_PK_SET(ctx, ssl))) { |
761 | | /* Decode as an Ed448 public key. */ |
762 | | idx = 0; |
763 | | ret = wc_Ed448PublicKeyDecode(der->buffer, &idx, key, der->length); |
764 | | } |
765 | | #endif |
766 | | if (ret == 0) { |
767 | | /* Get the minimum ECC key size from SSL or SSL context object. */ |
768 | | int minKeySz = ssl ? ssl->options.minEccKeySz : ctx->minEccKeySz; |
769 | | |
770 | | /* Format is known. */ |
771 | | *keyFormat = ED448k; |
772 | | *keyType = ed448_sa_algo; |
773 | | *keySize = ED448_KEY_SIZE; |
774 | | |
775 | | /* Check that the size of the ECC key is enough. */ |
776 | | if (ED448_KEY_SIZE < minKeySz) { |
777 | | WOLFSSL_MSG("ED448 private key too small"); |
778 | | ret = ECC_KEY_SIZE_E; |
779 | | } |
780 | | #if !defined(WOLFSSL_NO_CLIENT_AUTH) && !defined(NO_ED448_CLIENT_AUTH) |
781 | | if (ssl != NULL) { |
782 | | /* Ed448 requires caching enabled for tracking message |
783 | | * hash used in EdDSA_Update for signing */ |
784 | | ssl->options.cacheMessages = 1; |
785 | | } |
786 | | #endif |
787 | | } |
788 | | /* Not an Ed448 key but check whether we know what it is. */ |
789 | | else if (*keyFormat == 0) { |
790 | | WOLFSSL_MSG("Not an Ed448 key"); |
791 | | /* Format unknown so keep trying. */ |
792 | | ret = 0; |
793 | | } |
794 | | |
795 | | /* Free dynamically allocated data in key. */ |
796 | | wc_ed448_free(key); |
797 | | } |
798 | | |
799 | | WC_FREE_VAR_EX(key, heap, DYNAMIC_TYPE_ED448); |
800 | | return ret; |
801 | | } |
802 | | #endif /* HAVE_ED448 && HAVE_ED448_KEY_IMPORT */ |
803 | | |
804 | | #if defined(HAVE_FALCON) |
805 | | /* See if DER data is an Falcon private key. |
806 | | * |
807 | | * Checks size meets minimum Falcon key size. |
808 | | * |
809 | | * @param [in, out] ctx SSL context object. |
810 | | * @param [in, out] ssl SSL object. |
811 | | * @param [in] der DER encoding. |
812 | | * @param [in, out] keyFormat On in, expected format. 0 means unknown. |
813 | | * @param [in] heap Dynamic memory allocation hint. |
814 | | * @param [in] devId Device identifier. |
815 | | * @param [out] keyType Type of key. |
816 | | * @param [out] keySize Size of key. |
817 | | * @return 0 on success or not an Falcon key and format unknown. |
818 | | * @return FALCON_KEY_SIZE_E when key size doesn't meet minimum required. |
819 | | */ |
820 | | static int ProcessBufferTryDecodeFalcon(WOLFSSL_CTX* ctx, WOLFSSL* ssl, |
821 | | DerBuffer* der, int* keyFormat, void* heap, byte* keyType, int* keySize) |
822 | | { |
823 | | int ret; |
824 | | int inited = 0; |
825 | | falcon_key* key; |
826 | | |
827 | | /* Allocate a Falcon key to parse into. */ |
828 | | key = (falcon_key*)XMALLOC(sizeof(falcon_key), heap, DYNAMIC_TYPE_FALCON); |
829 | | if (key == NULL) { |
830 | | return MEMORY_E; |
831 | | } |
832 | | |
833 | | /* Initialize Falcon key. The struct came from 'heap' and, with |
834 | | * WOLFSSL_FALCON_DYNAMIC_KEYS, so must its encoded-key buffers. */ |
835 | | ret = wc_falcon_init_ex(key, heap, INVALID_DEVID); |
836 | | if (ret == 0) { |
837 | | byte level = 0; |
838 | | word32 idx; |
839 | | |
840 | | inited = 1; |
841 | | if (*keyFormat == FALCON_LEVEL1k) { |
842 | | level = 1; |
843 | | } |
844 | | else if (*keyFormat == FALCON_LEVEL5k) { |
845 | | level = 5; |
846 | | } |
847 | | |
848 | | if (level != 0) { |
849 | | /* Caller told us the level via the OID sum. */ |
850 | | ret = wc_falcon_set_level(key, level); |
851 | | if (ret == 0) { |
852 | | idx = 0; |
853 | | ret = wc_Falcon_PrivateKeyDecode(der->buffer, &idx, key, |
854 | | der->length); |
855 | | } |
856 | | } |
857 | | else if (*keyFormat == 0) { |
858 | | int lvlRet; |
859 | | |
860 | | /* Key format unknown. Try both levels; the expected OID inside |
861 | | * wc_Falcon_PrivateKeyDecode rejects non-matching DER. Re-init |
862 | | * between attempts so a partial first decode can't leave stale |
863 | | * bytes in key->k / key->p. */ |
864 | | idx = 0; |
865 | | lvlRet = wc_falcon_set_level(key, 1); |
866 | | if (lvlRet == WC_NO_ERR_TRACE(MEMORY_E)) { |
867 | | wc_falcon_free(key); |
868 | | XFREE(key, heap, DYNAMIC_TYPE_FALCON); |
869 | | return MEMORY_E; |
870 | | } |
871 | | if ((lvlRet == 0) && |
872 | | (wc_Falcon_PrivateKeyDecode(der->buffer, &idx, key, |
873 | | der->length) == 0)) { |
874 | | level = 1; |
875 | | } |
876 | | else { |
877 | | /* Only an attempt that ran can have dirtied the key: a level |
878 | | * that was not built returns before touching it. */ |
879 | | if (lvlRet == 0) { |
880 | | wc_falcon_free(key); |
881 | | if (wc_falcon_init_ex(key, heap, INVALID_DEVID) != 0) { |
882 | | XFREE(key, heap, DYNAMIC_TYPE_FALCON); |
883 | | return MEMORY_E; |
884 | | } |
885 | | } |
886 | | idx = 0; |
887 | | lvlRet = wc_falcon_set_level(key, 5); |
888 | | if (lvlRet == WC_NO_ERR_TRACE(MEMORY_E)) { |
889 | | wc_falcon_free(key); |
890 | | XFREE(key, heap, DYNAMIC_TYPE_FALCON); |
891 | | return MEMORY_E; |
892 | | } |
893 | | if ((lvlRet == 0) && |
894 | | (wc_Falcon_PrivateKeyDecode(der->buffer, &idx, key, |
895 | | der->length) == 0)) { |
896 | | level = 5; |
897 | | } |
898 | | } |
899 | | if (level == 0) { |
900 | | /* Not a Falcon key; let caller try another algorithm. */ |
901 | | WOLFSSL_MSG("Not a Falcon key"); |
902 | | wc_falcon_free(key); |
903 | | XFREE(key, heap, DYNAMIC_TYPE_FALCON); |
904 | | return 0; |
905 | | } |
906 | | ret = 0; |
907 | | } |
908 | | else { |
909 | | ret = ALGO_ID_E; |
910 | | } |
911 | | } |
912 | | |
913 | | if (ret == 0) { |
914 | | /* Get the minimum Falcon key size from SSL or SSL context object. */ |
915 | | int minKeySz = ssl ? ssl->options.minFalconKeySz : |
916 | | ctx->minFalconKeySz; |
917 | | |
918 | | if (key->level == 1) { |
919 | | *keyFormat = FALCON_LEVEL1k; |
920 | | *keyType = falcon_level1_sa_algo; |
921 | | *keySize = FALCON_LEVEL1_KEY_SIZE; |
922 | | } |
923 | | else { |
924 | | *keyFormat = FALCON_LEVEL5k; |
925 | | *keyType = falcon_level5_sa_algo; |
926 | | *keySize = FALCON_LEVEL5_KEY_SIZE; |
927 | | } |
928 | | |
929 | | /* Check that the size of the Falcon key is enough. */ |
930 | | if (*keySize < minKeySz) { |
931 | | WOLFSSL_MSG("Falcon private key too small"); |
932 | | ret = FALCON_KEY_SIZE_E; |
933 | | } |
934 | | } |
935 | | else if ((ret == WC_NO_ERR_TRACE(ALGO_ID_E)) && (*keyFormat == 0)) { |
936 | | WOLFSSL_MSG("Not a Falcon key"); |
937 | | /* Format unknown so keep trying. */ |
938 | | ret = 0; |
939 | | } |
940 | | |
941 | | /* Dispose of allocated key. With WOLFSSL_FALCON_DYNAMIC_KEYS the structure |
942 | | * owns heap buffers, so XFREE alone would leak. */ |
943 | | if (inited) { |
944 | | wc_falcon_free(key); |
945 | | } |
946 | | XFREE(key, heap, DYNAMIC_TYPE_FALCON); |
947 | | return ret; |
948 | | } |
949 | | #endif |
950 | | |
951 | | #if defined(WOLFSSL_HAVE_MLDSA) && !defined(WOLFSSL_MLDSA_NO_SIGN) && \ |
952 | | !defined(WOLFSSL_MLDSA_NO_ASN1) |
953 | | /* See if DER data is an Dilithium private key. |
954 | | * |
955 | | * Checks size meets minimum Falcon key size. |
956 | | * |
957 | | * @param [in, out] ctx SSL context object. |
958 | | * @param [in, out] ssl SSL object. |
959 | | * @param [in] der DER encoding. |
960 | | * @param [in, out] keyFormat On in, expected format. 0 means unknown. |
961 | | * @param [in] heap Dynamic memory allocation hint. |
962 | | * @param [in] devId Device identifier. |
963 | | * @param [out] keyType Type of key. |
964 | | * @param [out] keySize Size of key. |
965 | | * @return 0 on success or not a Dilithium key and format unknown. |
966 | | * @return MLDSA_KEY_SIZE_E when key size doesn't meet minimum required. |
967 | | */ |
968 | | static int ProcessBufferTryDecodeMlDsa(WOLFSSL_CTX* ctx, WOLFSSL* ssl, |
969 | | DerBuffer* der, int* keyFormat, void* heap, byte* keyType, int* keySize) |
970 | | { |
971 | | int ret; |
972 | | word32 idx; |
973 | | wc_MlDsaKey* key; |
974 | | int keyFormatTemp = 0; |
975 | | int keyTypeTemp = 0; |
976 | | int keySizeTemp = 0; |
977 | | |
978 | | /* Allocate a Dilithium key to parse into. */ |
979 | | key = (wc_MlDsaKey*)XMALLOC(sizeof(wc_MlDsaKey), heap, |
980 | | DYNAMIC_TYPE_MLDSA); |
981 | | if (key == NULL) { |
982 | | return MEMORY_E; |
983 | | } |
984 | | |
985 | | /* Initialize ML-DSA key. */ |
986 | | ret = wc_MlDsaKey_Init(key, NULL, INVALID_DEVID); |
987 | | if (ret == 0) { |
988 | | /* Decode as an ML-DSA private key. The FIPS wrapper for |
989 | | * wc_MlDsaKey_ImportPrivRaw gates on the per-thread |
990 | | * privateKeyReadEnable flag, which is unset by default in any |
991 | | * thread that hasn't called PRIVATE_KEY_UNLOCK(). Without the |
992 | | * bracket, loading an ML-DSA private key from a worker thread |
993 | | * fails with FIPS_PRIVATE_KEY_LOCKED_E. */ |
994 | | idx = 0; |
995 | | PRIVATE_KEY_UNLOCK(); |
996 | | ret = wc_MlDsaKey_PrivateKeyDecode(key, der->buffer, |
997 | | der->length, &idx); |
998 | | PRIVATE_KEY_LOCK(); |
999 | | if (ret == 0) { |
1000 | | ret = mldsa_get_oid_sum(key, &keyFormatTemp); |
1001 | | if (ret == 0) { |
1002 | | /* Format is known. */ |
1003 | | #if defined(WOLFSSL_MLDSA_FIPS204_DRAFT) |
1004 | | if (keyFormatTemp == DILITHIUM_LEVEL2k) { |
1005 | | keyTypeTemp = mldsa_44_sa_algo; |
1006 | | keySizeTemp = WC_MLDSA_44_KEY_SIZE; |
1007 | | } |
1008 | | else if (keyFormatTemp == DILITHIUM_LEVEL3k) { |
1009 | | keyTypeTemp = mldsa_65_sa_algo; |
1010 | | keySizeTemp = WC_MLDSA_65_KEY_SIZE; |
1011 | | } |
1012 | | else if (keyFormatTemp == DILITHIUM_LEVEL5k) { |
1013 | | keyTypeTemp = mldsa_87_sa_algo; |
1014 | | keySizeTemp = WC_MLDSA_87_KEY_SIZE; |
1015 | | } |
1016 | | else |
1017 | | #endif /* WOLFSSL_MLDSA_FIPS204_DRAFT */ |
1018 | | if (keyFormatTemp == ML_DSA_44k) { |
1019 | | keyTypeTemp = mldsa_44_sa_algo; |
1020 | | keySizeTemp = WC_MLDSA_44_KEY_SIZE; |
1021 | | } |
1022 | | else if (keyFormatTemp == ML_DSA_65k) { |
1023 | | keyTypeTemp = mldsa_65_sa_algo; |
1024 | | keySizeTemp = WC_MLDSA_65_KEY_SIZE; |
1025 | | } |
1026 | | else if (keyFormatTemp == ML_DSA_87k) { |
1027 | | keyTypeTemp = mldsa_87_sa_algo; |
1028 | | keySizeTemp = WC_MLDSA_87_KEY_SIZE; |
1029 | | } |
1030 | | else { |
1031 | | ret = ALGO_ID_E; |
1032 | | } |
1033 | | } |
1034 | | |
1035 | | if (ret == 0) { |
1036 | | /* Get the minimum Dilithium key size from SSL or SSL context |
1037 | | * object. */ |
1038 | | int minKeySz = ssl ? ssl->options.minMlDsaKeySz : |
1039 | | ctx->minMlDsaKeySz; |
1040 | | |
1041 | | /* Check that the size of the Dilithium key is enough. */ |
1042 | | if (keySizeTemp < minKeySz) { |
1043 | | WOLFSSL_MSG("ML-DSA private key too small"); |
1044 | | ret = MLDSA_KEY_SIZE_E; |
1045 | | } |
1046 | | } |
1047 | | |
1048 | | if (ret == 0) { |
1049 | | *keyFormat = keyFormatTemp; |
1050 | | *keyType = keyTypeTemp; |
1051 | | *keySize = keySizeTemp; |
1052 | | } |
1053 | | } |
1054 | | else if (*keyFormat == 0) { |
1055 | | WOLFSSL_MSG("Not an ML-DSA key"); |
1056 | | /* Unknown format wasn't dilithium, so keep trying other formats. */ |
1057 | | ret = 0; |
1058 | | } |
1059 | | |
1060 | | /* Free dynamically allocated data in key. */ |
1061 | | wc_MlDsaKey_Free(key); |
1062 | | } |
1063 | | |
1064 | | /* Dispose of allocated key. */ |
1065 | | XFREE(key, heap, DYNAMIC_TYPE_MLDSA); |
1066 | | return ret; |
1067 | | } |
1068 | | #endif /* WOLFSSL_HAVE_MLDSA */ |
1069 | | |
1070 | | #if defined(WOLFSSL_HAVE_SLHDSA) && !defined(WOLFSSL_SLHDSA_VERIFY_ONLY) |
1071 | | /* Try to decode the DER encoding as an SLH-DSA private key. |
1072 | | * |
1073 | | * @param [in] ctx SSL context object. |
1074 | | * @param [in] ssl SSL object. |
1075 | | * @param [in] der DER encoding. |
1076 | | * @param [in, out] keyFormat On in, expected format. 0 means unknown. |
1077 | | * @param [in] heap Dynamic memory allocation hint. |
1078 | | * @param [out] keyType Type of key (an slhdsa_*_sa_algo). |
1079 | | * @param [out] keySize Size of the private key. |
1080 | | * @return 0 on success or when not an SLH-DSA key (with keyFormat 0). |
1081 | | */ |
1082 | | static int ProcessBufferTryDecodeSlhDsa(WOLFSSL_CTX* ctx, WOLFSSL* ssl, |
1083 | | DerBuffer* der, int* keyFormat, void* heap, byte* keyType, int* keySize) |
1084 | | { |
1085 | | int ret; |
1086 | | word32 idx; |
1087 | | SlhDsaKey* key; |
1088 | | int keyTypeTemp = 0; |
1089 | | int keySizeTemp = 0; |
1090 | | |
1091 | | (void)ctx; |
1092 | | (void)ssl; |
1093 | | |
1094 | | /* Allocate an SLH-DSA key to parse into. */ |
1095 | | key = (SlhDsaKey*)XMALLOC(sizeof(SlhDsaKey), heap, DYNAMIC_TYPE_SLHDSA); |
1096 | | if (key == NULL) { |
1097 | | return MEMORY_E; |
1098 | | } |
1099 | | |
1100 | | /* wc_SlhDsaKey_Init returns NOT_COMPILED_IN before it zeroes the key, so |
1101 | | * clear it here to keep the unconditional Free below well defined. */ |
1102 | | XMEMSET(key, 0, sizeof(SlhDsaKey)); |
1103 | | |
1104 | | /* Initialise with an always-present placeholder parameter set; |
1105 | | * wc_SlhDsaKey_PrivateKeyDecode selects the real one from the key's |
1106 | | * algorithm OID. */ |
1107 | | ret = wc_SlhDsaKey_Init(key, WC_SLHDSA_DEFAULT_PARAM, heap, INVALID_DEVID); |
1108 | | if (ret == 0) { |
1109 | | idx = 0; |
1110 | | PRIVATE_KEY_UNLOCK(); |
1111 | | ret = wc_SlhDsaKey_PrivateKeyDecode(der->buffer, &idx, key, |
1112 | | der->length); |
1113 | | PRIVATE_KEY_LOCK(); |
1114 | | if (ret == 0) { |
1115 | | int param = (int)key->params->param; |
1116 | | |
1117 | | keyTypeTemp = SlhDsaParamToType(param); |
1118 | | if (keyTypeTemp == (byte)invalid_sa_algo) { |
1119 | | ret = ALGO_ID_E; |
1120 | | } |
1121 | | else { |
1122 | | keySizeTemp = wc_SlhDsaKey_PrivateSize(key); |
1123 | | if (keySizeTemp <= 0) { |
1124 | | ret = ALGO_ID_E; |
1125 | | } |
1126 | | } |
1127 | | |
1128 | | if (ret == 0) { |
1129 | | int oidSum = wc_SlhDsaParamToOid((enum SlhDsaParam)param); |
1130 | | |
1131 | | /* Negative for an unknown or disabled parameter set; storing it |
1132 | | * would leave a nonsense key format behind a zero return. */ |
1133 | | if (oidSum <= 0) { |
1134 | | ret = ALGO_ID_E; |
1135 | | } |
1136 | | else { |
1137 | | *keyFormat = oidSum; |
1138 | | *keyType = (byte)keyTypeTemp; |
1139 | | *keySize = keySizeTemp; |
1140 | | } |
1141 | | } |
1142 | | } |
1143 | | else if (*keyFormat == 0) { |
1144 | | WOLFSSL_MSG("Not an SLH-DSA key"); |
1145 | | /* Unknown format wasn't SLH-DSA, so keep trying other formats. */ |
1146 | | ret = 0; |
1147 | | } |
1148 | | } |
1149 | | |
1150 | | /* Free any data allocated by wc_SlhDsaKey_Init(). This also runs when Init |
1151 | | * failed part way through, releasing any hash objects allocated before the |
1152 | | * failure; it is a no-op once the key's parameter set has been cleared. */ |
1153 | | wc_SlhDsaKey_Free(key); |
1154 | | |
1155 | | /* Dispose of allocated key. */ |
1156 | | XFREE(key, heap, DYNAMIC_TYPE_SLHDSA); |
1157 | | return ret; |
1158 | | } |
1159 | | #endif /* WOLFSSL_HAVE_SLHDSA */ |
1160 | | |
1161 | | /* Try to decode DER data is a known private key. |
1162 | | * |
1163 | | * Checks size meets minimum for key type. |
1164 | | * |
1165 | | * @param [in, out] ctx SSL context object. |
1166 | | * @param [in, out] ssl SSL object. |
1167 | | * @param [in] der DER encoding. |
1168 | | * @param [in, out] keyFormat On in, expected format. 0 means unknown. |
1169 | | * @param [in] heap Dynamic memory allocation hint. |
1170 | | * @param [out] type Type of key: |
1171 | | * PRIVATEKEY_TYPE or ALT_PRIVATEKEY_TYPE. |
1172 | | * @return 0 on success. |
1173 | | * @return BAD_FUNC_ARG when der or keyFormat is NULL. |
1174 | | * @return BAD_FUNC_ARG when ctx and ssl are NULL. |
1175 | | * @return WOLFSSL_BAD_FILE when unable to identify the key format. |
1176 | | */ |
1177 | | static int ProcessBufferTryDecode(WOLFSSL_CTX* ctx, WOLFSSL* ssl, |
1178 | | DerBuffer* der, int* keyFormat, void* heap, int type) |
1179 | 0 | { |
1180 | 0 | int ret = 0; |
1181 | 0 | int devId = wolfSSL_CTX_GetDevId(ctx, ssl); |
1182 | 0 | byte* keyType = NULL; |
1183 | 0 | int* keySz = NULL; |
1184 | 0 | int matchAnyKey = 0; |
1185 | |
|
1186 | 0 | (void)heap; |
1187 | 0 | (void)devId; |
1188 | 0 | (void)type; |
1189 | | /* Only the per-algorithm decoders below read these back. */ |
1190 | 0 | (void)keyType; |
1191 | 0 | (void)keySz; |
1192 | | |
1193 | | /* Validate parameters. */ |
1194 | 0 | if ((der == NULL) || (keyFormat == NULL)) { |
1195 | 0 | ret = BAD_FUNC_ARG; |
1196 | 0 | } |
1197 | | /* Must have an SSL context or SSL object to use. */ |
1198 | 0 | if ((ret == 0) && (ctx == NULL) && (ssl == NULL)) { |
1199 | 0 | ret = BAD_FUNC_ARG; |
1200 | 0 | } |
1201 | |
|
1202 | 0 | if (ret == 0) { |
1203 | | /* Determine where to put key type and size in SSL or context object. */ |
1204 | | #ifdef WOLFSSL_DUAL_ALG_CERTS |
1205 | | if (type == ALT_PRIVATEKEY_TYPE) { |
1206 | | if (ssl != NULL) { |
1207 | | keyType = &ssl->buffers.altKeyType; |
1208 | | keySz = &ssl->buffers.altKeySz; |
1209 | | } |
1210 | | else { |
1211 | | keyType = &ctx->altPrivateKeyType; |
1212 | | keySz = &ctx->altPrivateKeySz; |
1213 | | } |
1214 | | } |
1215 | | else |
1216 | | #endif |
1217 | | /* Type is PRIVATEKEY_TYPE. */ |
1218 | 0 | if (ssl != NULL) { |
1219 | 0 | keyType = &ssl->buffers.keyType; |
1220 | 0 | keySz = &ssl->buffers.keySz; |
1221 | 0 | } |
1222 | 0 | else { |
1223 | 0 | keyType = &ctx->privateKeyType; |
1224 | 0 | keySz = &ctx->privateKeySz; |
1225 | 0 | } |
1226 | 0 | } |
1227 | |
|
1228 | 0 | #ifndef NO_RSA |
1229 | | /* Try RSA if key format is RSA or yet unknown. */ |
1230 | 0 | if ((ret == 0) && ((*keyFormat == 0) || (*keyFormat == RSAk))) { |
1231 | 0 | #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && \ |
1232 | 0 | (HAVE_FIPS_VERSION > 2)) |
1233 | 0 | ret = ProcessBufferTryDecodeRsa(ctx, ssl, der, keyFormat, devId, |
1234 | 0 | keyType, keySz); |
1235 | | #else |
1236 | | ret = ProcessBufferTryDecodeRsa(ctx, ssl, der, keyFormat, heap, devId, |
1237 | | keyType, keySz); |
1238 | | #endif |
1239 | 0 | matchAnyKey = 1; |
1240 | 0 | } |
1241 | 0 | #ifdef WC_RSA_PSS |
1242 | 0 | if((ret == 0) && (*keyFormat == RSAPSSk)) { |
1243 | | /* Require logic to verify that the der is RSAPSSk |
1244 | | * (when *keyFormat == RSAPSSK), and to detect that the der is RSAPSSk |
1245 | | * (when *keyFormat == 0). */ |
1246 | 0 | matchAnyKey = 1; |
1247 | 0 | } |
1248 | 0 | #endif /* WC_RSA_PSS */ |
1249 | 0 | #endif /* NO_RSA */ |
1250 | 0 | #ifdef HAVE_ECC |
1251 | | /* Try ECC if key format is ECDSA or SM2, or yet unknown. */ |
1252 | 0 | if ((ret == 0) && ((*keyFormat == 0) || (*keyFormat == ECDSAk) |
1253 | | #ifdef WOLFSSL_SM2 |
1254 | | || (*keyFormat == SM2k) |
1255 | | #endif |
1256 | 0 | )) { |
1257 | 0 | ret = ProcessBufferTryDecodeEcc(ctx, ssl, der, keyFormat, heap, devId, |
1258 | 0 | keyType, keySz); |
1259 | 0 | matchAnyKey = 1; |
1260 | 0 | } |
1261 | 0 | #endif /* HAVE_ECC */ |
1262 | | #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_IMPORT) |
1263 | | /* Try Ed25519 if key format is Ed25519 or yet unknown. */ |
1264 | | if ((ret == 0) && ((*keyFormat == 0 || *keyFormat == ED25519k))) { |
1265 | | ret = ProcessBufferTryDecodeEd25519(ctx, ssl, der, keyFormat, heap, |
1266 | | devId, keyType, keySz); |
1267 | | matchAnyKey = 1; |
1268 | | } |
1269 | | #endif /* HAVE_ED25519 && HAVE_ED25519_KEY_IMPORT */ |
1270 | | #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_IMPORT) |
1271 | | /* Try Ed448 if key format is Ed448 or yet unknown. */ |
1272 | | if ((ret == 0) && ((*keyFormat == 0 || *keyFormat == ED448k))) { |
1273 | | ret = ProcessBufferTryDecodeEd448(ctx, ssl, der, keyFormat, heap, devId, |
1274 | | keyType, keySz); |
1275 | | matchAnyKey = 1; |
1276 | | } |
1277 | | #endif /* HAVE_ED448 && HAVE_ED448_KEY_IMPORT */ |
1278 | | #if defined(HAVE_FALCON) |
1279 | | /* Try Falcon if key format is Falcon level 1k or 5k or yet unknown. */ |
1280 | | if ((ret == 0) && ((*keyFormat == 0) || (*keyFormat == FALCON_LEVEL1k) || |
1281 | | (*keyFormat == FALCON_LEVEL5k))) { |
1282 | | ret = ProcessBufferTryDecodeFalcon(ctx, ssl, der, keyFormat, heap, |
1283 | | keyType, keySz); |
1284 | | matchAnyKey = 1; |
1285 | | } |
1286 | | #endif /* HAVE_FALCON */ |
1287 | | #if defined(WOLFSSL_HAVE_MLDSA) && !defined(WOLFSSL_MLDSA_NO_SIGN) && \ |
1288 | | !defined(WOLFSSL_MLDSA_NO_ASN1) |
1289 | | /* Try Falcon if key format is Dilithium level 2k, 3k or 5k or yet unknown. |
1290 | | */ |
1291 | | if ((ret == 0) && |
1292 | | ((*keyFormat == 0) || |
1293 | | (*keyFormat == ML_DSA_44k) || |
1294 | | (*keyFormat == ML_DSA_65k) || |
1295 | | (*keyFormat == ML_DSA_87k) |
1296 | | #ifdef WOLFSSL_MLDSA_FIPS204_DRAFT |
1297 | | || (*keyFormat == DILITHIUM_LEVEL2k) |
1298 | | || (*keyFormat == DILITHIUM_LEVEL3k) |
1299 | | || (*keyFormat == DILITHIUM_LEVEL5k) |
1300 | | #endif |
1301 | | )) { |
1302 | | ret = ProcessBufferTryDecodeMlDsa(ctx, ssl, der, keyFormat, heap, |
1303 | | keyType, keySz); |
1304 | | matchAnyKey = 1; |
1305 | | } |
1306 | | #endif /* WOLFSSL_HAVE_MLDSA */ |
1307 | | #if defined(WOLFSSL_HAVE_SLHDSA) && !defined(WOLFSSL_SLHDSA_VERIFY_ONLY) |
1308 | | /* Try SLH-DSA if key format is an SLH-DSA key OID or yet unknown. */ |
1309 | | if ((ret == 0) && |
1310 | | ((*keyFormat == 0) || |
1311 | | (*keyFormat == SLH_DSA_SHA2_128Sk) || |
1312 | | (*keyFormat == SLH_DSA_SHA2_128Fk) || |
1313 | | (*keyFormat == SLH_DSA_SHA2_192Sk) || |
1314 | | (*keyFormat == SLH_DSA_SHA2_192Fk) || |
1315 | | (*keyFormat == SLH_DSA_SHA2_256Sk) || |
1316 | | (*keyFormat == SLH_DSA_SHA2_256Fk) || |
1317 | | (*keyFormat == SLH_DSA_SHAKE_128Sk) || |
1318 | | (*keyFormat == SLH_DSA_SHAKE_128Fk) || |
1319 | | (*keyFormat == SLH_DSA_SHAKE_192Sk) || |
1320 | | (*keyFormat == SLH_DSA_SHAKE_192Fk) || |
1321 | | (*keyFormat == SLH_DSA_SHAKE_256Sk) || |
1322 | | (*keyFormat == SLH_DSA_SHAKE_256Fk))) { |
1323 | | ret = ProcessBufferTryDecodeSlhDsa(ctx, ssl, der, keyFormat, heap, |
1324 | | keyType, keySz); |
1325 | | matchAnyKey = 1; |
1326 | | } |
1327 | | #endif /* WOLFSSL_HAVE_SLHDSA */ |
1328 | | |
1329 | | /* Check we know the format. */ |
1330 | 0 | if ((ret == 0) && |
1331 | 0 | ((*keyFormat == 0) || ((*keyFormat != 0) && (matchAnyKey == 0)))) { |
1332 | 0 | WOLFSSL_MSG("Not a supported key type"); |
1333 | | /* Not supported key format. */ |
1334 | 0 | ret = WOLFSSL_BAD_FILE; |
1335 | 0 | } |
1336 | |
|
1337 | 0 | return ret; |
1338 | 0 | } |
1339 | | |
1340 | | #if defined(WOLFSSL_ENCRYPTED_KEYS) && !defined(NO_PWDBASED) |
1341 | | /* Decrypt PKCS#8 private key. |
1342 | | * |
1343 | | * @param [in] info Encryption information. |
1344 | | * @param [in] der DER encoded data. |
1345 | | * @param [in] heap Dynamic memory allocation hint. |
1346 | | * @return 0 on success. |
1347 | | * @return MEMORY_E when dynamic memory allocation fails. |
1348 | | */ |
1349 | | static int ProcessBufferPrivPkcs8Dec(EncryptedInfo* info, DerBuffer* der, |
1350 | | void* heap) |
1351 | | { |
1352 | | int ret = 0; |
1353 | | word32 algId; |
1354 | | int passwordSz = NAME_SZ; |
1355 | | WC_DECLARE_VAR(password, char, NAME_SZ, 0); |
1356 | | |
1357 | | (void)heap; |
1358 | | #ifdef WOLFSSL_SMALL_STACK |
1359 | | /* Allocate memory for password. */ |
1360 | | password = (char*)XMALLOC(passwordSz, heap, DYNAMIC_TYPE_STRING); |
1361 | | if (password == NULL) { |
1362 | | ret = MEMORY_E; |
1363 | | } |
1364 | | #endif |
1365 | | |
1366 | | if (ret == 0) { |
1367 | | /* Get password. */ |
1368 | | ret = info->passwd_cb(password, passwordSz, PEM_PASS_READ, |
1369 | | info->passwd_userdata); |
1370 | | } |
1371 | | if (ret >= 0) { |
1372 | | /* Returned value is password size. */ |
1373 | | passwordSz = ret; |
1374 | | #ifdef WOLFSSL_CHECK_MEM_ZERO |
1375 | | wc_MemZero_Add("ProcessBuffer password", password, passwordSz); |
1376 | | #endif |
1377 | | |
1378 | | /* Decrypt PKCS#8 private key inline and get algorithm id. */ |
1379 | | ret = ToTraditionalEnc(der->buffer, der->length, password, passwordSz, |
1380 | | &algId); |
1381 | | } |
1382 | | if (ret >= 0) { |
1383 | | /* Zero out encrypted data not overwritten. */ |
1384 | | ForceZero(der->buffer + ret, der->length - (word32)ret); |
1385 | | /* Set decrypted data length. */ |
1386 | | der->length = (word32)ret; |
1387 | | } |
1388 | | |
1389 | | #ifdef WOLFSSL_SMALL_STACK |
1390 | | if (password != NULL) |
1391 | | #endif |
1392 | | { |
1393 | | /* Ensure password is zeroized. */ |
1394 | | ForceZero(password, (word32)passwordSz); |
1395 | | } |
1396 | | #ifdef WOLFSSL_SMALL_STACK |
1397 | | /* Dispose of password memory. */ |
1398 | | XFREE(password, heap, DYNAMIC_TYPE_STRING); |
1399 | | #elif defined(WOLFSSL_CHECK_MEM_ZERO) |
1400 | | wc_MemZero_Check(password, NAME_SZ); |
1401 | | #endif |
1402 | | return ret; |
1403 | | } |
1404 | | #endif /* WOLFSSL_ENCRYPTED_KEYS && !NO_PWDBASED */ |
1405 | | |
1406 | | /* Put the DER into the SSL or SSL context object. |
1407 | | * |
1408 | | * Precondition: ctx or ssl is not NULL. |
1409 | | * Precondition: Must be a private key type. |
1410 | | * |
1411 | | * @param [in, out] ctx SSL context object. |
1412 | | * @param [in, out] ssl SSL object. |
1413 | | * @param [in] der DER encoding. |
1414 | | * @return 0 on success. |
1415 | | */ |
1416 | | static int ProcessBufferPrivKeyHandleDer(WOLFSSL_CTX* ctx, WOLFSSL* ssl, |
1417 | | DerBuffer** der, int type) |
1418 | 0 | { |
1419 | 0 | int ret = 0; |
1420 | |
|
1421 | 0 | (void)type; |
1422 | |
|
1423 | | #ifdef WOLFSSL_DUAL_ALG_CERTS |
1424 | | if (type == ALT_PRIVATEKEY_TYPE) { |
1425 | | /* Put in alternate private key fields of objects. */ |
1426 | | if (ssl != NULL) { |
1427 | | /* Dispose of previous key if not context's. */ |
1428 | | if (ssl->buffers.weOwnAltKey) { |
1429 | | FreeDer(&ssl->buffers.altKey); |
1430 | | #ifdef WOLFSSL_BLIND_PRIVATE_KEY |
1431 | | FreeDer(&ssl->buffers.altKeyMask); |
1432 | | #endif |
1433 | | } |
1434 | | ssl->buffers.altKeyId = 0; |
1435 | | ssl->buffers.altKeyLabel = 0; |
1436 | | ssl->buffers.altKeyDevId = INVALID_DEVID; |
1437 | | /* Store key by reference and own it. */ |
1438 | | ssl->buffers.altKey = *der; |
1439 | | #ifdef WOLFSSL_CHECK_MEM_ZERO |
1440 | | wc_MemZero_Add("SSL Buffers key", (*der)->buffer, (*der)->length); |
1441 | | #endif |
1442 | | ssl->buffers.weOwnAltKey = 1; |
1443 | | } |
1444 | | else if (ctx != NULL) { |
1445 | | /* Dispose of previous key. */ |
1446 | | FreeDer(&ctx->altPrivateKey); |
1447 | | ctx->altPrivateKeyId = 0; |
1448 | | ctx->altPrivateKeyLabel = 0; |
1449 | | ctx->altPrivateKeyDevId = INVALID_DEVID; |
1450 | | /* Store key by reference. */ |
1451 | | ctx->altPrivateKey = *der; |
1452 | | #ifdef WOLFSSL_CHECK_MEM_ZERO |
1453 | | wc_MemZero_Add("CTX private key", (*der)->buffer, (*der)->length); |
1454 | | #endif |
1455 | | } |
1456 | | } |
1457 | | else |
1458 | | #endif /* WOLFSSL_DUAL_ALG_CERTS */ |
1459 | 0 | if (ssl != NULL) { |
1460 | | /* Dispose of previous key if not context's. */ |
1461 | 0 | if (ssl->buffers.weOwnKey) { |
1462 | 0 | FreeDer(&ssl->buffers.key); |
1463 | | #ifdef WOLFSSL_BLIND_PRIVATE_KEY |
1464 | | FreeDer(&ssl->buffers.keyMask); |
1465 | | #endif |
1466 | 0 | } |
1467 | 0 | ssl->buffers.keyId = 0; |
1468 | 0 | ssl->buffers.keyLabel = 0; |
1469 | 0 | ssl->buffers.keyDevId = INVALID_DEVID; |
1470 | | /* Store key by reference and own it. */ |
1471 | 0 | ssl->buffers.key = *der; |
1472 | | #ifdef WOLFSSL_CHECK_MEM_ZERO |
1473 | | wc_MemZero_Add("SSL Buffers key", (*der)->buffer, (*der)->length); |
1474 | | #endif |
1475 | 0 | ssl->buffers.weOwnKey = 1; |
1476 | 0 | } |
1477 | 0 | else if (ctx != NULL) { |
1478 | | /* Dispose of previous key. */ |
1479 | 0 | FreeDer(&ctx->privateKey); |
1480 | 0 | ctx->privateKeyId = 0; |
1481 | 0 | ctx->privateKeyLabel = 0; |
1482 | 0 | ctx->privateKeyDevId = INVALID_DEVID; |
1483 | | /* Store key by reference. */ |
1484 | 0 | ctx->privateKey = *der; |
1485 | | #ifdef WOLFSSL_CHECK_MEM_ZERO |
1486 | | wc_MemZero_Add("CTX private key", (*der)->buffer, (*der)->length); |
1487 | | #endif |
1488 | 0 | } |
1489 | |
|
1490 | 0 | return ret; |
1491 | 0 | } |
1492 | | |
1493 | | /* Decode private key. |
1494 | | * |
1495 | | * Precondition: ctx or ssl is not NULL. |
1496 | | * Precondition: Must be a private key type. |
1497 | | * |
1498 | | * @param [in, out] ctx SSL context object. |
1499 | | * @param [in, out] ssl SSL object. |
1500 | | * @param [in] der DER encoding. |
1501 | | * @param [in] format Original format of data. |
1502 | | * @param [in] info Encryption information. |
1503 | | * @param [in] heap Dynamic memory allocation hint. |
1504 | | * @param [in] type Type of data: |
1505 | | * PRIVATEKEY_TYPE or ALT_PRIVATEKEY_TYPE. |
1506 | | * @param [in] algId Algorithm id of key. |
1507 | | * @return 0 on success. |
1508 | | * @return WOLFSSL_BAD_FILE when not able to decode. |
1509 | | */ |
1510 | | static int ProcessBufferPrivateKey(WOLFSSL_CTX* ctx, WOLFSSL* ssl, |
1511 | | DerBuffer* der, int format, EncryptedInfo* info, void* heap, int type, |
1512 | | int algId) |
1513 | 0 | { |
1514 | 0 | int ret; |
1515 | |
|
1516 | 0 | (void)info; |
1517 | 0 | (void)format; |
1518 | | |
1519 | | /* Put the data into the SSL or SSL context object. */ |
1520 | 0 | ret = ProcessBufferPrivKeyHandleDer(ctx, ssl, &der, type); |
1521 | 0 | if (ret == 0) { |
1522 | | /* Try to decode the DER data. */ |
1523 | 0 | ret = ProcessBufferTryDecode(ctx, ssl, der, &algId, heap, type); |
1524 | 0 | } |
1525 | |
|
1526 | | #if defined(WOLFSSL_ENCRYPTED_KEYS) && !defined(NO_PWDBASED) |
1527 | | /* If private key type PKCS8 header wasn't already removed (algId == 0). */ |
1528 | | if (((ret != 0) || (algId == 0)) && (format != WOLFSSL_FILETYPE_PEM) && |
1529 | | (info->passwd_cb != NULL) && (algId == 0)) { |
1530 | | /* Try to decrypt DER data as a PKCS#8 private key. */ |
1531 | | ret = ProcessBufferPrivPkcs8Dec(info, der, heap); |
1532 | | if (ret >= 0) { |
1533 | | /* Try to decode decrypted data. */ |
1534 | | ret = ProcessBufferTryDecode(ctx, ssl, der, &algId, heap, type); |
1535 | | } |
1536 | | } |
1537 | | #endif /* WOLFSSL_ENCRYPTED_KEYS && !NO_PWDBASED */ |
1538 | |
|
1539 | | #ifdef WOLFSSL_BLIND_PRIVATE_KEY |
1540 | | { |
1541 | | int blindRet = 0; |
1542 | | #ifdef WOLFSSL_DUAL_ALG_CERTS |
1543 | | if (type == ALT_PRIVATEKEY_TYPE) { |
1544 | | if (ssl != NULL) { |
1545 | | blindRet = wolfssl_priv_der_blind(ssl->rng, ssl->buffers.altKey, |
1546 | | &ssl->buffers.altKeyMask); |
1547 | | } |
1548 | | else { |
1549 | | blindRet = wolfssl_priv_der_blind(NULL, ctx->altPrivateKey, |
1550 | | &ctx->altPrivateKeyMask); |
1551 | | } |
1552 | | } |
1553 | | else |
1554 | | #endif |
1555 | | if (ssl != NULL) { |
1556 | | blindRet = wolfssl_priv_der_blind(ssl->rng, ssl->buffers.key, |
1557 | | &ssl->buffers.keyMask); |
1558 | | } |
1559 | | else { |
1560 | | blindRet = wolfssl_priv_der_blind(NULL, ctx->privateKey, |
1561 | | &ctx->privateKeyMask); |
1562 | | } |
1563 | | if (ret == 0 && blindRet != 0) |
1564 | | ret = blindRet; |
1565 | | } |
1566 | | #endif |
1567 | | |
1568 | | /* Check if we were able to determine algorithm id. */ |
1569 | 0 | if ((ret == 0) && (algId == 0)) { |
1570 | | #ifdef OPENSSL_EXTRA |
1571 | | /* Decryption password is probably wrong. */ |
1572 | | if (info->passwd_cb) { |
1573 | | WOLFSSL_EVPerr(0, -WOLFSSL_EVP_R_BAD_DECRYPT_E); |
1574 | | } |
1575 | | #endif |
1576 | 0 | WOLFSSL_ERROR(WOLFSSL_BAD_FILE); |
1577 | | /* Unable to decode DER data. */ |
1578 | 0 | ret = WOLFSSL_BAD_FILE; |
1579 | 0 | } |
1580 | |
|
1581 | 0 | return ret; |
1582 | 0 | } |
1583 | | |
1584 | | /* Use the key OID to determine have options. |
1585 | | * |
1586 | | * @param [in, out] ctx SSL context object. |
1587 | | * @param [in, out] ssl SSL object. |
1588 | | * @param [in] keyOID OID for public/private key. |
1589 | | */ |
1590 | | static void wolfssl_set_have_from_key_oid(WOLFSSL_CTX* ctx, WOLFSSL* ssl, |
1591 | | int keyOID) |
1592 | 0 | { |
1593 | | /* Set which private key algorithm available based on key OID. */ |
1594 | 0 | switch (keyOID) { |
1595 | 0 | case ECDSAk: |
1596 | | #if defined(WOLFSSL_SM2) && defined(WOLFSSL_SM3) |
1597 | | case SM2k: |
1598 | | #endif |
1599 | | #ifdef HAVE_ED25519 |
1600 | | case ED25519k: |
1601 | | #endif |
1602 | | #ifdef HAVE_ED448 |
1603 | | case ED448k: |
1604 | | #endif |
1605 | 0 | if (ssl != NULL) { |
1606 | 0 | ssl->options.haveECC = 1; |
1607 | 0 | } |
1608 | 0 | else { |
1609 | 0 | ctx->haveECC = 1; |
1610 | 0 | } |
1611 | 0 | break; |
1612 | 0 | #ifndef NO_RSA |
1613 | 0 | case RSAk: |
1614 | 0 | #ifdef WC_RSA_PSS |
1615 | 0 | case RSAPSSk: |
1616 | 0 | #endif |
1617 | 0 | if (ssl != NULL) { |
1618 | 0 | ssl->options.haveRSA = 1; |
1619 | 0 | } |
1620 | 0 | else { |
1621 | 0 | ctx->haveRSA = 1; |
1622 | 0 | } |
1623 | 0 | break; |
1624 | 0 | #endif |
1625 | | #ifdef HAVE_FALCON |
1626 | | case FALCON_LEVEL1k: |
1627 | | case FALCON_LEVEL5k: |
1628 | | if (ssl != NULL) { |
1629 | | ssl->options.haveFalconSig = 1; |
1630 | | } |
1631 | | else { |
1632 | | ctx->haveFalconSig = 1; |
1633 | | } |
1634 | | break; |
1635 | | #endif /* HAVE_FALCON */ |
1636 | | #ifdef WOLFSSL_HAVE_MLDSA |
1637 | | case ML_DSA_44k: |
1638 | | case ML_DSA_65k: |
1639 | | case ML_DSA_87k: |
1640 | | #ifdef WOLFSSL_MLDSA_FIPS204_DRAFT |
1641 | | case DILITHIUM_LEVEL2k: |
1642 | | case DILITHIUM_LEVEL3k: |
1643 | | case DILITHIUM_LEVEL5k: |
1644 | | #endif |
1645 | | if (ssl != NULL) { |
1646 | | ssl->options.haveMlDsaSig = 1; |
1647 | | } |
1648 | | else { |
1649 | | ctx->haveMlDsaSig = 1; |
1650 | | } |
1651 | | break; |
1652 | | #endif /* WOLFSSL_HAVE_MLDSA */ |
1653 | | #ifdef WOLFSSL_HAVE_SLHDSA |
1654 | | case SLH_DSA_SHA2_128Sk: |
1655 | | case SLH_DSA_SHA2_128Fk: |
1656 | | case SLH_DSA_SHA2_192Sk: |
1657 | | case SLH_DSA_SHA2_192Fk: |
1658 | | case SLH_DSA_SHA2_256Sk: |
1659 | | case SLH_DSA_SHA2_256Fk: |
1660 | | case SLH_DSA_SHAKE_128Sk: |
1661 | | case SLH_DSA_SHAKE_128Fk: |
1662 | | case SLH_DSA_SHAKE_192Sk: |
1663 | | case SLH_DSA_SHAKE_192Fk: |
1664 | | case SLH_DSA_SHAKE_256Sk: |
1665 | | case SLH_DSA_SHAKE_256Fk: |
1666 | | if (ssl != NULL) { |
1667 | | ssl->options.haveSlhDsaSig = 1; |
1668 | | } |
1669 | | else { |
1670 | | ctx->haveSlhDsaSig = 1; |
1671 | | } |
1672 | | break; |
1673 | | #endif /* WOLFSSL_HAVE_SLHDSA */ |
1674 | 0 | default: |
1675 | 0 | WOLFSSL_MSG("Cert key not supported"); |
1676 | 0 | break; |
1677 | 0 | } |
1678 | 0 | } |
1679 | | |
1680 | | /* Set which private key algorithm we have against SSL or SSL context object. |
1681 | | * |
1682 | | * Precondition: ctx or ssl is not NULL. |
1683 | | * |
1684 | | * @param [in, out] ctx SSL context object. |
1685 | | * @param [in, out] ssl SSL object. |
1686 | | * @param [in] cert Decode certificate. |
1687 | | */ |
1688 | | static void ProcessBufferCertSetHave(WOLFSSL_CTX* ctx, WOLFSSL* ssl, |
1689 | | DecodedCert* cert) |
1690 | 0 | { |
1691 | 0 | if (ssl != NULL) { |
1692 | | /* Reset signatures we have in SSL. */ |
1693 | 0 | ssl->options.haveECDSAsig = 0; |
1694 | 0 | ssl->options.haveFalconSig = 0; |
1695 | 0 | ssl->options.haveMlDsaSig = 0; |
1696 | 0 | ssl->options.haveSlhDsaSig = 0; |
1697 | 0 | } |
1698 | | |
1699 | | /* Set which signature we have based on the type in the cert. */ |
1700 | 0 | switch (cert->signatureOID) { |
1701 | 0 | case CTC_SHAwECDSA: |
1702 | 0 | case CTC_SHA256wECDSA: |
1703 | 0 | case CTC_SHA384wECDSA: |
1704 | 0 | case CTC_SHA512wECDSA: |
1705 | | #ifdef HAVE_ED25519 |
1706 | | case CTC_ED25519: |
1707 | | #endif |
1708 | | #ifdef HAVE_ED448 |
1709 | | case CTC_ED448: |
1710 | | #endif |
1711 | | #if defined(WOLFSSL_SM2) && defined(WOLFSSL_SM3) |
1712 | | case CTC_SM3wSM2: |
1713 | | #endif |
1714 | 0 | WOLFSSL_MSG("ECDSA/ED25519/ED448 cert signature"); |
1715 | 0 | if (ssl) { |
1716 | 0 | ssl->options.haveECDSAsig = 1; |
1717 | 0 | } |
1718 | 0 | else if (ctx) { |
1719 | 0 | ctx->haveECDSAsig = 1; |
1720 | 0 | } |
1721 | 0 | break; |
1722 | | #ifdef HAVE_FALCON |
1723 | | case CTC_FALCON_LEVEL1: |
1724 | | case CTC_FALCON_LEVEL5: |
1725 | | WOLFSSL_MSG("Falcon cert signature"); |
1726 | | if (ssl) { |
1727 | | ssl->options.haveFalconSig = 1; |
1728 | | } |
1729 | | else if (ctx) { |
1730 | | ctx->haveFalconSig = 1; |
1731 | | } |
1732 | | break; |
1733 | | #endif |
1734 | | #ifdef WOLFSSL_HAVE_MLDSA |
1735 | | case CTC_ML_DSA_44: |
1736 | | case CTC_ML_DSA_65: |
1737 | | case CTC_ML_DSA_87: |
1738 | | #ifdef WOLFSSL_MLDSA_FIPS204_DRAFT |
1739 | | case CTC_DILITHIUM_LEVEL2: |
1740 | | case CTC_DILITHIUM_LEVEL3: |
1741 | | case CTC_DILITHIUM_LEVEL5: |
1742 | | #endif /* WOLFSSL_MLDSA_FIPS204_DRAFT */ |
1743 | | WOLFSSL_MSG("ML-DSA cert signature"); |
1744 | | if (ssl) { |
1745 | | ssl->options.haveMlDsaSig = 1; |
1746 | | } |
1747 | | else if (ctx) { |
1748 | | ctx->haveMlDsaSig = 1; |
1749 | | } |
1750 | | break; |
1751 | | #endif |
1752 | | #ifdef WOLFSSL_HAVE_SLHDSA |
1753 | | case CTC_SLH_DSA_SHA2_128S: |
1754 | | case CTC_SLH_DSA_SHA2_128F: |
1755 | | case CTC_SLH_DSA_SHA2_192S: |
1756 | | case CTC_SLH_DSA_SHA2_192F: |
1757 | | case CTC_SLH_DSA_SHA2_256S: |
1758 | | case CTC_SLH_DSA_SHA2_256F: |
1759 | | case CTC_SLH_DSA_SHAKE_128S: |
1760 | | case CTC_SLH_DSA_SHAKE_128F: |
1761 | | case CTC_SLH_DSA_SHAKE_192S: |
1762 | | case CTC_SLH_DSA_SHAKE_192F: |
1763 | | case CTC_SLH_DSA_SHAKE_256S: |
1764 | | case CTC_SLH_DSA_SHAKE_256F: |
1765 | | WOLFSSL_MSG("SLH-DSA cert signature"); |
1766 | | if (ssl) { |
1767 | | ssl->options.haveSlhDsaSig = 1; |
1768 | | } |
1769 | | else if (ctx) { |
1770 | | ctx->haveSlhDsaSig = 1; |
1771 | | } |
1772 | | break; |
1773 | | #endif |
1774 | 0 | default: |
1775 | 0 | WOLFSSL_MSG("Cert signature not supported"); |
1776 | 0 | break; |
1777 | 0 | } |
1778 | | |
1779 | 0 | #if defined(HAVE_ECC) || defined(HAVE_ED25519) || defined(HAVE_ED448) || \ |
1780 | 0 | defined(HAVE_FALCON) || defined(WOLFSSL_HAVE_MLDSA) || \ |
1781 | 0 | defined(WOLFSSL_HAVE_SLHDSA) || !defined(NO_RSA) |
1782 | 0 | #if defined(HAVE_ECC) || defined(HAVE_ED25519) || defined(HAVE_ED448) || \ |
1783 | 0 | defined(HAVE_FALCON) || defined(WOLFSSL_HAVE_MLDSA) || \ |
1784 | 0 | defined(WOLFSSL_HAVE_SLHDSA) |
1785 | | /* Set the private key curve OID. */ |
1786 | 0 | if (ssl != NULL) { |
1787 | 0 | ssl->pkCurveOID = cert->pkCurveOID; |
1788 | 0 | } |
1789 | 0 | else if (ctx) { |
1790 | 0 | ctx->pkCurveOID = cert->pkCurveOID; |
1791 | 0 | } |
1792 | 0 | #endif |
1793 | 0 | #ifndef WC_STRICT_SIG |
1794 | 0 | if ((ctx != NULL) || (ssl != NULL)) { |
1795 | 0 | wolfssl_set_have_from_key_oid(ctx, ssl, (int)cert->keyOID); |
1796 | 0 | } |
1797 | | #else |
1798 | | /* Set whether ECC is available based on signature available. */ |
1799 | | if (ssl != NULL) { |
1800 | | ssl->options.haveECC = ssl->options.haveECDSAsig; |
1801 | | } |
1802 | | else if (ctx) { |
1803 | | ctx->haveECC = ctx->haveECDSAsig; |
1804 | | } |
1805 | | #endif /* !WC_STRICT_SIG */ |
1806 | 0 | #endif |
1807 | 0 | } |
1808 | | |
1809 | | /* Check key size is valid. |
1810 | | * |
1811 | | * Precondition: ctx or ssl is not NULL. |
1812 | | * |
1813 | | * @param [in] min Minimum key size. |
1814 | | * @param [in] max Maximum key size. |
1815 | | * @param [in] keySz Key size. |
1816 | | * @param [in] err Error value to return when key size is invalid. |
1817 | | * @return 0 on success. |
1818 | | * @return err when verifying and min is less than 0 or key size is invalid. |
1819 | | */ |
1820 | | #define CHECK_KEY_SZ(min, max, keySz, err) \ |
1821 | 0 | (((min) < 0) || ((keySz) < (min)) || ((keySz) > (max))) ? (err) : 0 |
1822 | | |
1823 | | /* Check public key in certificate. |
1824 | | * |
1825 | | * @param [in, out] ctx SSL context object. |
1826 | | * @param [in, out] ssl SSL object. |
1827 | | * @param [in] cert Certificate object. |
1828 | | * @return 0 on success. |
1829 | | * @return Non-zero when an error occurred. |
1830 | | */ |
1831 | | static int ProcessBufferCertPublicKey(WOLFSSL_CTX* ctx, WOLFSSL* ssl, |
1832 | | DecodedCert* cert, int checkKeySz) |
1833 | 0 | { |
1834 | 0 | int ret = 0; |
1835 | 0 | byte keyType = 0; |
1836 | 0 | int keySz = 0; |
1837 | 0 | #ifndef NO_RSA |
1838 | 0 | word32 idx; |
1839 | 0 | #endif |
1840 | 0 | if (ctx == NULL && ssl == NULL) { |
1841 | 0 | return BAD_FUNC_ARG; |
1842 | 0 | } |
1843 | | |
1844 | | /* Get key size and check unless not verifying. */ |
1845 | 0 | switch (cert->keyOID) { |
1846 | 0 | #ifndef NO_RSA |
1847 | 0 | #ifdef WC_RSA_PSS |
1848 | 0 | case RSAPSSk: |
1849 | 0 | #endif |
1850 | 0 | case RSAk: |
1851 | 0 | keyType = rsa_sa_algo; |
1852 | | /* Determine RSA key size by parsing public key */ |
1853 | 0 | idx = 0; |
1854 | 0 | ret = wc_RsaPublicKeyDecode_ex(cert->publicKey, &idx, |
1855 | 0 | cert->pubKeySize, NULL, (word32*)&keySz, NULL, NULL); |
1856 | 0 | if ((ret == 0) && checkKeySz) { |
1857 | 0 | ret = CHECK_KEY_SZ(ssl ? ssl->options.minRsaKeySz : |
1858 | 0 | ctx->minRsaKeySz, RSA_MAX_SIZE / 8, keySz, RSA_KEY_SIZE_E); |
1859 | 0 | } |
1860 | 0 | #ifdef WC_RSA_PSS |
1861 | 0 | if (ssl) { |
1862 | 0 | ssl->useRsaPss = cert->keyOID == RSAPSSk; |
1863 | 0 | } |
1864 | 0 | if (ctx) { |
1865 | 0 | ctx->useRsaPss = cert->keyOID == RSAPSSk; |
1866 | 0 | } |
1867 | 0 | #endif |
1868 | 0 | break; |
1869 | 0 | #endif /* !NO_RSA */ |
1870 | 0 | #ifdef HAVE_ECC |
1871 | 0 | case ECDSAk: |
1872 | 0 | keyType = ecc_dsa_sa_algo; |
1873 | | /* Determine ECC key size based on curve */ |
1874 | | #ifdef WOLFSSL_CUSTOM_CURVES |
1875 | | if ((cert->pkCurveOID == 0) && (cert->pkCurveSize != 0)) { |
1876 | | keySz = cert->pkCurveSize; |
1877 | | } |
1878 | | else |
1879 | | #endif |
1880 | 0 | { |
1881 | 0 | keySz = wc_ecc_get_curve_size_from_id(wc_ecc_get_oid( |
1882 | 0 | cert->pkCurveOID, NULL, NULL)); |
1883 | 0 | } |
1884 | |
|
1885 | 0 | if (checkKeySz) { |
1886 | 0 | ret = CHECK_KEY_SZ(ssl ? ssl->options.minEccKeySz : |
1887 | 0 | ctx->minEccKeySz, (MAX_ECC_BITS + 7) / 8, keySz, |
1888 | 0 | ECC_KEY_SIZE_E); |
1889 | 0 | } |
1890 | 0 | break; |
1891 | 0 | #endif /* HAVE_ECC */ |
1892 | | #if defined(WOLFSSL_SM2) && defined(WOLFSSL_SM3) |
1893 | | case SM2k: |
1894 | | keyType = sm2_sa_algo; |
1895 | | /* Determine ECC key size based on curve */ |
1896 | | keySz = WOLFSSL_SM2_KEY_BITS / 8; |
1897 | | if (checkKeySz) { |
1898 | | ret = CHECK_KEY_SZ(ssl ? ssl->options.minEccKeySz : |
1899 | | ctx->minEccKeySz, (MAX_ECC_BITS + 7) / 8, keySz, |
1900 | | ECC_KEY_SIZE_E); |
1901 | | } |
1902 | | break; |
1903 | | #endif /* WOLFSSL_SM2 && WOLFSSL_SM3 */ |
1904 | | #ifdef HAVE_ED25519 |
1905 | | case ED25519k: |
1906 | | keyType = ed25519_sa_algo; |
1907 | | /* ED25519 is fixed key size */ |
1908 | | keySz = ED25519_KEY_SIZE; |
1909 | | if (checkKeySz) { |
1910 | | ret = CHECK_KEY_SZ(ssl ? ssl->options.minEccKeySz : |
1911 | | ctx->minEccKeySz, ED25519_KEY_SIZE, keySz, ECC_KEY_SIZE_E); |
1912 | | } |
1913 | | break; |
1914 | | #endif /* HAVE_ED25519 */ |
1915 | | #ifdef HAVE_ED448 |
1916 | | case ED448k: |
1917 | | keyType = ed448_sa_algo; |
1918 | | /* ED448 is fixed key size */ |
1919 | | keySz = ED448_KEY_SIZE; |
1920 | | if (checkKeySz) { |
1921 | | ret = CHECK_KEY_SZ(ssl ? ssl->options.minEccKeySz : |
1922 | | ctx->minEccKeySz, ED448_KEY_SIZE, keySz, ECC_KEY_SIZE_E); |
1923 | | } |
1924 | | break; |
1925 | | #endif /* HAVE_ED448 */ |
1926 | | #if defined(HAVE_FALCON) |
1927 | | case FALCON_LEVEL1k: |
1928 | | keyType = falcon_level1_sa_algo; |
1929 | | /* Falcon is fixed key size */ |
1930 | | keySz = FALCON_LEVEL1_KEY_SIZE; |
1931 | | if (checkKeySz) { |
1932 | | #ifdef WOLFSSL_NO_FALCON_LEVEL1 |
1933 | | ret = NOT_COMPILED_IN; |
1934 | | #else |
1935 | | ret = CHECK_KEY_SZ(ssl ? ssl->options.minFalconKeySz : |
1936 | | ctx->minFalconKeySz, FALCON_MAX_KEY_SIZE, keySz, |
1937 | | FALCON_KEY_SIZE_E); |
1938 | | #endif |
1939 | | } |
1940 | | break; |
1941 | | case FALCON_LEVEL5k: |
1942 | | keyType = falcon_level5_sa_algo; |
1943 | | /* Falcon is fixed key size */ |
1944 | | keySz = FALCON_LEVEL5_KEY_SIZE; |
1945 | | if (checkKeySz) { |
1946 | | #ifdef WOLFSSL_NO_FALCON_LEVEL5 |
1947 | | ret = NOT_COMPILED_IN; |
1948 | | #else |
1949 | | ret = CHECK_KEY_SZ(ssl ? ssl->options.minFalconKeySz : |
1950 | | ctx->minFalconKeySz, FALCON_MAX_KEY_SIZE, keySz, |
1951 | | FALCON_KEY_SIZE_E); |
1952 | | #endif |
1953 | | } |
1954 | | break; |
1955 | | #endif /* HAVE_FALCON */ |
1956 | | #if defined(WOLFSSL_HAVE_MLDSA) |
1957 | | #ifdef WOLFSSL_MLDSA_FIPS204_DRAFT |
1958 | | case DILITHIUM_LEVEL2k: |
1959 | | keyType = mldsa_44_sa_algo; |
1960 | | /* Dilithium is fixed key size */ |
1961 | | keySz = WC_MLDSA_44_KEY_SIZE; |
1962 | | if (checkKeySz) { |
1963 | | ret = CHECK_KEY_SZ(ssl ? ssl->options.minMlDsaKeySz : |
1964 | | ctx->minMlDsaKeySz, MLDSA_MAX_KEY_SIZE, keySz, |
1965 | | MLDSA_KEY_SIZE_E); |
1966 | | } |
1967 | | break; |
1968 | | case DILITHIUM_LEVEL3k: |
1969 | | keyType = mldsa_65_sa_algo; |
1970 | | /* Dilithium is fixed key size */ |
1971 | | keySz = WC_MLDSA_65_KEY_SIZE; |
1972 | | if (checkKeySz) { |
1973 | | ret = CHECK_KEY_SZ(ssl ? ssl->options.minMlDsaKeySz : |
1974 | | ctx->minMlDsaKeySz, MLDSA_MAX_KEY_SIZE, keySz, |
1975 | | MLDSA_KEY_SIZE_E); |
1976 | | } |
1977 | | break; |
1978 | | case DILITHIUM_LEVEL5k: |
1979 | | keyType = mldsa_87_sa_algo; |
1980 | | /* Dilithium is fixed key size */ |
1981 | | keySz = WC_MLDSA_87_KEY_SIZE; |
1982 | | if (checkKeySz) { |
1983 | | ret = CHECK_KEY_SZ(ssl ? ssl->options.minMlDsaKeySz : |
1984 | | ctx->minMlDsaKeySz, MLDSA_MAX_KEY_SIZE, keySz, |
1985 | | MLDSA_KEY_SIZE_E); |
1986 | | } |
1987 | | break; |
1988 | | #endif /* WOLFSSL_MLDSA_FIPS204_DRAFT */ |
1989 | | case ML_DSA_44k: |
1990 | | keyType = mldsa_44_sa_algo; |
1991 | | /* Dilithium is fixed key size */ |
1992 | | keySz = WC_MLDSA_44_KEY_SIZE; |
1993 | | if (checkKeySz) { |
1994 | | ret = CHECK_KEY_SZ(ssl ? ssl->options.minMlDsaKeySz : |
1995 | | ctx->minMlDsaKeySz, MLDSA_MAX_KEY_SIZE, keySz, |
1996 | | MLDSA_KEY_SIZE_E); |
1997 | | } |
1998 | | break; |
1999 | | case ML_DSA_65k: |
2000 | | keyType = mldsa_65_sa_algo; |
2001 | | /* Dilithium is fixed key size */ |
2002 | | keySz = WC_MLDSA_65_KEY_SIZE; |
2003 | | if (checkKeySz) { |
2004 | | ret = CHECK_KEY_SZ(ssl ? ssl->options.minMlDsaKeySz : |
2005 | | ctx->minMlDsaKeySz, MLDSA_MAX_KEY_SIZE, keySz, |
2006 | | MLDSA_KEY_SIZE_E); |
2007 | | } |
2008 | | break; |
2009 | | case ML_DSA_87k: |
2010 | | keyType = mldsa_87_sa_algo; |
2011 | | /* Dilithium is fixed key size */ |
2012 | | keySz = WC_MLDSA_87_KEY_SIZE; |
2013 | | if (checkKeySz) { |
2014 | | ret = CHECK_KEY_SZ(ssl ? ssl->options.minMlDsaKeySz : |
2015 | | ctx->minMlDsaKeySz, MLDSA_MAX_KEY_SIZE, keySz, |
2016 | | MLDSA_KEY_SIZE_E); |
2017 | | } |
2018 | | break; |
2019 | | #endif /* WOLFSSL_HAVE_MLDSA */ |
2020 | | #if defined(WOLFSSL_HAVE_SLHDSA) |
2021 | | case SLH_DSA_SHA2_128Sk: |
2022 | | case SLH_DSA_SHA2_128Fk: |
2023 | | case SLH_DSA_SHA2_192Sk: |
2024 | | case SLH_DSA_SHA2_192Fk: |
2025 | | case SLH_DSA_SHA2_256Sk: |
2026 | | case SLH_DSA_SHA2_256Fk: |
2027 | | case SLH_DSA_SHAKE_128Sk: |
2028 | | case SLH_DSA_SHAKE_128Fk: |
2029 | | case SLH_DSA_SHAKE_192Sk: |
2030 | | case SLH_DSA_SHAKE_192Fk: |
2031 | | case SLH_DSA_SHAKE_256Sk: |
2032 | | case SLH_DSA_SHAKE_256Fk: |
2033 | | { |
2034 | | int slhParam = wc_SlhDsaOidToParam((int)cert->keyOID); |
2035 | | int keySzTemp; |
2036 | | |
2037 | | if (slhParam < 0) { |
2038 | | ret = ALGO_ID_E; |
2039 | | break; |
2040 | | } |
2041 | | keyType = SlhDsaParamToType(slhParam); |
2042 | | if (keyType == (byte)invalid_sa_algo) { |
2043 | | ret = ALGO_ID_E; |
2044 | | break; |
2045 | | } |
2046 | | /* SLH-DSA has fixed, small public keys and no minimum-size |
2047 | | * option; store the public key size for reference. checkKeySz is |
2048 | | * unused for SLH-DSA (this is the only key type without a size |
2049 | | * check, so mark it used to avoid -Wunused-parameter in a build |
2050 | | * where SLH-DSA is the only certificate signature algorithm). */ |
2051 | | keySzTemp = wc_SlhDsaKey_PublicSizeFromParam( |
2052 | | (enum SlhDsaParam)slhParam); |
2053 | | if (keySzTemp <= 0) { |
2054 | | ret = ALGO_ID_E; |
2055 | | break; |
2056 | | } |
2057 | | keySz = keySzTemp; |
2058 | | (void)checkKeySz; |
2059 | | break; |
2060 | | } |
2061 | | #endif /* WOLFSSL_HAVE_SLHDSA */ |
2062 | | |
2063 | 0 | default: |
2064 | 0 | WOLFSSL_MSG("No key size check done on public key in certificate"); |
2065 | 0 | break; |
2066 | 0 | } |
2067 | | |
2068 | | /* Store the type and key size as there may not be a private key set. */ |
2069 | 0 | if (ssl != NULL) { |
2070 | 0 | ssl->buffers.keyType = keyType; |
2071 | 0 | ssl->buffers.keySz = keySz; |
2072 | 0 | } |
2073 | 0 | else { |
2074 | 0 | ctx->privateKeyType = keyType; |
2075 | 0 | ctx->privateKeySz = keySz; |
2076 | 0 | } |
2077 | |
|
2078 | 0 | return ret; |
2079 | 0 | } |
2080 | | |
2081 | | #ifdef WOLFSSL_DUAL_ALG_CERTS |
2082 | | static int ProcessBufferCertAltPublicKey(WOLFSSL_CTX* ctx, WOLFSSL* ssl, |
2083 | | DecodedCert* cert, int checkKeySz) |
2084 | | { |
2085 | | int ret = 0; |
2086 | | void* heap = WOLFSSL_HEAP(ctx, ssl); |
2087 | | byte keyType = 0; |
2088 | | int keySz = 0; |
2089 | | #ifndef NO_RSA |
2090 | | word32 idx; |
2091 | | #endif |
2092 | | |
2093 | | /* Check alternative key size of cert. */ |
2094 | | switch (cert->sapkiOID) { |
2095 | | /* No OID set. */ |
2096 | | case 0: |
2097 | | if (cert->sapkiLen != 0) { |
2098 | | /* Have the alternative key data but no OID. */ |
2099 | | ret = NOT_COMPILED_IN; |
2100 | | } |
2101 | | break; |
2102 | | |
2103 | | #ifndef NO_RSA |
2104 | | #ifdef WC_RSA_PSS |
2105 | | case RSAPSSk: |
2106 | | #endif |
2107 | | case RSAk: |
2108 | | keyType = rsa_sa_algo; |
2109 | | /* Determine RSA key size by parsing public key */ |
2110 | | idx = 0; |
2111 | | ret = wc_RsaPublicKeyDecode_ex(cert->sapkiDer, &idx, |
2112 | | cert->sapkiLen, NULL, (word32*)&keySz, NULL, NULL); |
2113 | | if ((ret == 0) && checkKeySz) { |
2114 | | ret = CHECK_KEY_SZ(ssl ? ssl->options.minRsaKeySz : |
2115 | | ctx->minRsaKeySz, RSA_MAX_SIZE / 8, keySz, RSA_KEY_SIZE_E); |
2116 | | } |
2117 | | break; |
2118 | | #endif /* !NO_RSA */ |
2119 | | #ifdef HAVE_ECC |
2120 | | case ECDSAk: |
2121 | | { |
2122 | | WC_DECLARE_VAR(temp_key, ecc_key, 1, 0); |
2123 | | keyType = ecc_dsa_sa_algo; |
2124 | | |
2125 | | WC_ALLOC_VAR_EX(temp_key, ecc_key, 1, heap, DYNAMIC_TYPE_ECC, |
2126 | | ret=MEMORY_E); |
2127 | | |
2128 | | /* Determine ECC key size. We have to decode the sapki for |
2129 | | * that. */ |
2130 | | if (ret == 0) { |
2131 | | ret = wc_ecc_init_ex(temp_key, heap, INVALID_DEVID); |
2132 | | if (ret == 0) { |
2133 | | idx = 0; |
2134 | | ret = wc_EccPublicKeyDecode(cert->sapkiDer, &idx, temp_key, |
2135 | | cert->sapkiLen); |
2136 | | if (ret == 0) { |
2137 | | keySz = wc_ecc_size(temp_key); |
2138 | | } |
2139 | | wc_ecc_free(temp_key); |
2140 | | } |
2141 | | } |
2142 | | WC_FREE_VAR_EX(temp_key, heap, DYNAMIC_TYPE_ECC); |
2143 | | |
2144 | | if ((ret == 0) && checkKeySz) { |
2145 | | ret = CHECK_KEY_SZ(ssl ? ssl->options.minEccKeySz : |
2146 | | ctx->minEccKeySz, (MAX_ECC_BITS + 7) / 8, keySz, |
2147 | | ECC_KEY_SIZE_E); |
2148 | | } |
2149 | | break; |
2150 | | } |
2151 | | #endif /* HAVE_ECC */ |
2152 | | #if defined(WOLFSSL_SM2) && defined(WOLFSSL_SM3) |
2153 | | case SM2k: |
2154 | | keyType = sm2_sa_algo; |
2155 | | /* Determine ECC key size based on curve */ |
2156 | | keySz = WOLFSSL_SM2_KEY_BITS / 8; |
2157 | | if (checkKeySz) { |
2158 | | ret = CHECK_KEY_SZ(ssl ? ssl->options.minEccKeySz : |
2159 | | ctx->minEccKeySz, (MAX_ECC_BITS + 7) / 8, keySz, |
2160 | | ECC_KEY_SIZE_E); |
2161 | | } |
2162 | | break; |
2163 | | #endif /* WOLFSSL_SM2 && WOLFSSL_SM3 */ |
2164 | | #ifdef HAVE_ED25519 |
2165 | | case ED25519k: |
2166 | | keyType = ed25519_sa_algo; |
2167 | | /* ED25519 is fixed key size */ |
2168 | | keySz = ED25519_KEY_SIZE; |
2169 | | if (checkKeySz) { |
2170 | | ret = CHECK_KEY_SZ(ssl ? ssl->options.minEccKeySz : |
2171 | | ctx->minEccKeySz, ED25519_KEY_SIZE, keySz, ECC_KEY_SIZE_E); |
2172 | | } |
2173 | | break; |
2174 | | #endif /* HAVE_ED25519 */ |
2175 | | #ifdef HAVE_ED448 |
2176 | | case ED448k: |
2177 | | keyType = ed448_sa_algo; |
2178 | | /* ED448 is fixed key size */ |
2179 | | keySz = ED448_KEY_SIZE; |
2180 | | if (checkKeySz) { |
2181 | | ret = CHECK_KEY_SZ(ssl ? ssl->options.minEccKeySz : |
2182 | | ctx->minEccKeySz, ED448_KEY_SIZE, keySz, ECC_KEY_SIZE_E); |
2183 | | } |
2184 | | break; |
2185 | | #endif /* HAVE_ED448 */ |
2186 | | #if defined(HAVE_FALCON) |
2187 | | case FALCON_LEVEL1k: |
2188 | | keyType = falcon_level1_sa_algo; |
2189 | | /* Falcon is fixed key size */ |
2190 | | keySz = FALCON_LEVEL1_KEY_SIZE; |
2191 | | if (checkKeySz) { |
2192 | | #ifdef WOLFSSL_NO_FALCON_LEVEL1 |
2193 | | ret = NOT_COMPILED_IN; |
2194 | | #else |
2195 | | ret = CHECK_KEY_SZ(ssl ? ssl->options.minFalconKeySz : |
2196 | | ctx->minFalconKeySz, FALCON_MAX_KEY_SIZE, keySz, |
2197 | | FALCON_KEY_SIZE_E); |
2198 | | #endif |
2199 | | } |
2200 | | break; |
2201 | | case FALCON_LEVEL5k: |
2202 | | keyType = falcon_level5_sa_algo; |
2203 | | /* Falcon is fixed key size */ |
2204 | | keySz = FALCON_LEVEL5_KEY_SIZE; |
2205 | | if (checkKeySz) { |
2206 | | #ifdef WOLFSSL_NO_FALCON_LEVEL5 |
2207 | | ret = NOT_COMPILED_IN; |
2208 | | #else |
2209 | | ret = CHECK_KEY_SZ(ssl ? ssl->options.minFalconKeySz : |
2210 | | ctx->minFalconKeySz, FALCON_MAX_KEY_SIZE, keySz, |
2211 | | FALCON_KEY_SIZE_E); |
2212 | | #endif |
2213 | | } |
2214 | | break; |
2215 | | #endif /* HAVE_FALCON */ |
2216 | | #if defined(WOLFSSL_HAVE_MLDSA) |
2217 | | #ifdef WOLFSSL_MLDSA_FIPS204_DRAFT |
2218 | | case DILITHIUM_LEVEL2k: |
2219 | | keyType = mldsa_44_sa_algo; |
2220 | | /* Dilithium is fixed key size */ |
2221 | | keySz = WC_MLDSA_44_KEY_SIZE; |
2222 | | if (checkKeySz) { |
2223 | | ret = CHECK_KEY_SZ(ssl ? ssl->options.minMlDsaKeySz : |
2224 | | ctx->minMlDsaKeySz, MLDSA_MAX_KEY_SIZE, keySz, |
2225 | | MLDSA_KEY_SIZE_E); |
2226 | | } |
2227 | | break; |
2228 | | case DILITHIUM_LEVEL3k: |
2229 | | keyType = mldsa_65_sa_algo; |
2230 | | /* Dilithium is fixed key size */ |
2231 | | keySz = WC_MLDSA_65_KEY_SIZE; |
2232 | | if (checkKeySz) { |
2233 | | ret = CHECK_KEY_SZ(ssl ? ssl->options.minMlDsaKeySz : |
2234 | | ctx->minMlDsaKeySz, MLDSA_MAX_KEY_SIZE, keySz, |
2235 | | MLDSA_KEY_SIZE_E); |
2236 | | } |
2237 | | break; |
2238 | | case DILITHIUM_LEVEL5k: |
2239 | | keyType = mldsa_87_sa_algo; |
2240 | | /* Dilithium is fixed key size */ |
2241 | | keySz = WC_MLDSA_87_KEY_SIZE; |
2242 | | if (checkKeySz) { |
2243 | | ret = CHECK_KEY_SZ(ssl ? ssl->options.minMlDsaKeySz : |
2244 | | ctx->minMlDsaKeySz, MLDSA_MAX_KEY_SIZE, keySz, |
2245 | | MLDSA_KEY_SIZE_E); |
2246 | | } |
2247 | | break; |
2248 | | #endif /* WOLFSSL_MLDSA_FIPS204_DRAFT */ |
2249 | | case ML_DSA_44k: |
2250 | | keyType = mldsa_44_sa_algo; |
2251 | | /* Dilithium is fixed key size */ |
2252 | | keySz = WC_MLDSA_44_KEY_SIZE; |
2253 | | if (checkKeySz) { |
2254 | | ret = CHECK_KEY_SZ(ssl ? ssl->options.minMlDsaKeySz : |
2255 | | ctx->minMlDsaKeySz, MLDSA_MAX_KEY_SIZE, keySz, |
2256 | | MLDSA_KEY_SIZE_E); |
2257 | | } |
2258 | | break; |
2259 | | case ML_DSA_65k: |
2260 | | keyType = mldsa_65_sa_algo; |
2261 | | /* Dilithium is fixed key size */ |
2262 | | keySz = WC_MLDSA_65_KEY_SIZE; |
2263 | | if (checkKeySz) { |
2264 | | ret = CHECK_KEY_SZ(ssl ? ssl->options.minMlDsaKeySz : |
2265 | | ctx->minMlDsaKeySz, MLDSA_MAX_KEY_SIZE, keySz, |
2266 | | MLDSA_KEY_SIZE_E); |
2267 | | } |
2268 | | break; |
2269 | | case ML_DSA_87k: |
2270 | | keyType = mldsa_87_sa_algo; |
2271 | | /* Dilithium is fixed key size */ |
2272 | | keySz = WC_MLDSA_87_KEY_SIZE; |
2273 | | if (checkKeySz) { |
2274 | | ret = CHECK_KEY_SZ(ssl ? ssl->options.minMlDsaKeySz : |
2275 | | ctx->minMlDsaKeySz, MLDSA_MAX_KEY_SIZE, keySz, |
2276 | | MLDSA_KEY_SIZE_E); |
2277 | | } |
2278 | | break; |
2279 | | #endif /* WOLFSSL_HAVE_MLDSA */ |
2280 | | #if defined(WOLFSSL_HAVE_SLHDSA) |
2281 | | case SLH_DSA_SHA2_128Sk: |
2282 | | case SLH_DSA_SHA2_128Fk: |
2283 | | case SLH_DSA_SHA2_192Sk: |
2284 | | case SLH_DSA_SHA2_192Fk: |
2285 | | case SLH_DSA_SHA2_256Sk: |
2286 | | case SLH_DSA_SHA2_256Fk: |
2287 | | case SLH_DSA_SHAKE_128Sk: |
2288 | | case SLH_DSA_SHAKE_128Fk: |
2289 | | case SLH_DSA_SHAKE_192Sk: |
2290 | | case SLH_DSA_SHAKE_192Fk: |
2291 | | case SLH_DSA_SHAKE_256Sk: |
2292 | | case SLH_DSA_SHAKE_256Fk: |
2293 | | { |
2294 | | /* This switch is over the alternative key's OID, so the |
2295 | | * parameter set comes from sapkiOID; keyOID describes the native |
2296 | | * key and would name a different algorithm entirely. */ |
2297 | | int slhParam = wc_SlhDsaOidToParam((int)cert->sapkiOID); |
2298 | | int keySzTemp; |
2299 | | |
2300 | | if (slhParam < 0) { |
2301 | | ret = ALGO_ID_E; |
2302 | | break; |
2303 | | } |
2304 | | keyType = SlhDsaParamToType(slhParam); |
2305 | | if (keyType == (byte)invalid_sa_algo) { |
2306 | | ret = ALGO_ID_E; |
2307 | | break; |
2308 | | } |
2309 | | keySzTemp = wc_SlhDsaKey_PublicSizeFromParam( |
2310 | | (enum SlhDsaParam)slhParam); |
2311 | | if (keySzTemp <= 0) { |
2312 | | ret = ALGO_ID_E; |
2313 | | break; |
2314 | | } |
2315 | | keySz = keySzTemp; |
2316 | | break; |
2317 | | } |
2318 | | #endif /* WOLFSSL_HAVE_SLHDSA */ |
2319 | | |
2320 | | default: |
2321 | | /* In this case, there was an OID that we didn't recognize. |
2322 | | * This is an error. Use not compiled in because likely the |
2323 | | * given algorithm was not enabled. */ |
2324 | | ret = NOT_COMPILED_IN; |
2325 | | WOLFSSL_MSG("No alt key size check done on certificate"); |
2326 | | break; |
2327 | | } |
2328 | | |
2329 | | if (ssl != NULL) { |
2330 | | ssl->buffers.altKeyType = (byte)keyType; |
2331 | | ssl->buffers.altKeySz = keySz; |
2332 | | } |
2333 | | else if (ctx != NULL) { |
2334 | | ctx->altPrivateKeyType = (byte)keyType; |
2335 | | ctx->altPrivateKeySz = keySz; |
2336 | | } |
2337 | | |
2338 | | return ret; |
2339 | | } |
2340 | | #endif /* WOLFSSL_DUAL_ALG_CERTS */ |
2341 | | |
2342 | | /* Parse the certificate and pull out information for TLS handshake. |
2343 | | * |
2344 | | * @param [in, out] ctx SSL context object. |
2345 | | * @param [in, out] ssl SSL object. |
2346 | | * @param [in] der DER encoded X509 certificate. |
2347 | | * @return 0 on success. |
2348 | | * @return MEMORY_E when dynamic memory allocation fails. |
2349 | | * @return WOLFSSL_BAD_FILE when decoding certificate fails. |
2350 | | */ |
2351 | | static int ProcessBufferCert(WOLFSSL_CTX* ctx, WOLFSSL* ssl, DerBuffer* der) |
2352 | 0 | { |
2353 | 0 | int ret = 0; |
2354 | 0 | void* heap = WOLFSSL_HEAP(ctx, ssl); |
2355 | | #if defined(HAVE_RPK) |
2356 | | RpkState* rpkState = ssl ? &ssl->options.rpkState : &ctx->rpkState; |
2357 | | #endif |
2358 | 0 | WC_DECLARE_VAR(cert, DecodedCert, 1, 0); |
2359 | | |
2360 | | /* Allocate memory for certificate to be decoded into. */ |
2361 | 0 | WC_ALLOC_VAR_EX(cert, DecodedCert, 1, heap, DYNAMIC_TYPE_DCERT, |
2362 | 0 | ret=MEMORY_E); |
2363 | 0 | if (WC_VAR_OK(cert)) |
2364 | 0 | { |
2365 | | /* Get device id from SSL context or SSL object. */ |
2366 | 0 | int devId = wolfSSL_CTX_GetDevId(ctx, ssl); |
2367 | |
|
2368 | 0 | WOLFSSL_MSG("Checking cert signature type"); |
2369 | | /* Initialize certificate object. */ |
2370 | 0 | InitDecodedCert_ex(cert, der->buffer, der->length, heap, devId); |
2371 | | |
2372 | | /* Decode up to and including public key. */ |
2373 | 0 | if (DecodeToKey(cert, 0) < 0) { |
2374 | 0 | WOLFSSL_MSG("Decode to key failed"); |
2375 | 0 | ret = WOLFSSL_BAD_FILE; |
2376 | 0 | } |
2377 | 0 | if (ret == 0) { |
2378 | 0 | int checkKeySz = 1; |
2379 | |
|
2380 | | #if defined(HAVE_RPK) |
2381 | | /* Store whether the crtificate is a raw public key. */ |
2382 | | rpkState->isRPKLoaded = cert->isRPK; |
2383 | | #endif /* HAVE_RPK */ |
2384 | | |
2385 | | /* Set which private key algorithm we have. */ |
2386 | 0 | ProcessBufferCertSetHave(ctx, ssl, cert); |
2387 | | |
2388 | | /* Don't check if verification is disabled for SSL. */ |
2389 | 0 | if ((ssl != NULL) && ssl->options.verifyNone) { |
2390 | 0 | checkKeySz = 0; |
2391 | 0 | } |
2392 | | /* Don't check if no SSL object verification is disabled for SSL |
2393 | | * context. */ |
2394 | 0 | else if ((ssl == NULL) && (ctx != NULL) && ctx->verifyNone) { |
2395 | 0 | checkKeySz = 0; |
2396 | 0 | } |
2397 | | |
2398 | | /* Check public key size. */ |
2399 | 0 | ret = ProcessBufferCertPublicKey(ctx, ssl, cert, checkKeySz); |
2400 | | #ifdef WOLFSSL_DUAL_ALG_CERTS |
2401 | | if (ret == 0) { |
2402 | | ret = ProcessBufferCertAltPublicKey(ctx, ssl, cert, checkKeySz); |
2403 | | } |
2404 | | #endif |
2405 | 0 | } |
2406 | 0 | } |
2407 | | |
2408 | | /* Dispose of dynamic memory in certificate object. */ |
2409 | 0 | FreeDecodedCert(cert); |
2410 | 0 | WC_FREE_VAR_EX(cert, heap, DYNAMIC_TYPE_DCERT); |
2411 | 0 | return ret; |
2412 | 0 | } |
2413 | | |
2414 | | /* Handle storing the DER encoding of the certificate. |
2415 | | * |
2416 | | * Do not free der outside of this function. |
2417 | | * |
2418 | | * @param [in, out] ctx SSL context object. |
2419 | | * @param [in, out] ssl SSL object. |
2420 | | * @param [in] der DER encoded certificate. |
2421 | | * @param [in] type Type of data: |
2422 | | * CERT_TYPE, CA_TYPE or TRUSTED_PEER_TYPE. |
2423 | | * @param [in] verify What verification to do. |
2424 | | * @return 0 on success. |
2425 | | * @return BAD_FUNC_ARG when type is CA_TYPE and ctx is NULL. |
2426 | | * @return WOLFSSL_BAD_CERTTYPE when data type is not supported. |
2427 | | */ |
2428 | | static int ProcessBufferCertHandleDer(WOLFSSL_CTX* ctx, WOLFSSL* ssl, |
2429 | | DerBuffer* der, int type, int verify) |
2430 | 0 | { |
2431 | 0 | int ret = 0; |
2432 | | |
2433 | | /* CA certificate to verify with. */ |
2434 | 0 | if (type == CA_TYPE) { |
2435 | | #ifdef WOLFSSL_TEST_APPLE_NATIVE_CERT_VALIDATION |
2436 | | /* TEST ONLY CODE: |
2437 | | * Store the DER encoding of the CA certificate so we can append it to |
2438 | | * the list of trusted CA certificates if the subsequent call to AddCA |
2439 | | * is successful */ |
2440 | | word32 derLen; |
2441 | | byte* derBuf; |
2442 | | if (ctx->doAppleNativeCertValidationFlag == 1) { |
2443 | | WOLFSSL_MSG("ANCV Test: copy DER CA cert"); |
2444 | | derLen = der->length; |
2445 | | derBuf = (byte*)XMALLOC(derLen, NULL, DYNAMIC_TYPE_TMP_BUFFER); |
2446 | | if (derBuf == NULL) { |
2447 | | return MEMORY_E; |
2448 | | } |
2449 | | XMEMCPY(derBuf, der->buffer, derLen); |
2450 | | } |
2451 | | else { |
2452 | | (void)derLen; |
2453 | | (void)derBuf; |
2454 | | } |
2455 | | #endif |
2456 | | /* verify CA unless user set to no verify */ |
2457 | 0 | ret = AddCA(ctx->cm, &der, WOLFSSL_USER_CA, verify); |
2458 | |
|
2459 | | #ifdef WOLFSSL_TEST_APPLE_NATIVE_CERT_VALIDATION |
2460 | | /* TEST ONLY CODE: |
2461 | | * Append the DER encoded CA certificate to the list of trusted CA |
2462 | | * certificates so we can inject them at verification time */ |
2463 | | if (ret == 1 && ctx->doAppleNativeCertValidationFlag == 1) { |
2464 | | WOLFSSL_MSG("ANCV Test: Appending CA to cert list"); |
2465 | | ret = wolfSSL_TestAppleNativeCertValidation_AppendCA(ctx, derBuf, |
2466 | | (int)derLen); |
2467 | | if (ret == WOLFSSL_SUCCESS) { |
2468 | | WOLFSSL_MSG("ANCV Test: Clearing CA table"); |
2469 | | /* Clear the CA table so we can ensure they won't be used for |
2470 | | * verification */ |
2471 | | ret = wolfSSL_CertManagerUnloadCAs(ctx->cm); |
2472 | | if (ret == WOLFSSL_SUCCESS) { |
2473 | | ret = 0; |
2474 | | } |
2475 | | } |
2476 | | XFREE(derBuf, NULL, DYNAMIC_TYPE_TMP_BUFFER); |
2477 | | } |
2478 | | #endif /* !WOLFSSL_TEST_APPLE_NATIVE_CERT_VALIDATION */ |
2479 | |
|
2480 | 0 | if (ret == 1) { |
2481 | 0 | ret = 0; |
2482 | 0 | } |
2483 | 0 | } |
2484 | | #ifdef WOLFSSL_TRUST_PEER_CERT |
2485 | | /* Trusted certificate to verify peer with. */ |
2486 | | else if (type == TRUSTED_PEER_TYPE) { |
2487 | | WOLFSSL_CERT_MANAGER* cm; |
2488 | | |
2489 | | /* Get certificate manager to add certificate to. */ |
2490 | | if (ctx != NULL) { |
2491 | | cm = ctx->cm; |
2492 | | } |
2493 | | else { |
2494 | | SSL_CM_WARNING(ssl); |
2495 | | cm = SSL_CM(ssl); |
2496 | | } |
2497 | | /* Add certificate as a trusted peer. */ |
2498 | | ret = AddTrustedPeer(cm, &der, verify); |
2499 | | if (ret != 1) { |
2500 | | WOLFSSL_MSG("Error adding trusted peer"); |
2501 | | } |
2502 | | } |
2503 | | #endif /* WOLFSSL_TRUST_PEER_CERT */ |
2504 | | /* Leaf certificate - our certificate. */ |
2505 | 0 | else if (type == CERT_TYPE) { |
2506 | 0 | if (ssl != NULL) { |
2507 | | /* Free previous certificate if we own it. */ |
2508 | 0 | if (ssl->buffers.weOwnCert) { |
2509 | 0 | FreeDer(&ssl->buffers.certificate); |
2510 | | #ifdef KEEP_OUR_CERT |
2511 | | /* Dispose of X509 version of certificate. */ |
2512 | | wolfSSL_X509_free(ssl->ourCert); |
2513 | | ssl->ourCert = NULL; |
2514 | | #endif |
2515 | 0 | } |
2516 | | /* Store certificate as ours. */ |
2517 | 0 | ssl->buffers.certificate = der; |
2518 | | #ifdef KEEP_OUR_CERT |
2519 | | ssl->keepCert = 1; /* hold cert for ssl lifetime */ |
2520 | | #endif |
2521 | | /* We have to free the certificate buffer. */ |
2522 | 0 | ssl->buffers.weOwnCert = 1; |
2523 | | /* ourCert is created on demand. */ |
2524 | 0 | } |
2525 | 0 | else if (ctx != NULL) { |
2526 | | /* Free previous certificate. */ |
2527 | 0 | FreeDer(&ctx->certificate); /* Make sure previous is free'd */ |
2528 | | #ifdef KEEP_OUR_CERT |
2529 | | /* Dispose of X509 version of certificate if we own it. */ |
2530 | | if (ctx->ownOurCert) { |
2531 | | wolfSSL_X509_free(ctx->ourCert); |
2532 | | } |
2533 | | ctx->ourCert = NULL; |
2534 | | #endif |
2535 | | /* Store certificate as ours. */ |
2536 | 0 | ctx->certificate = der; |
2537 | | /* ourCert is created on demand. */ |
2538 | 0 | } |
2539 | 0 | } |
2540 | 0 | else { |
2541 | | /* Dispose of DER buffer. */ |
2542 | 0 | FreeDer(&der); |
2543 | | /* Not a certificate type supported. */ |
2544 | 0 | ret = WOLFSSL_BAD_CERTTYPE; |
2545 | 0 | } |
2546 | |
|
2547 | 0 | return ret; |
2548 | 0 | } |
2549 | | |
2550 | | /* Process certificate based on type. |
2551 | | * |
2552 | | * @param [in, out] ctx SSL context object. |
2553 | | * @param [in, out] ssl SSL object. |
2554 | | * @param [in] buff Buffer holding original data. |
2555 | | * @param [in] sz Size of data in buffer. |
2556 | | * @param [in] der DER encoding of certificate. |
2557 | | * @param [in] format Format of data. |
2558 | | * @param [in] type Type of data: |
2559 | | * CERT_TYPE, CA_TYPE or TRUSTED_PEER_TYPE. |
2560 | | * @param [in] verify What verification to do. |
2561 | | * @return 0 on success. |
2562 | | * @return WOLFSSL_FATAL_ERROR on failure. |
2563 | | */ |
2564 | | static int ProcessBufferCertTypes(WOLFSSL_CTX* ctx, WOLFSSL* ssl, |
2565 | | const unsigned char* buff, long sz, DerBuffer* der, int format, int type, |
2566 | | int verify) |
2567 | 0 | { |
2568 | 0 | int ret; |
2569 | |
|
2570 | 0 | (void)buff; |
2571 | 0 | (void)sz; |
2572 | 0 | (void)format; |
2573 | |
|
2574 | 0 | ret = ProcessBufferCertHandleDer(ctx, ssl, der, type, verify); |
2575 | 0 | if ((ret == 0) && (type == CERT_TYPE)) { |
2576 | | /* Process leaf certificate. */ |
2577 | 0 | ret = ProcessBufferCert(ctx, ssl, der); |
2578 | 0 | } |
2579 | 0 | #if !defined(NO_WOLFSSL_CM_VERIFY) && (!defined(NO_WOLFSSL_CLIENT) || \ |
2580 | 0 | !defined(WOLFSSL_NO_CLIENT_AUTH)) |
2581 | | /* Hand bad CA or user certificate to callback. */ |
2582 | 0 | if ((ret < 0) && ((type == CA_TYPE) || (type == CERT_TYPE))) { |
2583 | | /* Check for verification callback that may override error. */ |
2584 | 0 | if ((ctx != NULL) && (ctx->cm != NULL) && |
2585 | 0 | (ctx->cm->verifyCallback != NULL)) { |
2586 | | /* Verify and use callback. */ |
2587 | 0 | ret = CM_VerifyBuffer_ex(ctx->cm, buff, sz, format, ret); |
2588 | | /* Convert error. */ |
2589 | 0 | if (ret == 0) { |
2590 | 0 | ret = WOLFSSL_FATAL_ERROR; |
2591 | 0 | } |
2592 | 0 | if (ret == 1) { |
2593 | 0 | ret = 0; |
2594 | 0 | } |
2595 | 0 | } |
2596 | 0 | } |
2597 | 0 | #endif /* NO_WOLFSSL_CM_VERIFY */ |
2598 | |
|
2599 | 0 | return ret; |
2600 | 0 | } |
2601 | | |
2602 | | /* Reset the cipher suites based on updated private key or certificate. |
2603 | | * |
2604 | | * @param [in, out] ctx SSL context object. |
2605 | | * @param [in, out] ssl SSL object. |
2606 | | * @param [in] type Type of certificate. |
2607 | | * @return 0 on success. |
2608 | | * @return WOLFSSL_FATAL_ERROR when allocation fails. |
2609 | | */ |
2610 | | static int ProcessBufferResetSuites(WOLFSSL_CTX* ctx, WOLFSSL* ssl, int type) |
2611 | 0 | { |
2612 | 0 | int ret = 0; |
2613 | | |
2614 | | /* Reset suites of SSL object. */ |
2615 | 0 | if (ssl != NULL) { |
2616 | 0 | if (ssl->options.side == WOLFSSL_SERVER_END) { |
2617 | | /* Allocate memory for suites. */ |
2618 | 0 | if (AllocateSuites(ssl) != 0) { |
2619 | 0 | ret = WOLFSSL_FATAL_ERROR; |
2620 | 0 | } |
2621 | 0 | else { |
2622 | | /* Determine cipher suites based on what we have. */ |
2623 | 0 | InitSuites(ssl->suites, ssl->version, ssl->buffers.keySz, |
2624 | 0 | WOLFSSL_HAVE_RSA, SSL_HAVE_PSK(ssl), ssl->options.haveDH, |
2625 | 0 | ssl->options.haveECDSAsig, ssl->options.haveECC, TRUE, |
2626 | 0 | ssl->options.haveStaticECC, |
2627 | 0 | ssl->options.useAnon, TRUE, |
2628 | 0 | TRUE, TRUE, TRUE, ssl->options.side); |
2629 | 0 | } |
2630 | 0 | } |
2631 | 0 | } |
2632 | | /* Reset suites of SSL context object. */ |
2633 | 0 | else if ((type == CERT_TYPE) && (ctx->method->side == WOLFSSL_SERVER_END)) { |
2634 | | /* Allocate memory for suites. */ |
2635 | 0 | if (AllocateCtxSuites(ctx) != 0) { |
2636 | 0 | ret = WOLFSSL_FATAL_ERROR; |
2637 | 0 | } |
2638 | 0 | else { |
2639 | | /* Determine cipher suites based on what we have. */ |
2640 | 0 | InitSuites(ctx->suites, ctx->method->version, ctx->privateKeySz, |
2641 | 0 | WOLFSSL_HAVE_RSA, CTX_HAVE_PSK(ctx), ctx->haveDH, |
2642 | 0 | ctx->haveECDSAsig, ctx->haveECC, TRUE, ctx->haveStaticECC, |
2643 | 0 | CTX_USE_ANON(ctx), |
2644 | 0 | TRUE, TRUE, TRUE, TRUE, ctx->method->side); |
2645 | 0 | } |
2646 | 0 | } |
2647 | |
|
2648 | 0 | return ret; |
2649 | 0 | } |
2650 | | |
2651 | | #ifndef WOLFSSL_DUAL_ALG_CERTS |
2652 | | /* Determine whether the type is for a private key. */ |
2653 | 0 | #define IS_PRIVKEY_TYPE(type) ((type) == PRIVATEKEY_TYPE) |
2654 | | #else |
2655 | | /* Determine whether the type is for a private key. */ |
2656 | | #define IS_PRIVKEY_TYPE(type) (((type) == PRIVATEKEY_TYPE) || \ |
2657 | | ((type) == ALT_PRIVATEKEY_TYPE)) |
2658 | | #endif |
2659 | | |
2660 | | /* Process a buffer of data. |
2661 | | * |
2662 | | * Data type is a private key or a certificate. |
2663 | | * The format can be ASN.1 (DER) or PEM. |
2664 | | * |
2665 | | * @param [in, out] ctx SSL context object. |
2666 | | * @param [in] buff Buffer holding data. |
2667 | | * @param [in] sz Size of data in buffer. |
2668 | | * @param [in] format Format of data: |
2669 | | * WOLFSSL_FILETYPE_PEM or WOLFSSL_FILETYPE_ASN1. |
2670 | | * @param [in] type Type of data: |
2671 | | * CERT_TYPE, CA_TYPE, TRUSTED_PEER_TYPE, |
2672 | | * PRIVATEKEY_TYPE or ALT_PRIVATEKEY_TYPE. |
2673 | | * @param [in, out] ssl SSL object. |
2674 | | * @param [out] used Number of bytes consumed. |
2675 | | * @param [in[ userChain Whether this certificate is for user's chain. |
2676 | | * @param [in] verify How to verify certificate. |
2677 | | * @param [in] source_name Associated filename or other source ID. |
2678 | | * @return 1 on success. |
2679 | | * @return Less than 1 on failure. |
2680 | | */ |
2681 | | int ProcessBuffer(WOLFSSL_CTX* ctx, const unsigned char* buff, long sz, |
2682 | | int format, int type, WOLFSSL* ssl, long* used, int userChain, int verify, |
2683 | | const char *source_name) |
2684 | 0 | { |
2685 | 0 | DerBuffer* der = NULL; |
2686 | 0 | int ret = 0; |
2687 | 0 | void* heap = WOLFSSL_HEAP(ctx, ssl); |
2688 | 0 | WC_DECLARE_VAR(info, EncryptedInfo, 1, 0); |
2689 | 0 | int algId = 0; |
2690 | | #ifdef WOLFSSL_DEBUG_CERTIFICATE_LOADS |
2691 | | long usedAtStart = used ? *used : 0L; |
2692 | | #else |
2693 | 0 | (void)source_name; |
2694 | 0 | #endif |
2695 | |
|
2696 | 0 | WOLFSSL_ENTER("ProcessBuffer"); |
2697 | | |
2698 | | /* Check data format is supported. */ |
2699 | 0 | if ((format != WOLFSSL_FILETYPE_ASN1) && (format != WOLFSSL_FILETYPE_PEM)) { |
2700 | 0 | ret = WOLFSSL_BAD_FILETYPE; |
2701 | 0 | } |
2702 | | /* Need an object to store certificate into. */ |
2703 | 0 | if ((ret == 0) && (ctx == NULL) && (ssl == NULL)) { |
2704 | 0 | ret = BAD_FUNC_ARG; |
2705 | 0 | } |
2706 | | /* CA certificates go into the SSL context object. */ |
2707 | 0 | if ((ret == 0) && (ctx == NULL) && (type == CA_TYPE)) { |
2708 | 0 | ret = BAD_FUNC_ARG; |
2709 | 0 | } |
2710 | | /* This API does not handle CHAIN_CERT_TYPE */ |
2711 | 0 | if ((ret == 0) && (type == CHAIN_CERT_TYPE)) { |
2712 | 0 | ret = BAD_FUNC_ARG; |
2713 | 0 | } |
2714 | | /* Reject negative size - would wrap to huge word32. */ |
2715 | 0 | if ((ret == 0) && (sz < 0)) { |
2716 | 0 | ret = BAD_FUNC_ARG; |
2717 | 0 | } |
2718 | |
|
2719 | | #ifdef WOLFSSL_SMALL_STACK |
2720 | | if (ret == 0) { |
2721 | | /* Allocate memory for encryption information. */ |
2722 | | info = (EncryptedInfo*)XMALLOC(sizeof(EncryptedInfo), heap, |
2723 | | DYNAMIC_TYPE_ENCRYPTEDINFO); |
2724 | | if (info == NULL) { |
2725 | | ret = MEMORY_E; |
2726 | | } |
2727 | | } |
2728 | | #endif |
2729 | 0 | if (ret == 0) { |
2730 | | /* Initialize encryption information. */ |
2731 | 0 | XMEMSET(info, 0, sizeof(EncryptedInfo)); |
2732 | | #if defined(WOLFSSL_ENCRYPTED_KEYS) && !defined(NO_PWDBASED) |
2733 | | if (ctx != NULL) { |
2734 | | info->passwd_cb = ctx->passwd_cb; |
2735 | | info->passwd_userdata = ctx->passwd_userdata; |
2736 | | } |
2737 | | #endif |
2738 | | |
2739 | | /* Get the DER data for a private key or certificate. */ |
2740 | 0 | ret = DataToDerBuffer(buff, (word32)sz, format, type, info, heap, &der, |
2741 | 0 | &algId); |
2742 | 0 | if (used != NULL) { |
2743 | | /* Update to amount used/consumed. */ |
2744 | 0 | *used = info->consumed; |
2745 | 0 | } |
2746 | | #ifdef WOLFSSL_SMALL_STACK |
2747 | | if (ret != 0) { |
2748 | | /* Info no longer needed as loading failed. */ |
2749 | | XFREE(info, heap, DYNAMIC_TYPE_ENCRYPTEDINFO); |
2750 | | } |
2751 | | #endif |
2752 | 0 | } |
2753 | |
|
2754 | 0 | if ((ret == 0) && IS_PRIVKEY_TYPE(type)) { |
2755 | | /* Process the private key. */ |
2756 | 0 | ret = ProcessBufferPrivateKey(ctx, ssl, der, format, info, heap, type, |
2757 | 0 | algId); |
2758 | 0 | WC_FREE_VAR_EX(info, heap, DYNAMIC_TYPE_ENCRYPTEDINFO); |
2759 | 0 | } |
2760 | 0 | else if (ret == 0) { |
2761 | | /* Processing a certificate. */ |
2762 | 0 | if (userChain) { |
2763 | | /* Take original buffer and add to user chain to send in TLS |
2764 | | * handshake. */ |
2765 | 0 | ret = ProcessUserChain(ctx, ssl, buff, sz, format, type, used, info, |
2766 | 0 | verify); |
2767 | | /* Additional chain is optional */ |
2768 | 0 | if (ret == WC_NO_ERR_TRACE(ASN_NO_PEM_HEADER)) { |
2769 | 0 | unsigned long pemErr = 0; |
2770 | 0 | CLEAR_ASN_NO_PEM_HEADER_ERROR(pemErr); |
2771 | 0 | ret = 0; |
2772 | 0 | } |
2773 | | #ifdef WOLFSSL_DEBUG_CERTIFICATE_LOADS |
2774 | | if (ret < 0) { |
2775 | | #ifdef NO_ERROR_STRINGS |
2776 | | WOLFSSL_DEBUG_PRINTF( |
2777 | | "ERROR: ProcessUserChain: certificate from %s at offset %ld" |
2778 | | " rejected with code %d\n", |
2779 | | source_name, usedAtStart, ret); |
2780 | | #else |
2781 | | WOLFSSL_DEBUG_PRINTF( |
2782 | | "ERROR: ProcessUserChain: certificate from %s at offset %ld" |
2783 | | " rejected with code %d: %s\n", |
2784 | | source_name, usedAtStart, ret, |
2785 | | wolfSSL_ERR_reason_error_string(ret)); |
2786 | | #endif |
2787 | | } |
2788 | | #endif /* WOLFSSL_DEBUG_CERTIFICATE_LOADS */ |
2789 | 0 | } |
2790 | |
|
2791 | 0 | WC_FREE_VAR_EX(info, heap, DYNAMIC_TYPE_ENCRYPTEDINFO); |
2792 | |
|
2793 | 0 | if (ret == 0) { |
2794 | | /* Process the different types of certificates. */ |
2795 | 0 | ret = ProcessBufferCertTypes(ctx, ssl, buff, sz, der, format, type, |
2796 | 0 | verify); |
2797 | | #ifdef WOLFSSL_DEBUG_CERTIFICATE_LOADS |
2798 | | if (ret < 0) { |
2799 | | #ifdef NO_ERROR_STRINGS |
2800 | | WOLFSSL_DEBUG_PRINTF( |
2801 | | "ERROR: ProcessBufferCertTypes: certificate from %s at" |
2802 | | " offset %ld rejected with code %d\n", |
2803 | | source_name, usedAtStart, ret); |
2804 | | #else |
2805 | | WOLFSSL_DEBUG_PRINTF( |
2806 | | "ERROR: ProcessBufferCertTypes: certificate from %s at" |
2807 | | " offset %ld rejected with code %d: %s\n", |
2808 | | source_name, usedAtStart, ret, |
2809 | | wolfSSL_ERR_reason_error_string(ret)); |
2810 | | #endif |
2811 | | } |
2812 | | #endif /* WOLFSSL_DEBUG_CERTIFICATE_LOADS */ |
2813 | 0 | } |
2814 | 0 | else { |
2815 | 0 | FreeDer(&der); |
2816 | 0 | } |
2817 | 0 | } |
2818 | | |
2819 | | /* Reset suites if this is a private key or user certificate. */ |
2820 | 0 | if ((ret == 0) && ((type == PRIVATEKEY_TYPE) || (type == CERT_TYPE))) { |
2821 | 0 | ret = ProcessBufferResetSuites(ctx, ssl, type); |
2822 | 0 | } |
2823 | | |
2824 | | /* Convert return code. */ |
2825 | 0 | if (ret == 0) { |
2826 | 0 | ret = 1; |
2827 | 0 | } |
2828 | 0 | else if (ret == WC_NO_ERR_TRACE(WOLFSSL_FATAL_ERROR)) { |
2829 | 0 | ret = 0; |
2830 | 0 | } |
2831 | 0 | WOLFSSL_LEAVE("ProcessBuffer", ret); |
2832 | 0 | return ret; |
2833 | 0 | } |
2834 | | |
2835 | | #if defined(WOLFSSL_WPAS) && defined(HAVE_CRL) |
2836 | | /* Try to parse data as a PEM CRL. |
2837 | | * |
2838 | | * @param [in] ctx SSL context object. |
2839 | | * @param [in] buff Buffer containing potential CRL in PEM format. |
2840 | | * @param [in] sz Amount of data in buffer remaining. |
2841 | | * @param [out] consumed Number of bytes in buffer was the CRL. |
2842 | | * @return 0 on success. |
2843 | | */ |
2844 | | static int ProcessChainBufferCRL(WOLFSSL_CTX* ctx, const unsigned char* buff, |
2845 | | long sz, long* consumed) |
2846 | | { |
2847 | | int ret = 0; |
2848 | | DerBuffer* der = NULL; |
2849 | | WC_DECLARE_VAR(info, EncryptedInfo, 1, 0); |
2850 | | |
2851 | | WOLFSSL_MSG("Trying a CRL"); |
2852 | | |
2853 | | /* A PEM CRL has no Proc-Type header, so the encryption information must |
2854 | | * start zeroed or PemToDer() acts on a stale encrypted-key flag. */ |
2855 | | WC_CALLOC_VAR_EX(info, EncryptedInfo, 1, ctx->heap, |
2856 | | DYNAMIC_TYPE_ENCRYPTEDINFO, ret = MEMORY_E); |
2857 | | |
2858 | | if (ret == 0) { |
2859 | | ret = PemToDer(buff, sz, CRL_TYPE, &der, NULL, info, NULL); |
2860 | | } |
2861 | | if (ret == 0) { |
2862 | | WOLFSSL_MSG(" Processed a CRL"); |
2863 | | wolfSSL_CertManagerLoadCRLBuffer(ctx->cm, der->buffer, der->length, |
2864 | | WOLFSSL_FILETYPE_ASN1); |
2865 | | FreeDer(&der); |
2866 | | *consumed = info->consumed; |
2867 | | } |
2868 | | |
2869 | | WC_FREE_VAR_EX(info, ctx->heap, DYNAMIC_TYPE_ENCRYPTEDINFO); |
2870 | | |
2871 | | return ret; |
2872 | | } |
2873 | | #endif |
2874 | | |
2875 | | /* Process all chain certificates (and CRLs) in the PEM data. |
2876 | | * |
2877 | | * @param [in, out] ctx SSL context object. |
2878 | | * @param [in, out] ssl SSL object. |
2879 | | * @param [in] buff Buffer containing PEM data. |
2880 | | * @param [in] sz Size of data in buffer. |
2881 | | * @param [in] type Type of data. |
2882 | | * @param [in] verify How to verify certificate. |
2883 | | * @param [in] source_name Associated filename or other source ID. |
2884 | | * @return 1 on success. |
2885 | | * @return 0 on failure. |
2886 | | * @return MEMORY_E when dynamic memory allocation fails. |
2887 | | */ |
2888 | | static int ProcessChainBuffer(WOLFSSL_CTX* ctx, WOLFSSL* ssl, |
2889 | | const unsigned char* buff, long sz, int type, int verify, |
2890 | | const char *source_name) |
2891 | 0 | { |
2892 | 0 | int ret = 0; |
2893 | 0 | long used = 0; |
2894 | 0 | int gotOne = 0; |
2895 | |
|
2896 | 0 | WOLFSSL_MSG("Processing CA PEM file"); |
2897 | | /* Keep processing file while no errors and data to parse. */ |
2898 | 0 | while ((ret >= 0) && (used < sz)) { |
2899 | 0 | long consumed = used; |
2900 | | |
2901 | | /* Process the buffer. */ |
2902 | 0 | ret = ProcessBuffer(ctx, buff + used, sz - used, WOLFSSL_FILETYPE_PEM, |
2903 | 0 | type, ssl, &consumed, 0, verify, source_name); |
2904 | | /* Memory allocation failure is fatal. */ |
2905 | 0 | if (ret == WC_NO_ERR_TRACE(MEMORY_E)) { |
2906 | 0 | gotOne = 0; |
2907 | 0 | } |
2908 | | /* Other error parsing. */ |
2909 | 0 | else if (ret < 0) { |
2910 | | #if defined(WOLFSSL_WPAS) && defined(HAVE_CRL) |
2911 | | /* Try parsing a CRL. */ |
2912 | | if (ProcessChainBufferCRL(ctx, buff + used, sz - used, |
2913 | | &consumed) == 0) { |
2914 | | ret = 0; |
2915 | | } |
2916 | | else |
2917 | | #endif |
2918 | | /* Check whether we made progress. */ |
2919 | 0 | if (consumed > 0) { |
2920 | 0 | WOLFSSL_ERROR(ret); |
2921 | 0 | WOLFSSL_MSG("CA Parse failed, with progress in file."); |
2922 | 0 | WOLFSSL_MSG("Search for other certs in file"); |
2923 | | /* Check if we have more data to parse to recover. */ |
2924 | 0 | if (used + consumed < sz) { |
2925 | 0 | ret = 0; |
2926 | 0 | } |
2927 | 0 | } |
2928 | 0 | else { |
2929 | | /* No progress in parsing being made - stop here. */ |
2930 | 0 | WOLFSSL_MSG("CA Parse failed, no progress in file."); |
2931 | 0 | WOLFSSL_MSG("Do not continue search for other certs in file"); |
2932 | 0 | } |
2933 | 0 | } |
2934 | 0 | else { |
2935 | | /* Got a certificate out. */ |
2936 | 0 | WOLFSSL_MSG(" Processed a CA"); |
2937 | 0 | gotOne = 1; |
2938 | 0 | } |
2939 | | /* Update used count. */ |
2940 | 0 | used += consumed; |
2941 | 0 | } |
2942 | | |
2943 | | /* May have other unparsable data but did we get a certificate? */ |
2944 | 0 | if (gotOne) { |
2945 | 0 | WOLFSSL_MSG("Processed at least one valid CA. Other stuff OK"); |
2946 | 0 | ret = 1; |
2947 | 0 | } |
2948 | 0 | return ret; |
2949 | 0 | } |
2950 | | |
2951 | | |
2952 | | /* Get verify settings for AddCA from SSL context. */ |
2953 | | #define GET_VERIFY_SETTING_CTX(ctx) \ |
2954 | 0 | ((ctx) && (ctx)->verifyNone ? NO_VERIFY : VERIFY) |
2955 | | /* Get verify settings for AddCA from SSL. */ |
2956 | | #define GET_VERIFY_SETTING_SSL(ssl) \ |
2957 | 0 | ((ssl)->options.verifyNone ? NO_VERIFY : VERIFY) |
2958 | | |
2959 | | #ifndef NO_FILESYSTEM |
2960 | | |
2961 | | /* Process data from a file as private keys, CRL or certificates. |
2962 | | * |
2963 | | * @param [in, out] ctx SSL context object. |
2964 | | * @param [in] fname Name of file to read. |
2965 | | * @param [in] format Format of data: |
2966 | | * WOLFSSL_FILETYPE_PEM or WOLFSSL_FILETYPE_ASN1. |
2967 | | * @param [in] type Type of data: |
2968 | | * CERT_TYPE, CA_TYPE, TRUSTED_PEER_TYPE, |
2969 | | * PRIVATEKEY_TYPE or ALT_PRIVATEKEY_TYPE. |
2970 | | * @param [in, out] ssl SSL object. |
2971 | | * @param [in] userChain Whether file contains chain of certificates. |
2972 | | * @param [in, out] crl CRL object to load data into. |
2973 | | * @param [in] verify How to verify certificates. |
2974 | | * @return 1 on success. |
2975 | | * @return WOLFSSL_BAD_FILE when reading the file fails. |
2976 | | * @return WOLFSSL_BAD_CERTTYPE when unable to detect certificate type. |
2977 | | */ |
2978 | | int ProcessFile(WOLFSSL_CTX* ctx, const char* fname, int format, int type, |
2979 | | WOLFSSL* ssl, int userChain, WOLFSSL_CRL* crl, int verify) |
2980 | 0 | { |
2981 | 0 | int ret = 0; |
2982 | 0 | #ifndef WOLFSSL_SMALL_STACK |
2983 | 0 | byte stackBuffer[FILE_BUFFER_SIZE]; |
2984 | 0 | #endif |
2985 | 0 | StaticBuffer content; |
2986 | 0 | long sz = 0; |
2987 | 0 | void* heap = WOLFSSL_HEAP(ctx, ssl); |
2988 | |
|
2989 | 0 | (void)crl; |
2990 | 0 | (void)heap; |
2991 | |
|
2992 | | #ifdef WOLFSSL_SMALL_STACK |
2993 | | static_buffer_init(&content); |
2994 | | #else |
2995 | 0 | static_buffer_init(&content, stackBuffer, FILE_BUFFER_SIZE); |
2996 | 0 | #endif |
2997 | | |
2998 | | /* Read file into static buffer. */ |
2999 | 0 | ret = wolfssl_read_file_static(fname, &content, heap, DYNAMIC_TYPE_FILE, |
3000 | 0 | &sz); |
3001 | 0 | if ((ret == 0) && (type == DETECT_CERT_TYPE) && |
3002 | 0 | (format != WOLFSSL_FILETYPE_PEM)) { |
3003 | 0 | WOLFSSL_MSG_CERT_LOG("Cannot detect certificate type when not PEM"); |
3004 | 0 | ret = WOLFSSL_BAD_CERTTYPE; |
3005 | 0 | } |
3006 | | /* Try to detect type by parsing cert header and footer. */ |
3007 | 0 | if ((ret == 0) && (type == DETECT_CERT_TYPE)) { |
3008 | 0 | #if !defined(NO_CODING) && !defined(WOLFSSL_NO_PEM) |
3009 | 0 | const char* header = NULL; |
3010 | 0 | const char* footer = NULL; |
3011 | | #ifdef HAVE_CRL |
3012 | | WOLFSSL_MSG_CERT("Detecting cert type... (including CRL_TYPE)"); |
3013 | | #else |
3014 | 0 | WOLFSSL_MSG_CERT("Detecting cert type... (HAVE_CRL not defined)"); |
3015 | 0 | #endif |
3016 | | |
3017 | | /* Look for CA header and footer - same as CERT_TYPE. */ |
3018 | 0 | if (wc_PemGetHeaderFooter(CA_TYPE, &header, &footer) == 0 && |
3019 | 0 | (XSTRNSTR((char*)content.buffer, header, sz) != NULL)) { |
3020 | 0 | type = CA_TYPE; |
3021 | 0 | WOLFSSL_MSG_CERT_LOG_EX("Detected cert type CA_TYPE = %d:", type); |
3022 | 0 | } |
3023 | | #ifdef HAVE_CRL |
3024 | | /* Look for CRL header and footer. */ |
3025 | | else if (wc_PemGetHeaderFooter(CRL_TYPE, &header, &footer) == 0 && |
3026 | | (XSTRNSTR((char*)content.buffer, header, sz) != NULL)) { |
3027 | | type = CRL_TYPE; |
3028 | | WOLFSSL_MSG_CERT_LOG_EX("Detected cert type CRL_TYPE = %d:", type); |
3029 | | } |
3030 | | #endif |
3031 | | /* Look for cert header and footer - same as CA_TYPE. */ |
3032 | 0 | else if (wc_PemGetHeaderFooter(CERT_TYPE, &header, &footer) == 0 && |
3033 | 0 | (XSTRNSTR((char*)content.buffer, header, sz) != |
3034 | 0 | NULL)) { |
3035 | 0 | type = CERT_TYPE; |
3036 | 0 | WOLFSSL_MSG_CERT_LOG_EX("Detected cert type CERT_TYPE = %d:", type); |
3037 | 0 | } |
3038 | 0 | else |
3039 | 0 | #endif /* !NO_CODING && !WOLFSSL_NO_PEM */ |
3040 | 0 | { |
3041 | | /* Not a header that we support. */ |
3042 | 0 | WOLFSSL_MSG_CERT_LOG("Failed to detect certificate type"); |
3043 | | #ifdef WOLFSSL_DEBUG_CERTIFICATE_LOADS |
3044 | | WOLFSSL_DEBUG_PRINTF( |
3045 | | "ERROR: ProcessFile: Failed to detect certificate type" |
3046 | | " of \"%s\"\n", |
3047 | | fname); |
3048 | | #endif |
3049 | 0 | ret = WOLFSSL_BAD_CERTTYPE; |
3050 | 0 | } |
3051 | 0 | } /* (ret == 0) && (type == DETECT_CERT_TYPE) */ |
3052 | |
|
3053 | 0 | if (ret == 0) { |
3054 | | /* When CA or trusted peer and PEM - process as a chain buffer. */ |
3055 | 0 | if (((type == CA_TYPE) || (type == TRUSTED_PEER_TYPE)) && |
3056 | 0 | (format == WOLFSSL_FILETYPE_PEM)) { |
3057 | 0 | WOLFSSL_MSG_CERT("Processing cert chain buffer..."); |
3058 | 0 | ret = ProcessChainBuffer(ctx, ssl, content.buffer, sz, type, |
3059 | 0 | verify, fname); |
3060 | 0 | } |
3061 | | #ifdef HAVE_CRL |
3062 | | else if (type == CRL_TYPE) { |
3063 | | WOLFSSL_MSG_CERT("Loading CRL..."); |
3064 | | ret = BufferLoadCRL(crl, content.buffer, sz, format, verify); |
3065 | | } |
3066 | | #endif |
3067 | | #ifdef WOLFSSL_DUAL_ALG_CERTS |
3068 | | else if (type == PRIVATEKEY_TYPE) { |
3069 | | /* When support for dual algorithm certificates is enabled, the |
3070 | | * private key file may contain both the primary and the |
3071 | | * alternative private key. Hence, we have to parse both of them. |
3072 | | */ |
3073 | | long consumed = 0; |
3074 | | |
3075 | | ret = ProcessBuffer(ctx, content.buffer, sz, format, type, ssl, |
3076 | | &consumed, userChain, verify, fname); |
3077 | | if ((ret == 1) && (consumed < sz)) { |
3078 | | ret = ProcessBuffer(ctx, content.buffer + consumed, |
3079 | | sz - consumed, format, ALT_PRIVATEKEY_TYPE, ssl, NULL, 0, |
3080 | | verify, fname); |
3081 | | } |
3082 | | } |
3083 | | #endif |
3084 | 0 | else { |
3085 | | /* Load all other certificate types. */ |
3086 | 0 | ret = ProcessBuffer(ctx, content.buffer, sz, format, type, ssl, |
3087 | 0 | NULL, userChain, verify, fname); |
3088 | 0 | } |
3089 | 0 | } |
3090 | | |
3091 | | /* Dispose of dynamically allocated data. */ |
3092 | 0 | static_buffer_free(&content, heap, DYNAMIC_TYPE_FILE); |
3093 | 0 | return ret; |
3094 | 0 | } |
3095 | | |
3096 | | #if defined(WOLFSSL_TRUST_PEER_CERT) |
3097 | | /* Load a trusted peer certificate from a file with the caller's verification |
3098 | | * setting. |
3099 | | * |
3100 | | * @param [in, out] ctx SSL context object. |
3101 | | * @param [in] file Name of peer certificate file. |
3102 | | * @param [in] format Format of data: WOLFSSL_FILETYPE_PEM or |
3103 | | * WOLFSSL_FILETYPE_ASN1. |
3104 | | * @param [in] verify How to verify the certificate. |
3105 | | * @return 1 on success. |
3106 | | * @return 0 when ctx or file is NULL. |
3107 | | * @return Negative on error. |
3108 | | */ |
3109 | | static int TrustPeerCertFile(WOLFSSL_CTX* ctx, const char* file, int format, |
3110 | | int verify) |
3111 | | { |
3112 | | int ret; |
3113 | | |
3114 | | /* Validate parameters. */ |
3115 | | if ((ctx == NULL) || (file == NULL)) { |
3116 | | ret = 0; |
3117 | | } |
3118 | | else { |
3119 | | ret = ProcessFile(ctx, file, format, TRUSTED_PEER_TYPE, NULL, 0, NULL, |
3120 | | verify); |
3121 | | } |
3122 | | |
3123 | | return ret; |
3124 | | } |
3125 | | #endif /* WOLFSSL_TRUST_PEER_CERT */ |
3126 | | |
3127 | | #ifndef NO_WOLFSSL_DIR |
3128 | | /* Load file when filename is in the path. |
3129 | | * |
3130 | | * @param [in, out] ctx SSL context object. |
3131 | | * @param [in] name Name of file. |
3132 | | * @param [in] verify How to verify a certificate. |
3133 | | * @param [in] flags Flags representing options for loading. |
3134 | | * @param [in, out] failCount Number of files that failed to load. |
3135 | | * @param [in, out] successCount Number of files successfully loaded. |
3136 | | * @return 1 on success. |
3137 | | * @return Not 1 when loading PEM certificate failed. |
3138 | | */ |
3139 | | static int wolfssl_ctx_load_path_file(WOLFSSL_CTX* ctx, const char* name, |
3140 | | int verify, int flags, int* failCount, int* successCount) |
3141 | 0 | { |
3142 | 0 | int ret; |
3143 | | |
3144 | | /* Attempt to load file as a CA. */ |
3145 | 0 | ret = ProcessFile(ctx, name, WOLFSSL_FILETYPE_PEM, CA_TYPE, NULL, 0, NULL, |
3146 | 0 | verify); |
3147 | 0 | if (ret != 1) { |
3148 | | /* When ignoring errors or loading PEM only and no PEM. don't fail. */ |
3149 | 0 | if ((flags & WOLFSSL_LOAD_FLAG_IGNORE_ERR) || |
3150 | 0 | ((flags & WOLFSSL_LOAD_FLAG_PEM_CA_ONLY) && |
3151 | 0 | (ret == WC_NO_ERR_TRACE(ASN_NO_PEM_HEADER)))) { |
3152 | 0 | unsigned long err = 0; |
3153 | 0 | CLEAR_ASN_NO_PEM_HEADER_ERROR(err); |
3154 | | #if defined(WOLFSSL_QT) |
3155 | | ret = 1; |
3156 | | #endif |
3157 | 0 | } |
3158 | 0 | else { |
3159 | 0 | WOLFSSL_ERROR(ret); |
3160 | 0 | WOLFSSL_MSG("Load CA file failed, continuing"); |
3161 | | /* Add to fail count. */ |
3162 | 0 | (*failCount)++; |
3163 | 0 | } |
3164 | 0 | } |
3165 | 0 | else { |
3166 | | #if defined(WOLFSSL_TRUST_PEER_CERT) && defined(OPENSSL_COMPATIBLE_DEFAULTS) |
3167 | | /* Try loading as a trusted peer certificate with the same |
3168 | | * verification setting as the CA load. */ |
3169 | | ret = TrustPeerCertFile(ctx, name, WOLFSSL_FILETYPE_PEM, verify); |
3170 | | if (ret != 1) { |
3171 | | WOLFSSL_MSG("wolfSSL_CTX_trust_peer_cert error. " |
3172 | | "Ignoring this error."); |
3173 | | } |
3174 | | #endif |
3175 | | /* Add to success count. */ |
3176 | 0 | (*successCount)++; |
3177 | 0 | } |
3178 | |
|
3179 | 0 | return ret; |
3180 | 0 | } |
3181 | | |
3182 | | /* Load PEM formatted CA files from a path. |
3183 | | * |
3184 | | * @param [in, out] ctx SSL context object. |
3185 | | * @param [in] path Path to directory to read. |
3186 | | * @param [in] flags Flags representing options for loading. |
3187 | | * @param [in] verify How to verify a certificate. |
3188 | | * @param [in] successCount Number of files successfully loaded. |
3189 | | * @return 1 on success. |
3190 | | * @return 0 on failure. |
3191 | | * @return MEMORY_E when dynamic memory allocation fails. |
3192 | | */ |
3193 | | static int wolfssl_ctx_load_path(WOLFSSL_CTX* ctx, const char* path, |
3194 | | word32 flags, int verify, int successCount) |
3195 | 0 | { |
3196 | 0 | int ret = 1; |
3197 | 0 | char* name = NULL; |
3198 | 0 | int fileRet; |
3199 | 0 | int failCount = 0; |
3200 | 0 | WC_DECLARE_VAR(readCtx, ReadDirCtx, 1, 0); |
3201 | | |
3202 | | /* Allocate memory for directory reading context. */ |
3203 | 0 | WC_ALLOC_VAR_EX(readCtx, ReadDirCtx, 1, ctx->heap, DYNAMIC_TYPE_DIRCTX, |
3204 | 0 | ret=MEMORY_E); |
3205 | |
|
3206 | 0 | if (ret == 1) { |
3207 | | /* Get name of first file in path. */ |
3208 | 0 | fileRet = wc_ReadDirFirst(readCtx, path, &name); |
3209 | | /* While getting filename doesn't fail and name returned, process file. |
3210 | | */ |
3211 | 0 | while ((fileRet == 0) && (name != NULL)) { |
3212 | 0 | WOLFSSL_MSG(name); |
3213 | | /* Load file. */ |
3214 | 0 | ret = wolfssl_ctx_load_path_file(ctx, name, verify, (int)flags, |
3215 | 0 | &failCount, &successCount); |
3216 | | /* Get next filename. */ |
3217 | 0 | fileRet = wc_ReadDirNext(readCtx, path, &name); |
3218 | 0 | } |
3219 | | /* Cleanup directory reading context. */ |
3220 | 0 | wc_ReadDirClose(readCtx); |
3221 | | |
3222 | | /* When not WOLFSSL_QT, ret is always overwritten. */ |
3223 | 0 | (void)ret; |
3224 | | |
3225 | | /* Return real directory read failure error codes. */ |
3226 | 0 | if (fileRet != WC_READDIR_NOFILE) { |
3227 | 0 | ret = fileRet; |
3228 | | #if defined(WOLFSSL_QT) || defined(WOLFSSL_IGNORE_BAD_CERT_PATH) |
3229 | | /* Ignore bad path error when flag set. */ |
3230 | | if ((ret == WC_NO_ERR_TRACE(BAD_PATH_ERROR)) && |
3231 | | (flags & WOLFSSL_LOAD_FLAG_IGNORE_BAD_PATH_ERR)) { |
3232 | | /* QSslSocket always loads certs in system folder |
3233 | | * when it is initialized. |
3234 | | * Compliant with OpenSSL when flag set. |
3235 | | */ |
3236 | | ret = 1; |
3237 | | } |
3238 | | else { |
3239 | | /* qssl socket wants to know errors. */ |
3240 | | WOLFSSL_ERROR(ret); |
3241 | | } |
3242 | | #endif |
3243 | 0 | } |
3244 | | /* Report failure if no files successfully loaded or there were |
3245 | | * failures. */ |
3246 | 0 | else if ((successCount == 0) || (failCount > 0)) { |
3247 | | /* Use existing error code if exists. */ |
3248 | | #if defined(WOLFSSL_QT) |
3249 | | /* Compliant with OpenSSL when flag set. */ |
3250 | | if (!(flags & WOLFSSL_LOAD_FLAG_IGNORE_ZEROFILE)) |
3251 | | #endif |
3252 | 0 | { |
3253 | | /* Return 0 when no files loaded. */ |
3254 | 0 | ret = 0; |
3255 | 0 | } |
3256 | 0 | } |
3257 | 0 | else { |
3258 | | /* We loaded something so it is a success. */ |
3259 | 0 | ret = 1; |
3260 | 0 | } |
3261 | |
|
3262 | 0 | WC_FREE_VAR_EX(readCtx, ctx->heap, DYNAMIC_TYPE_DIRCTX); |
3263 | 0 | } |
3264 | |
|
3265 | 0 | return ret; |
3266 | 0 | } |
3267 | | #endif |
3268 | | |
3269 | | /* Load a file and/or files in path |
3270 | | * |
3271 | | * No c_rehash. |
3272 | | * |
3273 | | * @param [in, out] ctx SSL context object. |
3274 | | * @param [in] file Name of file to load. May be NULL. |
3275 | | * @param [in] path Path to directory containing PEM CA files. |
3276 | | * May be NULL. |
3277 | | * @param [in] flags Flags representing options for loading. |
3278 | | * @return 1 on success. |
3279 | | * @return 0 on failure. |
3280 | | * @return NOT_COMPILED_IN when directory reading not supported and path is |
3281 | | * not NULL. |
3282 | | * @return Other negative on error. |
3283 | | */ |
3284 | | int wolfSSL_CTX_load_verify_locations_ex(WOLFSSL_CTX* ctx, const char* file, |
3285 | | const char* path, word32 flags) |
3286 | 0 | { |
3287 | 0 | int ret = 1; |
3288 | 0 | #ifndef NO_WOLFSSL_DIR |
3289 | 0 | int successCount = 0; |
3290 | 0 | #endif |
3291 | 0 | int verify = WOLFSSL_VERIFY_DEFAULT; |
3292 | |
|
3293 | 0 | WOLFSSL_MSG("wolfSSL_CTX_load_verify_locations_ex"); |
3294 | | |
3295 | | /* Validate parameters. */ |
3296 | 0 | if ((ctx == NULL) || ((file == NULL) && (path == NULL))) { |
3297 | 0 | ret = 0; |
3298 | 0 | } |
3299 | |
|
3300 | | #ifdef WOLFSSL_TEST_APPLE_NATIVE_CERT_VALIDATION |
3301 | | if (ret == 1) { |
3302 | | /* TEST ONLY CODE: force native cert validation on */ |
3303 | | WOLFSSL_MSG("ANCV Test: Loading system CA certs"); |
3304 | | wolfSSL_CTX_load_system_CA_certs(ctx); |
3305 | | } |
3306 | | #endif |
3307 | |
|
3308 | 0 | if (ret == 1) { |
3309 | | /* Get setting on how to verify certificates. */ |
3310 | 0 | verify = GET_VERIFY_SETTING_CTX(ctx); |
3311 | | /* Overwrite setting when flag set. */ |
3312 | 0 | if (flags & WOLFSSL_LOAD_FLAG_DATE_ERR_OKAY) { |
3313 | 0 | verify = VERIFY_SKIP_DATE; |
3314 | 0 | } |
3315 | |
|
3316 | 0 | if (file != NULL) { |
3317 | 0 | #ifdef WOLFSSL_PEM_TO_DER |
3318 | | /* Load the PEM formatted CA file */ |
3319 | 0 | ret = ProcessFile(ctx, file, WOLFSSL_FILETYPE_PEM, CA_TYPE, NULL, 0, |
3320 | 0 | NULL, verify); |
3321 | | #else |
3322 | | /* Load the DER formatted CA file */ |
3323 | | ret = ProcessFile(ctx, file, WOLFSSL_FILETYPE_ASN1, CA_TYPE, NULL, |
3324 | | 0, NULL, verify); |
3325 | | #endif |
3326 | 0 | #ifndef NO_WOLFSSL_DIR |
3327 | 0 | if (ret == 1) { |
3328 | | /* Include success in overall count. */ |
3329 | 0 | successCount++; |
3330 | 0 | } |
3331 | 0 | #endif |
3332 | | #if defined(WOLFSSL_TRUST_PEER_CERT) && defined(OPENSSL_COMPATIBLE_DEFAULTS) |
3333 | | if (ret == 1) { |
3334 | | /* Load CA as a trusted peer certificate. The verification |
3335 | | * setting computed above, including |
3336 | | * WOLFSSL_LOAD_FLAG_DATE_ERR_OKAY, applies to the trusted |
3337 | | * peer copy too. */ |
3338 | | #ifdef WOLFSSL_PEM_TO_DER |
3339 | | ret = TrustPeerCertFile(ctx, file, WOLFSSL_FILETYPE_PEM, |
3340 | | verify); |
3341 | | #else |
3342 | | ret = TrustPeerCertFile(ctx, file, WOLFSSL_FILETYPE_ASN1, |
3343 | | verify); |
3344 | | #endif |
3345 | | if (ret != 1) { |
3346 | | WOLFSSL_MSG("wolfSSL_CTX_trust_peer_cert error"); |
3347 | | } |
3348 | | } |
3349 | | #endif |
3350 | 0 | } |
3351 | 0 | } |
3352 | |
|
3353 | 0 | if ((ret == 1) && (path != NULL)) { |
3354 | 0 | #ifndef NO_WOLFSSL_DIR |
3355 | | /* Load CA files form path. */ |
3356 | 0 | ret = wolfssl_ctx_load_path(ctx, path, flags, verify, successCount); |
3357 | | #else |
3358 | | /* Loading a path not supported. */ |
3359 | | ret = NOT_COMPILED_IN; |
3360 | | (void)flags; |
3361 | | #endif |
3362 | 0 | } |
3363 | |
|
3364 | 0 | return ret; |
3365 | 0 | } |
3366 | | |
3367 | | /* Load a file and/or files in path |
3368 | | * |
3369 | | * No c_rehash. |
3370 | | * |
3371 | | * @param [in, out] ctx SSL context object. |
3372 | | * @param [in] file Name of file to load. May be NULL. |
3373 | | * @param [in] path Path to directory containing PEM CA files. |
3374 | | * May be NULL. |
3375 | | * @return 1 on success. |
3376 | | * @return 0 on failure. |
3377 | | */ |
3378 | | WOLFSSL_ABI |
3379 | | int wolfSSL_CTX_load_verify_locations(WOLFSSL_CTX* ctx, const char* file, |
3380 | | const char* path) |
3381 | 0 | { |
3382 | | /* Load using default flags/options. */ |
3383 | 0 | int ret = wolfSSL_CTX_load_verify_locations_ex(ctx, file, path, |
3384 | 0 | WOLFSSL_LOAD_VERIFY_DEFAULT_FLAGS); |
3385 | | |
3386 | | /* Return 1 on success or 0 on failure. */ |
3387 | 0 | return WS_RETURN_CODE(ret, 0); |
3388 | 0 | } |
3389 | | |
3390 | | /* Load a file and/or files in path, with OpenSSL-compatible semantics. |
3391 | | * |
3392 | | * No c_rehash. |
3393 | | * |
3394 | | * @param [in, out] ctx SSL context object. |
3395 | | * @param [in] file Name of file to load. May be NULL. |
3396 | | * @param [in] path Path to directory containing PEM CA files. |
3397 | | * May be NULL. |
3398 | | * @return 1 on success. |
3399 | | * @return 0 on failure. |
3400 | | */ |
3401 | | int wolfSSL_CTX_load_verify_locations_compat(WOLFSSL_CTX* ctx, const char* file, |
3402 | | const char* path) |
3403 | 0 | { |
3404 | | /* We want to keep trying to load more CA certs even if one cert in the |
3405 | | * directory is bad and can't be used (e.g. if one is expired), and we |
3406 | | * want to return success if any were successfully loaded (mimicking |
3407 | | * OpenSSL SSL_CTX_load_verify_locations() semantics), so we use |
3408 | | * WOLFSSL_LOAD_FLAG_IGNORE_ERR. OpenSSL (as of v3.3.2) actually |
3409 | | * returns success even if no certs are loaded (e.g. because the |
3410 | | * supplied "path" doesn't exist or access is prohibited), and only |
3411 | | * returns failure if the "file" is non-null and fails to load. |
3412 | | * |
3413 | | * Note that if a file is supplied and can't be successfully loaded, the |
3414 | | * overall call fails and the path is never even evaluated. This is |
3415 | | * consistent with OpenSSL behavior. |
3416 | | */ |
3417 | |
|
3418 | 0 | int ret = wolfSSL_CTX_load_verify_locations_ex(ctx, file, path, |
3419 | 0 | WOLFSSL_LOAD_VERIFY_DEFAULT_FLAGS | WOLFSSL_LOAD_FLAG_IGNORE_ERR); |
3420 | | |
3421 | | /* Return 1 on success or 0 on failure. */ |
3422 | 0 | return WS_RETURN_CODE(ret, 0); |
3423 | 0 | } |
3424 | | |
3425 | | #ifdef WOLFSSL_TRUST_PEER_CERT |
3426 | | /* Load a trusted peer certificate into SSL context. |
3427 | | * |
3428 | | * @param [in, out] ctx SSL context object. |
3429 | | * @param [in] file Name of peer certificate file. |
3430 | | * @param [in] format Format of data: |
3431 | | * WOLFSSL_FILETYPE_PEM or WOLFSSL_FILETYPE_ASN1. |
3432 | | * @return 1 on success. |
3433 | | * @return 0 when ctx or file is NULL. |
3434 | | */ |
3435 | | int wolfSSL_CTX_trust_peer_cert(WOLFSSL_CTX* ctx, const char* file, int format) |
3436 | | { |
3437 | | int ret; |
3438 | | |
3439 | | WOLFSSL_ENTER("wolfSSL_CTX_trust_peer_cert"); |
3440 | | |
3441 | | /* Validate parameters. */ |
3442 | | if ((ctx == NULL) || (file == NULL)) { |
3443 | | ret = 0; |
3444 | | } |
3445 | | else { |
3446 | | ret = TrustPeerCertFile(ctx, file, format, GET_VERIFY_SETTING_CTX(ctx)); |
3447 | | } |
3448 | | |
3449 | | return ret; |
3450 | | } |
3451 | | |
3452 | | /* Load a trusted peer certificate into SSL. |
3453 | | * |
3454 | | * @param [in, out] ssl SSL object. |
3455 | | * @param [in] file Name of peer certificate file. |
3456 | | * @param [in] format Format of data: |
3457 | | * WOLFSSL_FILETYPE_PEM or WOLFSSL_FILETYPE_ASN1. |
3458 | | * @return 1 on success. |
3459 | | * @return 0 when ssl or file is NULL. |
3460 | | */ |
3461 | | int wolfSSL_trust_peer_cert(WOLFSSL* ssl, const char* file, int format) |
3462 | | { |
3463 | | int ret; |
3464 | | |
3465 | | WOLFSSL_ENTER("wolfSSL_trust_peer_cert"); |
3466 | | |
3467 | | /* Validate parameters. */ |
3468 | | if ((ssl == NULL) || (file == NULL)) { |
3469 | | ret = 0; |
3470 | | } |
3471 | | else { |
3472 | | ret = ProcessFile(NULL, file, format, TRUSTED_PEER_TYPE, ssl, 0, NULL, |
3473 | | GET_VERIFY_SETTING_SSL(ssl)); |
3474 | | } |
3475 | | |
3476 | | return ret; |
3477 | | } |
3478 | | #endif /* WOLFSSL_TRUST_PEER_CERT */ |
3479 | | |
3480 | | |
3481 | | #ifdef WOLFSSL_DER_LOAD |
3482 | | |
3483 | | /* Load a CA certificate into SSL context. |
3484 | | * |
3485 | | * @param [in, out] ctx SSL context object. |
3486 | | * @param [in] file Name of peer certificate file. |
3487 | | * @param [in] format Format of data: |
3488 | | * WOLFSSL_FILETYPE_PEM or WOLFSSL_FILETYPE_ASN1. |
3489 | | * @return 1 on success. |
3490 | | * @return 0 on failure. |
3491 | | */ |
3492 | | int wolfSSL_CTX_der_load_verify_locations(WOLFSSL_CTX* ctx, const char* file, |
3493 | | int format) |
3494 | | { |
3495 | | int ret; |
3496 | | |
3497 | | WOLFSSL_ENTER("wolfSSL_CTX_der_load_verify_locations"); |
3498 | | |
3499 | | /* Validate parameters. */ |
3500 | | if ((ctx == NULL) || (file == NULL)) { |
3501 | | ret = 0; |
3502 | | } |
3503 | | else { |
3504 | | #ifdef WOLFSSL_TEST_APPLE_NATIVE_CERT_VALIDATION |
3505 | | /* TEST ONLY CODE: force native cert validation on */ |
3506 | | WOLFSSL_MSG("ANCV Test: loading system CA certs"); |
3507 | | wolfSSL_CTX_load_system_CA_certs(ctx); |
3508 | | #endif |
3509 | | ret = ProcessFile(ctx, file, format, CA_TYPE, NULL, 0, NULL, |
3510 | | GET_VERIFY_SETTING_CTX(ctx)); |
3511 | | } |
3512 | | |
3513 | | /* Return 1 on success or 0 on failure. */ |
3514 | | return WS_RC(ret); |
3515 | | } |
3516 | | |
3517 | | #endif /* WOLFSSL_DER_LOAD */ |
3518 | | |
3519 | | |
3520 | | /* Load a user certificate into SSL context. |
3521 | | * |
3522 | | * @param [in, out] ctx SSL context object. |
3523 | | * @param [in] file Name of user certificate file. |
3524 | | * @param [in] format Format of data: |
3525 | | * WOLFSSL_FILETYPE_PEM or WOLFSSL_FILETYPE_ASN1. |
3526 | | * @return 1 on success. |
3527 | | * @return 0 on failure. |
3528 | | */ |
3529 | | WOLFSSL_ABI |
3530 | | int wolfSSL_CTX_use_certificate_file(WOLFSSL_CTX* ctx, const char* file, |
3531 | | int format) |
3532 | 0 | { |
3533 | 0 | int ret; |
3534 | |
|
3535 | 0 | WOLFSSL_ENTER("wolfSSL_CTX_use_certificate_file"); |
3536 | |
|
3537 | 0 | ret = ProcessFile(ctx, file, format, CERT_TYPE, NULL, 0, NULL, |
3538 | 0 | GET_VERIFY_SETTING_CTX(ctx)); |
3539 | | |
3540 | | /* Return 1 on success or 0 on failure. */ |
3541 | 0 | return WS_RC(ret); |
3542 | 0 | } |
3543 | | |
3544 | | |
3545 | | /* Load a private key into SSL context. |
3546 | | * |
3547 | | * @param [in, out] ctx SSL context object. |
3548 | | * @param [in] file Name of private key file. |
3549 | | * @param [in] format Format of data: |
3550 | | * WOLFSSL_FILETYPE_PEM or WOLFSSL_FILETYPE_ASN1. |
3551 | | * @return 1 on success. |
3552 | | * @return 0 on failure. |
3553 | | */ |
3554 | | WOLFSSL_ABI |
3555 | | int wolfSSL_CTX_use_PrivateKey_file(WOLFSSL_CTX* ctx, const char* file, |
3556 | | int format) |
3557 | 0 | { |
3558 | 0 | int ret; |
3559 | |
|
3560 | 0 | WOLFSSL_ENTER("wolfSSL_CTX_use_PrivateKey_file"); |
3561 | |
|
3562 | 0 | ret = ProcessFile(ctx, file, format, PRIVATEKEY_TYPE, NULL, 0, NULL, |
3563 | 0 | GET_VERIFY_SETTING_CTX(ctx)); |
3564 | | |
3565 | | /* Return 1 on success or 0 on failure. */ |
3566 | 0 | return WS_RC(ret); |
3567 | 0 | } |
3568 | | |
3569 | | #ifdef WOLFSSL_DUAL_ALG_CERTS |
3570 | | /* Load an alternative private key into SSL context. |
3571 | | * |
3572 | | * @param [in, out] ctx SSL context object. |
3573 | | * @param [in] file Name of private key file. |
3574 | | * @param [in] format Format of data: |
3575 | | * WOLFSSL_FILETYPE_PEM or WOLFSSL_FILETYPE_ASN1. |
3576 | | * @return 1 on success. |
3577 | | * @return 0 on failure. |
3578 | | */ |
3579 | | int wolfSSL_CTX_use_AltPrivateKey_file(WOLFSSL_CTX* ctx, const char* file, |
3580 | | int format) |
3581 | | { |
3582 | | int ret; |
3583 | | |
3584 | | WOLFSSL_ENTER("wolfSSL_CTX_use_AltPrivateKey_file"); |
3585 | | |
3586 | | ret = ProcessFile(ctx, file, format, ALT_PRIVATEKEY_TYPE, NULL, 0, NULL, |
3587 | | GET_VERIFY_SETTING_CTX(ctx)); |
3588 | | |
3589 | | /* Return 1 on success or 0 on failure. */ |
3590 | | return WS_RC(ret); |
3591 | | } |
3592 | | #endif /* WOLFSSL_DUAL_ALG_CERTS */ |
3593 | | |
3594 | | |
3595 | | /* Load a PEM certificate chain into SSL context. |
3596 | | * |
3597 | | * @param [in, out] ctx SSL context object. |
3598 | | * @param [in] file Name of PEM certificate chain file. |
3599 | | * @return 1 on success. |
3600 | | * @return 0 on failure. |
3601 | | */ |
3602 | | WOLFSSL_ABI |
3603 | | int wolfSSL_CTX_use_certificate_chain_file(WOLFSSL_CTX* ctx, const char* file) |
3604 | 0 | { |
3605 | 0 | int ret; |
3606 | | |
3607 | | /* process up to MAX_CHAIN_DEPTH plus subject cert */ |
3608 | 0 | WOLFSSL_ENTER("wolfSSL_CTX_use_certificate_chain_file"); |
3609 | |
|
3610 | 0 | #ifdef WOLFSSL_PEM_TO_DER |
3611 | 0 | ret = ProcessFile(ctx, file, WOLFSSL_FILETYPE_PEM, CERT_TYPE, NULL, 1, NULL, |
3612 | 0 | GET_VERIFY_SETTING_CTX(ctx)); |
3613 | | #else |
3614 | | ret = ProcessFile(ctx, file, WOLFSSL_FILETYPE_ASN1, CERT_TYPE, NULL, 1, |
3615 | | NULL, GET_VERIFY_SETTING_CTX(ctx)); |
3616 | | #endif |
3617 | | |
3618 | | /* Return 1 on success or 0 on failure. */ |
3619 | 0 | return WS_RC(ret); |
3620 | 0 | } |
3621 | | |
3622 | | /* Load certificate chain into SSL context. |
3623 | | * |
3624 | | * Processes up to MAX_CHAIN_DEPTH plus subject cert. |
3625 | | * |
3626 | | * @param [in, out] ctx SSL context object. |
3627 | | * @param [in] file Name of private key file. |
3628 | | * @param [in] format Format of data: |
3629 | | * WOLFSSL_FILETYPE_PEM or WOLFSSL_FILETYPE_ASN1. |
3630 | | * @return 1 on success. |
3631 | | * @return 0 on failure. |
3632 | | */ |
3633 | | int wolfSSL_CTX_use_certificate_chain_file_format(WOLFSSL_CTX* ctx, |
3634 | | const char* file, int format) |
3635 | 0 | { |
3636 | 0 | int ret; |
3637 | |
|
3638 | 0 | WOLFSSL_ENTER("wolfSSL_CTX_use_certificate_chain_file_format"); |
3639 | |
|
3640 | 0 | ret = ProcessFile(ctx, file, format, CERT_TYPE, NULL, 1, NULL, |
3641 | 0 | GET_VERIFY_SETTING_CTX(ctx)); |
3642 | | |
3643 | | /* Return 1 on success or 0 on failure. */ |
3644 | 0 | return WS_RC(ret); |
3645 | 0 | } |
3646 | | |
3647 | | #endif /* NO_FILESYSTEM */ |
3648 | | |
3649 | | #ifdef WOLFSSL_SYS_CA_CERTS |
3650 | | |
3651 | | #ifdef USE_WINDOWS_API |
3652 | | |
3653 | | /* Load CA certificate from Windows store. |
3654 | | * |
3655 | | * Assumes loaded is 0. |
3656 | | * |
3657 | | * @param [in, out] ctx SSL context object. |
3658 | | * @param [out] loaded Whether CA certificates were loaded. |
3659 | | * @return 1 on success. |
3660 | | * @return 0 on failure. |
3661 | | */ |
3662 | | static int LoadSystemCaCertsWindows(WOLFSSL_CTX* ctx, byte* loaded) |
3663 | | { |
3664 | | int ret = 1; |
3665 | | word32 i; |
3666 | | HANDLE handle = NULL; |
3667 | | PCCERT_CONTEXT certCtx = NULL; |
3668 | | LPCSTR storeNames[2] = {"ROOT", "CA"}; |
3669 | | HCRYPTPROV_LEGACY hProv = (HCRYPTPROV_LEGACY)NULL; |
3670 | | |
3671 | | if ((ctx == NULL) || (loaded == NULL)) { |
3672 | | ret = 0; |
3673 | | } |
3674 | | |
3675 | | for (i = 0; (ret == 1) && (i < sizeof(storeNames)/sizeof(*storeNames)); |
3676 | | ++i) { |
3677 | | handle = CertOpenSystemStoreA(hProv, storeNames[i]); |
3678 | | if (handle != NULL) { |
3679 | | while ((certCtx = CertEnumCertificatesInStore(handle, certCtx)) |
3680 | | != NULL) { |
3681 | | if (certCtx->dwCertEncodingType == X509_ASN_ENCODING) { |
3682 | | if (ProcessBuffer(ctx, certCtx->pbCertEncoded, |
3683 | | certCtx->cbCertEncoded, WOLFSSL_FILETYPE_ASN1, |
3684 | | CA_TYPE, NULL, NULL, 0, |
3685 | | GET_VERIFY_SETTING_CTX(ctx), |
3686 | | storeNames[i]) == 1) { |
3687 | | /* |
3688 | | * Set "loaded" as long as we've loaded one CA |
3689 | | * cert. |
3690 | | */ |
3691 | | *loaded = 1; |
3692 | | } |
3693 | | } |
3694 | | } |
3695 | | } |
3696 | | else { |
3697 | | WOLFSSL_MSG_EX("Failed to open cert store %s.", storeNames[i]); |
3698 | | } |
3699 | | |
3700 | | if (handle != NULL && !CertCloseStore(handle, 0)) { |
3701 | | WOLFSSL_MSG_EX("Failed to close cert store %s.", storeNames[i]); |
3702 | | ret = 0; |
3703 | | } |
3704 | | } |
3705 | | |
3706 | | return ret; |
3707 | | } |
3708 | | |
3709 | | #elif defined(__APPLE__) |
3710 | | |
3711 | | #if defined(HAVE_SECURITY_SECTRUSTSETTINGS_H) \ |
3712 | | && !defined(WOLFSSL_APPLE_NATIVE_CERT_VALIDATION) |
3713 | | /* Manually obtains certificates from the system trust store and loads them |
3714 | | * directly into wolfSSL "the old way". |
3715 | | * |
3716 | | * As of MacOS 14.0 we are still able to use this method to access system |
3717 | | * certificates. Accessibility of this API is indicated by the presence of the |
3718 | | * Security/SecTrustSettings.h header. In the likely event that Apple removes |
3719 | | * access to this API on Macs, this function should be removed and the |
3720 | | * DoAppleNativeCertValidation() routine should be used for all devices. |
3721 | | * |
3722 | | * Assumes loaded is 0. |
3723 | | * |
3724 | | * @param [in, out] ctx SSL context object. |
3725 | | * @param [out] loaded Whether CA certificates were loaded. |
3726 | | * @return 1 on success. |
3727 | | * @return 0 on failure. |
3728 | | */ |
3729 | | static int LoadSystemCaCertsMac(WOLFSSL_CTX* ctx, byte* loaded) |
3730 | | { |
3731 | | int ret = 1; |
3732 | | word32 i; |
3733 | | const unsigned int trustDomains[] = { |
3734 | | kSecTrustSettingsDomainUser, |
3735 | | kSecTrustSettingsDomainAdmin, |
3736 | | kSecTrustSettingsDomainSystem |
3737 | | }; |
3738 | | CFArrayRef certs; |
3739 | | OSStatus stat; |
3740 | | CFIndex numCerts; |
3741 | | CFDataRef der; |
3742 | | CFIndex j; |
3743 | | |
3744 | | if ((ctx == NULL) || (loaded == NULL)) { |
3745 | | ret = 0; |
3746 | | } |
3747 | | |
3748 | | for (i = 0; (ret == 1) && (i < sizeof(trustDomains)/sizeof(*trustDomains)); |
3749 | | ++i) { |
3750 | | stat = SecTrustSettingsCopyCertificates( |
3751 | | (SecTrustSettingsDomain)trustDomains[i], &certs); |
3752 | | if (stat == errSecSuccess) { |
3753 | | numCerts = CFArrayGetCount(certs); |
3754 | | for (j = 0; j < numCerts; ++j) { |
3755 | | der = SecCertificateCopyData((SecCertificateRef) |
3756 | | CFArrayGetValueAtIndex(certs, j)); |
3757 | | if (der != NULL) { |
3758 | | if (ProcessBuffer(ctx, CFDataGetBytePtr(der), |
3759 | | CFDataGetLength(der), WOLFSSL_FILETYPE_ASN1, |
3760 | | CA_TYPE, NULL, NULL, 0, |
3761 | | GET_VERIFY_SETTING_CTX(ctx), |
3762 | | "MacOSX trustDomains") == 1) { |
3763 | | /* |
3764 | | * Set "loaded" as long as we've loaded one CA |
3765 | | * cert. |
3766 | | */ |
3767 | | *loaded = 1; |
3768 | | } |
3769 | | |
3770 | | CFRelease(der); |
3771 | | } |
3772 | | } |
3773 | | |
3774 | | CFRelease(certs); |
3775 | | } |
3776 | | else if (stat == errSecNoTrustSettings) { |
3777 | | WOLFSSL_MSG_EX("No trust settings for domain %d, moving to next " |
3778 | | "domain.", trustDomains[i]); |
3779 | | } |
3780 | | else { |
3781 | | WOLFSSL_MSG_EX("SecTrustSettingsCopyCertificates failed with" |
3782 | | " status %d.", stat); |
3783 | | ret = 0; |
3784 | | break; |
3785 | | } |
3786 | | } |
3787 | | |
3788 | | return ret; |
3789 | | } |
3790 | | #endif /* defined(HAVE_SECURITY_SECTRUSTSETTINGS_H) */ |
3791 | | |
3792 | | #elif !defined(NO_FILESYSTEM) |
3793 | | |
3794 | | /* Potential system CA certs directories on Linux/Unix distros. */ |
3795 | | static const char* systemCaDirs[] = { |
3796 | | #if defined(__ANDROID__) || defined(ANDROID) |
3797 | | "/system/etc/security/cacerts" /* Android */ |
3798 | | #else |
3799 | | "/etc/ssl/certs", /* Debian, Ubuntu, Gentoo, others */ |
3800 | | "/etc/pki/ca-trust/source/anchors", /* Fedora, RHEL */ |
3801 | | "/etc/pki/tls/certs" /* Older RHEL */ |
3802 | | #endif |
3803 | | }; |
3804 | | |
3805 | | /* Get CA directory list. |
3806 | | * |
3807 | | * @param [out] num Number of CA directories. |
3808 | | * @return CA directory list. |
3809 | | * @return NULL when num is NULL. |
3810 | | */ |
3811 | | const char** wolfSSL_get_system_CA_dirs(word32* num) |
3812 | 0 | { |
3813 | 0 | const char** ret; |
3814 | | |
3815 | | /* Validate parameters. */ |
3816 | 0 | if (num == NULL) { |
3817 | 0 | ret = NULL; |
3818 | 0 | } |
3819 | 0 | else { |
3820 | 0 | ret = systemCaDirs; |
3821 | 0 | *num = sizeof(systemCaDirs)/sizeof(*systemCaDirs); |
3822 | 0 | } |
3823 | |
|
3824 | 0 | return ret; |
3825 | 0 | } |
3826 | | |
3827 | | /* Load CA certificate from default system directories. |
3828 | | * |
3829 | | * Assumes loaded is 0. |
3830 | | * |
3831 | | * @param [in, out] ctx SSL context object. |
3832 | | * @param [out] loaded Whether CA certificates were loaded. |
3833 | | * @return 1 on success. |
3834 | | * @return 0 on failure. |
3835 | | */ |
3836 | 0 | static int LoadSystemCaCertsNix(WOLFSSL_CTX* ctx, byte* loaded) { |
3837 | 0 | int ret = 1; |
3838 | 0 | word32 i; |
3839 | |
|
3840 | 0 | if ((ctx == NULL) || (loaded == NULL)) { |
3841 | 0 | ret = 0; |
3842 | 0 | } |
3843 | |
|
3844 | 0 | for (i = 0; (ret == 1) && (i < sizeof(systemCaDirs)/sizeof(*systemCaDirs)); |
3845 | 0 | ++i) { |
3846 | 0 | WOLFSSL_MSG_EX("Attempting to load system CA certs from %s.", |
3847 | 0 | systemCaDirs[i]); |
3848 | | /* |
3849 | | * We want to keep trying to load more CA certs even if one cert in |
3850 | | * the directory is bad and can't be used (e.g. if one is expired), |
3851 | | * so we use WOLFSSL_LOAD_FLAG_IGNORE_ERR. |
3852 | | */ |
3853 | 0 | if (wolfSSL_CTX_load_verify_locations_ex(ctx, NULL, systemCaDirs[i], |
3854 | 0 | WOLFSSL_LOAD_FLAG_IGNORE_ERR) != 1) { |
3855 | 0 | WOLFSSL_MSG_EX("Failed to load CA certs from %s, trying " |
3856 | 0 | "next possible location.", systemCaDirs[i]); |
3857 | 0 | } |
3858 | 0 | else { |
3859 | 0 | WOLFSSL_MSG_EX("Loaded CA certs from %s.", |
3860 | 0 | systemCaDirs[i]); |
3861 | 0 | *loaded = 1; |
3862 | | /* Stop searching after we've loaded one directory. */ |
3863 | 0 | break; |
3864 | 0 | } |
3865 | 0 | } |
3866 | |
|
3867 | 0 | return ret; |
3868 | 0 | } |
3869 | | |
3870 | | #endif |
3871 | | |
3872 | | /* Load CA certificates from system defined locations. |
3873 | | * |
3874 | | * @param [in, out] ctx SSL context object. |
3875 | | * @return 1 on success. |
3876 | | * @return 0 on failure. |
3877 | | * @return WOLFSSL_BAD_PATH when no error but no certificates loaded. |
3878 | | */ |
3879 | | int wolfSSL_CTX_load_system_CA_certs(WOLFSSL_CTX* ctx) |
3880 | 0 | { |
3881 | 0 | int ret; |
3882 | 0 | byte loaded = 0; |
3883 | |
|
3884 | 0 | WOLFSSL_ENTER("wolfSSL_CTX_load_system_CA_certs"); |
3885 | |
|
3886 | | #ifdef USE_WINDOWS_API |
3887 | | |
3888 | | ret = LoadSystemCaCertsWindows(ctx, &loaded); |
3889 | | |
3890 | | #elif defined(__APPLE__) |
3891 | | |
3892 | | #if defined(HAVE_SECURITY_SECTRUSTSETTINGS_H) \ |
3893 | | && !defined(WOLFSSL_APPLE_NATIVE_CERT_VALIDATION) |
3894 | | /* As of MacOS 14.0 we are still able to access system certificates and |
3895 | | * load them manually into wolfSSL "the old way". Accessibility of this API |
3896 | | * is indicated by the presence of the Security/SecTrustSettings.h header */ |
3897 | | ret = LoadSystemCaCertsMac(ctx, &loaded); |
3898 | | #elif defined(WOLFSSL_APPLE_NATIVE_CERT_VALIDATION) |
3899 | | /* For other Apple devices, Apple has removed the ability to obtain |
3900 | | * certificates from the trust store, so we can't use wolfSSL's built-in |
3901 | | * certificate validation mechanisms anymore. We instead must call into the |
3902 | | * Security Framework APIs to authenticate peer certificates when received. |
3903 | | * (see src/internal.c:DoAppleNativeCertValidation()). |
3904 | | * Thus, there is no CA "loading" required, but to keep behavior consistent |
3905 | | * with the current API (not using system CA certs unless this function has |
3906 | | * been called), we simply set a flag indicating that the new apple trust |
3907 | | * verification routine should be used later */ |
3908 | | ctx->doAppleNativeCertValidationFlag = 1; |
3909 | | ret = 1; |
3910 | | loaded = 1; |
3911 | | |
3912 | | #if FIPS_VERSION_GE(2,0) /* Gate back to cert 3389 FIPS modules */ |
3913 | | #warning "Cryptographic operations may occur outside the FIPS module boundary" \ |
3914 | | "Please review FIPS claims for cryptography on this Apple device" |
3915 | | #endif /* FIPS_VERSION_GE(2,0) */ |
3916 | | |
3917 | | #else |
3918 | | /* HAVE_SECURITY_SECXXX_H macros are set by autotools or CMake when searching |
3919 | | * system for the required SDK headers. If building with user_settings.h, you |
3920 | | * will need to manually define WOLFSSL_APPLE_NATIVE_CERT_VALIDATION |
3921 | | * and ensure the appropriate Security.framework headers and libraries are |
3922 | | * visible to your compiler */ |
3923 | | #error "WOLFSSL_SYS_CA_CERTS on Apple devices requires Security.framework" \ |
3924 | | " header files to be detected, or a manual override with" \ |
3925 | | " WOLFSSL_APPLE_NATIVE_CERT_VALIDATION" |
3926 | | #endif |
3927 | | |
3928 | | #else |
3929 | |
|
3930 | 0 | ret = LoadSystemCaCertsNix(ctx, &loaded); |
3931 | |
|
3932 | 0 | #endif |
3933 | | |
3934 | | /* If we didn't fail but didn't load then we error out. */ |
3935 | 0 | if ((ret == 1) && (!loaded)) { |
3936 | 0 | ret = WOLFSSL_BAD_PATH; |
3937 | 0 | } |
3938 | |
|
3939 | 0 | WOLFSSL_LEAVE("wolfSSL_CTX_load_system_CA_certs", ret); |
3940 | |
|
3941 | 0 | return ret; |
3942 | 0 | } |
3943 | | |
3944 | | #endif /* WOLFSSL_SYS_CA_CERTS */ |
3945 | | |
3946 | | #ifdef OPENSSL_EXTRA |
3947 | | |
3948 | | /* Load a private key into SSL. |
3949 | | * |
3950 | | * @param [in, out] ssl SSL object. |
3951 | | * @param [in] pkey EVP private key. |
3952 | | * @return 1 on success. |
3953 | | * @return 0 on failure. |
3954 | | */ |
3955 | | int wolfSSL_use_PrivateKey(WOLFSSL* ssl, WOLFSSL_EVP_PKEY* pkey) |
3956 | | { |
3957 | | int ret; |
3958 | | |
3959 | | WOLFSSL_ENTER("wolfSSL_use_PrivateKey"); |
3960 | | |
3961 | | /* Validate parameters. */ |
3962 | | if ((ssl == NULL) || (pkey == NULL)) { |
3963 | | ret = 0; |
3964 | | } |
3965 | | else { |
3966 | | /* Get DER encoded key data from EVP private key. */ |
3967 | | ret = wolfSSL_use_PrivateKey_buffer(ssl, (unsigned char*)pkey->pkey.ptr, |
3968 | | pkey->pkey_sz, WOLFSSL_FILETYPE_ASN1); |
3969 | | } |
3970 | | |
3971 | | return ret; |
3972 | | } |
3973 | | |
3974 | | /* Load a DER encoded private key in a buffer into SSL. |
3975 | | * |
3976 | | * @param [in] pri Indicates type of private key. Ignored. |
3977 | | * @param [in, out] ssl SSL object. |
3978 | | * @param [in] der Buffer holding DER encoded private key. |
3979 | | * @param [in] derSz Size of data in bytes. |
3980 | | * @return 1 on success. |
3981 | | * @return 0 on failure. |
3982 | | */ |
3983 | | int wolfSSL_use_PrivateKey_ASN1(int pri, WOLFSSL* ssl, const unsigned char* der, |
3984 | | long derSz) |
3985 | | { |
3986 | | int ret; |
3987 | | |
3988 | | WOLFSSL_ENTER("wolfSSL_use_PrivateKey_ASN1"); |
3989 | | |
3990 | | (void)pri; |
3991 | | |
3992 | | /* Validate parameters. */ |
3993 | | if ((ssl == NULL) || (der == NULL)) { |
3994 | | ret = 0; |
3995 | | } |
3996 | | else { |
3997 | | ret = wolfSSL_use_PrivateKey_buffer(ssl, der, derSz, |
3998 | | WOLFSSL_FILETYPE_ASN1); |
3999 | | } |
4000 | | |
4001 | | return ret; |
4002 | | } |
4003 | | |
4004 | | /* Load a DER encoded private key in a buffer into SSL context. |
4005 | | * |
4006 | | * @param [in] pri Indicates type of private key. Ignored. |
4007 | | * @param [in, out] ctx SSL context object. |
4008 | | * @param [in] der Buffer holding DER encoded private key. |
4009 | | * @param [in] derSz Size of data in bytes. |
4010 | | * @return 1 on success. |
4011 | | * @return 0 on failure. |
4012 | | */ |
4013 | | int wolfSSL_CTX_use_PrivateKey_ASN1(int pri, WOLFSSL_CTX* ctx, |
4014 | | unsigned char* der, long derSz) |
4015 | | { |
4016 | | int ret; |
4017 | | |
4018 | | WOLFSSL_ENTER("wolfSSL_CTX_use_PrivateKey_ASN1"); |
4019 | | |
4020 | | (void)pri; |
4021 | | |
4022 | | /* Validate parameters. */ |
4023 | | if ((ctx == NULL) || (der == NULL)) { |
4024 | | ret = 0; |
4025 | | } |
4026 | | else { |
4027 | | ret = wolfSSL_CTX_use_PrivateKey_buffer(ctx, der, derSz, |
4028 | | WOLFSSL_FILETYPE_ASN1); |
4029 | | } |
4030 | | |
4031 | | return ret; |
4032 | | } |
4033 | | |
4034 | | |
4035 | | #ifndef NO_RSA |
4036 | | /* Load a DER encoded RSA private key in a buffer into SSL. |
4037 | | * |
4038 | | * @param [in, out] ssl SSL object. |
4039 | | * @param [in] der Buffer holding DER encoded RSA private key. |
4040 | | * @param [in] derSz Size of data in bytes. |
4041 | | * @return 1 on success. |
4042 | | * @return 0 on failure. |
4043 | | */ |
4044 | | int wolfSSL_use_RSAPrivateKey_ASN1(WOLFSSL* ssl, unsigned char* der, long derSz) |
4045 | | { |
4046 | | int ret; |
4047 | | |
4048 | | WOLFSSL_ENTER("wolfSSL_use_RSAPrivateKey_ASN1"); |
4049 | | |
4050 | | /* Validate parameters. */ |
4051 | | if ((ssl == NULL) || (der == NULL)) { |
4052 | | ret = 0; |
4053 | | } |
4054 | | else { |
4055 | | ret = wolfSSL_use_PrivateKey_buffer(ssl, der, derSz, |
4056 | | WOLFSSL_FILETYPE_ASN1); |
4057 | | } |
4058 | | |
4059 | | return ret; |
4060 | | } |
4061 | | #endif |
4062 | | |
4063 | | /* Load a certificate into SSL. |
4064 | | * |
4065 | | * @param [in, out] ssl SSL object. |
4066 | | * @param [in] x509 X509 certificate object. |
4067 | | * @return 1 on success. |
4068 | | * @return 0 on failure. |
4069 | | */ |
4070 | | int wolfSSL_use_certificate(WOLFSSL* ssl, WOLFSSL_X509* x509) |
4071 | | { |
4072 | | int ret; |
4073 | | |
4074 | | WOLFSSL_ENTER("wolfSSL_use_certificate"); |
4075 | | |
4076 | | /* Validate parameters. */ |
4077 | | if ((ssl == NULL) || (x509 == NULL) || (x509->derCert == NULL)) { |
4078 | | ret = 0; |
4079 | | } |
4080 | | else { |
4081 | | long idx = 0; |
4082 | | |
4083 | | /* Get DER encoded certificate data from X509 object. */ |
4084 | | ret = ProcessBuffer(NULL, x509->derCert->buffer, x509->derCert->length, |
4085 | | WOLFSSL_FILETYPE_ASN1, CERT_TYPE, ssl, &idx, 0, |
4086 | | GET_VERIFY_SETTING_SSL(ssl), |
4087 | | "x509 buffer"); |
4088 | | } |
4089 | | |
4090 | | /* Return 1 on success or 0 on failure. */ |
4091 | | return WS_RC(ret); |
4092 | | } |
4093 | | |
4094 | | #endif /* OPENSSL_EXTRA */ |
4095 | | |
4096 | | /* Load a DER encoded certificate in a buffer into SSL. |
4097 | | * |
4098 | | * @param [in, out] ssl SSL object. |
4099 | | * @param [in] der Buffer holding DER encoded certificate. |
4100 | | * @param [in] derSz Size of data in bytes. |
4101 | | * @return 1 on success. |
4102 | | * @return 0 on failure. |
4103 | | */ |
4104 | | int wolfSSL_use_certificate_ASN1(WOLFSSL* ssl, const unsigned char* der, |
4105 | | int derSz) |
4106 | 0 | { |
4107 | 0 | int ret; |
4108 | |
|
4109 | 0 | WOLFSSL_ENTER("wolfSSL_use_certificate_ASN1"); |
4110 | | |
4111 | | /* Validate parameters. */ |
4112 | 0 | if ((ssl == NULL) || (der == NULL)) { |
4113 | 0 | ret = 0; |
4114 | 0 | } |
4115 | 0 | else { |
4116 | 0 | long idx = 0; |
4117 | |
|
4118 | 0 | ret = ProcessBuffer(NULL, der, derSz, WOLFSSL_FILETYPE_ASN1, CERT_TYPE, |
4119 | 0 | ssl, &idx, 0, GET_VERIFY_SETTING_SSL(ssl), |
4120 | 0 | "asn1 buffer"); |
4121 | 0 | } |
4122 | | |
4123 | | /* Return 1 on success or 0 on failure. */ |
4124 | 0 | return WS_RC(ret); |
4125 | 0 | } |
4126 | | |
4127 | | #ifndef NO_FILESYSTEM |
4128 | | |
4129 | | /* Load a certificate from a file into SSL. |
4130 | | * |
4131 | | * @param [in, out] ssl SSL object. |
4132 | | * @param [in] file Name of file. |
4133 | | * @param [in] format Format of data: |
4134 | | * WOLFSSL_FILETYPE_PEM or WOLFSSL_FILETYPE_ASN1. |
4135 | | * @return 1 on success. |
4136 | | * @return 0 on failure. |
4137 | | * @return BAD_FUNC_ARG when ssl is NULL. |
4138 | | */ |
4139 | | WOLFSSL_ABI |
4140 | | int wolfSSL_use_certificate_file(WOLFSSL* ssl, const char* file, int format) |
4141 | 0 | { |
4142 | 0 | int ret; |
4143 | |
|
4144 | 0 | WOLFSSL_ENTER("wolfSSL_use_certificate_file"); |
4145 | | |
4146 | | /* Validate parameters. */ |
4147 | 0 | if (ssl == NULL) { |
4148 | 0 | ret = BAD_FUNC_ARG; |
4149 | 0 | } |
4150 | 0 | else { |
4151 | 0 | ret = ProcessFile(ssl->ctx, file, format, CERT_TYPE, ssl, 0, NULL, |
4152 | 0 | GET_VERIFY_SETTING_SSL(ssl)); |
4153 | | /* Return 1 on success or 0 on failure. */ |
4154 | 0 | ret = WS_RC(ret); |
4155 | 0 | } |
4156 | |
|
4157 | 0 | return ret; |
4158 | 0 | } |
4159 | | |
4160 | | |
4161 | | /* Load a private key from a file into SSL. |
4162 | | * |
4163 | | * @param [in, out] ssl SSL object. |
4164 | | * @param [in] file Name of file. |
4165 | | * @param [in] format Format of data: |
4166 | | * WOLFSSL_FILETYPE_PEM or WOLFSSL_FILETYPE_ASN1. |
4167 | | * @return 1 on success. |
4168 | | * @return 0 on failure. |
4169 | | * @return BAD_FUNC_ARG when ssl is NULL. |
4170 | | */ |
4171 | | WOLFSSL_ABI |
4172 | | int wolfSSL_use_PrivateKey_file(WOLFSSL* ssl, const char* file, int format) |
4173 | 0 | { |
4174 | 0 | int ret; |
4175 | |
|
4176 | 0 | WOLFSSL_ENTER("wolfSSL_use_PrivateKey_file"); |
4177 | | |
4178 | | /* Validate parameters. */ |
4179 | 0 | if (ssl == NULL) { |
4180 | 0 | ret = BAD_FUNC_ARG; |
4181 | 0 | } |
4182 | 0 | else { |
4183 | 0 | ret = ProcessFile(ssl->ctx, file, format, PRIVATEKEY_TYPE, ssl, 0, NULL, |
4184 | 0 | GET_VERIFY_SETTING_SSL(ssl)); |
4185 | | /* Return 1 on success or 0 on failure. */ |
4186 | 0 | ret = WS_RC(ret); |
4187 | 0 | } |
4188 | |
|
4189 | 0 | return ret; |
4190 | 0 | } |
4191 | | |
4192 | | |
4193 | | /* Load a PEM encoded certificate chain from a file into SSL. |
4194 | | * |
4195 | | * Process up to MAX_CHAIN_DEPTH plus subject cert. |
4196 | | * |
4197 | | * @param [in, out] ssl SSL object. |
4198 | | * @param [in] file Name of file. |
4199 | | * @return 1 on success. |
4200 | | * @return 0 on failure. |
4201 | | * @return BAD_FUNC_ARG when ssl is NULL. |
4202 | | */ |
4203 | | WOLFSSL_ABI |
4204 | | int wolfSSL_use_certificate_chain_file(WOLFSSL* ssl, const char* file) |
4205 | 0 | { |
4206 | 0 | int ret; |
4207 | |
|
4208 | 0 | WOLFSSL_ENTER("wolfSSL_use_certificate_chain_file"); |
4209 | | |
4210 | | /* Validate parameters. */ |
4211 | 0 | if (ssl == NULL) { |
4212 | 0 | ret = BAD_FUNC_ARG; |
4213 | 0 | } |
4214 | 0 | else { |
4215 | 0 | #ifdef WOLFSSL_PEM_TO_DER |
4216 | 0 | ret = ProcessFile(ssl->ctx, file, WOLFSSL_FILETYPE_PEM, CERT_TYPE, ssl, |
4217 | 0 | 1, NULL, GET_VERIFY_SETTING_SSL(ssl)); |
4218 | | #else |
4219 | | ret = ProcessFile(ssl->ctx, file, WOLFSSL_FILETYPE_ASN1, CERT_TYPE, ssl, |
4220 | | 1, NULL, GET_VERIFY_SETTING_SSL(ssl)); |
4221 | | #endif |
4222 | | /* Return 1 on success or 0 on failure. */ |
4223 | 0 | ret = WS_RC(ret); |
4224 | 0 | } |
4225 | |
|
4226 | 0 | return ret; |
4227 | 0 | } |
4228 | | |
4229 | | /* Load a certificate chain from a file into SSL. |
4230 | | * |
4231 | | * @param [in, out] ssl SSL object. |
4232 | | * @param [in] file Name of file. |
4233 | | * @param [in] format Format of data: |
4234 | | * WOLFSSL_FILETYPE_PEM or WOLFSSL_FILETYPE_ASN1. |
4235 | | * @return 1 on success. |
4236 | | * @return 0 on failure. |
4237 | | * @return BAD_FUNC_ARG when ssl is NULL. |
4238 | | */ |
4239 | | int wolfSSL_use_certificate_chain_file_format(WOLFSSL* ssl, const char* file, |
4240 | | int format) |
4241 | 0 | { |
4242 | 0 | int ret; |
4243 | | |
4244 | | /* process up to MAX_CHAIN_DEPTH plus subject cert */ |
4245 | 0 | WOLFSSL_ENTER("wolfSSL_use_certificate_chain_file_format"); |
4246 | | |
4247 | | /* Validate parameters. */ |
4248 | 0 | if (ssl == NULL) { |
4249 | 0 | ret = BAD_FUNC_ARG; |
4250 | 0 | } |
4251 | 0 | else { |
4252 | 0 | ret = ProcessFile(ssl->ctx, file, format, CERT_TYPE, ssl, 1, NULL, |
4253 | 0 | GET_VERIFY_SETTING_SSL(ssl)); |
4254 | | /* Return 1 on success or 0 on failure. */ |
4255 | 0 | ret = WS_RC(ret); |
4256 | 0 | } |
4257 | |
|
4258 | 0 | return ret; |
4259 | 0 | } |
4260 | | |
4261 | | #endif /* !NO_FILESYSTEM */ |
4262 | | |
4263 | | #ifdef OPENSSL_EXTRA |
4264 | | |
4265 | | #ifndef NO_FILESYSTEM |
4266 | | /* Load an RSA private key from a file into SSL context. |
4267 | | * |
4268 | | * @param [in, out] ctx SSL context object. |
4269 | | * @param [in] file Name of file. |
4270 | | * @param [in] format Format of data: |
4271 | | * WOLFSSL_FILETYPE_PEM or WOLFSSL_FILETYPE_ASN1. |
4272 | | * @return 1 on success. |
4273 | | * @return 0 on failure. |
4274 | | */ |
4275 | | int wolfSSL_CTX_use_RSAPrivateKey_file(WOLFSSL_CTX* ctx,const char* file, |
4276 | | int format) |
4277 | | { |
4278 | | WOLFSSL_ENTER("wolfSSL_CTX_use_RSAPrivateKey_file"); |
4279 | | |
4280 | | return wolfSSL_CTX_use_PrivateKey_file(ctx, file, format); |
4281 | | } |
4282 | | |
4283 | | /* Load an RSA private key from a file into SSL. |
4284 | | * |
4285 | | * @param [in, out] ssl SSL object. |
4286 | | * @param [in] file Name of file. |
4287 | | * @param [in] format Format of data: |
4288 | | * WOLFSSL_FILETYPE_PEM or WOLFSSL_FILETYPE_ASN1. |
4289 | | * @return 1 on success. |
4290 | | * @return 0 on failure. |
4291 | | * @return BAD_FUNC_ARG when ssl is NULL. |
4292 | | */ |
4293 | | int wolfSSL_use_RSAPrivateKey_file(WOLFSSL* ssl, const char* file, int format) |
4294 | | { |
4295 | | WOLFSSL_ENTER("wolfSSL_use_RSAPrivateKey_file"); |
4296 | | |
4297 | | return wolfSSL_use_PrivateKey_file(ssl, file, format); |
4298 | | } |
4299 | | #endif /* NO_FILESYSTEM */ |
4300 | | |
4301 | | #endif /* OPENSSL_EXTRA */ |
4302 | | |
4303 | | #if defined(WOLFSSL_TRUST_PEER_CERT) |
4304 | | /* Load a buffer of certificate/s as trusted peer certificates with the |
4305 | | * caller's verification setting. |
4306 | | * |
4307 | | * @param [in, out] ctx SSL context object. |
4308 | | * @param [in] in Buffer holding certificate/s. |
4309 | | * @param [in] sz Length of data in buffer in bytes. |
4310 | | * @param [in] format Format of data: WOLFSSL_FILETYPE_PEM or |
4311 | | * WOLFSSL_FILETYPE_ASN1. |
4312 | | * @param [in] verify How to verify the certificate/s. |
4313 | | * @return 1 on success. |
4314 | | * @return 0 on failure. |
4315 | | * @return BAD_FUNC_ARG when ctx or in is NULL, or sz is less than zero. |
4316 | | * @return Negative on error. |
4317 | | */ |
4318 | | static int TrustPeerCertBuffer(WOLFSSL_CTX* ctx, const unsigned char* in, |
4319 | | long sz, int format, int verify) |
4320 | | { |
4321 | | int ret; |
4322 | | |
4323 | | /* Validate parameters. */ |
4324 | | if ((ctx == NULL) || (in == NULL) || (sz < 0)) { |
4325 | | ret = BAD_FUNC_ARG; |
4326 | | } |
4327 | | else { |
4328 | | /* When PEM, treat as certificate chain of trusted peer |
4329 | | * certificates. */ |
4330 | | if (format == WOLFSSL_FILETYPE_PEM) { |
4331 | | ret = ProcessChainBuffer(ctx, NULL, in, sz, TRUSTED_PEER_TYPE, |
4332 | | verify, "peer"); |
4333 | | } |
4334 | | /* When DER, load the trusted peer certificate. */ |
4335 | | else { |
4336 | | ret = ProcessBuffer(ctx, in, sz, format, TRUSTED_PEER_TYPE, NULL, |
4337 | | NULL, 0, verify, "peer"); |
4338 | | } |
4339 | | } |
4340 | | |
4341 | | return ret; |
4342 | | } |
4343 | | #endif /* WOLFSSL_TRUST_PEER_CERT */ |
4344 | | |
4345 | | /* Load a buffer of certificate/s into SSL context. |
4346 | | * |
4347 | | * @param [in, out] ctx SSL context object. |
4348 | | * @param [in] in Buffer holding certificate or private key. |
4349 | | * @param [in] sz Length of data in buffer in bytes. |
4350 | | * @param [in] format Format of data: |
4351 | | * WOLFSSL_FILETYPE_PEM or WOLFSSL_FILETYPE_ASN1. |
4352 | | * @param [in] userChain Whether file contains chain of certificates. |
4353 | | * @param [in] flags Flags representing options for loading. |
4354 | | * @return 1 on success. |
4355 | | * @return 0 on failure. |
4356 | | * @return Negative on error. |
4357 | | */ |
4358 | | int wolfSSL_CTX_load_verify_buffer_ex(WOLFSSL_CTX* ctx, const unsigned char* in, |
4359 | | long sz, int format, int userChain, word32 flags) |
4360 | 0 | { |
4361 | 0 | int ret; |
4362 | 0 | int verify; |
4363 | |
|
4364 | 0 | WOLFSSL_ENTER("wolfSSL_CTX_load_verify_buffer_ex"); |
4365 | |
|
4366 | | #ifdef WOLFSSL_TEST_APPLE_NATIVE_CERT_VALIDATION |
4367 | | /* TEST ONLY CODE: force native cert validation on */ |
4368 | | if (ctx != NULL) { |
4369 | | WOLFSSL_MSG("ANCV Test: loading system CA certs"); |
4370 | | wolfSSL_CTX_load_system_CA_certs(ctx); |
4371 | | } |
4372 | | #endif |
4373 | | |
4374 | | /* Get setting on how to verify certificates. */ |
4375 | 0 | verify = GET_VERIFY_SETTING_CTX(ctx); |
4376 | | /* Overwrite setting when flag set. */ |
4377 | 0 | if (flags & WOLFSSL_LOAD_FLAG_DATE_ERR_OKAY) { |
4378 | 0 | verify = VERIFY_SKIP_DATE; |
4379 | 0 | } |
4380 | | |
4381 | | /* When PEM, treat as certificate chain of CA certificates. */ |
4382 | 0 | if (format == WOLFSSL_FILETYPE_PEM) { |
4383 | 0 | ret = ProcessChainBuffer(ctx, NULL, in, sz, CA_TYPE, verify, |
4384 | 0 | "PEM buffer"); |
4385 | 0 | } |
4386 | | /* When DER, load the CA certificate. */ |
4387 | 0 | else { |
4388 | 0 | ret = ProcessBuffer(ctx, in, sz, format, CA_TYPE, NULL, NULL, |
4389 | 0 | userChain, verify, "buffer"); |
4390 | 0 | } |
4391 | | #if defined(WOLFSSL_TRUST_PEER_CERT) && defined(OPENSSL_COMPATIBLE_DEFAULTS) |
4392 | | if (ret == 1) { |
4393 | | /* Load certificate/s as trusted peer certificate. The verification |
4394 | | * setting computed above, including WOLFSSL_LOAD_FLAG_DATE_ERR_OKAY, |
4395 | | * applies to the trusted peer copy too. */ |
4396 | | ret = TrustPeerCertBuffer(ctx, in, sz, format, verify); |
4397 | | } |
4398 | | #endif |
4399 | |
|
4400 | 0 | WOLFSSL_LEAVE("wolfSSL_CTX_load_verify_buffer_ex", ret); |
4401 | 0 | return ret; |
4402 | 0 | } |
4403 | | |
4404 | | /* Load a buffer of certificate/s into SSL context. |
4405 | | * |
4406 | | * @param [in, out] ctx SSL context object. |
4407 | | * @param [in] in Buffer holding certificate or private key. |
4408 | | * @param [in] sz Length of data in buffer in bytes. |
4409 | | * @param [in] format Format of data: |
4410 | | * WOLFSSL_FILETYPE_PEM or WOLFSSL_FILETYPE_ASN1. |
4411 | | * @return 1 on success. |
4412 | | * @return 0 on failure. |
4413 | | * @return Negative on error. |
4414 | | */ |
4415 | | int wolfSSL_CTX_load_verify_buffer(WOLFSSL_CTX* ctx, const unsigned char* in, |
4416 | | long sz, int format) |
4417 | 0 | { |
4418 | 0 | return wolfSSL_CTX_load_verify_buffer_ex(ctx, in, sz, format, 0, |
4419 | 0 | WOLFSSL_LOAD_VERIFY_DEFAULT_FLAGS); |
4420 | 0 | } |
4421 | | |
4422 | | /* Load a buffer of certificate chain into SSL context. |
4423 | | * |
4424 | | * @param [in, out] ctx SSL context object. |
4425 | | * @param [in] in Buffer holding certificate chain. |
4426 | | * @param [in] sz Length of data in buffer in bytes. |
4427 | | * @param [in] format Format of data: |
4428 | | * WOLFSSL_FILETYPE_PEM or WOLFSSL_FILETYPE_ASN1. |
4429 | | * @return 1 on success. |
4430 | | * @return 0 on failure. |
4431 | | * @return Negative on error. |
4432 | | */ |
4433 | | int wolfSSL_CTX_load_verify_chain_buffer_format(WOLFSSL_CTX* ctx, |
4434 | | const unsigned char* in, long sz, int format) |
4435 | 0 | { |
4436 | 0 | return wolfSSL_CTX_load_verify_buffer_ex(ctx, in, sz, format, 1, |
4437 | 0 | WOLFSSL_LOAD_VERIFY_DEFAULT_FLAGS); |
4438 | 0 | } |
4439 | | |
4440 | | |
4441 | | #ifdef WOLFSSL_TRUST_PEER_CERT |
4442 | | /* Load a buffer of certificate/s into SSL context. |
4443 | | * |
4444 | | * @param [in, out] ctx SSL context object. |
4445 | | * @param [in] in Buffer holding certificate/s. |
4446 | | * @param [in] sz Length of data in buffer in bytes. |
4447 | | * @param [in] format Format of data: |
4448 | | * WOLFSSL_FILETYPE_PEM or WOLFSSL_FILETYPE_ASN1. |
4449 | | * @return 1 on success. |
4450 | | * @return 0 on failure. |
4451 | | * @return BAD_FUNC_ARG when ctx or in is NULL, or sz is less than zero. |
4452 | | */ |
4453 | | int wolfSSL_CTX_trust_peer_buffer(WOLFSSL_CTX* ctx, const unsigned char* in, |
4454 | | long sz, int format) |
4455 | | { |
4456 | | int ret; |
4457 | | int verify; |
4458 | | |
4459 | | WOLFSSL_ENTER("wolfSSL_CTX_trust_peer_buffer"); |
4460 | | |
4461 | | /* Validate parameters. */ |
4462 | | if ((ctx == NULL) || (in == NULL) || (sz < 0)) { |
4463 | | ret = BAD_FUNC_ARG; |
4464 | | } |
4465 | | else { |
4466 | | #if WOLFSSL_LOAD_VERIFY_DEFAULT_FLAGS & WOLFSSL_LOAD_FLAG_DATE_ERR_OKAY |
4467 | | verify = VERIFY_SKIP_DATE; |
4468 | | #else |
4469 | | verify = GET_VERIFY_SETTING_CTX(ctx); |
4470 | | #endif |
4471 | | |
4472 | | ret = TrustPeerCertBuffer(ctx, in, sz, format, verify); |
4473 | | } |
4474 | | |
4475 | | return ret; |
4476 | | } |
4477 | | #endif /* WOLFSSL_TRUST_PEER_CERT */ |
4478 | | |
4479 | | /* Load a certificate in a buffer into SSL context. |
4480 | | * |
4481 | | * @param [in, out] ctx SSL context object. |
4482 | | * @param [in] in Buffer holding certificate. |
4483 | | * @param [in] sz Size of data in bytes. |
4484 | | * @param [in] format Format of data: |
4485 | | * WOLFSSL_FILETYPE_PEM or WOLFSSL_FILETYPE_ASN1. |
4486 | | * @return 1 on success. |
4487 | | * @return 0 on failure. |
4488 | | * @return Negative on error. |
4489 | | */ |
4490 | | int wolfSSL_CTX_use_certificate_buffer(WOLFSSL_CTX* ctx, |
4491 | | const unsigned char* in, long sz, int format) |
4492 | 0 | { |
4493 | 0 | int ret; |
4494 | |
|
4495 | 0 | WOLFSSL_ENTER("wolfSSL_CTX_use_certificate_buffer"); |
4496 | 0 | ret = ProcessBuffer(ctx, in, sz, format, CERT_TYPE, NULL, NULL, 0, |
4497 | 0 | GET_VERIFY_SETTING_CTX(ctx), "buffer"); |
4498 | 0 | WOLFSSL_LEAVE("wolfSSL_CTX_use_certificate_buffer", ret); |
4499 | |
|
4500 | 0 | return ret; |
4501 | 0 | } |
4502 | | |
4503 | | /* Load a private key in a buffer into SSL context. |
4504 | | * |
4505 | | * @param [in, out] ctx SSL context object. |
4506 | | * @param [in] in Buffer holding private key. |
4507 | | * @param [in] sz Size of data in bytes. |
4508 | | * @param [in] format Format of data: |
4509 | | * WOLFSSL_FILETYPE_PEM or WOLFSSL_FILETYPE_ASN1. |
4510 | | * @return 1 on success. |
4511 | | * @return 0 on failure. |
4512 | | * @return Negative on error. |
4513 | | */ |
4514 | | int wolfSSL_CTX_use_PrivateKey_buffer(WOLFSSL_CTX* ctx, const unsigned char* in, |
4515 | | long sz, int format) |
4516 | 0 | { |
4517 | 0 | int ret; |
4518 | 0 | long consumed = 0; |
4519 | |
|
4520 | 0 | WOLFSSL_ENTER("wolfSSL_CTX_use_PrivateKey_buffer"); |
4521 | |
|
4522 | 0 | ret = ProcessBuffer(ctx, in, sz, format, PRIVATEKEY_TYPE, NULL, &consumed, |
4523 | 0 | 0, GET_VERIFY_SETTING_CTX(ctx), "key buffer"); |
4524 | | #ifdef WOLFSSL_DUAL_ALG_CERTS |
4525 | | if ((ret == 1) && (consumed < sz)) { |
4526 | | /* When support for dual algorithm certificates is enabled, the |
4527 | | * buffer may contain both the primary and the alternative |
4528 | | * private key. Hence, we have to parse both of them. |
4529 | | */ |
4530 | | ret = ProcessBuffer(ctx, in + consumed, sz - consumed, format, |
4531 | | ALT_PRIVATEKEY_TYPE, NULL, NULL, 0, GET_VERIFY_SETTING_CTX(ctx), |
4532 | | "key buffer"); |
4533 | | } |
4534 | | #endif |
4535 | |
|
4536 | 0 | (void)consumed; |
4537 | |
|
4538 | 0 | WOLFSSL_LEAVE("wolfSSL_CTX_use_PrivateKey_buffer", ret); |
4539 | 0 | return ret; |
4540 | 0 | } |
4541 | | |
4542 | | #ifdef WOLFSSL_DUAL_ALG_CERTS |
4543 | | int wolfSSL_CTX_use_AltPrivateKey_buffer(WOLFSSL_CTX* ctx, |
4544 | | const unsigned char* in, long sz, int format) |
4545 | | { |
4546 | | int ret; |
4547 | | |
4548 | | WOLFSSL_ENTER("wolfSSL_CTX_use_AltPrivateKey_buffer"); |
4549 | | ret = ProcessBuffer(ctx, in, sz, format, ALT_PRIVATEKEY_TYPE, NULL, |
4550 | | NULL, 0, GET_VERIFY_SETTING_CTX(ctx), "alt key buffer"); |
4551 | | WOLFSSL_LEAVE("wolfSSL_CTX_use_AltPrivateKey_buffer", ret); |
4552 | | |
4553 | | return ret; |
4554 | | } |
4555 | | #endif /* WOLFSSL_DUAL_ALG_CERTS */ |
4556 | | |
4557 | | #ifdef WOLF_PRIVATE_KEY_ID |
4558 | | /* Load the id of a private key into SSL context. |
4559 | | * |
4560 | | * @param [in, out] ctx SSL context object. |
4561 | | * @param [in] id Buffer holding id. |
4562 | | * @param [in] sz Size of data in bytes. |
4563 | | * @param [in] devId Device identifier. |
4564 | | * @return 1 on success. |
4565 | | * @return 0 on failure. |
4566 | | */ |
4567 | | int wolfSSL_CTX_use_PrivateKey_Id(WOLFSSL_CTX* ctx, const unsigned char* id, |
4568 | | long sz, int devId) |
4569 | | { |
4570 | | int ret = 1; |
4571 | | |
4572 | | WOLFSSL_ENTER("wolfSSL_CTX_use_PrivateKey_Id"); |
4573 | | |
4574 | | if (ctx == NULL || id == NULL || sz < 0) { |
4575 | | return 0; |
4576 | | } |
4577 | | |
4578 | | /* Dispose of old private key and allocate and copy in id. */ |
4579 | | FreeDer(&ctx->privateKey); |
4580 | | if (AllocCopyDer(&ctx->privateKey, id, (word32)sz, PRIVATEKEY_TYPE, |
4581 | | ctx->heap) != 0) { |
4582 | | ret = 0; |
4583 | | } |
4584 | | if (ret == 1) { |
4585 | | /* Private key is an id. */ |
4586 | | ctx->privateKeyId = 1; |
4587 | | ctx->privateKeyLabel = 0; |
4588 | | /* Set private key device id to be one passed in or for SSL context. */ |
4589 | | if (devId != INVALID_DEVID) { |
4590 | | ctx->privateKeyDevId = devId; |
4591 | | } |
4592 | | else { |
4593 | | ctx->privateKeyDevId = ctx->devId; |
4594 | | } |
4595 | | |
4596 | | #ifdef WOLFSSL_DUAL_ALG_CERTS |
4597 | | /* Set the ID for the alternative key, too. User can still override that |
4598 | | * afterwards. */ |
4599 | | ret = wolfSSL_CTX_use_AltPrivateKey_Id(ctx, id, sz, devId); |
4600 | | #endif |
4601 | | } |
4602 | | |
4603 | | WOLFSSL_LEAVE("wolfSSL_CTX_use_PrivateKey_Id", ret); |
4604 | | return ret; |
4605 | | } |
4606 | | |
4607 | | /* Load the id of a private key into SSL context and set key size. |
4608 | | * |
4609 | | * @param [in, out] ctx SSL context object. |
4610 | | * @param [in] id Buffer holding id. |
4611 | | * @param [in] sz Size of data in bytes. |
4612 | | * @param [in] devId Device identifier. |
4613 | | * @param [in] keySz Size of key. |
4614 | | * @return 1 on success. |
4615 | | * @return 0 on failure. |
4616 | | */ |
4617 | | int wolfSSL_CTX_use_PrivateKey_Id_ex(WOLFSSL_CTX* ctx, const unsigned char* id, |
4618 | | long sz, int devId, long keySz) |
4619 | | { |
4620 | | int ret; |
4621 | | |
4622 | | WOLFSSL_ENTER("wolfSSL_CTX_use_PrivateKey_Id_ex"); |
4623 | | |
4624 | | ret = wolfSSL_CTX_use_PrivateKey_Id(ctx, id, sz, devId); |
4625 | | if (ret == 1) { |
4626 | | /* Set the key size which normally is calculated during decoding. */ |
4627 | | ctx->privateKeySz = (int)keySz; |
4628 | | } |
4629 | | |
4630 | | WOLFSSL_LEAVE("wolfSSL_CTX_use_PrivateKey_Id_ex", ret); |
4631 | | return ret; |
4632 | | } |
4633 | | |
4634 | | /* Load the label name of a private key into SSL context. |
4635 | | * |
4636 | | * @param [in, out] ctx SSL context object. |
4637 | | * @param [in] label Buffer holding label. |
4638 | | * @param [in] devId Device identifier. |
4639 | | * @return 1 on success. |
4640 | | * @return 0 on failure. |
4641 | | */ |
4642 | | int wolfSSL_CTX_use_PrivateKey_Label(WOLFSSL_CTX* ctx, const char* label, |
4643 | | int devId) |
4644 | | { |
4645 | | int ret = 1; |
4646 | | word32 sz; |
4647 | | |
4648 | | WOLFSSL_ENTER("wolfSSL_CTX_use_PrivateKey_Label"); |
4649 | | |
4650 | | if (ctx == NULL || label == NULL) { |
4651 | | return 0; |
4652 | | } |
4653 | | |
4654 | | sz = (word32)XSTRLEN(label) + 1; |
4655 | | |
4656 | | /* Dispose of old private key and allocate and copy in label. */ |
4657 | | FreeDer(&ctx->privateKey); |
4658 | | if (AllocCopyDer(&ctx->privateKey, (const byte*)label, (word32)sz, |
4659 | | PRIVATEKEY_TYPE, ctx->heap) != 0) { |
4660 | | ret = 0; |
4661 | | } |
4662 | | if (ret == 1) { |
4663 | | /* Private key is a label. */ |
4664 | | ctx->privateKeyId = 0; |
4665 | | ctx->privateKeyLabel = 1; |
4666 | | /* Set private key device id to be one passed in or for SSL context. */ |
4667 | | if (devId != INVALID_DEVID) { |
4668 | | ctx->privateKeyDevId = devId; |
4669 | | } |
4670 | | else { |
4671 | | ctx->privateKeyDevId = ctx->devId; |
4672 | | } |
4673 | | |
4674 | | #ifdef WOLFSSL_DUAL_ALG_CERTS |
4675 | | /* Set the ID for the alternative key, too. User can still override that |
4676 | | * afterwards. */ |
4677 | | ret = wolfSSL_CTX_use_AltPrivateKey_Label(ctx, label, devId); |
4678 | | #endif |
4679 | | } |
4680 | | |
4681 | | WOLFSSL_LEAVE("wolfSSL_CTX_use_PrivateKey_Label", ret); |
4682 | | return ret; |
4683 | | } |
4684 | | |
4685 | | #ifdef WOLFSSL_DUAL_ALG_CERTS |
4686 | | int wolfSSL_CTX_use_AltPrivateKey_Id(WOLFSSL_CTX* ctx, const unsigned char* id, |
4687 | | long sz, int devId) |
4688 | | { |
4689 | | int ret = 1; |
4690 | | |
4691 | | WOLFSSL_ENTER("wolfSSL_CTX_use_AltPrivateKey_Id"); |
4692 | | |
4693 | | if ((ctx == NULL) || (id == NULL) || (sz < 0)) { |
4694 | | ret = 0; |
4695 | | } |
4696 | | |
4697 | | if (ret == 1) { |
4698 | | FreeDer(&ctx->altPrivateKey); |
4699 | | if (AllocDer(&ctx->altPrivateKey, (word32)sz, ALT_PRIVATEKEY_TYPE, |
4700 | | ctx->heap) != 0) { |
4701 | | ret = 0; |
4702 | | } |
4703 | | } |
4704 | | if (ret == 1) { |
4705 | | XMEMCPY(ctx->altPrivateKey->buffer, id, (word32)sz); |
4706 | | ctx->altPrivateKeyId = 1; |
4707 | | if (devId != INVALID_DEVID) { |
4708 | | ctx->altPrivateKeyDevId = devId; |
4709 | | } |
4710 | | else { |
4711 | | ctx->altPrivateKeyDevId = ctx->devId; |
4712 | | } |
4713 | | } |
4714 | | |
4715 | | WOLFSSL_LEAVE("wolfSSL_CTX_use_AltPrivateKey_Id", ret); |
4716 | | return ret; |
4717 | | } |
4718 | | |
4719 | | int wolfSSL_CTX_use_AltPrivateKey_Id_ex(WOLFSSL_CTX* ctx, |
4720 | | const unsigned char* id, long sz, int devId, long keySz) |
4721 | | { |
4722 | | int ret; |
4723 | | |
4724 | | WOLFSSL_ENTER("wolfSSL_CTX_use_AltPrivateKey_Id_ex"); |
4725 | | |
4726 | | ret = wolfSSL_CTX_use_AltPrivateKey_Id(ctx, id, sz, devId); |
4727 | | if (ret == 1) { |
4728 | | ctx->altPrivateKeySz = (word32)keySz; |
4729 | | } |
4730 | | |
4731 | | WOLFSSL_LEAVE("wolfSSL_CTX_use_AltPrivateKey_Id_ex", ret); |
4732 | | return ret; |
4733 | | } |
4734 | | |
4735 | | int wolfSSL_CTX_use_AltPrivateKey_Label(WOLFSSL_CTX* ctx, const char* label, |
4736 | | int devId) |
4737 | | { |
4738 | | int ret = 1; |
4739 | | word32 sz; |
4740 | | |
4741 | | WOLFSSL_ENTER("wolfSSL_CTX_use_AltPrivateKey_Label"); |
4742 | | |
4743 | | if ((ctx == NULL) || (label == NULL)) { |
4744 | | ret = 0; |
4745 | | } |
4746 | | |
4747 | | if (ret == 1) { |
4748 | | sz = (word32)XSTRLEN(label) + 1; |
4749 | | FreeDer(&ctx->altPrivateKey); |
4750 | | if (AllocDer(&ctx->altPrivateKey, (word32)sz, ALT_PRIVATEKEY_TYPE, |
4751 | | ctx->heap) != 0) { |
4752 | | ret = 0; |
4753 | | } |
4754 | | } |
4755 | | if (ret == 1) { |
4756 | | XMEMCPY(ctx->altPrivateKey->buffer, label, sz); |
4757 | | ctx->altPrivateKeyLabel = 1; |
4758 | | if (devId != INVALID_DEVID) { |
4759 | | ctx->altPrivateKeyDevId = devId; |
4760 | | } |
4761 | | else { |
4762 | | ctx->altPrivateKeyDevId = ctx->devId; |
4763 | | } |
4764 | | } |
4765 | | |
4766 | | WOLFSSL_LEAVE("wolfSSL_CTX_use_AltPrivateKey_Label", ret); |
4767 | | return ret; |
4768 | | } |
4769 | | #endif /* WOLFSSL_DUAL_ALG_CERTS */ |
4770 | | #endif /* WOLF_PRIVATE_KEY_ID */ |
4771 | | |
4772 | | #if defined(WOLF_CRYPTO_CB) && !defined(NO_CERTS) |
4773 | | |
4774 | | static int wolfSSL_CTX_use_certificate_ex(WOLFSSL_CTX* ctx, |
4775 | | const char *label, const unsigned char *id, int idLen, int devId) |
4776 | | { |
4777 | | int ret; |
4778 | | byte *certData = NULL; |
4779 | | word32 certDataLen = 0; |
4780 | | word32 labelLen = 0; |
4781 | | int certFormat = 0; |
4782 | | |
4783 | | WOLFSSL_ENTER("wolfSSL_CTX_use_certificate_ex"); |
4784 | | |
4785 | | if (label != NULL) { |
4786 | | labelLen = (word32)XSTRLEN(label); |
4787 | | } |
4788 | | |
4789 | | ret = wc_CryptoCb_GetCert(devId, label, labelLen, id, idLen, |
4790 | | &certData, &certDataLen, &certFormat, ctx->heap); |
4791 | | if (ret != 0) { |
4792 | | ret = WOLFSSL_FAILURE; |
4793 | | goto exit; |
4794 | | } |
4795 | | |
4796 | | ret = ProcessBuffer(ctx, certData, certDataLen, certFormat, |
4797 | | CERT_TYPE, NULL, NULL, 0, GET_VERIFY_SETTING_CTX(ctx), |
4798 | | label ? label : "cert buffer"); |
4799 | | |
4800 | | exit: |
4801 | | XFREE(certData, ctx->heap, DYNAMIC_TYPE_CERT); |
4802 | | return ret; |
4803 | | } |
4804 | | |
4805 | | /* Load the label name of a certificate into the SSL context. |
4806 | | * |
4807 | | * @param [in, out] ctx SSL context object. |
4808 | | * @param [in] label Buffer holding label. |
4809 | | * @param [in] devId Device identifier. |
4810 | | * @return 1 on success. |
4811 | | * @return 0 on failure. |
4812 | | */ |
4813 | | int wolfSSL_CTX_use_certificate_label(WOLFSSL_CTX* ctx, |
4814 | | const char *label, int devId) |
4815 | | { |
4816 | | if ((ctx == NULL) || (label == NULL)) { |
4817 | | return WOLFSSL_FAILURE; |
4818 | | } |
4819 | | |
4820 | | return wolfSSL_CTX_use_certificate_ex(ctx, label, NULL, 0, devId); |
4821 | | } |
4822 | | |
4823 | | /* Load the id of a certificate into SSL context. |
4824 | | * |
4825 | | * @param [in, out] ctx SSL context object. |
4826 | | * @param [in] id Buffer holding id. |
4827 | | * @param [in] idLen Size of data in bytes. |
4828 | | * @param [in] devId Device identifier. |
4829 | | * @return 1 on success. |
4830 | | * @return 0 on failure. |
4831 | | */ |
4832 | | int wolfSSL_CTX_use_certificate_id(WOLFSSL_CTX* ctx, |
4833 | | const unsigned char *id, int idLen, int devId) |
4834 | | { |
4835 | | if ((ctx == NULL) || (id == NULL) || (idLen <= 0)) { |
4836 | | return WOLFSSL_FAILURE; |
4837 | | } |
4838 | | |
4839 | | return wolfSSL_CTX_use_certificate_ex(ctx, NULL, id, idLen, devId); |
4840 | | } |
4841 | | |
4842 | | #endif /* if defined(WOLF_CRYPTO_CB) && !defined(NO_CERTS) */ |
4843 | | |
4844 | | /* Load a certificate chain in a buffer into SSL context. |
4845 | | * |
4846 | | * @param [in, out] ctx SSL context object. |
4847 | | * @param [in] in Buffer holding DER encoded certificate chain. |
4848 | | * @param [in] sz Size of data in bytes. |
4849 | | * @param [in] format Format of data: |
4850 | | * WOLFSSL_FILETYPE_PEM or WOLFSSL_FILETYPE_ASN1. |
4851 | | * @return 1 on success. |
4852 | | * @return 0 on failure. |
4853 | | * @return Negative on error. |
4854 | | */ |
4855 | | int wolfSSL_CTX_use_certificate_chain_buffer_format(WOLFSSL_CTX* ctx, |
4856 | | const unsigned char* in, long sz, int format) |
4857 | 0 | { |
4858 | 0 | WOLFSSL_ENTER("wolfSSL_CTX_use_certificate_chain_buffer_format"); |
4859 | 0 | return ProcessBuffer(ctx, in, sz, format, CERT_TYPE, NULL, NULL, 1, |
4860 | 0 | GET_VERIFY_SETTING_CTX(ctx), "cert chain buffer"); |
4861 | 0 | } |
4862 | | |
4863 | | /* Load a PEM encoded certificate chain in a buffer into SSL context. |
4864 | | * |
4865 | | * @param [in, out] ctx SSL context object. |
4866 | | * @param [in] in Buffer holding DER encoded certificate chain. |
4867 | | * @param [in] sz Size of data in bytes. |
4868 | | * @return 1 on success. |
4869 | | * @return 0 on failure. |
4870 | | * @return Negative on error. |
4871 | | */ |
4872 | | int wolfSSL_CTX_use_certificate_chain_buffer(WOLFSSL_CTX* ctx, |
4873 | | const unsigned char* in, long sz) |
4874 | 0 | { |
4875 | 0 | #ifdef WOLFSSL_PEM_TO_DER |
4876 | 0 | return wolfSSL_CTX_use_certificate_chain_buffer_format(ctx, in, sz, |
4877 | 0 | WOLFSSL_FILETYPE_PEM); |
4878 | | #else |
4879 | | return wolfSSL_CTX_use_certificate_chain_buffer_format(ctx, in, sz, |
4880 | | WOLFSSL_FILETYPE_ASN1); |
4881 | | #endif |
4882 | 0 | } |
4883 | | |
4884 | | /* Load a user certificate in a buffer into SSL. |
4885 | | * |
4886 | | * @param [in, out] ssl SSL object. |
4887 | | * @param [in] in Buffer holding user certificate. |
4888 | | * @param [in] sz Size of data in bytes. |
4889 | | * @param [in] format Format of data: |
4890 | | * WOLFSSL_FILETYPE_PEM or WOLFSSL_FILETYPE_ASN1. |
4891 | | * @return 1 on success. |
4892 | | * @return 0 on failure. |
4893 | | * @return BAD_FUNC_ARG when ssl is NULL. |
4894 | | */ |
4895 | | int wolfSSL_use_certificate_buffer(WOLFSSL* ssl, const unsigned char* in, |
4896 | | long sz, int format) |
4897 | 0 | { |
4898 | 0 | int ret; |
4899 | |
|
4900 | 0 | WOLFSSL_ENTER("wolfSSL_use_certificate_buffer"); |
4901 | | |
4902 | | /* Validate parameters. */ |
4903 | 0 | if (ssl == NULL) { |
4904 | 0 | ret = BAD_FUNC_ARG; |
4905 | 0 | } |
4906 | 0 | else { |
4907 | 0 | ret = ProcessBuffer(ssl->ctx, in, sz, format, CERT_TYPE, ssl, NULL, 0, |
4908 | 0 | GET_VERIFY_SETTING_SSL(ssl), "cert buffer"); |
4909 | 0 | } |
4910 | |
|
4911 | 0 | return ret; |
4912 | 0 | } |
4913 | | |
4914 | | /* Load a private key in a buffer into SSL. |
4915 | | * |
4916 | | * @param [in, out] ssl SSL object. |
4917 | | * @param [in] in Buffer holding private key. |
4918 | | * @param [in] sz Size of data in bytes. |
4919 | | * @param [in] format Format of data: |
4920 | | * WOLFSSL_FILETYPE_PEM or WOLFSSL_FILETYPE_ASN1. |
4921 | | * @return 1 on success. |
4922 | | * @return 0 on failure. |
4923 | | * @return BAD_FUNC_ARG when ssl is NULL. |
4924 | | */ |
4925 | | int wolfSSL_use_PrivateKey_buffer(WOLFSSL* ssl, const unsigned char* in, |
4926 | | long sz, int format) |
4927 | 0 | { |
4928 | 0 | int ret; |
4929 | 0 | long consumed = 0; |
4930 | |
|
4931 | 0 | WOLFSSL_ENTER("wolfSSL_use_PrivateKey_buffer"); |
4932 | | |
4933 | | /* Validate parameters. */ |
4934 | 0 | if (ssl == NULL) { |
4935 | 0 | ret = BAD_FUNC_ARG; |
4936 | 0 | } |
4937 | 0 | else { |
4938 | 0 | ret = ProcessBuffer(ssl->ctx, in, sz, format, PRIVATEKEY_TYPE, ssl, |
4939 | 0 | &consumed, 0, GET_VERIFY_SETTING_SSL(ssl), "key buffer"); |
4940 | | #ifdef WOLFSSL_DUAL_ALG_CERTS |
4941 | | if ((ret == 1) && (consumed < sz)) { |
4942 | | /* When support for dual algorithm certificates is enabled, the |
4943 | | * buffer may contain both the primary and the alternative |
4944 | | * private key. Hence, we have to parse both of them. |
4945 | | */ |
4946 | | ret = ProcessBuffer(ssl->ctx, in + consumed, sz - consumed, format, |
4947 | | ALT_PRIVATEKEY_TYPE, ssl, NULL, 0, GET_VERIFY_SETTING_SSL(ssl), |
4948 | | "key buffer"); |
4949 | | } |
4950 | | #endif |
4951 | 0 | } |
4952 | |
|
4953 | 0 | return ret; |
4954 | 0 | } |
4955 | | |
4956 | | #ifdef WOLFSSL_DUAL_ALG_CERTS |
4957 | | int wolfSSL_use_AltPrivateKey_buffer(WOLFSSL* ssl, const unsigned char* in, |
4958 | | long sz, int format) |
4959 | | { |
4960 | | int ret; |
4961 | | |
4962 | | WOLFSSL_ENTER("wolfSSL_use_AltPrivateKey_buffer"); |
4963 | | ret = ProcessBuffer(ssl->ctx, in, sz, format, ALT_PRIVATEKEY_TYPE, ssl, |
4964 | | NULL, 0, GET_VERIFY_SETTING_SSL(ssl), "alt key buffer"); |
4965 | | WOLFSSL_LEAVE("wolfSSL_use_AltPrivateKey_buffer", ret); |
4966 | | |
4967 | | return ret; |
4968 | | } |
4969 | | #endif /* WOLFSSL_DUAL_ALG_CERTS */ |
4970 | | |
4971 | | #ifdef WOLF_PRIVATE_KEY_ID |
4972 | | /* Load the id of a private key into SSL. |
4973 | | * |
4974 | | * @param [in, out] ssl SSL object. |
4975 | | * @param [in] id Buffer holding id. |
4976 | | * @param [in] sz Size of data in bytes. |
4977 | | * @param [in] devId Device identifier. |
4978 | | * @return 1 on success. |
4979 | | * @return 0 on failure. |
4980 | | */ |
4981 | | int wolfSSL_use_PrivateKey_Id(WOLFSSL* ssl, const unsigned char* id, |
4982 | | long sz, int devId) |
4983 | | { |
4984 | | int ret = 1; |
4985 | | |
4986 | | if (ssl == NULL || id == NULL || sz < 0) { |
4987 | | return 0; |
4988 | | } |
4989 | | |
4990 | | /* Dispose of old private key if owned and allocate and copy in id. */ |
4991 | | if (ssl->buffers.weOwnKey) { |
4992 | | FreeDer(&ssl->buffers.key); |
4993 | | #ifdef WOLFSSL_BLIND_PRIVATE_KEY |
4994 | | FreeDer(&ssl->buffers.keyMask); |
4995 | | #endif |
4996 | | } |
4997 | | if (AllocCopyDer(&ssl->buffers.key, id, (word32)sz, PRIVATEKEY_TYPE, |
4998 | | ssl->heap) != 0) { |
4999 | | ret = 0; |
5000 | | } |
5001 | | if (ret == 1) { |
5002 | | /* Buffer now ours. */ |
5003 | | ssl->buffers.weOwnKey = 1; |
5004 | | /* Private key is an id. */ |
5005 | | ssl->buffers.keyId = 1; |
5006 | | ssl->buffers.keyLabel = 0; |
5007 | | /* Set private key device id to be one passed in or for SSL. */ |
5008 | | if (devId != INVALID_DEVID) { |
5009 | | ssl->buffers.keyDevId = devId; |
5010 | | } |
5011 | | else { |
5012 | | ssl->buffers.keyDevId = ssl->devId; |
5013 | | } |
5014 | | |
5015 | | #ifdef WOLFSSL_DUAL_ALG_CERTS |
5016 | | /* Set the ID for the alternative key, too. User can still override that |
5017 | | * afterwards. */ |
5018 | | ret = wolfSSL_use_AltPrivateKey_Id(ssl, id, sz, devId); |
5019 | | #endif |
5020 | | } |
5021 | | |
5022 | | return ret; |
5023 | | } |
5024 | | |
5025 | | /* Load the id of a private key into SSL and set key size. |
5026 | | * |
5027 | | * @param [in, out] ssl SSL object. |
5028 | | * @param [in] id Buffer holding id. |
5029 | | * @param [in] sz Size of data in bytes. |
5030 | | * @param [in] devId Device identifier. |
5031 | | * @param [in] keySz Size of key. |
5032 | | * @return 1 on success. |
5033 | | * @return 0 on failure. |
5034 | | */ |
5035 | | int wolfSSL_use_PrivateKey_Id_ex(WOLFSSL* ssl, const unsigned char* id, |
5036 | | long sz, int devId, long keySz) |
5037 | | { |
5038 | | int ret = wolfSSL_use_PrivateKey_Id(ssl, id, sz, devId); |
5039 | | if (ret == 1) { |
5040 | | /* Set the key size which normally is calculated during decoding. */ |
5041 | | ssl->buffers.keySz = (int)keySz; |
5042 | | } |
5043 | | |
5044 | | return ret; |
5045 | | } |
5046 | | |
5047 | | /* Load the label name of a private key into SSL. |
5048 | | * |
5049 | | * @param [in, out] ssl SSL object. |
5050 | | * @param [in] label Buffer holding label. |
5051 | | * @param [in] devId Device identifier. |
5052 | | * @return 1 on success. |
5053 | | * @return 0 on failure. |
5054 | | */ |
5055 | | int wolfSSL_use_PrivateKey_Label(WOLFSSL* ssl, const char* label, int devId) |
5056 | | { |
5057 | | int ret = 1; |
5058 | | word32 sz; |
5059 | | |
5060 | | if (ssl == NULL || label == NULL) { |
5061 | | return 0; |
5062 | | } |
5063 | | |
5064 | | sz = (word32)XSTRLEN(label) + 1; |
5065 | | |
5066 | | /* Dispose of old private key if owned and allocate and copy in label. */ |
5067 | | if (ssl->buffers.weOwnKey) { |
5068 | | FreeDer(&ssl->buffers.key); |
5069 | | #ifdef WOLFSSL_BLIND_PRIVATE_KEY |
5070 | | FreeDer(&ssl->buffers.keyMask); |
5071 | | #endif |
5072 | | } |
5073 | | if (AllocCopyDer(&ssl->buffers.key, (const byte*)label, (word32)sz, |
5074 | | PRIVATEKEY_TYPE, ssl->heap) != 0) { |
5075 | | ret = 0; |
5076 | | } |
5077 | | if (ret == 1) { |
5078 | | /* Buffer now ours. */ |
5079 | | ssl->buffers.weOwnKey = 1; |
5080 | | /* Private key is a label. */ |
5081 | | ssl->buffers.keyId = 0; |
5082 | | ssl->buffers.keyLabel = 1; |
5083 | | /* Set private key device id to be one passed in or for SSL. */ |
5084 | | if (devId != INVALID_DEVID) { |
5085 | | ssl->buffers.keyDevId = devId; |
5086 | | } |
5087 | | else { |
5088 | | ssl->buffers.keyDevId = ssl->devId; |
5089 | | } |
5090 | | |
5091 | | #ifdef WOLFSSL_DUAL_ALG_CERTS |
5092 | | /* Set the label for the alternative key, too. User can still override |
5093 | | * that afterwards. */ |
5094 | | ret = wolfSSL_use_AltPrivateKey_Label(ssl, label, devId); |
5095 | | #endif |
5096 | | } |
5097 | | |
5098 | | return ret; |
5099 | | } |
5100 | | |
5101 | | #ifdef WOLFSSL_DUAL_ALG_CERTS |
5102 | | int wolfSSL_use_AltPrivateKey_Id(WOLFSSL* ssl, const unsigned char* id, long sz, |
5103 | | int devId) |
5104 | | { |
5105 | | int ret = 1; |
5106 | | |
5107 | | if ((ssl == NULL) || (id == NULL) || (sz < 0)) { |
5108 | | ret = 0; |
5109 | | } |
5110 | | |
5111 | | if (ret == 1) { |
5112 | | if (ssl->buffers.weOwnAltKey) { |
5113 | | FreeDer(&ssl->buffers.altKey); |
5114 | | #ifdef WOLFSSL_BLIND_PRIVATE_KEY |
5115 | | FreeDer(&ssl->buffers.altKeyMask); |
5116 | | #endif |
5117 | | } |
5118 | | if (AllocDer(&ssl->buffers.altKey, (word32)sz, ALT_PRIVATEKEY_TYPE, |
5119 | | ssl->heap) != 0) { |
5120 | | ret = 0; |
5121 | | } |
5122 | | } |
5123 | | if (ret == 1) { |
5124 | | XMEMCPY(ssl->buffers.altKey->buffer, id, (word32)sz); |
5125 | | ssl->buffers.weOwnAltKey = 1; |
5126 | | ssl->buffers.altKeyId = 1; |
5127 | | if (devId != INVALID_DEVID) { |
5128 | | ssl->buffers.altKeyDevId = devId; |
5129 | | } |
5130 | | else { |
5131 | | ssl->buffers.altKeyDevId = ssl->devId; |
5132 | | } |
5133 | | } |
5134 | | |
5135 | | return ret; |
5136 | | } |
5137 | | |
5138 | | int wolfSSL_use_AltPrivateKey_Id_ex(WOLFSSL* ssl, const unsigned char* id, |
5139 | | long sz, int devId, long keySz) |
5140 | | { |
5141 | | int ret = wolfSSL_use_AltPrivateKey_Id(ssl, id, sz, devId); |
5142 | | if (ret == 1) { |
5143 | | ssl->buffers.altKeySz = (word32)keySz; |
5144 | | } |
5145 | | |
5146 | | return ret; |
5147 | | } |
5148 | | |
5149 | | int wolfSSL_use_AltPrivateKey_Label(WOLFSSL* ssl, const char* label, int devId) |
5150 | | { |
5151 | | int ret = 1; |
5152 | | word32 sz; |
5153 | | |
5154 | | if ((ssl == NULL) || (label == NULL)) { |
5155 | | ret = 0; |
5156 | | } |
5157 | | |
5158 | | if (ret == 1) { |
5159 | | sz = (word32)XSTRLEN(label) + 1; |
5160 | | if (ssl->buffers.weOwnAltKey) { |
5161 | | FreeDer(&ssl->buffers.altKey); |
5162 | | #ifdef WOLFSSL_BLIND_PRIVATE_KEY |
5163 | | FreeDer(&ssl->buffers.altKeyMask); |
5164 | | #endif |
5165 | | } |
5166 | | if (AllocDer(&ssl->buffers.altKey, (word32)sz, ALT_PRIVATEKEY_TYPE, |
5167 | | ssl->heap) != 0) { |
5168 | | ret = 0; |
5169 | | } |
5170 | | } |
5171 | | if (ret == 1) { |
5172 | | XMEMCPY(ssl->buffers.altKey->buffer, label, sz); |
5173 | | ssl->buffers.weOwnAltKey = 1; |
5174 | | ssl->buffers.altKeyLabel = 1; |
5175 | | if (devId != INVALID_DEVID) { |
5176 | | ssl->buffers.altKeyDevId = devId; |
5177 | | } |
5178 | | else { |
5179 | | ssl->buffers.altKeyDevId = ssl->devId; |
5180 | | } |
5181 | | } |
5182 | | |
5183 | | return ret; |
5184 | | } |
5185 | | #endif /* WOLFSSL_DUAL_ALG_CERTS */ |
5186 | | #endif /* WOLF_PRIVATE_KEY_ID */ |
5187 | | |
5188 | | /* Load a certificate chain in a buffer into SSL. |
5189 | | * |
5190 | | * @param [in, out] ssl SSL object. |
5191 | | * @param [in] in Buffer holding DER encoded certificate chain. |
5192 | | * @param [in] sz Size of data in bytes. |
5193 | | * @param [in] format Format of data: |
5194 | | * WOLFSSL_FILETYPE_PEM or WOLFSSL_FILETYPE_ASN1. |
5195 | | * @return 1 on success. |
5196 | | * @return 0 on failure. |
5197 | | * @return BAD_FUNC_ARG when ssl is NULL. |
5198 | | */ |
5199 | | int wolfSSL_use_certificate_chain_buffer_format(WOLFSSL* ssl, |
5200 | | const unsigned char* in, long sz, int format) |
5201 | 0 | { |
5202 | 0 | int ret; |
5203 | |
|
5204 | 0 | WOLFSSL_ENTER("wolfSSL_use_certificate_chain_buffer_format"); |
5205 | | |
5206 | | /* Validate parameters. */ |
5207 | 0 | if (ssl == NULL) { |
5208 | 0 | ret = BAD_FUNC_ARG; |
5209 | 0 | } |
5210 | 0 | else { |
5211 | 0 | ret = ProcessBuffer(ssl->ctx, in, sz, format, CERT_TYPE, ssl, NULL, 1, |
5212 | 0 | GET_VERIFY_SETTING_SSL(ssl), "cert chain buffer"); |
5213 | 0 | } |
5214 | |
|
5215 | 0 | return ret; |
5216 | 0 | } |
5217 | | |
5218 | | /* Load a PEM encoded certificate chain in a buffer into SSL. |
5219 | | * |
5220 | | * @param [in, out] ssl SSL object. |
5221 | | * @param [in] in Buffer holding DER encoded certificate chain. |
5222 | | * @param [in] sz Size of data in bytes. |
5223 | | * @return 1 on success. |
5224 | | * @return 0 on failure. |
5225 | | * @return Negative on error. |
5226 | | */ |
5227 | | int wolfSSL_use_certificate_chain_buffer(WOLFSSL* ssl, const unsigned char* in, |
5228 | | long sz) |
5229 | 0 | { |
5230 | 0 | #ifdef WOLFSSL_PEM_TO_DER |
5231 | 0 | return wolfSSL_use_certificate_chain_buffer_format(ssl, in, sz, |
5232 | 0 | WOLFSSL_FILETYPE_PEM); |
5233 | | #else |
5234 | | return wolfSSL_use_certificate_chain_buffer_format(ssl, in, sz, |
5235 | | WOLFSSL_FILETYPE_ASN1); |
5236 | | #endif |
5237 | 0 | } |
5238 | | |
5239 | | #if defined(OPENSSL_EXTRA) || defined(HAVE_LIGHTY) || \ |
5240 | | defined(WOLFSSL_MYSQL_COMPATIBLE) || defined(HAVE_STUNNEL) || \ |
5241 | | defined(WOLFSSL_NGINX) || defined(HAVE_POCO_LIB) || \ |
5242 | | defined(WOLFSSL_HAPROXY) |
5243 | | /* Add certificate to chain. |
5244 | | * |
5245 | | * @param [in, out] chain Buffer holding encoded certificate for TLS. |
5246 | | * @param [in] weOwn Indicates we need to free chain if repleced. |
5247 | | * @param [in] cert Buffer holding DER encoded certificate. |
5248 | | * @param [in] certSz Size of DER encoded certificate in bytes. |
5249 | | * @param [in] heap Dynamic memory allocation hint. |
5250 | | * @return 1 on success. |
5251 | | * @return 0 on failure. |
5252 | | */ |
5253 | | static int wolfssl_add_to_chain(DerBuffer** chain, int weOwn, const byte* cert, |
5254 | | word32 certSz, void* heap) |
5255 | | { |
5256 | | int res = 1; |
5257 | | int ret; |
5258 | | DerBuffer* oldChain = *chain; |
5259 | | DerBuffer* newChain = NULL; |
5260 | | word32 len = 0; |
5261 | | |
5262 | | if (oldChain != NULL) { |
5263 | | /* Get length of previous chain. */ |
5264 | | len = oldChain->length; |
5265 | | } |
5266 | | /* Check for integer overflow in size calculation. */ |
5267 | | if ((len > WOLFSSL_MAX_32BIT - CERT_HEADER_SZ) || |
5268 | | (certSz > WOLFSSL_MAX_32BIT - CERT_HEADER_SZ - len)) { |
5269 | | WOLFSSL_MSG("wolfssl_add_to_chain overflow"); |
5270 | | res = 0; |
5271 | | } |
5272 | | if (res == 1) { |
5273 | | /* Allocate DER buffer big enough to hold old and new certificates. */ |
5274 | | ret = AllocDer(&newChain, len + CERT_HEADER_SZ + certSz, CERT_TYPE, |
5275 | | heap); |
5276 | | if (ret != 0) { |
5277 | | WOLFSSL_MSG("AllocDer error"); |
5278 | | res = 0; |
5279 | | } |
5280 | | } |
5281 | | |
5282 | | if (res == 1) { |
5283 | | if (oldChain != NULL) { |
5284 | | /* Place old chain in new buffer. */ |
5285 | | XMEMCPY(newChain->buffer, oldChain->buffer, len); |
5286 | | } |
5287 | | /* Append length and DER encoded certificate. */ |
5288 | | c32to24(certSz, newChain->buffer + len); |
5289 | | XMEMCPY(newChain->buffer + len + CERT_HEADER_SZ, cert, certSz); |
5290 | | |
5291 | | /* Dispose of old chain if we own it. */ |
5292 | | if (weOwn) { |
5293 | | FreeDer(chain); |
5294 | | } |
5295 | | /* Replace chain. */ |
5296 | | *chain = newChain; |
5297 | | } |
5298 | | |
5299 | | return res; |
5300 | | } |
5301 | | #endif |
5302 | | |
5303 | | #ifdef OPENSSL_EXTRA |
5304 | | |
5305 | | /* Add a certificate to end of chain sent in TLS handshake. |
5306 | | * |
5307 | | * The certificate is only presented to the peer. It is deliberately kept out |
5308 | | * of the certificate manager so that it cannot be used to verify peers. |
5309 | | * |
5310 | | * @param [in, out] ctx SSL context. |
5311 | | * @param [in] der Buffer holding DER encoded certificate. |
5312 | | * @param [in] derSz Size of data in buffer. |
5313 | | * @return 1 on success. |
5314 | | * @return 0 on failure. |
5315 | | */ |
5316 | | static int wolfssl_ctx_add_to_chain(WOLFSSL_CTX* ctx, const byte* der, |
5317 | | int derSz) |
5318 | | { |
5319 | | int res; |
5320 | | |
5321 | | /* Add chain to DER buffer. */ |
5322 | | res = wolfssl_add_to_chain(&ctx->certChain, 1, der, (word32)derSz, |
5323 | | ctx->heap); |
5324 | | if (res == 1) { |
5325 | | ctx->certChainCnt++; |
5326 | | } |
5327 | | |
5328 | | return res; |
5329 | | } |
5330 | | |
5331 | | /* Add a certificate to chain sent in TLS handshake. |
5332 | | * |
5333 | | * @param [in, out] ctx SSL context. |
5334 | | * @param [in] x509 X509 certificate object. |
5335 | | * @return 1 on success. |
5336 | | * @return 0 on failure. |
5337 | | */ |
5338 | | long wolfSSL_CTX_add_extra_chain_cert(WOLFSSL_CTX* ctx, WOLFSSL_X509* x509) |
5339 | | { |
5340 | | int ret = 1; |
5341 | | int derSz = 0; |
5342 | | const byte* der = NULL; |
5343 | | |
5344 | | WOLFSSL_ENTER("wolfSSL_CTX_add_extra_chain_cert"); |
5345 | | |
5346 | | /* Validate parameters. */ |
5347 | | if ((ctx == NULL) || (x509 == NULL)) { |
5348 | | WOLFSSL_MSG("Bad Argument"); |
5349 | | ret = 0; |
5350 | | } |
5351 | | |
5352 | | if (ret == 1) { |
5353 | | /* Get the DER encoding of the certificate from the X509 object. */ |
5354 | | der = wolfSSL_X509_get_der(x509, &derSz); |
5355 | | /* Validate buffer. */ |
5356 | | if ((der == NULL) || (derSz <= 0)) { |
5357 | | WOLFSSL_MSG("Error getting X509 DER"); |
5358 | | ret = 0; |
5359 | | } |
5360 | | } |
5361 | | |
5362 | | if ((ret == 1) && (ctx->certificate == NULL)) { |
5363 | | WOLFSSL_ENTER("wolfSSL_use_certificate_chain_buffer_format"); |
5364 | | |
5365 | | /* Process buffer makes first certificate the leaf. */ |
5366 | | ret = ProcessBuffer(ctx, der, derSz, WOLFSSL_FILETYPE_ASN1, CERT_TYPE, |
5367 | | NULL, NULL, 1, GET_VERIFY_SETTING_CTX(ctx), "extra chain buffer"); |
5368 | | if (ret != 1) { |
5369 | | ret = 0; |
5370 | | } |
5371 | | } |
5372 | | else if (ret == 1) { |
5373 | | /* Add certificate to existing chain. */ |
5374 | | ret = wolfssl_ctx_add_to_chain(ctx, der, derSz); |
5375 | | } |
5376 | | |
5377 | | if (ret == 1) { |
5378 | | /* On success WOLFSSL_X509 memory is responsibility of SSL context. */ |
5379 | | wolfSSL_X509_free(x509); |
5380 | | x509 = NULL; |
5381 | | } |
5382 | | |
5383 | | WOLFSSL_LEAVE("wolfSSL_CTX_add_extra_chain_cert", ret); |
5384 | | return ret; |
5385 | | } |
5386 | | |
5387 | | #endif /* OPENSSL_EXTRA */ |
5388 | | |
5389 | | #if defined(OPENSSL_EXTRA) || defined(HAVE_LIGHTY) || \ |
5390 | | defined(WOLFSSL_MYSQL_COMPATIBLE) || defined(HAVE_STUNNEL) || \ |
5391 | | defined(WOLFSSL_NGINX) || defined(HAVE_POCO_LIB) || \ |
5392 | | defined(WOLFSSL_HAPROXY) |
5393 | | /* Load a certificate into SSL context. |
5394 | | * |
5395 | | * @param [in, out] ctx SSL context object. |
5396 | | * @param [in] x509 X509 certificate object. |
5397 | | * @return 1 on success. |
5398 | | * @return 0 on failure. |
5399 | | */ |
5400 | | int wolfSSL_CTX_use_certificate(WOLFSSL_CTX *ctx, WOLFSSL_X509 *x) |
5401 | | { |
5402 | | int res = 1; |
5403 | | int ret; |
5404 | | |
5405 | | WOLFSSL_ENTER("wolfSSL_CTX_use_certificate"); |
5406 | | |
5407 | | /* Validate parameters. */ |
5408 | | if ((ctx == NULL) || (x == NULL) || (x->derCert == NULL)) { |
5409 | | WOLFSSL_MSG("Bad parameter"); |
5410 | | res = 0; |
5411 | | } |
5412 | | |
5413 | | if (res == 1) { |
5414 | | /* Replace certificate buffer with one holding the new certificate. */ |
5415 | | FreeDer(&ctx->certificate); |
5416 | | ret = AllocCopyDer(&ctx->certificate, x->derCert->buffer, |
5417 | | x->derCert->length, CERT_TYPE, ctx->heap); |
5418 | | if (ret != 0) { |
5419 | | res = 0; |
5420 | | } |
5421 | | } |
5422 | | |
5423 | | #ifdef KEEP_OUR_CERT |
5424 | | if (res == 1) { |
5425 | | /* Dispose of our certificate if it is ours. */ |
5426 | | if ((ctx->ourCert != NULL) && ctx->ownOurCert) { |
5427 | | wolfSSL_X509_free(ctx->ourCert); |
5428 | | } |
5429 | | #ifndef WOLFSSL_X509_STORE_CERTS |
5430 | | /* Keep a reference to the new certificate. */ |
5431 | | ctx->ourCert = x; |
5432 | | if (wolfSSL_X509_up_ref(x) != 1) { |
5433 | | res = 0; |
5434 | | } |
5435 | | #else |
5436 | | /* Keep a copy of the new certificate. */ |
5437 | | ctx->ourCert = wolfSSL_X509_d2i_ex(NULL, x->derCert->buffer, |
5438 | | x->derCert->length, ctx->heap); |
5439 | | if (ctx->ourCert == NULL) { |
5440 | | res = 0; |
5441 | | } |
5442 | | #endif |
5443 | | /* Now own our certificate. */ |
5444 | | ctx->ownOurCert = 1; |
5445 | | } |
5446 | | #endif |
5447 | | |
5448 | | if (res == 1) { |
5449 | | /* Set have options based on public key OID. */ |
5450 | | wolfssl_set_have_from_key_oid(ctx, NULL, x->pubKeyOID); |
5451 | | } |
5452 | | |
5453 | | return res; |
5454 | | } |
5455 | | |
5456 | | /* Add the certificate to the chain in the SSL context and own the X509 object. |
5457 | | * |
5458 | | * @param [in, out] ctx SSL context object. |
5459 | | * @param [in] x509 X509 certificate object. |
5460 | | * @return 1 on success. |
5461 | | * @return 0 on failure. |
5462 | | */ |
5463 | | int wolfSSL_CTX_add0_chain_cert(WOLFSSL_CTX* ctx, WOLFSSL_X509* x509) |
5464 | | { |
5465 | | int ret; |
5466 | | |
5467 | | WOLFSSL_ENTER("wolfSSL_CTX_add0_chain_cert"); |
5468 | | |
5469 | | /* Add certificate to chain and copy or up reference it. */ |
5470 | | ret = wolfSSL_CTX_add1_chain_cert(ctx, x509); |
5471 | | if (ret == 1) { |
5472 | | /* Down reference or free original now as we own certificate. */ |
5473 | | wolfSSL_X509_free(x509); |
5474 | | x509 = NULL; |
5475 | | } |
5476 | | |
5477 | | return ret; |
5478 | | } |
5479 | | |
5480 | | /* Add the certificate to the chain in the SSL context. |
5481 | | * |
5482 | | * X509 object copied or up referenced. |
5483 | | * |
5484 | | * @param [in, out] ctx SSL context object. |
5485 | | * @param [in] x509 X509 certificate object. |
5486 | | * @return 1 on success. |
5487 | | * @return 0 on failure. |
5488 | | */ |
5489 | | int wolfSSL_CTX_add1_chain_cert(WOLFSSL_CTX* ctx, WOLFSSL_X509* x509) |
5490 | | { |
5491 | | int ret = 1; |
5492 | | |
5493 | | WOLFSSL_ENTER("wolfSSL_CTX_add1_chain_cert"); |
5494 | | |
5495 | | /* Validate parameters. */ |
5496 | | if ((ctx == NULL) || (x509 == NULL) || (x509->derCert == NULL)) { |
5497 | | ret = 0; |
5498 | | } |
5499 | | |
5500 | | /* Check if we already have set a certificate. */ |
5501 | | if ((ret == 1) && (ctx->certificate == NULL)) { |
5502 | | /* Use the certificate. */ |
5503 | | ret = wolfSSL_CTX_use_certificate(ctx, x509); |
5504 | | } |
5505 | | /* Increase reference count as we will store it. */ |
5506 | | else if ((ret == 1) && ((ret = wolfSSL_X509_up_ref(x509)) == 1)) { |
5507 | | /* Add DER encoding to chain. The certificate is presented to the |
5508 | | * peer only and must not become a CA to verify peers with. */ |
5509 | | ret = wolfssl_add_to_chain(&ctx->certChain, 1, |
5510 | | x509->derCert->buffer, x509->derCert->length, ctx->heap); |
5511 | | if (ret == 1) { |
5512 | | ctx->certChainCnt++; |
5513 | | } |
5514 | | /* Store cert in stack to free it later. */ |
5515 | | if ((ret == 1) && (ctx->x509Chain == NULL)) { |
5516 | | /* Create a stack for certificates. */ |
5517 | | ctx->x509Chain = wolfSSL_sk_X509_new_null(); |
5518 | | if (ctx->x509Chain == NULL) { |
5519 | | WOLFSSL_MSG("wolfSSL_sk_X509_new_null error"); |
5520 | | ret = 0; |
5521 | | } |
5522 | | } |
5523 | | if (ret == 1) { |
5524 | | /* Push the X509 object onto stack. */ |
5525 | | ret = wolfSSL_sk_X509_push(ctx->x509Chain, x509) > 0 |
5526 | | ? WOLFSSL_SUCCESS : WOLFSSL_FAILURE; |
5527 | | } |
5528 | | |
5529 | | if (ret != 1) { |
5530 | | /* Decrease reference count on error as we didn't store it. */ |
5531 | | wolfSSL_X509_free(x509); |
5532 | | x509 = NULL; |
5533 | | } |
5534 | | } |
5535 | | |
5536 | | return WS_RC(ret); |
5537 | | } |
5538 | | |
5539 | | #ifdef KEEP_OUR_CERT |
5540 | | /* Add the certificate to the chain in the SSL and own the X509 object. |
5541 | | * |
5542 | | * @param [in, out] ssl SSL object. |
5543 | | * @param [in] x509 X509 certificate object. |
5544 | | * @return 1 on success. |
5545 | | * @return 0 on failure. |
5546 | | */ |
5547 | | int wolfSSL_add0_chain_cert(WOLFSSL* ssl, WOLFSSL_X509* x509) |
5548 | | { |
5549 | | int ret = 1; |
5550 | | |
5551 | | WOLFSSL_ENTER("wolfSSL_add0_chain_cert"); |
5552 | | |
5553 | | /* Validate parameters. */ |
5554 | | if ((ssl == NULL) || (x509 == NULL) || (x509->derCert == NULL)) { |
5555 | | ret = 0; |
5556 | | } |
5557 | | |
5558 | | /* Check if we already have set a certificate. */ |
5559 | | if ((ret == 1) && (ssl->buffers.certificate == NULL)) { |
5560 | | /* Use the certificate. */ |
5561 | | ret = wolfSSL_use_certificate(ssl, x509); |
5562 | | if (ret == 1) { |
5563 | | /* Dispose of old certificate if we own it. */ |
5564 | | if (ssl->buffers.weOwnCert) { |
5565 | | wolfSSL_X509_free(ssl->ourCert); |
5566 | | } |
5567 | | /* Store cert to free it later. */ |
5568 | | ssl->ourCert = x509; |
5569 | | ssl->buffers.weOwnCert = 1; |
5570 | | } |
5571 | | } |
5572 | | else if (ret == 1) { |
5573 | | /* Add DER encoding to chain. */ |
5574 | | ret = wolfssl_add_to_chain(&ssl->buffers.certChain, |
5575 | | ssl->buffers.weOwnCertChain, x509->derCert->buffer, |
5576 | | x509->derCert->length, ssl->heap); |
5577 | | if (ret == 1) { |
5578 | | /* We now own cert chain. */ |
5579 | | ssl->buffers.weOwnCertChain = 1; |
5580 | | /* Account for the certificate just added to the chain. */ |
5581 | | ssl->buffers.certChainCnt++; |
5582 | | /* Create a stack to put certificate into. */ |
5583 | | if (ssl->ourCertChain == NULL) { |
5584 | | ssl->ourCertChain = wolfSSL_sk_X509_new_null(); |
5585 | | if (ssl->ourCertChain == NULL) { |
5586 | | WOLFSSL_MSG("wolfSSL_sk_X509_new_null error"); |
5587 | | ret = 0; |
5588 | | } |
5589 | | } |
5590 | | } |
5591 | | if (ret == 1) { |
5592 | | /* Push X509 object onto stack to be freed. */ |
5593 | | ret = wolfSSL_sk_X509_push(ssl->ourCertChain, x509) > 0 |
5594 | | ? WOLFSSL_SUCCESS : WOLFSSL_FAILURE; |
5595 | | } |
5596 | | } |
5597 | | return WS_RC(ret); |
5598 | | } |
5599 | | |
5600 | | /* Add the certificate to the chain in the SSL. |
5601 | | * |
5602 | | * X509 object is up referenced. |
5603 | | * |
5604 | | * @param [in, out] ssl SSL object. |
5605 | | * @param [in] x509 X509 certificate object. |
5606 | | * @return 1 on success. |
5607 | | * @return 0 on failure. |
5608 | | */ |
5609 | | int wolfSSL_add1_chain_cert(WOLFSSL* ssl, WOLFSSL_X509* x509) |
5610 | | { |
5611 | | int ret = 1; |
5612 | | |
5613 | | WOLFSSL_ENTER("wolfSSL_add1_chain_cert"); |
5614 | | |
5615 | | /* Validate parameters. */ |
5616 | | if ((ssl == NULL) || (x509 == NULL) || (x509->derCert == NULL)) { |
5617 | | ret = 0; |
5618 | | } |
5619 | | |
5620 | | /* Increase reference count on X509 object before adding. */ |
5621 | | if ((ret == 1) && ((ret = wolfSSL_X509_up_ref(x509)) == 1)) { |
5622 | | /* Add this to the chain. */ |
5623 | | if ((ret = wolfSSL_add0_chain_cert(ssl, x509)) != 1) { |
5624 | | /* Decrease reference count on error as not stored. */ |
5625 | | wolfSSL_X509_free(x509); |
5626 | | x509 = NULL; |
5627 | | } |
5628 | | } |
5629 | | |
5630 | | return ret; |
5631 | | } |
5632 | | |
5633 | | /* Clear all extra chain certificates set on the SSL object. |
5634 | | * |
5635 | | * Mirrors OpenSSL's SSL_clear_chain_certs(): drops the chain this SSL will |
5636 | | * send, whether it was added via SSL_add0_chain_cert / SSL_add1_chain_cert or |
5637 | | * inherited from the WOLFSSL_CTX. A chain still owned by the WOLFSSL_CTX is |
5638 | | * only detached, not freed, so the context is left usable. The leaf |
5639 | | * certificate and the private key are not affected. |
5640 | | * |
5641 | | * @param [in, out] ssl SSL object. |
5642 | | * @return 1 on success. |
5643 | | * @return 0 when ssl is NULL. |
5644 | | */ |
5645 | | int wolfSSL_clear_chain_certs(WOLFSSL* ssl) |
5646 | | { |
5647 | | WOLFSSL_ENTER("wolfSSL_clear_chain_certs"); |
5648 | | |
5649 | | if (ssl == NULL) |
5650 | | return 0; |
5651 | | |
5652 | | /* Free the DER-encoded chain buffer if this SSL owns it. */ |
5653 | | if (ssl->buffers.weOwnCertChain) { |
5654 | | FreeDer(&ssl->buffers.certChain); |
5655 | | ssl->buffers.weOwnCertChain = 0; |
5656 | | } |
5657 | | ssl->buffers.certChain = NULL; |
5658 | | ssl->buffers.certChainCnt = 0; |
5659 | | |
5660 | | /* Free the X509 stack used to track ownership of added chain certs. */ |
5661 | | if (ssl->ourCertChain != NULL) { |
5662 | | wolfSSL_sk_X509_pop_free(ssl->ourCertChain, NULL); |
5663 | | ssl->ourCertChain = NULL; |
5664 | | } |
5665 | | |
5666 | | return 1; |
5667 | | } |
5668 | | #endif /* KEEP_OUR_CERT */ |
5669 | | #endif /* OPENSSL_EXTRA, HAVE_LIGHTY, WOLFSSL_MYSQL_COMPATIBLE, HAVE_STUNNEL, |
5670 | | WOLFSSL_NGINX, HAVE_POCO_LIB, WOLFSSL_HAPROXY */ |
5671 | | |
5672 | | #ifdef OPENSSL_EXTRA |
5673 | | |
5674 | | /* Load a private key into SSL context. |
5675 | | * |
5676 | | * @param [in, out] ctx SSL context object. |
5677 | | * @param [in] pkey EVP private key. |
5678 | | * @return 1 on success. |
5679 | | * @return 0 on failure. |
5680 | | */ |
5681 | | int wolfSSL_CTX_use_PrivateKey(WOLFSSL_CTX *ctx, WOLFSSL_EVP_PKEY *pkey) |
5682 | | { |
5683 | | int ret = 1; |
5684 | | |
5685 | | WOLFSSL_ENTER("wolfSSL_CTX_use_PrivateKey"); |
5686 | | |
5687 | | /* Validate parameters. */ |
5688 | | if ((ctx == NULL) || (pkey == NULL) || (pkey->pkey.ptr == NULL)) { |
5689 | | ret = 0; |
5690 | | } |
5691 | | |
5692 | | if (ret == 1) { |
5693 | | switch (pkey->type) { |
5694 | | #if defined(WOLFSSL_KEY_GEN) && !defined(NO_RSA) |
5695 | | case WC_EVP_PKEY_RSA: |
5696 | | WOLFSSL_MSG("populating RSA key"); |
5697 | | ret = PopulateRSAEvpPkeyDer(pkey); |
5698 | | break; |
5699 | | #endif /* (WOLFSSL_KEY_GEN || OPENSSL_EXTRA) && !NO_RSA */ |
5700 | | #if !defined(HAVE_SELFTEST) && (defined(WOLFSSL_KEY_GEN) || \ |
5701 | | defined(WOLFSSL_CERT_GEN)) && !defined(NO_DSA) |
5702 | | case WC_EVP_PKEY_DSA: |
5703 | | break; |
5704 | | #endif /* !HAVE_SELFTEST && (WOLFSSL_KEY_GEN || WOLFSSL_CERT_GEN) && |
5705 | | * !NO_DSA */ |
5706 | | #ifdef HAVE_ECC |
5707 | | case WC_EVP_PKEY_EC: |
5708 | | WOLFSSL_MSG("populating ECC key"); |
5709 | | ret = ECC_populate_EVP_PKEY(pkey, pkey->ecc); |
5710 | | break; |
5711 | | #endif |
5712 | | #ifdef HAVE_ED25519 |
5713 | | case WC_EVP_PKEY_ED25519: |
5714 | | /* DER is already stored in pkey->pkey.ptr by d2i_evp_pkey. */ |
5715 | | WOLFSSL_MSG("populating Ed25519 key"); |
5716 | | break; |
5717 | | #endif |
5718 | | #ifdef HAVE_ED448 |
5719 | | case WC_EVP_PKEY_ED448: |
5720 | | /* DER is already stored in pkey->pkey.ptr by d2i_evp_pkey. */ |
5721 | | WOLFSSL_MSG("populating Ed448 key"); |
5722 | | break; |
5723 | | #endif |
5724 | | default: |
5725 | | ret = 0; |
5726 | | } |
5727 | | } |
5728 | | |
5729 | | if (ret == 1) { |
5730 | | /* ptr for WOLFSSL_EVP_PKEY struct is expected to be DER format */ |
5731 | | ret = wolfSSL_CTX_use_PrivateKey_buffer(ctx, |
5732 | | (const unsigned char*)pkey->pkey.ptr, pkey->pkey_sz, |
5733 | | WOLFSSL_FILETYPE_ASN1); |
5734 | | } |
5735 | | |
5736 | | return ret; |
5737 | | } |
5738 | | |
5739 | | #endif /* OPENSSL_EXTRA */ |
5740 | | |
5741 | | #if defined(OPENSSL_ALL) || defined(WOLFSSL_ASIO) || \ |
5742 | | defined(WOLFSSL_HAPROXY) || defined(WOLFSSL_NGINX) || defined(WOLFSSL_QT) |
5743 | | /* Load a DER encoded certificate in a buffer into SSL context. |
5744 | | * |
5745 | | * @param [in, out] ctx SSL context object. |
5746 | | * @param [in] der Buffer holding DER encoded certificate. |
5747 | | * @param [in] derSz Size of data in bytes. |
5748 | | * @return 1 on success. |
5749 | | * @return 0 on failure. |
5750 | | */ |
5751 | | int wolfSSL_CTX_use_certificate_ASN1(WOLFSSL_CTX *ctx, int derSz, |
5752 | | const unsigned char *der) |
5753 | | { |
5754 | | int ret = 1; |
5755 | | |
5756 | | WOLFSSL_ENTER("wolfSSL_CTX_use_certificate_ASN1"); |
5757 | | |
5758 | | /* Validate parameters. */ |
5759 | | if ((ctx == NULL) || (der == NULL)) { |
5760 | | ret = 0; |
5761 | | } |
5762 | | /* Load DER encoded certificate into SSL context. */ |
5763 | | if ((ret == 1) && (wolfSSL_CTX_use_certificate_buffer(ctx, der, derSz, |
5764 | | WOLFSSL_FILETYPE_ASN1) != 1)) { |
5765 | | ret = 0; |
5766 | | } |
5767 | | |
5768 | | return ret; |
5769 | | } |
5770 | | |
5771 | | #if defined(WOLFSSL_KEY_GEN) && !defined(NO_RSA) |
5772 | | /* Load an RSA private key into SSL context. |
5773 | | * |
5774 | | * @param [in, out] ctx SSL context object. |
5775 | | * @param [in] rsa RSA private key. |
5776 | | * @return 1 on success. |
5777 | | * @return 0 on failure. |
5778 | | * @return BAD_FUNC_ARG when ctx or rsa is NULL. |
5779 | | * @return MEMORY_E when dynamic memory allocation fails. |
5780 | | */ |
5781 | | int wolfSSL_CTX_use_RSAPrivateKey(WOLFSSL_CTX* ctx, WOLFSSL_RSA* rsa) |
5782 | | { |
5783 | | int ret = 1; |
5784 | | int derSize = 0; |
5785 | | unsigned char* der = NULL; |
5786 | | unsigned char* p; |
5787 | | |
5788 | | WOLFSSL_ENTER("wolfSSL_CTX_use_RSAPrivateKey"); |
5789 | | |
5790 | | /* Validate parameters. */ |
5791 | | if ((ctx == NULL) || (rsa == NULL)) { |
5792 | | WOLFSSL_MSG("one or more inputs were NULL"); |
5793 | | ret = BAD_FUNC_ARG; |
5794 | | } |
5795 | | |
5796 | | /* Get DER encoding size. */ |
5797 | | if ((ret == 1) && ((derSize = wolfSSL_i2d_RSAPrivateKey(rsa, NULL)) <= 0)) { |
5798 | | ret = 0; |
5799 | | } |
5800 | | |
5801 | | if (ret == 1) { |
5802 | | /* Allocate memory to hold DER encoding.. */ |
5803 | | der = (unsigned char*)XMALLOC((size_t)derSize, NULL, |
5804 | | DYNAMIC_TYPE_TMP_BUFFER); |
5805 | | if (der == NULL) { |
5806 | | WOLFSSL_MSG("Malloc failure"); |
5807 | | ret = MEMORY_E; |
5808 | | } |
5809 | | } |
5810 | | |
5811 | | if (ret == 1) { |
5812 | | /* Pointer passed in is modified.. */ |
5813 | | p = der; |
5814 | | /* Encode the RSA key as DER into buffer and get size. */ |
5815 | | if ((derSize = wolfSSL_i2d_RSAPrivateKey(rsa, &p)) <= 0) { |
5816 | | WOLFSSL_MSG("wolfSSL_i2d_RSAPrivateKey() failure"); |
5817 | | ret = 0; |
5818 | | } |
5819 | | } |
5820 | | |
5821 | | if (ret == 1) { |
5822 | | /* Load DER encoded certificate into SSL context. */ |
5823 | | ret = wolfSSL_CTX_use_PrivateKey_buffer(ctx, der, derSize, |
5824 | | SSL_FILETYPE_ASN1); |
5825 | | if (ret != WOLFSSL_SUCCESS) { |
5826 | | WOLFSSL_MSG("wolfSSL_CTX_USE_PrivateKey_buffer() failure"); |
5827 | | ret = 0; |
5828 | | } |
5829 | | } |
5830 | | |
5831 | | /* Dispos of dynamically allocated data. */ |
5832 | | if (der != NULL) { |
5833 | | ForceZero(der, (word32)derSize); |
5834 | | } |
5835 | | XFREE(der, NULL, DYNAMIC_TYPE_TMP_BUFFER); |
5836 | | return ret; |
5837 | | } |
5838 | | #endif /* WOLFSSL_KEY_GEN && !NO_RSA */ |
5839 | | |
5840 | | #endif /* OPENSSL_ALL || WOLFSSL_ASIO || WOLFSSL_HAPROXY || WOLFSSL_QT */ |
5841 | | |
5842 | | #endif /* !NO_CERTS */ |
5843 | | |
5844 | | #ifdef OPENSSL_EXTRA |
5845 | | |
5846 | | /* Use the default paths to look for CA certificate. |
5847 | | * |
5848 | | * This is an OpenSSL compatibility layer function, but it doesn't mirror |
5849 | | * the exact functionality of its OpenSSL counterpart. We don't support the |
5850 | | * notion of an "OpenSSL directory". This function will attempt to load the |
5851 | | * environment variables SSL_CERT_DIR and SSL_CERT_FILE, if either are |
5852 | | * found, they will be loaded. Otherwise, it will act as a wrapper around |
5853 | | * our native wolfSSL_CTX_load_system_CA_certs function. This function does |
5854 | | * conform to OpenSSL's return value conventions. |
5855 | | * |
5856 | | * @param [in] ctx SSL context object. |
5857 | | * @return 1 on success. |
5858 | | * @return 0 on failure. |
5859 | | * @return WOLFSSL_FATAL_ERROR when using a filesystem is not supported. |
5860 | | */ |
5861 | | int wolfSSL_CTX_set_default_verify_paths(WOLFSSL_CTX* ctx) |
5862 | | { |
5863 | | int ret; |
5864 | | #if defined(XGETENV) && !defined(NO_GETENV) |
5865 | | char* certDir = NULL; |
5866 | | char* certFile = NULL; |
5867 | | word32 flags = 0; |
5868 | | #elif !defined(WOLFSSL_SYS_CA_CERTS) |
5869 | | (void)ctx; |
5870 | | #endif |
5871 | | |
5872 | | WOLFSSL_ENTER("wolfSSL_CTX_set_default_verify_paths"); |
5873 | | |
5874 | | #if defined(XGETENV) && !defined(NO_GETENV) |
5875 | | /* // NOLINTBEGIN(concurrency-mt-unsafe) */ |
5876 | | certDir = wc_strdup_ex(XGETENV("SSL_CERT_DIR"), DYNAMIC_TYPE_TMP_BUFFER); |
5877 | | certFile = wc_strdup_ex(XGETENV("SSL_CERT_FILE"), DYNAMIC_TYPE_TMP_BUFFER); |
5878 | | flags = WOLFSSL_LOAD_FLAG_PEM_CA_ONLY; |
5879 | | |
5880 | | if ((certDir != NULL) || (certFile != NULL)) { |
5881 | | if (certDir != NULL) { |
5882 | | /* We want to keep trying to load more CA certs even if one cert in |
5883 | | * the directory is bad and can't be used (e.g. if one is |
5884 | | * expired), so we use WOLFSSL_LOAD_FLAG_IGNORE_ERR. |
5885 | | */ |
5886 | | flags |= WOLFSSL_LOAD_FLAG_IGNORE_ERR; |
5887 | | } |
5888 | | |
5889 | | /* Load CA certificates from environment variable locations. */ |
5890 | | ret = wolfSSL_CTX_load_verify_locations_ex(ctx, certFile, certDir, |
5891 | | flags); |
5892 | | if (ret != 1) { |
5893 | | WOLFSSL_MSG_EX("Failed to load CA certs from SSL_CERT_FILE: %s" |
5894 | | " SSL_CERT_DIR: %s. Error: %d", certFile, |
5895 | | certDir, ret); |
5896 | | ret = 0; |
5897 | | } |
5898 | | } |
5899 | | /* // NOLINTEND(concurrency-mt-unsafe) */ |
5900 | | else |
5901 | | #endif |
5902 | | |
5903 | | { |
5904 | | #ifdef NO_FILESYSTEM |
5905 | | WOLFSSL_MSG("wolfSSL_CTX_set_default_verify_paths not supported" |
5906 | | " with NO_FILESYSTEM enabled"); |
5907 | | ret = WOLFSSL_FATAL_ERROR; |
5908 | | #elif defined(WOLFSSL_SYS_CA_CERTS) |
5909 | | /* Load the system CA certificates. */ |
5910 | | ret = wolfSSL_CTX_load_system_CA_certs(ctx); |
5911 | | if (ret == WC_NO_ERR_TRACE(WOLFSSL_BAD_PATH)) { |
5912 | | /* OpenSSL doesn't treat the lack of a system CA cert directory as a |
5913 | | * failure. We do the same here. |
5914 | | */ |
5915 | | ret = 1; |
5916 | | } |
5917 | | #else |
5918 | | /* No source available: SSL_CERT_DIR/SSL_CERT_FILE not set and |
5919 | | * WOLFSSL_SYS_CA_CERTS not compiled in. Returning success would be |
5920 | | * fail-open since no trust anchors were loaded. */ |
5921 | | WOLFSSL_MSG("wolfSSL_CTX_set_default_verify_paths: no CA source " |
5922 | | "available (build without WOLFSSL_SYS_CA_CERTS and no " |
5923 | | "SSL_CERT_DIR/SSL_CERT_FILE env)"); |
5924 | | ret = WOLFSSL_FAILURE; |
5925 | | #endif |
5926 | | } |
5927 | | |
5928 | | #if defined(XGETENV) && !defined(NO_GETENV) |
5929 | | XFREE(certFile, NULL, DYNAMIC_TYPE_TMP_BUFFER); |
5930 | | XFREE(certDir, NULL, DYNAMIC_TYPE_TMP_BUFFER); |
5931 | | #endif |
5932 | | WOLFSSL_LEAVE("wolfSSL_CTX_set_default_verify_paths", ret); |
5933 | | |
5934 | | return ret; |
5935 | | } |
5936 | | |
5937 | | #endif /* OPENSSL_EXTRA */ |
5938 | | |
5939 | | #ifndef NO_DH |
5940 | | |
5941 | | /* Set the temporary DH parameters against the SSL. |
5942 | | * |
5943 | | * @param [in, out] ssl SSL object. |
5944 | | * @param [in] p Buffer holding prime. |
5945 | | * @param [in] pSz Length of prime in bytes. |
5946 | | * @param [in] g Buffer holding generator. |
5947 | | * @param [in] gSz Length of generator in bytes. |
5948 | | * @return 1 on success. |
5949 | | * @return 0 on failure. |
5950 | | * @return DH_KEY_SIZE_E when the prime is too short or long. |
5951 | | * @return SIDE_ERROR when the SSL is for a client. |
5952 | | */ |
5953 | | static int wolfssl_set_tmp_dh(WOLFSSL* ssl, unsigned char* p, int pSz, |
5954 | | unsigned char* g, int gSz) |
5955 | 0 | { |
5956 | 0 | int ret = 1; |
5957 | | |
5958 | | /* Check the size of the prime meets the requirements of the SSL. */ |
5959 | 0 | if (((word16)pSz < ssl->options.minDhKeySz) || |
5960 | 0 | ((word16)pSz > ssl->options.maxDhKeySz)) { |
5961 | 0 | ret = DH_KEY_SIZE_E; |
5962 | 0 | } |
5963 | | /* Only able to set DH parameters on server. */ |
5964 | 0 | if ((ret == 1) && (ssl->options.side == WOLFSSL_CLIENT_END)) { |
5965 | 0 | ret = SIDE_ERROR; |
5966 | 0 | } |
5967 | |
|
5968 | 0 | if (ret == 1) { |
5969 | 0 | #if !defined(WOLFSSL_OLD_PRIME_CHECK) && !defined(HAVE_FIPS) && \ |
5970 | 0 | !defined(HAVE_SELFTEST) |
5971 | | /* New DH parameters not tested for validity. */ |
5972 | 0 | ssl->options.dhKeyTested = 0; |
5973 | | /* New DH parameters must be tested for validity before use. */ |
5974 | 0 | ssl->options.dhDoKeyTest = 1; |
5975 | 0 | #endif |
5976 | | |
5977 | | /* Dispose of old DH parameters if we own it. */ |
5978 | 0 | if (ssl->buffers.weOwnDH) { |
5979 | 0 | XFREE(ssl->buffers.serverDH_P.buffer, ssl->heap, |
5980 | 0 | DYNAMIC_TYPE_PUBLIC_KEY); |
5981 | 0 | XFREE(ssl->buffers.serverDH_G.buffer, ssl->heap, |
5982 | 0 | DYNAMIC_TYPE_PUBLIC_KEY); |
5983 | 0 | } |
5984 | | |
5985 | | /* Assign the buffers and lengths to SSL. */ |
5986 | 0 | ssl->buffers.serverDH_P.buffer = p; |
5987 | 0 | ssl->buffers.serverDH_G.buffer = g; |
5988 | 0 | ssl->buffers.serverDH_P.length = (unsigned int)pSz; |
5989 | 0 | ssl->buffers.serverDH_G.length = (unsigned int)gSz; |
5990 | | /* We own the buffers. */ |
5991 | 0 | ssl->buffers.weOwnDH = 1; |
5992 | | /* We have a DH parameters to use. */ |
5993 | 0 | ssl->options.haveDH = 1; |
5994 | 0 | } |
5995 | | |
5996 | | /* Allocate space for cipher suites. */ |
5997 | 0 | if ((ret == 1) && (AllocateSuites(ssl) != 0)) { |
5998 | 0 | ssl->buffers.serverDH_P.buffer = NULL; |
5999 | 0 | ssl->buffers.serverDH_G.buffer = NULL; |
6000 | 0 | ret = 0; |
6001 | 0 | } |
6002 | 0 | if (ret == 1) { |
6003 | | /* Reset the cipher suites based on having a DH parameters now. */ |
6004 | 0 | InitSuites(ssl->suites, ssl->version, SSL_KEY_SZ(ssl), |
6005 | 0 | WOLFSSL_HAVE_RSA, SSL_HAVE_PSK(ssl), ssl->options.haveDH, |
6006 | 0 | ssl->options.haveECDSAsig, ssl->options.haveECC, TRUE, |
6007 | 0 | ssl->options.haveStaticECC, |
6008 | 0 | ssl->options.useAnon, TRUE, |
6009 | 0 | TRUE, TRUE, TRUE, ssl->options.side); |
6010 | 0 | } |
6011 | |
|
6012 | 0 | return ret; |
6013 | 0 | } |
6014 | | |
6015 | | /* Set the temporary DH parameters against the SSL. |
6016 | | * |
6017 | | * @param [in, out] ssl SSL object. |
6018 | | * @param [in] p Buffer holding prime. |
6019 | | * @param [in] pSz Length of prime in bytes. |
6020 | | * @param [in] g Buffer holding generator. |
6021 | | * @param [in] gSz Length of generator in bytes. |
6022 | | * @return 1 on success. |
6023 | | * @return 0 on failure. |
6024 | | * @return DH_KEY_SIZE_E when the prime is too short or long. |
6025 | | * @return SIDE_ERROR when the SSL is for a client. |
6026 | | * @return MEMORY_E when dynamic memory allocation fails. |
6027 | | */ |
6028 | | int wolfSSL_SetTmpDH(WOLFSSL* ssl, const unsigned char* p, int pSz, |
6029 | | const unsigned char* g, int gSz) |
6030 | 0 | { |
6031 | 0 | int ret = 1; |
6032 | 0 | byte* pAlloc = NULL; |
6033 | 0 | byte* gAlloc = NULL; |
6034 | |
|
6035 | 0 | WOLFSSL_ENTER("wolfSSL_SetTmpDH"); |
6036 | | |
6037 | | /* Validate parameters. */ |
6038 | 0 | if ((ssl == NULL) || (p == NULL) || (g == NULL)) { |
6039 | 0 | ret = 0; |
6040 | 0 | } |
6041 | |
|
6042 | 0 | if (ret == 1) { |
6043 | | /* Allocate buffers for p and g to be assigned into SSL. */ |
6044 | 0 | pAlloc = (byte*)XMALLOC((size_t)pSz, ssl->heap, |
6045 | 0 | DYNAMIC_TYPE_PUBLIC_KEY); |
6046 | 0 | gAlloc = (byte*)XMALLOC((size_t)gSz, ssl->heap, |
6047 | 0 | DYNAMIC_TYPE_PUBLIC_KEY); |
6048 | 0 | if ((pAlloc == NULL) || (gAlloc == NULL)) { |
6049 | | /* Memory will be freed below in the (ret != 1) block */ |
6050 | 0 | ret = MEMORY_E; |
6051 | 0 | } |
6052 | 0 | } |
6053 | 0 | if (ret == 1) { |
6054 | | /* Copy p and g into allocated buffers. */ |
6055 | 0 | XMEMCPY(pAlloc, p, (size_t)pSz); |
6056 | 0 | XMEMCPY(gAlloc, g, (size_t)gSz); |
6057 | | /* Set the buffers into SSL. */ |
6058 | 0 | ret = wolfssl_set_tmp_dh(ssl, pAlloc, pSz, gAlloc, gSz); |
6059 | 0 | } |
6060 | |
|
6061 | 0 | if (ret != 1 && ssl != NULL) { |
6062 | | /* Free the allocated buffers if not assigned into SSL. */ |
6063 | 0 | XFREE(pAlloc, ssl->heap, DYNAMIC_TYPE_PUBLIC_KEY); |
6064 | 0 | XFREE(gAlloc, ssl->heap, DYNAMIC_TYPE_PUBLIC_KEY); |
6065 | 0 | } |
6066 | |
|
6067 | 0 | WOLFSSL_LEAVE("wolfSSL_SetTmpDH", ret); |
6068 | 0 | return ret; |
6069 | 0 | } |
6070 | | |
6071 | | #if !defined(WOLFSSL_OLD_PRIME_CHECK) && !defined(HAVE_FIPS) && \ |
6072 | | !defined(HAVE_SELFTEST) |
6073 | | /* Check the DH parameters is valid. |
6074 | | * |
6075 | | * @param [in] p Buffer holding prime. |
6076 | | * @param [in] pSz Length of prime in bytes. |
6077 | | * @param [in] g Buffer holding generator. |
6078 | | * @param [in] gSz Length of generator in bytes. |
6079 | | * @return 1 on success. |
6080 | | * @return DH_CHECK_PUB_E when p is not a prime. |
6081 | | * @return BAD_FUNC_ARG when p or g is NULL, or pSz or gSz is 0. |
6082 | | * @return MEMORY_E when dynamic memory allocation fails. |
6083 | | */ |
6084 | | static int wolfssl_check_dh_key(unsigned char* p, int pSz, unsigned char* g, |
6085 | | int gSz) |
6086 | 0 | { |
6087 | 0 | WC_RNG rng; |
6088 | 0 | int ret = 0; |
6089 | 0 | WC_DECLARE_VAR(checkKey, DhKey, 1, 0); |
6090 | |
|
6091 | 0 | WC_ALLOC_VAR_EX(checkKey, DhKey, 1, NULL, DYNAMIC_TYPE_DH, |
6092 | 0 | ret=MEMORY_E); |
6093 | | /* Initialize a new random number generator. */ |
6094 | 0 | if ((ret == 0) && ((ret = wc_InitRng(&rng)) == 0)) { |
6095 | | /* Initialize a DH object. */ |
6096 | 0 | if ((ret = wc_InitDhKey(checkKey)) == 0) { |
6097 | | /* Check DH parameters. */ |
6098 | 0 | ret = wc_DhSetCheckKey(checkKey, p, (word32)pSz, g, (word32)gSz, |
6099 | 0 | NULL, 0, 0, &rng); |
6100 | | /* Dispose of DH object. */ |
6101 | 0 | wc_FreeDhKey(checkKey); |
6102 | 0 | } |
6103 | | /* Dispose of random number generator. */ |
6104 | 0 | wc_FreeRng(&rng); |
6105 | 0 | } |
6106 | |
|
6107 | 0 | WC_FREE_VAR_EX(checkKey, NULL, DYNAMIC_TYPE_DH); |
6108 | | /* Convert wolfCrypt return code to 1 on success and ret on failure. */ |
6109 | 0 | return WC_TO_WS_RC(ret); |
6110 | 0 | } |
6111 | | #endif |
6112 | | |
6113 | | /* Set the temporary DH parameters against the SSL context. |
6114 | | * |
6115 | | * @param [in, out] ctx SSL context object. |
6116 | | * @param [in] p Buffer holding prime. |
6117 | | * @param [in] pSz Length of prime in bytes. |
6118 | | * @param [in] g Buffer holding generator. |
6119 | | * @param [in] gSz Length of generator in bytes. |
6120 | | * @return 1 on success. |
6121 | | * @return 0 on failure. |
6122 | | * @return DH_KEY_SIZE_E when the prime is too short or long. |
6123 | | * @return SIDE_ERROR when the SSL is for a client. |
6124 | | * @return BAD_FUNC_ARG when ctx, p or g is NULL. |
6125 | | * @return DH_CHECK_PUB_E when p is not a prime. |
6126 | | * @return MEMORY_E when dynamic memory allocation fails. |
6127 | | */ |
6128 | | static int wolfssl_ctx_set_tmp_dh(WOLFSSL_CTX* ctx, unsigned char* p, int pSz, |
6129 | | unsigned char* g, int gSz) |
6130 | 0 | { |
6131 | 0 | int ret = 1; |
6132 | |
|
6133 | 0 | WOLFSSL_ENTER("wolfSSL_CTX_SetTmpDH"); |
6134 | |
|
6135 | 0 | if ((ctx == NULL) || (p == NULL) || (g == NULL)) |
6136 | 0 | ret = BAD_FUNC_ARG; |
6137 | | |
6138 | | /* Check the size of the prime meets the requirements of the SSL context. */ |
6139 | 0 | if (ret == 1) { |
6140 | 0 | if (((word16)pSz < ctx->minDhKeySz) || ((word16)pSz > ctx->maxDhKeySz)) { |
6141 | 0 | ret = DH_KEY_SIZE_E; |
6142 | 0 | } |
6143 | 0 | } |
6144 | |
|
6145 | 0 | #if !defined(WOLFSSL_OLD_PRIME_CHECK) && !defined(HAVE_FIPS) && \ |
6146 | 0 | !defined(HAVE_SELFTEST) |
6147 | 0 | if (ret == 1) { |
6148 | | /* Test DH parameters for validity. */ |
6149 | 0 | ret = wolfssl_check_dh_key(p, pSz, g, gSz); |
6150 | | /* Record as whether tested based on result of validity test. */ |
6151 | 0 | ctx->dhKeyTested = (ret == 1); |
6152 | 0 | } |
6153 | 0 | #endif |
6154 | |
|
6155 | 0 | if (ret == 1) { |
6156 | | /* Dispose of old DH parameters. */ |
6157 | 0 | XFREE(ctx->serverDH_P.buffer, ctx->heap, DYNAMIC_TYPE_PUBLIC_KEY); |
6158 | 0 | XFREE(ctx->serverDH_G.buffer, ctx->heap, DYNAMIC_TYPE_PUBLIC_KEY); |
6159 | | /* Assign the buffers and lengths to SSL context. */ |
6160 | 0 | ctx->serverDH_P.buffer = p; |
6161 | 0 | ctx->serverDH_G.buffer = g; |
6162 | 0 | ctx->serverDH_P.length = (unsigned int)pSz; |
6163 | 0 | ctx->serverDH_G.length = (unsigned int)gSz; |
6164 | | /* We have a DH parameters to use. */ |
6165 | 0 | ctx->haveDH = 1; |
6166 | 0 | } |
6167 | |
|
6168 | 0 | WOLFSSL_LEAVE("wolfSSL_CTX_SetTmpDH", 0); |
6169 | 0 | return ret; |
6170 | 0 | } |
6171 | | |
6172 | | /* Set the temporary DH parameters against the SSL context. |
6173 | | * |
6174 | | * @param [in, out] ctx SSL context object. |
6175 | | * @param [in] p Buffer holding prime. |
6176 | | * @param [in] pSz Length of prime in bytes. |
6177 | | * @param [in] g Buffer holding generator. |
6178 | | * @param [in] gSz Length of generator in bytes. |
6179 | | * @return 1 on success. |
6180 | | * @return 0 on failure. |
6181 | | * @return DH_KEY_SIZE_E when the prime is too short or long. |
6182 | | * @return SIDE_ERROR when the SSL is for a client. |
6183 | | * @return BAD_FUNC_ARG when ctx, p or g is NULL. |
6184 | | * @return DH_CHECK_PUB_E when p is not a prime. |
6185 | | */ |
6186 | | int wolfSSL_CTX_SetTmpDH(WOLFSSL_CTX* ctx, const unsigned char* p, int pSz, |
6187 | | const unsigned char* g, int gSz) |
6188 | 0 | { |
6189 | 0 | int ret = 1; |
6190 | 0 | byte* pAlloc = NULL; |
6191 | 0 | byte* gAlloc = NULL; |
6192 | | |
6193 | | /* Validate parameters. */ |
6194 | 0 | if ((ctx == NULL) || (p == NULL) || (g == NULL)) { |
6195 | 0 | ret = BAD_FUNC_ARG; |
6196 | 0 | } |
6197 | |
|
6198 | 0 | if (ret == 1) { |
6199 | | /* Allocate buffers for p and g to be assigned into SSL context. */ |
6200 | 0 | pAlloc = (byte*)XMALLOC((size_t)pSz, ctx->heap, |
6201 | 0 | DYNAMIC_TYPE_PUBLIC_KEY); |
6202 | 0 | gAlloc = (byte*)XMALLOC((size_t)gSz, ctx->heap, |
6203 | 0 | DYNAMIC_TYPE_PUBLIC_KEY); |
6204 | 0 | if ((pAlloc == NULL) || (gAlloc == NULL)) { |
6205 | 0 | ret = MEMORY_E; |
6206 | 0 | } |
6207 | 0 | } |
6208 | |
|
6209 | 0 | if (ret == 1) { |
6210 | | /* Copy p and g into allocated buffers. */ |
6211 | 0 | XMEMCPY(pAlloc, p, (size_t)pSz); |
6212 | 0 | XMEMCPY(gAlloc, g, (size_t)gSz); |
6213 | | /* Set the buffers into SSL context. */ |
6214 | 0 | ret = wolfssl_ctx_set_tmp_dh(ctx, pAlloc, pSz, gAlloc, gSz); |
6215 | 0 | } |
6216 | |
|
6217 | 0 | if ((ret != 1) && (ctx != NULL)) { |
6218 | | /* Free the allocated buffers if not assigned into SSL context. */ |
6219 | 0 | XFREE(pAlloc, ctx->heap, DYNAMIC_TYPE_PUBLIC_KEY); |
6220 | 0 | XFREE(gAlloc, ctx->heap, DYNAMIC_TYPE_PUBLIC_KEY); |
6221 | 0 | } |
6222 | 0 | return ret; |
6223 | 0 | } |
6224 | | |
6225 | | #ifdef OPENSSL_EXTRA |
6226 | | /* Set the temporary DH parameters against the SSL. |
6227 | | * |
6228 | | * @param [in, out] ssl SSL object. |
6229 | | * @param [in] dh DH object. |
6230 | | * @return 1 on success. |
6231 | | * @return 0 on failure. |
6232 | | * @return WOLFSSL_FATAL_ERROR on failure. |
6233 | | * @return BAD_FUNC_ARG when ssl or dh is NULL. |
6234 | | * @return DH_KEY_SIZE_E when the prime is too short or long. |
6235 | | * @return SIDE_ERROR when the SSL is for a client. |
6236 | | */ |
6237 | | long wolfSSL_set_tmp_dh(WOLFSSL *ssl, WOLFSSL_DH *dh) |
6238 | | { |
6239 | | int ret = 1; |
6240 | | byte* p = NULL; |
6241 | | byte* g = NULL; |
6242 | | int pSz = 0; |
6243 | | int gSz = 0; |
6244 | | |
6245 | | WOLFSSL_ENTER("wolfSSL_set_tmp_dh"); |
6246 | | |
6247 | | /* Validate parameters. */ |
6248 | | if ((ssl == NULL) || (dh == NULL)) { |
6249 | | ret = BAD_FUNC_ARG; |
6250 | | } |
6251 | | |
6252 | | if (ret == 1) { |
6253 | | /* Get sizes of p and g. */ |
6254 | | pSz = wolfSSL_BN_bn2bin(dh->p, NULL); |
6255 | | gSz = wolfSSL_BN_bn2bin(dh->g, NULL); |
6256 | | /* Validate p and g size. */ |
6257 | | if ((pSz <= 0) || (gSz <= 0)) { |
6258 | | ret = WOLFSSL_FATAL_ERROR; |
6259 | | } |
6260 | | } |
6261 | | |
6262 | | if (ret == 1) { |
6263 | | /* Allocate buffers for p and g to be assigned into SSL. */ |
6264 | | p = (byte*)XMALLOC((size_t)pSz, ssl->heap, DYNAMIC_TYPE_PUBLIC_KEY); |
6265 | | g = (byte*)XMALLOC((size_t)gSz, ssl->heap, DYNAMIC_TYPE_PUBLIC_KEY); |
6266 | | if ((p == NULL) || (g == NULL)) { |
6267 | | ret = MEMORY_E; |
6268 | | } |
6269 | | } |
6270 | | if (ret == 1) { |
6271 | | /* Encode p and g and get sizes. */ |
6272 | | pSz = wolfSSL_BN_bn2bin(dh->p, p); |
6273 | | gSz = wolfSSL_BN_bn2bin(dh->g, g); |
6274 | | /* Check encoding worked. */ |
6275 | | if ((pSz <= 0) || (gSz <= 0)) { |
6276 | | ret = WOLFSSL_FATAL_ERROR; |
6277 | | } |
6278 | | } |
6279 | | if (ret == 1) { |
6280 | | /* Set the buffers into SSL. */ |
6281 | | ret = wolfssl_set_tmp_dh(ssl, p, pSz, g, gSz); |
6282 | | } |
6283 | | |
6284 | | if ((ret != 1) && (ssl != NULL)) { |
6285 | | /* Free the allocated buffers if not assigned into SSL. */ |
6286 | | XFREE(p, ssl->heap, DYNAMIC_TYPE_PUBLIC_KEY); |
6287 | | XFREE(g, ssl->heap, DYNAMIC_TYPE_PUBLIC_KEY); |
6288 | | } |
6289 | | return ret; |
6290 | | } |
6291 | | |
6292 | | /* Set the temporary DH parameters object against the SSL context. |
6293 | | * |
6294 | | * @param [in, out] ctx SSL context object. |
6295 | | * @param [in] dh DH object. |
6296 | | * @return 1 on success. |
6297 | | * @return 0 on failure. |
6298 | | * @return DH_KEY_SIZE_E when the prime is too short or long. |
6299 | | * @return SIDE_ERROR when the SSL is for a client. |
6300 | | * @return BAD_FUNC_ARG when ctx, p or g is NULL. |
6301 | | * @return DH_CHECK_PUB_E when p is not a prime. |
6302 | | */ |
6303 | | long wolfSSL_CTX_set_tmp_dh(WOLFSSL_CTX* ctx, WOLFSSL_DH* dh) |
6304 | | { |
6305 | | int ret = 1; |
6306 | | int pSz = 0; |
6307 | | int gSz = 0; |
6308 | | byte* p = NULL; |
6309 | | byte* g = NULL; |
6310 | | |
6311 | | WOLFSSL_ENTER("wolfSSL_CTX_set_tmp_dh"); |
6312 | | |
6313 | | /* Validate parameters. */ |
6314 | | if ((ctx == NULL) || (dh == NULL)) { |
6315 | | ret = BAD_FUNC_ARG; |
6316 | | } |
6317 | | |
6318 | | if (ret == 1) { |
6319 | | /* Get sizes of p and g. */ |
6320 | | pSz = wolfSSL_BN_bn2bin(dh->p, NULL); |
6321 | | gSz = wolfSSL_BN_bn2bin(dh->g, NULL); |
6322 | | /* Validate p and g size. */ |
6323 | | if ((pSz <= 0) || (gSz <= 0)) { |
6324 | | ret = WOLFSSL_FATAL_ERROR; |
6325 | | } |
6326 | | } |
6327 | | |
6328 | | if (ret == 1) { |
6329 | | /* Allocate buffers for p and g to be assigned into SSL. */ |
6330 | | p = (byte*)XMALLOC((size_t)pSz, ctx->heap, DYNAMIC_TYPE_PUBLIC_KEY); |
6331 | | g = (byte*)XMALLOC((size_t)gSz, ctx->heap, DYNAMIC_TYPE_PUBLIC_KEY); |
6332 | | if ((p == NULL) || (g == NULL)) { |
6333 | | ret = MEMORY_E; |
6334 | | } |
6335 | | } |
6336 | | |
6337 | | if (ret == 1) { |
6338 | | /* Encode p and g and get sizes. */ |
6339 | | pSz = wolfSSL_BN_bn2bin(dh->p, p); |
6340 | | gSz = wolfSSL_BN_bn2bin(dh->g, g); |
6341 | | /* Check encoding worked. */ |
6342 | | if ((pSz <= 0) || (gSz <= 0)) { |
6343 | | ret = WOLFSSL_FATAL_ERROR; |
6344 | | } |
6345 | | } |
6346 | | if (ret == 1) { |
6347 | | /* Set the buffers into SSL context. */ |
6348 | | ret = wolfssl_ctx_set_tmp_dh(ctx, p, pSz, g, gSz); |
6349 | | } |
6350 | | |
6351 | | if ((ret != 1) && (ctx != NULL)) { |
6352 | | /* Free the allocated buffers if not assigned into SSL. */ |
6353 | | XFREE(p, ctx->heap, DYNAMIC_TYPE_PUBLIC_KEY); |
6354 | | XFREE(g, ctx->heap, DYNAMIC_TYPE_PUBLIC_KEY); |
6355 | | } |
6356 | | return ret; |
6357 | | } |
6358 | | |
6359 | | #endif /* OPENSSL_EXTRA */ |
6360 | | |
6361 | | #ifndef NO_CERTS |
6362 | | |
6363 | | /* Set the temporary DH parameters against the SSL context or SSL. |
6364 | | * |
6365 | | * @param [in, out] ctx SSL context object. |
6366 | | * @param [in, out] ssl SSL object. |
6367 | | * @param [in] buf Buffer holding encoded DH parameters. |
6368 | | * @param [in] sz Size of encoded DH parameters. |
6369 | | * @param [in] format Format of data: |
6370 | | * WOLFSSL_FILETYPE_PEM or WOLFSSL_FILETYPE_ASN1. |
6371 | | * @return 1 on success. |
6372 | | * @return 0 on failure. |
6373 | | * @return BAD_FUNC_ARG when ctx and ssl NULL or buf is NULL. |
6374 | | * @return NOT_COMPLED_IN when format is PEM but PEM is not supported. |
6375 | | * @return WOLFSSL_BAD_FILETYPE if format is not supported. |
6376 | | */ |
6377 | | static int ws_ctx_ssl_set_tmp_dh(WOLFSSL_CTX* ctx, WOLFSSL* ssl, |
6378 | | const unsigned char* buf, long sz, int format) |
6379 | 0 | { |
6380 | 0 | DerBuffer* der = NULL; |
6381 | 0 | int res = 1; |
6382 | 0 | int ret; |
6383 | | /* p and g size to allocate set to maximum valid size. */ |
6384 | 0 | word32 pSz = MAX_DH_SIZE; |
6385 | 0 | word32 gSz = MAX_DH_SIZE; |
6386 | 0 | byte* p = NULL; |
6387 | 0 | byte* g = NULL; |
6388 | 0 | void* heap = WOLFSSL_HEAP(ctx, ssl); |
6389 | | |
6390 | | /* Validate parameters. */ |
6391 | 0 | if (((ctx == NULL) && (ssl == NULL)) || (buf == NULL)) { |
6392 | 0 | res = BAD_FUNC_ARG; |
6393 | 0 | } |
6394 | | /* Check format is supported. */ |
6395 | 0 | if ((res == 1) && (format != WOLFSSL_FILETYPE_ASN1)) { |
6396 | 0 | if (format != WOLFSSL_FILETYPE_PEM) { |
6397 | 0 | res = WOLFSSL_BAD_FILETYPE; |
6398 | 0 | } |
6399 | | #ifndef WOLFSSL_PEM_TO_DER |
6400 | | else { |
6401 | | res = NOT_COMPILED_IN; |
6402 | | } |
6403 | | #endif |
6404 | 0 | } |
6405 | | |
6406 | | /* PemToDer allocates its own DER buffer. */ |
6407 | 0 | if ((res == 1) && (format != WOLFSSL_FILETYPE_PEM)) { |
6408 | | /* Create a DER buffer and copy in the encoded DH parameters. */ |
6409 | 0 | ret = AllocDer(&der, (word32)sz, DH_PARAM_TYPE, heap); |
6410 | 0 | if (ret == 0) { |
6411 | 0 | XMEMCPY(der->buffer, buf, (word32)sz); |
6412 | 0 | } |
6413 | 0 | else { |
6414 | 0 | res = ret; |
6415 | 0 | } |
6416 | 0 | } |
6417 | |
|
6418 | 0 | if (res == 1) { |
6419 | | /* Allocate enough memory to p and g to support valid use cases. */ |
6420 | 0 | p = (byte*)XMALLOC(pSz, heap, DYNAMIC_TYPE_PUBLIC_KEY); |
6421 | 0 | g = (byte*)XMALLOC(gSz, heap, DYNAMIC_TYPE_PUBLIC_KEY); |
6422 | 0 | if ((p == NULL) || (g == NULL)) { |
6423 | 0 | res = MEMORY_E; |
6424 | 0 | } |
6425 | 0 | } |
6426 | |
|
6427 | 0 | #ifdef WOLFSSL_PEM_TO_DER |
6428 | 0 | if ((res == 1) && (format == WOLFSSL_FILETYPE_PEM)) { |
6429 | | /* Convert from PEM to DER. */ |
6430 | | /* Try converting DH parameters from PEM to DER. */ |
6431 | 0 | ret = PemToDer(buf, sz, DH_PARAM_TYPE, &der, heap, NULL, NULL); |
6432 | 0 | if (ret < 0) { |
6433 | | /* Otherwise, try converting X9.43 format DH parameters. */ |
6434 | 0 | ret = PemToDer(buf, sz, X942_PARAM_TYPE, &der, heap, NULL, NULL); |
6435 | 0 | } |
6436 | | #if defined(WOLFSSL_WPAS) && !defined(NO_DSA) |
6437 | | if (ret < 0) { |
6438 | | /* Otherwise, try converting DSA parameters. */ |
6439 | | ret = PemToDer(buf, sz, DSA_PARAM_TYPE, &der, heap, NULL, NULL); |
6440 | | } |
6441 | | #endif /* WOLFSSL_WPAS && !NO_DSA */ |
6442 | 0 | if (ret < 0) { |
6443 | | /* Return error from conversion. */ |
6444 | 0 | res = ret; |
6445 | 0 | } |
6446 | 0 | } |
6447 | 0 | #endif /* WOLFSSL_PEM_TO_DER */ |
6448 | |
|
6449 | 0 | if (res == 1) { |
6450 | | /* Get the p and g from the DER encoded parameters. */ |
6451 | 0 | if (wc_DhParamsLoad(der->buffer, der->length, p, &pSz, g, &gSz) < 0) { |
6452 | 0 | res = WOLFSSL_BAD_FILETYPE; |
6453 | 0 | } |
6454 | 0 | else if (ssl != NULL) { |
6455 | | /* Set p and g into SSL. */ |
6456 | 0 | res = wolfssl_set_tmp_dh(ssl, p, (int)pSz, g, (int)gSz); |
6457 | 0 | } |
6458 | 0 | else { |
6459 | | /* Set p and g into SSL context. */ |
6460 | 0 | res = wolfssl_ctx_set_tmp_dh(ctx, p, (int)pSz, g, (int)gSz); |
6461 | 0 | } |
6462 | 0 | } |
6463 | | |
6464 | | /* Dispose of the DER buffer. */ |
6465 | 0 | FreeDer(&der); |
6466 | 0 | if (res != 1) { |
6467 | | /* Free the allocated buffers if not assigned into SSL or context. */ |
6468 | 0 | XFREE(p, heap, DYNAMIC_TYPE_PUBLIC_KEY); |
6469 | 0 | XFREE(g, heap, DYNAMIC_TYPE_PUBLIC_KEY); |
6470 | 0 | } |
6471 | 0 | return res; |
6472 | 0 | } |
6473 | | |
6474 | | |
6475 | | /* Set the temporary DH parameters against the SSL. |
6476 | | * |
6477 | | * @param [in, out] ssl SSL object. |
6478 | | * @param [in] buf Buffer holding encoded DH parameters. |
6479 | | * @param [in] sz Size of encoded DH parameters. |
6480 | | * @param [in] format Format of data: |
6481 | | * WOLFSSL_FILETYPE_PEM or WOLFSSL_FILETYPE_ASN1. |
6482 | | * @return 1 on success. |
6483 | | * @return BAD_FUNC_ARG when ssl or buf is NULL. |
6484 | | * @return NOT_COMPLED_IN when format is PEM but PEM is not supported. |
6485 | | * @return WOLFSSL_BAD_FILETYPE if format is not supported. |
6486 | | */ |
6487 | | int wolfSSL_SetTmpDH_buffer(WOLFSSL* ssl, const unsigned char* buf, long sz, |
6488 | | int format) |
6489 | 0 | { |
6490 | 0 | return ws_ctx_ssl_set_tmp_dh(NULL, ssl, buf, sz, format); |
6491 | 0 | } |
6492 | | |
6493 | | |
6494 | | /* Set the temporary DH parameters against the SSL context. |
6495 | | * |
6496 | | * @param [in, out] ctx SSL context object. |
6497 | | * @param [in] buf Buffer holding encoded DH parameters. |
6498 | | * @param [in] sz Size of encoded DH parameters. |
6499 | | * @param [in] format Format of data: |
6500 | | * WOLFSSL_FILETYPE_PEM or WOLFSSL_FILETYPE_ASN1. |
6501 | | * @return 1 on success. |
6502 | | * @return BAD_FUNC_ARG when ctx or buf is NULL. |
6503 | | * @return NOT_COMPLED_IN when format is PEM but PEM is not supported. |
6504 | | * @return WOLFSSL_BAD_FILETYPE if format is not supported. |
6505 | | */ |
6506 | | int wolfSSL_CTX_SetTmpDH_buffer(WOLFSSL_CTX* ctx, const unsigned char* buf, |
6507 | | long sz, int format) |
6508 | 0 | { |
6509 | 0 | return ws_ctx_ssl_set_tmp_dh(ctx, NULL, buf, sz, format); |
6510 | 0 | } |
6511 | | |
6512 | | #ifndef NO_FILESYSTEM |
6513 | | |
6514 | | /* Set the temporary DH parameters file against the SSL context or SSL. |
6515 | | * |
6516 | | * @param [in, out] ctx SSL context object. |
6517 | | * @param [in, out] ssl SSL object. |
6518 | | * @param [in] fname Name of file to load. |
6519 | | * @param [in] format Format of data: |
6520 | | * WOLFSSL_FILETYPE_PEM or WOLFSSL_FILETYPE_ASN1. |
6521 | | * @return 1 on success. |
6522 | | * @return BAD_FUNC_ARG when ctx and ssl NULL or fname is NULL. |
6523 | | * @return NOT_COMPLED_IN when format is PEM but PEM is not supported. |
6524 | | * @return WOLFSSL_BAD_FILETYPE if format is not supported. |
6525 | | */ |
6526 | | static int ws_ctx_ssl_set_tmp_dh_file(WOLFSSL_CTX* ctx, WOLFSSL* ssl, |
6527 | | const char* fname, int format) |
6528 | 0 | { |
6529 | 0 | int res = 1; |
6530 | 0 | int ret; |
6531 | 0 | #ifndef WOLFSSL_SMALL_STACK |
6532 | 0 | byte stackBuffer[FILE_BUFFER_SIZE]; |
6533 | 0 | #endif |
6534 | 0 | StaticBuffer dhFile; |
6535 | 0 | long sz = 0; |
6536 | 0 | void* heap = WOLFSSL_HEAP(ctx, ssl); |
6537 | | |
6538 | | /* Setup buffer to hold file contents. */ |
6539 | | #ifdef WOLFSSL_SMALL_STACK |
6540 | | static_buffer_init(&dhFile); |
6541 | | #else |
6542 | 0 | static_buffer_init(&dhFile, stackBuffer, FILE_BUFFER_SIZE); |
6543 | 0 | #endif |
6544 | | |
6545 | | /* Validate parameters. */ |
6546 | 0 | if (((ctx == NULL) && (ssl == NULL)) || (fname == NULL)) { |
6547 | 0 | res = BAD_FUNC_ARG; |
6548 | 0 | } |
6549 | |
|
6550 | 0 | if (res == 1) { |
6551 | | /* Read file into static buffer. */ |
6552 | 0 | ret = wolfssl_read_file_static(fname, &dhFile, heap, DYNAMIC_TYPE_FILE, |
6553 | 0 | &sz); |
6554 | 0 | if (ret != 0) { |
6555 | 0 | res = ret; |
6556 | 0 | } |
6557 | 0 | } |
6558 | 0 | if (res == 1) { |
6559 | 0 | if (ssl != NULL) { |
6560 | | /* Set encoded DH parameters into SSL. */ |
6561 | 0 | res = wolfSSL_SetTmpDH_buffer(ssl, dhFile.buffer, sz, format); |
6562 | 0 | } |
6563 | 0 | else { |
6564 | | /* Set encoded DH parameters into SSL context. */ |
6565 | 0 | res = wolfSSL_CTX_SetTmpDH_buffer(ctx, dhFile.buffer, sz, format); |
6566 | 0 | } |
6567 | 0 | } |
6568 | | |
6569 | | /* Dispose of any dynamically allocated data. */ |
6570 | 0 | static_buffer_free(&dhFile, heap, DYNAMIC_TYPE_FILE); |
6571 | 0 | return res; |
6572 | 0 | } |
6573 | | |
6574 | | /* Set the temporary DH parameters file against the SSL. |
6575 | | * |
6576 | | * @param [in, out] ssl SSL object. |
6577 | | * @param [in] fname Name of file to load. |
6578 | | * @param [in] format Format of data: |
6579 | | * WOLFSSL_FILETYPE_PEM or WOLFSSL_FILETYPE_ASN1. |
6580 | | * @return 1 on success. |
6581 | | * @return BAD_FUNC_ARG when ssl or fname is NULL. |
6582 | | * @return NOT_COMPLED_IN when format is PEM but PEM is not supported. |
6583 | | * @return WOLFSSL_BAD_FILETYPE if format is not supported. |
6584 | | */ |
6585 | | int wolfSSL_SetTmpDH_file(WOLFSSL* ssl, const char* fname, int format) |
6586 | 0 | { |
6587 | 0 | return ws_ctx_ssl_set_tmp_dh_file(NULL, ssl, fname, format); |
6588 | 0 | } |
6589 | | |
6590 | | |
6591 | | /* Set the temporary DH parameters file against the SSL context. |
6592 | | * |
6593 | | * @param [in, out] ctx SSL context object. |
6594 | | * @param [in] fname Name of file to load. |
6595 | | * @param [in] format Format of data: |
6596 | | * WOLFSSL_FILETYPE_PEM or WOLFSSL_FILETYPE_ASN1. |
6597 | | * @return 1 on success. |
6598 | | * @return BAD_FUNC_ARG when ctx or fname is NULL. |
6599 | | * @return NOT_COMPLED_IN when format is PEM but PEM is not supported. |
6600 | | * @return WOLFSSL_BAD_FILETYPE if format is not supported. |
6601 | | */ |
6602 | | int wolfSSL_CTX_SetTmpDH_file(WOLFSSL_CTX* ctx, const char* fname, int format) |
6603 | 0 | { |
6604 | | return ws_ctx_ssl_set_tmp_dh_file(ctx, NULL, fname, format); |
6605 | 0 | } |
6606 | | |
6607 | | #endif /* NO_FILESYSTEM */ |
6608 | | |
6609 | | #endif /* NO_CERTS */ |
6610 | | |
6611 | | #endif /* !NO_DH */ |
6612 | | |
6613 | | #endif /* !WOLFSSL_SSL_LOAD_INCLUDED */ |
6614 | | |