Plan 9 from Bell Labs’s /sources/contrib/mospak/tls-1.2/libsec-x509-chain-hostname.diff

Copyright © 2026 Plan 9 Foundation.
Distributed under the MIT License.
Download the Plan 9 distribution.


--- sys/include/libsec.h
+++ sys/include/libsec.h
@@ -402,6 +402,9 @@ char*		X509ecdsaverifydigest(uchar *sig, int siglen, u
 char*		rsapkcs1verify(RSApub *pk, int sigalg, uchar *msg, ulong msglen, uchar *sig, int siglen);
 ECpub*		X509toECpub(uchar *cert, int ncert, char *name, int nname, ECdomain *dom);
 char*		X509ecdsaverifydigest(uchar *sig, int siglen, uchar *edigest, int edigestlen, ECdomain *dom, ECpub *pub);
+char*		X509ecdsaverify(uchar *cert, int ncert, ECdomain *dom, ECpub *pub);
+char*		X509matchhostname(uchar *cert, int ncert, char *hostname);
+char*		X509verifychain(PEMChain *chain, PEMChain *roots, char *hostname);
 
 /*
  * elgamal
--- sys/src/libsec/port/x509.c
+++ sys/src/libsec/port/x509.c
@@ -99,6 +99,8 @@ struct Elem {
 struct Elem {
 	Tag	tag;
 	Value	val;
+	uchar*	raw;	/* the encoding this was decoded from ... */
+	int	rawlen;	/* ... so names can be compared as DER */
 };
 
 struct Elist {
@@ -256,7 +258,9 @@ ber_decode(uchar** pp, uchar* pend, Elem* pelem)
 	int length;
 	Tag tag;
 	Value val;
+	uchar* start;
 
+	start = *pp;
 	err = tag_decode(pp, pend, &tag, &isconstr);
 	if(err == ASN_OK) {
 		err = length_decode(pp, pend, &length);
@@ -270,6 +274,8 @@ ber_decode(uchar** pp, uchar* pend, Elem* pelem)
 			if(err == ASN_OK) {
 				pelem->tag = tag;
 				pelem->val = val;
+				pelem->raw = start;
+				pelem->rawlen = *pp - start;
 			}
 		}
 	}
@@ -685,6 +691,10 @@ cloop_done:
 cloop_done:
 		;
 	}
+	/* callers read va->len at once; an empty string is a Bytes of
+	 * length zero, not nil */
+	if(err == ASN_OK && ans == nil)
+		ans = makebytes(nil, 0);
 	*pp = p;
 	*pbytes = ans;
 	return err;
@@ -1573,6 +1583,9 @@ typedef struct CertX509 {
 	Bytes*	publickey;
 	int	signature_alg;
 	Bytes*	signature;
+	Bytes*	ext;		/* raw extensions SEQUENCE (opaque); nil if cert has none */
+	Bytes*	rawissuer;	/* issuer and subject as encoded, for name */
+	Bytes*	rawsubject;	/* comparison; the rendered strings are lossy */
 } CertX509;
 
 /* Algorithm object-ids */
@@ -1649,6 +1662,14 @@ static Ints *alg_oid_tab[NUMALGS+1] = {
 	(Ints*)&oid_sha512WithECDSA,
 	nil
 };
+/* rsaverify.c: shared PKCS#1 v1.5 encoding checks */
+extern int	pkcs1unpadbuf(uchar *buf, int len, mpint *modulus, int blocktype);
+extern uchar*	pkcs1digestinfo(int dlen, int *pfxlen);
+
+/* defined below, after the digest length table it consults */
+static char*	X509rsaverifydigest(uchar *sig, int siglen, uchar *edigest,
+			int alg, RSApub *pk);
+
 static DigestFun digestalg[NUMALGS+1] = {
 	md5, md5, md5, md5, sha1, sha1, sha2_256, sha2_384, sha2_512, sha2_224,
 	md5, sha1, sha2_256, sha2_384, sha2_512, sha2_224,
@@ -1691,6 +1712,9 @@ freecert(CertX509* c)
 		free(c->subject);
 	freebytes(c->publickey);
 	freebytes(c->signature);
+	freebytes(c->ext);
+	freebytes(c->rawissuer);
+	freebytes(c->rawsubject);
 	free(c);
 }
 
