Subversion Repositories planix.SVN

Rev

Rev 22 | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 22 Rev 77
Line 1... Line 1...
1
/*
1
/*
2
 *  devtls - record layer for transport layer security 1.0 and secure sockets layer 3.0
2
 *  devtls - record layer for transport layer security 1.2 and secure sockets layer 3.0
3
 */
3
 */
4
#include	"u.h"
4
#include	"u.h"
5
#include	"../port/lib.h"
5
#include	"../port/lib.h"
6
#include	"mem.h"
6
#include	"mem.h"
7
#include	"dat.h"
7
#include	"dat.h"
Line 9... Line 9...
9
#include	"../port/error.h"
9
#include	"../port/error.h"
10
 
10
 
11
#include	<libsec.h>
11
#include	<libsec.h>
12
 
12
 
13
typedef struct OneWay	OneWay;
13
typedef struct OneWay	OneWay;
14
typedef struct Secret		Secret;
14
typedef struct Secret	Secret;
15
typedef struct TlsRec	TlsRec;
15
typedef struct TlsRec	TlsRec;
16
typedef struct TlsErrs	TlsErrs;
16
typedef struct TlsErrs	TlsErrs;
17
 
17
 
18
enum {
18
enum {
19
	Statlen=	1024,		/* max. length of status or stats message */
19
	Statlen=	1024,		/* max. length of status or stats message */
20
	/* buffer limits */
20
	/* buffer limits */
21
	MaxRecLen		= 1<<14,	/* max payload length of a record layer message */
21
	MaxRecLen	= 1<<14,	/* max payload length of a record layer message */
22
	MaxCipherRecLen	= MaxRecLen + 2048,
22
	MaxCipherRecLen	= MaxRecLen + 2048,
23
	RecHdrLen		= 5,
23
	RecHdrLen	= 5,
24
	MaxMacLen		= SHA1dlen,
24
	MaxMacLen	= SHA2_256dlen,
25
 
25
 
26
	/* protocol versions we can accept */
26
	/* protocol versions we can accept */
-
 
27
	SSL3Version	= 0x0300,
27
	TLSVersion		= 0x0301,
28
	TLS10Version	= 0x0301,
28
	SSL3Version		= 0x0300,
29
	TLS11Version	= 0x0302,
29
	ProtocolVersion	= 0x0301,	/* maximum version we speak */
30
	TLS12Version	= 0x0303,
30
	MinProtoVersion	= 0x0300,	/* limits on version we accept */
31
	MinProtoVersion	= 0x0300,	/* limits on version we accept */
31
	MaxProtoVersion	= 0x03ff,
32
	MaxProtoVersion	= 0x03ff,
32
 
33
 
33
	/* connection states */
34
	/* connection states */
34
	SHandshake	= 1 << 0,	/* doing handshake */
35
	SHandshake	= 1 << 0,	/* doing handshake */
Line 71... Line 72...
71
	EProtocolVersion 		= 70,
72
	EProtocolVersion 		= 70,
72
	EInsufficientSecurity 	= 71,
73
	EInsufficientSecurity 	= 71,
73
	EInternalError 			= 80,
74
	EInternalError 			= 80,
74
	EUserCanceled 			= 90,
75
	EUserCanceled 			= 90,
75
	ENoRenegotiation 		= 100,
76
	ENoRenegotiation 		= 100,
-
 
77
	EUnrecognizedName		= 112,
76
 
78
 
77
	EMAX = 256
79
	EMAX = 256
78
};
80
};
79
 
81
 
80
struct Secret
82
struct Secret
81
{
83
{
82
	char		*encalg;	/* name of encryption alg */
84
	char		*encalg;	/* name of encryption alg */
83
	char		*hashalg;	/* name of hash alg */
85
	char		*hashalg;	/* name of hash alg */
-
 
86
 
-
 
87
	int		(*aead_enc)(Secret*, uchar*, int, uchar*, uchar*, int);
-
 
88
	int		(*aead_dec)(Secret*, uchar*, int, uchar*, uchar*, int);
-
 
89
 
84
	int		(*enc)(Secret*, uchar*, int);
90
	int		(*enc)(Secret*, uchar*, int);
85
	int		(*dec)(Secret*, uchar*, int);
91
	int		(*dec)(Secret*, uchar*, int);
86
	int		(*unpad)(uchar*, int, int);
92
	int		(*unpad)(uchar*, int, int);
87
	DigestState	*(*mac)(uchar*, ulong, uchar*, ulong, uchar*, DigestState*);
93
	DigestState*	(*mac)(uchar*, ulong, uchar*, ulong, uchar*, DigestState*);
-
 
94
 
88
	int		block;		/* encryption block len, 0 if none */
95
	int		block;		/* encryption block len, 0 if none */
89
	int		maclen;
96
	int		maclen;		/* # bytes of record mac / authentication tag */
-
 
97
	int		recivlen;	/* # bytes of record iv for AEAD ciphers */
90
	void		*enckey;
98
	void		*enckey;
91
	uchar	mackey[MaxMacLen];
99
	uchar		mackey[MaxMacLen];
92
};
100
};
93
 
101
 
94
struct OneWay
102
struct OneWay
95
{
103
{
96
	QLock		io;		/* locks io access */
104
	QLock		io;		/* locks io access */
97
	QLock		seclock;	/* locks secret paramaters */
105
	QLock		seclock;	/* locks secret paramaters */
98
	ulong		seq;
106
	u64int		seq;
99
	Secret		*sec;		/* cipher in use */
107
	Secret		*sec;		/* cipher in use */
100
	Secret		*new;		/* cipher waiting for enable */
108
	Secret		*new;		/* cipher waiting for enable */
101
};
109
};
102
 
110
 
103
struct TlsRec
111
struct TlsRec
Line 115... Line 123...
115
 
123
 
116
	Lock		statelk;
124
	Lock		statelk;
117
	int		state;
125
	int		state;
118
	int		debug;
126
	int		debug;
119
 
127
 
-
 
128
	/*
120
	/* record layer mac functions for different protocol versions */
129
	 * function to genrate authenticated data blob for different
-
 
130
	 * protocol versions
-
 
131
	 */
121
	void		(*packMac)(Secret*, uchar*, uchar*, uchar*, uchar*, int, uchar*);
132
	int		(*packAAD)(u64int, uchar*, uchar*);
122
 
133
 
123
	/* input side -- protected by in.io */
134
	/* input side -- protected by in.io */
124
	OneWay		in;
135
	OneWay		in;
125
	Block		*processed;	/* next bunch of application data */
136
	Block		*processed;	/* next bunch of application data */
126
	Block		*unprocessed;	/* data read from c but not parsed into records */
137
	Block		*unprocessed;	/* data read from c but not parsed into records */
Line 177... Line 188...
177
 
188
 
178
enum
189
enum
179
{
190
{
180
	/* max. open tls connections */
191
	/* max. open tls connections */
181
	MaxTlsDevs	= 1024
192
	MaxTlsDevs	= 1024
182
};
193
};
183
 
194
 
184
static	Lock	tdlock;
195
static	Lock	tdlock;
185
static	int	tdhiwat;
196
static	int	tdhiwat;
186
static	int	maxtlsdevs = 128;
197
static	int	maxtlsdevs = 128;
187
static	TlsRec	**tlsdevs;
198
static	TlsRec	**tlsdevs;
Line 217... Line 228...
217
static TlsRec	*newtls(Chan *c);
228
static TlsRec	*newtls(Chan *c);
218
static TlsRec	*mktlsrec(void);
229
static TlsRec	*mktlsrec(void);
219
static DigestState*sslmac_md5(uchar *p, ulong len, uchar *key, ulong klen, uchar *digest, DigestState *s);
230
static DigestState*sslmac_md5(uchar *p, ulong len, uchar *key, ulong klen, uchar *digest, DigestState *s);
220
static DigestState*sslmac_sha1(uchar *p, ulong len, uchar *key, ulong klen, uchar *digest, DigestState *s);
231
static DigestState*sslmac_sha1(uchar *p, ulong len, uchar *key, ulong klen, uchar *digest, DigestState *s);
221
static DigestState*nomac(uchar *p, ulong len, uchar *key, ulong klen, uchar *digest, DigestState *s);
232
static DigestState*nomac(uchar *p, ulong len, uchar *key, ulong klen, uchar *digest, DigestState *s);
222
static void	sslPackMac(Secret *sec, uchar *mackey, uchar *seq, uchar *header, uchar *body, int len, uchar *mac);
233
static int	sslPackAAD(u64int, uchar*, uchar*);
-
 
234
static int	tlsPackAAD(u64int, uchar*, uchar*);
223
static void	tlsPackMac(Secret *sec, uchar *mackey, uchar *seq, uchar *header, uchar *body, int len, uchar *mac);
235
static void	packMac(Secret*, uchar*, int, uchar*, int, uchar*);
224
static void	put64(uchar *p, vlong x);
236
static void	put64(uchar *p, u64int);
225
static void	put32(uchar *p, u32int);
237
static void	put32(uchar *p, u32int);
226
static void	put24(uchar *p, int);
238
static void	put24(uchar *p, int);
227
static void	put16(uchar *p, int);
239
static void	put16(uchar *p, int);
228
static u32int	get32(uchar *p);
-
 
