/src/wolfssl/src/ssl_api_dtls.c
Line | Count | Source |
1 | | /* ssl_api_dtls.c |
2 | | * |
3 | | * Copyright (C) 2006-2026 wolfSSL Inc. |
4 | | * |
5 | | * This file is part of wolfSSL. |
6 | | * |
7 | | * wolfSSL is free software; you can redistribute it and/or modify |
8 | | * it under the terms of the GNU General Public License as published by |
9 | | * the Free Software Foundation; either version 3 of the License, or |
10 | | * (at your option) any later version. |
11 | | * |
12 | | * wolfSSL is distributed in the hope that it will be useful, |
13 | | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
14 | | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
15 | | * GNU General Public License for more details. |
16 | | * |
17 | | * You should have received a copy of the GNU General Public License |
18 | | * along with this program; if not, write to the Free Software |
19 | | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA |
20 | | */ |
21 | | |
22 | | #include <wolfssl/wolfcrypt/libwolfssl_sources.h> |
23 | | |
24 | | #if !defined(WOLFSSL_SSL_API_DTLS_INCLUDED) |
25 | | #ifndef WOLFSSL_IGNORE_FILE_WARN |
26 | | #warning ssl_api_dtls.c does not need to be compiled separately from ssl.c |
27 | | #endif |
28 | | #else |
29 | | |
30 | | #ifndef WOLFCRYPT_ONLY |
31 | | |
32 | | #ifdef WOLFSSL_DTLS |
33 | | /* Set the file descriptor for an already connected DTLS object. |
34 | | * |
35 | | * @param [in] ssl SSL/TLS object. |
36 | | * @param [in] fd Connected socket file descriptor. |
37 | | * @return WOLFSSL_SUCCESS on success. |
38 | | * @return BAD_FUNC_ARG when ssl is NULL. |
39 | | */ |
40 | | int wolfSSL_set_dtls_fd_connected(WOLFSSL* ssl, int fd) |
41 | | { |
42 | | int ret; |
43 | | |
44 | | WOLFSSL_ENTER("wolfSSL_set_dtls_fd_connected"); |
45 | | |
46 | | if (ssl == NULL) { |
47 | | return BAD_FUNC_ARG; |
48 | | } |
49 | | |
50 | | ret = wolfSSL_set_fd(ssl, fd); |
51 | | if (ret == WOLFSSL_SUCCESS) |
52 | | ssl->buffers.dtlsCtx.connected = 1; |
53 | | |
54 | | return ret; |
55 | | } |
56 | | #endif |
57 | | |
58 | | /* Determine whether the object is configured for DTLS. |
59 | | * |
60 | | * @param [in] ssl SSL/TLS object. |
61 | | * @return 1 when using DTLS. |
62 | | * @return 0 otherwise, or when ssl is NULL. |
63 | | */ |
64 | | int wolfSSL_dtls(WOLFSSL* ssl) |
65 | 0 | { |
66 | 0 | int dtlsOpt = 0; |
67 | 0 | if (ssl) |
68 | 0 | dtlsOpt = ssl->options.dtls; |
69 | 0 | return dtlsOpt; |
70 | 0 | } |
71 | | |
72 | | #ifndef WOLFSSL_LEANPSK |
73 | | #if defined(WOLFSSL_DTLS) && defined(XINET_PTON) && \ |
74 | | !defined(WOLFSSL_NO_SOCK) && defined(HAVE_SOCKADDR) |
75 | | /* Create a DTLS peer address from a port and IPv4 address string. |
76 | | * |
77 | | * The returned object must be freed with wolfSSL_dtls_free_peer(). |
78 | | * |
79 | | * @param [in] port Port number. |
80 | | * @param [in] ip Dotted-decimal IPv4 address string. |
81 | | * @return Newly allocated peer address on success. |
82 | | * @return NULL on allocation error or when the address is invalid. |
83 | | */ |
84 | | void* wolfSSL_dtls_create_peer(int port, char* ip) |
85 | | { |
86 | | SOCKADDR_IN *addr; |
87 | | addr = (SOCKADDR_IN*)XMALLOC(sizeof(*addr), NULL, |
88 | | DYNAMIC_TYPE_SOCKADDR); |
89 | | if (addr == NULL) { |
90 | | return NULL; |
91 | | } |
92 | | |
93 | | addr->sin_family = AF_INET; |
94 | | addr->sin_port = XHTONS((word16)port); |
95 | | if (XINET_PTON(AF_INET, ip, &addr->sin_addr) < 1) { |
96 | | XFREE(addr, NULL, DYNAMIC_TYPE_SOCKADDR); |
97 | | return NULL; |
98 | | } |
99 | | |
100 | | return addr; |
101 | | } |
102 | | |
103 | | /* Free a DTLS peer address created with wolfSSL_dtls_create_peer(). |
104 | | * |
105 | | * @param [in] addr Peer address to free. |
106 | | * @return WOLFSSL_SUCCESS always. |
107 | | */ |
108 | | int wolfSSL_dtls_free_peer(void* addr) |
109 | | { |
110 | | XFREE(addr, NULL, DYNAMIC_TYPE_SOCKADDR); |
111 | | return WOLFSSL_SUCCESS; |
112 | | } |
113 | | #endif |
114 | | |
115 | | #ifdef WOLFSSL_DTLS |
116 | | /* Store a socket address into a socket address holder, resizing as needed. |
117 | | * |
118 | | * A NULL or zero-length peer frees the holder's buffer. An address that fits |
119 | | * the buffer already there is copied over it rather than reallocated: |
120 | | * EmbedSendTo reads peer.sa without taking peerLock, so freeing it on every |
121 | | * update would widen that race rather than leave it as it is. |
122 | | * |
123 | | * The record layer promotes a pending peer through this while already holding |
124 | | * peerLock, so it takes no lock of its own. |
125 | | * |
126 | | * @param [in, out] sockAddr Socket address holder. |
127 | | * @param [in] peer Socket address data, may be NULL to free. |
128 | | * @param [in] peerSz Length of socket address data in bytes. |
129 | | * @param [in] heap Heap hint for dynamic memory allocation. |
130 | | * @return WOLFSSL_SUCCESS on success. |
131 | | * @return WOLFSSL_FAILURE on allocation error. |
132 | | */ |
133 | | int wolfssl_local_SockAddrSet(WOLFSSL_SOCKADDR* sockAddr, void* peer, |
134 | | unsigned int peerSz, void* heap) |
135 | | { |
136 | | if (peer == NULL || peerSz == 0) { |
137 | | if (sockAddr->sa != NULL) |
138 | | XFREE(sockAddr->sa, heap, DYNAMIC_TYPE_SOCKADDR); |
139 | | sockAddr->sa = NULL; |
140 | | sockAddr->sz = 0; |
141 | | sockAddr->bufSz = 0; |
142 | | return WOLFSSL_SUCCESS; |
143 | | } |
144 | | |
145 | | if (peerSz > sockAddr->bufSz) { |
146 | | if (sockAddr->sa != NULL) |
147 | | XFREE(sockAddr->sa, heap, DYNAMIC_TYPE_SOCKADDR); |
148 | | sockAddr->sa = |
149 | | (void*)XMALLOC(peerSz, heap, DYNAMIC_TYPE_SOCKADDR); |
150 | | if (sockAddr->sa == NULL) { |
151 | | sockAddr->sz = 0; |
152 | | sockAddr->bufSz = 0; |
153 | | return WOLFSSL_FAILURE; |
154 | | } |
155 | | sockAddr->bufSz = peerSz; |
156 | | } |
157 | | XMEMCPY(sockAddr->sa, peer, peerSz); |
158 | | sockAddr->sz = peerSz; |
159 | | return WOLFSSL_SUCCESS; |
160 | | } |
161 | | #endif |
162 | | |
163 | | /* Set the DTLS peer address on the object. |
164 | | * |
165 | | * @param [in] ssl SSL/TLS object. |
166 | | * @param [in] peer Peer socket address, may be NULL to clear. |
167 | | * @param [in] peerSz Length of peer address in bytes. |
168 | | * @return WOLFSSL_SUCCESS on success. |
169 | | * @return WOLFSSL_FAILURE when ssl is NULL or on error. |
170 | | * @return WOLFSSL_NOT_IMPLEMENTED when DTLS is not compiled in. |
171 | | */ |
172 | | int wolfSSL_dtls_set_peer(WOLFSSL* ssl, void* peer, unsigned int peerSz) |
173 | 0 | { |
174 | | #ifdef WOLFSSL_DTLS |
175 | | int ret; |
176 | | |
177 | | if (ssl == NULL) |
178 | | return WOLFSSL_FAILURE; |
179 | | #ifdef WOLFSSL_RW_THREADED |
180 | | if (wc_LockRwLock_Wr(&ssl->buffers.dtlsCtx.peerLock) != 0) |
181 | | return WOLFSSL_FAILURE; |
182 | | #endif |
183 | | ret = wolfssl_local_SockAddrSet(&ssl->buffers.dtlsCtx.peer, peer, peerSz, |
184 | | ssl->heap); |
185 | | if (ret == WOLFSSL_SUCCESS && !(peer == NULL || peerSz == 0)) |
186 | | ssl->buffers.dtlsCtx.userSet = 1; |
187 | | else |
188 | | ssl->buffers.dtlsCtx.userSet = 0; |
189 | | #ifdef WOLFSSL_RW_THREADED |
190 | | if (wc_UnLockRwLock(&ssl->buffers.dtlsCtx.peerLock) != 0) |
191 | | ret = WOLFSSL_FAILURE; |
192 | | #endif |
193 | | return ret; |
194 | | #else |
195 | 0 | (void)ssl; |
196 | 0 | (void)peer; |
197 | 0 | (void)peerSz; |
198 | 0 | return WOLFSSL_NOT_IMPLEMENTED; |
199 | 0 | #endif |
200 | 0 | } |
201 | | |
202 | | #if defined(WOLFSSL_DTLS_CID) && !defined(WOLFSSL_NO_SOCK) |
203 | | /* Set the pending DTLS peer address on the object. |
204 | | * |
205 | | * Used with connection ID to stage a change of peer address. |
206 | | * |
207 | | * @param [in] ssl SSL/TLS object. |
208 | | * @param [in] peer Peer socket address. |
209 | | * @param [in] peerSz Length of peer address in bytes. |
210 | | * @return WOLFSSL_SUCCESS on success. |
211 | | * @return WOLFSSL_FAILURE when ssl is NULL or on error. |
212 | | * @return WOLFSSL_NOT_IMPLEMENTED when DTLS is not compiled in. |
213 | | */ |
214 | | int wolfSSL_dtls_set_pending_peer(WOLFSSL* ssl, void* peer, unsigned int peerSz) |
215 | | { |
216 | | #ifdef WOLFSSL_DTLS |
217 | | int ret = WC_NO_ERR_TRACE(WOLFSSL_FAILURE); |
218 | | |
219 | | if (ssl == NULL) |
220 | | return WOLFSSL_FAILURE; |
221 | | #ifdef WOLFSSL_RW_THREADED |
222 | | if (wc_LockRwLock_Wr(&ssl->buffers.dtlsCtx.peerLock) != 0) |
223 | | return WOLFSSL_FAILURE; |
224 | | #endif |
225 | | if (ssl->buffers.dtlsCtx.peer.sa != NULL && |
226 | | ssl->buffers.dtlsCtx.peer.sz == peerSz && |
227 | | sockAddrEqual((SOCKADDR_S*)ssl->buffers.dtlsCtx.peer.sa, |
228 | | (XSOCKLENT)ssl->buffers.dtlsCtx.peer.sz, (SOCKADDR_S*)peer, |
229 | | (XSOCKLENT)peerSz)) { |
230 | | /* Already the current peer. */ |
231 | | if (ssl->buffers.dtlsCtx.pendingPeer.sa != NULL) { |
232 | | /* Clear any other pendingPeer */ |
233 | | XFREE(ssl->buffers.dtlsCtx.pendingPeer.sa, ssl->heap, |
234 | | DYNAMIC_TYPE_SOCKADDR); |
235 | | ssl->buffers.dtlsCtx.pendingPeer.sa = NULL; |
236 | | ssl->buffers.dtlsCtx.pendingPeer.sz = 0; |
237 | | ssl->buffers.dtlsCtx.pendingPeer.bufSz = 0; |
238 | | } |
239 | | ret = WOLFSSL_SUCCESS; |
240 | | } |
241 | | else { |
242 | | ret = wolfssl_local_SockAddrSet(&ssl->buffers.dtlsCtx.pendingPeer, |
243 | | peer, peerSz, ssl->heap); |
244 | | } |
245 | | if (ret == WOLFSSL_SUCCESS) |
246 | | ssl->buffers.dtlsCtx.processingPendingRecord = 0; |
247 | | #ifdef WOLFSSL_RW_THREADED |
248 | | if (wc_UnLockRwLock(&ssl->buffers.dtlsCtx.peerLock) != 0) |
249 | | ret = WOLFSSL_FAILURE; |
250 | | #endif |
251 | | return ret; |
252 | | #else |
253 | | (void)ssl; |
254 | | (void)peer; |
255 | | (void)peerSz; |
256 | | return WOLFSSL_NOT_IMPLEMENTED; |
257 | | #endif |
258 | | } |
259 | | #endif /* WOLFSSL_DTLS_CID && !WOLFSSL_NO_SOCK */ |
260 | | |
261 | | /* Get a copy of the DTLS peer address from the object. |
262 | | * |
263 | | * @param [in] ssl SSL/TLS object. |
264 | | * @param [out] peer Buffer to hold the peer address. |
265 | | * @param [in, out] peerSz In: size of buffer. Out: length of address. |
266 | | * @return WOLFSSL_SUCCESS on success. |
267 | | * @return WOLFSSL_FAILURE when ssl is NULL or the buffer is too small. |
268 | | * @return WOLFSSL_NOT_IMPLEMENTED when DTLS is not compiled in. |
269 | | */ |
270 | | int wolfSSL_dtls_get_peer(WOLFSSL* ssl, void* peer, unsigned int* peerSz) |
271 | 0 | { |
272 | | #ifdef WOLFSSL_DTLS |
273 | | int ret = WC_NO_ERR_TRACE(WOLFSSL_FAILURE); |
274 | | if (ssl == NULL) |
275 | | return WOLFSSL_FAILURE; |
276 | | #ifdef WOLFSSL_RW_THREADED |
277 | | if (wc_LockRwLock_Rd(&ssl->buffers.dtlsCtx.peerLock) != 0) |
278 | | return WOLFSSL_FAILURE; |
279 | | #endif |
280 | | if (peer != NULL && peerSz != NULL |
281 | | && *peerSz >= ssl->buffers.dtlsCtx.peer.sz |
282 | | && ssl->buffers.dtlsCtx.peer.sa != NULL) { |
283 | | *peerSz = ssl->buffers.dtlsCtx.peer.sz; |
284 | | XMEMCPY(peer, ssl->buffers.dtlsCtx.peer.sa, *peerSz); |
285 | | ret = WOLFSSL_SUCCESS; |
286 | | } |
287 | | #ifdef WOLFSSL_RW_THREADED |
288 | | if (wc_UnLockRwLock(&ssl->buffers.dtlsCtx.peerLock) != 0) |
289 | | ret = WOLFSSL_FAILURE; |
290 | | #endif |
291 | | return ret; |
292 | | #else |
293 | 0 | (void)ssl; |
294 | 0 | (void)peer; |
295 | 0 | (void)peerSz; |
296 | 0 | return WOLFSSL_NOT_IMPLEMENTED; |
297 | 0 | #endif |
298 | 0 | } |
299 | | |
300 | | /* Get a pointer to the DTLS peer address stored on the object. |
301 | | * |
302 | | * @param [in] ssl SSL/TLS object. |
303 | | * @param [out] peer Pointer to the stored peer address. |
304 | | * @param [out] peerSz Length of the peer address in bytes. |
305 | | * @return WOLFSSL_SUCCESS on success. |
306 | | * @return WOLFSSL_FAILURE when an argument is NULL. |
307 | | * @return WOLFSSL_NOT_IMPLEMENTED when DTLS is not compiled in or threaded. |
308 | | */ |
309 | | int wolfSSL_dtls_get0_peer(WOLFSSL* ssl, const void** peer, |
310 | | unsigned int* peerSz) |
311 | 0 | { |
312 | | #if defined(WOLFSSL_DTLS) && !defined(WOLFSSL_RW_THREADED) |
313 | | if (ssl == NULL) |
314 | | return WOLFSSL_FAILURE; |
315 | | |
316 | | if (peer == NULL || peerSz == NULL) |
317 | | return WOLFSSL_FAILURE; |
318 | | |
319 | | *peer = ssl->buffers.dtlsCtx.peer.sa; |
320 | | *peerSz = ssl->buffers.dtlsCtx.peer.sz; |
321 | | return WOLFSSL_SUCCESS; |
322 | | #else |
323 | 0 | (void)ssl; |
324 | 0 | (void)peer; |
325 | 0 | (void)peerSz; |
326 | 0 | return WOLFSSL_NOT_IMPLEMENTED; |
327 | 0 | #endif |
328 | 0 | } |
329 | | |
330 | | #if defined(WOLFSSL_SCTP) && defined(WOLFSSL_DTLS) |
331 | | |
332 | | /* Enable DTLS over SCTP mode on the context. |
333 | | * |
334 | | * @param [in] ctx SSL/TLS context object. |
335 | | * @return WOLFSSL_SUCCESS on success. |
336 | | * @return BAD_FUNC_ARG when ctx is NULL. |
337 | | */ |
338 | | int wolfSSL_CTX_dtls_set_sctp(WOLFSSL_CTX* ctx) |
339 | | { |
340 | | WOLFSSL_ENTER("wolfSSL_CTX_dtls_set_sctp"); |
341 | | |
342 | | if (ctx == NULL) |
343 | | return BAD_FUNC_ARG; |
344 | | |
345 | | ctx->dtlsSctp = 1; |
346 | | return WOLFSSL_SUCCESS; |
347 | | } |
348 | | |
349 | | /* Enable DTLS over SCTP mode on the object. |
350 | | * |
351 | | * @param [in] ssl SSL/TLS object. |
352 | | * @return WOLFSSL_SUCCESS on success. |
353 | | * @return BAD_FUNC_ARG when ssl is NULL. |
354 | | */ |
355 | | int wolfSSL_dtls_set_sctp(WOLFSSL* ssl) |
356 | | { |
357 | | WOLFSSL_ENTER("wolfSSL_dtls_set_sctp"); |
358 | | |
359 | | if (ssl == NULL) |
360 | | return BAD_FUNC_ARG; |
361 | | |
362 | | ssl->options.dtlsSctp = 1; |
363 | | return WOLFSSL_SUCCESS; |
364 | | } |
365 | | |
366 | | #endif /* WOLFSSL_DTLS && WOLFSSL_SCTP */ |
367 | | |
368 | | #if (defined(WOLFSSL_SCTP) || defined(WOLFSSL_DTLS_MTU)) && \ |
369 | | defined(WOLFSSL_DTLS) |
370 | | |
371 | | /* Set the DTLS path MTU on the context. |
372 | | * |
373 | | * @param [in] ctx SSL/TLS context object. |
374 | | * @param [in] newMtu Maximum transmission unit in bytes. |
375 | | * @return WOLFSSL_SUCCESS on success. |
376 | | * @return BAD_FUNC_ARG when ctx is NULL or newMtu is too large. |
377 | | */ |
378 | | int wolfSSL_CTX_dtls_set_mtu(WOLFSSL_CTX* ctx, word16 newMtu) |
379 | | { |
380 | | if (ctx == NULL || newMtu > MAX_RECORD_SIZE) |
381 | | return BAD_FUNC_ARG; |
382 | | |
383 | | ctx->dtlsMtuSz = newMtu; |
384 | | return WOLFSSL_SUCCESS; |
385 | | } |
386 | | |
387 | | /* Set the DTLS path MTU on the object. |
388 | | * |
389 | | * @param [in] ssl SSL/TLS object. |
390 | | * @param [in] newMtu Maximum transmission unit in bytes. |
391 | | * @return WOLFSSL_SUCCESS on success. |
392 | | * @return BAD_FUNC_ARG when ssl is NULL. |
393 | | * @return WOLFSSL_FAILURE when newMtu is too large. |
394 | | */ |
395 | | int wolfSSL_dtls_set_mtu(WOLFSSL* ssl, word16 newMtu) |
396 | | { |
397 | | if (ssl == NULL) |
398 | | return BAD_FUNC_ARG; |
399 | | |
400 | | if (newMtu > MAX_RECORD_SIZE) { |
401 | | ssl->error = BAD_FUNC_ARG; |
402 | | return WOLFSSL_FAILURE; |
403 | | } |
404 | | |
405 | | ssl->dtlsMtuSz = newMtu; |
406 | | return WOLFSSL_SUCCESS; |
407 | | } |
408 | | |
409 | | #ifdef OPENSSL_EXTRA |
410 | | /* Set the DTLS path MTU on the object. |
411 | | * |
412 | | * Maps to the compatibility API SSL_set_mtu. Same as wolfSSL_dtls_set_mtu() |
413 | | * but returns only success or failure. |
414 | | * |
415 | | * @param [in] ssl SSL/TLS object. |
416 | | * @param [in] mtu Maximum transmission unit in bytes. |
417 | | * @return WOLFSSL_SUCCESS on success. |
418 | | * @return WOLFSSL_FAILURE on error. |
419 | | */ |
420 | | int wolfSSL_set_mtu_compat(WOLFSSL* ssl, unsigned short mtu) |
421 | | { |
422 | | if (wolfSSL_dtls_set_mtu(ssl, mtu) == WOLFSSL_SUCCESS) |
423 | | return WOLFSSL_SUCCESS; |
424 | | else |
425 | | return WOLFSSL_FAILURE; |
426 | | } |
427 | | #endif /* OPENSSL_EXTRA */ |
428 | | |
429 | | #endif /* WOLFSSL_DTLS && (WOLFSSL_SCTP || WOLFSSL_DTLS_MTU) */ |
430 | | |
431 | | #ifdef WOLFSSL_SRTP |
432 | | |
433 | | static const WOLFSSL_SRTP_PROTECTION_PROFILE gSrtpProfiles[] = { |
434 | | /* AES CCM 128, Salt:112-bits, Auth HMAC-SHA1 Tag: 80-bits |
435 | | * (master_key:128bits + master_salt:112bits) * 2 = 480 bits (60) */ |
436 | | {"SRTP_AES128_CM_SHA1_80", SRTP_AES128_CM_SHA1_80, |
437 | | (((128 + 112) * 2) / 8) }, |
438 | | /* AES CCM 128, Salt:112-bits, Auth HMAC-SHA1 Tag: 32-bits |
439 | | * (master_key:128bits + master_salt:112bits) * 2 = 480 bits (60) */ |
440 | | {"SRTP_AES128_CM_SHA1_32", SRTP_AES128_CM_SHA1_32, |
441 | | (((128 + 112) * 2) / 8) }, |
442 | | /* NULL Cipher, Salt:112-bits, Auth HMAC-SHA1 Tag 80-bits */ |
443 | | {"SRTP_NULL_SHA1_80", SRTP_NULL_SHA1_80, ((112 * 2) / 8)}, |
444 | | /* NULL Cipher, Salt:112-bits, Auth HMAC-SHA1 Tag 32-bits */ |
445 | | {"SRTP_NULL_SHA1_32", SRTP_NULL_SHA1_32, ((112 * 2) / 8)}, |
446 | | /* AES GCM 128, Salt: 96-bits, Auth GCM Tag 128-bits |
447 | | * (master_key:128bits + master_salt:96bits) * 2 = 448 bits (56) */ |
448 | | {"SRTP_AEAD_AES_128_GCM", SRTP_AEAD_AES_128_GCM, (((128 + 96) * 2) / 8) }, |
449 | | /* AES GCM 256, Salt: 96-bits, Auth GCM Tag 128-bits |
450 | | * (master_key:256bits + master_salt:96bits) * 2 = 704 bits (88) */ |
451 | | {"SRTP_AEAD_AES_256_GCM", SRTP_AEAD_AES_256_GCM, (((256 + 96) * 2) / 8) }, |
452 | | }; |
453 | | |
454 | | /* Find an SRTP protection profile by name or by id. |
455 | | * |
456 | | * @param [in] profile_str Profile name, or NULL to search by id. |
457 | | * @param [in] profile_str_len Length of profile name in bytes. |
458 | | * @param [in] id Profile id to search for when name is NULL. |
459 | | * @return Matching SRTP protection profile on success. |
460 | | * @return NULL when no profile matches. |
461 | | */ |
462 | | static const WOLFSSL_SRTP_PROTECTION_PROFILE* DtlsSrtpFindProfile( |
463 | | const char* profile_str, word32 profile_str_len, unsigned long id) |
464 | | { |
465 | | int i; |
466 | | const WOLFSSL_SRTP_PROTECTION_PROFILE* profile = NULL; |
467 | | for (i=0; |
468 | | i<(int)(sizeof(gSrtpProfiles)/sizeof(WOLFSSL_SRTP_PROTECTION_PROFILE)); |
469 | | i++) { |
470 | | if (profile_str != NULL) { |
471 | | word32 srtp_profile_len = (word32)XSTRLEN(gSrtpProfiles[i].name); |
472 | | if (srtp_profile_len == profile_str_len && |
473 | | XMEMCMP(gSrtpProfiles[i].name, profile_str, profile_str_len) |
474 | | == 0) { |
475 | | profile = &gSrtpProfiles[i]; |
476 | | break; |
477 | | } |
478 | | } |
479 | | else if (id != 0 && gSrtpProfiles[i].id == id) { |
480 | | profile = &gSrtpProfiles[i]; |
481 | | break; |
482 | | } |
483 | | } |
484 | | return profile; |
485 | | } |
486 | | |
487 | | /* Select SRTP protection profiles from a colon-separated name list. |
488 | | * |
489 | | * @param [out] id Bitmask of selected profile ids. |
490 | | * @param [in] profile_str Colon-separated list of SRTP profile names. |
491 | | * @return WOLFSSL_SUCCESS on success. |
492 | | * @return WOLFSSL_FAILURE when profile_str is NULL. |
493 | | */ |
494 | | static int DtlsSrtpSelProfiles(word16* id, const char* profile_str) |
495 | | { |
496 | | const WOLFSSL_SRTP_PROTECTION_PROFILE* profile; |
497 | | const char *current, *next = NULL; |
498 | | word32 length = 0, current_length; |
499 | | |
500 | | *id = 0; /* reset destination ID's */ |
501 | | |
502 | | if (profile_str == NULL) { |
503 | | return WOLFSSL_FAILURE; |
504 | | } |
505 | | |
506 | | /* loop on end of line or colon ":" */ |
507 | | next = profile_str; |
508 | | length = (word32)XSTRLEN(profile_str); |
509 | | do { |
510 | | current = next; |
511 | | next = XSTRSTR(current, ":"); |
512 | | if (next) { |
513 | | current_length = (word32)(next - current); |
514 | | ++next; /* ++ needed to skip ':' */ |
515 | | } else { |
516 | | current_length = (word32)XSTRLEN(current); |
517 | | } |
518 | | if (current_length < length) |
519 | | length = current_length; |
520 | | profile = DtlsSrtpFindProfile(current, current_length, 0); |
521 | | if (profile != NULL) { |
522 | | *id |= (1 << profile->id); /* selected bit based on ID */ |
523 | | } |
524 | | } while (next != NULL); |
525 | | return WOLFSSL_SUCCESS; |
526 | | } |
527 | | |
528 | | /* Set the SRTP protection profiles for DTLS on the context. |
529 | | * |
530 | | * @param [in] ctx SSL/TLS context object. |
531 | | * @param [in] profile_str Colon-separated list of SRTP profile names. |
532 | | * @return 0 on success, to match OpenSSL. |
533 | | * @return 1 on error, to match OpenSSL. |
534 | | */ |
535 | | int wolfSSL_CTX_set_tlsext_use_srtp(WOLFSSL_CTX* ctx, const char* profile_str) |
536 | | { |
537 | | int ret = WC_NO_ERR_TRACE(WOLFSSL_FAILURE); |
538 | | if (ctx != NULL) { |
539 | | ret = DtlsSrtpSelProfiles(&ctx->dtlsSrtpProfiles, profile_str); |
540 | | } |
541 | | |
542 | | if (ret == WC_NO_ERR_TRACE(WOLFSSL_FAILURE)) { |
543 | | ret = 1; |
544 | | } else { |
545 | | ret = 0; |
546 | | } |
547 | | |
548 | | return ret; |
549 | | } |
550 | | |
551 | | /* Set the SRTP protection profiles for DTLS on the object. |
552 | | * |
553 | | * @param [in] ssl SSL/TLS object. |
554 | | * @param [in] profile_str Colon-separated list of SRTP profile names. |
555 | | * @return 0 on success, to match OpenSSL. |
556 | | * @return 1 on error, to match OpenSSL. |
557 | | */ |
558 | | int wolfSSL_set_tlsext_use_srtp(WOLFSSL* ssl, const char* profile_str) |
559 | | { |
560 | | int ret = WC_NO_ERR_TRACE(WOLFSSL_FAILURE); |
561 | | if (ssl != NULL) { |
562 | | ret = DtlsSrtpSelProfiles(&ssl->dtlsSrtpProfiles, profile_str); |
563 | | } |
564 | | |
565 | | if (ret == WC_NO_ERR_TRACE(WOLFSSL_FAILURE)) { |
566 | | ret = 1; |
567 | | } else { |
568 | | ret = 0; |
569 | | } |
570 | | |
571 | | return ret; |
572 | | } |
573 | | |
574 | | /* Get the SRTP protection profile selected for the object. |
575 | | * |
576 | | * @param [in] ssl SSL/TLS object. |
577 | | * @return Selected SRTP protection profile on success. |
578 | | * @return NULL when ssl is NULL or none is selected. |
579 | | */ |
580 | | const WOLFSSL_SRTP_PROTECTION_PROFILE* wolfSSL_get_selected_srtp_profile( |
581 | | WOLFSSL* ssl) |
582 | | { |
583 | | const WOLFSSL_SRTP_PROTECTION_PROFILE* profile = NULL; |
584 | | if (ssl) { |
585 | | profile = DtlsSrtpFindProfile(NULL, 0, ssl->dtlsSrtpId); |
586 | | } |
587 | | return profile; |
588 | | } |
589 | | #ifndef NO_WOLFSSL_STUB |
590 | | /* Get the list of SRTP protection profiles set on the object. |
591 | | * |
592 | | * Not implemented - stub. |
593 | | * |
594 | | * @param [in] ssl SSL/TLS object. |
595 | | * @return NULL always. |
596 | | */ |
597 | | WOLF_STACK_OF(WOLFSSL_SRTP_PROTECTION_PROFILE)* wolfSSL_get_srtp_profiles( |
598 | | WOLFSSL* ssl) |
599 | | { |
600 | | /* Not yet implemented - should return list of available SRTP profiles |
601 | | * ssl->dtlsSrtpProfiles */ |
602 | | (void)ssl; |
603 | | return NULL; |
604 | | } |
605 | | #endif |
606 | | |
607 | | #define DTLS_SRTP_KEYING_MATERIAL_LABEL "EXTRACTOR-dtls_srtp" |
608 | | |
609 | | /* Export the DTLS-SRTP keying material for the object. |
610 | | * |
611 | | * When out is NULL, the length required is returned in olen. |
612 | | * |
613 | | * @param [in] ssl SSL/TLS object. |
614 | | * @param [out] out Buffer to hold keying material. May be NULL. |
615 | | * @param [in, out] olen In: size of buffer. Out: length of keying material. |
616 | | * @return WOLFSSL_SUCCESS on success. |
617 | | * @return BAD_FUNC_ARG when ssl or olen is NULL. |
618 | | * @return EXT_MISSING when DTLS-SRTP is not in use. |
619 | | * @return LENGTH_ONLY_E when out is NULL and olen has been set. |
620 | | * @return BUFFER_E when the buffer is too small. |
621 | | */ |
622 | | int wolfSSL_export_dtls_srtp_keying_material(WOLFSSL* ssl, |
623 | | unsigned char* out, size_t* olen) |
624 | | { |
625 | | const WOLFSSL_SRTP_PROTECTION_PROFILE* profile = NULL; |
626 | | |
627 | | if (ssl == NULL || olen == NULL) { |
628 | | return BAD_FUNC_ARG; |
629 | | } |
630 | | |
631 | | profile = DtlsSrtpFindProfile(NULL, 0, ssl->dtlsSrtpId); |
632 | | if (profile == NULL) { |
633 | | WOLFSSL_MSG("Not using DTLS SRTP"); |
634 | | return EXT_MISSING; |
635 | | } |
636 | | if (out == NULL) { |
637 | | *olen = (size_t)profile->kdfBits; |
638 | | return WC_NO_ERR_TRACE(LENGTH_ONLY_E); |
639 | | } |
640 | | |
641 | | if (*olen < (size_t)profile->kdfBits) { |
642 | | return BUFFER_E; |
643 | | } |
644 | | |
645 | | return wolfSSL_export_keying_material(ssl, out, (size_t)profile->kdfBits, |
646 | | DTLS_SRTP_KEYING_MATERIAL_LABEL, |
647 | | XSTR_SIZEOF(DTLS_SRTP_KEYING_MATERIAL_LABEL), NULL, 0, 0); |
648 | | } |
649 | | |
650 | | #endif /* WOLFSSL_SRTP */ |
651 | | |
652 | | #ifdef WOLFSSL_DTLS_DROP_STATS |
653 | | |
654 | | /* Get the DTLS dropped-record statistics for the object. |
655 | | * |
656 | | * @param [in] ssl SSL/TLS object. |
657 | | * @param [out] macDropCount Number of records dropped on MAC failure. |
658 | | * @param [out] replayDropCount Number of records dropped as replays. |
659 | | * @return WOLFSSL_SUCCESS on success. |
660 | | * @return BAD_FUNC_ARG when ssl is NULL. |
661 | | */ |
662 | | int wolfSSL_dtls_get_drop_stats(WOLFSSL* ssl, |
663 | | word32* macDropCount, word32* replayDropCount) |
664 | | { |
665 | | int ret; |
666 | | |
667 | | WOLFSSL_ENTER("wolfSSL_dtls_get_drop_stats"); |
668 | | |
669 | | if (ssl == NULL) |
670 | | ret = BAD_FUNC_ARG; |
671 | | else { |
672 | | ret = WOLFSSL_SUCCESS; |
673 | | if (macDropCount != NULL) |
674 | | *macDropCount = ssl->macDropCount; |
675 | | if (replayDropCount != NULL) |
676 | | *replayDropCount = ssl->replayDropCount; |
677 | | } |
678 | | |
679 | | WOLFSSL_LEAVE("wolfSSL_dtls_get_drop_stats", ret); |
680 | | return ret; |
681 | | } |
682 | | |
683 | | #endif /* WOLFSSL_DTLS_DROP_STATS */ |
684 | | |
685 | | #if defined(WOLFSSL_MULTICAST) |
686 | | |
687 | | /* Set the multicast member id on the context and enable multicast. |
688 | | * |
689 | | * @param [in] ctx SSL/TLS context object. |
690 | | * @param [in] id Multicast member id. |
691 | | * @return WOLFSSL_SUCCESS on success. |
692 | | * @return BAD_FUNC_ARG when ctx is NULL or id is out of range. |
693 | | */ |
694 | | int wolfSSL_CTX_mcast_set_member_id(WOLFSSL_CTX* ctx, word16 id) |
695 | | { |
696 | | int ret = 0; |
697 | | |
698 | | WOLFSSL_ENTER("wolfSSL_CTX_mcast_set_member_id"); |
699 | | |
700 | | if (ctx == NULL || id > WOLFSSL_MAX_8BIT) |
701 | | ret = BAD_FUNC_ARG; |
702 | | |
703 | | if (ret == 0) { |
704 | | ctx->haveEMS = 0; |
705 | | ctx->haveMcast = 1; |
706 | | ctx->mcastID = (byte)id; |
707 | | #ifndef WOLFSSL_USER_IO |
708 | | ctx->CBIORecv = EmbedReceiveFromMcast; |
709 | | #endif /* WOLFSSL_USER_IO */ |
710 | | |
711 | | ret = WOLFSSL_SUCCESS; |
712 | | } |
713 | | WOLFSSL_LEAVE("wolfSSL_CTX_mcast_set_member_id", ret); |
714 | | return ret; |
715 | | } |
716 | | |
717 | | /* Get the maximum number of multicast peers supported. |
718 | | * |
719 | | * @return Maximum number of multicast peers. |
720 | | */ |
721 | | int wolfSSL_mcast_get_max_peers(void) |
722 | | { |
723 | | return WOLFSSL_MULTICAST_PEERS; |
724 | | } |
725 | | |
726 | | #ifdef WOLFSSL_DTLS |
727 | | /* Determine the next highwater mark from the current sequence number. |
728 | | * |
729 | | * @param [in] cur Current sequence number. |
730 | | * @param [in] first First highwater threshold. |
731 | | * @param [in] second Second highwater threshold. |
732 | | * @param [in] high Maximum highwater threshold. |
733 | | * @return Next highwater mark, or 0 when cur is at or above high. |
734 | | */ |
735 | | static WC_INLINE word32 UpdateHighwaterMark(word32 cur, word32 first, |
736 | | word32 second, word32 high) |
737 | | { |
738 | | word32 newCur = 0; |
739 | | |
740 | | if (cur < first) |
741 | | newCur = first; |
742 | | else if (cur < second) |
743 | | newCur = second; |
744 | | else if (cur < high) |
745 | | newCur = high; |
746 | | |
747 | | return newCur; |
748 | | } |
749 | | #endif /* WOLFSSL_DTLS */ |
750 | | |
751 | | /* Set the master secret and derived keys directly on the object. |
752 | | * |
753 | | * Used with multicast to install externally derived keys. |
754 | | * |
755 | | * @param [in, out] ssl SSL/TLS object. |
756 | | * @param [in] epoch DTLS epoch to use. |
757 | | * @param [in] preMasterSecret Pre-master secret data. |
758 | | * @param [in] preMasterSz Length of pre-master secret in bytes. |
759 | | * @param [in] clientRandom Client random data (RAN_LEN bytes). |
760 | | * @param [in] serverRandom Server random data (RAN_LEN bytes). |
761 | | * @param [in] suite Cipher suite bytes (2). |
762 | | * @return WOLFSSL_SUCCESS on success. |
763 | | * @return WOLFSSL_FATAL_ERROR on error, including invalid arguments, a |
764 | | * secret longer than MAX_PREMASTER_SZ, and handshake arrays that |
765 | | * have already been released. The specific code is recorded in |
766 | | * ssl->error and can be read with wolfSSL_get_error(). |
767 | | */ |
768 | | int wolfSSL_set_secret(WOLFSSL* ssl, word16 epoch, |
769 | | const byte* preMasterSecret, word32 preMasterSz, |
770 | | const byte* clientRandom, const byte* serverRandom, |
771 | | const byte* suite) |
772 | | { |
773 | | int ret = 0; |
774 | | |
775 | | WOLFSSL_ENTER("wolfSSL_set_secret"); |
776 | | |
777 | | if (ssl == NULL || preMasterSecret == NULL || |
778 | | preMasterSz == 0 || preMasterSz > MAX_PREMASTER_SZ || |
779 | | clientRandom == NULL || serverRandom == NULL || suite == NULL) { |
780 | | |
781 | | ret = BAD_FUNC_ARG; |
782 | | } |
783 | | |
784 | | /* The handshake arrays are released once the handshake resources are |
785 | | * freed, so a reused object may not have them any more. */ |
786 | | if (ret == 0 && ssl->arrays == NULL) { |
787 | | WOLFSSL_MSG("Handshake arrays not available"); |
788 | | ret = BAD_FUNC_ARG; |
789 | | } |
790 | | |
791 | | if (ret == 0) { |
792 | | XMEMCPY(ssl->arrays->preMasterSecret, preMasterSecret, preMasterSz); |
793 | | if (preMasterSz < MAX_PREMASTER_SZ) { |
794 | | XMEMSET(ssl->arrays->preMasterSecret + preMasterSz, 0, |
795 | | MAX_PREMASTER_SZ - preMasterSz); |
796 | | } |
797 | | ssl->arrays->preMasterSz = preMasterSz; |
798 | | XMEMCPY(ssl->arrays->clientRandom, clientRandom, RAN_LEN); |
799 | | XMEMCPY(ssl->arrays->serverRandom, serverRandom, RAN_LEN); |
800 | | ssl->options.cipherSuite0 = suite[0]; |
801 | | ssl->options.cipherSuite = suite[1]; |
802 | | |
803 | | ret = SetCipherSpecs(ssl); |
804 | | } |
805 | | |
806 | | if (ret == 0) |
807 | | ret = MakeTlsMasterSecret(ssl); |
808 | | |
809 | | /* MakeTlsMasterSecret() does not clean it and the buffer now lives as |
810 | | * long as the object. This is the multicast entry point, so what was |
811 | | * copied in is a long-lived group secret. */ |
812 | | if (ssl != NULL && ssl->arrays != NULL) { |
813 | | ForceZero(ssl->arrays->preMasterSecret, MAX_PREMASTER_SZ); |
814 | | ssl->arrays->preMasterSz = 0; |
815 | | } |
816 | | |
817 | | if (ret == 0) { |
818 | | ssl->keys.encryptionOn = 1; |
819 | | ret = SetKeysSide(ssl, ENCRYPT_AND_DECRYPT_SIDE); |
820 | | } |
821 | | |
822 | | if (ret == 0) { |
823 | | if (ssl->options.dtls) { |
824 | | #ifdef WOLFSSL_DTLS |
825 | | WOLFSSL_DTLS_PEERSEQ* peerSeq; |
826 | | int i; |
827 | | |
828 | | ssl->keys.dtls_epoch = epoch; |
829 | | for (i = 0, peerSeq = ssl->keys.peerSeq; |
830 | | i < WOLFSSL_DTLS_PEERSEQ_SZ; |
831 | | i++, peerSeq++) { |
832 | | |
833 | | peerSeq->nextEpoch = epoch; |
834 | | peerSeq->prevSeq_lo = peerSeq->nextSeq_lo; |
835 | | peerSeq->prevSeq_hi = peerSeq->nextSeq_hi; |
836 | | peerSeq->nextSeq_lo = 0; |
837 | | peerSeq->nextSeq_hi = 0; |
838 | | XMEMCPY(peerSeq->prevWindow, peerSeq->window, DTLS_SEQ_SZ); |
839 | | XMEMSET(peerSeq->window, 0, DTLS_SEQ_SZ); |
840 | | peerSeq->highwaterMark = UpdateHighwaterMark(0, |
841 | | ssl->ctx->mcastFirstSeq, |
842 | | ssl->ctx->mcastSecondSeq, |
843 | | ssl->ctx->mcastMaxSeq); |
844 | | } |
845 | | #else |
846 | | (void)epoch; |
847 | | #endif |
848 | | } |
849 | | FreeHandshakeResources(ssl); |
850 | | ret = WOLFSSL_SUCCESS; |
851 | | } |
852 | | else { |
853 | | if (ssl) |
854 | | ssl->error = ret; |
855 | | ret = WOLFSSL_FATAL_ERROR; |
856 | | } |
857 | | WOLFSSL_LEAVE("wolfSSL_set_secret", ret); |
858 | | return ret; |
859 | | } |
860 | | |
861 | | #ifdef WOLFSSL_DTLS |
862 | | |
863 | | /* Add or remove a peer from the multicast peer list. |
864 | | * |
865 | | * @param [in] ssl SSL/TLS object. |
866 | | * @param [in] peerId Peer id to add or remove. |
867 | | * @param [in] sub 0 to add the peer, non-zero to remove it. |
868 | | * @return WOLFSSL_SUCCESS on success. |
869 | | * @return BAD_FUNC_ARG when ssl is NULL or peerId is out of range. |
870 | | * @return WOLFSSL_FATAL_ERROR when the peer list is full. |
871 | | */ |
872 | | int wolfSSL_mcast_peer_add(WOLFSSL* ssl, word16 peerId, int sub) |
873 | | { |
874 | | WOLFSSL_DTLS_PEERSEQ* p = NULL; |
875 | | int ret = WOLFSSL_SUCCESS; |
876 | | int i; |
877 | | |
878 | | WOLFSSL_ENTER("wolfSSL_mcast_peer_add"); |
879 | | if (ssl == NULL || peerId > WOLFSSL_MAX_8BIT) |
880 | | return BAD_FUNC_ARG; |
881 | | |
882 | | if (!sub) { |
883 | | /* Make sure it isn't already present, while keeping the last |
884 | | * open spot. */ |
885 | | for (i = 0; i < WOLFSSL_DTLS_PEERSEQ_SZ; i++) { |
886 | | if (ssl->keys.peerSeq[i].peerId == INVALID_PEER_ID) |
887 | | p = &ssl->keys.peerSeq[i]; |
888 | | if (ssl->keys.peerSeq[i].peerId == peerId) { |
889 | | WOLFSSL_MSG("Peer ID already in multicast peer list."); |
890 | | p = NULL; |
891 | | } |
892 | | } |
893 | | |
894 | | if (p != NULL) { |
895 | | XMEMSET(p, 0, sizeof(WOLFSSL_DTLS_PEERSEQ)); |
896 | | p->peerId = peerId; |
897 | | p->highwaterMark = UpdateHighwaterMark(0, |
898 | | ssl->ctx->mcastFirstSeq, |
899 | | ssl->ctx->mcastSecondSeq, |
900 | | ssl->ctx->mcastMaxSeq); |
901 | | } |
902 | | else { |
903 | | WOLFSSL_MSG("No room in peer list."); |
904 | | ret = WOLFSSL_FATAL_ERROR; |
905 | | } |
906 | | } |
907 | | else { |
908 | | for (i = 0; i < WOLFSSL_DTLS_PEERSEQ_SZ; i++) { |
909 | | if (ssl->keys.peerSeq[i].peerId == peerId) |
910 | | p = &ssl->keys.peerSeq[i]; |
911 | | } |
912 | | |
913 | | if (p != NULL) { |
914 | | p->peerId = INVALID_PEER_ID; |
915 | | } |
916 | | else { |
917 | | WOLFSSL_MSG("Peer not found in list."); |
918 | | } |
919 | | } |
920 | | |
921 | | WOLFSSL_LEAVE("wolfSSL_mcast_peer_add", ret); |
922 | | return ret; |
923 | | } |
924 | | |
925 | | /* Determine whether a multicast peer is known and active. |
926 | | * |
927 | | * @param [in] ssl SSL/TLS object. |
928 | | * @param [in] peerId Peer id to look up. |
929 | | * @return 1 when the peer is in the list with a non-zero sequence number. |
930 | | * @return 0 when the peer is not known or has not sent data. |
931 | | * @return BAD_FUNC_ARG when ssl is NULL or peerId is out of range. |
932 | | */ |
933 | | int wolfSSL_mcast_peer_known(WOLFSSL* ssl, unsigned short peerId) |
934 | | { |
935 | | int known = 0; |
936 | | int i; |
937 | | |
938 | | WOLFSSL_ENTER("wolfSSL_mcast_peer_known"); |
939 | | |
940 | | if (ssl == NULL || peerId > WOLFSSL_MAX_8BIT) { |
941 | | return BAD_FUNC_ARG; |
942 | | } |
943 | | |
944 | | for (i = 0; i < WOLFSSL_DTLS_PEERSEQ_SZ; i++) { |
945 | | if (ssl->keys.peerSeq[i].peerId == peerId) { |
946 | | if (ssl->keys.peerSeq[i].nextSeq_hi || |
947 | | ssl->keys.peerSeq[i].nextSeq_lo) { |
948 | | |
949 | | known = 1; |
950 | | } |
951 | | break; |
952 | | } |
953 | | } |
954 | | |
955 | | WOLFSSL_LEAVE("wolfSSL_mcast_peer_known", known); |
956 | | return known; |
957 | | } |
958 | | |
959 | | /* Set the multicast highwater callback and thresholds on the context. |
960 | | * |
961 | | * @param [in] ctx SSL/TLS context object. |
962 | | * @param [in] maxSeq Maximum sequence number threshold. |
963 | | * @param [in] first First sequence number threshold. |
964 | | * @param [in] second Second sequence number threshold. |
965 | | * @param [in] cb Highwater callback. |
966 | | * @return WOLFSSL_SUCCESS on success. |
967 | | * @return BAD_FUNC_ARG when an argument is NULL or thresholds are invalid. |
968 | | */ |
969 | | int wolfSSL_CTX_mcast_set_highwater_cb(WOLFSSL_CTX* ctx, word32 maxSeq, |
970 | | word32 first, word32 second, |
971 | | CallbackMcastHighwater cb) |
972 | | { |
973 | | if (ctx == NULL || (second && first > second) || |
974 | | first > maxSeq || second > maxSeq || cb == NULL) { |
975 | | |
976 | | return BAD_FUNC_ARG; |
977 | | } |
978 | | |
979 | | ctx->mcastHwCb = cb; |
980 | | ctx->mcastFirstSeq = first; |
981 | | ctx->mcastSecondSeq = second; |
982 | | ctx->mcastMaxSeq = maxSeq; |
983 | | |
984 | | return WOLFSSL_SUCCESS; |
985 | | } |
986 | | |
987 | | /* Set the user context passed to the multicast highwater callback. |
988 | | * |
989 | | * @param [in] ssl SSL/TLS object. |
990 | | * @param [in] ctx User context for the highwater callback. |
991 | | * @return WOLFSSL_SUCCESS on success. |
992 | | * @return BAD_FUNC_ARG when ssl or ctx is NULL. |
993 | | */ |
994 | | int wolfSSL_mcast_set_highwater_ctx(WOLFSSL* ssl, void* ctx) |
995 | | { |
996 | | if (ssl == NULL || ctx == NULL) |
997 | | return BAD_FUNC_ARG; |
998 | | |
999 | | ssl->mcastHwCbCtx = ctx; |
1000 | | |
1001 | | return WOLFSSL_SUCCESS; |
1002 | | } |
1003 | | |
1004 | | #endif /* WOLFSSL_DTLS */ |
1005 | | |
1006 | | #endif /* WOLFSSL_MULTICAST */ |
1007 | | |
1008 | | #endif /* WOLFSSL_LEANPSK */ |
1009 | | |
1010 | | #ifndef NO_TLS |
1011 | | #ifdef WOLFSSL_MULTICAST |
1012 | | |
1013 | | /* Read application data from a multicast DTLS object. |
1014 | | * |
1015 | | * @param [in] ssl SSL/TLS object. |
1016 | | * @param [out] id Peer id the data was received from. May be NULL. |
1017 | | * @param [out] data Buffer to hold the data read. |
1018 | | * @param [in] sz Size of the buffer in bytes. |
1019 | | * @return Number of bytes read on success. |
1020 | | * @return BAD_FUNC_ARG when ssl is NULL or sz is negative. |
1021 | | * @return Negative value on error. |
1022 | | */ |
1023 | | int wolfSSL_mcast_read(WOLFSSL* ssl, word16* id, void* data, int sz) |
1024 | | { |
1025 | | int ret = 0; |
1026 | | |
1027 | | WOLFSSL_ENTER("wolfSSL_mcast_read"); |
1028 | | |
1029 | | if ((ssl == NULL) || (sz < 0)) |
1030 | | return BAD_FUNC_ARG; |
1031 | | |
1032 | | ret = wolfSSL_read_internal(ssl, data, (size_t)sz, FALSE); |
1033 | | if (ssl->options.dtls && ssl->options.haveMcast && id != NULL) |
1034 | | *id = ssl->keys.curPeerId; |
1035 | | return ret; |
1036 | | } |
1037 | | |
1038 | | #endif /* WOLFSSL_MULTICAST */ |
1039 | | #endif /* !NO_TLS */ |
1040 | | |
1041 | | #ifdef WOLFSSL_DTLS |
1042 | | /* Get the DTLS MAC secret for the requested side and epoch. |
1043 | | * |
1044 | | * @param [in] ssl SSL/TLS object. |
1045 | | * @param [in] verify 1 for the verify (read) secret, 0 for the write one. |
1046 | | * @param [in] epochOrder Epoch order: PEER_ORDER, PREV_ORDER or CUR_ORDER. |
1047 | | * @return MAC secret on success. |
1048 | | * @return NULL when ssl is NULL, AEAD-only build, or epoch order is unknown. |
1049 | | */ |
1050 | | const byte* wolfSSL_GetDtlsMacSecret(WOLFSSL* ssl, int verify, int epochOrder) |
1051 | | { |
1052 | | #ifndef WOLFSSL_AEAD_ONLY |
1053 | | Keys* keys = NULL; |
1054 | | |
1055 | | (void)epochOrder; |
1056 | | |
1057 | | if (ssl == NULL) |
1058 | | return NULL; |
1059 | | |
1060 | | #ifdef HAVE_SECURE_RENEGOTIATION |
1061 | | switch (epochOrder) { |
1062 | | case PEER_ORDER: |
1063 | | if (IsDtlsMsgSCRKeys(ssl)) |
1064 | | keys = &ssl->secure_renegotiation->tmp_keys; |
1065 | | else |
1066 | | keys = &ssl->keys; |
1067 | | break; |
1068 | | case PREV_ORDER: |
1069 | | keys = &ssl->keys; |
1070 | | break; |
1071 | | case CUR_ORDER: |
1072 | | if (DtlsUseSCRKeys(ssl)) |
1073 | | keys = &ssl->secure_renegotiation->tmp_keys; |
1074 | | else |
1075 | | keys = &ssl->keys; |
1076 | | break; |
1077 | | default: |
1078 | | WOLFSSL_MSG("Unknown epoch order"); |
1079 | | return NULL; |
1080 | | } |
1081 | | #else |
1082 | | keys = &ssl->keys; |
1083 | | #endif |
1084 | | |
1085 | | if ( (ssl->options.side == WOLFSSL_CLIENT_END && !verify) || |
1086 | | (ssl->options.side == WOLFSSL_SERVER_END && verify) ) |
1087 | | return keys->client_write_MAC_secret; |
1088 | | else |
1089 | | return keys->server_write_MAC_secret; |
1090 | | #else |
1091 | | (void)ssl; |
1092 | | (void)verify; |
1093 | | (void)epochOrder; |
1094 | | |
1095 | | return NULL; |
1096 | | #endif |
1097 | | } |
1098 | | #endif /* WOLFSSL_DTLS */ |
1099 | | |
1100 | | /* Get whether the DTLS object is using non-blocking I/O. |
1101 | | * |
1102 | | * @param [in] ssl SSL/TLS object. |
1103 | | * @return 1 when non-blocking I/O is enabled. |
1104 | | * @return 0 when disabled or not a DTLS object. |
1105 | | * @return WOLFSSL_FAILURE when ssl is NULL. |
1106 | | */ |
1107 | | int wolfSSL_dtls_get_using_nonblock(WOLFSSL* ssl) |
1108 | 0 | { |
1109 | 0 | int useNb = 0; |
1110 | |
|
1111 | 0 | if (ssl == NULL) |
1112 | 0 | return WOLFSSL_FAILURE; |
1113 | | |
1114 | 0 | WOLFSSL_ENTER("wolfSSL_dtls_get_using_nonblock"); |
1115 | 0 | if (ssl->options.dtls) { |
1116 | | #ifdef WOLFSSL_DTLS |
1117 | | useNb = ssl->options.dtlsUseNonblock; |
1118 | | #endif |
1119 | 0 | } |
1120 | 0 | else { |
1121 | 0 | WOLFSSL_MSG("wolfSSL_dtls_get_using_nonblock() is " |
1122 | 0 | "DEPRECATED for non-DTLS use."); |
1123 | 0 | } |
1124 | 0 | return useNb; |
1125 | 0 | } |
1126 | | |
1127 | | #ifndef WOLFSSL_LEANPSK |
1128 | | |
1129 | | /* Set whether the DTLS object uses non-blocking I/O. |
1130 | | * |
1131 | | * @param [in] ssl SSL/TLS object. |
1132 | | * @param [in] nonblock 1 to use non-blocking I/O, 0 otherwise. |
1133 | | */ |
1134 | | void wolfSSL_dtls_set_using_nonblock(WOLFSSL* ssl, int nonblock) |
1135 | 0 | { |
1136 | 0 | (void)nonblock; |
1137 | |
|
1138 | 0 | WOLFSSL_ENTER("wolfSSL_dtls_set_using_nonblock"); |
1139 | |
|
1140 | 0 | if (ssl == NULL) |
1141 | 0 | return; |
1142 | | |
1143 | 0 | if (ssl->options.dtls) { |
1144 | | #ifdef WOLFSSL_DTLS |
1145 | | ssl->options.dtlsUseNonblock = (nonblock != 0); |
1146 | | #endif |
1147 | 0 | } |
1148 | 0 | else { |
1149 | 0 | WOLFSSL_MSG("wolfSSL_dtls_set_using_nonblock() is " |
1150 | 0 | "DEPRECATED for non-DTLS use."); |
1151 | 0 | } |
1152 | 0 | } |
1153 | | |
1154 | | #ifdef WOLFSSL_DTLS |
1155 | | |
1156 | | /* Get the current DTLS receive timeout, in seconds. |
1157 | | * |
1158 | | * @param [in] ssl SSL/TLS object. |
1159 | | * @return Current timeout in seconds, or 0 when ssl is NULL. |
1160 | | */ |
1161 | | int wolfSSL_dtls_get_current_timeout(WOLFSSL* ssl) |
1162 | | { |
1163 | | int timeout = 0; |
1164 | | if (ssl) |
1165 | | timeout = ssl->dtls_timeout; |
1166 | | |
1167 | | WOLFSSL_LEAVE("wolfSSL_dtls_get_current_timeout", timeout); |
1168 | | return timeout; |
1169 | | } |
1170 | | |
1171 | | #ifdef WOLFSSL_DTLS13 |
1172 | | |
1173 | | /* Determine whether a short receive timeout should be used. |
1174 | | * |
1175 | | * Recommended to be at most 1/4 of wolfSSL_dtls_get_current_timeout(). |
1176 | | * |
1177 | | * @param [in] ssl SSL/TLS object. |
1178 | | * @return 1 when a short timeout should be used. |
1179 | | * @return 0 otherwise, or when ssl is NULL. |
1180 | | */ |
1181 | | int wolfSSL_dtls13_use_quick_timeout(WOLFSSL* ssl) |
1182 | | { |
1183 | | return ssl != NULL && ssl->dtls13FastTimeout; |
1184 | | } |
1185 | | |
1186 | | /* Set whether a DTLS 1.3 connection sends acks immediately on a disruption. |
1187 | | * |
1188 | | * Sending more acks may increase traffic but can speed up the handshake. |
1189 | | * |
1190 | | * @param [in] ssl SSL/TLS object. |
1191 | | * @param [in] value Non-zero to send more acks, 0 otherwise. |
1192 | | */ |
1193 | | void wolfSSL_dtls13_set_send_more_acks(WOLFSSL* ssl, int value) |
1194 | | { |
1195 | | if (ssl != NULL) |
1196 | | ssl->options.dtls13SendMoreAcks = !!value; |
1197 | | } |
1198 | | #endif /* WOLFSSL_DTLS13 */ |
1199 | | |
1200 | | #ifndef NO_ASN_TIME |
1201 | | /* Check whether a sent flight waits for the peer's reply. |
1202 | | * |
1203 | | * @param [in] ssl SSL/TLS object. |
1204 | | * @return 1 when a flight waits. |
1205 | | * @return 0 otherwise. |
1206 | | */ |
1207 | | static int DtlsFlightWaits(WOLFSSL* ssl) |
1208 | | { |
1209 | | if (!IsDtlsNotSctpMode(ssl) || ssl->dtls_timeout <= 0 || |
1210 | | ssl->options.isClosed || ssl->options.connReset || |
1211 | | ssl->options.sentNotify || ssl->options.closeNotify || |
1212 | | ssl->buffers.outputBuffer.length > 0) |
1213 | | return 0; |
1214 | | #ifdef HAVE_WRITE_DUP |
1215 | | if (ssl->dupWrite != NULL) |
1216 | | return 0; |
1217 | | #endif |
1218 | | #ifdef WOLFSSL_HAVE_HS_SUSPEND |
1219 | | if (IsHsSuspendErr(ssl->error)) |
1220 | | return 0; |
1221 | | #endif |
1222 | | #ifdef WOLFSSL_DTLS13 |
1223 | | if (IsAtLeastTLSv1_3(ssl->version)) |
1224 | | return ssl->dtls13Rtx.rtxRecords != NULL; |
1225 | | #endif |
1226 | | return ssl->options.handShakeState != HANDSHAKE_DONE && |
1227 | | (ssl->dtls_tx_msg_list != NULL || ssl->dtls_tx_msg != NULL); |
1228 | | } |
1229 | | |
1230 | | /* Start the retransmission timer for the current flight. |
1231 | | * |
1232 | | * @param [in] ssl SSL/TLS object. |
1233 | | */ |
1234 | | static void DtlsTimerRestart(WOLFSSL* ssl) |
1235 | | { |
1236 | | ssl->dtls_timer_start = LowResTimer(); |
1237 | | /* Round up a second so a whole-second clock never expires it early. */ |
1238 | | ssl->dtls_timer_len = (word32)ssl->dtls_timeout + 1; |
1239 | | ssl->dtls_timer_seq = ssl->keys.dtls_handshake_number; |
1240 | | } |
1241 | | #endif /* !NO_ASN_TIME */ |
1242 | | |
1243 | | /* Get the seconds left until the retransmission timer expires. |
1244 | | * |
1245 | | * @param [in] ssl SSL/TLS object. |
1246 | | * @param [out] left Seconds left, 0 once the timer expired. |
1247 | | * @return 1 when the timer runs. |
1248 | | * @return 0 when no timer runs. |
1249 | | */ |
1250 | | static int DtlsTimerLeft(WOLFSSL* ssl, word32* left) |
1251 | | { |
1252 | | #ifndef NO_ASN_TIME |
1253 | | word32 elapsed; |
1254 | | |
1255 | | if (!DtlsFlightWaits(ssl)) { |
1256 | | ssl->dtls_timer_len = 0; |
1257 | | return 0; |
1258 | | } |
1259 | | if (ssl->dtls_timer_len == 0 || |
1260 | | ssl->dtls_timer_seq != ssl->keys.dtls_handshake_number) |
1261 | | DtlsTimerRestart(ssl); |
1262 | | |
1263 | | elapsed = LowResTimer() - ssl->dtls_timer_start; |
1264 | | *left = (elapsed < ssl->dtls_timer_len) ? |
1265 | | ssl->dtls_timer_len - elapsed : 0; |
1266 | | return 1; |
1267 | | #else |
1268 | | (void)ssl; |
1269 | | (void)left; |
1270 | | return 0; |
1271 | | #endif |
1272 | | } |
1273 | | |
1274 | | /* Get the time left until the DTLS retransmission timer expires. |
1275 | | * |
1276 | | * The timer starts on the first call after a flight was sent. |
1277 | | * |
1278 | | * @param [in] ssl SSL/TLS object. |
1279 | | * @param [out] timeleft Time left, zero once the timer expired. |
1280 | | * @return 1 when the timer runs. |
1281 | | * @return 0 when no timer runs, or ssl or timeleft is NULL. |
1282 | | */ |
1283 | | int wolfSSL_DTLSv1_get_timeout(WOLFSSL* ssl, WOLFSSL_TIMEVAL* timeleft) |
1284 | | { |
1285 | | word32 left = 0; |
1286 | | |
1287 | | if (ssl == NULL || timeleft == NULL) |
1288 | | return 0; |
1289 | | |
1290 | | XMEMSET(timeleft, 0, sizeof(WOLFSSL_TIMEVAL)); |
1291 | | if (!DtlsTimerLeft(ssl, &left)) |
1292 | | return 0; |
1293 | | |
1294 | | timeleft->tv_sec = left; |
1295 | | return 1; |
1296 | | } |
1297 | | |
1298 | | /* Retransmit the last flight when the DTLS retransmission timer expired. |
1299 | | * |
1300 | | * @param [in] ssl SSL/TLS object. |
1301 | | * @return 1 when the timer expired and the flight was retransmitted. |
1302 | | * @return 0 when no timer runs or it has not expired yet. |
1303 | | * @return WOLFSSL_FATAL_ERROR when ssl is NULL or retransmitting failed. |
1304 | | */ |
1305 | | int wolfSSL_DTLSv1_handle_timeout(WOLFSSL* ssl) |
1306 | | { |
1307 | | word32 left = 0; |
1308 | | |
1309 | | if (ssl == NULL) |
1310 | | return WOLFSSL_FATAL_ERROR; |
1311 | | if (!DtlsTimerLeft(ssl, &left) || left > 0) |
1312 | | return 0; |
1313 | | #ifdef WOLFSSL_DTLS13 |
1314 | | ssl->dtls13FastTimeout = 0; |
1315 | | #endif |
1316 | | if (wolfSSL_dtls_got_timeout(ssl) != WOLFSSL_SUCCESS) { |
1317 | | #ifndef NO_ASN_TIME |
1318 | | ssl->dtls_timer_len = 0; |
1319 | | #endif |
1320 | | return WOLFSSL_FATAL_ERROR; |
1321 | | } |
1322 | | #ifndef NO_ASN_TIME |
1323 | | DtlsTimerRestart(ssl); |
1324 | | #endif |
1325 | | |
1326 | | return 1; |
1327 | | } |
1328 | | |
1329 | | #ifndef NO_WOLFSSL_STUB |
1330 | | /* Set the initial DTLS timeout duration. |
1331 | | * |
1332 | | * Not implemented - stub for OpenSSL compatibility. |
1333 | | * |
1334 | | * @param [in] ssl SSL/TLS object. |
1335 | | * @param [in] duration_ms Initial timeout duration in milliseconds. |
1336 | | */ |
1337 | | void wolfSSL_DTLSv1_set_initial_timeout_duration(WOLFSSL* ssl, |
1338 | | word32 duration_ms) |
1339 | | { |
1340 | | WOLFSSL_STUB("SSL_DTLSv1_set_initial_timeout_duration"); |
1341 | | (void)ssl; |
1342 | | (void)duration_ms; |
1343 | | } |
1344 | | #endif |
1345 | | |
1346 | | /* Set the initial DTLS receive timeout, in seconds, on the object. |
1347 | | * |
1348 | | * @param [in] ssl SSL/TLS object. |
1349 | | * @param [in] timeout Initial timeout in seconds. |
1350 | | * @return WOLFSSL_SUCCESS on success. |
1351 | | * @return BAD_FUNC_ARG when ssl is NULL, timeout is negative or greater than |
1352 | | * the maximum timeout. |
1353 | | */ |
1354 | | int wolfSSL_dtls_set_timeout_init(WOLFSSL* ssl, int timeout) |
1355 | | { |
1356 | | if (ssl == NULL || timeout < 0) |
1357 | | return BAD_FUNC_ARG; |
1358 | | |
1359 | | if (timeout > ssl->dtls_timeout_max) { |
1360 | | WOLFSSL_MSG("Can't set dtls timeout init greater than dtls timeout " |
1361 | | "max"); |
1362 | | return BAD_FUNC_ARG; |
1363 | | } |
1364 | | |
1365 | | ssl->dtls_timeout_init = timeout; |
1366 | | ssl->dtls_timeout = timeout; |
1367 | | |
1368 | | return WOLFSSL_SUCCESS; |
1369 | | } |
1370 | | |
1371 | | /* Set the maximum DTLS receive timeout, in seconds, on the object. |
1372 | | * |
1373 | | * @param [in] ssl SSL/TLS object. |
1374 | | * @param [in] timeout Maximum timeout in seconds. |
1375 | | * @return WOLFSSL_SUCCESS on success. |
1376 | | * @return BAD_FUNC_ARG when ssl is NULL, timeout is negative or less than |
1377 | | * the initial timeout. |
1378 | | */ |
1379 | | int wolfSSL_dtls_set_timeout_max(WOLFSSL* ssl, int timeout) |
1380 | | { |
1381 | | if (ssl == NULL || timeout < 0) |
1382 | | return BAD_FUNC_ARG; |
1383 | | |
1384 | | if (timeout < ssl->dtls_timeout_init) { |
1385 | | WOLFSSL_MSG("Can't set dtls timeout max less than dtls timeout init"); |
1386 | | return BAD_FUNC_ARG; |
1387 | | } |
1388 | | |
1389 | | ssl->dtls_timeout_max = timeout; |
1390 | | |
1391 | | return WOLFSSL_SUCCESS; |
1392 | | } |
1393 | | |
1394 | | /* Process a DTLS timeout, retransmitting messages as needed. |
1395 | | * |
1396 | | * @param [in] ssl SSL/TLS object. |
1397 | | * @return WOLFSSL_SUCCESS on success. |
1398 | | * @return WOLFSSL_FATAL_ERROR when ssl is NULL, not DTLS, or on error. |
1399 | | */ |
1400 | | int wolfSSL_dtls_got_timeout(WOLFSSL* ssl) |
1401 | | { |
1402 | | int result = WOLFSSL_SUCCESS; |
1403 | | WOLFSSL_ENTER("wolfSSL_dtls_got_timeout"); |
1404 | | |
1405 | | if (ssl == NULL || !ssl->options.dtls) |
1406 | | return WOLFSSL_FATAL_ERROR; |
1407 | | |
1408 | | #ifdef WOLFSSL_DTLS13 |
1409 | | if (IsAtLeastTLSv1_3(ssl->version)) { |
1410 | | result = Dtls13RtxTimeout(ssl); |
1411 | | if (result < 0) { |
1412 | | if (result == WC_NO_ERR_TRACE(WANT_WRITE)) |
1413 | | ssl->dtls13SendingAckOrRtx = 1; |
1414 | | ssl->error = result; |
1415 | | WOLFSSL_ERROR(result); |
1416 | | return WOLFSSL_FATAL_ERROR; |
1417 | | } |
1418 | | |
1419 | | return WOLFSSL_SUCCESS; |
1420 | | } |
1421 | | #endif /* WOLFSSL_DTLS13 */ |
1422 | | |
1423 | | /* Do we have any 1.2 messages stored? */ |
1424 | | if (ssl->dtls_tx_msg_list != NULL || ssl->dtls_tx_msg != NULL) { |
1425 | | if (DtlsMsgPoolTimeout(ssl) < 0){ |
1426 | | ssl->error = SOCKET_ERROR_E; |
1427 | | WOLFSSL_ERROR(ssl->error); |
1428 | | result = WOLFSSL_FATAL_ERROR; |
1429 | | } |
1430 | | else if ((result = DtlsMsgPoolSend(ssl, 0)) < 0) { |
1431 | | ssl->error = result; |
1432 | | WOLFSSL_ERROR(result); |
1433 | | result = WOLFSSL_FATAL_ERROR; |
1434 | | } |
1435 | | else { |
1436 | | /* Reset return value to success */ |
1437 | | result = WOLFSSL_SUCCESS; |
1438 | | } |
1439 | | } |
1440 | | |
1441 | | WOLFSSL_LEAVE("wolfSSL_dtls_got_timeout", result); |
1442 | | return result; |
1443 | | } |
1444 | | |
1445 | | /* Retransmit all stored DTLS handshake messages. |
1446 | | * |
1447 | | * @param [in] ssl SSL/TLS object. |
1448 | | * @return WOLFSSL_SUCCESS on success. |
1449 | | * @return WOLFSSL_FATAL_ERROR when ssl is NULL or on error. |
1450 | | */ |
1451 | | int wolfSSL_dtls_retransmit(WOLFSSL* ssl) |
1452 | | { |
1453 | | WOLFSSL_ENTER("wolfSSL_dtls_retransmit"); |
1454 | | |
1455 | | if (ssl == NULL) |
1456 | | return WOLFSSL_FATAL_ERROR; |
1457 | | |
1458 | | if (!ssl->options.handShakeDone) { |
1459 | | int result; |
1460 | | #ifdef WOLFSSL_DTLS13 |
1461 | | if (IsAtLeastTLSv1_3(ssl->version)) |
1462 | | result = Dtls13DoScheduledWork(ssl); |
1463 | | else |
1464 | | #endif |
1465 | | result = DtlsMsgPoolSend(ssl, 0); |
1466 | | if (result < 0) { |
1467 | | ssl->error = result; |
1468 | | WOLFSSL_ERROR(result); |
1469 | | return WOLFSSL_FATAL_ERROR; |
1470 | | } |
1471 | | } |
1472 | | |
1473 | | return WOLFSSL_SUCCESS; |
1474 | | } |
1475 | | |
1476 | | #ifdef WOLFSSL_DTLS13 |
1477 | | /* Is this object the kind the scheduled-work API operates on? |
1478 | | * |
1479 | | * A write-dup pair parks the read side's scheduled work in the shared WriteDup |
1480 | | * struct, and only wolfSSL_write() reconciles it back onto the write side. |
1481 | | * Such applications already have a working drain and are deliberately out of |
1482 | | * scope here, on either side of the pair. |
1483 | | */ |
1484 | | static int Dtls13ScheduledWorkObject(WOLFSSL* ssl) |
1485 | | { |
1486 | | if (!ssl->options.dtls || !IsAtLeastTLSv1_3(ssl->version)) |
1487 | | return 0; |
1488 | | #ifdef HAVE_WRITE_DUP |
1489 | | if (ssl->dupWrite != NULL) |
1490 | | return 0; |
1491 | | #endif |
1492 | | |
1493 | | return 1; |
1494 | | } |
1495 | | |
1496 | | /* Is there any point running the scheduled work now? |
1497 | | * |
1498 | | * While the handshake runs, wolfSSL_connect()/wolfSSL_accept() and |
1499 | | * wolfSSL_dtls_retransmit() already drive it. Kept separate from the object |
1500 | | * check above so the pump can tell a caller error, which it reports, from |
1501 | | * simply having nothing to do yet, which it does not. |
1502 | | */ |
1503 | | static int Dtls13ScheduledWorkReady(WOLFSSL* ssl) |
1504 | | { |
1505 | | return Dtls13ScheduledWorkObject(ssl) && ssl->options.handShakeDone; |
1506 | | } |
1507 | | |
1508 | | /* Can a key update be sent right now? |
1509 | | * |
1510 | | * DTLS must not have two in flight, so not while one of ours is still |
1511 | | * unacknowledged. This governs both an update we scheduled ourselves and |
1512 | | * answering one the peer asked for, because Tls13UpdateKeys() silently drops |
1513 | | * the former in that state. Both entry points ask this same question: the |
1514 | | * predicate must not report work the pump then declines or discards, or a |
1515 | | * drain loop over the pair never terminates and the caller is misled about |
1516 | | * what was done. |
1517 | | */ |
1518 | | static int Dtls13CanSendKeyUpdate(WOLFSSL* ssl) |
1519 | | { |
1520 | | return !ssl->dtls13WaitKeyUpdateAck; |
1521 | | } |
1522 | | |
1523 | | /* Check whether the object has DTLS 1.3 work waiting to be sent. |
1524 | | * |
1525 | | * Only meaningful with WOLFSSL_RW_THREADED, where the read path does not |
1526 | | * transmit. The answer is advisory: it can change as soon as it is returned, |
1527 | | * and wolfSSL_dtls13_do_scheduled_work() is safe to call regardless. |
1528 | | * |
1529 | | * Reports only work that wolfSSL_dtls13_do_scheduled_work() can carry out, so |
1530 | | * that a drain loop over the pair terminates. |
1531 | | * |
1532 | | * @param [in] ssl SSL/TLS object. |
1533 | | * @return 1 when there is work to send, or when it could not be determined. |
1534 | | * @return 0 when there is nothing to do, or ssl is not supported here. |
1535 | | */ |
1536 | | int wolfSSL_dtls13_pending_work(WOLFSSL* ssl) |
1537 | | { |
1538 | | int pending = 0; |
1539 | | |
1540 | | WOLFSSL_ENTER("wolfSSL_dtls13_pending_work"); |
1541 | | |
1542 | | if (ssl == NULL || !Dtls13ScheduledWorkReady(ssl)) |
1543 | | return 0; |
1544 | | |
1545 | | /* A record this API built but could only write in part. The pump retries |
1546 | | * it, so a drain loop has to be told the retry is still owed. */ |
1547 | | if (ssl->buffers.outputBuffer.length > 0 && ssl->dtls13SendingAckOrRtx) |
1548 | | pending = 1; |
1549 | | |
1550 | | /* dtls13WaitKeyUpdateAck deliberately does not count as work: it says we |
1551 | | * are waiting on the peer, not that we have anything to send. */ |
1552 | | if (!pending && (ssl->dtls13DoKeyUpdate || ssl->options.sendKeyUpdate) && |
1553 | | !IsWriteClosed(ssl) && Dtls13CanSendKeyUpdate(ssl)) { |
1554 | | pending = 1; |
1555 | | } |
1556 | | |
1557 | | if (!pending) { |
1558 | | #ifdef WOLFSSL_RW_THREADED |
1559 | | if (wc_LockMutex(&ssl->dtls13Rtx.mutex) != 0) { |
1560 | | /* Report work rather than nothing, so a drain loop calls the pump |
1561 | | * and surfaces the error instead of stopping silently. */ |
1562 | | return 1; |
1563 | | } |
1564 | | #endif |
1565 | | pending = ssl->dtls13Rtx.sendAcks || ssl->dtls13Rtx.retransmit; |
1566 | | #ifdef WOLFSSL_RW_THREADED |
1567 | | (void)wc_UnLockMutex(&ssl->dtls13Rtx.mutex); |
1568 | | #endif |
1569 | | } |
1570 | | |
1571 | | WOLFSSL_LEAVE("wolfSSL_dtls13_pending_work", pending); |
1572 | | |
1573 | | return pending; |
1574 | | } |
1575 | | |
1576 | | /* Send any DTLS 1.3 work that was scheduled while reading. |
1577 | | * |
1578 | | * Covers pending ACKs, retransmissions and key updates, including the |
1579 | | * KeyUpdate response a peer asked for. With WOLFSSL_RW_THREADED the read path |
1580 | | * never transmits, because doing so would race the write thread over the |
1581 | | * output buffer and the sending key schedule, so this work is only performed |
1582 | | * from the write side. An application that reads without writing has to call |
1583 | | * this or those messages are never sent, and the peer keeps retransmitting |
1584 | | * what it is waiting to have acknowledged. |
1585 | | * |
1586 | | * Call it from the same thread used for writing. Calling it concurrently with |
1587 | | * a write on another thread has the same effect as two concurrent writes. |
1588 | | * |
1589 | | * Not for write-dup applications: those park the read side's work in the |
1590 | | * shared WriteDup struct, which only wolfSSL_write() reconciles, so they |
1591 | | * already have a drain and get WOLFSSL_FATAL_ERROR here rather than a call |
1592 | | * that quietly does nothing. |
1593 | | * |
1594 | | * Note that a key update started from our own side still needs the peer's ACK |
1595 | | * to be processed before it completes, and that processing rotates the sending |
1596 | | * keys, so it cannot run here. |
1597 | | * |
1598 | | * A send that only wrote part of a record reports WANT_WRITE through |
1599 | | * wolfSSL_get_error(). The record is held and the next call sends the rest, |
1600 | | * so that is a retry rather than a reason to stop draining. |
1601 | | * |
1602 | | * @param [in] ssl SSL/TLS object. |
1603 | | * @return WOLFSSL_SUCCESS on success, including when there was nothing to do. |
1604 | | * @return WOLFSSL_FATAL_ERROR when ssl is NULL, unsupported, or on error. |
1605 | | */ |
1606 | | int wolfSSL_dtls13_do_scheduled_work(WOLFSSL* ssl) |
1607 | | { |
1608 | | int ret; |
1609 | | |
1610 | | WOLFSSL_ENTER("wolfSSL_dtls13_do_scheduled_work"); |
1611 | | |
1612 | | if (ssl == NULL) |
1613 | | return WOLFSSL_FATAL_ERROR; |
1614 | | |
1615 | | /* Rejecting the object is a usage error, not something that happened to |
1616 | | * the connection, so leave ssl->error alone. It is sticky: SendData() |
1617 | | * only clears it for WANT_WRITE, WC_PENDING_E and the DTLS MAC/decrypt |
1618 | | * cases, and wolfSSL_write() skips the write-dup drain entirely while it |
1619 | | * is set, so recording one here would disable the very drain a write-dup |
1620 | | * application relies on. */ |
1621 | | #ifdef HAVE_WRITE_DUP |
1622 | | if (ssl->dupWrite != NULL) { |
1623 | | WOLFSSL_MSG("Write dup objects drain through wolfSSL_write"); |
1624 | | WOLFSSL_LEAVE("wolfSSL_dtls13_do_scheduled_work", WOLFSSL_FATAL_ERROR); |
1625 | | return WOLFSSL_FATAL_ERROR; |
1626 | | } |
1627 | | #endif |
1628 | | |
1629 | | if (!Dtls13ScheduledWorkObject(ssl)) { |
1630 | | WOLFSSL_MSG("Not a DTLS 1.3 object this API handles"); |
1631 | | WOLFSSL_LEAVE("wolfSSL_dtls13_do_scheduled_work", WOLFSSL_FATAL_ERROR); |
1632 | | return WOLFSSL_FATAL_ERROR; |
1633 | | } |
1634 | | |
1635 | | if (!Dtls13ScheduledWorkReady(ssl)) |
1636 | | return WOLFSSL_SUCCESS; |
1637 | | |
1638 | | /* An earlier call may have left a record only partly written. Retry it |
1639 | | * first: the request that produced it has already been consumed, so |
1640 | | * nothing else would send it, and every other caller of |
1641 | | * Dtls13DoScheduledWork() pairs it with this same flush. Only a record |
1642 | | * this API built is retried here, which is what dtls13SendingAckOrRtx |
1643 | | * marks, so a partly written application record is left to the |
1644 | | * wolfSSL_write() the caller has to repeat anyway. */ |
1645 | | if (ssl->buffers.outputBuffer.length > 0 && ssl->dtls13SendingAckOrRtx) { |
1646 | | ret = SendBuffered(ssl); |
1647 | | if (ret != 0) { |
1648 | | ssl->error = ret; |
1649 | | WOLFSSL_ERROR(ret); |
1650 | | return WOLFSSL_FATAL_ERROR; |
1651 | | } |
1652 | | ssl->dtls13SendingAckOrRtx = 0; |
1653 | | } |
1654 | | |
1655 | | ret = Dtls13DoScheduledWork(ssl); |
1656 | | if (ret < 0) { |
1657 | | ssl->error = ret; |
1658 | | WOLFSSL_ERROR(ret); |
1659 | | return WOLFSSL_FATAL_ERROR; |
1660 | | } |
1661 | | |
1662 | | if (IsWriteClosed(ssl)) |
1663 | | ssl->options.sendKeyUpdate = 0; |
1664 | | |
1665 | | /* Answer a KeyUpdate the peer requested. The read path defers this the |
1666 | | * same way, and SendData() is otherwise the only thing that clears it. |
1667 | | * Skip it while one of ours is still unacknowledged: DTLS must not have |
1668 | | * two KeyUpdates in flight, and Dtls13DoScheduledWork() above may have |
1669 | | * just started one. */ |
1670 | | if (ssl->options.sendKeyUpdate && Dtls13CanSendKeyUpdate(ssl)) { |
1671 | | /* Drop the request before sending, as SendData() does. DTLS commits |
1672 | | * the record to the retransmit queue and consumes the handshake |
1673 | | * number on WANT_WRITE as well, and sets dtls13WaitKeyUpdateAck |
1674 | | * unconditionally, so the update is under way from that point on. |
1675 | | * Leaving the request set would send a second one once the peer |
1676 | | * acknowledges the first. */ |
1677 | | ssl->options.sendKeyUpdate = 0; |
1678 | | /* Mark the record as ours so a short write is retried by the flush |
1679 | | * above rather than waiting for a retransmission timer. */ |
1680 | | ssl->dtls13SendingAckOrRtx = 1; |
1681 | | ret = SendTls13KeyUpdate(ssl); |
1682 | | if (ret != 0) { |
1683 | | /* Keep the mark only while there is something left to send, so a |
1684 | | * failure that wrote nothing does not leave it standing. */ |
1685 | | ssl->dtls13SendingAckOrRtx = |
1686 | | (ssl->buffers.outputBuffer.length > 0); |
1687 | | ssl->error = ret; |
1688 | | WOLFSSL_ERROR(ret); |
1689 | | return WOLFSSL_FATAL_ERROR; |
1690 | | } |
1691 | | ssl->dtls13SendingAckOrRtx = 0; |
1692 | | } |
1693 | | |
1694 | | WOLFSSL_LEAVE("wolfSSL_dtls13_do_scheduled_work", WOLFSSL_SUCCESS); |
1695 | | |
1696 | | return WOLFSSL_SUCCESS; |
1697 | | } |
1698 | | #endif /* WOLFSSL_DTLS13 */ |
1699 | | |
1700 | | #endif /* DTLS */ |
1701 | | #endif /* LEANPSK */ |
1702 | | |
1703 | | #if (defined(WOLFSSL_DTLS) || defined(WOLFSSL_SEND_HRR_COOKIE)) && \ |
1704 | | !defined(NO_WOLFSSL_SERVER) |
1705 | | |
1706 | | /* Securely erase and free a cookie secret. |
1707 | | * |
1708 | | * @param [in] ssl SSL/TLS object. |
1709 | | * @param [in, out] secret Cookie secret buffer, left empty. |
1710 | | */ |
1711 | | void FreeCookieSecret(WOLFSSL* ssl, buffer* secret) |
1712 | | { |
1713 | | if (secret->buffer != NULL) { |
1714 | | ForceZero(secret->buffer, secret->length); |
1715 | | #ifdef WOLFSSL_CHECK_MEM_ZERO |
1716 | | /* Drop the entry InstallCookieSecret() added */ |
1717 | | wc_MemZero_Check(secret->buffer, secret->length); |
1718 | | #endif |
1719 | | XFREE(secret->buffer, ssl->heap, DYNAMIC_TYPE_COOKIE_PWD); |
1720 | | secret->buffer = NULL; |
1721 | | secret->length = 0; |
1722 | | } |
1723 | | } |
1724 | | |
1725 | | /* Build a cookie secret in a buffer the SSL object does not own yet. |
1726 | | * |
1727 | | * @param [in] ssl SSL/TLS object. |
1728 | | * @param [out] out New secret. Left empty on failure. |
1729 | | * @param [in] secret Secret data to copy, or NULL to generate one. |
1730 | | * @param [in] secretSz Length of the secret in bytes. |
1731 | | * @return 0 on success. |
1732 | | * @return BAD_FUNC_ARG when secretSz is 0. |
1733 | | * @return MEMORY_ERROR on allocation failure. |
1734 | | * @return BAD_STATE_E when a secret must be generated without an RNG. |
1735 | | * @return A negative error code when the RNG fails. |
1736 | | */ |
1737 | | static int NewCookieSecret(WOLFSSL* ssl, buffer* out, const byte* secret, |
1738 | | word32 secretSz) |
1739 | | { |
1740 | | int ret; |
1741 | | |
1742 | | out->buffer = NULL; |
1743 | | out->length = 0; |
1744 | | |
1745 | | if (secretSz == 0) { |
1746 | | WOLFSSL_MSG("Cookie secret must not be empty"); |
1747 | | return BAD_FUNC_ARG; |
1748 | | } |
1749 | | |
1750 | | if (secret == NULL && ssl->rng == NULL) { |
1751 | | WOLFSSL_MSG("Cookie secret generation requires an initialized RNG"); |
1752 | | return BAD_STATE_E; |
1753 | | } |
1754 | | |
1755 | | out->buffer = (byte*)XMALLOC(secretSz, ssl->heap, DYNAMIC_TYPE_COOKIE_PWD); |
1756 | | if (out->buffer == NULL) { |
1757 | | WOLFSSL_MSG("couldn't allocate new cookie secret"); |
1758 | | return MEMORY_ERROR; |
1759 | | } |
1760 | | out->length = secretSz; |
1761 | | |
1762 | | /* If the supplied secret is NULL, randomly generate a new secret. */ |
1763 | | if (secret != NULL) |
1764 | | XMEMCPY(out->buffer, secret, secretSz); |
1765 | | else { |
1766 | | ret = wc_RNG_GenerateBlock(ssl->rng, out->buffer, secretSz); |
1767 | | if (ret != 0) { |
1768 | | FreeCookieSecret(ssl, out); |
1769 | | return ret; |
1770 | | } |
1771 | | } |
1772 | | return 0; |
1773 | | } |
1774 | | |
1775 | | /* Install a secret built by NewCookieSecret(), freeing the one it replaces. |
1776 | | * Cannot fail. |
1777 | | * |
1778 | | * @param [in] ssl SSL/TLS object. |
1779 | | * @param [in, out] dst Secret in the SSL object to replace. |
1780 | | * @param [in, out] src New secret. Left empty. |
1781 | | * @param [in] name Name of dst, for the memory-zero check. |
1782 | | */ |
1783 | | static void InstallCookieSecret(WOLFSSL* ssl, buffer* dst, buffer* src, |
1784 | | const char* name) |
1785 | | { |
1786 | | FreeCookieSecret(ssl, dst); |
1787 | | dst->buffer = src->buffer; |
1788 | | dst->length = src->length; |
1789 | | src->buffer = NULL; |
1790 | | src->length = 0; |
1791 | | #ifdef WOLFSSL_CHECK_MEM_ZERO |
1792 | | wc_MemZero_Add(name, dst->buffer, dst->length); |
1793 | | #else |
1794 | | (void)name; |
1795 | | #endif |
1796 | | } |
1797 | | |
1798 | | /* Atomically replace one cookie secret. |
1799 | | * |
1800 | | * The replacement is built in its own allocation and only swapped in once it |
1801 | | * holds the new secret, so a failure leaves the secret the object already had |
1802 | | * in place rather than none at all. |
1803 | | * |
1804 | | * @param [in] ssl SSL/TLS object. |
1805 | | * @param [in, out] dst Secret in the SSL object to replace. |
1806 | | * @param [in] secret Secret data to copy, or NULL to generate one. |
1807 | | * @param [in] secretSz Length of the secret in bytes. Never 0. |
1808 | | * @param [in] name Name of dst, for the memory-zero check. |
1809 | | * @return 0 on success. |
1810 | | * @return MEMORY_ERROR on allocation failure. |
1811 | | * @return A negative error code when the RNG fails. The object is left as it |
1812 | | * was found. |
1813 | | */ |
1814 | | int SetCookieSecret(WOLFSSL* ssl, buffer* dst, const byte* secret, |
1815 | | word32 secretSz, const char* name) |
1816 | | { |
1817 | | buffer newSecret; |
1818 | | int ret; |
1819 | | |
1820 | | ret = NewCookieSecret(ssl, &newSecret, secret, secretSz); |
1821 | | if (ret != 0) |
1822 | | return ret; |
1823 | | |
1824 | | /* Swap: the old secret is only dropped once the new one is ready. */ |
1825 | | InstallCookieSecret(ssl, dst, &newSecret, name); |
1826 | | return 0; |
1827 | | } |
1828 | | |
1829 | | #endif /* (WOLFSSL_DTLS || WOLFSSL_SEND_HRR_COOKIE) && !NO_WOLFSSL_SERVER */ |
1830 | | |
1831 | | #if defined(WOLFSSL_DTLS) && !defined(NO_WOLFSSL_SERVER) |
1832 | | |
1833 | | /* Set the DTLS cookie secret used to generate HelloVerifyRequest cookies. |
1834 | | * |
1835 | | * When secret is NULL a new secret is randomly generated. The object's RNG |
1836 | | * must be initialized. This is not an SSL function. |
1837 | | * |
1838 | | * @param [in] ssl SSL/TLS object. |
1839 | | * @param [in] secret Cookie secret data, or NULL to generate one. |
1840 | | * @param [in] secretSz Length of secret in bytes, 0 to use the default. |
1841 | | * @return 0 on success. |
1842 | | * @return BAD_FUNC_ARG when ssl is NULL or secret is set with size 0. |
1843 | | * @return BAD_STATE_E when a secret must be generated and the RNG has been |
1844 | | * released. The secret in use is unchanged. |
1845 | | * @return MEMORY_ERROR on allocation failure. The secret in use is unchanged. |
1846 | | */ |
1847 | | int wolfSSL_DTLS_SetCookieSecret(WOLFSSL* ssl, |
1848 | | const byte* secret, word32 secretSz) |
1849 | | { |
1850 | | int ret; |
1851 | | |
1852 | | WOLFSSL_ENTER("wolfSSL_DTLS_SetCookieSecret"); |
1853 | | |
1854 | | if (ssl == NULL) { |
1855 | | WOLFSSL_MSG("need a SSL object"); |
1856 | | return BAD_FUNC_ARG; |
1857 | | } |
1858 | | |
1859 | | if (secret != NULL && secretSz == 0) { |
1860 | | WOLFSSL_MSG("can't have a new secret without a size"); |
1861 | | return BAD_FUNC_ARG; |
1862 | | } |
1863 | | |
1864 | | /* If secretSz is 0, use the default size. */ |
1865 | | if (secretSz == 0) |
1866 | | secretSz = COOKIE_SECRET_SZ; |
1867 | | |
1868 | | ret = SetCookieSecret(ssl, &ssl->buffers.dtlsCookieSecret, secret, secretSz, |
1869 | | "wolfSSL_DTLS_SetCookieSecret secret"); |
1870 | | |
1871 | | WOLFSSL_LEAVE("wolfSSL_DTLS_SetCookieSecret", ret); |
1872 | | return ret; |
1873 | | } |
1874 | | |
1875 | | struct chGoodDisableReadCbCtx { |
1876 | | ClientHelloGoodCb userCb; |
1877 | | void* userCtx; |
1878 | | }; |
1879 | | |
1880 | | /* ClientHello good callback that stops reading. |
1881 | | * |
1882 | | * Wraps the user's callback so that reading is disabled once a ClientHello |
1883 | | * with a valid cookie has been received. Used by wolfDTLS_accept_stateless(). |
1884 | | * |
1885 | | * @param [in] ssl SSL/TLS object. |
1886 | | * @param [in] ctx Context with user callback and its context. |
1887 | | * @return 0 on success or when no user callback set. |
1888 | | * @return Value returned by the user callback when negative. |
1889 | | */ |
1890 | | static int chGoodDisableReadCB(WOLFSSL* ssl, void* ctx) |
1891 | | { |
1892 | | struct chGoodDisableReadCbCtx* cb = (struct chGoodDisableReadCbCtx*)ctx; |
1893 | | int ret = 0; |
1894 | | if (cb->userCb != NULL) |
1895 | | ret = cb->userCb(ssl, cb->userCtx); |
1896 | | if (ret >= 0) |
1897 | | wolfSSL_SSLDisableRead(ssl); |
1898 | | return ret; |
1899 | | } |
1900 | | |
1901 | | /** |
1902 | | * Statelessly listen for a connection |
1903 | | * @param ssl The ssl object to use for listening to connections |
1904 | | * @return WOLFSSL_SUCCESS - ClientHello containing a valid cookie was received |
1905 | | * The connection can be continued with wolfSSL_accept |
1906 | | * WOLFSSL_FAILURE - The I/O layer returned WANT_READ. This is either |
1907 | | * because there is no data to read and we are using |
1908 | | * non-blocking sockets or we sent a cookie request |
1909 | | * and we are waiting for a reply. The user should |
1910 | | * call wolfDTLS_accept_stateless again after data |
1911 | | * becomes available in the I/O layer. |
1912 | | * WOLFSSL_FATAL_ERROR - A fatal error occurred. The ssl object should |
1913 | | * be free'd and allocated again to continue. |
1914 | | */ |
1915 | | int wolfDTLS_accept_stateless(WOLFSSL* ssl) |
1916 | | { |
1917 | | byte disableRead; |
1918 | | int ret = WC_NO_ERR_TRACE(WOLFSSL_FATAL_ERROR); |
1919 | | struct chGoodDisableReadCbCtx cb; |
1920 | | |
1921 | | WOLFSSL_ENTER("wolfDTLS_SetChGoodCb"); |
1922 | | |
1923 | | if (ssl == NULL) |
1924 | | return WOLFSSL_FATAL_ERROR; |
1925 | | |
1926 | | /* A general-purpose object is promoted by the wolfSSL_accept() below, as |
1927 | | * it has always been; only a client is rejected here. That is what |
1928 | | * wolfSSL_accept() itself does with one, so the outcome is unchanged. */ |
1929 | | if (ssl->options.side == WOLFSSL_CLIENT_END) { |
1930 | | WOLFSSL_MSG("wolfDTLS_accept_stateless cannot be used on a client"); |
1931 | | ssl->error = SIDE_ERROR; |
1932 | | WOLFSSL_ERROR(ssl->error); |
1933 | | return WOLFSSL_FATAL_ERROR; |
1934 | | } |
1935 | | |
1936 | | if (ssl->options.dtls && !ssl->options.sendCookie) { |
1937 | | WOLFSSL_MSG("wolfDTLS_accept_stateless requires cookies enabled; " |
1938 | | "use wolfSSL_accept"); |
1939 | | ssl->error = BAD_STATE_E; |
1940 | | WOLFSSL_ERROR(ssl->error); |
1941 | | return WOLFSSL_FATAL_ERROR; |
1942 | | } |
1943 | | |
1944 | | /* Save this to restore it later */ |
1945 | | disableRead = (byte)ssl->options.disableRead; |
1946 | | cb.userCb = ssl->chGoodCb; |
1947 | | cb.userCtx = ssl->chGoodCtx; |
1948 | | |
1949 | | /* Register our own callback so that we can disable reading */ |
1950 | | if (wolfDTLS_SetChGoodCb(ssl, chGoodDisableReadCB, &cb) != WOLFSSL_SUCCESS) |
1951 | | return WOLFSSL_FATAL_ERROR; |
1952 | | |
1953 | | ssl->options.returnOnGoodCh = 1; |
1954 | | ret = wolfSSL_accept(ssl); |
1955 | | ssl->options.returnOnGoodCh = 0; |
1956 | | /* restore user options */ |
1957 | | ssl->options.disableRead = disableRead; |
1958 | | (void)wolfDTLS_SetChGoodCb(ssl, cb.userCb, cb.userCtx); |
1959 | | if (ret == WOLFSSL_SUCCESS) { |
1960 | | WOLFSSL_MSG("should not happen. maybe the user called " |
1961 | | "wolfDTLS_accept_stateless instead of wolfSSL_accept"); |
1962 | | } |
1963 | | else if (ssl->error == WC_NO_ERR_TRACE(WANT_READ) || |
1964 | | ssl->error == WC_NO_ERR_TRACE(WANT_WRITE)) { |
1965 | | ssl->error = 0; |
1966 | | if (ssl->options.dtlsStateful) |
1967 | | ret = WOLFSSL_SUCCESS; |
1968 | | else |
1969 | | ret = WOLFSSL_FAILURE; |
1970 | | } |
1971 | | else { |
1972 | | ret = WOLFSSL_FATAL_ERROR; |
1973 | | } |
1974 | | return ret; |
1975 | | } |
1976 | | |
1977 | | /* Notify after cookie verification, or, with cookies disabled, after complete |
1978 | | * successful ClientHello processing (once per connection). When the cookie |
1979 | | * exchange is skipped on resumption, notify without cookie verification. For |
1980 | | * DTLS 1.3 this is before the PSK binder is checked. |
1981 | | * |
1982 | | * WC_NO_INLINE: wolfDTLS_accept_stateless passes the address of a stack-local |
1983 | | * context here; the restore call before return clears it again. Preventing |
1984 | | * inlining hides that cross-frame assignment from GCC's -Wdangling-pointer |
1985 | | * analysis, which otherwise flags a false positive on GCC 14+. |
1986 | | * |
1987 | | * @param [in] ssl SSL/TLS object. |
1988 | | * @param [in] cb Callback to call. NULL to clear. |
1989 | | * @param [in] user_ctx Context to pass to the callback. |
1990 | | * @return WOLFSSL_SUCCESS on success. |
1991 | | * @return BAD_FUNC_ARG when ssl is NULL. |
1992 | | */ |
1993 | | WC_NO_INLINE |
1994 | | int wolfDTLS_SetChGoodCb(WOLFSSL* ssl, ClientHelloGoodCb cb, void* user_ctx) |
1995 | | { |
1996 | | WOLFSSL_ENTER("wolfDTLS_SetChGoodCb"); |
1997 | | |
1998 | | if (ssl == NULL) |
1999 | | return BAD_FUNC_ARG; |
2000 | | |
2001 | | ssl->chGoodCb = cb; |
2002 | | ssl->chGoodCtx = user_ctx; |
2003 | | |
2004 | | return WOLFSSL_SUCCESS; |
2005 | | } |
2006 | | |
2007 | | /* Notify the ClientHello good callback when cookies are disabled. |
2008 | | * |
2009 | | * Called by the accept functions once a complete first ClientHello has been |
2010 | | * processed. With cookies enabled the stateless ClientHello processing calls |
2011 | | * it instead. A negative return stops the accept call before its state |
2012 | | * advances, so the next accept call asks again. |
2013 | | * |
2014 | | * @param [in, out] ssl SSL/TLS object. |
2015 | | * @return 0 when the callback is not called or succeeds. |
2016 | | * @return The callback's negative error code otherwise. |
2017 | | */ |
2018 | | int DtlsNoCookieChGood(WOLFSSL* ssl) |
2019 | | { |
2020 | | int ret = 0; |
2021 | | |
2022 | | if (ssl->options.dtls && !ssl->options.sendCookie && |
2023 | | ssl->chGoodCb != NULL) { |
2024 | | ret = ssl->chGoodCb(ssl, ssl->chGoodCtx); |
2025 | | if (ret > 0) |
2026 | | ret = 0; |
2027 | | } |
2028 | | |
2029 | | return ret; |
2030 | | } |
2031 | | |
2032 | | /* Set a secondary DTLS 1.2 cookie secret used only when verifying a received |
2033 | | * HelloVerifyRequest cookie, and only if the primary secret (set by |
2034 | | * wolfSSL_DTLS_SetCookieSecret()) fails to verify it. |
2035 | | * |
2036 | | * This supports an application-driven cookie-secret rotation on a stateless |
2037 | | * server: after rotating the primary secret, install the previous secret here |
2038 | | * so that cookies already issued under it are still accepted for an overlap |
2039 | | * window. It is never used to issue cookies. |
2040 | | * |
2041 | | * This is the DTLS 1.2 counterpart of wolfSSL_set_hrr_cookie_secret_secondary() |
2042 | | * (which covers the DTLS 1.3 HelloRetryRequest cookie); the two cookie |
2043 | | * mechanisms keep separate secrets, so a server that handles both versions sets |
2044 | | * both secondary secrets. |
2045 | | * |
2046 | | * @param [in] ssl SSL/TLS object. |
2047 | | * @param [in] secret Secondary secret to verify cookies against. A value |
2048 | | * of NULL (or a secretSz of 0) clears any previously set |
2049 | | * secondary secret. |
2050 | | * @param [in] secretSz Size of secret data in bytes. |
2051 | | * @return 0 on success. |
2052 | | * @return BAD_FUNC_ARG when ssl is NULL. |
2053 | | * @return MEMORY_ERROR on allocation failure. The secondary secret in use is |
2054 | | * unchanged. |
2055 | | */ |
2056 | | int wolfSSL_DTLS_SetCookieSecretSecondary(WOLFSSL* ssl, |
2057 | | const byte* secret, word32 secretSz) |
2058 | | { |
2059 | | int ret; |
2060 | | |
2061 | | WOLFSSL_ENTER("wolfSSL_DTLS_SetCookieSecretSecondary"); |
2062 | | |
2063 | | if (ssl == NULL) { |
2064 | | WOLFSSL_MSG("need a SSL object"); |
2065 | | return BAD_FUNC_ARG; |
2066 | | } |
2067 | | |
2068 | | /* A NULL/empty secret just clears the secondary secret. */ |
2069 | | if (secret == NULL || secretSz == 0) { |
2070 | | FreeCookieSecret(ssl, &ssl->buffers.dtlsCookieSecretSecondary); |
2071 | | WOLFSSL_LEAVE("wolfSSL_DTLS_SetCookieSecretSecondary", 0); |
2072 | | return 0; |
2073 | | } |
2074 | | |
2075 | | ret = SetCookieSecret(ssl, &ssl->buffers.dtlsCookieSecretSecondary, secret, |
2076 | | secretSz, "wolfSSL_DTLS_SetCookieSecretSecondary " |
2077 | | "secret"); |
2078 | | |
2079 | | WOLFSSL_LEAVE("wolfSSL_DTLS_SetCookieSecretSecondary", ret); |
2080 | | return ret; |
2081 | | } |
2082 | | |
2083 | | #endif /* WOLFSSL_DTLS && !NO_WOLFSSL_SERVER */ |
2084 | | |
2085 | | #if (defined(WOLFSSL_DTLS) || defined(WOLFSSL_SEND_HRR_COOKIE)) && \ |
2086 | | !defined(NO_WOLFSSL_SERVER) && !defined(NO_TLS) |
2087 | | static int CheckCookieSide(WOLFSSL* ssl) |
2088 | | { |
2089 | | if (ssl == NULL) |
2090 | | return BAD_FUNC_ARG; |
2091 | | #ifdef WOLFSSL_DTLS |
2092 | | if (!ssl->options.dtls) |
2093 | | #endif |
2094 | | { |
2095 | | #ifdef WOLFSSL_SEND_HRR_COOKIE |
2096 | | if (!IsAtLeastTLSv1_3(ssl->version)) |
2097 | | #endif |
2098 | | return BAD_FUNC_ARG; |
2099 | | } |
2100 | | /* A general-purpose object from DTLS_method()/TLS_method() has not chosen |
2101 | | * a side yet. The policy it is given here survives the promotion to the |
2102 | | * server side - see InitSSL() - so only a client is rejected, which is |
2103 | | * what wolfSSL_send_hrr_cookie() and wolfSSL_disable_hrr_cookie() have |
2104 | | * always done. */ |
2105 | | if (ssl->options.side == WOLFSSL_CLIENT_END) |
2106 | | return SIDE_ERROR; |
2107 | | return WOLFSSL_SUCCESS; |
2108 | | } |
2109 | | |
2110 | | /* Reject a cookie policy change once the DTLS handshake has committed to |
2111 | | * stateful processing, when it can no longer take effect. |
2112 | | * |
2113 | | * @param [in] ssl SSL/TLS object. |
2114 | | * @return WOLFSSL_SUCCESS when the policy can still be changed. |
2115 | | * @return BAD_STATE_E when the handshake has already committed. |
2116 | | */ |
2117 | | int CheckCookieState(WOLFSSL* ssl) |
2118 | | { |
2119 | | #ifdef WOLFSSL_DTLS |
2120 | | if (ssl->options.dtls && ssl->options.dtlsStateful) { |
2121 | | WOLFSSL_MSG("Cookie policy is fixed once the handshake starts"); |
2122 | | return BAD_STATE_E; |
2123 | | } |
2124 | | #else |
2125 | | (void)ssl; |
2126 | | #endif |
2127 | | return WOLFSSL_SUCCESS; |
2128 | | } |
2129 | | |
2130 | | int wolfSSL_disable_cookie(WOLFSSL* ssl) |
2131 | | { |
2132 | | int ret; |
2133 | | |
2134 | | WOLFSSL_ENTER("wolfSSL_disable_cookie"); |
2135 | | |
2136 | | ret = CheckCookieSide(ssl); |
2137 | | if (ret != WOLFSSL_SUCCESS) |
2138 | | return ret; |
2139 | | ret = CheckCookieState(ssl); |
2140 | | if (ret != WOLFSSL_SUCCESS) |
2141 | | return ret; |
2142 | | |
2143 | | #ifdef WOLFSSL_DTLS |
2144 | | if (ssl->options.dtls) { |
2145 | | FreeCookieSecret(ssl, &ssl->buffers.dtlsCookieSecret); |
2146 | | FreeCookieSecret(ssl, &ssl->buffers.dtlsCookieSecretSecondary); |
2147 | | } |
2148 | | #endif |
2149 | | #ifdef WOLFSSL_SEND_HRR_COOKIE |
2150 | | FreeCookieSecret(ssl, &ssl->buffers.tls13CookieSecret); |
2151 | | FreeCookieSecret(ssl, &ssl->buffers.tls13CookieSecretSecondary); |
2152 | | #endif |
2153 | | ssl->options.sendCookie = 0; |
2154 | | return WOLFSSL_SUCCESS; |
2155 | | } |
2156 | | |
2157 | | #ifdef WOLFSSL_SEND_HRR_COOKIE |
2158 | | /* Default HelloRetryRequest cookie secret size: the digest the cookie MAC is |
2159 | | * built with. */ |
2160 | | #ifndef NO_SHA256 |
2161 | | #define TLS13_COOKIE_SECRET_SZ WC_SHA256_DIGEST_SIZE |
2162 | | #elif defined(WOLFSSL_SHA384) |
2163 | | #define TLS13_COOKIE_SECRET_SZ WC_SHA384_DIGEST_SIZE |
2164 | | #elif defined(WOLFSSL_TLS13_SHA512) |
2165 | | #define TLS13_COOKIE_SECRET_SZ WC_SHA512_DIGEST_SIZE |
2166 | | #elif defined(WOLFSSL_SM3) |
2167 | | #define TLS13_COOKIE_SECRET_SZ WC_SM3_DIGEST_SIZE |
2168 | | #else |
2169 | | #error "No digest to available to use with HMAC for cookies." |
2170 | | #endif |
2171 | | #endif /* WOLFSSL_SEND_HRR_COOKIE */ |
2172 | | |
2173 | | /* Turn the cookie policy on, optionally replacing the HelloRetryRequest |
2174 | | * cookie secret. |
2175 | | * |
2176 | | * Every secret the policy needs, including the DTLS 1.2 fallback secret of a |
2177 | | * DTLS 1.3 server, is built before any is installed, so a failure leaves the |
2178 | | * object as it was found. The handshake state is not checked here: the public |
2179 | | * entry points check it, and InitSSL_DtlsServer() runs before it matters. |
2180 | | * |
2181 | | * @param [in, out] ssl SSL/TLS object. |
2182 | | * @param [in] hrrSecret HRR cookie secret to install, or NULL to |
2183 | | * generate one. |
2184 | | * @param [in] hrrSecretSz Length of hrrSecret in bytes, 0 for the |
2185 | | * default size. |
2186 | | * @param [in] replaceHrr Replace an HRR secret that is already set. |
2187 | | * When 0, only missing secrets are generated and |
2188 | | * application-supplied secrets are kept. |
2189 | | * @return WOLFSSL_SUCCESS on success. |
2190 | | * @return BAD_FUNC_ARG when ssl is NULL or the protocol has no cookie. |
2191 | | * @return SIDE_ERROR when called on a client. |
2192 | | * @return A negative error code when a secret cannot be generated. The |
2193 | | * object is left as it was found. |
2194 | | */ |
2195 | | int CookiePolicySet(WOLFSSL* ssl, const byte* hrrSecret, word32 hrrSecretSz, |
2196 | | int replaceHrr) |
2197 | | { |
2198 | | int ret; |
2199 | | #ifdef WOLFSSL_SEND_HRR_COOKIE |
2200 | | buffer newTls13Secret; |
2201 | | #endif |
2202 | | #ifdef WOLFSSL_DTLS |
2203 | | buffer newDtlsSecret; |
2204 | | #endif |
2205 | | |
2206 | | #ifdef WOLFSSL_SEND_HRR_COOKIE |
2207 | | XMEMSET(&newTls13Secret, 0, sizeof(newTls13Secret)); |
2208 | | #endif |
2209 | | #ifdef WOLFSSL_DTLS |
2210 | | XMEMSET(&newDtlsSecret, 0, sizeof(newDtlsSecret)); |
2211 | | #endif |
2212 | | |
2213 | | ret = CheckCookieSide(ssl); |
2214 | | if (ret != WOLFSSL_SUCCESS) |
2215 | | return ret; |
2216 | | |
2217 | | /* Prepare. Every step that can fail runs here. */ |
2218 | | #ifdef WOLFSSL_SEND_HRR_COOKIE |
2219 | | if (IsAtLeastTLSv1_3(ssl->version) && |
2220 | | (replaceHrr || ssl->buffers.tls13CookieSecret.buffer == NULL)) { |
2221 | | ret = NewCookieSecret(ssl, &newTls13Secret, hrrSecret, |
2222 | | (hrrSecretSz != 0) ? hrrSecretSz |
2223 | | : (word32)TLS13_COOKIE_SECRET_SZ); |
2224 | | if (ret != 0) |
2225 | | goto cleanup; |
2226 | | } |
2227 | | #endif |
2228 | | #ifdef WOLFSSL_DTLS |
2229 | | /* DTLS 1.3 also needs this secret for DTLS 1.2 fallback. */ |
2230 | | if (ssl->options.dtls && ssl->buffers.dtlsCookieSecret.buffer == NULL) { |
2231 | | ret = NewCookieSecret(ssl, &newDtlsSecret, NULL, COOKIE_SECRET_SZ); |
2232 | | if (ret != 0) |
2233 | | goto cleanup; |
2234 | | } |
2235 | | #endif |
2236 | | |
2237 | | /* Install. Nothing below this point can fail. */ |
2238 | | #ifdef WOLFSSL_SEND_HRR_COOKIE |
2239 | | if (newTls13Secret.buffer != NULL) { |
2240 | | InstallCookieSecret(ssl, &ssl->buffers.tls13CookieSecret, |
2241 | | &newTls13Secret, "tls13CookieSecret"); |
2242 | | } |
2243 | | #endif |
2244 | | #ifdef WOLFSSL_DTLS |
2245 | | if (newDtlsSecret.buffer != NULL) { |
2246 | | InstallCookieSecret(ssl, &ssl->buffers.dtlsCookieSecret, |
2247 | | &newDtlsSecret, "dtlsCookieSecret"); |
2248 | | } |
2249 | | #endif |
2250 | | ssl->options.sendCookie = 1; |
2251 | | |
2252 | | (void)hrrSecret; |
2253 | | (void)hrrSecretSz; |
2254 | | (void)replaceHrr; |
2255 | | return WOLFSSL_SUCCESS; |
2256 | | |
2257 | | cleanup: |
2258 | | /* Only the secrets this call built are dropped. */ |
2259 | | #ifdef WOLFSSL_SEND_HRR_COOKIE |
2260 | | FreeCookieSecret(ssl, &newTls13Secret); |
2261 | | #endif |
2262 | | #ifdef WOLFSSL_DTLS |
2263 | | FreeCookieSecret(ssl, &newDtlsSecret); |
2264 | | #endif |
2265 | | return ret; |
2266 | | } |
2267 | | |
2268 | | /* Prepare missing secrets without replacing application-supplied secrets. |
2269 | | * |
2270 | | * @param [in, out] ssl SSL/TLS object. |
2271 | | * @return The result of CookiePolicySet(). |
2272 | | */ |
2273 | | int CookiePolicyEnable(WOLFSSL* ssl) |
2274 | | { |
2275 | | return CookiePolicySet(ssl, NULL, 0, 0); |
2276 | | } |
2277 | | |
2278 | | int wolfSSL_enable_cookie(WOLFSSL* ssl) |
2279 | | { |
2280 | | int ret; |
2281 | | |
2282 | | WOLFSSL_ENTER("wolfSSL_enable_cookie"); |
2283 | | |
2284 | | ret = CheckCookieSide(ssl); |
2285 | | if (ret != WOLFSSL_SUCCESS) |
2286 | | return ret; |
2287 | | ret = CheckCookieState(ssl); |
2288 | | if (ret != WOLFSSL_SUCCESS) |
2289 | | return ret; |
2290 | | |
2291 | | return CookiePolicyEnable(ssl); |
2292 | | } |
2293 | | #endif /* (WOLFSSL_DTLS || WOLFSSL_SEND_HRR_COOKIE) && !NO_WOLFSSL_SERVER && */ |
2294 | | /* !NO_TLS */ |
2295 | | |
2296 | | #endif /* !WOLFCRYPT_ONLY */ |
2297 | | |
2298 | | #endif /* !WOLFSSL_SSL_API_DTLS_INCLUDED */ |