Subversion Repositories planix.SVN

Rev

Rev 22 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 22 Rev 26
Line 4... Line 4...
4
#include <libsec.h>
4
#include <libsec.h>
5
 
-
 
6
typedef DigestState*(*DigestFun)(uchar*,ulong,uchar*,DigestState*);
-
 
7
 
-
 
8
/* ANSI offsetof, backwards. */
-
 
9
#define	OFFSETOF(a, b)	offsetof(b, a)
-
 
10
 
5
 
11
/*=============================================================*/
6
/*=============================================================*/
12
/*  general ASN1 declarations and parsing
7
/*  general ASN1 declarations and parsing
13
 *
8
 *
14
 *  For now, this is used only for extracting the key from an
9
 *  For now, this is used only for extracting the key from an
Line 56... Line 51...
56
#define UniversalString 28
51
#define UniversalString 28
57
#define BMPString 30
52
#define BMPString 30
58
 
53
 
59
struct Bytes {
54
struct Bytes {
60
	int	len;
55
	int	len;
61
	uchar	data[1];
56
	uchar	data[];
62
};
57
};
63
 
58
 
64
struct Ints {
59
struct Ints {
65
	int	len;
60
	int	len;
66
	int	data[1];
61
	int	data[];
67
};
62
};
68
 
63
 
69
struct Bits {
64
struct Bits {
70
	int	len;		/* number of bytes */
65
	int	len;		/* number of bytes */
71
	int	unusedbits;	/* unused bits in last byte */
66
	int	unusedbits;	/* unused bits in last byte */
72
	uchar	data[1];	/* most-significant bit first */
67
	uchar	data[];		/* most-significant bit first */
73
};
68
};
74
 
69
 
75
struct Tag {
70
struct Tag {
76
	int	class;
71
	int	class;
77
	int	num;
72
	int	num;
Line 133... Line 128...
133
static int	is_octetstring(Elem* pe, Bytes** poctets);
128
static int	is_octetstring(Elem* pe, Bytes** poctets);
134
static int	is_oid(Elem* pe, Ints** poid);
129
static int	is_oid(Elem* pe, Ints** poid);
135
static int	is_string(Elem* pe, char** pstring);
130
static int	is_string(Elem* pe, char** pstring);
136
static int	is_time(Elem* pe, char** ptime);
131
static int	is_time(Elem* pe, char** ptime);
137
static int	decode(uchar* a, int alen, Elem* pelem);
132
static int	decode(uchar* a, int alen, Elem* pelem);
138
static int	decode_seq(uchar* a, int alen, Elist** pelist);
-
 
139
static int	decode_value(uchar* a, int alen, int kind, int isconstr, Value* pval);
-
 
140
static int	encode(Elem e, Bytes** pbytes);
133
static int	encode(Elem e, Bytes** pbytes);
141
static int	oid_lookup(Ints* o, Ints** tab);
134
static int	oid_lookup(Ints* o, Ints** tab);
142
static void	freevalfields(Value* v);
135
static void	freevalfields(Value* v);
143
static mpint	*asn1mpint(Elem *e);
136
static mpint	*asn1mpint(Elem *e);
144
 
-
 
145
 
-
 
-
 
137
static void	edump(Elem);
146
 
138
 
147
#define TAG_MASK 0x1F
139
#define TAG_MASK 0x1F
148
#define CONSTR_MASK 0x20
140
#define CONSTR_MASK 0x20
149
#define CLASS_MASK 0xC0
141
#define CLASS_MASK 0xC0
150
#define MAXOBJIDLEN 20
142
#define MAXOBJIDLEN 20
Line 167... Line 159...
167
{
159
{
168
	void *p;
160
	void *p;
169
	if(n==0)
161
	if(n==0)
170
		n=1;
162
		n=1;
171
	p = malloc(n);
163
	p = malloc(n);
172
	if(p == nil){
164
	if(p == nil)
173
		exits("out of memory");
165
		sysfatal("out of memory");
174
	}
-
 
175
	memset(p, 0, n);
166
	memset(p, 0, n);
176
	setmalloctag(p, getcallerpc(&n));
167
	setmalloctag(p, getcallerpc(&n));
177
	return p;
168
	return p;
178
}
169
}
179
 
170
 
180
static char*
171
static char*
181
estrdup(char *s)
172
estrdup(char *s)
182
{
173
{
183
	char *d, *d0;
174
	char *d;
-
 
175
	int n;
184
 
176
 
185
	if(!s)
-
 
186
		return 0;
177
	n = strlen(s)+1;
187
	d = d0 = emalloc(strlen(s)+1);
178
	d = emalloc(n);
188
	while(*d++ = *s++)
179
	memmove(d, s, n);
189
		;
-
 
190
	return d0;
180
	return d;
191
}
181
}
192
 
182
 
193
 
183
 
194
/*
184
/*
195
 * Decode a[0..len] as a BER encoding of an ASN1 type.
185
 * Decode a[0..len] as a BER encoding of an ASN1 type.
Line 199... Line 189...
199
 */
189
 */
200
static int
190
static int
201
decode(uchar* a, int alen, Elem* pelem)
191
decode(uchar* a, int alen, Elem* pelem)
202
{
192
{
203
	uchar* p = a;
193
	uchar* p = a;
-
 
194
	int err;
204
 
195
 
205
	return  ber_decode(&p, &a[alen], pelem);
196
	err = ber_decode(&p, &a[alen], pelem);
206
}
-
 
207
 
-
 
208
/*
-
 
209
 * Like decode, but continue decoding after first element
-
 
210
 * of array ends.
-
 
211
 */
-
 
212
static int
-
 
213
decode_seq(uchar* a, int alen, Elist** pelist)
197
	if(err == ASN_OK && p != &a[alen])
214
{
-
 
215
	uchar* p = a;
198
		err = ASN_EVALLEN;
216
 
-
 
217
	return seq_decode(&p, &a[alen], -1, 1, pelist);
-
 
218
}
-
 
219
 
-
 
220
/*
-
 
221
 * Decode the whole array as a BER encoding of an ASN1 value,
-
 
222
 * (i.e., the part after the tag and length).
-
 
223
 * Assume the value is encoded as universal tag "kind".
-
 
224
 * The constr arg is 1 if the value is constructed, 0 if primitive.
-
 
225
 * If there's an error, the return string will contain the error.
-
 
226
 * Depending on the error, the returned value may or may not
-
 
227
 * be nil.
-
 
228
 */
-
 
229
static int
-
 
230
decode_value(uchar* a, int alen, int kind, int isconstr, Value* pval)
-
 
231
{
-
 
232
	uchar* p = a;
199
	return err;
233
 
-
 
234
	return value_decode(&p, &a[alen], alen, kind, isconstr, pval);
-
 
235
}
200
}
236
 
201
 
237
/*
202
/*
238
 * All of the following decoding routines take arguments:
203
 * All of the following decoding routines take arguments:
239
 *	uchar **pp;
204
 *	uchar **pp;
Line 255... Line 220...
255
	int isconstr;
220
	int isconstr;
256
	int length;
221
	int length;
257
	Tag tag;
222
	Tag tag;
258
	Value val;
223
	Value val;
259
 
224
 
-
 
225
	memset(pelem, 0, sizeof(*pelem));
260
	err = tag_decode(pp, pend, &tag, &isconstr);
226
	err = tag_decode(pp, pend, &tag, &isconstr);
261
	if(err == ASN_OK) {
227
	if(err == ASN_OK) {
262
		err = length_decode(pp, pend, &length);
228
		err = length_decode(pp, pend, &length);
263
		if(err == ASN_OK) {
229
		if(err == ASN_OK) {
264
			if(tag.class == Universal) {
230
			if(tag.class == Universal)
265
				err = value_decode(pp, pend, length, tag.num, isconstr, &val);
231
				err = value_decode(pp, pend, length, tag.num, isconstr, &val);
266
				if(val.tag == VSeq || val.tag == VSet)
-
 
267
					setmalloctag(val.u.seqval, getcallerpc(&pp));
-
 
268
			}else
232
			else
269
				err = value_decode(pp, pend, length, OCTET_STRING, 0, &val);
233
				err = value_decode(pp, pend, length, OCTET_STRING, 0, &val);
270
			if(err == ASN_OK) {
234
			if(err == ASN_OK) {
271
				pelem->tag = tag;
235
				pelem->tag = tag;
272
				pelem->val = val;
236
				pelem->val = val;
273
			}
237
			}
Line 325... Line 289...
325
	}
289
	}
326
	else
290
	else
327
		err = ASN_ESHORT;
291
		err = ASN_ESHORT;
328
	*pp = p;
292
	*pp = p;
329
	*plength = num;
293
	*plength = num;
330
	return err;
294
	return err;
331
}
295
}
332
 
296
 
333
/* Decode a value field  */
297
/* Decode a value field  */
334
static int
298
static int
335
value_decode(uchar** pp, uchar* pend, int length, int kind, int isconstr, Value* pval)
299
value_decode(uchar** pp, uchar* pend, int length, int kind, int isconstr, Value* pval)
336
{
300
{
337
	int err;
301
	int err;
Line 341... Line 305...
341
	int subids[MAXOBJIDLEN];
305
	int subids[MAXOBJIDLEN];
342
	int isubid;
306
	int isubid;
343
	Elist*	vl;
307
	Elist*	vl;
344
	uchar* p;
308
	uchar* p;
345
	uchar* pe;
309
	uchar* pe;
346
 
310
 
347
	err = ASN_OK;
311
	err = ASN_OK;
348
	p = *pp;
312
	p = *pp;
349
	if(length == -1) {	/* "indefinite" length spec */
313
	if(length == -1) {	/* "indefinite" length spec */
350
		if(!isconstr)
314
		if(!isconstr)
351
			err = ASN_EINVAL;
315
			err = ASN_EINVAL;
352
	}
316
	}
353
	else if(p + length > pend)
317
	else if(p + length > pend)
354
		err = ASN_EVALLEN;
318
		err = ASN_EVALLEN;
355
	if(err != ASN_OK)
319
	if(err != ASN_OK)
356
		return err;
320
		return err;
Line 363... Line 327...
363
		else
327
		else
364
			err = ASN_EINVAL;
328
			err = ASN_EINVAL;
365
		break;
329
		break;
366
 
330
 
367
	case BOOLEAN:
331
	case BOOLEAN:
368
		if(isconstr)
332
		if(isconstr)
369
			err = ASN_ECONSTR;
333
			err = ASN_ECONSTR;
370
		else if(length != 1)
334
		else if(length != 1)
371
			err = ASN_EVALLEN;
335
			err = ASN_EVALLEN;
372
		else {
336
		else {
373
			pval->tag = VBool;
337
			pval->tag = VBool;
374
			pval->u.boolval = (*p++ != 0);
338
			pval->u.boolval = (*p++ != 0);
375
		}
339
		}
376
		break;
340
		break;
377
 
341
 
378
	case INTEGER:
342
	case INTEGER:
379
	case ENUMERATED:
343
	case ENUMERATED:
380
		if(isconstr)
344
		if(isconstr)
381
			err = ASN_ECONSTR;
345
			err = ASN_ECONSTR;
Line 398... Line 362...
398
		if(isconstr) {
362
		if(isconstr) {
399
			if(length == -1 && p + 2 <= pend && *p == 0 && *(p+1) ==0) {
363
			if(length == -1 && p + 2 <= pend && *p == 0 && *(p+1) ==0) {
400
				pval->u.bitstringval = makebits(0, 0, 0);
364
				pval->u.bitstringval = makebits(0, 0, 0);
401
				p += 2;
365
				p += 2;
402
			}
366
			}
403
			else
-
 
404
				/* TODO: recurse and concat results */
367
			else	/* TODO: recurse and concat results */
405
				err = ASN_EUNIMPL;
368
				err = ASN_EUNIMPL;
406
		}
369
		}
407
		else {
370
		else {
408
			if(length < 2) {
371
			if(length < 2) {
409
				if(length == 1 && *p == 0) {
372
				if(length == 1 && *p == 0) {
410
					pval->u.bitstringval = makebits(0, 0, 0);
373
					pval->u.bitstringval = makebits(0, 0, 0);
411
					p++;
374
					p++;
412
				}
375
				}
Line 500... Line 463...
500
		}
463
		}
501
		break;
464
		break;
502
 
465
 
503
	case SEQUENCE:
466
	case SEQUENCE:
504
		err = seq_decode(&p, pend, length, isconstr, &vl);
467
		err = seq_decode(&p, pend, length, isconstr, &vl);
505
		setmalloctag(vl, getcallerpc(&pp));
-
 
506
		if(err == ASN_OK) {
468
		if(err == ASN_OK) {
507
			pval->tag = VSeq ;
469
			pval->tag = VSeq ;
508
			pval->u.seqval = vl;
470
			pval->u.seqval = vl;
509
		}
471
		}
510
		break;
472
		break;
511
 
473
 
512
	case SETOF:
474
	case SETOF:
513
		err = seq_decode(&p, pend, length, isconstr, &vl);
475
		err = seq_decode(&p, pend, length, isconstr, &vl);
514
		setmalloctag(vl, getcallerpc(&pp));
-
 
515
		if(err == ASN_OK) {
476
		if(err == ASN_OK) {
516
			pval->tag = VSet;
477
			pval->tag = VSet;
517
			pval->u.setval = vl;
478
			pval->u.setval = vl;
518
		}
479
		}
519
		break;
480
		break;
-
 
481
 
520
	case UTF8String:
482
	case UTF8String:
521
	case NumericString:
483
	case NumericString:
522
	case PrintableString:
484
	case PrintableString:
523
	case TeletexString:
485
	case TeletexString:
524
	case VideotexString:
486
	case VideotexString:
Line 528... Line 490...
528
	case GraphicString:
490
	case GraphicString:
529
	case VisibleString:
491
	case VisibleString:
530
	case GeneralString:
492
	case GeneralString:
531
	case UniversalString:
493
	case UniversalString:
532
	case BMPString:
494
	case BMPString:
533
		/* TODO: figure out when character set conversion is necessary */
-
 
534
		err = octet_decode(&p, pend, length, isconstr, &va);
495
		err = octet_decode(&p, pend, length, isconstr, &va);
535
		if(err == ASN_OK) {
496
		if(err == ASN_OK) {
-
 
497
			uchar *s;
-
 
498
			char *d;
-
 
499
			Rune r;
-
 
500
			int n;
-
 
501
 
-
 
502
			switch(kind){
536
			pval->tag = VString;
503
			case UniversalString:
-
 
504
				n = va->len / 4;
-
 
505
				d = emalloc(n*UTFmax+1);
537
			pval->u.stringval = (char*)emalloc(va->len+1);
506
				pval->u.stringval = d;
-
 
507
				s = va->data;
-
 
508
				while(n > 0){
-
 
509
					r = s[0]<<24 | s[1]<<16 | s[2]<<8 | s[3];
-
 
510
					if(r == 0)
-
 
511
						break;
-
 
512
					n--;
-
 
513
					s += 4;
-
 
514
					d += runetochar(d, &r);
-
 
515
				}
-
 
516
				*d = 0;
-
 
517
				break;
-
 
518
			case BMPString:
-
 
519
				n = va->len / 2;
-
 
520
				d = emalloc(n*UTFmax+1);
538
			memmove(pval->u.stringval, va->data, va->len);
521
				pval->u.stringval = d;
-
 
522
				s = va->data;
-
 
523
				while(n > 0){
-
 
524
					r = s[0]<<8 | s[1];
-
 
525
					if(r == 0)
-
 
526
						break;
-
 
527
					n--;
-
 
528
					s += 2;
-
 
529
					d += runetochar(d, &r);
-
 
530
				}
-
 
531
				*d = 0;
-
 
532
				break;
-
 
533
			default:
-
 
534
				n = va->len;
-
 
535
				d = emalloc(n+1);
539
			pval->u.stringval[va->len] = 0;
536
				pval->u.stringval = d;
-
 
537
				s = va->data;
-
 
538
				while(n > 0){
-
 
539
					if((*d = *s) == 0)
-
 
540
						break;
-
 
541
					n--;
-
 
542
					s++;
-
 
543
					d++;
-
 
544
				}
-
 
545
				*d = 0;
-
 
546
				break;
-
 
547
			}
-
 
548
			if(n != 0){
-
 
549
				err = ASN_EINVAL;
-
 
550
				free(pval->u.stringval);
-
 
551
			} else 
-
 
552
				pval->tag = VString;
540
			free(va);
553
			free(va);
541
		}
554
		}
542
		break;
555
		break;
543
 
556
 
544
	default:
557
	default:
545
		if(p+length > pend)
558
		if(p+length > pend)
546
			err = ASN_EVALLEN;
559
			err = ASN_EVALLEN;
547
		else {
560
		else {
Line 556... Line 569...
556
}
569
}
557
 
570
 
558
/*
571
/*
559
 * Decode an int in format where count bytes are
572
 * Decode an int in format where count bytes are
560
 * concatenated to form value.
573
 * concatenated to form value.
561
 * Although ASN1 allows any size integer, we return
574
 * Although ASN1 allows any size integer, we return
562
 * an error if the result doesn't fit in a 32-bit int.
575
 * an error if the result doesn't fit in a 32-bit int.
563
 * If unsgned is not set, make sure to propagate sign bit.
576
 * If unsgned is not set, make sure to propagate sign bit.
564
 */
577
 */
565
static int
578
static int
566
int_decode(uchar** pp, uchar* pend, int count, int unsgned, int* pint)
579
int_decode(uchar** pp, uchar* pend, int count, int unsgned, int* pint)
Line 628... Line 641...
628
 
641
 
629
/*
642
/*
630
 * Decode an octet string, recursively if isconstr.
643
 * Decode an octet string, recursively if isconstr.
631
 * We've already checked that length==-1 implies isconstr==1,
644
 * We've already checked that length==-1 implies isconstr==1,
632
 * and otherwise that specified length fits within (*pp..pend)
645
 * and otherwise that specified length fits within (*pp..pend)
633
 */
646
 */
634
static int
647
static int
635
octet_decode(uchar** pp, uchar* pend, int length, int isconstr, Bytes** pbytes)
648
octet_decode(uchar** pp, uchar* pend, int length, int isconstr, Bytes** pbytes)
636
{
649
{
637
	int err;
650
	int err;
638
	uchar* p;
651
	uchar* p;
639
	Bytes* ans;
652
	Bytes* ans;
640
	Bytes* newans;
653
	Bytes* newans;
641
	uchar* pstart;
654
	uchar* pstart;
642
	uchar* pold;
655
	uchar* pold;
643
	Elem	elem;
656
	Elem	elem;
644
 
657
 
645
	err = ASN_OK;
658
	err = ASN_OK;
646
	p = *pp;
659
	p = *pp;
647
	ans = nil;
660
	ans = nil;
648
	if(length >= 0 && !isconstr) {
661
	if(length >= 0 && !isconstr) {
649
		ans = makebytes(p, length);
662
		ans = makebytes(p, length);
650
		p += length;
663
		p += length;
651
	}
664
	}
652
	else {
665
	else {
653
		/* constructed, either definite or indefinite length */
666
		/* constructed, either definite or indefinite length */
654
		pstart = p;
667
		pstart = p;
655
		for(;;) {
668
		for(;;) {
656
			if(length >= 0 && p >= pstart + length) {
669
			if(length >= 0 && p >= pstart + length) {
657
				if(p != pstart + length)
670
				if(p != pstart + length)
658
					err = ASN_EVALLEN;
671
					err = ASN_EVALLEN;
659
				break;
672
				break;
660
			}
673
			}
661
			pold = p;
674
			pold = p;
662
			err = ber_decode(&p, pend, &elem);
675
			err = ber_decode(&p, pend, &elem);
663
			if(err != ASN_OK)
676
			if(err != ASN_OK)
664
				break;
677
				break;
665
			switch(elem.val.tag) {
678
			switch(elem.val.tag) {
666
			case VOctets:
679
			case VOctets:
667
				newans = catbytes(ans, elem.val.u.octetsval);
680
				newans = catbytes(ans, elem.val.u.octetsval);
-
 
681
				freevalfields(&elem.val);
668
				freebytes(ans);
682
				freebytes(ans);
669
				ans = newans;
683
				ans = newans;
670
				break;
684
				break;
671
 
685
 
672
			case VEOC:
686
			case VEOC:
673
				if(length != -1) {
687
				if(length == -1)
674
					p = pold;
688
					goto cloop_done;
675
					err = ASN_EINVAL;
-
 
676
				}
-
 
677
				goto cloop_done;
689
				/* no break */
678
 
-
 
679
			default:
690
			default:
-
 
691
				freevalfields(&elem.val);
680
				p = pold;
692
				p = pold;
681
				err = ASN_EINVAL;
693
				err = ASN_EINVAL;
682
				goto cloop_done;
694
				goto cloop_done;
683
			}
695
			}
684
		}
696
		}
685
cloop_done:
697
cloop_done:
-
 
698
		if(err != ASN_OK){
-
 
699
			freebytes(ans);
-
 
700
			ans = nil;
686
		;
701
		}
687
	}
702
	}
688
	*pp = p;
703
	*pp = p;
689
	*pbytes = ans;
704
	*pbytes = ans;
690
	return err;
705
	return err;
691
}
706
}
Line 734... Line 749...
734
				break;
749
				break;
735
			}
750
			}
736
			else
751
			else
737
				lve = mkel(elem, lve);
752
				lve = mkel(elem, lve);
738
		}
753
		}
739
		if(err == ASN_OK) {
754
		if(err != ASN_OK)
-
 
755
			freeelist(lve);
-
 
756
		else {
740
			/* reverse back to original order */
757
			/* reverse back to original order */
741
			while(lve != nil) {
758
			while(lve != nil) {
742
				lveold = lve;
759
				lveold = lve;
743
				lve = lve->tl;
760
				lve = lve->tl;
744
				lveold->tl = ans;
761
				lveold->tl = ans;
Line 746... Line 763...
746
			}
763
			}
747
		}
764
		}
748
	}
765
	}