229
static int	get16(uchar *p);
240
static int	get16(uchar *p);
230
static void	tlsSetState(TlsRec *tr, int new, int old);
241
static void	tlsSetState(TlsRec *tr, int new, int old);
231
static void	rcvAlert(TlsRec *tr, int err);
242
static void	rcvAlert(TlsRec *tr, int err);
232
static void	sendAlert(TlsRec *tr, int err);
243
static void	sendAlert(TlsRec *tr, int err);
233
static void	rcvError(TlsRec *tr, int err, char *msg, ...);
244
static void	rcvError(TlsRec *tr, int err, char *msg, ...);
234
static int	rc4enc(Secret *sec, uchar *buf, int n);
245
static int	rc4enc(Secret *sec, uchar *buf, int n);
235
static int	des3enc(Secret *sec, uchar *buf, int n);
246
static int	des3enc(Secret *sec, uchar *buf, int n);
236
static int	des3dec(Secret *sec, uchar *buf, int n);
247
static int	des3dec(Secret *sec, uchar *buf, int n);
237
static int	aesenc(Secret *sec, uchar *buf, int n);
248
static int	aesenc(Secret *sec, uchar *buf, int n);
238
static int	aesdec(Secret *sec, uchar *buf, int n);
249
static int	aesdec(Secret *sec, uchar *buf, int n);
-
 
250
static int	ccpoly_aead_enc(Secret *sec, uchar *aad, int aadlen, uchar *reciv, uchar *data, int len);
-
 
251
static int	ccpoly_aead_dec(Secret *sec, uchar *aad, int aadlen, uchar *reciv, uchar *data, int len);
-
 
252
static int	aesgcm_aead_enc(Secret *sec, uchar *aad, int aadlen, uchar *reciv, uchar *data, int len);
-
 
253
static int	aesgcm_aead_dec(Secret *sec, uchar *aad, int aadlen, uchar *reciv, uchar *data, int len);
239
static int	noenc(Secret *sec, uchar *buf, int n);
254
static int	noenc(Secret *sec, uchar *buf, int n);
240
static int	sslunpad(uchar *buf, int n, int block);
255
static int	sslunpad(uchar *buf, int n, int block);
241
static int	tlsunpad(uchar *buf, int n, int block);
256
static int	tlsunpad(uchar *buf, int n, int block);
242
static void	freeSec(Secret *sec);
257
static void	freeSec(Secret *sec);
243
static char	*tlsstate(int s);
258
static char	*tlsstate(int s);
Line 274... Line 289...
274
	case Qtopdir:
289
	case Qtopdir:
275
		if(s == DEVDOTDOT){
290
		if(s == DEVDOTDOT){
276
			q.path = QID(0, Qtopdir);
291
			q.path = QID(0, Qtopdir);
277
			q.type = QTDIR;
292
			q.type = QTDIR;
278
			devdir(c, q, "#a", 0, eve, 0555, dp);
293
			devdir(c, q, "#a", 0, eve, 0555, dp);
279
			return 1;
294
			return 1;
280
		}
295
		}
281
		if(s > 0)
296
		if(s > 0)
282
			return -1;
297
			return -1;
283
		q.path = QID(0, Qprotodir);
298
		q.path = QID(0, Qprotodir);
284
		q.type = QTDIR;
299
		q.type = QTDIR;
Line 322... Line 337...
322
			nm = tr->user;
337
			nm = tr->user;
323
		else
338
		else
324
			nm = eve;
339
			nm = eve;
325
		if((name = trnames[s]) == nil){
340
		if((name = trnames[s]) == nil){
326
			name = trnames[s] = smalloc(16);
341
			name = trnames[s] = smalloc(16);
327
			snprint(name, 16, "%d", s);
342
			sprint(name, "%d", s);
328
		}
343
		}
329
		devdir(c, q, name, 0, nm, 0555, dp);
344
		devdir(c, q, name, 0, nm, 0555, dp);
330
		unlock(&tdlock);
345
		unlock(&tdlock);
331
		return 1;
346
		return 1;
332
	case Qconvdir:
347
	case Qconvdir:
Line 335... Line 350...
335
			q.type = QTDIR;
350
			q.type = QTDIR;
336
			devdir(c, q, "tls", 0, eve, 0555, dp);
351
			devdir(c, q, "tls", 0, eve, 0555, dp);
337
			return 1;
352
			return 1;
338
		}
353
		}
339
		if(s < 0 || s >= nelem(convdir))
354
		if(s < 0 || s >= nelem(convdir))
340
			return -1;
355
			return -1;
341
		lock(&tdlock);
356
		lock(&tdlock);
342
		tr = tlsdevs[CONV(c->qid)];
357
		tr = tlsdevs[CONV(c->qid)];
343
		if(tr != nil){
358
		if(tr != nil){
344
			nm = tr->user;
359
			nm = tr->user;
345
			perm = tr->perm;
360
			perm = tr->perm;
346
		}else{
361
		}else{
347
			perm = 0;
362
			perm = 0;
Line 394... Line 409...
394
 
409
 
395
static Walkqid*
410
static Walkqid*
396
tlswalk(Chan *c, Chan *nc, char **name, int nname)
411
tlswalk(Chan *c, Chan *nc, char **name, int nname)
397
{
412
{
398
	return devwalk(c, nc, name, nname, nil, 0, tlsgen);
413
	return devwalk(c, nc, name, nname, nil, 0, tlsgen);
399
}
414
}
400
 
415
 
401
static int
416
static int
402
tlsstat(Chan *c, uchar *db, int n)
417
tlsstat(Chan *c, uchar *db, int n)
403
{
418
{
404
	return devstat(c, db, n, nil, 0, tlsgen);
419
	return devstat(c, db, n, nil, 0, tlsgen);
405
}
420
}
406
 
421
 
407
static Chan*
422
static Chan*
408
tlsopen(Chan *c, int omode)
423
tlsopen(Chan *c, int omode)
409
{
424
{
410
	TlsRec *tr, **pp;
425
	TlsRec *tr, **pp;
411
	int t, perm;
426
	int t;
412
 
-
 
413
	perm = 0;
-
 
414
	omode &= 3;
-
 
415
	switch(omode) {
-
 
416
	case OREAD:
-
 
417
		perm = 4;
-
 
418
		break;
-
 
419
	case OWRITE:
-
 
420
		perm = 2;
-
 
421
		break;
-
 
422
	case ORDWR:
-
 
423
		perm = 6;
-
 
424
		break;
-
 
425
	}
-
 
426
 
427
 
427
	t = TYPE(c->qid);
428
	t = TYPE(c->qid);
428
	switch(t) {
429
	switch(t) {
429
	default:
430
	default:
430
		panic("tlsopen");
431
		panic("tlsopen");
Line 436... Line 437...
436
		break;
437
		break;
437
	case Qclonus:
438
	case Qclonus:
438
		tr = newtls(c);
439
		tr = newtls(c);
439
		if(tr == nil)
440
		if(tr == nil)
440
			error(Enodev);
441
			error(Enodev);
441
		break;
442
		break;
442
	case Qctl:
443
	case Qctl:
443
	case Qdata:
444
	case Qdata:
444
	case Qhand:
445
	case Qhand:
445
	case Qstatus:
446
	case Qstatus:
446
	case Qstats:
447
	case Qstats:
447
		if((t == Qstatus || t == Qstats) && omode != OREAD)
448
		if((t == Qstatus || t == Qstats) && omode != OREAD)
448
			error(Eperm);
449
			error(Eperm);
449
		if(waserror()) {
450
		if(waserror()) {
450
			unlock(&tdlock);
451
			unlock(&tdlock);
Line 453... Line 454...
453
		lock(&tdlock);
454
		lock(&tdlock);
454
		pp = &tlsdevs[CONV(c->qid)];
455
		pp = &tlsdevs[CONV(c->qid)];
455
		tr = *pp;
456
		tr = *pp;
456
		if(tr == nil)
457
		if(tr == nil)
457
			error("must open connection using clone");
458
			error("must open connection using clone");
458
		if((perm & (tr->perm>>6)) != perm
-
 
459
		&& (strcmp(up->user, tr->user) != 0
459
		devpermcheck(tr->user, tr->perm, omode);
460
		    || (perm & tr->perm) != perm))
-
 
461
			error(Eperm);
-
 
462
		if(t == Qhand){
460
		if(t == Qhand){
463
			if(waserror()){
461
			if(waserror()){
464
				unlock(&tr->hqlock);
462
				unlock(&tr->hqlock);
465
				nexterror();
463
				nexterror();
466
			}
464
			}
Line 475... Line 473...
475
			poperror();
473
			poperror();
476
		}
474
		}
477
		tr->ref++;
475
		tr->ref++;
478
		unlock(&tdlock);
476
		unlock(&tdlock);
479
		poperror();
477
		poperror();
480
		break;
478
		break;
481
	case Qencalgs:
479
	case Qencalgs:
482
	case Qhashalgs:
480
	case Qhashalgs:
483
		if(omode != OREAD)
481
		if(omode != OREAD)
484
			error(Eperm);
482
			error(Eperm);
485
		break;
483
		break;
486
	}
484
	}
487
	c->mode = openmode(omode);
485
	c->mode = openmode(omode);
488
	c->flag |= COPEN;
486
	c->flag |= COPEN;
489
	c->offset = 0;
487
	c->offset = 0;
490
	c->iounit = qiomaxatomic;
488
	c->iounit = MaxRecLen;
491
	return c;
489
	return c;
492
}
490
}
493
 
491
 
494
static int
492
static int
495
tlswstat(Chan *c, uchar *dp, int n)
493
tlswstat(Chan *c, uchar *dp, int n)
Line 516... Line 514...
516
	rv = convM2D(dp, n, &d[0], (char*) &d[1]);
514
	rv = convM2D(dp, n, &d[0], (char*) &d[1]);
517
	if(rv == 0)
515
	if(rv == 0)
518
		error(Eshortstat);
516
		error(Eshortstat);
519
	if(!emptystr(d->uid))
517
	if(!emptystr(d->uid))
520
		kstrdup(&tr->user, d->uid);
518
		kstrdup(&tr->user, d->uid);
521
	if(d->mode != ~0UL)
519
	if(d->mode != -1)
522
		tr->perm = d->mode;
520
		tr->perm = d->mode;
523
 
521
 
524
	free(d);
522
	free(d);
525
	poperror();
523
	poperror();
526
	unlock(&tdlock);
524
	unlock(&tdlock);
Line 538... Line 536...
538
			tr->handq = nil;
536
			tr->handq = nil;
539
		}
537
		}
540
		if(tr->hprocessed != nil){
538
		if(tr->hprocessed != nil){
541
			freeb(tr->hprocessed);
539
			freeb(tr->hprocessed);
542
			tr->hprocessed = nil;
540
			tr->hprocessed = nil;
543
		}
541
		}
544
	}
542
	}
