mirror of https://github.com/shred/acme4j
Add method to write full certificate
parent
69fbe81e2d
commit
d6b4a43847
|
@ -113,12 +113,11 @@ public class ClientTest {
|
||||||
LOG.info("Certificate URL: " + certificate.getLocation());
|
LOG.info("Certificate URL: " + certificate.getLocation());
|
||||||
|
|
||||||
// Download the leaf certificate and certificate chain.
|
// Download the leaf certificate and certificate chain.
|
||||||
X509Certificate cert = certificate.download();
|
X509Certificate[] fullChain = certificate.downloadFullChain();
|
||||||
X509Certificate[] chain = certificate.downloadChain();
|
|
||||||
|
|
||||||
// Write a combined file containing the certificate and chain.
|
// Write a combined file containing the certificate and chain.
|
||||||
try (FileWriter fw = new FileWriter(DOMAIN_CHAIN_FILE)) {
|
try (FileWriter fw = new FileWriter(DOMAIN_CHAIN_FILE)) {
|
||||||
CertificateUtils.writeX509CertificateChain(fw, cert, chain);
|
CertificateUtils.writeX509Certificates(fw, fullChain);
|
||||||
}
|
}
|
||||||
|
|
||||||
// That's all! Configure your web server to use the DOMAIN_KEY_FILE and
|
// That's all! Configure your web server to use the DOMAIN_KEY_FILE and
|
||||||
|
|
|
@ -39,6 +39,7 @@ import org.bouncycastle.openssl.jcajce.JcaPEMWriter;
|
||||||
import org.bouncycastle.operator.OperatorCreationException;
|
import org.bouncycastle.operator.OperatorCreationException;
|
||||||
import org.bouncycastle.operator.jcajce.JcaContentSignerBuilder;
|
import org.bouncycastle.operator.jcajce.JcaContentSignerBuilder;
|
||||||
import org.bouncycastle.pkcs.PKCS10CertificationRequest;
|
import org.bouncycastle.pkcs.PKCS10CertificationRequest;
|
||||||
|
import org.shredzone.acme4j.Certificate;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Utility class offering convenience methods for certificates.
|
* Utility class offering convenience methods for certificates.
|
||||||
|
@ -107,7 +108,10 @@ public final class CertificateUtils {
|
||||||
* @param chain
|
* @param chain
|
||||||
* {@link X509Certificate} chain to add to the certificate. {@code null}
|
* {@link X509Certificate} chain to add to the certificate. {@code null}
|
||||||
* values are ignored, array may be empty.
|
* values are ignored, array may be empty.
|
||||||
|
* @deprecated use {@link Certificate#downloadFullChain()} and
|
||||||
|
* {@link #writeX509Certificates(Writer, X509Certificate[])} instead
|
||||||
*/
|
*/
|
||||||
|
@Deprecated
|
||||||
public static void writeX509CertificateChain(Writer w, X509Certificate cert, X509Certificate... chain)
|
public static void writeX509CertificateChain(Writer w, X509Certificate cert, X509Certificate... chain)
|
||||||
throws IOException {
|
throws IOException {
|
||||||
try (JcaPEMWriter jw = new JcaPEMWriter(w)) {
|
try (JcaPEMWriter jw = new JcaPEMWriter(w)) {
|
||||||
|
@ -118,6 +122,22 @@ public final class CertificateUtils {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Writes multiple X.509 certificates to a PEM file.
|
||||||
|
*
|
||||||
|
* @param w
|
||||||
|
* {@link Writer} to write the certificate chain to. The {@link Writer} is
|
||||||
|
* closed after use.
|
||||||
|
* @param certs
|
||||||
|
* {@link X509Certificate} certificates to add to the certificate.
|
||||||
|
* {@code null} values are ignored, array may be empty.
|
||||||
|
* @since 1.1
|
||||||
|
*/
|
||||||
|
public static void writeX509Certificates(Writer w, X509Certificate... certs)
|
||||||
|
throws IOException {
|
||||||
|
writeX509CertificateChain(w, null, certs);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Writes an {@link X509Certificate} unless it is {@code null}.
|
* Writes an {@link X509Certificate} unless it is {@code null}.
|
||||||
*
|
*
|
||||||
|
|
|
@ -87,6 +87,7 @@ public class CertificateUtilsTest {
|
||||||
* writes a correct chain.
|
* writes a correct chain.
|
||||||
*/
|
*/
|
||||||
@Test
|
@Test
|
||||||
|
@SuppressWarnings("deprecation")
|
||||||
public void testWriteX509CertificateChain() throws IOException, CertificateException {
|
public void testWriteX509CertificateChain() throws IOException, CertificateException {
|
||||||
X509Certificate leaf = createCertificate();
|
X509Certificate leaf = createCertificate();
|
||||||
X509Certificate chain1 = createCertificate();
|
X509Certificate chain1 = createCertificate();
|
||||||
|
@ -116,6 +117,25 @@ public class CertificateUtilsTest {
|
||||||
out = w.toString();
|
out = w.toString();
|
||||||
}
|
}
|
||||||
assertThat(countCertificates(out), is(3));
|
assertThat(countCertificates(out), is(3));
|
||||||
|
|
||||||
|
try (StringWriter w = new StringWriter()) {
|
||||||
|
CertificateUtils.writeX509Certificates(w, leaf);
|
||||||
|
out = w.toString();
|
||||||
|
}
|
||||||
|
assertThat(countCertificates(out), is(1));
|
||||||
|
|
||||||
|
try (StringWriter w = new StringWriter()) {
|
||||||
|
CertificateUtils.writeX509Certificates(w, leaf, chain1);
|
||||||
|
out = w.toString();
|
||||||
|
}
|
||||||
|
assertThat(countCertificates(out), is(2));
|
||||||
|
|
||||||
|
try (StringWriter w = new StringWriter()) {
|
||||||
|
CertificateUtils.writeX509Certificates(w,
|
||||||
|
new X509Certificate[] { leaf, chain1 });
|
||||||
|
out = w.toString();
|
||||||
|
}
|
||||||
|
assertThat(countCertificates(out), is(2));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -65,15 +65,7 @@ Most web servers, like _Apache_, _nginx_, but also other servers like _postfix_
|
||||||
|
|
||||||
```java
|
```java
|
||||||
try (FileWriter fw = new FileWriter("cert-chain.crt")) {
|
try (FileWriter fw = new FileWriter("cert-chain.crt")) {
|
||||||
CertificateUtils.writeX509CertificateChain(fw, cert, chain);
|
CertificateUtils.writeX509Certificates(fw, fullChain);
|
||||||
}
|
|
||||||
```
|
|
||||||
|
|
||||||
Alternatively:
|
|
||||||
|
|
||||||
```java
|
|
||||||
try (FileWriter fw = new FileWriter("cert-chain.crt")) {
|
|
||||||
CertificateUtils.writeX509CertificateChain(fw, null, fullChain);
|
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
@ -84,7 +76,7 @@ try (FileWriter fw = new FileWriter("cert.pem")) {
|
||||||
CertificateUtils.writeX509Certificate(cert, fw);
|
CertificateUtils.writeX509Certificate(cert, fw);
|
||||||
}
|
}
|
||||||
try (FileWriter fw = new FileWriter("chain.pem")) {
|
try (FileWriter fw = new FileWriter("chain.pem")) {
|
||||||
CertificateUtils.writeX509CertificateChain(fw, null, chain);
|
CertificateUtils.writeX509Certificates(fw, chain);
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue