- } else if (!isSignedBy(last_cert, this_cert.getSubjectPublicKeyInfo()))
- throw new SSLException("the server sent a broken chain of certificates");
+ } else {
+
+ // don't check the top cert since some very old root certs lack a BasicConstraints field.
+ if (certlen + 3 + i < numcertbytes) {
+ // defend against Mike Benham's attack
+ X509Extension basicConstraints = this_cert.getTBSCertificate().getExtensions().getExtension(X509Extensions.BasicConstraints);
+ if (basicConstraints == null) throw new SSLException("certificate did not contain a basic constraints block");
+ DERInputStream dis = new DERInputStream(new ByteArrayInputStream(basicConstraints.getValue().getOctets()));
+ BasicConstraints bc = new BasicConstraints((DERConstructedSequence)dis.readObject());
+ if (!bc.isCA()) throw new SSLException("non-CA certificate used for signing");
+ }
+
+ if (!isSignedBy(last_cert, this_cert.getSubjectPublicKeyInfo()))
+ throw new SSLException("the server sent a broken chain of certificates");
+ }