749
	*pp = p;
766
	*pp = p;
750
	*pelist = ans;
767
	*pelist = ans;
751
	setmalloctag(ans, getcallerpc(&pp));
-
 
752
	return err;
768
	return err;
753
}
769
}
754
 
770
 
755
/*
771
/*
756
 * Encode e by BER rules, putting answer in *pbytes.
772
 * Encode e by BER rules, putting answer in *pbytes.
Line 975... Line 991...
975
		if(bb != nil) {
991
		if(bb != nil) {
976
			if(!lenonly)
992
			if(!lenonly)
977
				memmove(p, bb->data, bb->len);
993
				memmove(p, bb->data, bb->len);
978
			p += bb->len;
994
			p += bb->len;
979
		}
995
		}
980
			else
996
		else
981
				err = ASN_EINVAL;
997
			err = ASN_EINVAL;
982
		break;
998
		break;
983
 
999
 
984
	case NULLTAG:
1000
	case NULLTAG:
985
		break;
1001
		break;
986
 
1002
 
Line 1182... Line 1198...
1182
		}
1198
		}
1183
		else if(pe->tag.num == BOOLEAN && pe->val.tag == VBool) {
1199
		else if(pe->tag.num == BOOLEAN && pe->val.tag == VBool) {
1184
			*pint = pe->val.u.boolval;
1200
			*pint = pe->val.u.boolval;
1185
			return 1;
1201
			return 1;
1186
		}
1202
		}
1187
	}
1203
	}
1188
	return 0;
1204
	return 0;
1189
}
1205
}
1190
 
1206
 
1191
/*
1207
/*
1192
 * for convience, all VInt's are readable via this routine,
1208
 * for convience, all VInt's are readable via this routine,
1193
 * as well as all VBigInt's
1209
 * as well as all VBigInt's
1194
 */
1210
 */
1195
static int
1211
static int
1196
is_bigint(Elem* pe, Bytes** pbigint)
1212
is_bigint(Elem* pe, Bytes** pbigint)
1197
{
1213
{
1198
	int v, n, i;
-
 
1199
 
-
 
1200
	if(pe->tag.class == Universal && pe->tag.num == INTEGER) {
1214
	if(pe->tag.class == Universal && pe->tag.num == INTEGER && pe->val.tag == VBigInt) {
1201
		if(pe->val.tag == VBigInt)
-
 
1202
			*pbigint = pe->val.u.bigintval;
1215
		*pbigint = pe->val.u.bigintval;
1203
		else if(pe->val.tag == VInt){
-
 
1204
			v = pe->val.u.intval;
-
 
1205
			for(n = 1; n < 4; n++)
-
 
1206
				if((1 << (8 * n)) > v)
-
 
1207
					break;
-
 
1208
			*pbigint = newbytes(n);
-
 
1209
			for(i = 0; i < n; i++)
-
 
1210
				(*pbigint)->data[i] = (v >> ((n - 1 - i) * 8));
-
 
1211
		}else
-
 
1212
			return 0;
-
 
1213
		return 1;
1216
		return 1;
1214
	}
1217
	}
1215
	return 0;
1218
	return 0;
1216
}
1219
}
1217
 
1220
 
1218
static int
1221
static int
1219
is_bitstring(Elem* pe, Bits** pbits)
1222
is_bitstring(Elem* pe, Bits** pbits)
1220
{
1223
{
1221
	if(pe->tag.class == Universal && pe->tag.num == BIT_STRING && pe->val.tag == VBitString) {
1224
	if(pe->tag.class == Universal && pe->tag.num == BIT_STRING && pe->val.tag == VBitString) {
1222
		*pbits = pe->val.u.bitstringval;
1225
		*pbits = pe->val.u.bitstringval;
1223
		return 1;
1226
		return 1;
1224
	}
1227
	}
1225
	return 0;
1228
	return 0;
1226
}
1229
}
1227
 
1230
 
1228
static int
1231
static int
1229
is_octetstring(Elem* pe, Bytes** poctets)
1232
is_octetstring(Elem* pe, Bytes** poctets)
1230
{
1233
{
1231
	if(pe->tag.class == Universal && pe->tag.num == OCTET_STRING && pe->val.tag == VOctets) {
1234
	if(pe->tag.class == Universal && pe->tag.num == OCTET_STRING && pe->val.tag == VOctets) {
1232
		*poctets = pe->val.u.octetsval;
1235
		*poctets = pe->val.u.octetsval;
1233
		return 1;
1236
		return 1;
1234
	}
1237
	}
1235
	return 0;
1238
	return 0;
1236
}
1239
}
1237
 
1240
 
1238
static int
1241
static int
1239
is_oid(Elem* pe, Ints** poid)
1242
is_oid(Elem* pe, Ints** poid)
1240
{
1243
{
1241
	if(pe->tag.class == Universal && pe->tag.num == OBJECT_ID && pe->val.tag == VObjId) {
1244
	if(pe->tag.class == Universal && pe->tag.num == OBJECT_ID && pe->val.tag == VObjId) {
Line 1287... Line 1290...
1287
 * malloc and return a new Bytes structure capable of
1290
 * malloc and return a new Bytes structure capable of
1288
 * holding len bytes. (len >= 0)
1291
 * holding len bytes. (len >= 0)
1289
 */
1292
 */
1290
static Bytes*
1293
static Bytes*
1291
newbytes(int len)
1294
newbytes(int len)
1292
{
1295
{
1293
	Bytes* ans;
1296
	Bytes* ans;
1294
 
1297
 
-
 
1298
	if(len < 0)
-
 
1299
		abort();
1295
	ans = (Bytes*)emalloc(OFFSETOF(data[0], Bytes) + len);
1300
	ans = emalloc(sizeof(Bytes) + len);
1296
	ans->len = len;
1301
	ans->len = len;
1297
	return ans;
1302
	return ans;
1298
}
1303
}
1299
 
1304
 
1300
/*
1305
/*
1301
 * newbytes(len), with data initialized from buf
1306
 * newbytes(len), with data initialized from buf
Line 1311... Line 1316...
1311
}
1316
}
1312
 
1317
 
1313
static void
1318
static void
1314
freebytes(Bytes* b)
1319
freebytes(Bytes* b)
1315
{
1320
{
1316
	if(b != nil)
-
 
1317
		free(b);
1321
	free(b);
1318
}
1322
}
1319
 
1323
 
1320
/*
1324
/*
1321
 * Make a new Bytes, containing bytes of b1 followed by those of b2.
1325
 * Make a new Bytes, containing bytes of b1 followed by those of b2.
1322
 * Either b1 or b2 or both can be nil.
1326
 * Either b1 or b2 or both can be nil.
Line 1347... Line 1351...
1347
}
1351
}
1348
 
1352
 
1349
/* len is number of ints */
1353
/* len is number of ints */
1350
static Ints*
1354
static Ints*
1351
newints(int len)
1355
newints(int len)
1352
{
1356
{
1353
	Ints* ans;
1357
	Ints* ans;
1354
 
1358
 
-
 
1359
	if(len < 0 || len > ((uint)-1>>1)/sizeof(int))
-
 
1360
		abort();
1355
	ans = (Ints*)emalloc(OFFSETOF(data[0], Ints) + len*sizeof(int));
1361
	ans = emalloc(sizeof(Ints) + len*sizeof(int));
1356
	ans->len = len;
1362
	ans->len = len;
1357
	return ans;
1363
	return ans;
1358
}
1364
}
1359
 
1365
 
1360
static Ints*
1366
static Ints*
1361
makeints(int* buf, int len)
1367
makeints(int* buf, int len)
1362
{
1368
{
1363
	Ints* ans;
1369
	Ints* ans;
1364
 
1370
 
1365
	ans = newints(len);
1371
	ans = newints(len);
1366
	if(len > 0)
-
 
1367
		memmove(ans->data, buf, len*sizeof(int));
1372
	memmove(ans->data, buf, len*sizeof(int));
1368
	return ans;
1373
	return ans;
1369
}
1374
}
1370
 
1375
 
1371
static void
1376
static void
1372
freeints(Ints* b)
1377
freeints(Ints* b)
1373
{
1378
{
1374
	if(b != nil)
-
 
1375
		free(b);
1379
	free(b);
1376
}
1380
}
1377
 
1381
 
1378
/* len is number of bytes */
1382
/* len is number of bytes */
1379
static Bits*
1383
static Bits*
1380
newbits(int len)
1384
newbits(int len)
1381
{
1385
{
1382
	Bits* ans;
1386
	Bits* ans;
1383
 
1387
 
-
 
1388
	if(len < 0)
-
 
1389
		abort();
1384
	ans = (Bits*)emalloc(OFFSETOF(data[0], Bits) + len);
1390
	ans = emalloc(sizeof(Bits) + len);
1385
	ans->len = len;
1391
	ans->len = len;
1386
	ans->unusedbits = 0;
1392
	ans->unusedbits = 0;
1387
	return ans;
1393
	return ans;
1388
}
1394
}
1389
 
1395
 
Line 1399... Line 1405...
1399
}
1405
}
1400
 
1406
 
1401
static void
1407
static void
1402
freebits(Bits* b)
1408
freebits(Bits* b)
1403
{
1409
{
1404
	if(b != nil)
-
 
1405
		free(b);
1410
	free(b);
1406
}
1411
}
1407
 
1412
 
1408
static Elist*
1413
static Elist*
1409
mkel(Elem e, Elist* tail)
1414
mkel(Elem e, Elist* tail)
1410
{
1415
{
Line 1419... Line 1424...
1419
 
1424
 
1420
static int
1425
static int
1421
elistlen(Elist* el)
1426
elistlen(Elist* el)
1422
{
1427
{
1423
	int ans = 0;
1428
	int ans = 0;
1424
	while(el != nil) {
1429
	while(el != nil) {
1425
		ans++;
1430
		ans++;
1426
		el = el->tl;
1431
		el = el->tl;
1427
	}
1432
	}
1428
	return ans;
1433
	return ans;
1429
}
1434
}
Line 1467... Line 1472...
1467
		break;
1472
		break;
1468
	case VObjId:
1473
	case VObjId:
1469
		freeints(v->u.objidval);
1474
		freeints(v->u.objidval);
1470
		break;
1475
		break;
1471
	case VString:
1476
	case VString:
1472
		if(v->u.stringval)
-
 
1473
			free(v->u.stringval);
1477
		free(v->u.stringval);
1474
		break;
1478
		break;
1475
	case VSeq:
1479
	case VSeq:
1476
		el = v->u.seqval;
1480
		el = v->u.seqval;
1477
		for(l = el; l != nil; l = l->tl)
1481
		for(l = el; l != nil; l = l->tl)
1478
			freevalfields(&l->hd.val);
1482
			freevalfields(&l->hd.val);
1479
		if(el)
-
 
1480
			freeelist(el);
1483
		freeelist(el);
1481
		break;
1484
		break;
1482
	case VSet:
1485
	case VSet:
1483
		el = v->u.setval;
1486
		el = v->u.setval;
1484
		for(l = el; l != nil; l = l->tl)
1487
		for(l = el; l != nil; l = l->tl)
1485
			freevalfields(&l->hd.val);
1488
			freevalfields(&l->hd.val);
1486
		if(el)
-
 
1487
			freeelist(el);
1489
		freeelist(el);
1488
		break;
1490
		break;
1489
	}
1491
	}
-
 
1492
	memset(v, 0, sizeof(*v));
-
 
1493
}
-
 
1494
 
-
 
1495
static mpint*
-
 
1496
asn1mpint(Elem *e)
-
 
1497
{
-
 
1498
	Bytes *b;
-
 
1499
	int v;
-
 
1500
 
-
 
1501
	if(is_int(e, &v))
-
 
1502
		return itomp(v, nil);
-
 
1503
	if(is_bigint(e, &b))
-
 
1504
		return betomp(b->data, b->len, nil);
-
 
1505
	return nil;
1490
}
1506
}
1491
 