@@ -1827,6 +1851,7 @@ decode_cert(Bytes* a)
 	c->publickey = nil;
 	c->signature_alg = -1;
 	c->signature = nil;
+	c->ext = nil;
 
 	/* Certificate */
  	if(!is_seq(&ecert, &elcert) || elistlen(elcert) !=3)
@@ -1870,6 +1895,7 @@ decode_cert(Bytes* a)
 		c->serial = -1;	/* else we have to change cert struct */
   	}
 	c->issuer = parse_name(eissuer);
+	c->rawissuer = makebytes(eissuer->raw, eissuer->rawlen);
 	if(c->issuer == nil)
 		goto errret;
 	/* Validity */
@@ -1888,6 +1914,7 @@ decode_cert(Bytes* a)
 
 	/* resume CertificateInfo */
  	c->subject = parse_name(esubj);
+	c->rawsubject = makebytes(esubj->raw, esubj->rawlen);
 	if(c->subject == nil)
 		goto errret;
 
@@ -1910,6 +1937,18 @@ decode_cert(Bytes* a)
 	if(bits->unusedbits != 0)
 		goto errret;
  	c->publickey = makebytes(bits->data, bits->len);
+
+	/* optional TBSCertificate extensions: [3] EXPLICIT Extensions */
+	el = el->tl;
+	while(el != nil){
+		if(el->hd.tag.class == Context && el->hd.tag.num == 3
+		&& el->hd.val.tag == VOctets){
+			c->ext = el->hd.val.u.octetsval;
+			el->hd.val.u.octetsval = nil;	/* steal ownership */
+			break;
+		}
+		el = el->tl;
+	}
 
 	/*resume Certificate */
 	if(c->signature_alg < 0)
@@ -2107,19 +2146,25 @@ errret:
 	return nil;
 }
 
