summaryrefslogtreecommitdiff
path: root/appl/lib/spki/verifier.b
blob: d712fd2ac24d570b103963aacb756244e6431962 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
implement Verifier;

#
# Copyright © 2004 Vita Nuova Holdings Limited
#

include "sys.m";
	sys: Sys;

include "keyring.m";
	kr: Keyring;
	IPint: import kr;

include "bufio.m";
	bufio: Bufio;
	Iobuf: import bufio;

include "sexprs.m";
	sexprs: Sexprs;
	Sexp: import sexprs;

include "spki.m";
	spki: SPKI;
	Hash, Key, Cert, Name, Subject, Signature, Seqel, Toplev, Valid: import spki;
	dump: import spki;

include "encoding.m";
	base64: Encoding;

debug := 0;

init()
{
	sys = load Sys Sys->PATH;
	kr = load Keyring Keyring->PATH;
	bufio = load Bufio Bufio->PATH;
	sexprs = load Sexprs Sexprs->PATH;
	spki = load SPKI SPKI->PATH;
	base64 = load Encoding Encoding->BASE64PATH;

	sexprs->init();
	spki->init();
}

putkey(keys: list of ref Key, k: ref Key): list of ref Key
{
	for(kl := keys; kl != nil; kl = tl kl)
		if(k.eq(hd kl))
			return keys;
	return k :: keys;
}

keybyhash(h: ref Hash, keys: list of ref Key): ref Key
{
	for(kl := keys; kl != nil; kl = tl kl){
		k := hd kl;
		if(k.hash != nil && h.eq(k.hash))
			return k;
	}
	return nil;
}

verify(seq: list of ref Seqel): (ref Speaksfor, list of ref Seqel, string)
{
	stack: list of ref Seqel;
	keys: list of ref Key;
	n0: ref Name;
	cn: ref Cert;
	delegate := 1;
	tag: ref Sexp;
	val: ref Valid;
	for(; seq != nil; seq = tl seq){
		pick s := hd seq {
		C =>
			diag := checkcert(s.c);
			if(diag != nil)
				return (nil, seq, diag);
			if(stack != nil){
				pick h := hd stack {
				C =>
					if(!delegate)
						return(nil, seq, "previous auth certificate did not delegate");
					if(!h.c.subject.principal().eq(s.c.issuer.principal))
						return (nil, seq, "certificate chain has mismatched principals");
					if(debug)
						sys->print("issuer %s ok\n", s.c.issuer.principal.text());
				}
				stack = tl stack;
			}
			stack = s :: stack;
			if(n0 == nil)
				n0 = s.c.issuer;
			cn = s.c;
			pick t := s.c {
			A or KH or O =>
				delegate = t.delegate;
				if(tag != nil){
					tag = spki->tagintersect(tag, t.tag);
					if(tag == nil)
						return (nil, seq, "certificate chain has null authority");
				}else
					tag = t.tag;
				if(val != nil){
					if(t.valid != nil){
						(ok, iv) := (*val).intersect(*t.valid);
						if(!ok)
							return (nil, seq, "certificate chain is not currently valid");
						*val = iv;
					}
				}else
					val = t.valid;
			}
		K =>
			stack = s :: stack;
		O =>
			if(s.op == "debug"){
				debug = !debug;
				continue;
			}
			if(s.op != "hash" || s.args == nil || tl s.args != nil)
				return (nil, seq, "invalid operation to `do'");
			alg := (hd s.args).astext();
			if(alg != "md5" && alg != "sha1")
				return (nil, seq, "invalid hash operation");
			if(stack == nil)
				return (nil, seq, "verification stack empty");
			pick h := hd stack {
			K =>
				a := h.k.hashed(alg);
				if(debug)
					dump("do hash", a);
				keys = putkey(keys, h.k);
				stack = tl stack;
			C =>
				;
			* =>
				return (nil, seq, "invalid type of operand for hash");
			}
		S =>
			if(stack == nil)
				return (nil, seq, "verification stack empty");
			sig := s.sig;
			if(sig.key == nil)
				return (nil, seq, "neither hash nor key for signature");
			if(sig.key.pk == nil){
				k := keybyhash(sig.key.hash, keys);
				if(k == nil)
					return (nil, seq, "unknown key for signature");
				sig.key = k;
			}
			pick c := hd stack {
			C =>
				if(c.c.e == nil)
					return (nil, seq, "missing canonical expression for cert");
				a := c.c.e.pack();
				# verify signature ...
				if(debug)
					dump("cert a", a);
				h := spki->hashbytes(a, "md5");
				if(debug){
					dump("hash cert", h);
					sys->print("hash = %q\n", base64->enc(h));
				}
				failed := spki->checksig(c.c, sig);
				if(debug)
					sys->print("checksig: %q\n", failed);
				if(failed != nil)
					return (nil, seq, "signature verification failed: "+failed);
			* =>
				return (nil, seq, "invalid type of signature operand");
			}
		}
	}
	if(n0 != nil && cn != nil){
		if(debug){
			if(tag != nil)
				auth := sys->sprint(" regarding %q", tag.text());
			sys->print("%q speaks for %q%s\n", cn.subject.text(), n0.text(), auth);
		}
		return (ref Speaksfor(cn.subject, n0, tag, val), nil, nil);
	}
	return (nil, nil, nil);
}

checkcert(c: ref Cert): string
{
	return nil;
}