1507
 
1492
/* end of general ASN1 functions */
1508
/* end of general ASN1 functions */
1493
 
1509
 
1494
 
1510
 
Line 1567... Line 1583...
1567
	char*	issuer;
1583
	char*	issuer;
1568
	char*	validity_start;
1584
	char*	validity_start;
1569
	char*	validity_end;
1585
	char*	validity_end;
1570
	char*	subject;
1586
	char*	subject;
1571
	int	publickey_alg;
1587
	int	publickey_alg;
1572
	Bytes*	publickey;
1588
	Bits*	publickey;
1573
	int	signature_alg;
1589
	int	signature_alg;
1574
	Bytes*	signature;
1590
	Bits*	signature;
-
 
1591
	int	curve;
1575
} CertX509;
1592
} CertX509;
1576
 
1593
 
1577
/* Algorithm object-ids */
1594
/* Algorithm object-ids */
1578
enum {
1595
enum {
1579
	ALG_rsaEncryption,
1596
	ALG_rsaEncryption,
1580
	ALG_md2WithRSAEncryption,
1597
	ALG_md2WithRSAEncryption,
1581
	ALG_md4WithRSAEncryption,
1598
	ALG_md4WithRSAEncryption,
1582
	ALG_md5WithRSAEncryption,
1599
	ALG_md5WithRSAEncryption,
-
 
1600
 
1583
	ALG_sha1WithRSAEncryption,
1601
	ALG_sha1WithRSAEncryption,
1584
	ALG_sha1WithRSAEncryptionOiw,
1602
	ALG_sha1WithRSAEncryptionOiw,
-
 
1603
 
-
 
1604
	ALG_sha256WithRSAEncryption,
-
 
1605
	ALG_sha384WithRSAEncryption,
-
 
1606
	ALG_sha512WithRSAEncryption,
-
 
1607
	ALG_sha224WithRSAEncryption,
-
 
1608
 
-
 
1609
	ALG_ecPublicKey,
-
 
1610
	ALG_sha1WithECDSA,
-
 
1611
	ALG_sha256WithECDSA,
-
 
1612
	ALG_sha384WithECDSA,
-
 
1613
	ALG_sha512WithECDSA,
-
 
1614
 
1585
	ALG_md5,
1615
	ALG_md5,
-
 
1616
	ALG_sha1,
-
 
1617
	ALG_sha256,
-
 
1618
	ALG_sha384,
-
 
1619
	ALG_sha512,
-
 
1620
	ALG_sha224,
-
 
1621
 
1586
	NUMALGS
1622
	NUMALGS
1587
};
1623
};
-
 
1624
 
1588
typedef struct Ints7 {
1625
typedef struct Ints15 {
-
 
1626
	int		len;
-
 
1627
	int		data[15];
-
 
1628
} Ints15;
-
 
1629
 
-
 
1630
typedef struct DigestAlg {
-
 
1631
	int		alg;
-
 
1632
	DigestState*	(*fun)(uchar*,ulong,uchar*,DigestState*);
1589
	int		len;
1633
	int		len;
1590
	int		data[7];
1634
} DigestAlg;
-
 
1635
 
-
 
1636
static DigestAlg alg_md5 = { ALG_md5, md5, MD5dlen};
-
 
1637
static DigestAlg alg_sha1 = { ALG_sha1, sha1, SHA1dlen };
-
 
1638
static DigestAlg alg_sha256 = { ALG_sha256, sha2_256, SHA2_256dlen };
-
 
1639
static DigestAlg alg_sha384 = { ALG_sha384, sha2_384, SHA2_384dlen };
-
 
1640
static DigestAlg alg_sha512 = { ALG_sha512, sha2_512, SHA2_512dlen };
-
 
1641
static DigestAlg alg_sha224 = { ALG_sha224, sha2_224, SHA2_224dlen };
-
 
1642
 
-
 
1643
/* maximum length of digest output of the digest algs above */
1591
} Ints7;
1644
enum {
-
 
1645
	MAXdlen = SHA2_512dlen,
-
 
1646
};
-
 
1647
 
1592
static Ints7 oid_rsaEncryption = {7, 1, 2, 840, 113549, 1, 1, 1 };
1648
static Ints15 oid_rsaEncryption = {7, 1, 2, 840, 113549, 1, 1, 1 };
-
 
1649
 
1593
static Ints7 oid_md2WithRSAEncryption = {7, 1, 2, 840, 113549, 1, 1, 2 };
1650
static Ints15 oid_md2WithRSAEncryption = {7, 1, 2, 840, 113549, 1, 1, 2 };
1594
static Ints7 oid_md4WithRSAEncryption = {7, 1, 2, 840, 113549, 1, 1, 3 };
1651
static Ints15 oid_md4WithRSAEncryption = {7, 1, 2, 840, 113549, 1, 1, 3 };
1595
static Ints7 oid_md5WithRSAEncryption = {7, 1, 2, 840, 113549, 1, 1, 4 };
1652
static Ints15 oid_md5WithRSAEncryption = {7, 1, 2, 840, 113549, 1, 1, 4 };
1596
static Ints7 oid_sha1WithRSAEncryption ={7, 1, 2, 840, 113549, 1, 1, 5 };
1653
static Ints15 oid_sha1WithRSAEncryption ={7, 1, 2, 840, 113549, 1, 1, 5 };
1597
static Ints7 oid_sha1WithRSAEncryptionOiw ={6, 1, 3, 14, 3, 2, 29 };
1654
static Ints15 oid_sha1WithRSAEncryptionOiw ={6, 1, 3, 14, 3, 2, 29 };
-
 
1655
static Ints15 oid_sha256WithRSAEncryption = {7, 1, 2, 840, 113549, 1, 1, 11 };
-
 
1656
static Ints15 oid_sha384WithRSAEncryption = {7, 1, 2, 840, 113549, 1, 1, 12 };
-
 
1657
static Ints15 oid_sha512WithRSAEncryption = {7, 1, 2, 840, 113549, 1, 1, 13 };
-
 
1658
static Ints15 oid_sha224WithRSAEncryption = {7, 1, 2, 840, 113549, 1, 1, 14 };
-
 
1659
 
-
 
1660
static Ints15 oid_ecPublicKey = {6, 1, 2, 840, 10045, 2, 1 };
-
 
1661
static Ints15 oid_sha1WithECDSA = {6, 1, 2, 840, 10045, 4, 1 };
-
 
1662
static Ints15 oid_sha256WithECDSA = {7, 1, 2, 840, 10045, 4, 3, 2 };
-
 
1663
static Ints15 oid_sha384WithECDSA = {7, 1, 2, 840, 10045, 4, 3, 3 };
-
 
1664
static Ints15 oid_sha512WithECDSA = {7, 1, 2, 840, 10045, 4, 3, 4 };
-
 
1665
 
1598
static Ints7 oid_md5 ={6, 1, 2, 840, 113549, 2, 5, 0 };
1666
static Ints15 oid_md5 = {6, 1, 2, 840, 113549, 2, 5 };
-
 
1667
static Ints15 oid_sha1 = {6, 1, 3, 14, 3, 2, 26 };
-
 
1668
static Ints15 oid_sha256= {9, 2, 16, 840, 1, 101, 3, 4, 2, 1 };
-
 
1669
static Ints15 oid_sha384= {9, 2, 16, 840, 1, 101, 3, 4, 2, 2 };
-
 
1670
static Ints15 oid_sha512= {9, 2, 16, 840, 1, 101, 3, 4, 2, 3 };
-
 
1671
static Ints15 oid_sha224= {9, 2, 16, 840, 1, 101, 3, 4, 2, 4 };
-
 
1672
 
1599
static Ints *alg_oid_tab[NUMALGS+1] = {
1673
static Ints *alg_oid_tab[NUMALGS+1] = {
1600
	(Ints*)&oid_rsaEncryption,
1674
	(Ints*)&oid_rsaEncryption,
1601
	(Ints*)&oid_md2WithRSAEncryption,
1675
	(Ints*)&oid_md2WithRSAEncryption,
1602
	(Ints*)&oid_md4WithRSAEncryption,
1676
	(Ints*)&oid_md4WithRSAEncryption,
1603
	(Ints*)&oid_md5WithRSAEncryption,
1677
	(Ints*)&oid_md5WithRSAEncryption,
-
 
1678
 
1604
	(Ints*)&oid_sha1WithRSAEncryption,
1679
	(Ints*)&oid_sha1WithRSAEncryption,
1605
	(Ints*)&oid_sha1WithRSAEncryptionOiw,
1680
	(Ints*)&oid_sha1WithRSAEncryptionOiw,
-
 
1681
 
-
 
1682
	(Ints*)&oid_sha256WithRSAEncryption,
-
 
1683
	(Ints*)&oid_sha384WithRSAEncryption,
-
 
1684
	(Ints*)&oid_sha512WithRSAEncryption,
-
 
1685
	(Ints*)&oid_sha224WithRSAEncryption,
-
 
1686
 
-
 
1687
	(Ints*)&oid_ecPublicKey,
-
 
1688
	(Ints*)&oid_sha1WithECDSA,
-
 
1689
	(Ints*)&oid_sha256WithECDSA,
-
 
1690
	(Ints*)&oid_sha384WithECDSA,
-
 
1691
	(Ints*)&oid_sha512WithECDSA,
-
 
1692
 
1606
	(Ints*)&oid_md5,
1693
	(Ints*)&oid_md5,
-
 
1694
	(Ints*)&oid_sha1,
-
 
1695
	(Ints*)&oid_sha256,
-
 
1696
	(Ints*)&oid_sha384,
-
 
1697
	(Ints*)&oid_sha512,
-
 
1698
	(Ints*)&oid_sha224,
-
 
1699
	nil
-
 
1700
};
-
 
1701
 
-
 
1702
static DigestAlg *digestalg[NUMALGS+1] = {
-
 
1703
	&alg_md5, &alg_md5, &alg_md5, &alg_md5,
-
 
1704
	&alg_sha1, &alg_sha1,
-
 
1705
	&alg_sha256, &alg_sha384, &alg_sha512, &alg_sha224,
-
 
1706
	&alg_sha256, &alg_sha1, &alg_sha256, &alg_sha384, &alg_sha512,
-
 
1707
	&alg_md5, &alg_sha1, &alg_sha256, &alg_sha384, &alg_sha512, &alg_sha224,
1607
	nil
1708
	nil
1608
};
1709
};
-
 
1710
 
-
 
1711
static Bytes* encode_digest(DigestAlg *da, uchar *digest);
-
 
1712
 
1609
static DigestFun digestalg[NUMALGS+1] = { md5, md5, md5, md5, sha1, sha1, md5, nil };
1713
static Ints15 oid_secp256r1 = {7, 1, 2, 840, 10045, 3, 1, 7};
-
 
1714
static Ints15 oid_secp384r1 = {5, 1, 3, 132, 0, 34};
-
 
1715
 
-
 
1716
static Ints *namedcurves_oid_tab[] = {
-
 
1717
	(Ints*)&oid_secp256r1,
-
 
1718
	(Ints*)&oid_secp384r1,
-
 
1719
	nil,
-
 
1720
};
-
 
1721
static void (*namedcurves[])(mpint *p, mpint *a, mpint *b, mpint *x, mpint *y, mpint *n, mpint *h) = {
-
 
1722
	secp256r1,
-
 
1723
	secp384r1,
-
 
1724
	nil,
-
 
1725
};
1610
 
1726
 
1611
static void
1727
static void
1612
freecert(CertX509* c)
1728
freecert(CertX509* c)
1613
{
1729
{
1614
	if(!c) return;
1730
	if(c == nil)
1615
	if(c->issuer != nil)
1731
		return;
1616
		free(c->issuer);
1732
	free(c->issuer);
1617
	if(c->validity_start != nil)
-
 
1618
		free(c->validity_start);
1733
	free(c->validity_start);
1619
	if(c->validity_end != nil)
-
 
1620
		free(c->validity_end);
1734
	free(c->validity_end);
1621
	if(c->subject != nil)
-
 
1622
		free(c->subject);
1735
	free(c->subject);
1623
	freebytes(c->publickey);
1736
	freebits(c->publickey);
1624
	freebytes(c->signature);
1737
	freebits(c->signature);
1625
	free(c);
1738
	free(c);
1626
}
1739
}
1627
 
1740
 
1628
/*
1741
/*
1629
 * Parse the Name ASN1 type.
1742
 * Parse the Name ASN1 type.
Line 1691... Line 1804...
1691
 * or -1 if not found.
1804
 * or -1 if not found.
1692
 * For now, ignore parameters, since none of our algorithms need them.
1805
 * For now, ignore parameters, since none of our algorithms need them.
1693
 */
1806
 */
1694
static int
1807
static int
1695
parse_alg(Elem* e)
1808
parse_alg(Elem* e)
1696
{
1809
{
1697
	Elist* el;
1810
	Elist* el;
1698
	Ints* oid;
1811
	Ints* oid;
1699
 
1812
 
1700
	if(!is_seq(e, &el) || el == nil || !is_oid(&el->hd, &oid))
1813
	if(!is_seq(e, &el) || el == nil || !is_oid(&el->hd, &oid))
1701
		return -1;
1814
		return -1;
1702
	return oid_lookup(oid, alg_oid_tab);
1815
	return oid_lookup(oid, alg_oid_tab);
-
 
1816
}
-
 
1817
 
-
 
1818
static int
-
 
1819
parse_curve(Elem* e)
-
 
1820
{
-
 
1821
	Elist* el;
-
 
1822
	Ints* oid;
-
 
1823
 
-
 
1824
	if(!is_seq(e, &el) || elistlen(el)<2 || !is_oid(&el->tl->hd, &oid))
-
 
1825
		return -1;
-
 
1826
	return oid_lookup(oid, namedcurves_oid_tab);
1703
}
1827
}
1704
 
1828
 
