-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathssl_client_hello_read.c
128 lines (104 loc) · 3.55 KB
/
ssl_client_hello_read.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
#include <stdlib.h>
#include <string.h>
#include "ssl_client_hello.h"
/* Consume a byte from the data stream
*/
#define CONSUME_BYTES( len ) data_ptr += (len); data_len -= (len);
#define CONSUME_BYTE() CONSUME_BYTES( 1 )
/*
* Parse an SSL2 Client Hello message
*
* Parameters:
* hello_data Pointer to the hello data structure to be filled
* data_ptr pointer to the data stream to parse
* data_len how much data is there to parse
* message if we encounter an error, will be updated to point to
* an error message
*
* Return:
* 0 No problem encountered
* -1 A problem was encountered parsing the client hello
*/
int ssl2_client_hello_read(struct ssl2_client_hello_data *hello_data, uint8_t * data_ptr, uint32_t data_len, char **message)
{
uint16_t challenge_len;
if (hello_data == NULL) {
*message = "NULL hello_data passed to ssl2_client_hello_read";
return -1;
}
if (data_len < 8) {
*message = "SSL2 Client Hello record is too short";
return -1;
}
hello_data->client_version = (*data_ptr << 8);
CONSUME_BYTE();
hello_data->client_version |= *data_ptr;
CONSUME_BYTE();
hello_data->cipher_specs_len = (*data_ptr << 8);
CONSUME_BYTE();
hello_data->cipher_specs_len |= *data_ptr;
CONSUME_BYTE();
if (hello_data->cipher_specs_len % 3) {
*message = "SSL2 Client Hello record has invalid cipher spec length";
return -1;
}
if (hello_data->cipher_specs_len > MAX_CIPHER_SPECS_LEN) {
*message = "SSL2 Client Hello record has too large a cipher spec length";
return -1;
}
hello_data->session_id_len = (*data_ptr << 8);
CONSUME_BYTE();
hello_data->session_id_len |= *data_ptr;
CONSUME_BYTE();
if (hello_data->session_id_len != 0 && hello_data->session_id_len != 16) {
*message = "SSL2 Client Hello record has invalid session id length";
return -1;
}
challenge_len = (*data_ptr << 8);
CONSUME_BYTE();
challenge_len |= *data_ptr;
CONSUME_BYTE();
if (challenge_len < 16 || challenge_len > 32) {
*message = "SSL2 Client Hello record has invalid session id length";
return -1;
}
/* Check the record is long enough to contain the data it claims to
* contain
*/
if (data_len < ((hello_data->cipher_specs_len * 3) + hello_data->session_id_len + challenge_len)) {
*message = "SSL2 Client Hello record is too short";
return -1;
}
/* Copy the cipher specs */
if (memcpy(hello_data->cipher_specs, data_ptr, hello_data->cipher_specs_len * 3) != hello_data->cipher_specs) {
*message = "SSL2 Client Hello record encountered memcpy error";
return -1;
}
CONSUME_BYTES(hello_data->cipher_specs_len * 3);
/* Copy the session id */
if (memcpy(hello_data->session_id, data_ptr, hello_data->session_id_len) != hello_data->session_id) {
*message = "SSL2 Client Hello record encountered memcpy error";
return -1;
}
CONSUME_BYTES(hello_data->session_id_len);
/* Per Appendix E.2 of the TLS RFCs, the challenge should be padded with
* leading zeroes.
*/
if (challenge_len < 32) {
if (memset(hello_data->challenge, 0, 32 - challenge_len) != hello_data->challenge) {
*message = "SSL2 Client Hello record encountered memset error";
return -1;
}
}
/* Copy the challenge */
if (memcpy(hello_data->challenge + (32 - challenge_len), data_ptr, challenge_len) !=
hello_data->challenge + (32 - challenge_len)) {
*message = "SSL2 Client Hello record encountered memcpy error";
return -1;
}
CONSUME_BYTES(challenge_len);
/* The SSL2 Draft does not make it clear if the SSL record can or should
* not contain any extraneous data.
*/
return 0;
}