GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Add support for cross-signing to pkitesting (#16103) · netty/netty@e1adc35

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • pkitesting/src
    • main/java/io/netty/pkitesting
      • CertificateBuilder.java
      • X509Bundle.java
    • test/java/io/netty/pkitesting
      • CertificateBuilderTest.java
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎pkitesting/src/main/java/io/netty/pkitesting/CertificateBuilder.java‎

Copy file name to clipboardExpand all lines: pkitesting/src/main/java/io/netty/pkitesting/CertificateBuilder.java
+40-9Lines changed: 40 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ public final class CertificateBuilder {
133133
X500Principal subject;
134134
boolean isCertificateAuthority;
135135
OptionalInt pathLengthConstraint = OptionalInt.empty();
136-
PublicKey publicKey;
136+
KeyPair keyPair;
137137
Set<String> extendedKeyUsage = new TreeSet<>();
138138
Extension keyUsage;
139139

@@ -163,7 +163,7 @@ public CertificateBuilder copy() {
163163
copy.subject = subject;
164164
copy.isCertificateAuthority = isCertificateAuthority;
165165
copy.pathLengthConstraint = pathLengthConstraint;
166-
copy.publicKey = publicKey;
166+
copy.keyPair = keyPair;
167167
copy.keyUsage = keyUsage;
168168
copy.extendedKeyUsage = new TreeSet<>(extendedKeyUsage);
169169
copy.provider = provider;
@@ -453,11 +453,42 @@ public CertificateBuilder rsa2048() {
453453
* <p>
454454
* If the given public key is {@code null} (the default) then a new key-pair will be generated instead.
455455
*
456-
* @param key The public key to wrap in a certificate.
456+
* @param publicKey The public key to wrap in a certificate.
457457
* @return This certificate builder.
458458
*/
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;
461492
return this;
462493
}
463494

@@ -671,8 +702,8 @@ public CertificateBuilder addExtendedKeyUsageMicrosoftSmartcardLogin() {
671702
* @throws Exception If something went wrong in the process.
672703
*/
673704
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.");
676707
}
677708
if (!algorithm.supportSigning()) {
678709
throw new IllegalStateException("Cannot create a self-signed certificate with a " +
@@ -709,10 +740,10 @@ public X509Bundle buildIssuedBy(X509Bundle issuerBundle) throws Exception {
709740
*/
710741
public X509Bundle buildIssuedBy(X509Bundle issuerBundle, String signAlg) throws Exception {
711742
final KeyPair keyPair;
712-
if (publicKey == null) {
743+
if (this.keyPair == null) {
713744
keyPair = generateKeyPair(provider);
714745
} else {
715-
keyPair = new KeyPair(publicKey, null);
746+
keyPair = this.keyPair;
716747
}
717748

718749
X500Principal issuerPrincipal = issuerBundle.getCertificate().getSubjectX500Principal();
Collapse file

‎pkitesting/src/main/java/io/netty/pkitesting/X509Bundle.java‎

Copy file name to clipboardExpand all lines: pkitesting/src/main/java/io/netty/pkitesting/X509Bundle.java
+23Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
import java.security.cert.X509Certificate;
3737
import java.util.Arrays;
3838
import java.util.Base64;
39+
import java.util.LinkedHashSet;
3940
import java.util.List;
4041
import javax.net.ssl.KeyManagerFactory;
4142
import javax.net.ssl.TrustManager;
@@ -429,4 +430,26 @@ public KeyManagerFactory toKeyManagerFactory(String algorithm)
429430
kmf.init(toKeyStore(EmptyArrays.EMPTY_CHARS), EmptyArrays.EMPTY_CHARS);
430431
return kmf;
431432
}
433+
434+
/**
435+
* Create a new {@link X509Bundle} that has the leaf and root certificates of <em>this</em> bundle,
436+
* but a certificate path that is the combination all the intermediate certificates of both this and the given
437+
* bundle.
438+
* <p>
439+
* This is useful when building a bundle with a cross-signed certificate,
440+
* or when you just want to have additional unrelated intermediate certificates in the path.
441+
*
442+
* @param other The other bundle.
443+
* @return A new {@link X509Bundle} that has the leaf and root of this bundle,
444+
* but the combined intermediates of both bundles.
445+
*/
446+
public X509Bundle mergeIntermediates(X509Bundle other) {
447+
LinkedHashSet<X509Certificate> certs = new LinkedHashSet<>(Arrays.asList(certPath));
448+
X509Certificate[] otherPath = other.certPath;
449+
for (int i = 1; i < otherPath.length; i++) {
450+
X509Certificate intermediate = otherPath[i];
451+
certs.add(intermediate);
452+
}
453+
return fromCertificatePath(certs.toArray(EmptyArrays.EMPTY_X509_CERTIFICATES), root, keyPair);
454+
}
432455
}
Collapse file

‎pkitesting/src/test/java/io/netty/pkitesting/CertificateBuilderTest.java‎

Copy file name to clipboardExpand all lines: pkitesting/src/test/java/io/netty/pkitesting/CertificateBuilderTest.java
+41Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -425,6 +425,47 @@ void generateCertificateWithGivenPublicKey() throws Exception {
425425
assertThrows(NullPointerException.class, () -> bundle.getPrivateKeyPEM());
426426
}
427427

428+
@Test
429+
void generateCertificateWithGivenKeyPair() throws Exception {
430+
X509Bundle root = BASE.copy()
431+
.setIsCertificateAuthority(true)
432+
.setKeyUsage(true, KeyUsage.digitalSignature, KeyUsage.keyCertSign, KeyUsage.cRLSign)
433+
.buildSelfSigned();
434+
KeyPair keyPair = Algorithm.ecp256.generateKeyPair(new SecureRandom(), null);
435+
X509Bundle bundle = BASE.copy()
436+
.subject("CN=leaf.netty.io")
437+
.keyPair(keyPair)
438+
.buildIssuedBy(root);
439+
assertThat(bundle.getKeyPair().getPublic()).isSameAs(keyPair.getPublic());
440+
assertThat(bundle.getKeyPair().getPrivate()).isSameAs(keyPair.getPrivate());
441+
assertNotNull(bundle.getPrivateKeyPEM());
442+
}
443+
444+
@Test
445+
void authenticatingCrossSignedCertificate() throws Exception {
446+
CertificateBuilder caBuilder = BASE.copy()
447+
.setIsCertificateAuthority(true)
448+
.setKeyUsage(true, KeyUsage.digitalSignature, KeyUsage.keyCertSign, KeyUsage.cRLSign);
449+
450+
// Roots. Different subjects.
451+
X509Bundle oldRoot = caBuilder.subject("CN=root-a").buildSelfSigned();
452+
X509Bundle newRoot = caBuilder.subject("CN=root-b").buildSelfSigned();
453+
454+
// Intermediates. Same subject. Will be cross-signed.
455+
CertificateBuilder intermediateBuilder = caBuilder.copy().subject("CN=issuer.netty.io");
456+
X509Bundle oldIssuer = intermediateBuilder.buildIssuedBy(oldRoot);
457+
X509Bundle crossIssuer = intermediateBuilder.keyPair(oldIssuer.getKeyPair()).buildIssuedBy(newRoot);
458+
459+
// Leaf. Cross-signed by cross-signed issuer.
460+
CertificateBuilder leafBuilder = BASE.copy().subject("CN=leaf.netty.io").addExtendedKeyUsageClientAuth();
461+
X509Bundle leaf = leafBuilder.buildIssuedBy(crossIssuer)
462+
.mergeIntermediates(leafBuilder.buildIssuedBy(oldIssuer));
463+
464+
// This trust manager only knows about the old root. It will throw if it can't find a path to it.
465+
X509TrustManager oldRootTrustManager = (X509TrustManager) oldRoot.toTrustManager();
466+
oldRootTrustManager.checkClientTrusted(leaf.getCertificatePath(), "UNKNOWN");
467+
}
468+
428469
private static X509TrustManager getX509TrustManager(X509Bundle root) throws Exception {
429470
TrustManagerFactory tmf = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
430471
PKIXBuilderParameters params = new PKIXBuilderParameters(Collections.singleton(

0 commit comments

Comments
0 (0)
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.