1705
static CertX509*
1829
static CertX509*
1706
decode_cert(Bytes* a)
1830
decode_cert(uchar *buf, int len)
1707
{
1831
{
1708
	int ok = 0;
1832
	int ok = 0;
1709
	int n;
1833
	int n;
1710
	CertX509* c = nil;
-
 
1711
	Elem  ecert;
1834
	Elem  ecert;
1712
	Elem* ecertinfo;
1835
	Elem* ecertinfo;
1713
	Elem* esigalg;
1836
	Elem* esigalg;
1714
	Elem* esig;
1837
	Elem* esig;
1715
	Elem* eserial;
1838
	Elem* eserial;
Line 1723... Line 1846...
1723
	Elist* elvalidity = nil;
1846
	Elist* elvalidity = nil;
1724
	Elist* elpubkey = nil;
1847
	Elist* elpubkey = nil;
1725
	Bits* bits = nil;
1848
	Bits* bits = nil;
1726
	Bytes* b;
1849
	Bytes* b;
1727
	Elem* e;
1850
	Elem* e;
-
 
1851
	CertX509* c = nil;
1728
 
1852
 
1729
	if(decode(a->data, a->len, &ecert) != ASN_OK)
1853
	if(decode(buf, len, &ecert) != ASN_OK)
1730
		goto errret;
1854
		goto errret;
1731
 
1855
 
1732
	c = (CertX509*)emalloc(sizeof(CertX509));
1856
	c = (CertX509*)emalloc(sizeof(CertX509));
1733
	c->serial = -1;
1857
	c->serial = -1;
1734
	c->issuer = nil;
1858
	c->issuer = nil;
1735
	c->validity_start = nil;
1859
	c->validity_start = nil;
1736
	c->validity_end = nil;
1860
	c->validity_end = nil;
Line 1774... Line 1898...
1774
 	evalidity = &el->hd;
1898
 	evalidity = &el->hd;
1775
 	el = el->tl;
1899
 	el = el->tl;
1776
 	esubj = &el->hd;
1900
 	esubj = &el->hd;
1777
 	el = el->tl;
1901
 	el = el->tl;
1778
 	epubkey = &el->hd;
1902
 	epubkey = &el->hd;
1779
 	if(!is_int(eserial, &c->serial)) {
1903
	if(!is_int(eserial, &c->serial)) {
1780
		if(!is_bigint(eserial, &b))
1904
		if(!is_bigint(eserial, &b))
1781
			goto errret;
1905
			goto errret;
1782
		c->serial = -1;	/* else we have to change cert struct */
1906
		c->serial = -1;	/* else we have to change cert struct */
1783
  	}
1907
  	}
1784
	c->issuer = parse_name(eissuer);
1908
	c->issuer = parse_name(eissuer);
Line 1789... Line 1913...
1789
		goto errret;
1913
		goto errret;
1790
	if(elistlen(elvalidity) != 2)
1914
	if(elistlen(elvalidity) != 2)
1791
		goto errret;
1915
		goto errret;
1792
	e = &elvalidity->hd;
1916
	e = &elvalidity->hd;
1793
	if(!is_time(e, &c->validity_start))
1917
	if(!is_time(e, &c->validity_start))
1794
		goto errret;
1918
		goto errret;
1795
	e->val.u.stringval = nil;	/* string ownership transfer */
1919
	e->val.u.stringval = nil;	/* string ownership transfer */
1796
	e = &elvalidity->tl->hd;
1920
	e = &elvalidity->tl->hd;
1797
 	if(!is_time(e, &c->validity_end))
1921
 	if(!is_time(e, &c->validity_end))
1798
		goto errret;
1922
		goto errret;
1799
	e->val.u.stringval = nil;	/* string ownership transfer */
1923
	e->val.u.stringval = nil;	/* string ownership transfer */
1800
 
1924
 
1801
	/* resume CertificateInfo */
1925
	/* resume CertificateInfo */
1802
 	c->subject = parse_name(esubj);
1926
 	c->subject = parse_name(esubj);
1803
	if(c->subject == nil)
1927
	if(c->subject == nil)
1804
		goto errret;
1928
		goto errret;
1805
 
1929
 
1806
	/* SubjectPublicKeyInfo */
1930
	/* SubjectPublicKeyInfo */
1807
 	if(!is_seq(epubkey, &elpubkey))
1931
	if(!is_seq(epubkey, &elpubkey))
1808
		goto errret;
1932
		goto errret;
1809
	if(elistlen(elpubkey) != 2)
1933
	if(elistlen(elpubkey) != 2)
1810
		goto errret;
1934
		goto errret;
1811
 
1935
 
1812
	c->publickey_alg = parse_alg(&elpubkey->hd);
1936
	c->publickey_alg = parse_alg(&elpubkey->hd);
1813
	if(c->publickey_alg < 0)
1937
	if(c->publickey_alg < 0)
1814
		goto errret;
1938
		goto errret;
-
 
1939
	c->curve = -1;
-
 
1940
	if(c->publickey_alg == ALG_ecPublicKey){
1815
  	if(!is_bitstring(&elpubkey->tl->hd, &bits))
1941
		c->curve = parse_curve(&elpubkey->hd);
-
 
1942
		if(c->curve < 0)
1816
		goto errret;
1943
			goto errret;
-
 
1944
	}
-
 
1945
	elpubkey = elpubkey->tl;
1817
	if(bits->unusedbits != 0)
1946
	if(!is_bitstring(&elpubkey->hd, &bits))
1818
		goto errret;
1947
		goto errret;
-
 
1948
	elpubkey->hd.val.u.bitstringval = nil;	/* transfer ownership */
1819
 	c->publickey = makebytes(bits->data, bits->len);
1949
	c->publickey = bits;
1820
 
1950
 
1821
	/*resume Certificate */
1951
	/*resume Certificate */
1822
	if(c->signature_alg < 0)
1952
	if(c->signature_alg < 0)
1823
		goto errret;
1953
		goto errret;
1824
 	if(!is_bitstring(esig, &bits))
1954
	if(!is_bitstring(esig, &bits))
1825
		goto errret;
1955
		goto errret;
-
 
1956
	esig->val.u.bitstringval = nil;	/* transfer ownership */
1826
 	c->signature = makebytes(bits->data, bits->len);
1957
	c->signature = bits;
1827
	ok = 1;
1958
	ok = 1;
1828
 
1959
 
1829
errret:
1960
errret:
1830
	freevalfields(&ecert.val);	/* recurses through lists, too */
1961
	freevalfields(&ecert.val);	/* recurses through lists, too */
1831
	if(!ok){
1962
	if(!ok){
Line 1834... Line 1965...
1834
	}
1965
	}
1835
	return c;
1966
	return c;
1836
}
1967
}
1837
 
1968
 
1838
/*
1969
/*
1839
 *	RSAPublickKey :: SEQUENCE {
1970
 *	RSAPublickKey ::= SEQUENCE {
1840
 *		modulus INTEGER,
1971
 *		modulus INTEGER,
1841
 *		publicExponent INTEGER
1972
 *		publicExponent INTEGER
1842
 *	}
1973
 *	}
1843
 */
1974
 */
1844
static RSApub*
1975
RSApub*
1845
decode_rsapubkey(Bytes* a)
1976
asn1toRSApub(uchar *buf, int len)
1846
{
1977
{
1847
	Elem e;
1978
	Elem e;
1848
	Elist *el, *l;
1979
	Elist *el;
1849
	mpint *mp;
-
 
1850
	RSApub* key;
1980
	RSApub* key;
1851
 
1981
 
1852
	l = nil;
1982
	key = nil;
1853
	key = rsapuballoc();
-
 
1854
	if(decode(a->data, a->len, &e) != ASN_OK)
1983
	if(decode(buf, len, &e) != ASN_OK)
1855
		goto errret;
1984
		goto errret;
1856
	if(!is_seq(&e, &el) || elistlen(el) != 2)
1985
	if(!is_seq(&e, &el) || elistlen(el) != 2)
1857
		goto errret;
1986
		goto errret;
1858
 
1987
 
1859
	l = el;
1988
	key = rsapuballoc();
1860
 
-
 
1861
	key->n = mp = asn1mpint(&el->hd);
1989
	if((key->n = asn1mpint(&el->hd)) == nil)
1862
	if(mp == nil)
-
 
1863
		goto errret;
1990
		goto errret;
1864
 
-
 
1865
	el = el->tl;
1991
	el = el->tl;
1866
	key->ek = mp = asn1mpint(&el->hd);
1992
	if((key->ek = asn1mpint(&el->hd)) == nil)
1867
	if(mp == nil)
-
 
1868
		goto errret;
1993
		goto errret;
1869
 
1994
 
1870
	if(l != nil)
-
 
1871
		freeelist(l);
1995
	freevalfields(&e.val);
1872
	return key;
1996
	return key;
1873
errret:
1997
errret:
1874
	if(l != nil)
-
 
1875
		freeelist(l);
1998
	freevalfields(&e.val);
1876
	rsapubfree(key);
1999
	rsapubfree(key);
1877
	return nil;
2000
	return nil;
-
 
2001
 
1878
}
2002
}
1879
 
2003
 
1880
/*
2004
/*
1881
 *	RSAPrivateKey ::= SEQUENCE {
2005
 *	RSAPrivateKey ::= SEQUENCE {
1882
 *		version Version,
2006
 *		version Version,
Line 1887... Line 2011...
1887
 *		prime2 INTEGER, -- q
2011
 *		prime2 INTEGER, -- q
1888
 *		exponent1 INTEGER, -- d mod (p-1)
2012
 *		exponent1 INTEGER, -- d mod (p-1)
1889
 *		exponent2 INTEGER, -- d mod (q-1)
2013
 *		exponent2 INTEGER, -- d mod (q-1)
1890
 *		coefficient INTEGER -- (inverse of q) mod p }
2014
 *		coefficient INTEGER -- (inverse of q) mod p }
1891
 */
2015
 */
1892
static RSApriv*
2016
RSApriv*
1893
decode_rsaprivkey(Bytes* a)
2017
asn1toRSApriv(uchar *buf, int len)
1894
{
2018
{
1895
	int version;
2019
	int version;
1896
	Elem e;
2020
	Elem e;
1897
	Elist *el;
2021
	Elist *el;
1898
	mpint *mp;
2022
	Bytes *b;
1899
	RSApriv* key;
2023
	RSApriv* key = nil;
-
 
2024
 
-
 
2025
	if(decode(buf, len, &e) != ASN_OK)
-
 
2026
		goto errret;
-
 
2027
	if(!is_seq(&e, &el))
-
 
2028
		goto errret;
-
 
2029
 
-
 
2030
	if(!is_int(&el->hd, &version) || version != 0)
-
 
2031
		goto errret;
-
 
2032
 
-
 
2033
	if(elistlen(el) != 9){
-
 
2034
		if(elistlen(el) == 3
-
 
2035
		&& parse_alg(&el->tl->hd) == ALG_rsaEncryption
-
 
2036
		&& is_octetstring(&el->tl->tl->hd, &b)){
-
 
2037
			key = asn1toRSApriv(b->data, b->len);
-
 
2038
			if(key != nil)
-
 
2039
				goto done;
-
 
2040
		}
-
 
2041
		goto errret;
-
 
2042
	}
1900
 
2043
 
1901
	key = rsaprivalloc();
2044
	key = rsaprivalloc();
1902
	if(decode(a->data, a->len, &e) != ASN_OK)
-
 
1903
		goto errret;
-
 
1904
	if(!is_seq(&e, &el) || elistlen(el) != 9)
-
 
1905
		goto errret;
-
 
1906
	if(!is_int(&el->hd, &version) || version != 0)
-
 
1907
		goto errret;
-
 
1908
 
-
 
1909
	el = el->tl;
2045
	el = el->tl;
1910
	key->pub.n = mp = asn1mpint(&el->hd);
2046
	if((key->pub.n = asn1mpint(&el->hd)) == nil)
1911
	if(mp == nil)
-
 
1912
		goto errret;
2047
		goto errret;
1913
 
2048
 
1914
	el = el->tl;
2049
	el = el->tl;
1915
	key->pub.ek = mp = asn1mpint(&el->hd);
2050
	if((key->pub.ek = asn1mpint(&el->hd)) == nil)
1916
	if(mp == nil)
-
 
1917
		goto errret;
2051
		goto errret;
1918
 
2052
 
1919
	el = el->tl;
2053
	el = el->tl;
1920
	key->dk = mp = asn1mpint(&el->hd);
2054
	if((key->dk = asn1mpint(&el->hd)) == nil)
1921
	if(mp == nil)
-
 
1922
		goto errret;
2055
		goto errret;
1923
 
2056
 
1924
	el = el->tl;
2057
	el = el->tl;
1925
	key->q = mp = asn1mpint(&el->hd);
2058
	if((key->q = asn1mpint(&el->hd)) == nil)
1926
	if(mp == nil)
-
 
1927
		goto errret;
2059
		goto errret;
1928
 
2060
 
1929
	el = el->tl;
2061
	el = el->tl;
1930
	key->p = mp = asn1mpint(&el->hd);
2062
	if((key->p = asn1mpint(&el->hd)) == nil)
1931
	if(mp == nil)
-
 
1932
		goto errret;
2063
		goto errret;
1933
 
2064
 
1934
	el = el->tl;
2065
	el = el->tl;
1935
	key->kq = mp = asn1mpint(&el->hd);
2066
	if((key->kq = asn1mpint(&el->hd)) == nil)
1936
	if(mp == nil)
-
 
1937
		goto errret;
-
 
1938
 
-
 
1939
	el = el->tl;
-
 
1940
	key->kp = mp = asn1mpint(&el->hd);
-
 
1941
	if(mp == nil)
-
 
1942
		goto errret;
-
 
1943
 
-
 
1944
	el = el->tl;
-
 
1945
	key->c2 = mp = asn1mpint(&el->hd);
-
 
1946
	if(mp == nil)
-
 
1947
		goto errret;
-
 
1948
 
-
 
1949
	return key;
-
 
1950
errret:
-
 
1951
	rsaprivfree(key);
-
 
1952
	return nil;
-
 
1953
}
-
 
1954
 
-
 
1955
/*
-
 
1956
 * 	DSAPrivateKey ::= SEQUENCE{
-
 
1957
 *		version Version,
-
 
1958
 *		p INTEGER,
-
 
1959
 *		q INTEGER,
-
 
1960
 *		g INTEGER, -- alpha
-
 
1961
 *		pub_key INTEGER, -- key
-
 
1962
 *		priv_key INTEGER, -- secret
-
 
1963
 *	}
-
 
1964
 */
-
 
1965
static DSApriv*
-
 
1966
decode_dsaprivkey(Bytes* a)
-
 
1967
{
-
 
1968
	int version;
-
 
1969
	Elem e;
-
 
1970
	Elist *el;
-
 
1971
	mpint *mp;
-
 
1972
	DSApriv* key;
-
 
1973
 
-
 
1974
	key = dsaprivalloc();
-
 
1975
	if(decode(a->data, a->len, &e) != ASN_OK)
-
 
1976
		goto errret;
-
 
1977
	if(!is_seq(&e, &el) || elistlen(el) != 6)
-
 
1978
		goto errret;
-
 
1979
version = -1;
-
 
1980
	if(!is_int(&el->hd, &version) || version != 0)
-
 
1981
{
-
 
1982
fprint(2, "version %d\n", version);
-
 
1983
		goto errret;
-
 
1984
}
-
 
1985
 
-
 
1986
	el = el->tl;
-
 
1987
	key->pub.p = mp = asn1mpint(&el->hd);
-
 
1988
	if(mp == nil)
-
 
1989
		goto errret;
-
 
1990
 
-
 
1991
	el = el->tl;
-
 
1992
	key->pub.q = mp = asn1mpint(&el->hd);
-
 
1993
	if(mp == nil)
-
 
1994
		goto errret;
-
 
1995
 
-
 
1996
	el = el->tl;
-
 
1997
	key->pub.alpha = mp = asn1mpint(&el->hd);
-
 
1998
	if(mp == nil)
-
 
1999
		goto errret;
-
 
2000
 
-
 
2001
	el = el->tl;
-
 
2002
	key->pub.key = mp = asn1mpint(&el->hd);
-
 
2003
	if(mp == nil)
-
 
2004
		goto errret;
2067
		goto errret;
2005
 
2068
 
2006
	el = el->tl;
2069
	el = el->tl;
2007
	key->secret = mp = asn1mpint(&el->hd);
2070
	if((key->kp = asn1mpint(&el->hd)) == nil)
2008
	if(mp == nil)
-
 
2009
		goto errret;
2071
		goto errret;
2010
 
2072
 
-
 
2073
	el = el->tl;
-
 
2074
	if((key->c2 = asn1mpint(&el->hd)) == nil)
-
 
2075
		goto errret;
-
 
2076
 
-
 
2077
done:
-
 
2078
	freevalfields(&e.val);
2011
	return key;
2079
	return key;
2012
errret:
2080
errret:
-
 
2081
	freevalfields(&e.val);
2013
	dsaprivfree(key);
2082
	rsaprivfree(key);
2014
	return nil;
2083
	return nil;
2015
}
-
 
2016
 
-
 
2017
static mpint*
-
 
2018
asn1mpint(Elem *e)
-
 
2019
{
-
 
2020
	Bytes *b;
-
 
2021
	mpint *mp;
-
 
2022
	int v;
-
 
2023
 
-
 
2024
	if(is_int(e, &v))
-
 
2025
		return itomp(v, nil);
-
 
2026
	if(is_bigint(e, &b)) {
-
 
2027
		mp = betomp(b->data, b->len, nil);
-
 
2028
		freebytes(b);
-
 
2029
		return mp;
-
 
2030
	}
-
 
2031
	return nil;
-
 
2032
}
-
 
2033
 
-
 
2034
static mpint*
-
 
2035
pkcs1pad(Bytes *b, mpint *modulus)
-
 
2036
{
-
 
2037
	int n = (mpsignif(modulus)+7)/8;
-
 
2038
	int pm1, i;
-
 
2039
	uchar *p;
-
 
2040
	mpint *mp;
-
 
2041
 
-
 
2042
	pm1 = n - 1 - b->len;
-
 
2043
	p = (uchar*)emalloc(n);
-
 
2044
	p[0] = 0;
-
 
2045
	p[1] = 1;
-
 
2046
	for(i = 2; i < pm1; i++)
-
 
2047
		p[i] = 0xFF;
-
 
2048
	p[pm1] = 0;
-
 
2049
	memcpy(&p[pm1+1], b->data, b->len);
-
 
2050
	mp = betomp(p, n, nil);
-
 
2051
	free(p);
-
 
2052
	return mp;
-
 
2053
}
-
 
2054
 
-
 
2055
RSApriv*
-
 
2056
asn1toRSApriv(uchar *kd, int kn)
-
 
2057
{
-
 
2058
	Bytes *b;
-
 
2059
	RSApriv *key;
-
 
2060
 
-
 
2061
	b = makebytes(kd, kn);
-
 
2062
	key = decode_rsaprivkey(b);
-
 
2063
	freebytes(b);
-
 
2064
	return key;
-
 
2065
}
-
 
2066
 
-
 
2067
DSApriv*
-
 
2068
asn1toDSApriv(uchar *kd, int kn)
-
 
2069
{
-
 
2070
	Bytes *b;
-
 
2071
	DSApriv *key;
-
 
2072
 
-
 
2073
	b = makebytes(kd, kn);
-
 
2074
	key = decode_dsaprivkey(b);
-
 
2075
	freebytes(b);
-
 
2076
	return key;
-
 
2077
}
2084
}
2078
 
