7e7e3f2
diff -up openssl-1.0.1k/apps/apps.c.trusted-first openssl-1.0.1k/apps/apps.c
7e7e3f2
--- openssl-1.0.1k/apps/apps.c.trusted-first	2015-01-08 15:00:36.000000000 +0100
7e7e3f2
+++ openssl-1.0.1k/apps/apps.c	2015-01-09 10:19:45.476779456 +0100
a78828f
@@ -2365,6 +2365,8 @@ int args_verify(char ***pargs, int *parg
30aa930
 		flags |= X509_V_FLAG_NOTIFY_POLICY;
30aa930
 	else if (!strcmp(arg, "-check_ss_sig"))
30aa930
 		flags |= X509_V_FLAG_CHECK_SS_SIGNATURE;
30aa930
+	else if (!strcmp(arg, "-trusted_first"))
30aa930
+		flags |= X509_V_FLAG_TRUSTED_FIRST;
30aa930
 	else
30aa930
 		return 0;
30aa930
 
7e7e3f2
diff -up openssl-1.0.1k/apps/cms.c.trusted-first openssl-1.0.1k/apps/cms.c
7e7e3f2
--- openssl-1.0.1k/apps/cms.c.trusted-first	2015-01-08 15:00:36.000000000 +0100
7e7e3f2
+++ openssl-1.0.1k/apps/cms.c	2015-01-09 10:19:45.476779456 +0100
9c324da
@@ -642,6 +642,7 @@ int MAIN(int argc, char **argv)
9c324da
 		BIO_printf (bio_err, "-text          include or delete text MIME headers\n");
9c324da
 		BIO_printf (bio_err, "-CApath dir    trusted certificates directory\n");
9c324da
 		BIO_printf (bio_err, "-CAfile file   trusted certificates file\n");
9c324da
+		BIO_printf (bio_err, "-trusted_first use trusted certificates first when building the trust chain\n");
9c324da
 		BIO_printf (bio_err, "-crl_check     check revocation status of signer's certificate using CRLs\n");
9c324da
 		BIO_printf (bio_err, "-crl_check_all check revocation status of signer's certificate chain using CRLs\n");
9c324da
 #ifndef OPENSSL_NO_ENGINE
7e7e3f2
diff -up openssl-1.0.1k/apps/ocsp.c.trusted-first openssl-1.0.1k/apps/ocsp.c
7e7e3f2
--- openssl-1.0.1k/apps/ocsp.c.trusted-first	2015-01-09 10:19:45.477779478 +0100
7e7e3f2
+++ openssl-1.0.1k/apps/ocsp.c	2015-01-09 10:20:57.726413440 +0100
a78828f
@@ -605,6 +605,7 @@ int MAIN(int argc, char **argv)
7e7e3f2
 		BIO_printf (bio_err, "-path                path to use in OCSP request\n");
7e7e3f2
 		BIO_printf (bio_err, "-CApath dir          trusted certificates directory\n");
7e7e3f2
 		BIO_printf (bio_err, "-CAfile file         trusted certificates file\n");
7e7e3f2
+		BIO_printf (bio_err, "-trusted_first       use trusted certificates first when building the trust chain\n");
7e7e3f2
 		BIO_printf (bio_err, "-VAfile file         validator certificates file\n");
7e7e3f2
 		BIO_printf (bio_err, "-validity_period n   maximum validity discrepancy in seconds\n");
7e7e3f2
 		BIO_printf (bio_err, "-status_age n        maximum status age in seconds\n");
7e7e3f2
diff -up openssl-1.0.1k/apps/s_client.c.trusted-first openssl-1.0.1k/apps/s_client.c
7e7e3f2
--- openssl-1.0.1k/apps/s_client.c.trusted-first	2015-01-09 10:19:45.438778596 +0100
7e7e3f2
+++ openssl-1.0.1k/apps/s_client.c	2015-01-09 10:19:45.477779478 +0100
a78828f
@@ -299,6 +299,7 @@ static void sc_usage(void)
9c324da
 	BIO_printf(bio_err," -pass arg     - private key file pass phrase source\n");
9c324da
 	BIO_printf(bio_err," -CApath arg   - PEM format directory of CA's\n");
9c324da
 	BIO_printf(bio_err," -CAfile arg   - PEM format file of CA's\n");
9c324da
+	BIO_printf(bio_err," -trusted_first - Use trusted CA's first when building the trust chain\n");
9c324da
 	BIO_printf(bio_err," -reconnect    - Drop and re-make the connection with the same Session-ID\n");
9c324da
 	BIO_printf(bio_err," -pause        - sleep(1) after each read(2) and write(2) system call\n");
a78828f
 	BIO_printf(bio_err," -prexit       - print session information even on connection failure\n");
7e7e3f2
diff -up openssl-1.0.1k/apps/smime.c.trusted-first openssl-1.0.1k/apps/smime.c
7e7e3f2
--- openssl-1.0.1k/apps/smime.c.trusted-first	2015-01-08 15:00:36.000000000 +0100
7e7e3f2
+++ openssl-1.0.1k/apps/smime.c	2015-01-09 10:19:45.477779478 +0100
9c324da
@@ -479,6 +479,7 @@ int MAIN(int argc, char **argv)
9c324da
 		BIO_printf (bio_err, "-text          include or delete text MIME headers\n");
9c324da
 		BIO_printf (bio_err, "-CApath dir    trusted certificates directory\n");
9c324da
 		BIO_printf (bio_err, "-CAfile file   trusted certificates file\n");
9c324da
+		BIO_printf (bio_err, "-trusted_first use trusted certificates first when building the trust chain\n");
9c324da
 		BIO_printf (bio_err, "-crl_check     check revocation status of signer's certificate using CRLs\n");
9c324da
 		BIO_printf (bio_err, "-crl_check_all check revocation status of signer's certificate chain using CRLs\n");
9c324da
 #ifndef OPENSSL_NO_ENGINE
7e7e3f2
diff -up openssl-1.0.1k/apps/s_server.c.trusted-first openssl-1.0.1k/apps/s_server.c
7e7e3f2
--- openssl-1.0.1k/apps/s_server.c.trusted-first	2015-01-09 10:19:45.445778755 +0100
7e7e3f2
+++ openssl-1.0.1k/apps/s_server.c	2015-01-09 10:19:45.478779501 +0100
a78828f
@@ -502,6 +502,7 @@ static void sv_usage(void)
9c324da
 	BIO_printf(bio_err," -state        - Print the SSL states\n");
9c324da
 	BIO_printf(bio_err," -CApath arg   - PEM format directory of CA's\n");
9c324da
 	BIO_printf(bio_err," -CAfile arg   - PEM format file of CA's\n");
9c324da
+	BIO_printf(bio_err," -trusted_first - Use trusted CA's first when building the trust chain\n");
9c324da
 	BIO_printf(bio_err," -nocert       - Don't use any certificates (Anon-DH)\n");
9c324da
 	BIO_printf(bio_err," -cipher arg   - play with 'openssl ciphers' to see what goes here\n");
9c324da
 	BIO_printf(bio_err," -serverpref   - Use server's cipher preferences\n");
7e7e3f2
diff -up openssl-1.0.1k/apps/s_time.c.trusted-first openssl-1.0.1k/apps/s_time.c
7e7e3f2
--- openssl-1.0.1k/apps/s_time.c.trusted-first	2015-01-09 10:19:45.391777534 +0100
7e7e3f2
+++ openssl-1.0.1k/apps/s_time.c	2015-01-09 10:19:45.478779501 +0100
9c324da
@@ -179,6 +179,7 @@ static void s_time_usage(void)
9c324da
                 file if not specified by this option\n\
9c324da
 -CApath arg   - PEM format directory of CA's\n\
9c324da
 -CAfile arg   - PEM format file of CA's\n\
9c324da
+-trusted_first - Use trusted CA's first when building the trust chain\n\
9c324da
 -cipher       - preferred cipher to use, play with 'openssl ciphers'\n\n";
9c324da
 
9c324da
 	printf( "usage: s_time <args>\n\n" );
7e7e3f2
diff -up openssl-1.0.1k/apps/ts.c.trusted-first openssl-1.0.1k/apps/ts.c
7e7e3f2
--- openssl-1.0.1k/apps/ts.c.trusted-first	2015-01-09 10:19:45.435778529 +0100
7e7e3f2
+++ openssl-1.0.1k/apps/ts.c	2015-01-09 10:19:45.478779501 +0100
9c324da
@@ -383,7 +383,7 @@ int MAIN(int argc, char **argv)
9c324da
 		   "ts -verify [-data file_to_hash] [-digest digest_bytes] "
9c324da
 		   "[-queryfile request.tsq] "
9c324da
 		   "-in response.tsr [-token_in] "
9c324da
-		   "-CApath ca_path -CAfile ca_file.pem "
9c324da
+		   "-CApath ca_path -CAfile ca_file.pem -trusted_first"
9c324da
 		   "-untrusted cert_file.pem\n");
