components/openssl/openssl-1.0.1/patches/33_cert_chain.patch
author jenny.yung@oracle.com <jenny.yung@oracle.com>
Thu, 24 Oct 2013 14:16:19 -0700
branchs11-update
changeset 2801 02364d9168f2
child 1641 2fc479afcf70
permissions -rw-r--r--
16623515 OpenSSL: Need to update the license file 16922032 Need X509_V_FLAG_PARTIAL_CHAIN - ability to trust a leaf certificate

This patch comes from OpenSSL upstream code, and the change has been commited to OpenSSL 1.0.2.
  http://git.openssl.org/gitweb/?p=openssl.git;a=commit;h=fbd2164044f92383955a801ad1b2857d71e83f27
  http://git.openssl.org/gitweb/?p=openssl.git;a=commit;h=51e7a4378a78bb0870a2cdc5c524c230c929ebcb
  http://git.openssl.org/gitweb/?p=openssl.git;a=commit;h=2dabd822366df7b2608b55d5ca5f31d5d484cbaf

Index: openssl/crypto/x509/x509_trs.c
============================================================================
$ diff -ru crypto/x509/x509_trs.c crypto/x509/x509_trs.c 
--- openssl/crypto/x509/x509_trs.c.orig	4 Dec 2012 17:26:04 -0000	1.133.2.11.2.6.2.3
+++ openssl/crypto/x509/x509_trs.c	14 Dec 2012 14:30:45 -0000	1.133.2.11.2.6.2.4
@@ -114,6 +114,15 @@ int X509_check_trust(X509 *x, int id, int flags)
 	X509_TRUST *pt;
 	int idx;
 	if(id == -1) return 1;
+	/* We get this as a default value */
+	if (id == 0)
+		{
+		int rv;
+		rv = obj_trust(NID_anyExtendedKeyUsage, x, 0);
+		if (rv != X509_TRUST_UNTRUSTED)
+			return rv;
+		return trust_compat(NULL, x, 0);
+		}
 	idx = X509_TRUST_get_by_id(id);
 	if(idx == -1) return default_trust(id, x, flags);
 	pt = X509_TRUST_get0(idx);
Index: openssl/crypto/x509/x509_vfy.c
============================================================================
$ cvs diff -u -r1.105.2.9.2.4.2.3 -r1.105.2.9.2.4.2.4 x509_vfy.c
--- openssl/crypto/x509/x509_vfy.c	14 Dec 2012 12:53:48 -0000	1.105.2.9.2.4.2.3
+++ openssl/crypto/x509/x509_vfy.c	14 Dec 2012 14:30:46 -0000	1.105.2.9.2.4.2.4
@@ -150,6 +150,33 @@
 	}
 #endif
 