+/*
+ * Convert an ASN.1 INTEGER Elem into a freshly-allocated mpint.
+ * We can't use is_bigint() here -- in the VBigInt case it aliases
+ * pe->val.u.bigintval (no copy), so freeing the returned Bytes would
+ * double-free it when the surrounding ASN tree is later freed.
+ * We read the bytes in place and leave tree ownership alone.
+ */
 static mpint*
 asn1mpint(Elem *e)
 {
 	Bytes *b;
-	mpint *mp;
-	int v;
-
-	if(is_int(e, &v))
-		return itomp(v, nil);
-	if(is_bigint(e, &b)) {
-		mp = betomp(b->data, b->len, nil);
-		freebytes(b);
-		return mp;
+
+	if(e->tag.class != Universal || e->tag.num != INTEGER)
+		return nil;
+	if(e->val.tag == VInt)
+		return itomp(e->val.u.intval, nil);
+	if(e->val.tag == VBigInt){
+		b = e->val.u.bigintval;
+		return betomp(b->data, b->len, nil);
 	}
 	return nil;
 }
@@ -2201,60 +2246,7 @@ digest_certinfo(Bytes *cert, DigestFun digestfun, ucha
 	(*digestfun)(info, infolen, digest, nil);
 }
 
-static char*
-verify_signature(Bytes* signature, RSApub *pk, uchar *edigest, Elem **psigalg)
-{
-	Elem e;
-	Elist *el;
-	Bytes *digest;
-	uchar *pkcs1buf, *buf;
-	int buflen;
-	mpint *pkcs1;
-	int nlen;
-	char *err;
 
-	err = nil;
-	pkcs1buf = nil;
-
-	/* one less than the byte length of the modulus */
-	nlen = (mpsignif(pk->n)-1)/8;
-
-	/* see 9.2.1 of rfc2437 */
-	pkcs1 = betomp(signature->data, signature->len, nil);
-	mpexp(pkcs1, pk->ek, pk->n, pkcs1);
-	buflen = mptobe(pkcs1, nil, 0, &pkcs1buf);
-	buf = pkcs1buf;
-	if(buflen != nlen || buf[0] != 1) {
-		err = "expected 1";
-		goto end;
-	}
-	buf++;
-	while(buf[0] == 0xff)
-		buf++;
-	if(buf[0] != 0) {
-		err = "expected 0";
-		goto end;
-	}
-	buf++;
-	buflen -= buf-pkcs1buf;
-	if(decode(buf, buflen, &e) != ASN_OK || !is_seq(&e, &el) || elistlen(el) != 2 ||
-			!is_octetstring(&el->tl->hd, &digest)) {
-		err = "signature parse error";
-		goto end;
-	}
-	*psigalg = &el->hd;
-	if(memcmp(digest->data, edigest, digest->len) == 0)
-		goto end;
-	err = "digests did not match";
-
-end:
-	if(pkcs1 != nil)
-		mpfree(pkcs1);
-	if(pkcs1buf != nil)
-		free(pkcs1buf);
-	return err;
-}
-
 RSApub*
 X509toRSApub(uchar *cert, int ncert, char *name, int nname)
 {
@@ -2450,21 +2442,616 @@ X509verify(uchar *cert, int ncert, RSApub *pk)
 	char *e;
 	Bytes *b;
 	CertX509 *c;
-	uchar digest[SHA1dlen];
-	Elem *sigalg;
+	uchar digest[SHA2_512dlen];	/* sized for any supported hash */
 
 	b = makebytes(cert, ncert);
 	c = decode_cert(b);
-	if(c != nil)
-		digest_certinfo(b, digestalg[c->signature_alg], digest);
+	if(c == nil){
+		freebytes(b);
+		return "cannot decode cert";
+	}
+	/* digestalg[] has nil holes for key-only OIDs; a cert may name one */
+	if(c->signature_alg < 0 || digestalg[c->signature_alg] == nil){
+		freecert(c);
+		freebytes(b);
+		return "unsupported signature algorithm";
+	}
+	digest_certinfo(b, digestalg[c->signature_alg], digest);
 	freebytes(b);
-	if(c == nil)
+	e = X509rsaverifydigest(c->signature->data, c->signature->len,
+		digest, c->signature_alg, pk);
+	freecert(c);
+	return e;
+}
+
+/* OID for SubjectAltName X.509v3 extension
+ * (id-ce-subjectAltName, 2.5.29.17) */
+static Ints9 oid_subjectAltName = {4, 2, 5, 29, 17};
+
+/* digest length table, parallel to digestalg[] */
+static int digestlen[NUMALGS+1] = {
+	MD5dlen, MD5dlen, MD5dlen, MD5dlen, SHA1dlen, SHA1dlen,
+	SHA2_256dlen, SHA2_384dlen, SHA2_512dlen, SHA2_224dlen,
+	MD5dlen, SHA1dlen, SHA2_256dlen, SHA2_384dlen, SHA2_512dlen, SHA2_224dlen,
+	0,
+	SHA1dlen, SHA2_256dlen, SHA2_384dlen, SHA2_512dlen,
+	0,
+};
+
+/*
+ * Verify an X.509 certificate whose TBS is signed with RSA PKCS#1 v1.5.
+ * Sibling of X509ecdsaverifydigest; alg indexes digestalg[]/digestlen[].
+ */
+static char*
+X509rsaverifydigest(uchar *sig, int siglen, uchar *edigest, int alg,
+	RSApub *pk)
+{
+	uchar *buf, *pfx, *em;
+	int len, pfxlen, emlen, edigestlen;
+	mpint *x, *y;
+	char *err;
+
+	edigestlen = digestlen[alg];
+	pfx = pkcs1digestinfo(edigestlen, &pfxlen);
+	if(pfx == nil)
+		return "unsupported signature algorithm";
+
+	x = betomp(sig, siglen, nil);
+	if(x == nil)
+		return "bad signature encoding";
+	y = rsaencrypt(pk, x, nil);
+	mpfree(x);
+	if(y == nil)
+		return "rsaencrypt failed";
+	len = mptobe(y, nil, 0, &buf);
+	mpfree(y);
+	if(buf == nil)
+		return "mptobe failed";
+
+	/*
+	 * RFC 3447 Section 8.2.2: rebuild the expected encoded message and
+	 * compare it whole.  Parsing the recovered block instead accepts
+	 * short padding, trailing bytes after the DigestInfo, and a
+	 * substituted digest OID -- the shape a low public exponent lets
+	 * an attacker forge without the private key.
+	 */
+	err = "bad signature";
+	len = pkcs1unpadbuf(buf, len, pk->n, 1);
+	emlen = pfxlen + edigestlen;
+	if(len == emlen && (em = malloc(emlen)) != nil){
+		memmove(em, pfx, pfxlen);
+		memmove(em + pfxlen, edigest, edigestlen);
+		if(tsmemcmp(buf, em, emlen) == 0)
+			err = nil;
+		memset(em, 0, emlen);
+		free(em);
+	}
+	free(buf);
+	return err;
+}
+
+/*
+ * Verify an X.509 certificate whose TBS is signed with ECDSA.
+ * dom/pub come from the issuer certificate (extract via X509toECpub).
+ */
+char*
+X509ecdsaverify(uchar *cert, int ncert, ECdomain *dom, ECpub *pub)
+{
+	char *e;
+	Bytes *b;
+	CertX509 *c;
+	uchar digest[SHA2_512dlen];
+
+	b = makebytes(cert, ncert);
+	c = decode_cert(b);
+	if(c == nil){
+		freebytes(b);
 		return "cannot decode cert";
-	e = verify_signature(c->signature, pk, digest, &sigalg);
+	}
+	if(c->signature_alg < 0
+	|| digestalg[c->signature_alg] == nil
+	|| digestlen[c->signature_alg] == 0){
+		freecert(c);
+		freebytes(b);
+		return "unsupported signature algorithm";
+	}
+	digest_certinfo(b, digestalg[c->signature_alg], digest);
+	freebytes(b);
+	e = X509ecdsaverifydigest(c->signature->data, c->signature->len,
+		digest, digestlen[c->signature_alg], dom, pub);
 	freecert(c);
 	return e;
 }
 
+/*
+ * RFC 6125 Section 6.4.3: case-insensitive DNS-name match with leftmost-label
+ * wildcard.  "*.example.com" matches "foo.example.com" but NOT
+ * "foo.bar.example.com" (one-label wildcard only) or "example.com".
+ * pat is the SAN/CN pattern; host is the caller's expected hostname.
+ * Both assumed to be lowercase-normalized on arrival.
+ */
+static int
+matchhostname(char *pat, int patlen, char *host)
+{
+	int hlen, plen;
+	char *star, *remainder;
+	int prefixlen, suffixlen, i;
+
+	hlen = strlen(host);
+	plen = patlen;
+
+	/* exact match (case-insensitive) */
+	if(plen == hlen && cistrncmp(pat, host, plen) == 0)
+		return 1;
+
+	/* look for a wildcard '*' -- must be in the first label, alone */
+	star = memchr(pat, '*', plen);
+	if(star == nil)
+		return 0;
+	if(star != pat)	/* leftmost-label-only wildcard */
+		return 0;
+	if(plen == 1)	/* bare '*' pattern matches no host (RFC 6125 Section 6.4.3) */
+		return 0;
+	/* '*' must not span a dot -- it covers exactly one label */
+	remainder = star + 1;
+	if(remainder < pat + plen && *remainder != '.')
+		return 0;
+	/* pat = "*" + remainder; host must have at least one label + remainder */
+	suffixlen = (pat + plen) - remainder;
+	if(hlen < suffixlen + 1)	/* need at least 1 char + suffix */
+		return 0;
+	/* the leftmost host label must not contain a dot */
+	for(i = 0; i < hlen - suffixlen; i++){
+		if(host[i] == '.')
+			return 0;
+	}
+	prefixlen = hlen - suffixlen;
+	if(prefixlen < 1)
+		return 0;
+	return cistrncmp(remainder, host + prefixlen, suffixlen) == 0;
+}
+
+/*
+ * Check a certificate's subjectAltName entries against the expected
+ * hostname.  Returns nil on match, error string on failure.
+ *
+ * The subject commonName is not consulted.  RFC 9525 Section 2 says
+ * other RDNs "MUST NOT be used to identify a service", and a rendered
+ * subject string cannot say which attribute a value came from, so a
+ * certificate whose subject ends in an unvalidated OU would match on
+ * it.  Go removed the same fallback in 1.15 and now only names it when
+ * explaining a failure.  A certificate with no subjectAltName is
+ * therefore rejected; CAs have been required to issue one since 2017.
+ */
+/*
+ * Parse an address literal into out, returning its length in octets:
+ * 4 for dotted-quad IPv4, 16 for IPv6, 0 if s is not an address at all
+ * (in which case it is treated as a DNS name).
+ *
+ * A scope id ("fe80::1%ether0") is refused: it names a local interface,
+ * never a peer identity, and no certificate can carry one.  The mixed
+ * form "::ffff:1.2.3.4" is refused too, so one address has exactly one
+ * encoding to compare against -- an iPAddress SAN holds 4 or 16 raw
+ * octets and a certificate that means IPv4 must use the 4-octet form.
+ */
+static int
+parseip(char *s, uchar out[16])
+{
+	int i, v, ell, n;
+	char *p;
+	uchar buf[16];
+
+	if(strchr(s, ':') == nil){
+		for(i = 0; i < 4; i++){
+			if(*s < '0' || *s > '9')
+				return 0;
+			v = strtol(s, &p, 10);
+			if(p == s || v < 0 || v > 255)
+				return 0;
+			out[i] = v;
+			s = p;
+			if(i < 3){
+				if(*s != '.')
+					return 0;
+				s++;
+			}
+		}
+		return *s == 0 ? 4 : 0;
+	}
+
+	if(strchr(s, '%') != nil || strchr(s, '.') != nil)
+		return 0;
+	memset(buf, 0, sizeof(buf));
+	ell = -1;
+	n = 0;
+	if(s[0] == ':'){
+		if(s[1] != ':')
+			return 0;
+		s += 2;
+		ell = 0;
+		if(*s == 0){		/* "::" is all zeros */
+			memmove(out, buf, 16);
+			return 16;
+		}
+	}
+	for(;;){
+		if(*s < '0')
+			return 0;
+		v = strtol(s, &p, 16);
+		if(p == s || v < 0 || v > 0xffff || p - s > 4)
+			return 0;
+		if(n + 2 > 16)
+			return 0;
+		buf[n++] = v >> 8;
+		buf[n++] = v;
+		s = p;
+		if(*s == 0)
+			break;
+		if(*s != ':')
+			return 0;
+		s++;
+		if(*s == ':'){
+			if(ell >= 0)
+				return 0;
+			ell = n;
+			s++;
+			if(*s == 0)
+				break;
+		}
+	}
+	if(ell < 0){
+		if(n != 16)
+			return 0;
+	}else{
+		if(n >= 16)
+			return 0;
+		memmove(buf + 16 - (n - ell), buf + ell, n - ell);
+		memset(buf + ell, 0, 16 - n);
+	}
+	memmove(out, buf, 16);
+	return 16;
+}
+
+char*
+X509matchhostname(uchar *cert, int ncert, char *hostname)
+{
+	Bytes *b;
+	CertX509 *c;
+	Elem eext, ealt;
+	Elist *el, *l;
+	Ints *oid;
+	Bytes *altoct, *name;
+	int havesan, matched, iplen, hlen;
+	uchar ipaddr[16];
+	char *err, *hostbuf;
+
+	if(hostname == nil || *hostname == 0)
+		return "no hostname to match against";
+
+	/* RFC 6125 Section 6.4.1: strip trailing dot in reference identifier. */
+	hostbuf = nil;
+	hlen = strlen(hostname);
+	if(hlen > 0 && hostname[hlen-1] == '.'){
+		hostbuf = emalloc(hlen);
+		memmove(hostbuf, hostname, hlen-1);
+		hostbuf[hlen-1] = 0;
+		hostname = hostbuf;
+	}
+
+	/* If hostname is an IPv4 literal, we match iPAddress SAN entries
+	 * instead of dNSName entries (RFC 5280
+	 * Section 4.2.1.6, RFC 6125 Section 1.7.2). */
+	iplen = parseip(hostname, ipaddr);
+
+	b = makebytes(cert, ncert);
+	c = decode_cert(b);
+	freebytes(b);
+	if(c == nil){
+		free(hostbuf);
+		return "cannot decode cert";
+	}
+
+	err = "hostname does not match cert";
+	havesan = 0;
+	matched = 0;
+
+	if(c->ext != nil && decode(c->ext->data, c->ext->len, &eext) == ASN_OK){
+		if(is_seq(&eext, &el)){
+			for(; el != nil && !matched; el = el->tl){
+				if(!is_seq(&el->hd, &l) || elistlen(l) < 2)
+					continue;
+				if(!is_oid(&l->hd, &oid) || !ints_eq(oid, (Ints*)&oid_subjectAltName))
+					continue;
+				/* skip optional BOOLEAN critical flag */
+				l = l->tl;
+				if(l->hd.val.tag == VBool)
+					l = l->tl;
+				if(l == nil || !is_octetstring(&l->hd, &altoct))
+					continue;
+				if(decode(altoct->data, altoct->len, &ealt) != ASN_OK)
+					continue;
+				if(!is_seq(&ealt, &l)){
+					freevalfields(&ealt.val);
+					continue;
+				}
+				for(; l != nil; l = l->tl){
+					if(l->hd.tag.class != Context)
+						continue;
+					if(l->hd.val.tag != VOctets)
+						continue;
+					name = l->hd.val.u.octetsval;
+					/*
+					 * GeneralName [7] iPAddress: 4 raw octets
+					 * for IPv4, 16 for IPv6.  An address is
+					 * matched only against these, never against
+					 * a dNSName -- RFC 9525 Section 6.2.
+					 */
+					if(l->hd.tag.num == 7 && iplen != 0){
+						havesan = 1;
+						if(name->len == iplen
+						&& memcmp(name->data, ipaddr, iplen) == 0){
+							matched = 1;
+							break;
+						}
+						continue;
+					}
+					/* GeneralName [2] dNSName IA5String -- only when hostname
+					 * is a textual name, not when it's an IP literal. */
+					if(l->hd.tag.num == 2 && iplen == 0){
+						havesan = 1;
+						if(matchhostname((char*)name->data, name->len, hostname)){
+							matched = 1;
+							break;
+						}
+					}
+				}
+				freevalfields(&ealt.val);
+			}
+		}
+		freevalfields(&eext.val);
+	}
+
+	if(matched)
+		err = nil;
+	else if(!havesan)
+		err = "cert has no dNSName or iPAddress SAN";
+	freecert(c);
+	free(hostbuf);
+	return err;
+}
+
+/*
+ * Verify that `cert` was signed by `signer` -- extracts the signer's public
+ * key based on its publickey_alg and calls the appropriate algorithm-specific
+ * verify.  Handles both RSA and ECDSA issuers.  Returns nil on success.
+ */
+static char*
+cert_signedby(uchar *cert, int ncert, uchar *signer, int nsigner)
+{
+	Bytes *sb;
+	CertX509 *sc;
+	RSApub *rsa;
+	ECpub *ec;
+	ECdomain dom;
+	char *err;
+
+	sb = makebytes(signer, nsigner);
+	sc = decode_cert(sb);
+	freebytes(sb);
+	if(sc == nil)
+		return "cannot decode signer cert";
+
+	err = "unsupported signer key algorithm";
+	switch(sc->publickey_alg){
+	case ALG_rsaEncryption:
+		rsa = decode_rsapubkey(sc->publickey);
+		if(rsa == nil){
+			err = "cannot decode signer RSA pubkey";
+			break;
+		}
+		err = X509verify(cert, ncert, rsa);
+		rsapubfree(rsa);
+		break;
+	case ALG_ecPublicKey:
+		if(sc->curve < 0 || sc->curve >= NUMCURVES){
+			err = "unsupported signer EC curve";
+			break;
+		}
+		ecdominit(&dom, namedcurves[sc->curve]);
+		ec = ecdecodepub(&dom, sc->publickey->data, sc->publickey->len);
+		if(ec == nil){
+			ecdomfree(&dom);
+			err = "cannot decode signer EC pubkey";
+			break;
+		}
+		err = X509ecdsaverify(cert, ncert, &dom, ec);
+		ecpubfree(ec);
+		ecdomfree(&dom);
+		break;
+	}
+	freecert(sc);
+	return err;
+}
+
+/*
+ * Path-building budget.  MAXHOPS bounds chain length (RFC 5280 implementations
+ * commonly cap at 10).  MAXVERIFY caps the number of cryptographic signature
+ * checks across the whole call: a malicious pool full of look-alike subjects
+ * could otherwise force quadratic verify cost.  Both are generous for real
+ * Web PKI, where chains are 3-5 deep and pools are small.
+ */
+enum {
+	MAXHOPS		= 10,
+	MAXVERIFY	= 100,
+};
+
+/*
+ * Recursive DFS path-builder.  `cert` is the cert at the head of the
+ * partially-built path (depth 0 = leaf).  Try every entry in `pool`
+ * and `roots` whose subject matches cert->issuer and whose key
+ * verifies cert's signature; on a hit from `roots` we have reached a
+ * trust anchor and succeed; on a hit from `pool` we descend with the
+ * discovered intermediate as the new head.  On dead-end the recursion
+ * returns to its caller, which tries its next candidate -- this is
+ * the backtracking that distinguishes full DFS from a "first parent
+ * wins" walk.
+ *
+ * `visited[0..nvisited-1]` is the live path (used for cycle
+ * detection across cross-signs).  *budget is shared across all
+ * branches; when it reaches 0, the search aborts globally.
+ *
+ * Returns nil on success.  On failure, returns the most informative
+ * error string seen across the explored branches: a fatal error
+ * (budget exhausted, depth cap hit) takes priority over the generic
+ * "no path to a trust anchor".
+ */
+static char*
+verify_chain_dfs(CertX509 *cert, uchar *cert_pem, int cert_pemlen,
+	PEMChain *pool, PEMChain *roots,
+	PEMChain **visited, int nvisited,
+	int depth, int *budget)
+{
+	PEMChain *cands[2];
+	PEMChain *p;
+	Bytes *cb;
+	CertX509 *parent;
+	char *err, *suberr;
+	int i, j, fromroots, seen;
+
+	if(*budget <= 0)
+		return "signature-verify budget exhausted";
+
+	err = "no path to a trust anchor";
+	cands[0] = pool;	/* intermediates first */
+	cands[1] = roots;	/* trust anchors last */
+
+	for(i = 0; i < 2; i++){
+		fromroots = (i == 1);
+		for(p = cands[i]; p != nil; p = p->next){
+			if(p->pem == nil)
+				continue;
+			seen = 0;
+			for(j = 0; j < nvisited; j++)
+				if(visited[j] == p){
+					seen = 1;
+					break;
+				}
+			if(seen)
+				continue;
+			cb = makebytes(p->pem, p->pemlen);
+			parent = decode_cert(cb);
+			freebytes(cb);
+			if(parent == nil)
+				continue;
+			if(parent->subject == nil || cert->issuer == nil
+			|| parent->rawsubject == nil || cert->rawissuer == nil
+			|| parent->rawsubject->len != cert->rawissuer->len
+			|| memcmp(parent->rawsubject->data, cert->rawissuer->data,
+				cert->rawissuer->len) != 0){
+				freecert(parent);
+				continue;
+			}
+			if(*budget <= 0){
+				freecert(parent);
+				return "signature-verify budget exhausted";
+			}
+			(*budget)--;
+			suberr = cert_signedby(cert_pem, cert_pemlen,
+				p->pem, p->pemlen);
+			if(suberr != nil){
+				freecert(parent);
+				/* A candidate whose name matched but whose
+				 * signature did not says more than the
+				 * generic default; keep the first one. */
+				if(strcmp(err, "no path to a trust anchor") == 0)
+					err = suberr;
+				continue;
+			}
+			if(fromroots){
+				/* Trust anchor reached.  The anchor is not
+				 * subjected to further per-cert checks. */
+				freecert(parent);
+				return nil;
+			}
+			if(depth + 1 >= MAXHOPS){
+				freecert(parent);
+				if(strcmp(err, "no path to a trust anchor") == 0)
+					err = "chain length exceeds MAXHOPS";
+				continue;
+			}
+			visited[nvisited] = p;
+			suberr = verify_chain_dfs(parent, p->pem, p->pemlen,
+				pool, roots, visited, nvisited+1,
+				depth+1, budget);
+			freecert(parent);
+			if(suberr == nil)
+				return nil;
+			/* Budget exhaustion is fatal across all branches. */
+			if(strcmp(suberr, "signature-verify budget exhausted") == 0)
+				return suberr;
+			err = suberr;
+		}
+	}
+	return err;
+}
+
+/*
+ * Walk a certificate chain.  `chain->pem` is the leaf (end-entity cert).
+ * `chain->next..` is treated as an unordered intermediate pool, NOT a fixed
+ * linear path: the verifier finds, for each cert, any candidate in the pool
+ * (or in `roots`) whose subject matches the cert's issuer and whose signature
+ * verifies the cert.  Cross-signed paths are accepted: a server may
+ * present a leaf-R3-DST-X3 chain while the client trusts only ISRG-X1,
+ * and R3 may also chain to ISRG-X1 directly.
+ *
+ * Search uses depth-first recursion with backtracking: when multiple
+ * candidate parents are present (e.g. a cross-signed intermediate), each is
+ * tried in turn and a dead-end branch unwinds to try the next candidate.
+ *
+ * Also calls X509matchhostname on the leaf when `hostname` is non-nil/empty.
+ *
+ * Bounded by MAXHOPS chain length and MAXVERIFY total signature checks.
+ *
+ * BUG: does not currently check validity dates, basicConstraints CA:TRUE,
+ * keyUsage/extKeyUsage, or nameConstraints.  The cryptographic chain and
+ * hostname binding are enforced.
+ *
+ * Returns nil on success, error string on failure.
+ */
+char*
+X509verifychain(PEMChain *chain, PEMChain *roots, char *hostname)
+{
+	char *err;
+	Bytes *cb;
+	CertX509 *leaf;
+	PEMChain *visited[MAXHOPS+1];
+	int budget;
+
+	if(chain == nil || chain->pem == nil)
+		return "empty chain";
+	if(roots == nil)
+		return "no trust anchors configured";
+
+	if(hostname != nil && *hostname != 0){
+		err = X509matchhostname(chain->pem, chain->pemlen, hostname);
+		if(err != nil)
+			return err;
+	}
+
+	cb = makebytes(chain->pem, chain->pemlen);
+	leaf = decode_cert(cb);
+	freebytes(cb);
+	if(leaf == nil)
+		return "cannot decode leaf cert";
+
+	visited[0] = chain;
+	budget = MAXVERIFY;
+	err = verify_chain_dfs(leaf, chain->pem, chain->pemlen,
+		chain->next, roots, visited, 1, 0, &budget);
+	freecert(leaf);
+	return err;
+}
+
 /* ------- Elem constructors ---------- */
 static Elem
 Null(void)
@@ -2892,19 +3479,24 @@ X509dump(uchar *cert, int ncert)
 	Bytes *b;
 	CertX509 *c;
 	RSApub *pk;
-	uchar digest[SHA1dlen];
-	Elem *sigalg;
+	uchar digest[SHA2_512dlen];	/* sized for any supported hash */
 
 	print("begin X509dump\n");
 	b = makebytes(cert, ncert);
 	c = decode_cert(b);
-	if(c != nil)
-		digest_certinfo(b, digestalg[c->signature_alg], digest);
-	freebytes(b);
 	if(c == nil){
+		freebytes(b);
 		print("cannot decode cert");
 		return;
 	}
+	if(c->signature_alg < 0 || digestalg[c->signature_alg] == nil){
+		freebytes(b);
+		freecert(c);
+		print("unsupported signature algorithm\n");
+		return;
+	}
+	digest_certinfo(b, digestalg[c->signature_alg], digest);
+	freebytes(b);
 
 	print("serial %d\n", c->serial);
 	print("issuer %s\n", c->issuer);
@@ -2914,14 +3506,11 @@ X509dump(uchar *cert, int ncert)
 	print("pubkey e=%B n(%d)=%B\n", pk->ek, mpsignif(pk->n), pk->n);
 
 	print("sigalg=%d digest=%.*H\n", c->signature_alg, SHA2_256dlen, digest);
-	e = verify_signature(c->signature, pk, digest, &sigalg);
-	if(e==nil){
+	e = X509rsaverifydigest(c->signature->data, c->signature->len,
+		digest, c->signature_alg, pk);
+	if(e == nil)
 		e = "nil (meaning ok)";
-		print("sigalg=\n");
-		if(sigalg)
-			edump(*sigalg);
-	}
-	print("self-signed verify_signature returns: %s\n", e);
+	print("self-signed X509rsaverifydigest returns: %s\n", e);
 
 	rsapubfree(pk);
 	freecert(c);

Bell Labs OSI certified Powered by Plan 9

(Return to Plan 9 Home Page)

Copyright © 2026 Plan 9 Foundation. All Rights Reserved.
Comments to webmaster@9p.io.