xref: /inferno-os/module/spki.m (revision d6b4eae8eb0a5ca3119414005e483fedd63a62d6)
146439007SCharles.ForsythRawsexprs: module
246439007SCharles.Forsyth{
346439007SCharles.Forsyth	PATH:	con "rawsexprs.dis";
446439007SCharles.Forsyth
546439007SCharles.Forsyth	Sexp: adt {
646439007SCharles.Forsyth		pick {
746439007SCharles.Forsyth		String =>
846439007SCharles.Forsyth			s: string;
946439007SCharles.Forsyth			hint:	string;
1046439007SCharles.Forsyth		Binary =>
1146439007SCharles.Forsyth			data:	array of byte;
1246439007SCharles.Forsyth			hint: string;
1346439007SCharles.Forsyth		List =>
1446439007SCharles.Forsyth			l:	cyclic list of ref Sexp;
1546439007SCharles.Forsyth		}
1646439007SCharles.Forsyth
1746439007SCharles.Forsyth		unpack:	fn(a: array of byte): (ref Sexp, array of byte, string);
1846439007SCharles.Forsyth		text:	fn(e: self ref Sexp): string;
1946439007SCharles.Forsyth		packedsize:	fn(e: self ref Sexp): int;
2046439007SCharles.Forsyth		pack:	fn(e: self ref Sexp): array of byte;
2146439007SCharles.Forsyth	};
2246439007SCharles.Forsyth
2346439007SCharles.Forsyth	init:	fn();
2446439007SCharles.Forsyth};
2546439007SCharles.Forsyth
2646439007SCharles.ForsythSPKI: module
2746439007SCharles.Forsyth{
2846439007SCharles.Forsyth	PATH: con "/dis/lib/spki/spki.dis";
2946439007SCharles.Forsyth
3046439007SCharles.Forsyth	Hash: adt {
3146439007SCharles.Forsyth		alg:	string;
3246439007SCharles.Forsyth		hash:	array of byte;
3346439007SCharles.Forsyth
3446439007SCharles.Forsyth		sexp:	fn(h: self ref Hash): ref Sexprs->Sexp;
3546439007SCharles.Forsyth		text:	fn(h: self ref Hash): string;
3646439007SCharles.Forsyth		eq:	fn(h1: self ref Hash, h2: ref Hash): int;
3746439007SCharles.Forsyth	};
3846439007SCharles.Forsyth
3946439007SCharles.Forsyth	Key: adt {
40*d6b4eae8Sforsyth		pk:	ref Keyring->PK;	# either pk/sk or hash might be nil
41*d6b4eae8Sforsyth		sk:	ref Keyring->SK;
4246439007SCharles.Forsyth		nbits:	int;
437a5ff069SCharles.Forsyth		halg:	string;	# basic signature hash algorithm
447a5ff069SCharles.Forsyth		henc:	string;	# pre-signature encoding
457a5ff069SCharles.Forsyth		hash:	list of ref Hash;
4646439007SCharles.Forsyth
4746439007SCharles.Forsyth		hashed:	fn(k: self ref Key, alg: string): array of byte;
487a5ff069SCharles.Forsyth		hashexp:	fn(k: self ref Key, alg: string): ref Hash;
497a5ff069SCharles.Forsyth		ishash:	fn(k: self ref Key): int;
507a5ff069SCharles.Forsyth		public:	fn(k: self ref Key): ref Key;
5146439007SCharles.Forsyth		sigalg:	fn(k: self ref Key): string;
5246439007SCharles.Forsyth		text:	fn(k: self ref Key): string;
5346439007SCharles.Forsyth		sexp:	fn(k: self ref Key): ref Sexprs->Sexp;
5446439007SCharles.Forsyth		eq:	fn(k1: self ref Key, k2: ref Key): int;
5546439007SCharles.Forsyth	};
5646439007SCharles.Forsyth
5746439007SCharles.Forsyth	Name: adt {
5846439007SCharles.Forsyth		principal:	ref Key;
5946439007SCharles.Forsyth		names:	list of string;
6046439007SCharles.Forsyth
6146439007SCharles.Forsyth		isprincipal:	fn(n: self ref Name): int;
6246439007SCharles.Forsyth		local:	fn(n: self ref Name): ref Name;
6346439007SCharles.Forsyth		islocal:	fn(n: self ref Name): int;
6446439007SCharles.Forsyth		isprefix:	fn(n1: self ref Name, n2: ref Name): int;
6546439007SCharles.Forsyth		text:	fn(n: self ref Name): string;
6646439007SCharles.Forsyth		sexp:	fn(n: self ref Name): ref Sexprs->Sexp;
6746439007SCharles.Forsyth		eq:	fn(n1: self ref Name, n2: ref Name): int;
6846439007SCharles.Forsyth	};
6946439007SCharles.Forsyth
7046439007SCharles.Forsyth	Cert: adt {
7146439007SCharles.Forsyth		e:	ref Sexprs->Sexp;	# S-expression, if originally parsed
7246439007SCharles.Forsyth		issuer:	ref Name;
7346439007SCharles.Forsyth		subject:	ref Subject;
7446439007SCharles.Forsyth		valid:	ref Valid;
7546439007SCharles.Forsyth		pick {
7646439007SCharles.Forsyth		A or KH or O =>	# auth, keyholder or object
7746439007SCharles.Forsyth			delegate:	int;
7846439007SCharles.Forsyth			tag:	ref Sexprs->Sexp;
7946439007SCharles.Forsyth		N =>	# name
8046439007SCharles.Forsyth		}
8146439007SCharles.Forsyth
8246439007SCharles.Forsyth		text:	fn(c: self ref Cert): string;
8346439007SCharles.Forsyth		sexp:	fn(c: self ref Cert): ref Sexprs->Sexp;
8446439007SCharles.Forsyth	};
8546439007SCharles.Forsyth
8646439007SCharles.Forsyth	# the pick might move to a more general `Principal' structure,
8746439007SCharles.Forsyth	# allowing compound and quoting principals
8846439007SCharles.Forsyth	Subject: adt {
8946439007SCharles.Forsyth		pick{
9046439007SCharles.Forsyth		P =>
9146439007SCharles.Forsyth			key:	ref Key;
9246439007SCharles.Forsyth		N =>
9346439007SCharles.Forsyth			name:	ref Name;
9446439007SCharles.Forsyth		O =>
9546439007SCharles.Forsyth			hash:	ref Hash;
9646439007SCharles.Forsyth		KH =>
9746439007SCharles.Forsyth			holder:	ref Name;
9846439007SCharles.Forsyth		T =>
9946439007SCharles.Forsyth			k, n:	int;
10046439007SCharles.Forsyth			subs:	cyclic list of ref Subject;
10146439007SCharles.Forsyth		}
10246439007SCharles.Forsyth
10346439007SCharles.Forsyth		eq:	fn(s1: self ref Subject, s2: ref Subject): int;
10446439007SCharles.Forsyth		principal:	fn(s: self ref Subject): ref Key;
10546439007SCharles.Forsyth		text:	fn(s: self ref Subject): string;
10646439007SCharles.Forsyth		sexp:	fn(s: self ref Subject): ref Sexprs->Sexp;
10746439007SCharles.Forsyth	};
10846439007SCharles.Forsyth
10946439007SCharles.Forsyth	Principal: adt[T] {
11046439007SCharles.Forsyth		pick{
11146439007SCharles.Forsyth		N =>
11246439007SCharles.Forsyth			name:	ref Name;
11346439007SCharles.Forsyth		Q =>
11446439007SCharles.Forsyth			quoter:	T;
11546439007SCharles.Forsyth			quotes:	cyclic ref Principal;
11646439007SCharles.Forsyth		}
11746439007SCharles.Forsyth	};
11846439007SCharles.Forsyth
11946439007SCharles.Forsyth	Signature: adt {
12046439007SCharles.Forsyth		hash:	ref Hash;
12146439007SCharles.Forsyth		key:	ref Key;	# find by hash if necessary
1227a5ff069SCharles.Forsyth		sa:	string;	# alg[-[encoding-]hash]
123*d6b4eae8Sforsyth		sig:	list of (string, array of byte);
12446439007SCharles.Forsyth
12546439007SCharles.Forsyth		algs:	fn(s: self ref Signature): (string, string, string);
12646439007SCharles.Forsyth		sexp:	fn(s: self ref Signature): ref Sexprs->Sexp;
12746439007SCharles.Forsyth		text:	fn(s: self ref Signature): string;
12846439007SCharles.Forsyth	};
12946439007SCharles.Forsyth
13046439007SCharles.Forsyth	Seqel: adt {
13146439007SCharles.Forsyth		pick{
13246439007SCharles.Forsyth		C =>
13346439007SCharles.Forsyth			c: ref Cert;
13446439007SCharles.Forsyth		K =>
13546439007SCharles.Forsyth			k: ref Key;
13646439007SCharles.Forsyth		O =>
13746439007SCharles.Forsyth			op: string;
13846439007SCharles.Forsyth			args: list of ref Sexprs->Sexp;
13946439007SCharles.Forsyth		S =>
14046439007SCharles.Forsyth			sig: ref Signature;
14146439007SCharles.Forsyth		RV =>	# <reval>
14246439007SCharles.Forsyth			ok:	list of (string, string);
14346439007SCharles.Forsyth			onetime:	array of byte;
14446439007SCharles.Forsyth			valid:	ref Valid;
14546439007SCharles.Forsyth		CRL =>
14646439007SCharles.Forsyth			bad:	list of (string, string);
14746439007SCharles.Forsyth			valid:	ref Valid;
14846439007SCharles.Forsyth		Delta =>
14946439007SCharles.Forsyth			hash:	string;
15046439007SCharles.Forsyth			bad:	list of (string, string);
15146439007SCharles.Forsyth			valid:	ref Valid;
15246439007SCharles.Forsyth		E =>
15346439007SCharles.Forsyth			exp:	ref Sexprs->Sexp;
15446439007SCharles.Forsyth		}
15546439007SCharles.Forsyth
1567a5ff069SCharles.Forsyth		sexp:	fn(se: self ref Seqel): ref Sexprs->Sexp;
15746439007SCharles.Forsyth		text:	fn(se: self ref Seqel): string;
15846439007SCharles.Forsyth	};
15946439007SCharles.Forsyth
16046439007SCharles.Forsyth	Valid: adt {
16146439007SCharles.Forsyth		notbefore:	string;
16246439007SCharles.Forsyth		notafter:	string;
16346439007SCharles.Forsyth
16446439007SCharles.Forsyth		intersect:	fn(a: self Valid, b: Valid): (int, Valid);
16546439007SCharles.Forsyth		text:	fn(a: self Valid): string;
16646439007SCharles.Forsyth		sexp:	fn(a: self Valid): ref Sexprs->Sexp;
16746439007SCharles.Forsyth	};
16846439007SCharles.Forsyth
16946439007SCharles.Forsyth	Toplev: adt {
17046439007SCharles.Forsyth		pick {
17146439007SCharles.Forsyth		C =>
17246439007SCharles.Forsyth			v:	ref Cert;
17346439007SCharles.Forsyth		Sig =>
17446439007SCharles.Forsyth			v:	ref Signature;
17546439007SCharles.Forsyth		K =>
17646439007SCharles.Forsyth			v:	ref Key;
17746439007SCharles.Forsyth		Seq =>
17846439007SCharles.Forsyth			v:	list of ref Seqel;
17946439007SCharles.Forsyth		}
1807a5ff069SCharles.Forsyth
1817a5ff069SCharles.Forsyth		sexp:	fn(t: self ref Toplev): ref Sexprs->Sexp;
1827a5ff069SCharles.Forsyth		text:	fn(t: self ref Toplev): string;
18346439007SCharles.Forsyth	};
18446439007SCharles.Forsyth
18546439007SCharles.Forsyth	init:	fn();
18646439007SCharles.Forsyth
18746439007SCharles.Forsyth	# parse structures
18846439007SCharles.Forsyth	parse:	fn(s: ref Sexprs->Sexp): (ref Toplev, string);
18946439007SCharles.Forsyth	parseseq:	fn(s: ref Sexprs->Sexp): list of ref Seqel;
19046439007SCharles.Forsyth	parsecert:	fn(s: ref Sexprs->Sexp): ref Cert;
19146439007SCharles.Forsyth	parsesig:	fn(s: ref Sexprs->Sexp): ref Signature;
19246439007SCharles.Forsyth	parsename:	fn(s: ref Sexprs->Sexp): ref Name;
19346439007SCharles.Forsyth	parsekey:	fn(s: ref Sexprs->Sexp): ref Key;
19446439007SCharles.Forsyth	parsehash:	fn(s: ref Sexprs->Sexp): ref Hash;
19546439007SCharles.Forsyth	parsecompound:	fn(s: ref Sexprs->Sexp): ref Name;
19646439007SCharles.Forsyth	parsevalid:	fn(s: ref Sexprs->Sexp): ref Valid;
19746439007SCharles.Forsyth
19846439007SCharles.Forsyth	# signature checking
19946439007SCharles.Forsyth	checksig:	fn(c: ref Cert, sig: ref Signature): string;
200*d6b4eae8Sforsyth	sig2icert:	fn(sig: ref Signature, signer: string, exp: int): ref Keyring->Certificate;
20146439007SCharles.Forsyth
2027a5ff069SCharles.Forsyth	# signature making
2037a5ff069SCharles.Forsyth	signcert:	fn(c: ref Cert, sigalg: string, key: ref Key): (ref Signature, string);
2047a5ff069SCharles.Forsyth	signbytes:	fn(a: array of byte, sigalg: string, key: ref Key): (ref Signature, string);
2057a5ff069SCharles.Forsyth
20646439007SCharles.Forsyth	# tags
20746439007SCharles.Forsyth	maketag:	fn(e: ref Sexprs->Sexp): ref Sexprs->Sexp;
20846439007SCharles.Forsyth	tagintersect:	fn(t1: ref Sexprs->Sexp, t2: ref Sexprs->Sexp): ref Sexprs->Sexp;
20946439007SCharles.Forsyth	tagimplies:	fn(t1: ref Sexprs->Sexp, t2: ref Sexprs->Sexp): int;
21046439007SCharles.Forsyth
21146439007SCharles.Forsyth	# hash canonical s-expression
21246439007SCharles.Forsyth	hashbytes:	fn(a: array of byte, alg: string): array of byte;
21346439007SCharles.Forsyth	hashexp:	fn(e: ref Sexprs->Sexp, alg: string): array of byte;
21446439007SCharles.Forsyth
21546439007SCharles.Forsyth	# convert between date and time strings and Inferno form
21646439007SCharles.Forsyth	date2epoch:	fn(s: string): int;	# YYYY-MM-DD_HH:MM:SS
21746439007SCharles.Forsyth	epoch2date:	fn(t: int): string;
21846439007SCharles.Forsyth	time2secs:	fn(s: string): int;	# HH:MM:SS
21946439007SCharles.Forsyth	secs2time:	fn(t: int): string;
22046439007SCharles.Forsyth
2217a5ff069SCharles.Forsyth	# misc
2227a5ff069SCharles.Forsyth	sigalgs:	fn(algs: string): (string, string, string);
2237a5ff069SCharles.Forsyth
22446439007SCharles.Forsyth	# debugging
22546439007SCharles.Forsyth	dump:	fn(s: string, a: array of byte);
22646439007SCharles.Forsyth};
22746439007SCharles.Forsyth
22846439007SCharles.ForsythProofs: module
22946439007SCharles.Forsyth{
23046439007SCharles.Forsyth	Proof: adt {
23146439007SCharles.Forsyth		n:	int;
23246439007SCharles.Forsyth
23346439007SCharles.Forsyth		parse:	fn(s: string): ref Proof;
23446439007SCharles.Forsyth		sexp:	fn(p: self ref Proof): ref Sexprs->Sexp;
23546439007SCharles.Forsyth		text:	fn(p: self ref Proof): string;
23646439007SCharles.Forsyth	};
23746439007SCharles.Forsyth
23846439007SCharles.Forsyth	init:	fn(): string;
23946439007SCharles.Forsyth};
24046439007SCharles.Forsyth
24146439007SCharles.ForsythVerifier: module
24246439007SCharles.Forsyth{
24346439007SCharles.Forsyth	PATH:	con "/dis/lib/spki/verifier.dis";
24446439007SCharles.Forsyth
24546439007SCharles.Forsyth	Speaksfor: adt {
24646439007SCharles.Forsyth		subject:	ref SPKI->Subject;
24746439007SCharles.Forsyth		name:	ref SPKI->Name;
24846439007SCharles.Forsyth		regarding:	ref Sexprs->Sexp;
24946439007SCharles.Forsyth		valid:	ref SPKI->Valid;
25046439007SCharles.Forsyth	};
25146439007SCharles.Forsyth
25246439007SCharles.Forsyth	init:	fn();
25346439007SCharles.Forsyth	verify:	fn(seq: list of ref SPKI->Seqel): (ref Speaksfor, list of ref SPKI->Seqel, string);
25446439007SCharles.Forsyth};
255