+/* Given a certificate try and find an exact match in the store */
+
+static X509 *lookup_cert_match(X509_STORE_CTX *ctx, X509 *x)
+	{
+	STACK_OF(X509) *certs;
+	X509 *xtmp = NULL;
+	int i;
+	/* Lookup all certs with matching subject name */
+	certs = ctx->lookup_certs(ctx, X509_get_subject_name(x));
+	if (certs == NULL)
+		return NULL;
+	/* Look for exact match */
+	for (i = 0; i < sk_X509_num(certs); i++)
+		{
+		xtmp = sk_X509_value(certs, i);
+		if (!X509_cmp(xtmp, x))
+			break;
+		}
+	if (i < sk_X509_num(certs))
+		CRYPTO_add(&xtmp->references,1,CRYPTO_LOCK_X509);
+	else
+		xtmp = NULL;
+	sk_X509_pop_free(certs, X509_free);
+	return xtmp;
+	}
+
+
 int X509_verify_cert(X509_STORE_CTX *ctx)
 	{
 	X509 *x,*xtmp,*chain_ss=NULL;
@@ -307,8 +307,13 @@ int X509_verify_cert(X509_STORE_CTX *ctx)
 
 	/* we now have our chain, lets check it... */
 
-	/* Is last certificate looked up self signed? */
-	if (!ctx->check_issued(ctx,x,x))
+	i = check_trust(ctx);
+
+	/* If explicitly rejected error */
+	if (i == X509_TRUST_REJECTED)
+		goto end;
+	/* If not explicitly trusted then indicate error */
+	if (i != X509_TRUST_TRUSTED)
 		{
 		if ((chain_ss == NULL) || !ctx->check_issued(ctx, x, chain_ss))
 			{
@@ -346,12 +351,6 @@ int X509_verify_cert(X509_STORE_CTX *ctx)
 	
 	if (!ok) goto end;
 
-	/* The chain extensions are OK: check trust */
-
-	if (param->trust > 0) ok = check_trust(ctx);
-
-	if (!ok) goto end;
-
 	/* We may as well copy down any DSA parameters that are required */
 	X509_get_pubkey_parameters(NULL,ctx->chain);
 
@@ -642,28 +641,54 @@ static int check_name_constraints(X509_STORE_CTX *ctx)
 
 static int check_trust(X509_STORE_CTX *ctx)
 {
-#ifdef OPENSSL_NO_CHAIN_VERIFY
-	return 1;
-#else
 	int i, ok;
-	X509 *x;
+	X509 *x = NULL;
 	int (*cb)(int xok,X509_STORE_CTX *xctx);
 	cb=ctx->verify_cb;
-/* For now just check the last certificate in the chain */
-	i = sk_X509_num(ctx->chain) - 1;
-	x = sk_X509_value(ctx->chain, i);
-	ok = X509_check_trust(x, ctx->param->trust, 0);
-	if (ok == X509_TRUST_TRUSTED)
-		return 1;
-	ctx->error_depth = i;
-	ctx->current_cert = x;
-	if (ok == X509_TRUST_REJECTED)
-		ctx->error = X509_V_ERR_CERT_REJECTED;
-	else
-		ctx->error = X509_V_ERR_CERT_UNTRUSTED;
-	ok = cb(0, ctx);
-	return ok;
-#endif
+	/* Check all trusted certificates in chain */
+	for (i = ctx->last_untrusted; i < sk_X509_num(ctx->chain); i++)
+		{
+		x = sk_X509_value(ctx->chain, i);
+		ok = X509_check_trust(x, ctx->param->trust, 0);
+		/* If explicitly trusted return trusted */
+		if (ok == X509_TRUST_TRUSTED)
+			return X509_TRUST_TRUSTED;
+		/* If explicitly rejected notify callback and reject if
+		 * not overridden.
+		 */
+		if (ok == X509_TRUST_REJECTED)
+			{
+			ctx->error_depth = i;
+			ctx->current_cert = x;
+			ctx->error = X509_V_ERR_CERT_REJECTED;
+			ok = cb(0, ctx);
+			if (!ok)
+				return X509_TRUST_REJECTED;
+			}
+		}
+	/* If we accept partial chains and have at least one trusted
+	 * certificate return success.
+	 */
+	if (ctx->param->flags & X509_V_FLAG_PARTIAL_CHAIN)
+		{
+		X509 *mx;
+		if (ctx->last_untrusted < sk_X509_num(ctx->chain))
+			return X509_TRUST_TRUSTED;
+		x = sk_X509_value(ctx->chain, 0);
+		mx = lookup_cert_match(ctx, x);
+		if (mx)
+			{
+			(void)sk_X509_set(ctx->chain, 0, mx);
+			X509_free(x);
+			ctx->last_untrusted = 0;
+			return X509_TRUST_TRUSTED;
+			}
+		}
+
+	/* If no trusted certs in chain at all return untrusted and
+	 * allow standard (no issuer cert) etc errors to be indicated.
+	 */
+	return X509_TRUST_UNTRUSTED;
 }
 
 static int check_revocation(X509_STORE_CTX *ctx)
@@ -1591,6 +1630,8 @@ static int internal_verify(X509_STORE_CTX *ctx)
 		xs=xi;
 	else
 		{
+		if (ctx->param->flags & X509_V_FLAG_PARTIAL_CHAIN && n == 0)
+			return check_cert_time(ctx, xi);
 		if (n <= 0)
 			{
 			ctx->error=X509_V_ERR_UNABLE_TO_VERIFY_LEAF_SIGNATURE;
Index: openssl/crypto/x509/x509_vfy.h
============================================================================
$ cvs diff -u -r1.67.2.3.4.1 -r1.67.2.3.4.2 x509_vfy.h
--- openssl/crypto/x509/x509_vfy.h	26 Sep 2012 13:50:42 -0000	1.67.2.3.4.1
+++ openssl/crypto/x509/x509_vfy.h	14 Dec 2012 14:30:46 -0000	1.67.2.3.4.2
@@ -390,6 +390,8 @@
 /* Check selfsigned CA signature */
 #define X509_V_FLAG_CHECK_SS_SIGNATURE		0x4000
 
+/* Allow partial chains if at least one certificate is in trusted store */
+#define X509_V_FLAG_PARTIAL_CHAIN		0x80000
 
 #define X509_VP_FLAG_DEFAULT			0x1
 #define X509_VP_FLAG_OVERWRITE			0x2
Index: openssl/apps/apps.c
============================================================================
$ cvs diff -u -r1.133.2.11.2.6.2.3 -r1.133.2.11.2.6.2.4 apps.c
--- openssl/apps/apps.c	4 Dec 2012 17:26:04 -0000	1.133.2.11.2.6.2.3
+++ openssl/apps/apps.c	14 Dec 2012 14:30:45 -0000	1.133.2.11.2.6.2.4
@@ -2361,6 +2361,8 @@
 		flags |= X509_V_FLAG_NOTIFY_POLICY;
 	else if (!strcmp(arg, "-check_ss_sig"))
 		flags |= X509_V_FLAG_CHECK_SS_SIGNATURE;
+	else if (!strcmp(arg, "-partial_chain"))
+		flags |= X509_V_FLAG_PARTIAL_CHAIN;
 	else
 		return 0;