9c324da
  cleanup:
9c324da
 	/* Clean up. */
7e7e3f2
diff -up openssl-1.0.1k/apps/verify.c.trusted-first openssl-1.0.1k/apps/verify.c
7e7e3f2
--- openssl-1.0.1k/apps/verify.c.trusted-first	2015-01-08 15:00:36.000000000 +0100
7e7e3f2
+++ openssl-1.0.1k/apps/verify.c	2015-01-09 10:19:45.478779501 +0100
9c324da
@@ -237,7 +237,7 @@ int MAIN(int argc, char **argv)
9c324da
 
9c324da
 end:
9c324da
 	if (ret == 1) {
9c324da
-		BIO_printf(bio_err,"usage: verify [-verbose] [-CApath path] [-CAfile file] [-purpose purpose] [-crl_check]");
9c324da
+		BIO_printf(bio_err,"usage: verify [-verbose] [-CApath path] [-CAfile file] [-trusted_first] [-purpose purpose] [-crl_check]");
9c324da
 		BIO_printf(bio_err," [-attime timestamp]");
9c324da
 #ifndef OPENSSL_NO_ENGINE
9c324da
 		BIO_printf(bio_err," [-engine e]");
7e7e3f2
diff -up openssl-1.0.1k/crypto/x509/x509_vfy.c.trusted-first openssl-1.0.1k/crypto/x509/x509_vfy.c
7e7e3f2
--- openssl-1.0.1k/crypto/x509/x509_vfy.c.trusted-first	2015-01-09 10:19:45.443778710 +0100
7e7e3f2
+++ openssl-1.0.1k/crypto/x509/x509_vfy.c	2015-01-09 10:19:45.479779524 +0100
9c324da
@@ -207,6 +207,21 @@ int X509_verify_cert(X509_STORE_CTX *ctx
30aa930
 
30aa930
 		/* If we are self signed, we break */
30aa930
 		if (ctx->check_issued(ctx, x,x)) break;
30aa930
+		/* If asked see if we can find issuer in trusted store first */
30aa930
+		if (ctx->param->flags & X509_V_FLAG_TRUSTED_FIRST)
30aa930
+			{
30aa930
+			ok = ctx->get_issuer(&xtmp, ctx, x);
30aa930
+			if (ok < 0)
30aa930
+				return ok;
30aa930
+			/* If successful for now free up cert so it
30aa930
+			 * will be picked up again later.
30aa930
+			 */
30aa930
+			if (ok > 0)
30aa930
+				{
30aa930
+				X509_free(xtmp);
30aa930
+				break;
30aa930
+				}
30aa930
+			}
30aa930
 
30aa930
 		/* If we were passed a cert chain, use it first */
30aa930
 		if (ctx->untrusted != NULL)
7e7e3f2
diff -up openssl-1.0.1k/crypto/x509/x509_vfy.h.trusted-first openssl-1.0.1k/crypto/x509/x509_vfy.h
7e7e3f2
--- openssl-1.0.1k/crypto/x509/x509_vfy.h.trusted-first	2015-01-09 10:19:45.266774706 +0100
7e7e3f2
+++ openssl-1.0.1k/crypto/x509/x509_vfy.h	2015-01-09 10:19:45.479779524 +0100
30aa930
@@ -389,6 +389,8 @@ void X509_STORE_CTX_set_depth(X509_STORE
30aa930
 #define X509_V_FLAG_USE_DELTAS			0x2000
30aa930
 /* Check selfsigned CA signature */
30aa930
 #define X509_V_FLAG_CHECK_SS_SIGNATURE		0x4000
30aa930
+/* Use trusted store first */
30aa930
+#define X509_V_FLAG_TRUSTED_FIRST		0x8000
30aa930
 
30aa930
 
30aa930
 #define X509_VP_FLAG_DEFAULT			0x1
7e7e3f2
diff -up openssl-1.0.1k/doc/apps/cms.pod.trusted-first openssl-1.0.1k/doc/apps/cms.pod
7e7e3f2
--- openssl-1.0.1k/doc/apps/cms.pod.trusted-first	2015-01-08 15:00:36.000000000 +0100
7e7e3f2
+++ openssl-1.0.1k/doc/apps/cms.pod	2015-01-09 10:19:45.479779524 +0100
9c324da
@@ -35,6 +35,7 @@ B<openssl> B<cms>
9c324da
 [B<-print>]
9c324da
 [B<-CAfile file>]
9c324da
 [B<-CApath dir>]
9c324da
+[B<-trusted_first>]
9c324da
 [B<-md digest>]
9c324da
 [B<-[cipher]>]
9c324da
 [B<-nointern>]
a78828f
@@ -243,6 +244,12 @@ B<-verify>. This directory must be a sta
9c324da
 is a hash of each subject name (using B<x509 -hash>) should be linked
9c324da
 to each certificate.
9c324da
 
9c324da
+=item B<-trusted_first>
9c324da
+
9c324da
+Use certificates in CA file or CA directory before untrusted certificates
9c324da
+from the message when building the trust chain to verify certificates.
9c324da
+This is mainly useful in environments with Bridge CA or Cross-Certified CAs.
9c324da
+
9c324da
 =item B<-md digest>
9c324da
 
9c324da
 digest algorithm to use when signing or resigning. If not present then the
7e7e3f2
diff -up openssl-1.0.1k/doc/apps/ocsp.pod.trusted-first openssl-1.0.1k/doc/apps/ocsp.pod
7e7e3f2
--- openssl-1.0.1k/doc/apps/ocsp.pod.trusted-first	2015-01-09 10:19:45.436778551 +0100
7e7e3f2
+++ openssl-1.0.1k/doc/apps/ocsp.pod	2015-01-09 10:19:45.479779524 +0100
9c324da
@@ -29,6 +29,7 @@ B<openssl> B<ocsp>
9c324da
 [B<-path>]
9c324da
 [B<-CApath dir>]
9c324da
 [B<-CAfile file>]
9c324da
+[B<-trusted_first>]
9c324da
 [B<-VAfile file>]
9c324da
 [B<-validity_period n>]
9c324da
 [B<-status_age n>]
7e7e3f2
@@ -142,6 +143,13 @@ connection timeout to the OCSP responder
9c324da
 file or pathname containing trusted CA certificates. These are used to verify
9c324da
 the signature on the OCSP response.
9c324da
 
9c324da
+=item B<-trusted_first>
9c324da
+
9c324da
+Use certificates in CA file or CA directory over certificates provided
9c324da
+in the response or residing in other certificates file when building the trust
9c324da
+chain to verify responder certificate.
9c324da
+This is mainly useful in environments with Bridge CA or Cross-Certified CAs.
9c324da
+
9c324da
 =item B<-verify_other file>
9c324da
 
9c324da
 file containing additional certificates to search when attempting to locate
7e7e3f2
diff -up openssl-1.0.1k/doc/apps/s_client.pod.trusted-first openssl-1.0.1k/doc/apps/s_client.pod
7e7e3f2
--- openssl-1.0.1k/doc/apps/s_client.pod.trusted-first	2015-01-09 10:19:45.451778890 +0100
7e7e3f2
+++ openssl-1.0.1k/doc/apps/s_client.pod	2015-01-09 10:19:45.479779524 +0100
a78828f
@@ -19,6 +19,7 @@ B<openssl> B<s_client>
9c324da
 [B<-pass arg>]
9c324da
 [B<-CApath directory>]
9c324da
 [B<-CAfile filename>]
9c324da
+[B<-trusted_first>]
9c324da
 [B<-reconnect>]
9c324da
 [B<-pause>]
9c324da
 [B<-showcerts>]
a78828f
@@ -121,7 +122,7 @@ also used when building the client certi
9c324da
 A file containing trusted certificates to use during server authentication
9c324da
 and to use when attempting to build the client certificate chain.
9c324da
 
9c324da
-=item B<-purpose, -ignore_critical, -issuer_checks, -crl_check, -crl_check_all, -policy_check, -extended_crl, -x509_strict, -policy -check_ss_sig>
9c324da
+=item B<-purpose, -ignore_critical, -issuer_checks, -crl_check, -crl_check_all, -policy_check, -extended_crl, -x509_strict, -policy -check_ss_sig, -trusted_first>
9c324da
 
9c324da
 Set various certificate chain valiadition option. See the
9c324da
 L<B<verify>|verify(1)> manual page for details.
7e7e3f2
diff -up openssl-1.0.1k/doc/apps/smime.pod.trusted-first openssl-1.0.1k/doc/apps/smime.pod
7e7e3f2
--- openssl-1.0.1k/doc/apps/smime.pod.trusted-first	2015-01-08 15:00:36.000000000 +0100
7e7e3f2
+++ openssl-1.0.1k/doc/apps/smime.pod	2015-01-09 10:19:45.479779524 +0100
9c324da
@@ -15,6 +15,9 @@ B<openssl> B<smime>
9c324da
 [B<-pk7out>]
9c324da
 [B<-[cipher]>]
9c324da
 [B<-in file>]
9c324da
+[B<-CAfile file>]
9c324da
+[B<-CApath dir>]
9c324da
+[B<-trusted_first>]
9c324da
 [B<-certfile file>]
9c324da
 [B<-signer file>]
9c324da
 [B<-recip  file>]
9c324da
@@ -146,6 +149,12 @@ B<-verify>. This directory must be a sta
9c324da
 is a hash of each subject name (using B<x509 -hash>) should be linked
9c324da
 to each certificate.
9c324da
 
9c324da
+=item B<-trusted_first>
9c324da
+
9c324da
+Use certificates in CA file or CA directory over certificates provided
9c324da
+in the message when building the trust chain to verify a certificate.
9c324da
+This is mainly useful in environments with Bridge CA or Cross-Certified CAs.
9c324da
+
9c324da
 =item B<-md digest>
9c324da
 
9c324da
 digest algorithm to use when signing or resigning. If not present then the
7e7e3f2
diff -up openssl-1.0.1k/doc/apps/s_server.pod.trusted-first openssl-1.0.1k/doc/apps/s_server.pod
7e7e3f2
--- openssl-1.0.1k/doc/apps/s_server.pod.trusted-first	2015-01-09 10:19:45.451778890 +0100
7e7e3f2
+++ openssl-1.0.1k/doc/apps/s_server.pod	2015-01-09 10:19:45.479779524 +0100
9c324da
@@ -33,6 +33,7 @@ B<openssl> B<s_server>
9c324da
 [B<-state>]
9c324da
 [B<-CApath directory>]
9c324da
 [B<-CAfile filename>]
9c324da
+[B<-trusted_first>]
9c324da
 [B<-nocert>]
9c324da
 [B<-cipher cipherlist>]
a78828f
 [B<-serverpref>]
a78828f
@@ -178,6 +179,12 @@ and to use when attempting to build the
9c324da
 is also used in the list of acceptable client CAs passed to the client when
9c324da
 a certificate is requested.
9c324da
 
9c324da
+=item B<-trusted_first>
9c324da
+
9c324da
+Use certificates in CA file or CA directory before other certificates 
9c324da
+when building the trust chain to verify client certificates.
9c324da
+This is mainly useful in environments with Bridge CA or Cross-Certified CAs.
9c324da
+
9c324da
 =item B<-state>
9c324da
 
9c324da
 prints out the SSL session states.
7e7e3f2
diff -up openssl-1.0.1k/doc/apps/s_time.pod.trusted-first openssl-1.0.1k/doc/apps/s_time.pod
7e7e3f2
--- openssl-1.0.1k/doc/apps/s_time.pod.trusted-first	2015-01-08 15:00:36.000000000 +0100
7e7e3f2
+++ openssl-1.0.1k/doc/apps/s_time.pod	2015-01-09 10:19:45.480779546 +0100
9c324da
@@ -14,6 +14,7 @@ B<openssl> B<s_time>
9c324da
 [B<-key filename>]
9c324da
 [B<-CApath directory>]
9c324da
 [B<-CAfile filename>]
9c324da
+[B<-trusted_first>]
9c324da
 [B<-reuse>]
9c324da
 [B<-new>]
9c324da
 [B<-verify depth>]
9c324da
@@ -76,6 +77,12 @@ also used when building the client certi
9c324da
 A file containing trusted certificates to use during server authentication
9c324da
 and to use when attempting to build the client certificate chain.
9c324da
 
9c324da
+=item B<-trusted_first>
9c324da
+
9c324da
+Use certificates in CA file or CA directory over the certificates provided
9c324da
+by the server when building the trust chain to verify server certificate.
9c324da
+This is mainly useful in environments with Bridge CA or Cross-Certified CAs.
9c324da
+
9c324da
 =item B<-new>
9c324da
 
9c324da
 performs the timing test using a new session ID for each connection.
7e7e3f2
diff -up openssl-1.0.1k/doc/apps/ts.pod.trusted-first openssl-1.0.1k/doc/apps/ts.pod
7e7e3f2
--- openssl-1.0.1k/doc/apps/ts.pod.trusted-first	2014-10-15 15:49:15.000000000 +0200
7e7e3f2
+++ openssl-1.0.1k/doc/apps/ts.pod	2015-01-09 10:19:45.480779546 +0100
9c324da
@@ -46,6 +46,7 @@ B<-verify>
9c324da
 [B<-token_in>]
9c324da
 [B<-CApath> trusted_cert_path]
9c324da
 [B<-CAfile> trusted_certs.pem]
9c324da
+[B<-trusted_first>]
9c324da
 [B<-untrusted> cert_file.pem]
9c324da
 
9c324da
 =head1 DESCRIPTION
9c324da
@@ -324,6 +325,12 @@ L<verify(1)|verify(1)> for additional de
9c324da
 or B<-CApath> must be specified.
9c324da
 (Optional)
9c324da
 
9c324da
+=item B<-trusted_first>
9c324da
+
9c324da
+Use certificates in CA file or CA directory before other certificates
9c324da
+when building the trust chain to verify certificates.
9c324da
+This is mainly useful in environments with Bridge CA or Cross-Certified CAs.
9c324da
+
9c324da
 =item B<-untrusted> cert_file.pem
9c324da
 
9c324da
 Set of additional untrusted certificates in PEM format which may be
7e7e3f2
diff -up openssl-1.0.1k/doc/apps/verify.pod.trusted-first openssl-1.0.1k/doc/apps/verify.pod
7e7e3f2
--- openssl-1.0.1k/doc/apps/verify.pod.trusted-first	2015-01-08 15:00:36.000000000 +0100
7e7e3f2
+++ openssl-1.0.1k/doc/apps/verify.pod	2015-01-09 10:19:45.480779546 +0100
9c324da
@@ -9,6 +9,7 @@ verify - Utility to verify certificates.
9c324da
 B<openssl> B<verify>
9c324da
 [B<-CApath directory>]
9c324da
 [B<-CAfile file>]
9c324da
+[B<-trusted_first>]
9c324da
 [B<-purpose purpose>]
9c324da
 [B<-policy arg>]
9c324da
 [B<-ignore_critical>]
9c324da
@@ -56,6 +57,12 @@ in PEM format concatenated together.
9c324da
 A file of untrusted certificates. The file should contain multiple certificates
9c324da
 in PEM format concatenated together.
9c324da
 
9c324da
+=item B<-trusted_first>
9c324da
+
9c324da
+Use certificates in CA file or CA directory before the certificates in the untrusted
9c324da
+file when building the trust chain to verify certificates.
9c324da
+This is mainly useful in environments with Bridge CA or Cross-Certified CAs.
9c324da
+
9c324da
 =item B<-purpose purpose>
9c324da
 
9c324da
 The intended use for the certificate. If this option is not specified,