@@ -64,7 +64,11 @@ public WolfSSLTrustX509(KeyStore in) {
6464
6565 /**
6666 * Sort provided certificate chain by subject and issuer.
67- * Begin with leaf cert, end with last most intermediate cert.
67+ *
68+ * Begin with leaf cert, end with last most intermediate cert. Current
69+ * routine assumes that peer cert will be first in the provided certs
70+ * array, and will use that as a base/starting point to sort intermediate
71+ * certs going up the chain.
6872 *
6973 * @param certs Peer certificate chain, assuming leaf/peer is first
7074 *
@@ -84,9 +88,15 @@ private X509Certificate[] sortCertChainBySubjectIssuer(
8488 throw new CertificateException ("Input cert chain null" );
8589 }
8690
91+ /* If certs array is only one cert (peer), just return copy of it */
92+ if (certs .length == 1 ) {
93+ return certs .clone ();
94+ }
95+
8796 /* Make copy of peer cert chain, so we don't change original */
8897 chain = certs .clone ();
8998
99+ /* Print out chain for debugging */
90100 WolfSSLDebug .log (getClass (), WolfSSLDebug .INFO ,
91101 "sorting peer chain (" + chain .length + " certs):" );
92102 for (i = 0 ; i < chain .length ; i ++) {
@@ -119,6 +129,7 @@ private X509Certificate[] sortCertChainBySubjectIssuer(
119129 }
120130 }
121131
132+ /* Print out sorted peer chain for debugging */
122133 WolfSSLDebug .log (getClass (), WolfSSLDebug .INFO ,
123134 "sorted peer chain (" + (curr + 1 ) + " certs):" );
124135 for (i = 0 ; i <= curr ; i ++) {
@@ -141,6 +152,9 @@ private X509Certificate[] sortCertChainBySubjectIssuer(
141152 * Finds and returns X509Certificate matching the root CA that will
142153 * verify the given leaf/intermediate certificate.
143154 *
155+ * This will search through the provided KeyStore for the approproate
156+ * root CA that correctly verifies the given certificate.
157+ *
144158 * @param cert Certificate for which to find verifying root CA
145159 * @param ks KeyStore to search in for root CA
146160 *
@@ -160,11 +174,14 @@ private X509Certificate findRootCAFromKeyStoreForCert(X509Certificate cert,
160174 boolean rootFound = false ;
161175
162176 if (cert == null || ks == null ) {
163- throw new CertificateException ("cert or keystore is null" );
177+ throw new CertificateException ("Certificate or KeyStore is null" );
164178 }
165179
166180 /* Issuer name we need to match */
167181 X500Principal issuer = cert .getIssuerX500Principal ();
182+ if (issuer == null ) {
183+ throw new CertificateException ("Unable to get expected issuer" );
184+ }
168185
169186 WolfSSLDebug .log (getClass (), WolfSSLDebug .INFO ,
170187 "Searching KeyStore for root CA matching: " + issuer .getName ());
@@ -255,6 +272,7 @@ private X509Certificate findRootCAFromKeyStoreForCert(X509Certificate cert,
255272 }
256273 }
257274
275+ /* Free native WolfSSLCertManager resources */
258276 cm .free ();
259277
260278 if (rootFound == true ) {
0 commit comments