2085
 
2079
/*
2086
/*
2080
 * digest(CertificateInfo)
2087
 * digest(CertificateInfo)
2081
 * Our ASN.1 library doesn't return pointers into the original
2088
 * Our ASN.1 library doesn't return pointers into the original
2082
 * data array, so we need to do a little hand decoding.
2089
 * data array, so we need to do a little hand decoding.
2083
 */
2090
 */
2084
static void
2091
static int
2085
digest_certinfo(Bytes *cert, DigestFun digestfun, uchar *digest)
2092
digest_certinfo(uchar *cert, int ncert, DigestAlg *da, uchar *digest)
2086
{
2093
{
2087
	uchar *info, *p, *pend;
2094
	uchar *info, *p, *pend;
2088
	ulong infolen;
-
 
2089
	int isconstr, length;
2095
	int isconstr, length;
2090
	Tag tag;
2096
	Tag tag;
2091
	Elem elem;
2097
	Elem elem;
2092
 
2098
 
2093
	p = cert->data;
2099
	p = cert;
2094
	pend = cert->data + cert->len;
2100
	pend = cert + ncert;
2095
	if(tag_decode(&p, pend, &tag, &isconstr) != ASN_OK ||
2101
	if(tag_decode(&p, pend, &tag, &isconstr) != ASN_OK ||
2096
	   tag.class != Universal || tag.num != SEQUENCE ||
2102
	   tag.class != Universal || tag.num != SEQUENCE ||
2097
	   length_decode(&p, pend, &length) != ASN_OK ||
2103
	   length_decode(&p, pend, &length) != ASN_OK ||
2098
	   p+length > pend ||
2104
	   p+length > pend ||
2099
	   p+length < p)
2105
	   p+length < p)
2100
		return;
2106
		return -1;
2101
	info = p;
2107
	info = p;
2102
	if(ber_decode(&p, pend, &elem) != ASN_OK)
2108
	if(ber_decode(&p, pend, &elem) != ASN_OK)
2103
		return;
2109
		return -1;
2104
	freevalfields(&elem.val);
2110
	freevalfields(&elem.val);
2105
	if(elem.tag.num != SEQUENCE)
2111
	if(elem.tag.num != SEQUENCE)
2106
		return;
2112
		return -1;
-
 
2113
	(*da->fun)(info, p - info, digest, nil);
-
 
2114
	return da->len;
-
 
2115
}
-
 
2116
 
-
 
2117
mpint*
-
 
2118
pkcs1padbuf(uchar *buf, int len, mpint *modulus, int blocktype)
-
 
2119
{
-
 
2120
	int i, n = (mpsignif(modulus)-1)/8;
2107
	infolen = p - info;
2121
	int pad = n - 2 - len;
-
 
2122
	uchar *p;
-
 
2123
	mpint *mp;
-
 
2124
 
-
 
2125
	if(pad < 8){
-
 
2126
		werrstr("rsa modulus too small");
-
 
2127
		return nil;
-
 
2128
	}
-
 
2129
	if((p = malloc(n)) == nil)
-
 
2130
		return nil;
-
 
2131
	p[0] = blocktype;
-
 
2132
	switch(blocktype){
-
 
2133
	default:
-
 
2134
	case 1:
-
 
2135
		memset(p+1, 0xFF, pad);
-
 
2136
		break;
-
 
2137
	case 2:
-
 
2138
		for(i=1; i <= pad; i++)
-
 
2139
			p[i] = 1 + nfastrand(255);
-
 
2140
		break;
-
 
2141
	}
-
 
2142
	p[1+pad] = 0;
-
 
2143
	memmove(p+2+pad, buf, len);
-
 
2144
	mp = betomp(p, n, nil);
-
 
2145
	free(p);
-
 
2146
	return mp;
-
 
2147
}
-
 
2148
 
-
 
2149
int
-
 
2150
pkcs1unpadbuf(uchar *buf, int len, mpint *modulus, int blocktype)
-
 
2151
{
-
 
2152
	uchar *p = buf + 1, *e = buf + len;
-
 
2153
 
-
 
2154
	if(len < 1 || len != (mpsignif(modulus)-1)/8 || buf[0] != blocktype)
-
 
2155
		return -1;
-
 
2156
	switch(blocktype){
-
 
2157
	default:
-
 
2158
	case 1:
-
 
2159
		while(p < e && *p == 0xFF)
-
 
2160
			p++;
-
 
2161
		break;
-
 
2162
	case 2:
-
 
2163
		while(p < e && *p != 0x00)
-
 
2164
			p++;
-
 
2165
		break;
-
 
2166
	}
-
 
2167
	if(p - buf <= 8 || p >= e || *p++ != 0x00)
-
 
2168
		return -1;
-
 
2169
	memmove(buf, p, len = e - p);
-
 
2170
	return len;
-
 
2171
}
-
 
2172
 
-
 
2173
static char Ebadsig[] = "bad signature";
-
 
2174
 
-
 
2175
char*
-
 
2176
X509rsaverifydigest(uchar *sig, int siglen, uchar *edigest, int edigestlen, RSApub *pk)
-
 
2177
{
-
 
2178
	mpint *x, *y;
-
 
2179
	DigestAlg **dp;
-
 
2180
	Bytes *digest;
-
 
2181
	uchar *buf;
-
 
2182
	int len;
-
 
2183
	char *err;
-
 
2184
 
-
 
2185
	x = betomp(sig, siglen, nil);
-
 
2186
	y = rsaencrypt(pk, x, nil);
-
 
2187
	mpfree(x);
-
 
2188
	len = mptobe(y, nil, 0, &buf);
-
 
2189
	mpfree(y);	
-
 
2190
 
-
 
2191
	err = Ebadsig;
-
 
2192
	len = pkcs1unpadbuf(buf, len, pk->n, 1);
-
 
2193
	if(len == edigestlen && tsmemcmp(buf, edigest, edigestlen) == 0)
-
 
2194
		err = nil;
-
 
2195
	for(dp = digestalg; err != nil && *dp != nil; dp++){
-
 
2196
		if((*dp)->len != edigestlen)
-
 
2197
			continue;
2108
	(*digestfun)(info, infolen, digest, nil);
2198
		digest = encode_digest(*dp, edigest);
-
 
2199
		if(digest->len == len && tsmemcmp(digest->data, buf, len) == 0)
-
 
2200
			err = nil;
-
 
2201
		freebytes(digest);
-
 
2202
	}
-
 
2203
	free(buf);
-
 
2204
	return err;
2109
}
2205
}
2110
 
2206
 
2111
static char*
2207
char*
2112
verify_signature(Bytes* signature, RSApub *pk, uchar *edigest, Elem **psigalg)
2208
X509ecdsaverifydigest(uchar *sig, int siglen, uchar *edigest, int edigestlen, ECdomain *dom, ECpub *pub)
2113
{
2209
{
2114
	Elem e;
2210
	Elem e;
2115
	Elist *el;
2211
	Elist *el;
2116
	Bytes *digest;
-
 
2117
	uchar *pkcs1buf, *buf;
-
 
2118
	int buflen;
-
 
2119
	mpint *pkcs1;
2212
	mpint *r, *s;
2120
	int nlen;
-
 
2121
	char *err;
2213
	char *err;
2122
 
2214
 
2123
	err = nil;
2215
	r = s = nil;
2124
	pkcs1buf = nil;
-
 
2125
 
-
 
2126
	/* one less than the byte length of the modulus */
-
 
2127
	nlen = (mpsignif(pk->n)-1)/8;
-
 
2128
 
-
 
2129
	/* see 9.2.1 of rfc2437 */
-
 
2130
	pkcs1 = betomp(signature->data, signature->len, nil);
-
 
2131
	mpexp(pkcs1, pk->ek, pk->n, pkcs1);
-
 
2132
	buflen = mptobe(pkcs1, nil, 0, &pkcs1buf);
-
 
2133
	buf = pkcs1buf;
2216
	err = Ebadsig;
2134
	if(buflen != nlen || buf[0] != 1) {
2217
	if(decode(sig, siglen, &e) != ASN_OK)
2135
		err = "expected 1";
-
 
2136
		goto end;
2218
		goto end;
2137
	}
-
 
2138
	buf++;
-
 
2139
	while(buf[0] == 0xff)
-
 
2140
		buf++;
-
 
2141
	if(buf[0] != 0) {
-
 
2142
		err = "expected 0";
2219
	if(!is_seq(&e, &el) || elistlen(el) != 2)
2143
		goto end;
2220
		goto end;
2144
	}
-
 
2145
	buf++;
-
 
2146
	buflen -= buf-pkcs1buf;
2221
	r = asn1mpint(&el->hd);
2147
	if(decode(buf, buflen, &e) != ASN_OK || !is_seq(&e, &el) || elistlen(el) != 2 ||
-
 
2148
			!is_octetstring(&el->tl->hd, &digest)) {
-
 
2149
		err = "signature parse error";
2222
	if(r == nil)
2150
		goto end;
2223
		goto end;
2151
	}
2224
	el = el->tl;
2152
	*psigalg = &el->hd;
2225
	s = asn1mpint(&el->hd);
2153
	if(memcmp(digest->data, edigest, digest->len) == 0)
2226
	if(s == nil)
2154
		goto end;
2227
		goto end;
2155
	err = "digests did not match";
2228
	if(ecdsaverify(dom, pub, edigest, edigestlen, r, s))
2156
 
2229
		err = nil;
2157
end:
2230
end:
2158
	if(pkcs1 != nil)
2231
	freevalfields(&e.val);
2159
		mpfree(pkcs1);
2232
	mpfree(s);
2160
	if(pkcs1buf != nil)
-
 
2161
		free(pkcs1buf);
2233
	mpfree(r);
2162
	return err;
2234
	return err;
2163
}
2235
}
2164
 
2236
 
2165
RSApub*
2237
static void
2166
X509toRSApub(uchar *cert, int ncert, char *name, int nname)
2238
copysubject(char *name, int nname, char *subject)
2167
{
2239
{
2168
	char *e;
2240
	char *e;
2169
	Bytes *b;
-
 
2170
	CertX509 *c;
-
 
2171
	RSApub *pk;
-
 
2172
 
2241
 
2173
	b = makebytes(cert, ncert);
2242
	if(name == nil)
2174
	c = decode_cert(b);
2243
		return;
2175
	freebytes(b);
2244
	memset(name, 0, nname);
2176
	if(c == nil)
2245
	if(subject == nil)
2177
		return nil;
2246
		return;
2178
	if(name != nil && c->subject != nil){
2247
	strncpy(name, subject, nname-1);
2179
		e = strchr(c->subject, ',');
2248
	e = strchr(name, ',');
2180
		if(e != nil)
2249
	if(e != nil)
2181
			*e = 0;	/* take just CN part of Distinguished Name */
2250
		*e = 0;	/* take just CN part of Distinguished Name */
2182
		strncpy(name, c->subject, nname);
-
 
2183
	}
-
 
2184
	pk = decode_rsapubkey(c->publickey);
-
 
2185
	freecert(c);
-
 
2186
	return pk;
-
 
2187
}
-
 
2188
 
-
 
2189
int
-
 
2190
getalgo(Elem *e)
-
 
2191
{
-
 
2192
	Value *v;
-
 
2193
	Elist *el;
-
 
2194
	int a;
-
 
2195
 
-
 
2196
	if((a = parse_alg(e)) >= 0)
-
 
2197
		return a;
-
 
2198
	v = &e->val;
-
 
2199
	if(v->tag == VSeq){
-
 
2200
		print("Seq\n");
-
 
2201
		for(el = v->u.seqval; el!=nil; el = el->tl){
-
 
2202
			if((a = getalgo(&el->hd)) >= 0)
-
 
2203
				return a;
-
 
2204
		}
-
 
2205
	}
-
 
2206
	return -1;
-
 
2207
}
2251
}
2208
 
2252
 
2209
static void edump(Elem e);
-
 
2210
 
-
 
