1
This patch is adding support for TLS hello extensions and externally
2
generated pre-shared key material to OpenSSL 0.9.8e. This is
3
based on the patch from Alexey Kobozev <akobozev@cisco.com>
4
(sent to openssl-dev mailing list on Tue, 07 Jun 2005 15:40:58 +0300).
8
diff -uprN openssl-0.9.8e.orig/ssl/Makefile openssl-0.9.8e/ssl/Makefile
9
--- openssl-0.9.8e.orig/ssl/Makefile 2006-02-03 17:49:35.000000000 -0800
10
+++ openssl-0.9.8e/ssl/Makefile 2007-03-22 20:23:19.000000000 -0700
11
@@ -24,7 +24,7 @@ LIBSRC= \
12
s2_meth.c s2_srvr.c s2_clnt.c s2_lib.c s2_enc.c s2_pkt.c \
13
s3_meth.c s3_srvr.c s3_clnt.c s3_lib.c s3_enc.c s3_pkt.c s3_both.c \
14
s23_meth.c s23_srvr.c s23_clnt.c s23_lib.c s23_pkt.c \
15
- t1_meth.c t1_srvr.c t1_clnt.c t1_lib.c t1_enc.c \
16
+ t1_meth.c t1_srvr.c t1_clnt.c t1_lib.c t1_enc.c t1_ext.c \
17
d1_meth.c d1_srvr.c d1_clnt.c d1_lib.c d1_pkt.c \
19
ssl_lib.c ssl_err2.c ssl_cert.c ssl_sess.c \
20
@@ -35,7 +35,7 @@ LIBOBJ= \
21
s2_meth.o s2_srvr.o s2_clnt.o s2_lib.o s2_enc.o s2_pkt.o \
22
s3_meth.o s3_srvr.o s3_clnt.o s3_lib.o s3_enc.o s3_pkt.o s3_both.o \
23
s23_meth.o s23_srvr.o s23_clnt.o s23_lib.o s23_pkt.o \
24
- t1_meth.o t1_srvr.o t1_clnt.o t1_lib.o t1_enc.o \
25
+ t1_meth.o t1_srvr.o t1_clnt.o t1_lib.o t1_enc.o t1_ext.o \
26
d1_meth.o d1_srvr.o d1_clnt.o d1_lib.o d1_pkt.o \
28
ssl_lib.o ssl_err2.o ssl_cert.o ssl_sess.o \
29
@@ -968,3 +968,4 @@ t1_srvr.o: ../include/openssl/ssl23.h ..
30
t1_srvr.o: ../include/openssl/stack.h ../include/openssl/symhacks.h
31
t1_srvr.o: ../include/openssl/tls1.h ../include/openssl/x509.h
32
t1_srvr.o: ../include/openssl/x509_vfy.h ssl_locl.h t1_srvr.c
33
+t1_ext.o: t1_ext.c ssl_locl.h
34
diff -uprN openssl-0.9.8e.orig/ssl/s3_clnt.c openssl-0.9.8e/ssl/s3_clnt.c
35
--- openssl-0.9.8e.orig/ssl/s3_clnt.c 2006-09-28 05:23:15.000000000 -0700
36
+++ openssl-0.9.8e/ssl/s3_clnt.c 2007-03-22 20:23:19.000000000 -0700
37
@@ -601,6 +601,20 @@ int ssl3_client_hello(SSL *s)
39
*(p++)=0; /* Add the NULL method */
41
+ /* send client hello extensions if any */
42
+ if (s->version >= TLS1_VERSION && s->tls_extension)
44
+ // set the total extensions length
45
+ s2n(s->tls_extension->length + 4, p);
47
+ // put the extensions with type and length
48
+ s2n(s->tls_extension->type, p);
49
+ s2n(s->tls_extension->length, p);
51
+ memcpy(p, s->tls_extension->data, s->tls_extension->length);
52
+ p+=s->tls_extension->length;
57
*(d++)=SSL3_MT_CLIENT_HELLO;
58
@@ -623,7 +637,7 @@ int ssl3_get_server_hello(SSL *s)
59
STACK_OF(SSL_CIPHER) *sk;
63
+ int i,al,ok,pre_shared;
66
#ifndef OPENSSL_NO_COMP
67
@@ -690,7 +704,24 @@ int ssl3_get_server_hello(SSL *s)
71
- if (j != 0 && j == s->session->session_id_length
72
+ /* check if we want to resume the session based on external pre-shared secret */
74
+ if (s->version >= TLS1_VERSION && s->tls_session_secret_cb)
76
+ SSL_CIPHER *pref_cipher=NULL;
77
+ s->session->master_key_length=sizeof(s->session->master_key);
78
+ if (s->tls_session_secret_cb(s, s->session->master_key, &s->session->master_key_length,
79
+ NULL, &pref_cipher, s->tls_session_secret_cb_arg))
82
+ s->session->cipher=pref_cipher ? pref_cipher : ssl_get_cipher_by_char(s,p+j);
83
+ s->session->session_id_length = j;
84
+ memcpy(s->session->session_id, p, j);
89
+ if ((pre_shared || j != 0) && j == s->session->session_id_length
90
&& memcmp(p,s->session->session_id,j) == 0)
92
if(s->sid_ctx_length != s->session->sid_ctx_length
93
diff -uprN openssl-0.9.8e.orig/ssl/s3_srvr.c openssl-0.9.8e/ssl/s3_srvr.c
94
--- openssl-0.9.8e.orig/ssl/s3_srvr.c 2007-02-07 12:36:40.000000000 -0800
95
+++ openssl-0.9.8e/ssl/s3_srvr.c 2007-03-22 20:23:19.000000000 -0700
96
@@ -945,6 +945,75 @@ int ssl3_get_client_hello(SSL *s)
100
+ /* Check for TLS client hello extension here */
101
+ if (p < (d+n) && s->version >= TLS1_VERSION)
103
+ if (s->tls_extension_cb)
105
+ TLS_EXTENSION tls_ext;
106
+ unsigned short ext_total_len;
108
+ n2s(p, ext_total_len);
109
+ n2s(p, tls_ext.type);
110
+ n2s(p, tls_ext.length);
112
+ // sanity check in TLS extension len
113
+ if (tls_ext.length > (d+n) - p)
115
+ // just cut the lenth to packet border
116
+ tls_ext.length = (d+n) - p;
121
+ // returns an alert code or 0
122
+ al = s->tls_extension_cb(s, &tls_ext, s->tls_extension_cb_arg);
125
+ SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,SSL_R_PEER_ERROR);
131
+ /* Check if we want to use external pre-shared secret for this handshake */
132
+ /* for not reused session only */
133
+ if (!s->hit && s->version >= TLS1_VERSION && s->tls_session_secret_cb)
135
+ SSL_CIPHER *pref_cipher=NULL;
137
+ s->session->master_key_length=sizeof(s->session->master_key);
138
+ if(s->tls_session_secret_cb(s, s->session->master_key, &s->session->master_key_length,
139
+ ciphers, &pref_cipher, s->tls_session_secret_cb_arg))
142
+ s->session->ciphers=ciphers;
143
+ s->session->verify_result=X509_V_OK;
147
+ /* check if some cipher was preferred by call back */
148
+ pref_cipher=pref_cipher ? pref_cipher : ssl3_choose_cipher(s, s->session->ciphers, SSL_get_ciphers(s));
149
+ if (pref_cipher == NULL)
151
+ al=SSL_AD_HANDSHAKE_FAILURE;
152
+ SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,SSL_R_NO_SHARED_CIPHER);
156
+ s->session->cipher=pref_cipher;
158
+ if (s->cipher_list)
159
+ sk_SSL_CIPHER_free(s->cipher_list);
161
+ if (s->cipher_list_by_id)
162
+ sk_SSL_CIPHER_free(s->cipher_list_by_id);
164
+ s->cipher_list = sk_SSL_CIPHER_dup(s->session->ciphers);
165
+ s->cipher_list_by_id = sk_SSL_CIPHER_dup(s->session->ciphers);
169
/* Given s->session->ciphers and SSL_get_ciphers, we must
172
diff -uprN openssl-0.9.8e.orig/ssl/ssl.h openssl-0.9.8e/ssl/ssl.h
173
--- openssl-0.9.8e.orig/ssl/ssl.h 2007-02-19 09:55:07.000000000 -0800
174
+++ openssl-0.9.8e/ssl/ssl.h 2007-03-22 20:23:19.000000000 -0700
175
@@ -345,6 +345,7 @@ extern "C" {
176
* 'struct ssl_st *' function parameters used to prototype callbacks
178
typedef struct ssl_st *ssl_crock_st;
179
+typedef struct tls_extension_st TLS_EXTENSION;
181
/* used to hold info on the particular ciphers used */
182
typedef struct ssl_cipher_st
183
@@ -366,6 +367,8 @@ DECLARE_STACK_OF(SSL_CIPHER)
184
typedef struct ssl_st SSL;
185
typedef struct ssl_ctx_st SSL_CTX;
187
+typedef int (*tls_session_secret_cb_fn)(SSL *s, void *secret, int *secret_len, STACK_OF(SSL_CIPHER) *peer_ciphers, SSL_CIPHER **cipher, void *arg);
189
/* Used to hold functions for SSLv2 or SSLv3/TLSv1 functions */
190
typedef struct ssl_method_st
192
@@ -973,6 +976,15 @@ struct ssl_st
194
int client_version; /* what was passed, used for
195
* SSLv3/TLS rollback check */
197
+ /* TLS externsions */
198
+ TLS_EXTENSION *tls_extension;
199
+ int (*tls_extension_cb)(SSL *s, TLS_EXTENSION *tls_ext, void *arg);
200
+ void *tls_extension_cb_arg;
202
+ /* TLS pre-shared secret session resumption */
203
+ tls_session_secret_cb_fn tls_session_secret_cb;
204
+ void *tls_session_secret_cb_arg;
208
@@ -1538,6 +1550,13 @@ void *SSL_COMP_get_compression_methods(v
209
int SSL_COMP_add_compression_method(int id,void *cm);
212
+/* TLS extensions functions */
213
+int SSL_set_hello_extension(SSL *s, int ext_type, void *ext_data, int ext_len);
214
+int SSL_set_hello_extension_cb(SSL *s, int (*cb)(SSL *, TLS_EXTENSION *, void *), void *arg);
216
+/* Pre-shared secret session resumption functions */
217
+int SSL_set_session_secret_cb(SSL *s, tls_session_secret_cb_fn tls_session_secret_cb, void *arg);
219
/* BEGIN ERROR CODES */
220
/* The following lines are auto generated by the script mkerr.pl. Any changes
221
* made after this point may be overwritten when the script is next run.
222
@@ -1719,6 +1738,7 @@ void ERR_load_SSL_strings(void);
223
#define SSL_F_TLS1_ENC 210
224
#define SSL_F_TLS1_SETUP_KEY_BLOCK 211
225
#define SSL_F_WRITE_PENDING 212
226
+#define SSL_F_SSL_SET_HELLO_EXTENSION 213
229
#define SSL_R_APP_DATA_IN_HANDSHAKE 100
230
diff -uprN openssl-0.9.8e.orig/ssl/ssl_err.c openssl-0.9.8e/ssl/ssl_err.c
231
--- openssl-0.9.8e.orig/ssl/ssl_err.c 2006-11-21 12:14:46.000000000 -0800
232
+++ openssl-0.9.8e/ssl/ssl_err.c 2007-03-22 20:23:19.000000000 -0700
233
@@ -242,6 +242,7 @@ static ERR_STRING_DATA SSL_str_functs[]=
234
{ERR_FUNC(SSL_F_TLS1_ENC), "TLS1_ENC"},
235
{ERR_FUNC(SSL_F_TLS1_SETUP_KEY_BLOCK), "TLS1_SETUP_KEY_BLOCK"},
236
{ERR_FUNC(SSL_F_WRITE_PENDING), "WRITE_PENDING"},
237
+{ERR_FUNC(SSL_F_SSL_SET_HELLO_EXTENSION), "SSL_set_hello_extension"},
241
diff -uprN openssl-0.9.8e.orig/ssl/ssl_sess.c openssl-0.9.8e/ssl/ssl_sess.c
242
--- openssl-0.9.8e.orig/ssl/ssl_sess.c 2007-02-10 02:40:24.000000000 -0800
243
+++ openssl-0.9.8e/ssl/ssl_sess.c 2007-03-22 20:23:19.000000000 -0700
244
@@ -656,6 +656,15 @@ long SSL_CTX_get_timeout(const SSL_CTX *
245
return(s->session_timeout);
248
+int SSL_set_session_secret_cb(SSL *s, int (*tls_session_secret_cb)(SSL *s, void *secret, int *secret_len,
249
+ STACK_OF(SSL_CIPHER) *peer_ciphers, SSL_CIPHER **cipher, void *arg), void *arg)
251
+ if (s == NULL) return(0);
252
+ s->tls_session_secret_cb = tls_session_secret_cb;
253
+ s->tls_session_secret_cb_arg = arg;
257
typedef struct timeout_param_st
260
diff -uprN openssl-0.9.8e.orig/ssl/t1_ext.c openssl-0.9.8e/ssl/t1_ext.c
261
--- openssl-0.9.8e.orig/ssl/t1_ext.c 1969-12-31 16:00:00.000000000 -0800
262
+++ openssl-0.9.8e/ssl/t1_ext.c 2007-03-22 20:23:19.000000000 -0700
266
+#include "ssl_locl.h"
269
+int SSL_set_hello_extension(SSL *s, int ext_type, void *ext_data, int ext_len)
271
+ if(s->version >= TLS1_VERSION)
273
+ if(s->tls_extension)
275
+ OPENSSL_free(s->tls_extension);
276
+ s->tls_extension = NULL;
281
+ s->tls_extension = OPENSSL_malloc(sizeof(TLS_EXTENSION) + ext_len);
282
+ if(!s->tls_extension)
284
+ SSLerr(SSL_F_SSL_SET_HELLO_EXTENSION, ERR_R_MALLOC_FAILURE);
288
+ s->tls_extension->type = ext_type;
289
+ s->tls_extension->length = ext_len;
290
+ s->tls_extension->data = s->tls_extension + 1;
291
+ memcpy(s->tls_extension->data, ext_data, ext_len);
300
+int SSL_set_hello_extension_cb(SSL *s, int (*cb)(SSL *, TLS_EXTENSION *, void *), void *arg)
302
+ if(s->version >= TLS1_VERSION)
304
+ s->tls_extension_cb = cb;
305
+ s->tls_extension_cb_arg = arg;
312
diff -uprN openssl-0.9.8e.orig/ssl/t1_lib.c openssl-0.9.8e/ssl/t1_lib.c
313
--- openssl-0.9.8e.orig/ssl/t1_lib.c 2007-01-21 08:07:25.000000000 -0800
314
+++ openssl-0.9.8e/ssl/t1_lib.c 2007-03-22 20:23:19.000000000 -0700
315
@@ -97,6 +97,10 @@ int tls1_new(SSL *s)
317
void tls1_free(SSL *s)
319
+ if(s->tls_extension)
321
+ OPENSSL_free(s->tls_extension);
326
diff -uprN openssl-0.9.8e.orig/ssl/tls1.h openssl-0.9.8e/ssl/tls1.h
327
--- openssl-0.9.8e.orig/ssl/tls1.h 2006-06-14 10:52:01.000000000 -0700
328
+++ openssl-0.9.8e/ssl/tls1.h 2007-03-22 20:23:19.000000000 -0700
329
@@ -296,6 +296,14 @@ extern "C" {
330
#define TLS_MD_MASTER_SECRET_CONST "\x6d\x61\x73\x74\x65\x72\x20\x73\x65\x63\x72\x65\x74" /*master secret*/
333
+/* TLS extension struct */
334
+struct tls_extension_st
336
+ unsigned short type;
337
+ unsigned short length;
344
diff -uprN openssl-0.9.8e.orig/util/ssleay.num openssl-0.9.8e/util/ssleay.num
345
--- openssl-0.9.8e.orig/util/ssleay.num 2006-11-30 05:04:43.000000000 -0800
346
+++ openssl-0.9.8e/util/ssleay.num 2007-03-22 20:24:07.000000000 -0700
347
@@ -238,3 +238,6 @@ SSL_CTX_set_info_callback
348
SSL_CTX_sess_get_new_cb 287 EXIST::FUNCTION:
349
SSL_CTX_get_client_cert_cb 288 EXIST::FUNCTION:
350
SSL_CTX_sess_get_remove_cb 289 EXIST::FUNCTION:
351
+SSL_set_hello_extension 290 EXIST::FUNCTION:
352
+SSL_set_hello_extension_cb 291 EXIST::FUNCTION:
353
+SSL_set_session_secret_cb 292 EXIST::FUNCTION: