2 * Copyright (c) 2007, Cameron Rich
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are met:
9 * * Redistributions of source code must retain the above copyright notice,
10 * this list of conditions and the following disclaimer.
11 * * Redistributions in binary form must reproduce the above copyright notice,
12 * this list of conditions and the following disclaimer in the documentation
13 * and/or other materials provided with the distribution.
14 * * Neither the name of the axTLS project nor the names of its contributors
15 * may be used to endorse or promote products derived from this software
16 * without specific prior written permission.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
22 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
25 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
26 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
27 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
38 #ifdef CONFIG_SSL_ENABLE_CLIENT /* all commented out if no client */
40 static int send_client_hello(SSL
*ssl
);
41 static int process_server_hello(SSL
*ssl
);
42 static int process_server_hello_done(SSL
*ssl
);
43 static int send_client_key_xchg(SSL
*ssl
);
44 static int process_cert_req(SSL
*ssl
);
45 static int send_cert_verify(SSL
*ssl
);
48 * Establish a new SSL connection to an SSL server.
50 EXP_FUNC SSL
* STDCALL
ssl_client_new(SSL_CTX
*ssl_ctx
, int client_fd
, const
51 uint8_t *session_id
, uint8_t sess_id_size
)
56 SOCKET_BLOCK(client_fd
); /* ensure blocking mode */
57 ssl
= ssl_new(ssl_ctx
, client_fd
);
59 if (session_id
&& ssl_ctx
->num_sessions
)
61 if (sess_id_size
> SSL_SESSION_ID_SIZE
) /* validity check */
67 memcpy(ssl
->session_id
, session_id
, sess_id_size
);
68 ssl
->sess_id_size
= sess_id_size
;
69 SET_SSL_FLAG(SSL_SESSION_RESUME
); /* just flag for later */
72 SET_SSL_FLAG(SSL_IS_CLIENT
);
73 ret
= do_client_connect(ssl
);
78 * Process the handshake record.
80 int do_clnt_handshake(SSL
*ssl
, int handshake_type
, uint8_t *buf
, int hs_len
)
84 /* To get here the state must be valid */
85 switch (handshake_type
)
88 ret
= process_server_hello(ssl
);
92 ret
= process_certificate(ssl
, &ssl
->x509_ctx
);
95 case HS_SERVER_HELLO_DONE
:
96 if ((ret
= process_server_hello_done(ssl
)) == SSL_OK
)
98 if (IS_SET_SSL_FLAG(SSL_HAS_CERT_REQ
))
100 if ((ret
= send_certificate(ssl
)) == SSL_OK
&&
101 (ret
= send_client_key_xchg(ssl
)) == SSL_OK
)
103 send_cert_verify(ssl
);
108 ret
= send_client_key_xchg(ssl
);
112 (ret
= send_change_cipher_spec(ssl
)) == SSL_OK
)
114 ret
= send_finished(ssl
);
120 ret
= process_cert_req(ssl
);
124 ret
= process_finished(ssl
, hs_len
);
125 disposable_free(ssl
); /* free up some memory */
128 case HS_HELLO_REQUEST
:
130 ret
= do_client_connect(ssl
);
138 * Do the handshaking from the beginning.
140 int do_client_connect(SSL
*ssl
)
144 send_client_hello(ssl
); /* send the client hello */
145 ssl
->bm_read_index
= 0;
146 ssl
->next_state
= HS_SERVER_HELLO
;
147 ssl
->hs_status
= SSL_NOT_OK
; /* not connected */
148 x509_free(ssl
->x509_ctx
);
150 /* sit in a loop until it all looks good */
151 while (ssl
->hs_status
!= SSL_OK
)
153 ret
= basic_read(ssl
, NULL
);
157 if (ret
!= SSL_ERROR_CONN_LOST
)
159 /* let the server know we are dying and why */
160 if (send_alert(ssl
, ret
))
162 /* something nasty happened, so get rid of it */
163 kill_ssl_session(ssl
->ssl_ctx
->ssl_sessions
, ssl
);
171 ssl
->hs_status
= ret
; /* connected? */
176 * Send the initial client hello.
178 static int send_client_hello(SSL
*ssl
)
180 uint8_t *buf
= ssl
->bm_data
;
181 time_t tm
= time(NULL
);
182 uint8_t *tm_ptr
= &buf
[6]; /* time will go here */
185 buf
[0] = HS_CLIENT_HELLO
;
188 /* byte 3 is calculated later */
192 /* client random value - spec says that 1st 4 bytes are big endian time */
193 *tm_ptr
++ = (uint8_t)(((long)tm
& 0xff000000) >> 24);
194 *tm_ptr
++ = (uint8_t)(((long)tm
& 0x00ff0000) >> 16);
195 *tm_ptr
++ = (uint8_t)(((long)tm
& 0x0000ff00) >> 8);
196 *tm_ptr
++ = (uint8_t)(((long)tm
& 0x000000ff));
197 get_random(SSL_RANDOM_SIZE
-4, &buf
[10]);
198 memcpy(ssl
->dc
->client_random
, &buf
[6], SSL_RANDOM_SIZE
);
199 offset
= 6 + SSL_RANDOM_SIZE
;
201 /* give session resumption a go */
202 if (IS_SET_SSL_FLAG(SSL_SESSION_RESUME
)) /* set initially by user */
204 buf
[offset
++] = ssl
->sess_id_size
;
205 memcpy(&buf
[offset
], ssl
->session_id
, ssl
->sess_id_size
);
206 offset
+= ssl
->sess_id_size
;
207 CLR_SSL_FLAG(SSL_SESSION_RESUME
); /* clear so we can set later */
211 /* no session id - because no session resumption just yet */
215 buf
[offset
++] = 0; /* number of ciphers */
216 buf
[offset
++] = NUM_PROTOCOLS
*2;/* number of ciphers */
218 /* put all our supported protocols in our request */
219 for (i
= 0; i
< NUM_PROTOCOLS
; i
++)
221 buf
[offset
++] = 0; /* cipher we are using */
222 buf
[offset
++] = ssl_prot_prefs
[i
];
225 buf
[offset
++] = 1; /* no compression */
227 buf
[3] = offset
- 4; /* handshake size */
229 return send_packet(ssl
, PT_HANDSHAKE_PROTOCOL
, NULL
, offset
);
233 * Process the server hello.
235 static int process_server_hello(SSL
*ssl
)
237 uint8_t *buf
= ssl
->bm_data
;
238 int pkt_size
= ssl
->bm_index
;
239 int version
= (buf
[4] << 4) + buf
[5];
240 int num_sessions
= ssl
->ssl_ctx
->num_sessions
;
241 uint8_t sess_id_size
;
242 int offset
, ret
= SSL_OK
;
244 /* check that we are talking to a TLSv1 server */
246 return SSL_ERROR_INVALID_VERSION
;
248 /* get the server random value */
249 memcpy(ssl
->dc
->server_random
, &buf
[6], SSL_RANDOM_SIZE
);
250 offset
= 6 + SSL_RANDOM_SIZE
; /* skip of session id size */
251 sess_id_size
= buf
[offset
++];
255 ssl
->session
= ssl_session_update(num_sessions
,
256 ssl
->ssl_ctx
->ssl_sessions
, ssl
, &buf
[offset
]);
257 memcpy(ssl
->session
->session_id
, &buf
[offset
], sess_id_size
);
259 /* pad the rest with 0's */
260 if (sess_id_size
< SSL_SESSION_ID_SIZE
)
262 memset(&ssl
->session
->session_id
[sess_id_size
], 0,
263 SSL_SESSION_ID_SIZE
-sess_id_size
);
267 memcpy(ssl
->session_id
, &buf
[offset
], sess_id_size
);
268 ssl
->sess_id_size
= sess_id_size
;
269 offset
+= sess_id_size
;
271 /* get the real cipher we are using */
272 ssl
->cipher
= buf
[++offset
];
273 ssl
->next_state
= IS_SET_SSL_FLAG(SSL_SESSION_RESUME
) ?
274 HS_FINISHED
: HS_CERTIFICATE
;
276 offset
++; // skip the compr
277 PARANOIA_CHECK(pkt_size
, offset
);
278 ssl
->dc
->bm_proc_index
= offset
+1;
285 * Process the server hello done message.
287 static int process_server_hello_done(SSL
*ssl
)
289 ssl
->next_state
= HS_FINISHED
;
294 * Send a client key exchange message.
296 static int send_client_key_xchg(SSL
*ssl
)
298 uint8_t *buf
= ssl
->bm_data
;
299 uint8_t premaster_secret
[SSL_SECRET_SIZE
];
300 int enc_secret_size
= -1;
302 buf
[0] = HS_CLIENT_KEY_XCHG
;
305 premaster_secret
[0] = 0x03; /* encode the version number */
306 premaster_secret
[1] = 0x01;
307 get_random(SSL_SECRET_SIZE
-2, &premaster_secret
[2]);
308 DISPLAY_RSA(ssl
, ssl
->x509_ctx
->rsa_ctx
);
310 /* rsa_ctx->bi_ctx is not thread-safe */
311 SSL_CTX_LOCK(ssl
->ssl_ctx
->mutex
);
312 enc_secret_size
= RSA_encrypt(ssl
->x509_ctx
->rsa_ctx
, premaster_secret
,
313 SSL_SECRET_SIZE
, &buf
[6], 0);
314 SSL_CTX_UNLOCK(ssl
->ssl_ctx
->mutex
);
316 buf
[2] = (enc_secret_size
+ 2) >> 8;
317 buf
[3] = (enc_secret_size
+ 2) & 0xff;
318 buf
[4] = enc_secret_size
>> 8;
319 buf
[5] = enc_secret_size
& 0xff;
321 generate_master_secret(ssl
, premaster_secret
);
322 return send_packet(ssl
, PT_HANDSHAKE_PROTOCOL
, NULL
, enc_secret_size
+6);
326 * Process the certificate request.
328 static int process_cert_req(SSL
*ssl
)
330 uint8_t *buf
= &ssl
->bm_data
[ssl
->dc
->bm_proc_index
];
332 int offset
= (buf
[2] << 4) + buf
[3];
333 int pkt_size
= ssl
->bm_index
;
335 /* don't do any processing - we will send back an RSA certificate anyway */
336 ssl
->next_state
= HS_SERVER_HELLO_DONE
;
337 SET_SSL_FLAG(SSL_HAS_CERT_REQ
);
338 ssl
->dc
->bm_proc_index
+= offset
;
339 PARANOIA_CHECK(pkt_size
, offset
);
345 * Send a certificate verify message.
347 static int send_cert_verify(SSL
*ssl
)
349 uint8_t *buf
= ssl
->bm_data
;
350 uint8_t dgst
[MD5_SIZE
+SHA1_SIZE
];
351 RSA_CTX
*rsa_ctx
= ssl
->ssl_ctx
->rsa_ctx
;
354 DISPLAY_RSA(ssl
, rsa_ctx
);
356 buf
[0] = HS_CERT_VERIFY
;
359 finished_digest(ssl
, NULL
, dgst
); /* calculate the digest */
361 /* rsa_ctx->bi_ctx is not thread-safe */
364 SSL_CTX_LOCK(ssl
->ssl_ctx
->mutex
);
365 n
= RSA_encrypt(rsa_ctx
, dgst
, sizeof(dgst
), &buf
[6], 1);
366 SSL_CTX_UNLOCK(ssl
->ssl_ctx
->mutex
);
370 ret
= SSL_ERROR_INVALID_KEY
;
375 buf
[4] = n
>> 8; /* add the RSA size (not officially documented) */
380 ret
= send_packet(ssl
, PT_HANDSHAKE_PROTOCOL
, NULL
, n
+4);
386 #endif /* CONFIG_SSL_ENABLE_CLIENT */