545
	unlock(&tr->hqlock);
543
	unlock(&tr->hqlock);
546
}
544
}
547
 
545
 
548
static void
546
static void
Line 600... Line 598...
600
static void
598
static void
601
ensure(TlsRec *s, Block **l, int n)
599
ensure(TlsRec *s, Block **l, int n)
602
{
600
{
603
	int sofar, i;
601
	int sofar, i;
604
	Block *b, *bl;
602
	Block *b, *bl;
605
 
603
 
606
	sofar = 0;
604
	sofar = 0;
607
	for(b = *l; b; b = b->next){
605
	for(b = *l; b; b = b->next){
608
		sofar += BLEN(b);
606
		sofar += BLEN(b);
609
		if(sofar >= n)
607
		if(sofar >= n)
610
			return;
608
			return;
Line 690... Line 688...
690
	b = *l;
688
	b = *l;
691
	if(BLEN(b) == n){
689
	if(BLEN(b) == n){
692
		*l = b->next;
690
		*l = b->next;
693
		b->next = nil;
691
		b->next = nil;
694
		return b;
692
		return b;
695
	}
693
	}
696
 
694
 
697
	i = 0;
695
	i = 0;
698
	for(bb = b; bb != nil && i < n; bb = bb->next)
696
	for(bb = b; bb != nil && i < n; bb = bb->next)
699
		i += BLEN(bb);
697
		i += BLEN(bb);
700
	if(i > n)
698
	if(i > n)
701
		i = n;
699
		i = n;
Line 730... Line 728...
730
static void
728
static void
731
tlsrecread(TlsRec *tr)
729
tlsrecread(TlsRec *tr)
732
{
730
{
733
	OneWay *volatile in;
731
	OneWay *volatile in;
734
	Block *volatile b;
732
	Block *volatile b;
735
	uchar *p, seq[8], header[RecHdrLen], hmac[MaxMacLen];
733
	uchar *p, aad[8+RecHdrLen], header[RecHdrLen], hmac[MaxMacLen];
736
	int volatile nconsumed;
734
	int volatile nconsumed;
737
	int len, type, ver, unpad_len;
735
	int len, type, ver, unpad_len, aadlen, ivlen;
-
 
736
	Secret *sec;
738
 
737
 
739
	nconsumed = 0;
738
	nconsumed = 0;
740
	if(waserror()){
739
	if(waserror()){
741
		if(strcmp(up->errstr, Eintr) == 0 && !waserror()){
740
		if(strcmp(up->errstr, Eintr) == 0 && !waserror()){
742
			regurgitate(tr, header, nconsumed);
741
			regurgitate(tr, header, nconsumed);
Line 782... Line 781...
782
	b = nil;
781
	b = nil;
783
	if(waserror()){
782
	if(waserror()){
784
		if(b != nil)
783
		if(b != nil)
785
			freeb(b);
784
			freeb(b);
786
		tlsError(tr, "channel error");
785
		tlsError(tr, "channel error");
787
		nexterror();
786
		nexterror();
788
	}
787
	}
789
	b = qgrab(&tr->unprocessed, len);
788
	b = qgrab(&tr->unprocessed, len);
790
if(tr->debug) pprint("consumed unprocessed %d\n", len);
789
if(tr->debug) pprint("consumed unprocessed %d\n", len);
791
 
790
 
792
	in = &tr->in;
791
	in = &tr->in;
793
	if(waserror()){
792
	if(waserror()){
794
		qunlock(&in->seclock);
793
		qunlock(&in->seclock);
795
		nexterror();
794
		nexterror();
796
	}
795
	}
797
	qlock(&in->seclock);
796
	qlock(&in->seclock);
798
	p = b->rp;
797
	p = b->rp;
-
 
798
	sec = in->sec;
799
	if(in->sec != nil) {
799
	if(sec != nil) {
800
		/* to avoid Canvel-Hiltgen-Vaudenay-Vuagnoux attack, all errors here
800
		/* to avoid Canvel-Hiltgen-Vaudenay-Vuagnoux attack, all errors here
801
		        should look alike, including timing of the response. */
801
		        should look alike, including timing of the response. */
802
		unpad_len = (*in->sec->dec)(in->sec, p, len);
802
		if(sec->aead_dec != nil)
803
		if(unpad_len >= in->sec->maclen)
803
			unpad_len = len;
-
 
804
		else {
804
			len = unpad_len - in->sec->maclen;
805
			unpad_len = (*sec->dec)(sec, p, len);
805
if(tr->debug) pprint("decrypted %d\n", unpad_len);
806
if(tr->debug) pprint("decrypted %d\n", unpad_len);
806
if(tr->debug) pdump(unpad_len, p, "decrypted:");
807
if(tr->debug) pdump(unpad_len, p, "decrypted:");
-
 
808
		}
-
 
809
 
-
 
810
		ivlen = sec->recivlen;
-
 
811
		if(tr->version >= TLS11Version){
-
 
812
			if(ivlen == 0)
-
 
813
				ivlen = sec->block;
-
 
814
		}
-
 
815
		len -= ivlen;
-
 
816
		if(len < 0)
-
 
817
			rcvError(tr, EDecodeError, "runt record message");
-
 
818
		unpad_len -= ivlen;
-
 
819
		p += ivlen;
-
 
820
 
-
 
821
		if(unpad_len >= sec->maclen)
-
 
822
			len = unpad_len - sec->maclen;
807
 
823
 
808
		/* update length */
824
		/* update length */
809
		put16(header+3, len);
825
		put16(header+3, len);
-
 
826
		aadlen = (*tr->packAAD)(in->seq++, header, aad);
-
 
827
		if(sec->aead_dec != nil) {
-
 
828
			len = (*sec->aead_dec)(sec, aad, aadlen, p - ivlen, p, unpad_len);
810
		put64(seq, in->seq);
829
			if(len < 0)
-
 
830
				rcvError(tr, EBadRecordMac, "record mac mismatch");
811
		in->seq++;
831
		} else {
812
		(*tr->packMac)(in->sec, in->sec->mackey, seq, header, p, len, hmac);
832
			packMac(sec, aad, aadlen, p, len, hmac);
813
		if(unpad_len < in->sec->maclen)
833
			if(unpad_len < sec->maclen)
814
			rcvError(tr, EBadRecordMac, "short record mac");
834
				rcvError(tr, EBadRecordMac, "short record mac");
815
		if(memcmp(hmac, p+len, in->sec->maclen) != 0)
835
			if(tsmemcmp(hmac, p + len, sec->maclen) != 0)
816
			rcvError(tr, EBadRecordMac, "record mac mismatch");
836
				rcvError(tr, EBadRecordMac, "record mac mismatch");
-
 
837
		}
-
 
838
		b->rp = p;
817
		b->wp = b->rp + len;
839
		b->wp = p+len;
818
	}
840
	}
819
	qunlock(&in->seclock);
841
	qunlock(&in->seclock);
820
	poperror();
842
	poperror();
821
	if(len < 0)
843
	if(len < 0)
822
		rcvError(tr, EDecodeError, "runt record message");
844
		rcvError(tr, EDecodeError, "runt record message");
Line 846... Line 868...
846
			rcvAlert(tr, p[1]);
868
			rcvAlert(tr, p[1]);
847
		if(p[0] != 1)
869
		if(p[0] != 1)
848
			rcvError(tr, EIllegalParameter, "invalid alert fatal code");
870
			rcvError(tr, EIllegalParameter, "invalid alert fatal code");
849
 
871
 
850
		/*
872
		/*
851
		 * propate non-fatal alerts to handshaker
873
		 * propagate non-fatal alerts to handshaker
852
		 */
874
		 */
-
 
875
		switch(p[1]){
853
		if(p[1] == ECloseNotify) {
876
		case ECloseNotify:
854
			tlsclosed(tr, SRClose);
877
			tlsclosed(tr, SRClose);
855
			if(tr->opened)
878
			if(tr->opened)
856
				error("tls hungup");
879
				error("tls hungup");
857
			error("close notify");
880
			error("close notify");
858
		}
881
			break;
859
		if(p[1] == ENoRenegotiation)
882
		case ENoRenegotiation:
860
			alertHand(tr, "no renegotiation");
883
			alertHand(tr, "no renegotiation");
-
 
884
			break;
861
		else if(p[1] == EUserCanceled)
885
		case EUserCanceled:
862
			alertHand(tr, "handshake canceled by user");
886
			alertHand(tr, "handshake canceled by user");
-
 
887
			break;
-
 
888
		case EUnrecognizedName:
-
 
889
			/* happens in response to SNI, can be ignored. */
-
 
890
			break;
863
		else
891
		default:
864
			rcvError(tr, EIllegalParameter, "invalid alert code");
892
			rcvError(tr, EIllegalParameter, "invalid alert code");
-
 
893
		}
865
		break;
894
		break;
866
	case RHandshake:
895
	case RHandshake:
867
		/*
896
		/*
868
		 * don't worry about dropping the block
897
		 * don't worry about dropping the block
869
		 * qbwrite always queues even if flow controlled and interrupted.
898
		 * qbwrite always queues even if flow controlled and interrupted.
Line 1072... Line 1101...
1072
		while(tr->processed == nil)
1101
		while(tr->processed == nil)
1073
			tlsrecread(tr);
1102
			tlsrecread(tr);
1074
 
1103
 
1075
		/* return at most what was asked for */
1104
		/* return at most what was asked for */
1076
		b = qgrab(&tr->processed, n);
1105
		b = qgrab(&tr->processed, n);
1077
if(tr->debug) pprint("consumed processed %ld\n", BLEN(b));
1106
if(tr->debug) pprint("consumed processed %zd\n", BLEN(b));
1078
if(tr->debug) pdump(BLEN(b), b->rp, "consumed:");
1107
if(tr->debug) pdump(BLEN(b), b->rp, "consumed:");
1079
		qunlock(&tr->in.io);
1108
		qunlock(&tr->in.io);
1080
		poperror();
1109
		poperror();
1081
		tr->datain += BLEN(b);
1110
		tr->datain += BLEN(b);
1082
	}else{
1111
	}else{
Line 1112... Line 1141...
1112
		qunlock(&tr->hqread);
1141
		qunlock(&tr->hqread);
1113
		tr->handin += BLEN(b);
1142
		tr->handin += BLEN(b);
1114
	}
1143
	}
1115
 
1144
 
1116
	return b;
1145
	return b;
1117
}
1146
}
1118
 
1147
 
1119
static long
1148
static long
1120
tlsread(Chan *c, void *a, long n, vlong off)
1149
tlsread(Chan *c, void *a, long n, vlong off)
1121
{
1150
{
1122
	Block *volatile b;
1151
	Block *volatile b;
1123
	Block *nb;
1152
	Block *nb;
1124
	uchar *va;
1153
	uchar *va;
Line 1148... Line 1177...
1148
		if(tr->in.new != nil)
1177
		if(tr->in.new != nil)
1149
			s = seprint(s, e, "NewEncIn: %s\nNewHashIn: %s\n", tr->in.new->encalg, tr->in.new->hashalg);
1178
			s = seprint(s, e, "NewEncIn: %s\nNewHashIn: %s\n", tr->in.new->encalg, tr->in.new->hashalg);
1150
		if(tr->out.sec != nil)
1179
		if(tr->out.sec != nil)
1151
			s = seprint(s, e, "EncOut: %s\nHashOut: %s\n", tr->out.sec->encalg, tr->out.sec->hashalg);
1180
			s = seprint(s, e, "EncOut: %s\nHashOut: %s\n", tr->out.sec->encalg, tr->out.sec->hashalg);
1152
		if(tr->out.new != nil)
1181
		if(tr->out.new != nil)
1153
			seprint(s, e, "NewEncOut: %s\nNewHashOut: %s\n", tr->out.new->encalg, tr->out.new->hashalg);
1182
			s = seprint(s, e, "NewEncOut: %s\nNewHashOut: %s\n", tr->out.new->encalg, tr->out.new->hashalg);
-
 
1183
		if(tr->c != nil)
-
 
1184
			seprint(s, e, "Chan: %s\n", chanpath(tr->c));
1154
		qunlock(&tr->in.seclock);
1185
		qunlock(&tr->in.seclock);
1155
		qunlock(&tr->out.seclock);
1186
		qunlock(&tr->out.seclock);
1156
		n = readstr(offset, a, n, buf);
1187
		n = readstr(offset, a, n, buf);
1157
		free(buf);
1188
		free(buf);
1158
		return n;
1189
		return n;
Line 1208... Line 1239...
1208
static void
1239
static void
1209
tlsrecwrite(TlsRec *tr, int type, Block *b)
1240
tlsrecwrite(TlsRec *tr, int type, Block *b)
1210
{
1241
{
1211
	Block *volatile bb;
1242
	Block *volatile bb;
1212
	Block *nb;
1243
	Block *nb;
1213
	uchar *p, seq[8];
1244
	uchar *p, aad[8+RecHdrLen];
1214
	OneWay *volatile out;
1245
	OneWay *volatile out;
1215
	int n, maclen, pad, ok;
1246
	int n, ivlen, maclen, aadlen, pad, ok;
-
 
1247
	Secret *sec;
1216
 
1248
 
1217
	out = &tr->out;
1249
	out = &tr->out;
1218
	bb = b;
1250
	bb = b;
1219
	if(waserror()){
1251
	if(waserror()){
1220
		qunlock(&out->io);
1252
		qunlock(&out->io);
1221
		if(bb != nil)
1253
		if(bb != nil)
1222
			freeb(bb);
1254
			freeb(bb);
1223
		nexterror();
1255
		nexterror();
1224
	}
1256
	}
1225
	qlock(&out->io);
1257
	qlock(&out->io);
1226
if(tr->debug)pprint("send %ld\n", BLEN(b));
1258
if(tr->debug)pprint("send %zd\n", BLEN(b));
1227
if(tr->debug)pdump(BLEN(b), b->rp, "sent:");
1259
if(tr->debug)pdump(BLEN(b), b->rp, "sent:");
1228
 
1260
 
1229
 
1261
 
1230
	ok = SHandshake|SOpen|SRClose;
1262
	ok = SHandshake|SOpen|SRClose;
1231
	if(type == RAlert)
1263
	if(type == RAlert)
Line 1243... Line 1275...
1243
			nexterror();
1275
			nexterror();
1244
		}
1276
		}
1245
		qlock(&out->seclock);
1277
		qlock(&out->seclock);
1246
		maclen = 0;
1278
		maclen = 0;
1247
		pad = 0;
1279
		pad = 0;
-
 
1280
		ivlen = 0;
-
 
1281
		sec = out->sec;
1248
		if(out->sec != nil){
1282
		if(sec != nil){
1249
			maclen = out->sec->maclen;
1283
			maclen = sec->maclen;
1250
			pad = maclen + out->sec->block;
1284
			pad = maclen + sec->block;
-
 
1285
			ivlen = sec->recivlen;
-
 
1286
			if(tr->version >= TLS11Version){
-
 
1287
				if(ivlen == 0)
-
 
1288
					ivlen = sec->block;
-
 
1289
			}
1251
		}
1290
		}
1252
		n = BLEN(bb);
1291
		n = BLEN(bb);
1253
		if(n > MaxRecLen){
1292
		if(n > MaxRecLen){
1254
			n = MaxRecLen;
1293
			n = MaxRecLen;
1255
			nb = allocb(n + pad + RecHdrLen);
1294
			nb = allocb(RecHdrLen + ivlen + n + pad);
1256
			memmove(nb->wp + RecHdrLen, bb->rp, n);
1295
			memmove(nb->wp + RecHdrLen + ivlen, bb->rp, n);
1257
			bb->rp += n;
1296
			bb->rp += n;
1258
		}else{
1297
		}else{
1259
			/*
1298
			/*
1260
			 * carefully reuse bb so it will get freed if we're out of memory
1299
			 * carefully reuse bb so it will get freed if we're out of memory
1261
			 */
1300
			 */
1262
			bb = padblock(bb, RecHdrLen);
1301
			bb = padblock(bb, RecHdrLen + ivlen);
1263
			if(pad)
1302
			if(pad)
1264
				nb = padblock(bb, -pad);
1303
				nb = padblock(bb, -pad);
1265
			else
1304
			else
1266
				nb = bb;
1305
				nb = bb;
1267
			bb = nil;
1306
			bb = nil;
Line 1270... Line 1309...
1270
		p = nb->rp;
1309
		p = nb->rp;
1271
		p[0] = type;
1310
		p[0] = type;
1272
		put16(p+1, tr->version);
1311
		put16(p+1, tr->version);
1273
		put16(p+3, n);
1312
		put16(p+3, n);
1274
 
1313
 
1275
		if(out->sec != nil){
1314
		if(sec != nil){
1276
			put64(seq, out->seq);
1315
			aadlen = (*tr->packAAD)(out->seq++, p, aad);
1277
			out->seq++;
1316
			if(sec->aead_enc != nil)
1278
			(*tr->packMac)(out->sec, out->sec->mackey, seq, p, p + RecHdrLen, n, p + RecHdrLen + n);
1317
				n = (*sec->aead_enc)(sec, aad, aadlen, p + RecHdrLen, p + RecHdrLen + ivlen, n) + ivlen;
1279
			n += maclen;
1318
			else {
1280
 
-
 
1281
			/* encrypt */
1319
				if(ivlen > 0)
-
 
1320
					prng(p + RecHdrLen, ivlen);
-
 
1321
				packMac(sec, aad, aadlen, p + RecHdrLen + ivlen, n, p + RecHdrLen + ivlen + n);
1282
			n = (*out->sec->enc)(out->sec, p + RecHdrLen, n);
1322
				n = (*sec->enc)(sec, p + RecHdrLen, ivlen + n + maclen);
-
 
1323
			}
1283
			nb->wp = p + RecHdrLen + n;
1324
			nb->wp = p + RecHdrLen + n;
1284
 
1325
 
1285
			/* update length */
1326
			/* update length */
1286
			put16(p+3, n);
1327
			put16(p+3, n);
1287
		}
1328
		}
Line 1348... Line 1389...
1348
{
1389
{
1349
	char	*name;
1390
	char	*name;
1350
	int	maclen;
1391
	int	maclen;
1351
	void	(*initkey)(Hashalg *, int, Secret *, uchar*);
1392
	void	(*initkey)(Hashalg *, int, Secret *, uchar*);
1352
};
1393
};
1353
 
1394
 
1354
static void
1395
static void
1355
initmd5key(Hashalg *ha, int version, Secret *s, uchar *p)
1396
initmd5key(Hashalg *ha, int version, Secret *s, uchar *p)
1356
{
1397
{
1357
	s->maclen = ha->maclen;
1398
	s->maclen = ha->maclen;
1358
	if(version == SSL3Version)
1399
	if(version == SSL3Version)
1359
		s->mac = sslmac_md5;
1400
		s->mac = sslmac_md5;
1360
	else
1401
	else
1361
		s->mac = hmac_md5;
1402
		s->mac = hmac_md5;
1362
	memmove(s->mackey, p, ha->maclen);
1403
	memmove(s->mackey, p, ha->maclen);
1363
}
1404
}
1364
 
1405
 
1365
static void
1406
static void
1366
initclearmac(Hashalg *, int, Secret *s, uchar *)
1407
initclearmac(Hashalg *, int, Secret *s, uchar *)
1367
{
1408
{
1368
	s->maclen = 0;
-
 
1369
	s->mac = nomac;
1409
	s->mac = nomac;
1370
}
1410
}
1371
 
1411
 
1372
static void
1412
static void
1373
initsha1key(Hashalg *ha, int version, Secret *s, uchar *p)
1413
initsha1key(Hashalg *ha, int version, Secret *s, uchar *p)
1374
{
1414
{
1375
	s->maclen = ha->maclen;
1415
	s->maclen = ha->maclen;
1376
	if(version == SSL3Version)
1416
	if(version == SSL3Version)
1377
		s->mac = sslmac_sha1;
1417
		s->mac = sslmac_sha1;
1378
	else
1418
	else
1379
		s->mac = hmac_sha1;
1419
		s->mac = hmac_sha1;
-
 
1420
	memmove(s->mackey, p, ha->maclen);
-
 
1421
}
-
 
1422
 
-
 
1423
static void
-
 
1424
initsha2_256key(Hashalg *ha, int version, Secret *s, uchar *p)
-
 
1425
{
-
 
1426
	if(version == SSL3Version)
-
 
1427
		error("sha256 cannot be used with SSL");
-
 
1428
	s->maclen = ha->maclen;
-
 
1429
	s->mac = hmac_sha2_256;
1380
	memmove(s->mackey, p, ha->maclen);
1430
	memmove(s->mackey, p, ha->maclen);
1381
}
1431
}
1382
 
1432
 
1383
static Hashalg hashtab[] =
1433
static Hashalg hashtab[] =
1384
{
1434
{
1385
	{ "clear", 0, initclearmac, },
1435
	{ "clear",	0,		initclearmac, },
1386
	{ "md5", MD5dlen, initmd5key, },
1436
	{ "md5",	MD5dlen,	initmd5key, },
1387
	{ "sha1", SHA1dlen, initsha1key, },
1437
	{ "sha1",	SHA1dlen,	initsha1key, },
-
 
1438
	{ "sha256",	SHA2_256dlen,	initsha2_256key, },
1388
	{ 0 }
1439
	{ 0 }
1389
};
1440
};
1390
 
1441
 
1391
static Hashalg*
1442
static Hashalg*
1392
parsehashalg(char *p)
1443
parsehashalg(char *p)
1393
{
1444
{
1394
	Hashalg *ha;
1445
	Hashalg *ha;
1395
 
1446
 
1396
	for(ha = hashtab; ha->name; ha++)
1447
	for(ha = hashtab; ha->name; ha++)
1397
		if(strcmp(p, ha->name) == 0)
1448
		if(strcmp(p, ha->name) == 0)
1398
			return ha;
1449
			return ha;
1399
	error("unsupported hash algorithm");
1450
	error("unsupported hash algorithm");
1400
	return nil;
1451
	return nil;
Line 1409... Line 1460...
1409
	void	(*initkey)(Encalg *ea, Secret *, uchar*, uchar*);
1460
	void	(*initkey)(Encalg *ea, Secret *, uchar*, uchar*);
1410
};
1461
};
1411
 
1462
 
1412
static void
1463
static void
1413
initRC4key(Encalg *ea, Secret *s, uchar *p, uchar *)
1464
initRC4key(Encalg *ea, Secret *s, uchar *p, uchar *)
1414
{
1465
{
1415
	s->enckey = smalloc(sizeof(RC4state));
1466
	s->enckey = smalloc(sizeof(RC4state));
1416
	s->enc = rc4enc;
1467
	s->enc = rc4enc;
1417
	s->dec = rc4enc;
1468
	s->dec = rc4enc;
1418
	s->block = 0;
-
 
1419
	setupRC4state(s->enckey, p, ea->keylen);
1469
	setupRC4state(s->enckey, p, ea->keylen);
1420
}
1470
}
1421
 
1471
 
1422
static void
1472
static void
1423
initDES3key(Encalg *, Secret *s, uchar *p, uchar *iv)
1473
initDES3key(Encalg *, Secret *s, uchar *p, uchar *iv)
1424
{
1474
{
1425
	s->enckey = smalloc(sizeof(DES3state));
1475
	s->enckey = smalloc(sizeof(DES3state));
1426
	s->enc = des3enc;
1476
	s->enc = des3enc;
1427
	s->dec = des3dec;
1477
	s->dec = des3dec;
1428
	s->block = 8;
1478
	s->block = 8;
1429
	setupDES3state(s->enckey, (uchar(*)[8])p, iv);
1479
	setupDES3state(s->enckey, (uchar(*)[8])p, iv);
1430
}
1480
}
1431
 
1481
 
1432
static void
1482
static void
1433
initAESkey(Encalg *ea, Secret *s, uchar *p, uchar *iv)
1483
initAESkey(Encalg *ea, Secret *s, uchar *p, uchar *iv)
1434
{
1484
{
1435
	s->enckey = smalloc(sizeof(AESstate));
1485
	s->enckey = smalloc(sizeof(AESstate));
1436
	s->enc = aesenc;
1486
	s->enc = aesenc;
1437
	s->dec = aesdec;
1487
	s->dec = aesdec;
1438
	s->block = 16;
1488
	s->block = 16;
1439
	setupAESstate(s->enckey, p, ea->keylen, iv);
1489
	setupAESstate(s->enckey, p, ea->keylen, iv);
1440
}
1490
}
-
 
1491
 
-
 
1492
static void
-
 
1493
initccpolykey(Encalg *ea, Secret *s, uchar *p, uchar *iv)
-
 
1494
{
-
 
1495
	s->enckey = smalloc(sizeof(Chachastate));
-
 
1496
	s->aead_enc = ccpoly_aead_enc;
-
 
1497
	s->aead_dec = ccpoly_aead_dec;
-
 
1498
	s->maclen = Poly1305dlen;
-
 
1499
	if(ea->ivlen == 0) {
-
 
1500
		/* older draft version, iv is 64-bit sequence number */
-
 
1501
		setupChachastate(s->enckey, p, ea->keylen, nil, 64/8, 20);
-
 
1502
	} else {
-
 
1503
		/* IETF standard, 96-bit iv xored with sequence number */
-
 
1504
		memmove(s->mackey, iv, ea->ivlen);
-
 
1505
		setupChachastate(s->enckey, p, ea->keylen, iv, ea->ivlen, 20);
-
 
1506
	}
-
 
1507
}
-
 
1508
 
-
 
1509
static void
-
 
1510
initaesgcmkey(Encalg *ea, Secret *s, uchar *p, uchar *iv)
-
 
1511
{
-
 
1512
	s->enckey = smalloc(sizeof(AESGCMstate));
-
 
1513
	s->aead_enc = aesgcm_aead_enc;
-
 
1514
	s->aead_dec = aesgcm_aead_dec;
-
 
1515
	s->maclen = 16;
-
 
1516
	s->recivlen = 8;
-
 
1517
	memmove(s->mackey, iv, ea->ivlen);
-
 
1518
	prng(s->mackey + ea->ivlen, s->recivlen);
-
 
1519
	setupAESGCMstate(s->enckey, p, ea->keylen, nil, 0);
-
 
1520
}
1441
 
1521
 
1442
static void
1522
static void
1443
initclearenc(Encalg *, Secret *s, uchar *, uchar *)
1523
initclearenc(Encalg *, Secret *s, uchar *, uchar *)
1444
{
1524
{
1445
	s->enc = noenc;
1525
	s->enc = noenc;
1446
	s->dec = noenc;
1526
	s->dec = noenc;
1447
	s->block = 0;
-
 
1448
}
1527
}
1449
 
1528
 
1450
static Encalg encrypttab[] =
1529
static Encalg encrypttab[] =
1451
{
1530
{
1452
	{ "clear", 0, 0, initclearenc },
1531
	{ "clear", 0, 0, initclearenc },
1453
	{ "rc4_128", 128/8, 0, initRC4key },
1532
	{ "rc4_128", 128/8, 0, initRC4key },
1454
	{ "3des_ede_cbc", 3 * 8, 8, initDES3key },
1533
	{ "3des_ede_cbc", 3 * 8, 8, initDES3key },
1455
	{ "aes_128_cbc", 128/8, 16, initAESkey },
1534
	{ "aes_128_cbc", 128/8, 16, initAESkey },
1456
	{ "aes_256_cbc", 256/8, 16, initAESkey },
1535
	{ "aes_256_cbc", 256/8, 16, initAESkey },
-
 
1536
	{ "ccpoly64_aead", 256/8, 0, initccpolykey },
-
 
1537
	{ "ccpoly96_aead", 256/8, 96/8, initccpolykey },
-
 
1538
	{ "aes_128_gcm_aead", 128/8, 4, initaesgcmkey },
-
 
1539
	{ "aes_256_gcm_aead", 256/8, 4, initaesgcmkey },
1457
	{ 0 }
1540
	{ 0 }
1458
};
1541
};
1459
 
1542
 
1460
static Encalg*
1543
static Encalg*
1461
parseencalg(char *p)
1544
parseencalg(char *p)
Line 1493... Line 1576...
1493
	case Qhand:
1576
	case Qhand:
1494
		p = a;
1577
		p = a;
1495
		e = p + n;
1578
		e = p + n;
1496
		do{
1579
		do{
1497
			m = e - p;
1580
			m = e - p;
1498
			if(m > MaxRecLen)
1581
			if(m > c->iounit)
1499
				m = MaxRecLen;
1582
				m = c->iounit;
1500
 
1583
 
1501
			b = allocb(m);
1584
			b = allocb(m);
1502
			if(waserror()){
1585
			if(waserror()){
1503
				freeb(b);
1586
				freeb(b);
1504
				nexterror();
1587
				nexterror();
Line 1543... Line 1626...
1543
			error(Einuse);
1626
			error(Einuse);
1544
		m = strtol(cb->f[2], nil, 0);
1627
		m = strtol(cb->f[2], nil, 0);
1545
		if(m < MinProtoVersion || m > MaxProtoVersion)
1628
		if(m < MinProtoVersion || m > MaxProtoVersion)
1546
			error("unsupported version");
1629
			error("unsupported version");
1547
		tr->c = buftochan(cb->f[1]);
1630
		tr->c = buftochan(cb->f[1]);
1548
		tr->version = m;
1631
		tr->version = m;
1549
		tlsSetState(tr, SHandshake, SClosed);
1632
		tlsSetState(tr, SHandshake, SClosed);
1550
	}else if(strcmp(cb->f[0], "version") == 0){
1633
	}else if(strcmp(cb->f[0], "version") == 0){
1551
		if(cb->nf != 2)
1634
		if(cb->nf != 2)
1552
			error("usage: version vers");
1635
			error("usage: version vers");
1553
		if(tr->c == nil)
1636
		if(tr->c == nil)
1554
			error("must set fd before version");
1637
			error("must set fd before version");
1555
		if(tr->verset)
1638
		if(tr->verset)
1556
			error("version already set");
1639
			error("version already set");
1557
		m = strtol(cb->f[1], nil, 0);
1640
		m = strtol(cb->f[1], nil, 0);
-
 
1641
		if(m < MinProtoVersion || m > MaxProtoVersion)
-
 
1642
			error("unsupported version");
1558
		if(m == SSL3Version)
1643
		if(m == SSL3Version)
1559
			tr->packMac = sslPackMac;
1644
			tr->packAAD = sslPackAAD;
1560
		else if(m == TLSVersion)
-
 
1561
			tr->packMac = tlsPackMac;
-
 
1562
		else
1645
		else
1563
			error("unsupported version");
1646
			tr->packAAD = tlsPackAAD;
1564
		tr->verset = 1;
1647
		tr->verset = 1;
1565
		tr->version = m;
1648
		tr->version = m;
1566
	}else if(strcmp(cb->f[0], "secret") == 0){
1649
	}else if(strcmp(cb->f[0], "secret") == 0){
1567
		if(cb->nf != 5)
1650
		if(cb->nf != 5)
1568
			error("usage: secret hashalg encalg isclient secretdata");
1651
			error("usage: secret hashalg encalg isclient secretdata");
Line 1580... Line 1663...
1580
 
1663
 
1581
		ha = parsehashalg(cb->f[1]);
1664
		ha = parsehashalg(cb->f[1]);
1582
		ea = parseencalg(cb->f[2]);
1665
		ea = parseencalg(cb->f[2]);
1583
 
1666
 
1584
		p = cb->f[4];
1667
		p = cb->f[4];
1585
		m = (strlen(p)*3)/2;
1668
		m = (strlen(p)*3)/2 + 1;
1586
		x = smalloc(m);
1669
		x = smalloc(m);
1587
		tos = nil;
1670
		tos = smalloc(sizeof(Secret));
1588
		toc = nil;
1671
		toc = smalloc(sizeof(Secret));
1589
		if(waserror()){
1672
		if(waserror()){
-
 
1673
			free(x);
1590
			freeSec(tos);
1674
			freeSec(tos);
1591
			freeSec(toc);
1675
			freeSec(toc);
1592
			free(x);
-
 
1593
			nexterror();
1676
			nexterror();
1594
		}
1677
		}
-
 
1678
 
1595
		m = dec64(x, m, p, strlen(p));
1679
		m = dec64(x, m, p, strlen(p));
-
 
1680
		memset(p, 0, strlen(p));
1596
		if(m < 2 * ha->maclen + 2 * ea->keylen + 2 * ea->ivlen)
1681
		if(m < 2 * ha->maclen + 2 * ea->keylen + 2 * ea->ivlen)
1597
			error("not enough secret data provided");
1682
			error("not enough secret data provided");
1598
 
1683
 
1599
		tos = smalloc(sizeof(Secret));
-
 
1600
		toc = smalloc(sizeof(Secret));
-
 
1601
		if(!ha->initkey || !ea->initkey)
1684
		if(!ha->initkey || !ea->initkey)
1602
			error("misimplemented secret algorithm");
1685
			error("misimplemented secret algorithm");
-
 
1686
 
1603
		(*ha->initkey)(ha, tr->version, tos, &x[0]);
1687
		(*ha->initkey)(ha, tr->version, tos, &x[0]);
1604
		(*ha->initkey)(ha, tr->version, toc, &x[ha->maclen]);
1688
		(*ha->initkey)(ha, tr->version, toc, &x[ha->maclen]);
1605
		(*ea->initkey)(ea, tos, &x[2 * ha->maclen], &x[2 * ha->maclen + 2 * ea->keylen]);
1689
		(*ea->initkey)(ea, tos, &x[2 * ha->maclen], &x[2 * ha->maclen + 2 * ea->keylen]);
1606
		(*ea->initkey)(ea, toc, &x[2 * ha->maclen + ea->keylen], &x[2 * ha->maclen + 2 * ea->keylen + ea->ivlen]);
1690
		(*ea->initkey)(ea, toc, &x[2 * ha->maclen + ea->keylen], &x[2 * ha->maclen + 2 * ea->keylen + ea->ivlen]);
1607
 
1691
 
1608
		if(!tos->mac || !tos->enc || !tos->dec
1692
		if(!tos->aead_enc || !tos->aead_dec || !toc->aead_enc || !toc->aead_dec)
1609
		|| !toc->mac || !toc->enc || !toc->dec)
1693
			if(!tos->mac || !tos->enc || !tos->dec || !toc->mac || !toc->enc || !toc->dec)
1610
			error("missing algorithm implementations");
1694
				error("missing algorithm implementations");
-
 
1695
 
1611
		if(strtol(cb->f[3], nil, 0) == 0){
1696
		if(strtol(cb->f[3], nil, 0) == 0){
1612
			tr->in.new = tos;
1697
			tr->in.new = tos;
1613
			tr->out.new = toc;
1698
			tr->out.new = toc;
1614
		}else{
1699
		}else{
1615
			tr->in.new = toc;
1700
			tr->in.new = toc;
Line 1778... Line 1863...
1778
	if(p == 0)
1863
	if(p == 0)
1779
		error(Ebadarg);
1864
		error(Ebadarg);
1780
	fd = strtoul(p, 0, 0);
1865
	fd = strtoul(p, 0, 0);
1781
	if(fd < 0)
1866
	if(fd < 0)
1782
		error(Ebadarg);
1867
		error(Ebadarg);
1783
	c = fdtochan(fd, -1, 0, 1);	/* error check and inc ref */
1868
	c = fdtochan(fd, ORDWR, 1, 1);	/* error check and inc ref */
1784
	return c;
1869
	return c;
1785
}
1870
}
1786
 
1871
 
1787
static void
1872
static void
1788
sendAlert(TlsRec *tr, int err)
1873
sendAlert(TlsRec *tr, int err)
Line 1822... Line 1907...
1822
static void
1907
static void
1823
tlsError(TlsRec *tr, char *msg)
1908
tlsError(TlsRec *tr, char *msg)
1824
{
1909
{
1825
	int s;
1910
	int s;
1826
 
1911
 
1827
if(tr->debug)pprint("tleError %s\n", msg);
1912
if(tr->debug)pprint("tlsError %s\n", msg);
1828
	lock(&tr->statelk);
1913
	lock(&tr->statelk);
1829
	s = tr->state;
1914
	s = tr->state;
1830
	tr->state = SError;
1915
	tr->state = SError;
1831
	if(s != SError){
1916
	if(s != SError){
1832
		strncpy(tr->err, msg, ERRMAX - 1);
1917
		strncpy(tr->err, msg, ERRMAX - 1);
Line 1894... Line 1979...
1894
		np = smalloc(sizeof(TlsRec*) * newmax);
1979
		np = smalloc(sizeof(TlsRec*) * newmax);
1895
		memmove(np, tlsdevs, sizeof(TlsRec*) * maxtlsdevs);
1980
		memmove(np, tlsdevs, sizeof(TlsRec*) * maxtlsdevs);
1896
		tlsdevs = np;
1981
		tlsdevs = np;
1897
		pp = &tlsdevs[maxtlsdevs];
1982
		pp = &tlsdevs[maxtlsdevs];
1898
		memset(pp, 0, sizeof(TlsRec*)*(newmax - maxtlsdevs));
1983
		memset(pp, 0, sizeof(TlsRec*)*(newmax - maxtlsdevs));
1899
 
1984
 
1900
		nmp = smalloc(sizeof *nmp * newmax);
1985
		nmp = smalloc(sizeof *nmp * newmax);
1901
		memmove(nmp, trnames, sizeof *nmp * maxtlsdevs);
1986
		memmove(nmp, trnames, sizeof *nmp * maxtlsdevs);
1902
		trnames = nmp;
1987
		trnames = nmp;
1903
 
1988
 
1904
		maxtlsdevs = newmax;
1989
		maxtlsdevs = newmax;
Line 1953... Line 2038...
1953
	return "Unknown";
2038
	return "Unknown";
1954
}
2039
}
1955
 
2040
 
1956
static void
2041
static void
1957
freeSec(Secret *s)
2042
freeSec(Secret *s)
1958
{
2043
{
1959
	if(s != nil){
2044
	if(s == nil)
-
 
2045
		return;
1960
		free(s->enckey);
2046
	free(s->enckey);
1961
		free(s);
2047
	free(s);
1962
	}
-
 
1963
}
2048
}
1964
 
2049
 
1965
static int
2050
static int
1966
noenc(Secret *, uchar *, int n)
2051
noenc(Secret *, uchar *, int n)
1967
{
2052
{
1968
	return n;
2053
	return n;
1969
}
2054
}
1970
 
2055
 
1971
static int
2056
static int
1972
rc4enc(Secret *sec, uchar *buf, int n)
2057
rc4enc(Secret *sec, uchar *buf, int n)
1973
{
2058
{
1974
	rc4(sec->enckey, buf, n);
2059
	rc4(sec->enckey, buf, n);
1975
	return n;
2060
	return n;
1976
}
2061
}
1977
 
2062
 
1978
static int
2063
static int
1979
tlsunpad(uchar *buf, int n, int block)
2064
tlsunpad(uchar *buf, int n, int block)
1980
{
2065
{
1981
	int pad, nn;
2066
	int pad, nn;
Line 1990... Line 2075...
1990
	return nn;
2075
	return nn;
1991
}
2076
}
1992
 
2077
 
1993
static int
2078
static int
1994
sslunpad(uchar *buf, int n, int block)
2079
sslunpad(uchar *buf, int n, int block)
1995
{
2080
{
1996
	int pad, nn;
2081
	int pad, nn;
1997
 
2082
 
1998
	pad = buf[n - 1];
2083
	pad = buf[n - 1];
1999
	nn = n - 1 - pad;
2084
	nn = n - 1 - pad;
2000
	if(nn <= 0 || n % block)
2085
	if(nn <= 0 || n % block)
2001
		return -1;
2086
		return -1;
2002
	return nn;
2087
	return nn;
2003
}
2088
}
2004
 
2089
 
2005
static int
2090
static int
2006
blockpad(uchar *buf, int n, int block)
2091
blockpad(uchar *buf, int n, int block)
2007
{
2092
{
2008
	int pad, nn;
2093
	int pad, nn;
2009
 
2094
 
2010
	nn = n + block;
2095
	nn = n + block;
2011
	nn -= nn % block;
2096
	nn -= nn % block;
2012
	pad = nn - (n + 1);
2097
	pad = nn - (n + 1);
2013
	while(n < nn)
2098
	while(n < nn)
2014
		buf[n++] = pad;
2099
		buf[n++] = pad;
2015
	return nn;
2100
	return nn;
2016
}
2101
}
2017
 
2102
		
2018
static int
2103
static int
2019
des3enc(Secret *sec, uchar *buf, int n)
2104
des3enc(Secret *sec, uchar *buf, int n)
2020
{
2105
{
2021
	n = blockpad(buf, n, 8);
2106
	n = blockpad(buf, n, 8);
2022
	des3CBCencrypt(buf, n, sec->enckey);
2107
	des3CBCencrypt(buf, n, sec->enckey);
2023
	return n;
2108
	return n;
2024
}
2109
}
2025
 
2110
 
2026
static int
2111
static int
2027
des3dec(Secret *sec, uchar *buf, int n)
2112
des3dec(Secret *sec, uchar *buf, int n)
2028
{
2113
{
2029
	des3CBCdecrypt(buf, n, sec->enckey);
2114
	des3CBCdecrypt(buf, n, sec->enckey);
2030
	return (*sec->unpad)(buf, n, 8);
2115
	return (*sec->unpad)(buf, n, 8);
2031
}
2116
}
Line 2041... Line 2126...
2041
static int
2126
static int
2042
aesdec(Secret *sec, uchar *buf, int n)
2127
aesdec(Secret *sec, uchar *buf, int n)
2043
{
2128
{
2044
	aesCBCdecrypt(buf, n, sec->enckey);
2129
	aesCBCdecrypt(buf, n, sec->enckey);
2045
	return (*sec->unpad)(buf, n, 16);
2130
	return (*sec->unpad)(buf, n, 16);
-
 
2131
}
-
 
2132
 
-
 
2133
static void
-
 
2134
ccpoly_aead_setiv(Secret *sec, uchar seq[8])
-
 
2135
{
-
 
2136
	uchar iv[ChachaIVlen];
-
 
2137
	Chachastate *cs;
-
 
2138
	int i;
-
 
2139
 
-
 
2140
	cs = (Chachastate*)sec->enckey;
-
 
2141
	if(cs->ivwords == 2){
-
 
2142
		chacha_setiv(cs, seq);
-
 
2143
		return;
-
 
2144
	}
-
 
2145
 
-
 
2146
	memmove(iv, sec->mackey, ChachaIVlen);
-
 
2147
	for(i=0; i<8; i++)
-
 
2148
		iv[i+(ChachaIVlen-8)] ^= seq[i];
-
 
2149
 
-
 
2150
	chacha_setiv(cs, iv);
-
 
2151
 
-
 
2152
	memset(iv, 0, sizeof(iv));
-
 
2153
}
-
 
2154
 
-
 
2155
static int
-
 
2156
ccpoly_aead_enc(Secret *sec, uchar *aad, int aadlen, uchar *reciv, uchar *data, int len)
-
 
2157
{
-
 
2158
	USED(reciv);
-
 
2159
	ccpoly_aead_setiv(sec, aad);
-
 
2160
	ccpoly_encrypt(data, len, aad, aadlen, data+len, sec->enckey);
-
 
2161
	return len + sec->maclen;
-
 
2162
}
-
 
2163
 
-
 
2164
static int
-
 
2165
ccpoly_aead_dec(Secret *sec, uchar *aad, int aadlen, uchar *reciv, uchar *data, int len)
-
 
2166
{
-
 
2167
	USED(reciv);
-
 
2168
	len -= sec->maclen;
-
 
2169
	if(len < 0)
-
 
2170
		return -1;
-
 
2171
	ccpoly_aead_setiv(sec, aad);
-
 
2172
	if(ccpoly_decrypt(data, len, aad, aadlen, data+len, sec->enckey) != 0)
-
 
2173
		return -1;
-
 
2174
	return len;
-
 
2175
}
-
 
2176
 
-
 
2177
static int
-
 
2178
aesgcm_aead_enc(Secret *sec, uchar *aad, int aadlen, uchar *reciv, uchar *data, int len)
-
 
2179
{
-
 
2180
	uchar iv[12];
-
 
2181
	int i;
-
 
2182
 
-
 
2183
	memmove(iv, sec->mackey, 4+8);
-
 
2184
	for(i=0; i<8; i++) iv[4+i] ^= aad[i];
-
 
2185
	memmove(reciv, iv+4, 8);
-
 
2186
	aesgcm_setiv(sec->enckey, iv, 12);
-
 
2187
	memset(iv, 0, sizeof(iv));
-
 
2188
	aesgcm_encrypt(data, len, aad, aadlen, data+len, sec->enckey);
-
 
2189
	return len + sec->maclen;
-
 
2190
}
-
 
2191
 
-
 
2192
static int
-
 
2193
aesgcm_aead_dec(Secret *sec, uchar *aad, int aadlen, uchar *reciv, uchar *data, int len)
-
 
2194
{
-
 
2195
	uchar iv[12];
-
 
2196
 
-
 
2197
	len -= sec->maclen;
-
 
2198
	if(len < 0)
-
 
2199
		return -1;
-
 
2200
	memmove(iv, sec->mackey, 4);
-
 
2201
	memmove(iv+4, reciv, 8);
-
 
2202
	aesgcm_setiv(sec->enckey, iv, 12);
-
 
2203
	memset(iv, 0, sizeof(iv));
-
 
2204
	if(aesgcm_decrypt(data, len, aad, aadlen, data+len, sec->enckey) != 0)
-
 
2205
		return -1;
-
 
2206
	return len;
2046
}
2207
}
-
 
2208
 
2047
 
2209
 
2048
static DigestState*
2210
static DigestState*
2049
nomac(uchar *, ulong, uchar *, ulong, uchar *, DigestState *)
2211
nomac(uchar *, ulong, uchar *, ulong, uchar *, DigestState *)
2050
{
2212
{
2051
	return nil;
2213
	return nil;
2052
}
2214
}
2053
 
2215
 
2054
/*
2216
/*
2055
 * sslmac: mac calculations for ssl 3.0 only; tls 1.0 uses the standard hmac.
2217
 * sslmac: mac calculations for ssl 3.0 only; tls 1.0 uses the standard hmac.
2056
 */
2218
 */
2057
static DigestState*
2219
static DigestState*
2058
sslmac_x(uchar *p, ulong len, uchar *key, ulong klen, uchar *digest, DigestState *s,
2220
sslmac_x(uchar *p, ulong len, uchar *key, ulong klen, uchar *digest, DigestState *s,
2059
	DigestState*(*x)(uchar*, ulong, uchar*, DigestState*), int xlen, int padlen)
2221
	DigestState*(*x)(uchar*, ulong, uchar*, DigestState*), int xlen, int padlen)
2060
{
2222
{
2061
	int i;
2223
	int i;
2062
	uchar pad[48], innerdigest[20];
2224
	uchar pad[48], innerdigest[20];
Line 2100... Line 2262...
2100
 
2262
 
2101
static DigestState*
2263
static DigestState*
2102
sslmac_md5(uchar *p, ulong len, uchar *key, ulong klen, uchar *digest, DigestState *s)
2264
sslmac_md5(uchar *p, ulong len, uchar *key, ulong klen, uchar *digest, DigestState *s)
2103
{
2265
{
2104
	return sslmac_x(p, len, key, klen, digest, s, md5, MD5dlen, 48);
2266
	return sslmac_x(p, len, key, klen, digest, s, md5, MD5dlen, 48);
2105
}
2267
}
2106
 
2268
 
2107
static void
2269
static int
2108
sslPackMac(Secret *sec, uchar *mackey, uchar *seq, uchar *header, uchar *body, int len, uchar *mac)
2270
sslPackAAD(u64int seq, uchar *hdr, uchar *aad)
2109
{
2271
{
2110
	DigestState *s;
2272
	put64(aad, seq);
-
 
2273
	aad[8] = hdr[0];
-
 
2274
	aad[9] = hdr[3];
-
 
2275
	aad[10] = hdr[4];
2111
	uchar buf[11];
2276
	return 11;
-
 
2277
}
2112
 
2278
 
-
 
2279
static int
-
 
2280
tlsPackAAD(u64int seq, uchar *hdr, uchar *aad)
-
 
2281
{
2113
	memmove(buf, seq, 8);
2282
	put64(aad, seq);
2114
	buf[8] = header[0];
2283
	aad[8] = hdr[0];
2115
	buf[9] = header[3];
2284
	aad[9] = hdr[1];
2116
	buf[10] = header[4];
2285
	aad[10] = hdr[2];
2117
 
-
 
2118
	s = (*sec->mac)(buf, 11, mackey, sec->maclen, 0, 0);
2286
	aad[11] = hdr[3];
2119
	(*sec->mac)(body, len, mackey, sec->maclen, mac, s);
2287
	aad[12] = hdr[4];
-
 
2288
	return 13;
2120
}
2289
}
2121
 
2290
 
2122
static void
2291
static void
2123
tlsPackMac(Secret *sec, uchar *mackey, uchar *seq, uchar *header, uchar *body, int len, uchar *mac)
2292
packMac(Secret *sec, uchar *aad, int aadlen, uchar *body, int bodylen, uchar *mac)
2124
{
2293
{
2125
	DigestState *s;
2294
	DigestState *s;
2126
	uchar buf[13];
-
 
2127
 
-
 
2128
	memmove(buf, seq, 8);
-
 
2129
	memmove(&buf[8], header, 5);
-
 
2130
 
2295
 
2131
	s = (*sec->mac)(buf, 13, mackey, sec->maclen, 0, 0);
2296
	s = (*sec->mac)(aad, aadlen, sec->mackey, sec->maclen, nil, nil);
2132
	(*sec->mac)(body, len, mackey, sec->maclen, mac, s);
2297
	(*sec->mac)(body, bodylen, sec->mackey, sec->maclen, mac, s);
2133
}
2298
}
2134
 
2299
 
2135
static void
2300
static void
2136
put32(uchar *p, u32int x)
2301
put32(uchar *p, u32int x)
2137
{
2302
{
2138
	p[0] = x>>24;
2303
	p[0] = x>>24;
2139
	p[1] = x>>16;
2304
	p[1] = x>>16;
2140
	p[2] = x>>8;
2305
	p[2] = x>>8;
2141
	p[3] = x;
2306
	p[3] = x;
2142
}
2307
}
2143
 
2308
 
2144
static void
2309
static void
2145
put64(uchar *p, vlong x)
2310
put64(uchar *p, u64int x)
2146
{
2311
{
2147
	put32(p, (u32int)(x >> 32));
2312
	put32(p, x >> 32);
2148
	put32(p+4, (u32int)x);
2313
	put32(p+4, x);
2149
}
2314
}
2150
 
2315
 
2151
static void
2316
static void
2152
put24(uchar *p, int x)
2317
put24(uchar *p, int x)
2153
{
2318
{
2154
	p[0] = x>>16;
2319
	p[0] = x>>16;
2155
	p[1] = x>>8;
2320
	p[1] = x>>8;
2156
	p[2] = x;
2321
	p[2] = x;
Line 2159... Line 2324...
2159
static void
2324
static void
2160
put16(uchar *p, int x)
2325
put16(uchar *p, int x)
2161
{
2326
{
2162
	p[0] = x>>8;
2327
	p[0] = x>>8;
2163
	p[1] = x;
2328
	p[1] = x;
2164
}
-
 
2165
 
-
 
2166
static u32int
-
 
2167
get32(uchar *p)
-
 
2168
{
-
 
2169
	return (p[0]<<24)|(p[1]<<16)|(p[2]<<8)|p[3];
-
 
2170
}
2329
}
2171
 
2330
 
2172
static int
2331
static int
2173
get16(uchar *p)
2332
get16(uchar *p)
2174
{
2333
{