]>
Commit | Line | Data |
---|---|---|
a58eb06d | 1 | /* |
da1c088f | 2 | * Copyright 2016-2023 The OpenSSL Project Authors. All Rights Reserved. |
a58eb06d | 3 | * |
909f1a2e | 4 | * Licensed under the Apache License 2.0 (the "License"); |
a58eb06d TS |
5 | * you may not use this file except in compliance with the License. |
6 | * You may obtain a copy of the License at | |
7 | * https://www.openssl.org/source/license.html | |
8 | * or in the file LICENSE in the source distribution. | |
9 | */ | |
10 | ||
11 | #include <string.h> | |
12 | #include <openssl/ssl.h> | |
13 | #include <openssl/bio.h> | |
14 | #include <openssl/err.h> | |
15 | ||
ee05588d MC |
16 | /* We include internal headers so we can check if the buffers are allocated */ |
17 | #include "../ssl/ssl_local.h" | |
18 | #include "../ssl/record/record_local.h" | |
ca20f61f | 19 | #include "internal/recordmethod.h" |
ee05588d MC |
20 | #include "../ssl/record/methods/recmethod_local.h" |
21 | ||
0d345f0e | 22 | #include "internal/packet.h" |
a58eb06d | 23 | |
20f8bc72 | 24 | #include "helpers/ssltestlib.h" |
a58eb06d TS |
25 | #include "testutil.h" |
26 | ||
27 | struct async_ctrs { | |
28 | unsigned int rctr; | |
29 | unsigned int wctr; | |
30 | }; | |
31 | ||
32 | static SSL_CTX *serverctx = NULL; | |
33 | static SSL_CTX *clientctx = NULL; | |
34 | ||
35 | #define MAX_ATTEMPTS 100 | |
36 | ||
ee05588d MC |
37 | static int checkbuffers(SSL *s, int isalloced) |
38 | { | |
39 | SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s); | |
40 | OSSL_RECORD_LAYER *rrl = sc->rlayer.rrl; | |
41 | OSSL_RECORD_LAYER *wrl = sc->rlayer.wrl; | |
42 | ||
43 | if (isalloced) | |
44 | return rrl->rbuf.buf != NULL && wrl->wbuf[0].buf != NULL; | |
45 | ||
46 | return rrl->rbuf.buf == NULL && wrl->wbuf[0].buf == NULL; | |
47 | } | |
a58eb06d TS |
48 | |
49 | /* | |
50 | * There are 9 passes in the tests | |
51 | * 0 = control test | |
52 | * tests during writes | |
53 | * 1 = free buffers | |
54 | * 2 = + allocate buffers after free | |
55 | * 3 = + allocate buffers again | |
56 | * 4 = + free buffers after allocation | |
57 | * tests during reads | |
58 | * 5 = + free buffers | |
59 | * 6 = + free buffers again | |
60 | * 7 = + allocate buffers after free | |
61 | * 8 = + free buffers after allocation | |
62 | */ | |
63 | static int test_func(int test) | |
64 | { | |
65 | int result = 0; | |
66 | SSL *serverssl = NULL, *clientssl = NULL; | |
67 | int ret; | |
68 | size_t i, j; | |
69 | const char testdata[] = "Test data"; | |
70 | char buf[sizeof(testdata)]; | |
71 | ||
72 | if (!TEST_true(create_ssl_objects(serverctx, clientctx, &serverssl, &clientssl, | |
73 | NULL, NULL))) { | |
74 | TEST_error("Test %d failed: Create SSL objects failed\n", test); | |
75 | goto end; | |
76 | } | |
77 | ||
78 | if (!TEST_true(create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE))) { | |
79 | TEST_error("Test %d failed: Create SSL connection failed\n", test); | |
80 | goto end; | |
81 | } | |
82 | ||
83 | /* | |
84 | * Send and receive some test data. Do the whole thing twice to ensure | |
85 | * we hit at least one async event in both reading and writing | |
86 | */ | |
87 | for (j = 0; j < 2; j++) { | |
88 | int len; | |
89 | ||
90 | /* | |
91 | ||
92 | * Write some test data. It should never take more than 2 attempts | |
93 | * (the first one might be a retryable fail). | |
94 | */ | |
95 | for (ret = -1, i = 0, len = 0; len != sizeof(testdata) && i < 2; | |
96 | i++) { | |
97 | /* test == 0 mean to free/allocate = control */ | |
ee05588d MC |
98 | if (test >= 1 && (!TEST_true(SSL_free_buffers(clientssl)) |
99 | || !TEST_true(checkbuffers(clientssl, 0)))) | |
a58eb06d | 100 | goto end; |
ee05588d MC |
101 | if (test >= 2 && (!TEST_true(SSL_alloc_buffers(clientssl)) |
102 | || !TEST_true(checkbuffers(clientssl, 1)))) | |
a58eb06d TS |
103 | goto end; |
104 | /* allocate a second time */ | |
ee05588d MC |
105 | if (test >= 3 && (!TEST_true(SSL_alloc_buffers(clientssl)) |
106 | || !TEST_true(checkbuffers(clientssl, 1)))) | |
a58eb06d | 107 | goto end; |
ee05588d MC |
108 | if (test >= 4 && (!TEST_true(SSL_free_buffers(clientssl)) |
109 | || !TEST_true(checkbuffers(clientssl, 0)))) | |
a58eb06d TS |
110 | goto end; |
111 | ||
112 | ret = SSL_write(clientssl, testdata + len, | |
113 | sizeof(testdata) - len); | |
114 | if (ret > 0) { | |
115 | len += ret; | |
116 | } else { | |
117 | int ssl_error = SSL_get_error(clientssl, ret); | |
118 | ||
119 | if (ssl_error == SSL_ERROR_SYSCALL || | |
120 | ssl_error == SSL_ERROR_SSL) { | |
121 | TEST_error("Test %d failed: Failed to write app data\n", test); | |
122 | goto end; | |
123 | } | |
124 | } | |
125 | } | |
126 | if (!TEST_size_t_eq(len, sizeof(testdata))) | |
127 | goto end; | |
128 | /* | |
bdcacd93 | 129 | * Now read the test data. It may take more attempts here because |
a58eb06d TS |
130 | * it could fail once for each byte read, including all overhead |
131 | * bytes from the record header/padding etc. | |
132 | */ | |
133 | for (ret = -1, i = 0, len = 0; len != sizeof(testdata) && | |
134 | i < MAX_ATTEMPTS; i++) | |
135 | { | |
ee05588d MC |
136 | if (test >= 5 && (!TEST_true(SSL_free_buffers(serverssl)) |
137 | || !TEST_true(checkbuffers(serverssl, 0)))) | |
a58eb06d TS |
138 | goto end; |
139 | /* free a second time */ | |
ee05588d MC |
140 | if (test >= 6 && (!TEST_true(SSL_free_buffers(serverssl)) |
141 | || !TEST_true(checkbuffers(serverssl, 0)))) | |
a58eb06d | 142 | goto end; |
ee05588d MC |
143 | if (test >= 7 && (!TEST_true(SSL_alloc_buffers(serverssl)) |
144 | || !TEST_true(checkbuffers(serverssl, 1)))) | |
a58eb06d | 145 | goto end; |
ee05588d MC |
146 | if (test >= 8 && (!TEST_true(SSL_free_buffers(serverssl)) |
147 | || !TEST_true(checkbuffers(serverssl, 0)))) | |
a58eb06d TS |
148 | goto end; |
149 | ||
150 | ret = SSL_read(serverssl, buf + len, sizeof(buf) - len); | |
151 | if (ret > 0) { | |
152 | len += ret; | |
153 | } else { | |
154 | int ssl_error = SSL_get_error(serverssl, ret); | |
155 | ||
156 | if (ssl_error == SSL_ERROR_SYSCALL || | |
157 | ssl_error == SSL_ERROR_SSL) { | |
158 | TEST_error("Test %d failed: Failed to read app data\n", test); | |
159 | goto end; | |
160 | } | |
161 | } | |
162 | } | |
163 | if (!TEST_mem_eq(buf, len, testdata, sizeof(testdata))) | |
164 | goto end; | |
165 | } | |
166 | ||
167 | result = 1; | |
168 | end: | |
169 | if (!result) | |
170 | ERR_print_errors_fp(stderr); | |
171 | ||
172 | SSL_free(clientssl); | |
173 | SSL_free(serverssl); | |
174 | ||
175 | return result; | |
176 | } | |
177 | ||
a43ce58f SL |
178 | OPT_TEST_DECLARE_USAGE("certfile privkeyfile\n") |
179 | ||
43405889 P |
180 | int setup_tests(void) |
181 | { | |
182 | char *cert, *pkey; | |
183 | ||
8d242823 MC |
184 | if (!test_skip_common_options()) { |
185 | TEST_error("Error parsing test options\n"); | |
186 | return 0; | |
187 | } | |
188 | ||
43405889 P |
189 | if (!TEST_ptr(cert = test_get_argument(0)) |
190 | || !TEST_ptr(pkey = test_get_argument(1))) | |
191 | return 0; | |
a58eb06d | 192 | |
5e30f2fd | 193 | if (!create_ssl_ctx_pair(NULL, TLS_server_method(), TLS_client_method(), |
5c587fb6 | 194 | TLS1_VERSION, 0, |
43405889 | 195 | &serverctx, &clientctx, cert, pkey)) { |
a58eb06d | 196 | TEST_error("Failed to create SSL_CTX pair\n"); |
43405889 | 197 | return 0; |
a58eb06d TS |
198 | } |
199 | ||
200 | ADD_ALL_TESTS(test_func, 9); | |
43405889 P |
201 | return 1; |
202 | } | |
a58eb06d | 203 | |
43405889 P |
204 | void cleanup_tests(void) |
205 | { | |
a58eb06d TS |
206 | SSL_CTX_free(clientctx); |
207 | SSL_CTX_free(serverctx); | |
a58eb06d | 208 | } |