2211
RSApub*
2253
ECpub*
2212
asn1toRSApub(uchar *der, int nder)
2254
X509toECpub(uchar *cert, int ncert, char *name, int nname, ECdomain *dom)
2213
{
2255
{
2214
	Elem e;
-
 
2215
	Elist *el, *l;
-
 
2216
	int n;
-
 
2217
	Bits *b;
2256
	CertX509 *c;
2218
	RSApub *key;
2257
	ECpub *pub;
2219
	mpint *mp;
-
 
2220
 
2258
 
2221
	if(decode(der, nder, &e) != ASN_OK){
2259
	c = decode_cert(cert, ncert);
2222
		print("didn't parse\n");
-
 
2223
		return nil;
-
 
2224
	}
-
 
2225
	if(!is_seq(&e, &el)){
-
 
2226
		print("no seq");
-
 
2227
		return nil;
-
 
2228
	}
-
 
2229
	if((n = elistlen(el)) != 2){
-
 
2230
		print("bad length %d\n", n);
-
 
2231
		return nil;
-
 
2232
	}
-
 
2233
	if((n = getalgo(&el->hd)) < 0){
-
 
2234
		print("no algo\n");
-
 
2235
		return nil;
-
 
2236
	}
-
 
2237
	if(n != 0){
2260
	if(c == nil)
2238
		print("cant do algorithm %d\n", n);
-
 
2239
		return nil;
-
 
2240
	}
-
 
2241
	if(!is_bitstring(&el->tl->hd, &b)){
-
 
2242
		print("no bits\n");
-
 
2243
		return nil;
-
 
2244
	}
-
 
2245
	if(decode(b->data, b->len, &e) != ASN_OK){
-
 
2246
		print("no second decode\n");
-
 
2247
		return nil;
-
 
2248
	}
-
 
2249
	if(!is_seq(&e, &el)){
-
 
2250
		print("no second seq\n");
-
 
2251
		return nil;
-
 
2252
	}
-
 
2253
	if(elistlen(el) != 2){
-
 
2254
		print("no second length\n");
-
 
2255
		return nil;
2261
		return nil;
-
 
2262
	copysubject(name, nname, c->subject);
-
 
2263
	pub = nil;
-
 
2264
	if(c->publickey_alg == ALG_ecPublicKey){
-
 
2265
		ecdominit(dom, namedcurves[c->curve]);
-
 
2266
		pub = ecdecodepub(dom, c->publickey->data, c->publickey->len);
-
 
2267
		if(pub == nil)
-
 
2268
			ecdomfree(dom);
2256
	}
2269
	}
2257
	key = rsapuballoc();
-
 
2258
 
-
 
2259
	l = el;
-
 
2260
 
-
 
2261
	key->n = mp = asn1mpint(&el->hd);
-
 
2262
	if(mp == nil)
2270
	freecert(c);
2263
		goto errret;
2271
	return pub;
2264
 
2272
}
2265
	el = el->tl;
-
 
2266
	key->ek = mp = asn1mpint(&el->hd);
-
 
2267
	if(mp == nil)
-
 
2268
		goto errret;
-
 
2269
 
2273
 
-
 
2274
char*
-
 
2275
X509ecdsaverify(uchar *cert, int ncert, ECdomain *dom, ECpub *pk)
-
 
2276
{
-
 
2277
	char *e;
-
 
2278
	CertX509 *c;
-
 
2279
	int digestlen;
-
 
2280
	uchar digest[MAXdlen];
-
 
2281
 
-
 
2282
	c = decode_cert(cert, ncert);
2270
	if(l != nil)
2283
	if(c == nil)
-
 
2284
		return "cannot decode cert";
-
 
2285
	digestlen = digest_certinfo(cert, ncert, digestalg[c->signature_alg], digest);
-
 
2286
	if(digestlen <= 0){
2271
		freeelist(l);
2287
		freecert(c);
-
 
2288
		return "cannot decode certinfo";
-
 
2289
	}
-
 
2290
	e = X509ecdsaverifydigest(c->signature->data, c->signature->len, digest, digestlen, dom, pk);
-
 
2291
	freecert(c);
2272
	return key;
2292
	return e;
-
 
2293
}
-
 
2294
 
2273
errret:
2295
RSApub*
-
 
2296
X509toRSApub(uchar *cert, int ncert, char *name, int nname)
-
 
2297
{
-
 
2298
	CertX509 *c;
-
 
2299
	RSApub *pub;
-
 
2300
 
-
 
2301
	c = decode_cert(cert, ncert);
2274
	if(l != nil)
2302
	if(c == nil)
2275
		freeelist(l);
2303
		return nil;
-
 
2304
	copysubject(name, nname, c->subject);
-
 
2305
	pub = nil;
-
 
2306
	if(c->publickey_alg == ALG_rsaEncryption)
-
 
2307
		pub = asn1toRSApub(c->publickey->data, c->publickey->len);
2276
	rsapubfree(key);
2308
	freecert(c);
2277
	return nil;
2309
	return pub;
2278
}
2310
}
2279
 
2311
 
2280
char*
2312
char*
2281
X509verify(uchar *cert, int ncert, RSApub *pk)
2313
X509rsaverify(uchar *cert, int ncert, RSApub *pk)
2282
{
2314
{
2283
	char *e;
2315
	char *e;
2284
	Bytes *b;
-
 
2285
	CertX509 *c;
2316
	CertX509 *c;
2286
	uchar digest[SHA1dlen];
2317
	int digestlen;
2287
	Elem *sigalg;
2318
	uchar digest[MAXdlen];
2288
 
2319
 
2289
	b = makebytes(cert, ncert);
2320
	c = decode_cert(cert, ncert);
2290
	c = decode_cert(b);
-
 
2291
	if(c != nil)
-
 
2292
		digest_certinfo(b, digestalg[c->signature_alg], digest);
-
 
2293
	freebytes(b);
-
 
2294
	if(c == nil)
2321
	if(c == nil)
2295
		return "cannot decode cert";
2322
		return "cannot decode cert";
-
 
2323
	digestlen = digest_certinfo(cert, ncert, digestalg[c->signature_alg], digest);
-
 
2324
	if(digestlen <= 0){
-
 
2325
		freecert(c);
-
 
2326
		return "cannot decode certinfo";
-
 
2327
	}
2296
	e = verify_signature(c->signature, pk, digest, &sigalg);
2328
	e = X509rsaverifydigest(c->signature->data, c->signature->len, digest, digestlen, pk);
2297
	freecert(c);
2329
	freecert(c);
2298
	return e;
2330
	return e;
2299
}
2331
}
2300
 
2332
 
2301
/* ------- Elem constructors ---------- */
2333
/* ------- Elem constructors ---------- */
2302
static Elem
2334
static Elem
2303
Null(void)
2335
Null(void)
2304
{
2336
{
2305
	Elem e;
2337
	Elem e;
2306
 
2338
 
2307
	e.tag.class = Universal;
2339
	e.tag.class = Universal;
2308
	e.tag.num = NULLTAG;
2340
	e.tag.num = NULLTAG;
2309
	e.val.tag = VNull;
2341
	e.val.tag = VNull;
2310
	return e;
2342
	return e;
2311
}
2343
}
Line 2317... Line 2349...
2317
 
2349
 
2318
	e.tag.class = Universal;
2350
	e.tag.class = Universal;
2319
	e.tag.num = INTEGER;
2351
	e.tag.num = INTEGER;
2320
	e.val.tag = VInt;
2352
	e.val.tag = VInt;
2321
	e.val.u.intval = j;
2353
	e.val.u.intval = j;
2322
	return e;
2354
	return e;
2323
}
2355
}
2324
 
2356
 
2325
static Elem
2357
static Elem
2326
mkbigint(mpint *p)
2358
mkbigint(mpint *p)
2327
{
2359
{
2328
	Elem e;
2360
	Elem e;
2329
	uchar *buf;
-
 
2330
	int buflen;
-
 
2331
 
2361
 
2332
	e.tag.class = Universal;
2362
	e.tag.class = Universal;
2333
	e.tag.num = INTEGER;
2363
	e.tag.num = INTEGER;
2334
	e.val.tag = VBigInt;
2364
	e.val.tag = VBigInt;
-
 
2365
	e.val.u.bigintval = newbytes((mpsignif(p)+8)/8);
-
 
2366
	if(p->sign < 0){
-
 
2367
		mpint *s = mpnew(e.val.u.bigintval->len*8+1);
-
 
2368
		mpleft(mpone, e.val.u.bigintval->len*8, s);
2335
	buflen = mptobe(p, nil, 0, &buf);
2369
		mpadd(p, s, s);
2336
	e.val.u.bigintval = makebytes(buf, buflen);
2370
		mptober(s, e.val.u.bigintval->data, e.val.u.bigintval->len);
2337
	free(buf);
2371
		mpfree(s);
-
 
2372
	} else {
-
 
2373
		mptober(p, e.val.u.bigintval->data, e.val.u.bigintval->len);
-
 
2374
	}
2338
	return e;
2375
	return e;
2339
}
2376
}
-
 
2377
 
-
 
2378
static int
-
 
2379
printable(char *s)
-
 
2380
{
-
 
2381
	int c;
-
 
2382
 
-
 
2383
	while((c = (uchar)*s++) != 0){
-
 
2384
		if((c >= 'a' && c <= 'z')
-
 
2385
		|| (c >= 'A' && c <= 'Z')
-
 
2386
		|| (c >= '0' && c <= '9')
-
 
2387
		|| strchr("'=()+,-./:? ", c) != nil)
-
 
2388
			continue;
-
 
2389
		return 0;
-
 
2390
	}
-
 
2391
	return 1;
-
 
2392
}
-
 
2393
 
-
 
2394
#define DirectoryString 0
2340
 
2395
 
2341
static Elem
2396
static Elem
2342
mkstring(char *s)
2397
mkstring(char *s, int t)
2343
{
2398
{
2344
	Elem e;
2399
	Elem e;
2345
 
2400
 
-
 
2401
	if(t == DirectoryString)
-
 
2402
		t = printable(s) ? PrintableString : UTF8String;
2346
	e.tag.class = Universal;
2403
	e.tag.class = Universal;
2347
	e.tag.num = IA5String;
2404
	e.tag.num = t;
2348
	e.val.tag = VString;
2405
	e.val.tag = VString;
2349
	e.val.u.stringval = estrdup(s);
2406
	e.val.u.stringval = estrdup(s);
2350
	return e;
2407
	return e;
2351
}
2408
}
2352
 
-
 
2353
static Elem
-
 
2354
mkoctet(uchar *buf, int buflen)
-
 
2355
{
-
 
2356
	Elem e;
-
 
2357
 
2409
 
-
 
2410
static Elem
-
 
2411
mkoctet(uchar *buf, int buflen)
-
 
2412
{
-
 
2413
	Elem e;
-
 
2414
 
2358
	e.tag.class = Universal;
2415
	e.tag.class = Universal;
2359
	e.tag.num = OCTET_STRING;
2416
	e.tag.num = OCTET_STRING;
2360
	e.val.tag = VOctets;
2417
	e.val.tag = VOctets;
2361
	e.val.u.octetsval = makebytes(buf, buflen);
2418
	e.val.u.octetsval = makebytes(buf, buflen);
2362
	return e;
2419
	return e;
2363
}
2420
}
2364
 
2421
 
2365
static Elem
2422
static Elem
2366
mkbits(uchar *buf, int buflen)
2423
mkbits(uchar *buf, int buflen)
2367
{
2424
{
2368
	Elem e;
2425
	Elem e;
2369
 
2426
 
2370
	e.tag.class = Universal;
2427
	e.tag.class = Universal;
2371
	e.tag.num = BIT_STRING;
2428
	e.tag.num = BIT_STRING;
2372
	e.val.tag = VBitString;
2429
	e.val.tag = VBitString;
Line 2382... Line 2439...
2382
	Tm *tm = gmtime(t);
2439
	Tm *tm = gmtime(t);
2383
 
2440
 
2384
	e.tag.class = Universal;
2441
	e.tag.class = Universal;
2385
	e.tag.num = UTCTime;
2442
	e.tag.num = UTCTime;
2386
	e.val.tag = VString;
2443
	e.val.tag = VString;
2387
	snprint(utc, 50, "%.2d%.2d%.2d%.2d%.2d%.2dZ",
2444
	snprint(utc, sizeof(utc), "%.2d%.2d%.2d%.2d%.2d%.2dZ",
2388
		tm->year % 100, tm->mon+1, tm->mday, tm->hour, tm->min, tm->sec);
2445
		tm->year % 100, tm->mon+1, tm->mday, tm->hour, tm->min, tm->sec);
2389
	e.val.u.stringval = estrdup(utc);
2446
	e.val.u.stringval = estrdup(utc);
2390
	return e;
2447
	return e;
2391
}
2448
}
2392
 
2449
 
2393
static Elem
2450
static Elem
2394
mkoid(Ints *oid)
2451
mkoid(Ints *oid)
2395
{
2452
{
2396
	Elem e;
2453
	Elem e;
2397
 
2454
 
2398
	e.tag.class = Universal;
2455
	e.tag.class = Universal;
2399
	e.tag.num = OBJECT_ID;
2456
	e.tag.num = OBJECT_ID;
2400
	e.val.tag = VObjId;
2457
	e.val.tag = VObjId;
2401
	e.val.u.objidval = makeints(oid->data, oid->len);
2458
	e.val.u.objidval = makeints(oid->data, oid->len);
2402
	return e;
2459
	return e;
2403
}
2460
}
2404
 
2461
 
2405
static Elem
2462
static Elem
2406
mkseq(Elist *el)
2463
mkseq(Elist *el)
2407
{
2464
{
2408
	Elem e;
2465
	Elem e;
2409
 
2466
 
2410
	e.tag.class = Universal;
2467
	e.tag.class = Universal;
2411
	e.tag.num = SEQUENCE;
2468
	e.tag.num = SEQUENCE;
2412
	e.val.tag = VSeq;
2469
	e.val.tag = VSeq;
2413
	e.val.u.seqval = el;
2470
	e.val.u.seqval = el;
2414
	return e;
2471
	return e;
2415
}
2472
}
2416
 
2473
 
2417
static Elem
2474
static Elem
2418
mkset(Elist *el)
2475
mkset(Elist *el)
2419
{
2476
{
2420
	Elem e;
2477
	Elem e;
2421
 
2478
 
2422
	e.tag.class = Universal;
2479
	e.tag.class = Universal;
2423
	e.tag.num = SETOF;
2480
	e.tag.num = SETOF;
2424
	e.val.tag = VSet;
2481
	e.val.tag = VSet;
2425
	e.val.u.setval = el;
2482
	e.val.u.setval = el;
2426
	return e;
2483
	return e;
2427
}
2484
}
2428
 
2485
 
2429
static Elem
2486
static Elem
2430
mkalg(int alg)
2487
mkalg(int alg)
2431
{
2488
{
2432
	return mkseq(mkel(mkoid(alg_oid_tab[alg]), mkel(Null(), nil)));
2489
	return mkseq(mkel(mkoid(alg_oid_tab[alg]), mkel(Null(), nil)));
2433
}
2490
}
2434
 
2491
 
2435
typedef struct Ints7pref {
2492
typedef struct Ints7pref {
2436
	int		len;
2493
	int	len;
2437
	int		data[7];
2494
	int	data[7];
2438
	char	prefix[4];
2495
	char	prefix[4];
-
 
2496
	int	stype;
2439
} Ints7pref;
2497
} Ints7pref;
2440
Ints7pref DN_oid[] = {
2498
Ints7pref DN_oid[] = {
2441
	{4, 2, 5, 4, 6, 0, 0, 0,  "C="},
2499
	{4, 2, 5, 4, 6, 0, 0, 0,        "C=", PrintableString},
2442
	{4, 2, 5, 4, 8, 0, 0, 0,  "ST="},
2500
	{4, 2, 5, 4, 8, 0, 0, 0,        "ST=",DirectoryString},
2443
	{4, 2, 5, 4, 7, 0, 0, 0,  "L="},
2501
	{4, 2, 5, 4, 7, 0, 0, 0,        "L=", DirectoryString},
2444
	{4, 2, 5, 4, 10, 0, 0, 0, "O="},
2502
	{4, 2, 5, 4, 10, 0, 0, 0,       "O=", DirectoryString},
2445
	{4, 2, 5, 4, 11, 0, 0, 0, "OU="},
2503
	{4, 2, 5, 4, 11, 0, 0, 0,       "OU=",DirectoryString},
2446
	{4, 2, 5, 4, 3, 0, 0, 0,  "CN="},
2504
	{4, 2, 5, 4, 3, 0, 0, 0,        "CN=",DirectoryString},
2447
 	{7, 1,2,840,113549,1,9,1, "E="},
2505
	{7, 1,2,840,113549,1,9,1,       "E=", IA5String},
-
 
2506
	{7, 0,9,2342,19200300,100,1,25,	"DC=",IA5String},
2448
};
2507
};
2449
 
