6bb1db40534ad8498681dfda7c394e455b005e4c
[openssl.git] / test / recordlentest.c
1 /*
2  * Copyright 2017 The OpenSSL Project Authors. All Rights Reserved.
3  *
4  * Licensed under the OpenSSL license (the "License").  You may not use
5  * this file except in compliance with the License.  You can obtain a copy
6  * in the file LICENSE in the source distribution or at
7  * https://www.openssl.org/source/license.html
8  */
9
10 #include <string.h>
11
12 #include "ssltestlib.h"
13 #include "testutil.h"
14 #include "test_main_custom.h"
15
16 static char *cert = NULL;
17 static char *privkey = NULL;
18
19 #define TEST_PLAINTEXT_OVERFLOW_OK      0
20 #define TEST_PLAINTEXT_OVERFLOW_NOT_OK  1
21 #define TEST_ENCRYPTED_OVERFLOW_TLS1_3_OK       2
22 #define TEST_ENCRYPTED_OVERFLOW_TLS1_3_NOT_OK   3
23 #define TEST_ENCRYPTED_OVERFLOW_TLS1_2_OK       4
24 #define TEST_ENCRYPTED_OVERFLOW_TLS1_2_NOT_OK   5
25
26 #define TOTAL_RECORD_OVERFLOW_TESTS 6
27
28 static int write_record(BIO *b, size_t len, int rectype, int recversion)
29 {
30     unsigned char header[SSL3_RT_HEADER_LENGTH];
31     size_t written;
32     unsigned char buf[256];
33
34     memset(buf, 0, sizeof(buf));
35
36     header[0] = rectype;
37     header[1] = (recversion >> 8) & 0xff;
38     header[2] = recversion & 0xff;
39     header[3] = (len >> 8) & 0xff;
40     header[4] = len & 0xff;
41
42     if (!BIO_write_ex(b, header, SSL3_RT_HEADER_LENGTH, &written)
43             || written != SSL3_RT_HEADER_LENGTH)
44         return 0;
45
46     while (len > 0) {
47         size_t outlen;
48
49         if (len > sizeof(buf))
50             outlen = sizeof(buf);
51         else
52             outlen = len;
53
54         if (!BIO_write_ex(b, buf, outlen, &written)
55                 || written != outlen)
56             return 0;
57
58         len -= outlen;
59     }
60
61     return 1;
62 }
63
64 static int fail_due_to_record_overflow(int enc)
65 {
66     long err = ERR_peek_error();
67     int reason;
68
69     if (enc)
70         reason = SSL_R_ENCRYPTED_LENGTH_TOO_LONG;
71     else
72         reason = SSL_R_DATA_LENGTH_TOO_LONG;
73
74     if (ERR_GET_LIB(err) == ERR_LIB_SSL
75             && ERR_GET_REASON(err) == reason)
76         return 1;
77
78     return 0;
79 }
80
81 static int test_record_plain_overflow(int idx)
82 {
83     SSL_CTX *cctx = NULL, *sctx = NULL;
84     SSL *clientssl = NULL, *serverssl = NULL;
85     int testresult = 0;
86     size_t len = 0;
87     size_t written;
88     int overf_expected;
89     unsigned char buf;
90     BIO *serverbio;
91     int recversion;
92
93 #ifdef OPENSSL_NO_TLS1_2
94     if (idx == TEST_ENCRYPTED_OVERFLOW_TLS1_2_OK
95             || idx == TEST_ENCRYPTED_OVERFLOW_TLS1_2_NOT_OK)
96         return 1;
97 #endif
98 #ifdef OPENSSL_NO_TLS1_3
99     if (idx == TEST_ENCRYPTED_OVERFLOW_TLS1_3_OK
100             || idx == TEST_ENCRYPTED_OVERFLOW_TLS1_3_NOT_OK)
101         return 1;
102 #endif
103
104     ERR_clear_error();
105
106     if (!create_ssl_ctx_pair(TLS_server_method(), TLS_client_method(), &sctx,
107                              &cctx, cert, privkey)) {
108         printf("Unable to create SSL_CTX pair\n");
109         goto end;
110     }
111
112     if (idx == TEST_ENCRYPTED_OVERFLOW_TLS1_2_OK
113             || idx == TEST_ENCRYPTED_OVERFLOW_TLS1_2_NOT_OK) {
114         len = SSL3_RT_MAX_ENCRYPTED_LENGTH - SSL3_RT_MAX_COMPRESSED_OVERHEAD;
115         SSL_CTX_set_max_proto_version(sctx, TLS1_2_VERSION);
116     } else if (idx == TEST_ENCRYPTED_OVERFLOW_TLS1_3_OK
117                || idx == TEST_ENCRYPTED_OVERFLOW_TLS1_3_NOT_OK) {
118         len = SSL3_RT_MAX_TLS13_ENCRYPTED_LENGTH;
119     }
120
121     if (!create_ssl_objects(sctx, cctx, &serverssl, &clientssl, NULL, NULL)) {
122         printf("Unable to create SSL objects\n");
123         goto end;
124     }
125
126     serverbio = SSL_get_rbio(serverssl);
127
128     if (idx == TEST_PLAINTEXT_OVERFLOW_OK
129             || idx == TEST_PLAINTEXT_OVERFLOW_NOT_OK) {
130         len = SSL3_RT_MAX_PLAIN_LENGTH;
131
132         if (idx == TEST_PLAINTEXT_OVERFLOW_NOT_OK)
133             len++;
134
135         if (!write_record(serverbio, len, SSL3_RT_HANDSHAKE, TLS1_VERSION)) {
136             printf("Unable to write plaintext record\n");
137             goto end;
138         }
139
140         if (SSL_accept(serverssl) > 0) {
141             printf("Unexpected success reading plaintext record\n");
142             goto end;
143         }
144
145         overf_expected = (idx == TEST_PLAINTEXT_OVERFLOW_OK) ? 0 : 1;
146         if (fail_due_to_record_overflow(0) != overf_expected) {
147             printf("Unexpected error value received\n");
148             goto end;
149         }
150
151         goto success;
152     }
153
154     if (!create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE)) {
155         printf("Unable to create SSL connection\n");
156         goto end;
157     }
158
159     if (idx == TEST_ENCRYPTED_OVERFLOW_TLS1_2_NOT_OK
160             || idx == TEST_ENCRYPTED_OVERFLOW_TLS1_3_NOT_OK) {
161         overf_expected = 1;
162         len++;
163     } else {
164         overf_expected = 0;
165     }
166
167     if (idx == TEST_ENCRYPTED_OVERFLOW_TLS1_3_OK
168             || idx == TEST_ENCRYPTED_OVERFLOW_TLS1_3_NOT_OK)
169         recversion = TLS1_VERSION;
170     else
171         recversion = TLS1_2_VERSION;
172
173     if (!write_record(serverbio, len, SSL3_RT_APPLICATION_DATA, recversion)) {
174         printf("Unable to write encryprted record\n");
175         goto end;
176     }
177
178     if (SSL_read_ex(serverssl, &buf, sizeof(buf), &written)) {
179         printf("Unexpected success reading encrypted record\n");
180         goto end;
181     }
182
183     if (fail_due_to_record_overflow(1) != overf_expected) {
184         printf("Unexpected error value received\n");
185         goto end;
186     }
187
188  success:
189     testresult = 1;
190
191  end:
192     if(!testresult)
193         ERR_print_errors_fp(stdout);
194     SSL_free(serverssl);
195     SSL_free(clientssl);
196     SSL_CTX_free(sctx);
197     SSL_CTX_free(cctx);
198
199     return testresult;
200 }
201
202 int test_main(int argc, char *argv[])
203 {
204     int testresult = 1;
205
206     if (argc != 3) {
207         printf("Invalid argument count\n");
208         return 1;
209     }
210
211     cert = argv[1];
212     privkey = argv[2];
213
214     ADD_ALL_TESTS(test_record_plain_overflow, TOTAL_RECORD_OVERFLOW_TESTS);
215
216     testresult = run_tests(argv[0]);
217
218     bio_s_mempacket_test_free();
219
220     return testresult;
221 }