@@ -133,7 +133,7 @@ public final class CertificateBuilder { |
133 | 133 | X500Principal subject; |
134 | 134 | boolean isCertificateAuthority; |
135 | 135 | OptionalInt pathLengthConstraint = OptionalInt.empty(); |
136 | | - PublicKey publicKey; |
| 136 | + KeyPair keyPair; |
137 | 137 | Set<String> extendedKeyUsage = new TreeSet<>(); |
138 | 138 | Extension keyUsage; |
139 | 139 |
|
@@ -163,7 +163,7 @@ public CertificateBuilder copy() { |
163 | 163 | copy.subject = subject; |
164 | 164 | copy.isCertificateAuthority = isCertificateAuthority; |
165 | 165 | copy.pathLengthConstraint = pathLengthConstraint; |
166 | | - copy.publicKey = publicKey; |
| 166 | + copy.keyPair = keyPair; |
167 | 167 | copy.keyUsage = keyUsage; |
168 | 168 | copy.extendedKeyUsage = new TreeSet<>(extendedKeyUsage); |
169 | 169 | copy.provider = provider; |
@@ -453,11 +453,42 @@ public CertificateBuilder rsa2048() { |
453 | 453 | * <p> |
454 | 454 | * If the given public key is {@code null} (the default) then a new key-pair will be generated instead. |
455 | 455 | * |
456 | | - * @param key The public key to wrap in a certificate. |
| 456 | + * @param publicKey The public key to wrap in a certificate. |
457 | 457 | * @return This certificate builder. |
458 | 458 | */ |
459 | | - public CertificateBuilder publicKey(PublicKey key) { |
460 | | - publicKey = key; |
| 459 | + public CertificateBuilder publicKey(PublicKey publicKey) { |
| 460 | + if (publicKey == null) { |
| 461 | + keyPair = null; |
| 462 | + } else { |
| 463 | + keyPair = new KeyPair(publicKey, null); |
| 464 | + } |
| 465 | + return this; |
| 466 | + } |
| 467 | + |
| 468 | + /** |
| 469 | + * Instruct the certificate builder to not generate its own key pair, but to instead create a certificate that |
| 470 | + * uses the given key pair. |
| 471 | + * <p> |
| 472 | + * This method is useful if you want to use an existing key-pair, e.g. to emulate a certificate authority |
| 473 | + * responding to a Certificate Signing Request (CSR), or when creating cross-signed certificates. |
| 474 | + * <p> |
| 475 | + * Cross-signing is when two certificates have the same subject and public key, but are signed by different keys. |
| 476 | + * In effect, it's the same logical certificate, but manifest as two different "concrete" certificate objects, i.e. |
| 477 | + * two different {@link X509Bundle} objects. Cross-signing can be done to both leaf certificates and to issuers, |
| 478 | + * and can create complicated certificate graphs. The technique is used for introducing new roots and issuers |
| 479 | + * to a PKI system, in a backwards compatible way where certificates can be trusted by peers that aren't familiar |
| 480 | + * with the new roots or issuers. |
| 481 | + * <p> |
| 482 | + * If the given key pair is {@code null} (the default) then a new key-pair will be generated instead. |
| 483 | + * |
| 484 | + * @param keyPair The key pair to use when creating a certificate. |
| 485 | + * @return This certificate builder. |
| 486 | + */ |
| 487 | + public CertificateBuilder keyPair(KeyPair keyPair) { |
| 488 | + if (keyPair != null && keyPair.getPublic() == null) { |
| 489 | + throw new IllegalArgumentException("The given key pair must have a public key"); |
| 490 | + } |
| 491 | + this.keyPair = keyPair; |
461 | 492 | return this; |
462 | 493 | } |
463 | 494 |
|
@@ -671,8 +702,8 @@ public CertificateBuilder addExtendedKeyUsageMicrosoftSmartcardLogin() { |
671 | 702 | * @throws Exception If something went wrong in the process. |
672 | 703 | */ |
673 | 704 | public X509Bundle buildSelfSigned() throws Exception { |
674 | | - if (publicKey != null) { |
675 | | - throw new IllegalStateException("Cannot create a self-signed certificate with a public key from a CSR."); |
| 705 | + if (keyPair != null && (keyPair.getPublic() == null || keyPair.getPrivate() == null)) { |
| 706 | + throw new IllegalStateException("Cannot create a self-signed certificate with an incomplete key pair."); |
676 | 707 | } |
677 | 708 | if (!algorithm.supportSigning()) { |
678 | 709 | throw new IllegalStateException("Cannot create a self-signed certificate with a " + |
@@ -709,10 +740,10 @@ public X509Bundle buildIssuedBy(X509Bundle issuerBundle) throws Exception { |
709 | 740 | */ |
710 | 741 | public X509Bundle buildIssuedBy(X509Bundle issuerBundle, String signAlg) throws Exception { |
711 | 742 | final KeyPair keyPair; |
712 | | - if (publicKey == null) { |
| 743 | + if (this.keyPair == null) { |
713 | 744 | keyPair = generateKeyPair(provider); |
714 | 745 | } else { |
715 | | - keyPair = new KeyPair(publicKey, null); |
| 746 | + keyPair = this.keyPair; |
716 | 747 | } |
717 | 748 |
|
718 | 749 | X500Principal issuerPrincipal = issuerBundle.getCertificate().getSubjectX500Principal(); |
|