2508
 
2450
static Elem
2509
static Elem
2451
mkname(Ints7pref *oid, char *subj)
2510
mkname(Ints7pref *oid, char *subj)
2452
{
2511
{
2453
	return mkset(mkel(mkseq(mkel(mkoid((Ints*)oid), mkel(mkstring(subj), nil))), nil));
2512
	return mkset(mkel(mkseq(mkel(mkoid((Ints*)oid), mkel(mkstring(subj, oid->stype), nil))), nil));
2454
}
2513
}
2455
 
2514
 
2456
static Elem
2515
static Elem
2457
mkDN(char *dn)
2516
mkDN(char *dn)
2458
{
2517
{
Line 2472... Line 2531...
2472
	}
2531
	}
2473
	free(d2);
2532
	free(d2);
2474
	return mkseq(el);
2533
	return mkseq(el);
2475
}
2534
}
2476
 
2535
 
2477
uchar*
2536
/*
-
 
2537
 * DigestInfo ::= SEQUENCE {
-
 
2538
 *	digestAlgorithm AlgorithmIdentifier,
-
 
2539
 *	digest OCTET STRING }
-
 
2540
 */
-
 
2541
static Bytes*
2478
RSApubtoasn1(RSApub *pub, int *keylen)
2542
encode_digest(DigestAlg *da, uchar *digest)
2479
{
2543
{
-
 
2544
	Bytes *b = nil;
2480
	Elem pubkey;
2545
	Elem e = mkseq(
-
 
2546
		mkel(mkalg(da->alg),
-
 
2547
		mkel(mkoctet(digest, da->len),
-
 
2548
		nil)));
-
 
2549
	encode(e, &b);
-
 
2550
	freevalfields(&e.val);
-
 
2551
	return b;
-
 
2552
}
-
 
2553
 
-
 
2554
int
-
 
2555
asn1encodedigest(DigestState* (*fun)(uchar*, ulong, uchar*, DigestState*), uchar *digest, uchar *buf, int len)
-
 
2556
{
2481
	Bytes *pkbytes;
2557
	Bytes *bytes;
-
 
2558
	DigestAlg **dp;
-
 
2559
 
-
 
2560
	for(dp = digestalg; *dp != nil; dp++){
-
 
2561
		if((*dp)->fun != fun)
-
 
2562
			continue;
-
 
2563
		bytes = encode_digest(*dp, digest);
-
 
2564
		if(bytes == nil)
-
 
2565
			break;
-
 
2566
		if(bytes->len > len){
-
 
2567
			freebytes(bytes);
-
 
2568
			break;
-
 
2569
		}
-
 
2570
		len = bytes->len;
-
 
2571
		memmove(buf, bytes->data, len);
-
 
2572
		freebytes(bytes);
-
 
2573
		return len;
-
 
2574
	}
-
 
2575
	return -1;
-
 
2576
}
-
 
2577
 
-
 
2578
static Elem
-
 
2579
mkcont(Elem e, int num)
-
 
2580
{
-
 
2581
	e = mkseq(mkel(e, nil));
-
 
2582
	e.tag.class = Context;
-
 
2583
	e.tag.num = num;
-
 
2584
	return e;
-
 
2585
}
-
 
2586
 
-
 
2587
static Elem
-
 
2588
mkaltname(char *s)
-
 
2589
{
-
 
2590
	Elem e;
-
 
2591
	int i;
-
 
2592
 
-
 
2593
	for(i=0; i<nelem(DN_oid); i++){
-
 
2594
		if(strstr(s, DN_oid[i].prefix) != nil)
-
 
2595
			return mkcont(mkDN(s), 4); /* DN */
-
 
2596
	}
-
 
2597
	e = mkstring(s, IA5String);
-
 
2598
	e.tag.class = Context;
-
 
2599
	e.tag.num = strchr(s, '@') != nil ? 1 : 2; /* email : DNS */
-
 
2600
	return e;
-
 
2601
}
-
 
2602
 
-
 
2603
static Elist*
-
 
2604
mkaltnames(char *alts)
-
 
2605
{
-
 
2606
	Elist *el;
2482
	uchar *key;
2607
	char *s, *p;
-
 
2608
 
-
 
2609
	if(alts == nil)
-
 
2610
		return nil;
2483
 
2611
 
2484
	key = nil;
2612
	el = nil;
-
 
2613
	alts = estrdup(alts);
-
 
2614
	for(s = alts; s != nil; s = p){
-
 
2615
		while(*s == ' ')
-
 
2616
			s++;
-
 
2617
		if(*s == '\0')
-
 
2618
			break;
-
 
2619
		if((p = strchr(s, ',')) != nil)
-
 
2620
			*p++ = 0;
2485
	pubkey = mkseq(mkel(mkbigint(pub->n),mkel(mkint(mptoi(pub->ek)),nil)));
2621
		el = mkel(mkaltname(s), el);
-
 
2622
	}
-
 
2623
	free(alts);
-
 
2624
	return el;
-
 
2625
}
-
 
2626
 
-
 
2627
static Elist*
-
 
2628
mkextel(Elem e, Ints *oid, Elist *el)
-
 
2629
{
-
 
2630
	Bytes *b = nil;
-
 
2631
 
2486
	if(encode(pubkey, &pkbytes) != ASN_OK)
2632
	if(encode(e, &b) == ASN_OK){
-
 
2633
		el = mkel(mkseq(
-
 
2634
			mkel(mkoid(oid),
-
 
2635
			mkel(mkoctet(b->data, b->len),
2487
		goto errret;
2636
			nil))), el);
-
 
2637
		freebytes(b);
-
 
2638
	}
2488
	freevalfields(&pubkey.val);
2639
	freevalfields(&e.val);
-
 
2640
	return el;
-
 
2641
}
-
 
2642
 
-
 
2643
static Ints15 oid_subjectAltName = {4, 2, 5, 29, 17 };
-
 
2644
static Ints15 oid_extensionRequest = { 7, 1, 2, 840, 113549, 1, 9, 14};
-
 
2645
 
-
 
2646
static Elist*
-
 
2647
mkextensions(char *alts, int req)
-
 
2648
{
-
 
2649
	Elist *sl, *xl;
-
 
2650
 
-
 
2651
	xl = nil;
-
 
2652
	if((sl = mkaltnames(alts)) != nil)
-
 
2653
		xl = mkextel(mkseq(sl), (Ints*)&oid_subjectAltName, xl);
-
 
2654
	if(xl != nil){
-
 
2655
		if(req) return mkel(mkcont(mkseq(
-
 
2656
			mkel(mkoid((Ints*)&oid_extensionRequest),
-
 
2657
			mkel(mkset(mkel(mkseq(xl), nil)), nil))), 0), nil);
-
 
2658
		return mkel(mkcont(mkseq(xl), 3), nil);
-
 
2659
	}
-
 
2660
	return nil;
-
 
2661
}
-
 
2662
 
-
 
2663
static char*
-
 
2664
splitalts(char *s)
-
 
2665
{
-
 
2666
	int q;
-
 
2667
 
-
 
2668
	for(q = 0; *s != '\0'; s++){
-
 
2669
		if(*s == '\'')
-
 
2670
			q ^= 1;
-
 
2671
		else if(q == 0 && *s == ','){
-
 
2672
			*s++ = 0;
-
 
2673
			return s;
-
 
2674
		}
-
 
2675
	}
-
 
2676
	return nil;
-
 
2677
}
-
 
2678
 
-
 
2679
static Bytes*
-
 
2680
encode_rsapubkey(RSApub *pk)
-
 
2681
{
-
 
2682
	Bytes *b = nil;
2489
	pubkey = mkseq(
2683
	Elem e = mkseq(
2490
		mkel(mkalg(ALG_rsaEncryption),
2684
		mkel(mkbigint(pk->n),
2491
		mkel(mkbits(pkbytes->data, pkbytes->len),
2685
		mkel(mpsignif(pk->ek)<32 ? mkint(mptoi(pk->ek)) : mkbigint(pk->ek),
2492
		nil)));
2686
		nil)));
-
 
2687
	encode(e, &b);
2493
	freebytes(pkbytes);
2688
	freevalfields(&e.val);
-
 
2689
	return b;
-
 
2690
}
-
 
2691
 
-
 
2692
int
2494
	if(encode(pubkey, &pkbytes) != ASN_OK)
2693
asn1encodeRSApub(RSApub *pk, uchar *buf, int len)
-
 
2694
{
-
 
2695
	Bytes *b = encode_rsapubkey(pk);
-
 
2696
	if(b == nil)
2495
		goto errret;
2697
		return -1;
2496
	if(keylen)
2698
	if(b->len > len){
2497
		*keylen = pkbytes->len;
2699
		freebytes(b);
2498
	key = malloc(pkbytes->len);
2700
		werrstr("buffer too small");
-
 
2701
		return -1;
-
 
2702
	}
2499
	memmove(key, pkbytes->data, pkbytes->len);
2703
	memmove(buf, b->data, len = b->len);
2500
	free(pkbytes);
2704
	freebytes(b);
2501
errret:
-
 
2502
	freevalfields(&pubkey.val);
-
 
2503
	return key;
2705
	return len;
2504
}
2706
}
2505
 
2707
 
2506
uchar*
2708
uchar*
2507
X509gen(RSApriv *priv, char *subj, ulong valid[2], int *certlen)
2709
X509rsagen(RSApriv *priv, char *subj, ulong valid[2], int *certlen)
2508
{
2710
{
2509
	int serial = 0;
2711
	int serial = 0, sigalg = ALG_sha256WithRSAEncryption;
2510
	uchar *cert = nil;
2712
	uchar *cert = nil;
2511
	RSApub *pk = rsaprivtopub(priv);
-
 
2512
	Bytes *certbytes, *pkbytes, *certinfobytes, *sigbytes;
2713
	Bytes *certbytes, *pkbytes, *certinfobytes, *sigbytes;
2513
	Elem e, certinfo, issuer, subject, pubkey, validity, sig;
2714
	Elem e, certinfo;
-
 
2715
	DigestAlg *da;
2514
	uchar digest[MD5dlen], *buf;
2716
	uchar digest[MAXdlen], *buf;
2515
	int buflen;
2717
	int buflen;
2516
	mpint *pkcs1;
2718
	mpint *pkcs1;
-
 
2719
	char *alts;
-
 
2720
 
-
 
2721
	if((pkbytes = encode_rsapubkey(&priv->pub)) == nil)
-
 
2722
		return nil;
-
 
2723
 
-
 
2724
	subj = estrdup(subj);
-
 
2725
	alts = splitalts(subj);
2517
 
2726
 
2518
	e.val.tag = VInt;  /* so freevalfields at errret is no-op */
-
 
2519
	issuer = mkDN(subj);
-
 
2520
	subject = mkDN(subj);
-
 
2521
	pubkey = mkseq(mkel(mkbigint(pk->n),mkel(mkint(mptoi(pk->ek)),nil)));
-
 
2522
	if(encode(pubkey, &pkbytes) != ASN_OK)
-
 
2523
		goto errret;
-
 
2524
	freevalfields(&pubkey.val);
-
 
2525
	pubkey = mkseq(
-
 
2526
		mkel(mkalg(ALG_rsaEncryption),
-
 
2527
		mkel(mkbits(pkbytes->data, pkbytes->len),
-
 
2528
		nil)));
-
 
2529
	freebytes(pkbytes);
-
 
2530
	validity = mkseq(
-
 
2531
		mkel(mkutc(valid[0]),
-
 
2532
		mkel(mkutc(valid[1]),
-
 
2533
		nil)));
-
 
2534
	certinfo = mkseq(
-
 
2535
		mkel(mkint(serial),
-
 
2536
		mkel(mkalg(ALG_md5WithRSAEncryption),
-
 
2537
		mkel(issuer,
-
 
2538
		mkel(validity,
-
 
2539
		mkel(subject,
-
 
2540
		mkel(pubkey,
-
 
2541
		nil)))))));
-
 
2542
	if(encode(certinfo, &certinfobytes) != ASN_OK)
-
 
2543
		goto errret;
-
 
2544
	md5(certinfobytes->data, certinfobytes->len, digest, 0);
-
 
2545
	freebytes(certinfobytes);
-
 
2546
	sig = mkseq(
-
 
2547
		mkel(mkalg(ALG_md5),
-
 
2548
		mkel(mkoctet(digest, MD5dlen),
-
 
2549
		nil)));
-
 
2550
	if(encode(sig, &sigbytes) != ASN_OK)
-
 
2551
		goto errret;
-
 
2552
	pkcs1 = pkcs1pad(sigbytes, pk->n);
-
 
2553
	freebytes(sigbytes);
-
 
2554
	rsadecrypt(priv, pkcs1, pkcs1);
-
 
2555
	buflen = mptobe(pkcs1, nil, 0, &buf);
-
 
2556
	mpfree(pkcs1);
-
 
2557
	e = mkseq(
2727
	e = mkseq(
-
 
2728
		mkel(mkcont(mkint(2), 0),
-
 
2729
		mkel(mkint(serial),
-
 
2730
		mkel(mkalg(sigalg),
-
 
2731
		mkel(mkDN(subj),
-
 
2732
		mkel(mkseq(
-
 
2733
			mkel(mkutc(valid[0]),
-
 
2734
			mkel(mkutc(valid[1]),
-
 
2735
			nil))),
-
 
2736
		mkel(mkDN(subj),
-
 
2737
		mkel(mkseq(
-
 
2738
			mkel(mkalg(ALG_rsaEncryption),
-
 
2739
			mkel(mkbits(pkbytes->data, pkbytes->len),
-
 
2740
			nil))),
-
 
2741
		mkextensions(alts, 0)))))))));
-
 
2742
	freebytes(pkbytes);
-
 
2743
	if(encode(e, &certinfobytes) != ASN_OK)
-
 
2744
		goto errret;
-
 
2745
 
-
 
2746
	da = digestalg[sigalg];
-
 
2747
	(*da->fun)(certinfobytes->data, certinfobytes->len, digest, 0);
-
 
2748
	freebytes(certinfobytes);
-
 
2749
	certinfo = e;
-
 
2750
 
-
 
2751
	sigbytes = encode_digest(da, digest);
-
 
2752
	if(sigbytes == nil)
-
 
2753
		goto errret;
-
 
2754
	pkcs1 = pkcs1padbuf(sigbytes->data, sigbytes->len, priv->pub.n, 1);
-
 
2755
	freebytes(sigbytes);
-
 
2756
	if(pkcs1 == nil)
-
 
2757
		goto errret;
-
 
2758
 
-
 
2759
	rsadecrypt(priv, pkcs1, pkcs1);
-
 
2760
	buflen = mptobe(pkcs1, nil, 0, &buf);
-
 
2761
	mpfree(pkcs1);
-
 
2762
	e = mkseq(
2558
		mkel(certinfo,
2763
		mkel(certinfo,
2559
		mkel(mkalg(ALG_md5WithRSAEncryption),
2764
		mkel(mkalg(sigalg),
2560
		mkel(mkbits(buf, buflen),
2765
		mkel(mkbits(buf, buflen),
2561
		nil))));
2766
		nil))));
2562
	free(buf);
2767
	free(buf);
2563
	if(encode(e, &certbytes) != ASN_OK)
2768
	if(encode(e, &certbytes) != ASN_OK)
2564
		goto errret;
2769
		goto errret;
2565
	if(certlen)
2770
	if(certlen != nil)
2566
		*certlen = certbytes->len;
2771
		*certlen = certbytes->len;
2567
	cert = certbytes->data;
2772
	cert = (uchar*)certbytes;
-
 
2773
	memmove(cert, certbytes->data, certbytes->len);
2568
errret:
2774
errret:
2569
	freevalfields(&e.val);
2775
	freevalfields(&e.val);
-
 
2776
	free(subj);
2570
	return cert;
2777
	return cert;
2571
}
2778
}
2572
 
