|
29 | 29 | import java.security.Provider; |
30 | 30 | import java.security.PublicKey; |
31 | 31 | import java.security.Signature; |
| 32 | +import java.security.KeyFactory; |
32 | 33 | import java.security.SignatureException; |
| 34 | +import java.security.spec.X509EncodedKeySpec; |
| 35 | +import java.security.spec.InvalidKeySpecException; |
33 | 36 | import java.security.cert.CertificateEncodingException; |
34 | 37 | import java.security.cert.CertificateException; |
35 | 38 | import java.security.cert.CertificateExpiredException; |
@@ -449,7 +452,7 @@ public void verify(PublicKey key, Provider p) |
449 | 452 | sig.initVerify(key); |
450 | 453 | sig.update(this.getTBSCertificate()); |
451 | 454 | } catch (Exception e) { |
452 | | - throw new CertificateException(); |
| 455 | + throw new CertificateException(e); |
453 | 456 | } |
454 | 457 |
|
455 | 458 | if (sig.verify(this.getSignature()) == false) { |
@@ -487,20 +490,41 @@ public void free() { |
487 | 490 | @Override |
488 | 491 | public PublicKey getPublicKey() { |
489 | 492 |
|
| 493 | + String type = null; |
| 494 | + byte[] der = null; |
| 495 | + KeyFactory kf = null; |
| 496 | + PublicKey key = null; |
| 497 | + X509EncodedKeySpec spec = null; |
| 498 | + |
490 | 499 | WolfSSLDebug.log(getClass(), WolfSSLDebug.INFO, |
491 | 500 | "entered getPublicKey()"); |
492 | 501 |
|
493 | 502 | if (this.cert == null) { |
494 | 503 | return null; |
495 | 504 | } |
496 | | - String type = this.cert.getPubkeyType(); |
497 | | - byte[] der = this.cert.getPubkey(); |
| 505 | + |
| 506 | + type = this.cert.getPubkeyType(); |
| 507 | + der = this.cert.getPubkey(); |
498 | 508 |
|
499 | 509 | 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) { |
502 | 524 | return null; |
503 | 525 | } |
| 526 | + |
| 527 | + return key; |
504 | 528 | } |
505 | 529 |
|
506 | 530 | /* If unsupported critical extension is found then wolfSSL should not parse |
@@ -585,66 +609,6 @@ protected void finalize() throws Throwable { |
585 | 609 | } |
586 | 610 | } |
587 | 611 |
|
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 | | - |
648 | 612 | /* wolfSSL Principal class */ |
649 | 613 | private class WolfSSLPrincipal implements Principal { |
650 | 614 | private String name; |
|
0 commit comments