aboutsummaryrefslogtreecommitdiff
path: root/networking/tls.c
diff options
context:
space:
mode:
authorDenys Vlasenko <vda.linux@googlemail.com>2017-01-14 12:49:32 +0100
committerDenys Vlasenko <vda.linux@googlemail.com>2017-01-14 12:49:32 +0100
commitceff6b0ea9d8084b439e7d80f42c073d3bdda58d (patch)
treee9ca7cd16d52455abfb4292dbea707e22385de36 /networking/tls.c
parenta6f8651911716d1d1624712eb19e4f3608767c7e (diff)
downloadbusybox-w32-ceff6b0ea9d8084b439e7d80f42c073d3bdda58d.tar.gz
busybox-w32-ceff6b0ea9d8084b439e7d80f42c073d3bdda58d.tar.bz2
busybox-w32-ceff6b0ea9d8084b439e7d80f42c073d3bdda58d.zip
tls: work-in-progress TLS1.2 test applet
function old new delta tls_main - 733 +733 dump - 230 +230 xread_tls_block - 180 +180 get_der_len - 76 +76 enter_der_item - 70 +70 skip_der_item - 56 +56 get24be - 24 +24 tls_error_die - 19 +19 packed_usage 31010 31027 +17 applet_names 2549 2553 +4 applet_main 1472 1476 +4 applet_suid 92 93 +1 applet_install_loc 184 185 +1 ------------------------------------------------------------------------------ (add/remove: 9/0 grow/shrink: 5/0 up/down: 1415/0) Total: 1415 bytes Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
Diffstat (limited to 'networking/tls.c')
-rw-r--r--networking/tls.c643
1 files changed, 643 insertions, 0 deletions
diff --git a/networking/tls.c b/networking/tls.c
new file mode 100644
index 000000000..983ed507d
--- /dev/null
+++ b/networking/tls.c
@@ -0,0 +1,643 @@
1/*
2 * Licensed under GPLv2, see file LICENSE in this source tree.
3 *
4 * Copyright (C) 2017 Denys Vlasenko
5 */
6//config:config TLS
7//config: bool "tls (debugging)"
8//config: default n
9
10//applet:IF_TLS(APPLET(tls, BB_DIR_USR_BIN, BB_SUID_DROP))
11
12//kbuild:lib-$(CONFIG_TLS) += tls.o
13////kbuild:lib-$(CONFIG_TLS) += tls_ciphers.o
14////kbuild:lib-$(CONFIG_TLS) += tls_aes.o
15////kbuild:lib-$(CONFIG_TLS) += tls_aes_gcm.o
16
17//usage:#define tls_trivial_usage
18//usage: "HOST[:PORT]"
19//usage:#define tls_full_usage "\n\n"
20
21#include "libbb.h"
22//#include "tls_cryptoapi.h"
23//#include "tls_ciphers.h"
24
25#if 1
26# define dbg(...) fprintf(stderr, __VA_ARGS__)
27#else
28# define dbg(...) ((void)0)
29#endif
30
31#define RECORD_TYPE_CHANGE_CIPHER_SPEC 20
32#define RECORD_TYPE_ALERT 21
33#define RECORD_TYPE_HANDSHAKE 22
34#define RECORD_TYPE_APPLICATION_DATA 23
35
36#define HANDSHAKE_HELLO_REQUEST 0
37#define HANDSHAKE_CLIENT_HELLO 1
38#define HANDSHAKE_SERVER_HELLO 2
39#define HANDSHAKE_HELLO_VERIFY_REQUEST 3
40#define HANDSHAKE_NEW_SESSION_TICKET 4
41#define HANDSHAKE_CERTIFICATE 11
42#define HANDSHAKE_SERVER_KEY_EXCHANGE 12
43#define HANDSHAKE_CERTIFICATE_REQUEST 13
44#define HANDSHAKE_SERVER_HELLO_DONE 14
45#define HANDSHAKE_CERTIFICATE_VERIFY 15
46#define HANDSHAKE_CLIENT_KEY_EXCHANGE 16
47#define HANDSHAKE_FINISHED 20
48
49#define SSL_NULL_WITH_NULL_NULL 0x0000
50#define SSL_RSA_WITH_NULL_MD5 0x0001
51#define SSL_RSA_WITH_NULL_SHA 0x0002
52#define SSL_RSA_WITH_RC4_128_MD5 0x0004
53#define SSL_RSA_WITH_RC4_128_SHA 0x0005
54#define SSL_RSA_WITH_3DES_EDE_CBC_SHA 0x000A /* 10 */
55#define TLS_RSA_WITH_AES_128_CBC_SHA 0x002F /* 47 */
56#define TLS_RSA_WITH_AES_256_CBC_SHA 0x0035 /* 53 */
57#define TLS_EMPTY_RENEGOTIATION_INFO_SCSV 0x00FF
58
59#define TLS_RSA_WITH_IDEA_CBC_SHA 0x0007 /* 7 */
60#define SSL_DHE_RSA_WITH_3DES_EDE_CBC_SHA 0x0016 /* 22 */
61#define SSL_DH_anon_WITH_RC4_128_MD5 0x0018 /* 24 */
62#define SSL_DH_anon_WITH_3DES_EDE_CBC_SHA 0x001B /* 27 */
63#define TLS_DHE_RSA_WITH_AES_128_CBC_SHA 0x0033 /* 51 */
64#define TLS_DHE_RSA_WITH_AES_256_CBC_SHA 0x0039 /* 57 */
65#define TLS_DHE_RSA_WITH_AES_128_CBC_SHA256 0x0067 /* 103 */
66#define TLS_DHE_RSA_WITH_AES_256_CBC_SHA256 0x006B /* 107 */
67#define TLS_DH_anon_WITH_AES_128_CBC_SHA 0x0034 /* 52 */
68#define TLS_DH_anon_WITH_AES_256_CBC_SHA 0x003A /* 58 */
69#define TLS_RSA_WITH_AES_128_CBC_SHA256 0x003C /* 60 */
70#define TLS_RSA_WITH_AES_256_CBC_SHA256 0x003D /* 61 */
71#define TLS_RSA_WITH_SEED_CBC_SHA 0x0096 /* 150 */
72#define TLS_PSK_WITH_AES_128_CBC_SHA 0x008C /* 140 */
73#define TLS_PSK_WITH_AES_128_CBC_SHA256 0x00AE /* 174 */
74#define TLS_PSK_WITH_AES_256_CBC_SHA384 0x00AF /* 175 */
75#define TLS_PSK_WITH_AES_256_CBC_SHA 0x008D /* 141 */
76#define TLS_DHE_PSK_WITH_AES_128_CBC_SHA 0x0090 /* 144 */
77#define TLS_DHE_PSK_WITH_AES_256_CBC_SHA 0x0091 /* 145 */
78#define TLS_ECDH_ECDSA_WITH_AES_128_CBC_SHA 0xC004 /* 49156 */
79#define TLS_ECDH_ECDSA_WITH_AES_256_CBC_SHA 0xC005 /* 49157 */
80#define TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA 0xC009 /* 49161 */
81#define TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA 0xC00A /* 49162 */
82#define TLS_ECDHE_RSA_WITH_3DES_EDE_CBC_SHA 0xC012 /* 49170 */
83#define TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA 0xC013 /* 49171 */
84#define TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA 0xC014 /* 49172 */
85#define TLS_ECDH_RSA_WITH_AES_128_CBC_SHA 0xC00E /* 49166 */
86#define TLS_ECDH_RSA_WITH_AES_256_CBC_SHA 0xC00F /* 49167 */
87#define TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256 0xC023 /* 49187 */
88#define TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA384 0xC024 /* 49188 */
89#define TLS_ECDH_ECDSA_WITH_AES_128_CBC_SHA256 0xC025 /* 49189 */
90#define TLS_ECDH_ECDSA_WITH_AES_256_CBC_SHA384 0xC026 /* 49190 */
91#define TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256 0xC027 /* 49191 */
92#define TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384 0xC028 /* 49192 */
93#define TLS_ECDH_RSA_WITH_AES_128_CBC_SHA256 0xC029 /* 49193 */
94#define TLS_ECDH_RSA_WITH_AES_256_CBC_SHA384 0xC02A /* 49194 */
95
96#define TLS_RSA_WITH_AES_128_GCM_SHA256 0x009C /* 156 */
97#define TLS_RSA_WITH_AES_256_GCM_SHA384 0x009D /* 157 */
98#define TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256 0xC02B /* 49195 */
99#define TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384 0xC02C /* 49196 */
100#define TLS_ECDH_ECDSA_WITH_AES_128_GCM_SHA256 0xC02D /* 49197 */
101#define TLS_ECDH_ECDSA_WITH_AES_256_GCM_SHA384 0xC02E /* 49198 */
102#define TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 0xC02F /* 49199 */
103#define TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 0xC030 /* 49200 */
104#define TLS_ECDH_RSA_WITH_AES_128_GCM_SHA256 0xC031 /* 49201 */
105#define TLS_ECDH_RSA_WITH_AES_256_GCM_SHA384 0xC032 /* 49202 */
106
107//Tested against kernel.org:
108//TLS 1.1
109//#define TLS_MAJ 3
110//#define TLS_MIN 2
111//#define CIPHER_ID TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA // ok, recvs SERVER_KEY_EXCHANGE
112//TLS 1.2
113#define TLS_MAJ 3
114#define TLS_MIN 3
115//#define CIPHER_ID TLS_RSA_WITH_AES_256_CBC_SHA256 // ok, no SERVER_KEY_EXCHANGE
116// All GCMs:
117//#define CIPHER_ID TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384 // SSL_ALERT_HANDSHAKE_FAILURE
118//#define CIPHER_ID TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256 // SSL_ALERT_HANDSHAKE_FAILURE
119//#define CIPHER_ID TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 // ok, recvs SERVER_KEY_EXCHANGE
120//#define CIPHER_ID TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256
121//#define CIPHER_ID TLS_ECDH_ECDSA_WITH_AES_256_GCM_SHA384
122//#define CIPHER_ID TLS_ECDH_ECDSA_WITH_AES_128_GCM_SHA256 // SSL_ALERT_HANDSHAKE_FAILURE
123//#define CIPHER_ID TLS_ECDH_RSA_WITH_AES_256_GCM_SHA384
124//#define CIPHER_ID TLS_ECDH_RSA_WITH_AES_128_GCM_SHA256 // SSL_ALERT_HANDSHAKE_FAILURE
125//#define CIPHER_ID TLS_RSA_WITH_AES_256_GCM_SHA384 // ok, no SERVER_KEY_EXCHANGE
126#define CIPHER_ID TLS_RSA_WITH_AES_128_GCM_SHA256 // ok, no SERVER_KEY_EXCHANGE
127//#define CIPHER_ID TLS_DH_anon_WITH_AES_256_CBC_SHA // SSL_ALERT_HANDSHAKE_FAILURE
128// (tested b/c this one doesn't req server certs... no luck)
129//test TLS_RSA_WITH_AES_128_CBC_SHA, in tls 1.2 it's mandated to be always supported
130
131struct transport_hdr {
132 uint8_t type;
133 uint8_t proto_maj, proto_min;
134 uint8_t len16_hi, len16_lo;
135};
136
137typedef struct tls_state {
138 int fd;
139
140 uint8_t *pubkey;
141 int pubkey_len;
142
143 int insize;
144 int tail;
145 // RFC 5246
146 // |6.2.1. Fragmentation
147 // | The record layer fragments information blocks into TLSPlaintext
148 // | records carrying data in chunks of 2^14 bytes or less. Client
149 // | message boundaries are not preserved in the record layer (i.e.,
150 // | multiple client messages of the same ContentType MAY be coalesced
151 // | into a single TLSPlaintext record, or a single message MAY be
152 // | fragmented across several records)
153 // |...
154 // | length
155 // | The length (in bytes) of the following TLSPlaintext.fragment.
156 // | The length MUST NOT exceed 2^14.
157 // |...
158 // | 6.2.2. Record Compression and Decompression
159 // |...
160 // | Compression must be lossless and may not increase the content length
161 // | by more than 1024 bytes. If the decompression function encounters a
162 // | TLSCompressed.fragment that would decompress to a length in excess of
163 // | 2^14 bytes, it MUST report a fatal decompression failure error.
164 // |...
165 // | length
166 // | The length (in bytes) of the following TLSCompressed.fragment.
167 // | The length MUST NOT exceed 2^14 + 1024.
168 //
169 // Since our buffer also contains 5-byte headers, make it a bit bigger:
170 uint8_t inbuf[18*1024];
171} tls_state_t;
172
173static
174tls_state_t *new_tls_state(void)
175{
176 tls_state_t *tls = xzalloc(sizeof(*tls));
177 tls->fd = -1;
178 return tls;
179}
180
181static unsigned get24be(const uint8_t *p)
182{
183 return 0x100*(0x100*p[0] + p[1]) + p[2];
184}
185
186static void dump(const void *vp, int len)
187{
188 char hexbuf[32 * 1024 + 4];
189 const uint8_t *p = vp;
190
191 while (len > 0) {
192 unsigned xhdr_len;
193 if (len < 5) {
194 bin2hex(hexbuf, (void*)p, len)[0] = '\0';
195 dbg("< |%s|\n", hexbuf);
196 return;
197 }
198 xhdr_len = 0x100*p[3] + p[4];
199 dbg("< hdr_type:%u ver:%u.%u len:%u", p[0], p[1], p[2], xhdr_len);
200 p += 5;
201 len -= 5;
202 if (len >= 4 && p[-5] == RECORD_TYPE_HANDSHAKE) {
203 unsigned len24 = get24be(p + 1);
204 dbg(" type:%u len24:%u", p[0], len24);
205 }
206 if (xhdr_len > len)
207 xhdr_len = len;
208 bin2hex(hexbuf, (void*)p, xhdr_len)[0] = '\0';
209 dbg(" |%s|\n", hexbuf);
210 p += xhdr_len;
211 len -= xhdr_len;
212 }
213}
214
215static void tls_error_die(tls_state_t *tls)
216{
217 dump(tls->inbuf, tls->insize + tls->tail);
218 xfunc_die();
219}
220
221static int xread_tls_block(tls_state_t *tls)
222{
223 int len;
224 int total;
225 int target;
226
227 dbg("insize:%u tail:%u\n", tls->insize, tls->tail);
228 memmove(tls->inbuf, tls->inbuf + tls->insize, tls->tail);
229 errno = 0;
230 total = tls->tail;
231 target = sizeof(tls->inbuf);
232 for (;;) {
233 if (total >= sizeof(struct transport_hdr) && target == sizeof(tls->inbuf)) {
234 struct transport_hdr *xhdr = (void*)tls->inbuf;
235 target = sizeof(*xhdr) + (0x100 * xhdr->len16_hi + xhdr->len16_lo);
236 }
237 /* if total >= target, we have a full packet (and possibly more)... */
238 if (target - total <= 0)
239 break;
240 len = safe_read(tls->fd, tls->inbuf + total, sizeof(tls->inbuf) - total);
241 if (len <= 0)
242 bb_perror_msg_and_die("short read");
243 total += len;
244 }
245 tls->tail = -(target - total);
246 tls->insize = target;
247 target -= sizeof(struct transport_hdr);
248 dbg("got block len:%u\n", target);
249 return target;
250}
251
252static void send_client_hello(tls_state_t *tls)
253{
254 struct client_hello {
255 struct transport_hdr xhdr;
256 uint8_t type;
257 uint8_t len24_hi, len24_mid, len24_lo;
258 uint8_t proto_maj, proto_min;
259 uint8_t rand32[32];
260 uint8_t session_id_len;
261 /* uint8_t session_id[]; */
262 uint8_t cipherid_len16_hi, cipherid_len16_lo;
263 uint8_t cipherid[2 * 1]; /* actually variable */
264 uint8_t comprtypes_len;
265 uint8_t comprtypes[1]; /* actually variable */
266 };
267 struct client_hello hello;
268
269 memset(&hello, 0, sizeof(hello));
270 hello.xhdr.type = RECORD_TYPE_HANDSHAKE;
271 hello.xhdr.proto_maj = TLS_MAJ;
272 hello.xhdr.proto_min = TLS_MIN;
273 hello.xhdr.len16_hi = (sizeof(hello) - sizeof(hello.xhdr)) >> 8;
274 hello.xhdr.len16_lo = (sizeof(hello) - sizeof(hello.xhdr));
275 hello.type = HANDSHAKE_CLIENT_HELLO;
276 hello.len24_mid = (sizeof(hello) - sizeof(hello.xhdr) - 4) >> 8;
277 hello.len24_lo = (sizeof(hello) - sizeof(hello.xhdr) - 4);
278 hello.proto_maj = TLS_MAJ;
279 hello.proto_min = TLS_MIN;
280 //fillrand(hello.rand32, sizeof(hello.rand32));
281 open_read_close("/dev/urandom", hello.rand32, sizeof(hello.rand32));
282 //hello.session_id_len = 0;
283 //hello.cipherid_len16_hi = 0;
284 hello.cipherid_len16_lo = 2;
285 hello.cipherid[0] = CIPHER_ID >> 8;
286 hello.cipherid[1] = CIPHER_ID & 0xff;
287 hello.comprtypes_len = 1;
288 //hello.comprtypes[0] = 0;
289
290 xwrite(tls->fd, &hello, sizeof(hello));
291}
292
293static void get_server_hello_or_die(tls_state_t *tls)
294{
295 struct server_hello {
296 struct transport_hdr xhdr;
297 uint8_t type;
298 uint8_t len24_hi, len24_mid, len24_lo;
299 uint8_t proto_maj, proto_min;
300 uint8_t rand32[32]; /* first 4 bytes are unix time in BE format */
301 uint8_t session_id_len;
302 uint8_t session_id[32];
303 uint8_t cipherid_hi, cipherid_lo;
304 uint8_t comprtype;
305 /* extensions may follow, but only those which client offered in its Hello */
306 };
307 struct server_hello *hp;
308 int len;
309
310 len = xread_tls_block(tls);
311
312 hp = (void*)tls->inbuf;
313 if (len != 74 /* TODO: if we accept extensions, should be < instead of != */
314 || hp->xhdr.type != RECORD_TYPE_HANDSHAKE
315 || hp->xhdr.proto_maj != TLS_MAJ
316 || hp->xhdr.proto_min != TLS_MIN
317 ) {
318 /* example: RECORD_TYPE_ALERT if server can't support our ciphers */
319 tls_error_die(tls);
320 }
321 dbg("got HANDSHAKE\n");
322 // 02 000046 03|03 58|78|cf|c1 50|a5|49|ee|7e|29|48|71|fe|97|fa|e8|2d|19|87|72|90|84|9d|37|a3|f0|cb|6f|5f|e3|3c|2f |20 |d8|1a|78|96|52|d6|91|01|24|b3|d6|5b|b7|d0|6c|b3|e1|78|4e|3c|95|de|74|a0|ba|eb|a7|3a|ff|bd|a2|bf |00|9c |00|
323 // SvHl len=70 maj.min unixtime^^^ 28randbytes^^^^^^^^^^^^^^^^^^^^^^^^^^^^_^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^_^^^ slen sid32bytes^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ cipSel comprSel
324 if (hp->type != HANDSHAKE_SERVER_HELLO
325 || hp->len24_hi != 0
326 || hp->len24_mid != 0
327 || hp->len24_lo != 70
328 || hp->proto_maj != TLS_MAJ
329 || hp->proto_min != TLS_MIN
330 || hp->session_id_len != 32
331 || hp->cipherid_hi != (CIPHER_ID >> 8)
332 || hp->cipherid_lo != (CIPHER_ID & 0xff)
333 || hp->comprtype != 0
334 ) {
335 tls_error_die(tls);
336 }
337 dbg("got SERVER_HELLO\n");
338}
339
340static unsigned get_der_len(uint8_t **bodyp, uint8_t *der, uint8_t *end)
341{
342 unsigned len;
343
344 if (end - der < 2)
345 xfunc_die();
346// if ((der[0] & 0x1f) == 0x1f) /* not single-byte item code? */
347// xfunc_die();
348
349 len = der[1]; /* maybe it's short len */
350 if (len >= 0x80) {
351 /* no */
352 if (len != 0x82) {
353 /* 0x80 is "0 bytes of len", invalid DER: must use short len if can */
354 /* 0x81 is "1 byte of len", invalid DER */
355 /* >0x82 is "3+ bytes of len", should not happen realistically */
356 xfunc_die();
357 }
358 if (end - der < 4)
359 xfunc_die();
360 /* it's "ii 82 xx yy" */
361 len = 0x100*der[2] + der[3];
362 if (len < 0x80)
363 xfunc_die(); /* invalid DER: must use short len if can */
364
365 der += 2; /* skip [code]+[82]+[2byte_len] */
366 }
367 der += 2; /* skip [code]+[1byte_len] */
368
369 if (end - der < len)
370 xfunc_die();
371 *bodyp = der;
372
373 return len;
374}
375
376static uint8_t *enter_der_item(uint8_t *der, uint8_t **endp)
377{
378 uint8_t *new_der;
379 unsigned len = get_der_len(&new_der, der, *endp);
380 dbg("entered der @%p:0x%02x len:%u inner_byte @%p:0x%02x\n", der, der[0], len, new_der, new_der[0]);
381 /* Move "end" position to cover only this item */
382 *endp = new_der + len;
383 return new_der;
384}
385
386static uint8_t *skip_der_item(uint8_t *der, uint8_t *end)
387{
388 uint8_t *new_der;
389 unsigned len = get_der_len(&new_der, der, end);
390 /* Skip body */
391 new_der += len;
392 dbg("skipped der 0x%02x, next byte 0x%02x\n", der[0], new_der[0]);
393 return new_der;
394}
395
396static void *find_key_in_der_cert(int *key_len, uint8_t *der, int len)
397{
398/* Example: partial decode of kernel.org certificate in DER format.
399 * SEQ 0x05ac/1452 bytes (Certificate): 308205ac
400 * SEQ 0x0494/1172 bytes (tbsCertificate): 30820494
401 * [ASN_CONTEXT_SPECIFIC | ASN_CONSTRUCTED | 0] 3 bytes: a003
402 * INTEGER (version): 0201 02
403 * INTEGER 0x11 bytes (serialNumber): 0211 00 9f85bf664b0cddafca508679501b2be4
404 * //^^^^^^note: matrixSSL also allows [ASN_CONTEXT_SPECIFIC | ASN_PRIMITIVE | 2] = 0x82 type
405 * SEQ 0x0d bytes (signatureAlgo): 300d
406 * OID 9 bytes: 0609 2a864886f70d01010b (OID_SHA256_RSA_SIG 42.134.72.134.247.13.1.1.11)
407 * NULL: 0500
408 * SEQ 0x5f bytes (issuer): 305f
409 * SET 11 bytes: 310b
410 * SEQ 9 bytes: 3009
411 * OID 3 bytes: 0603 550406
412 * Printable string "FR": 1302 4652
413 * SET 14 bytes: 310e
414 * SEQ 12 bytes: 300c
415 * OID 3 bytes: 0603 550408
416 * Printable string "Paris": 1305 5061726973
417 * SET 14 bytes: 310e
418 * SEQ 12 bytes: 300c
419 * OID 3 bytes: 0603 550407
420 * Printable string "Paris": 1305 5061726973
421 * SET 14 bytes: 310e
422 * SEQ 12 bytes: 300c
423 * OID 3 bytes: 0603 55040a
424 * Printable string "Gandi": 1305 47616e6469
425 * SET 32 bytes: 3120
426 * SEQ 30 bytes: 301e
427 * OID 3 bytes: 0603 550403
428 * Printable string "Gandi Standard SSL CA 2": 1317
429 * 47616e6469205374616e646172642053534c2043412032
430 * SEQ 30 bytes (validity): 301e
431 * TIME "161011000000Z": 170d 3136313031313030303030305a
432 * TIME "191011235959Z": 170d 3139313031313233353935395a
433 * SEQ 0x5b/91 bytes (subject): 305b //I did not decode this
434 * 3121301f060355040b1318446f6d61696e20436f
435 * 6e74726f6c2056616c6964617465643121301f06
436 * 0355040b1318506f73697469766553534c204d75
437 * 6c74692d446f6d61696e31133011060355040313
438 * 0a6b65726e656c2e6f7267
439 * SEQ 0x01a2/418 bytes (subjectPublicKeyInfo): 308201a2
440 * SEQ 13 bytes (algorithm): 300d
441 * OID 9 bytes: 0609 2a864886f70d010101 (OID_RSA_KEY_ALG 42.134.72.134.247.13.1.1.1)
442 * NULL: 0500
443 * BITSTRING 0x018f/399 bytes (publicKey): 0382018f
444 * ????: 00
445 * //after the zero byte, it appears key itself uses DER encoding:
446 * SEQ 0x018a/394 bytes: 3082018a
447 * INTEGER 0x0181/385 bytes (modulus): 02820181
448 * 00b1ab2fc727a3bef76780c9349bf3
449 * ...24 more blocks of 15 bytes each...
450 * 90e895291c6bc8693b65
451 * INTEGER 3 bytes (exponent): 0203 010001
452 * [ASN_CONTEXT_SPECIFIC | ASN_CONSTRUCTED | 0x3] 0x01e5 bytes (X509v3 extensions): a38201e5
453 * SEQ 0x01e1 bytes: 308201e1
454 * ...
455 * Certificate is a DER-encoded data structure. Each DER element has a length,
456 * which makes it easy to skip over large compound elements of any complexity
457 * without parsing them.
458 *
459 * Certificate is a sequence of three elements:
460 * tbsCertificate (SEQ)
461 * signatureAlgorithm (AlgorithmIdentifier)
462 * signatureValue (BIT STRING)
463 *
464 * In turn, tbsCertificate is a sequence of:
465 * version
466 * serialNumber
467 * signatureAlgo (AlgorithmIdentifier)
468 * issuer (Name, has complex structure)
469 * validity (Validity, SEQ of two Times)
470 * subject (Name)
471 * subjectPublicKeyInfo (SEQ)
472 * ...
473 *
474 * subjectPublicKeyInfo is a sequence of:
475 * algorithm (AlgorithmIdentifier)
476 * publicKey (BIT STRING)
477 *
478 * Essentially, we want subjectPublicKeyInfo.publicKey
479 */
480 uint8_t *end = der + len;
481
482 /* enter "Certificate" item: [der, end) will be only Cert */
483 der = enter_der_item(der, &end);
484
485 /* enter "tbsCertificate" item: [der, end) will be only tbsCert */
486 der = enter_der_item(der, &end);
487
488 /* skip up to subjectPublicKeyInfo */
489 der = skip_der_item(der, end); /* version */
490 der = skip_der_item(der, end); /* serialNumber */
491 der = skip_der_item(der, end); /* signatureAlgo */
492 der = skip_der_item(der, end); /* issuer */
493 der = skip_der_item(der, end); /* validity */
494 der = skip_der_item(der, end); /* subject */
495
496 /* enter "subjectPublicKeyInfo" */
497 der = enter_der_item(der, &end);
498
499 /* skip "subjectPublicKeyInfo.algorithm" */
500 der = skip_der_item(der, end);
501 /* enter "subjectPublicKeyInfo.publicKey" */
502// die_if_not_this_der_type(der, end, 0x03); /* must be BITSTRING */
503 der = enter_der_item(der, &end);
504
505 /* return a copy */
506 *key_len = end - der;
507 dbg("copying key bytes:%u, first:0x%02x\n", *key_len, der[0]);
508 return xmemdup(der, *key_len);
509}
510
511static void get_server_cert_or_die(tls_state_t *tls)
512{
513 struct transport_hdr *xhdr;
514 uint8_t *certbuf;
515 int len, len1;
516
517 len = xread_tls_block(tls);
518 xhdr = (void*)tls->inbuf;
519 if (len < sizeof(*xhdr) + 10
520 || xhdr->type != RECORD_TYPE_HANDSHAKE
521 || xhdr->proto_maj != TLS_MAJ
522 || xhdr->proto_min != TLS_MIN
523 ) {
524 tls_error_die(tls);
525 }
526 dbg("got HANDSHAKE\n");
527 certbuf = (void*)(xhdr + 1);
528 if (certbuf[0] != HANDSHAKE_CERTIFICATE)
529 tls_error_die(tls);
530 dbg("got CERTIFICATE\n");
531 // 0b 00|11|24 00|11|21 00|05|b0 30|82|05|ac|30|82|04|94|a0|03|02|01|02|02|11|00|9f|85|bf|66|4b|0c|dd|af|ca|50|86|79|50|1b|2b|e4|30|0d... (4392 bytes)
532 // Cert len=4388 ChainLen CertLen^ DER encoded X509 starts here. openssl x509 -in FILE -inform DER -noout -text
533 len1 = get24be(certbuf + 1);
534 if (len1 > len - 4) tls_error_die(tls);
535 len = len1;
536
537 len1 = get24be(certbuf + 4);
538 if (len1 > len - 3) tls_error_die(tls);
539 len = len1;
540
541 len1 = get24be(certbuf + 7);
542 if (len1 > len - 3) tls_error_die(tls);
543 len = len1;
544
545 if (len)
546 tls->pubkey = find_key_in_der_cert(&tls->pubkey_len, certbuf + 10, len);
547}
548
549static void tls_handshake(tls_state_t *tls)
550{
551 // Client RFC 5246 Server
552 // (*) - optional messages, not always sent
553 //
554 // ClientHello ------->
555 // ServerHello
556 // Certificate*
557 // ServerKeyExchange*
558 // CertificateRequest*
559 // <------- ServerHelloDone
560 // Certificate*
561 // ClientKeyExchange
562 // CertificateVerify*
563 // [ChangeCipherSpec]
564 // Finished ------->
565 // [ChangeCipherSpec]
566 // <------- Finished
567 // Application Data <------> Application Data
568 int len;
569
570 send_client_hello(tls);
571#if 0 /* dump */
572 for (;;) {
573 uint8_t buf[16*1024];
574 sleep(2);
575 len = recv(tls->fd, buf, sizeof(buf), 0); //MSG_DONTWAIT);
576 if (len < 0) {
577 if (errno == EAGAIN)
578 continue;
579 bb_perror_msg_and_die("recv");
580 }
581 if (len == 0)
582 break;
583 dump(buf, len);
584 }
585#endif
586
587 get_server_hello_or_die(tls);
588
589 //RFC 5246
590 // The server MUST send a Certificate message whenever the agreed-
591 // upon key exchange method uses certificates for authentication
592 // (this includes all key exchange methods defined in this document
593 // except DH_anon). This message will always immediately follow the
594 // ServerHello message.
595 //
596 // IOW: in practice, Certificate *always* follows.
597 // (for example, kernel.org does not even accept DH_anon cipher id)
598 get_server_cert_or_die(tls);
599
600 len = xread_tls_block(tls);
601 /* Next handshake type is not predetermined */
602 switch (tls->inbuf[5]) {
603 case HANDSHAKE_SERVER_KEY_EXCHANGE:
604 //0c 0001c7 03|00|17|41|04|87|94|2e|2f|68|d0|c9|f4|97|a8|2d|ef|ed|67|ea|c6|f3|b3|56|47|5d|27|b6|bd|ee|70|25|30|5e|b0|8e|f6|21|5a... 459 bytes
605 //SvKey len^^^
606 dbg("got SERVER_KEY_EXCHANGE\n");
607 len = xread_tls_block(tls);
608 break;
609 case HANDSHAKE_CERTIFICATE_REQUEST:
610 dbg("got CERTIFICATE_REQUEST\n");
611 len = xread_tls_block(tls);
612 break;
613 case HANDSHAKE_SERVER_HELLO_DONE:
614 // 0e 000000 (len:0)
615 dbg("got SERVER_HELLO_DONE\n");
616 break;
617 default:
618 tls_error_die(tls);
619 }
620}
621
622int tls_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
623int tls_main(int argc UNUSED_PARAM, char **argv)
624{
625 tls_state_t *tls;
626 len_and_sockaddr *lsa;
627 int fd;
628
629 // INIT_G();
630 // getopt32(argv, "myopts")
631
632 if (!argv[1])
633 bb_show_usage();
634
635 lsa = xhost2sockaddr(argv[1], 443);
636 fd = xconnect_stream(lsa);
637
638 tls = new_tls_state();
639 tls->fd = fd;
640 tls_handshake(tls);
641
642 return EXIT_SUCCESS;
643}