2779
 
2573
uchar*
2780
uchar*
2574
X509req(RSApriv *priv, char *subj, int *certlen)
2781
X509rsareq(RSApriv *priv, char *subj, int *certlen)
2575
{
2782
{
2576
	/* RFC 2314, PKCS #10 Certification Request Syntax */
2783
	/* RFC 2314, PKCS #10 Certification Request Syntax */
2577
	int version = 0;
2784
	int version = 0, sigalg = ALG_sha256WithRSAEncryption;
2578
	uchar *cert = nil;
2785
	uchar *cert = nil;
2579
	RSApub *pk = rsaprivtopub(priv);
-
 
2580
	Bytes *certbytes, *pkbytes, *certinfobytes, *sigbytes;
2786
	Bytes *certbytes, *pkbytes, *certinfobytes, *sigbytes;
2581
	Elem e, certinfo, subject, pubkey, sig;
2787
	Elem e, certinfo;
-
 
2788
	DigestAlg *da;
2582
	uchar digest[MD5dlen], *buf;
2789
	uchar digest[MAXdlen], *buf;
2583
	int buflen;
2790
	int buflen;
2584
	mpint *pkcs1;
2791
	mpint *pkcs1;
-
 
2792
	char *alts;
2585
 
2793
 
2586
	e.val.tag = VInt;  /* so freevalfields at errret is no-op */
-
 
2587
	subject = mkDN(subj);
-
 
2588
	pubkey = mkseq(mkel(mkbigint(pk->n),mkel(mkint(mptoi(pk->ek)),nil)));
-
 
2589
	if(encode(pubkey, &pkbytes) != ASN_OK)
2794
	if((pkbytes = encode_rsapubkey(&priv->pub)) == nil)
2590
		goto errret;
2795
		return nil;
2591
	freevalfields(&pubkey.val);
-
 
-
 
2796
 
2592
	pubkey = mkseq(
2797
	subj = estrdup(subj);
2593
		mkel(mkalg(ALG_rsaEncryption),
-
 
2594
		mkel(mkbits(pkbytes->data, pkbytes->len),
2798
	alts = splitalts(subj);
2595
		nil)));
2799
 
2596
	freebytes(pkbytes);
-
 
2597
	certinfo = mkseq(
2800
	e = mkseq(
2598
		mkel(mkint(version),
2801
		mkel(mkint(version),
2599
		mkel(subject,
2802
		mkel(mkDN(subj),
2600
		mkel(pubkey,
2803
		mkel(mkseq(
-
 
2804
			mkel(mkalg(ALG_rsaEncryption),
-
 
2805
			mkel(mkbits(pkbytes->data, pkbytes->len),
2601
		nil))));
2806
			nil))),
-
 
2807
		mkextensions(alts, 1)))));
-
 
2808
	freebytes(pkbytes);
2602
	if(encode(certinfo, &certinfobytes) != ASN_OK)
2809
	if(encode(e, &certinfobytes) != ASN_OK)
2603
		goto errret;
2810
		goto errret;
-
 
2811
	da = digestalg[sigalg];
2604
	md5(certinfobytes->data, certinfobytes->len, digest, 0);
2812
	(*da->fun)(certinfobytes->data, certinfobytes->len, digest, 0);
2605
	freebytes(certinfobytes);
2813
	freebytes(certinfobytes);
2606
	sig = mkseq(
2814
	certinfo = e;
2607
		mkel(mkalg(ALG_md5),
-
 
-
 
2815
 
2608
		mkel(mkoctet(digest, MD5dlen),
2816
	sigbytes = encode_digest(da, digest);
2609
		nil)));
-
 
2610
	if(encode(sig, &sigbytes) != ASN_OK)
2817
	if(sigbytes == nil)
2611
		goto errret;
2818
		goto errret;
2612
	pkcs1 = pkcs1pad(sigbytes, pk->n);
2819
	pkcs1 = pkcs1padbuf(sigbytes->data, sigbytes->len, priv->pub.n, 1);
2613
	freebytes(sigbytes);
2820
	freebytes(sigbytes);
-
 
2821
	if(pkcs1 == nil)
-
 
2822
		goto errret;
-
 
2823
 
2614
	rsadecrypt(priv, pkcs1, pkcs1);
2824
	rsadecrypt(priv, pkcs1, pkcs1);
2615
	buflen = mptobe(pkcs1, nil, 0, &buf);
2825
	buflen = mptobe(pkcs1, nil, 0, &buf);
2616
	mpfree(pkcs1);
2826
	mpfree(pkcs1);
2617
	e = mkseq(
2827
	e = mkseq(
2618
		mkel(certinfo,
2828
		mkel(certinfo,
2619
		mkel(mkalg(ALG_md5),
2829
		mkel(mkalg(sigalg),
2620
		mkel(mkbits(buf, buflen),
2830
		mkel(mkbits(buf, buflen),
2621
		nil))));
2831
		nil))));
2622
	free(buf);
2832
	free(buf);
2623
	if(encode(e, &certbytes) != ASN_OK)
2833
	if(encode(e, &certbytes) != ASN_OK)
2624
		goto errret;
2834
		goto errret;
2625
	if(certlen)
2835
	if(certlen != nil)
2626
		*certlen = certbytes->len;
2836
		*certlen = certbytes->len;
2627
	cert = certbytes->data;
2837
	cert = (uchar*)certbytes;
-
 
2838
	memmove(cert, certbytes->data, certbytes->len);
2628
errret:
2839
errret:
2629
	freevalfields(&e.val);
2840
	freevalfields(&e.val);
-
 
2841
	free(subj);
2630
	return cert;
2842
	return cert;
-
 
2843
}
-
 
2844
 
-
 
2845
static void
-
 
2846
digestSPKI(int alg, uchar *pubkey, int npubkey, DigestState* (*fun)(uchar*, ulong, uchar*, DigestState*), uchar *digest)
-
 
2847
{
-
 
2848
	Bytes *b = nil;
-
 
2849
	Elem e = mkseq(mkel(mkalg(alg), mkel(mkbits(pubkey, npubkey), nil)));
-
 
2850
	encode(e, &b);
-
 
2851
	freevalfields(&e.val);
-
 
2852
	(*fun)(b->data, b->len, digest, nil);
-
 
2853
	freebytes(b);
-
 
2854
}
-
 
2855
 
-
 
2856
int
-
 
2857
X509digestSPKI(uchar *cert, int ncert, DigestState* (*fun)(uchar*, ulong, uchar*, DigestState*), uchar *digest)
-
 
2858
{
-
 
2859
	CertX509 *c;
-
 
2860
 
-
 
2861
	c = decode_cert(cert, ncert);
-
 
2862
	if(c == nil){
-
 
2863
		werrstr("cannot decode cert");
-
 
2864
		return -1;
-
 
2865
	}
-
 
2866
	digestSPKI(c->publickey_alg, c->publickey->data, c->publickey->len, fun, digest);
-
 
2867
	freecert(c);
-
 
2868
	return 0;
2631
}
2869
}
2632
 
2870
 
2633
static char*
2871
static char*
2634
tagdump(Tag tag)
2872
tagdump(Tag tag)
2635
{
2873
{
-
 
2874
	static char buf[32];
-
 
2875
 
2636
	if(tag.class != Universal)
2876
	if(tag.class != Universal){
2637
		return smprint("class%d,num%d", tag.class, tag.num);
2877
		snprint(buf, sizeof(buf), "class%d,num%d", tag.class, tag.num);
-
 
2878
		return buf;
-
 
2879
	}
2638
	switch(tag.num){
2880
	switch(tag.num){
2639
	case BOOLEAN: return "BOOLEAN";
2881
	case BOOLEAN: return "BOOLEAN";
2640
	case INTEGER: return "INTEGER";
2882
	case INTEGER: return "INTEGER";
2641
	case BIT_STRING: return "BIT STRING";
2883
	case BIT_STRING: return "BIT STRING";
2642
	case OCTET_STRING: return "OCTET STRING";
2884
	case OCTET_STRING: return "OCTET STRING";
Line 2661... Line 2903...
2661
	case VisibleString: return "VisibleString";
2903
	case VisibleString: return "VisibleString";
2662
	case GeneralString: return "GeneralString";
2904
	case GeneralString: return "GeneralString";
2663
	case UniversalString: return "UniversalString";
2905
	case UniversalString: return "UniversalString";
2664
	case BMPString: return "BMPString";
2906
	case BMPString: return "BMPString";
2665
	default:
2907
	default:
2666
		return smprint("Universal,num%d", tag.num);
2908
		snprint(buf, sizeof(buf), "Universal,num%d", tag.num);
-
 
2909
		return buf;
2667
	}
2910
	}
2668
}
2911
}
2669
 
2912
 
2670
static void
2913
static void
2671
edump(Elem e)
2914
edump(Elem e)
Line 2681... Line 2924...
2681
	case VInt: print("Int %d",v.u.intval); break;
2924
	case VInt: print("Int %d",v.u.intval); break;
2682
	case VOctets: print("Octets[%d] %.2x%.2x...",v.u.octetsval->len,v.u.octetsval->data[0],v.u.octetsval->data[1]); break;
2925
	case VOctets: print("Octets[%d] %.2x%.2x...",v.u.octetsval->len,v.u.octetsval->data[0],v.u.octetsval->data[1]); break;
2683
	case VBigInt: print("BigInt[%d] %.2x%.2x...",v.u.bigintval->len,v.u.bigintval->data[0],v.u.bigintval->data[1]); break;
2926
	case VBigInt: print("BigInt[%d] %.2x%.2x...",v.u.bigintval->len,v.u.bigintval->data[0],v.u.bigintval->data[1]); break;
2684
	case VReal: print("Real..."); break;
2927
	case VReal: print("Real..."); break;
2685
	case VOther: print("Other..."); break;
2928
	case VOther: print("Other..."); break;
2686
	case VBitString: print("BitString");
-
 
2687
		for(i = 0; i<v.u.bitstringval->len; i++)
-
 
2688
			print(" %02x", v.u.bitstringval->data[i]);
2929
	case VBitString: print("BitString[%d]...", v.u.bitstringval->len*8 - v.u.bitstringval->unusedbits); break;
2689
		break;
-
 
2690
	case VNull: print("Null"); break;
2930
	case VNull: print("Null"); break;
2691
	case VEOC: print("EOC..."); break;
2931
	case VEOC: print("EOC..."); break;
2692
	case VObjId: print("ObjId");
2932
	case VObjId: print("ObjId");
2693
		for(i = 0; i<v.u.objidval->len; i++)
2933
		for(i = 0; i<v.u.objidval->len; i++)
2694
			print(" %d", v.u.objidval->data[i]);
2934
			print(" %d", v.u.objidval->data[i]);
Line 2720... Line 2960...
2720
 
2960
 
2721
void
2961
void
2722
X509dump(uchar *cert, int ncert)
2962
X509dump(uchar *cert, int ncert)
2723
{
2963
{
2724
	char *e;
2964
	char *e;
2725
	Bytes *b;
-
 
2726
	CertX509 *c;
2965
	CertX509 *c;
2727
	RSApub *pk;
2966
	RSApub *rsapub;
-
 
2967
	ECpub *ecpub;
-
 
2968
	ECdomain ecdom;
2728
	uchar digest[SHA1dlen];
2969
	int digestlen;
2729
	Elem *sigalg;
2970
	uchar digest[MAXdlen];
2730
 
2971
 
2731
	print("begin X509dump\n");
2972
	print("begin X509dump\n");
2732
	b = makebytes(cert, ncert);
2973
	c = decode_cert(cert, ncert);
2733
	c = decode_cert(b);
-
 
2734
	if(c != nil)
-
 
2735
		digest_certinfo(b, digestalg[c->signature_alg], digest);
-
 
2736
	freebytes(b);
-
 
2737
	if(c == nil){
2974
	if(c == nil){
2738
		print("cannot decode cert");
2975
		print("cannot decode cert\n");
-
 
2976
		return;
-
 
2977
	}
-
 
2978
 
-
 
2979
	digestlen = digest_certinfo(cert, ncert, digestalg[c->signature_alg], digest);
-
 
2980
	if(digestlen <= 0){
-
 
2981
		freecert(c);
-
 
2982
		print("cannot decode certinfo\n");
2739
		return;
2983
		return;
2740
	}
2984
	}
2741
 
2985
 
2742
	print("serial %d\n", c->serial);
2986
	print("serial %d\n", c->serial);
2743
	print("issuer %s\n", c->issuer);
2987
	print("issuer %s\n", c->issuer);
2744
	print("validity %s %s\n", c->validity_start, c->validity_end);
2988
	print("validity %s %s\n", c->validity_start, c->validity_end);
2745
	print("subject %s\n", c->subject);
2989
	print("subject %s\n", c->subject);
2746
	pk = decode_rsapubkey(c->publickey);
2990
	print("sigalg=%d digest=%.*H\n", c->signature_alg, digestlen, digest);
2747
	print("pubkey e=%B n(%d)=%B\n", pk->ek, mpsignif(pk->n), pk->n);
2991
	print("publickey_alg=%d pubkey[%d] %.*H\n", c->publickey_alg, c->publickey->len,
-
 
2992
		c->publickey->len, c->publickey->data);
2748
 
2993
 
-
 
2994
	switch(c->publickey_alg){
-
 
2995
	case ALG_rsaEncryption:
-
 
2996
		rsapub = asn1toRSApub(c->publickey->data, c->publickey->len);
-
 
2997
		if(rsapub != nil){
2749
	print("sigalg=%d digest=%.*H\n", c->signature_alg, MD5dlen, digest);
2998
			print("rsa pubkey e=%B n(%d)=%B\n", rsapub->ek, mpsignif(rsapub->n), rsapub->n);
2750
	e = verify_signature(c->signature, pk, digest, &sigalg);
2999
			e = X509rsaverifydigest(c->signature->data, c->signature->len,
-
 
3000
				digest, digestlen, rsapub);
2751
	if(e==nil){
3001
			if(e==nil)
2752
		e = "nil (meaning ok)";
3002
				e = "nil (meaning ok)";
-
 
3003
			print("self-signed X509rsaverifydigest returns: %s\n", e);
-
 
3004
			rsapubfree(rsapub);
-
 
3005
		}
-
 
3006
		break;
-
 
3007
	case ALG_ecPublicKey:
-
 
3008
		ecdominit(&ecdom, namedcurves[c->curve]);
-
 
3009
		ecpub = ecdecodepub(&ecdom, c->publickey->data, c->publickey->len);
2753
		print("sigalg=\n");
3010
		if(ecpub != nil){
-
 
3011
			e = X509ecdsaverifydigest(c->signature->data, c->signature->len,
-
 
3012
				digest, digestlen, &ecdom, ecpub);
2754
		if(sigalg)
3013
			if(e==nil)
-
 
3014
				e = "nil (meaning ok)";
-
 
3015
			print("self-signed X509ecdsaverifydigest returns: %s\n", e);
2755
			edump(*sigalg);
3016
			ecpubfree(ecpub);
-
 
3017
		}
-
 
3018
		ecdomfree(&ecdom);
-
 
3019
		break;
2756
	}
3020
	}
2757
	print("self-signed verify_signature returns: %s\n", e);
-
 
2758
 
3021
 
-
 
3022
	digestSPKI(c->publickey_alg, c->publickey->data, c->publickey->len, sha2_256, digest);
-
 
3023
	print("publickey_thumbprint sha256=%.*[\n", SHA2_256dlen, digest);
-
 
3024
 
-
 
3025
	sha2_256(cert, ncert, digest, nil);
-
 
3026
	print("cert_thumbprint sha256=%.*[\n", SHA2_256dlen, digest);
-
 
3027
 
2759
	rsapubfree(pk);
3028
	sha1(cert, ncert, digest, nil);
-
 
3029
	print("cert_thumbprint sha1=%.*H\n", SHA1dlen, digest);
-
 
3030
 
2760
	freecert(c);
3031
	freecert(c);
2761
	print("end X509dump\n");
3032
	print("end X509dump\n");
2762
}
3033
}