Skip to content

Commit 5948162

Browse files
committed
JSSE: refactor X509Certificate.getPublicKey() to use JCE classes to generate PublicKey, fixes compatibility with wolfJCE underneath
1 parent 45bc899 commit 5948162

2 files changed

Lines changed: 30 additions & 66 deletions

File tree

src/java/com/wolfssl/provider/jsse/WolfSSLX509.java

Lines changed: 29 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,10 @@
2929
import java.security.Provider;
3030
import java.security.PublicKey;
3131
import java.security.Signature;
32+
import java.security.KeyFactory;
3233
import java.security.SignatureException;
34+
import java.security.spec.X509EncodedKeySpec;
35+
import java.security.spec.InvalidKeySpecException;
3336
import java.security.cert.CertificateEncodingException;
3437
import java.security.cert.CertificateException;
3538
import java.security.cert.CertificateExpiredException;
@@ -449,7 +452,7 @@ public void verify(PublicKey key, Provider p)
449452
sig.initVerify(key);
450453
sig.update(this.getTBSCertificate());
451454
} catch (Exception e) {
452-
throw new CertificateException();
455+
throw new CertificateException(e);
453456
}
454457

455458
if (sig.verify(this.getSignature()) == false) {
@@ -487,20 +490,41 @@ public void free() {
487490
@Override
488491
public PublicKey getPublicKey() {
489492

493+
String type = null;
494+
byte[] der = null;
495+
KeyFactory kf = null;
496+
PublicKey key = null;
497+
X509EncodedKeySpec spec = null;
498+
490499
WolfSSLDebug.log(getClass(), WolfSSLDebug.INFO,
491500
"entered getPublicKey()");
492501

493502
if (this.cert == null) {
494503
return null;
495504
}
496-
String type = this.cert.getPubkeyType();
497-
byte[] der = this.cert.getPubkey();
505+
506+
type = this.cert.getPubkeyType();
507+
der = this.cert.getPubkey();
498508

499509
try {
500-
return new WolfSSLPubKey(der, type, "X.509");
501-
} catch (WolfSSLException e) {
510+
if (type.equals("RSA")) {
511+
kf = KeyFactory.getInstance("RSA");
512+
} else if (type.equals("ECC")) {
513+
kf = KeyFactory.getInstance("EC");
514+
} else if (type.equals("DSA")) {
515+
kf = KeyFactory.getInstance("DSA");
516+
}
517+
518+
if (kf != null) {
519+
spec = new X509EncodedKeySpec(der);
520+
key = (PublicKey)kf.generatePublic(spec);
521+
}
522+
523+
} catch (NoSuchAlgorithmException | InvalidKeySpecException e) {
502524
return null;
503525
}
526+
527+
return key;
504528
}
505529

506530
/* If unsupported critical extension is found then wolfSSL should not parse
@@ -585,66 +609,6 @@ protected void finalize() throws Throwable {
585609
}
586610
}
587611

588-
589-
/* wolfSSL public key class */
590-
private class WolfSSLPubKey implements PublicKey {
591-
/**
592-
* Default serial ID
593-
*/
594-
private static final long serialVersionUID = 1L;
595-
private byte[] encoding;
596-
private String type;
597-
private String format = "X.509";
598-
599-
/**
600-
* Creates a new public key class
601-
* @param der DER format key
602-
* @param type key type i.e. WolfSSL.RSAk
603-
* @param curveOID can be null in RSA case
604-
* @throws WolfSSLException
605-
*/
606-
private WolfSSLPubKey(byte[] der, String type, String format)
607-
throws WolfSSLException {
608-
this.format = format;
609-
this.encoding = der;
610-
if (this.encoding == null) {
611-
throw new WolfSSLException("Error creating key");
612-
}
613-
this.type = type;
614-
615-
WolfSSLDebug.log(getClass(), WolfSSLDebug.INFO,
616-
"created new WolfSSLPubKey");
617-
}
618-
619-
@Override
620-
public String getAlgorithm() {
621-
622-
WolfSSLDebug.log(getClass(), WolfSSLDebug.INFO,
623-
"entered getAlgorithm()");
624-
625-
return this.type;
626-
}
627-
628-
@Override
629-
public String getFormat() {
630-
631-
WolfSSLDebug.log(getClass(), WolfSSLDebug.INFO,
632-
"entered getFormat()");
633-
634-
return this.format;
635-
}
636-
637-
@Override
638-
public byte[] getEncoded() {
639-
640-
WolfSSLDebug.log(getClass(), WolfSSLDebug.INFO,
641-
"entered getEncoded()");
642-
643-
return this.encoding;
644-
}
645-
646-
}
647-
648612
/* wolfSSL Principal class */
649613
private class WolfSSLPrincipal implements Principal {
650614
private String name;

src/test/com/wolfssl/provider/jsse/test/WolfSSLX509Test.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -365,7 +365,6 @@ public void testVerifyProvider() {
365365
pass("\t\t\t... skipped");
366366
return;
367367
}
368-
System.out.print("\n\t Signature provider " + sigProvider.getName());
369368

370369
store = KeyStore.getInstance(tf.keyStoreType);
371370
stream = new FileInputStream(tf.allJKS);
@@ -403,6 +402,7 @@ public void testVerifyProvider() {
403402
} catch (KeyStoreException | NoSuchAlgorithmException |
404403
CertificateException | IOException | WolfSSLException e) {
405404
error("\t... failed");
405+
e.printStackTrace();
406406
fail("general failure");
407407
}
408408
pass("\t... passed");

0 commit comments

Comments
 (0)