/** * Creates a {@link PemX509Certificate} from raw {@code byte[]}. * * ATTENTION: It's assumed that the given argument is a PEM/PKCS#8 encoded value. * No input validation is performed to validate it. */ public static PemX509Certificate valueOf(byte[] key) { return valueOf(Unpooled.wrappedBuffer(key)); }
/** * Creates a {@link PemX509Certificate} from raw {@code ByteBuf}. * * ATTENTION: It's assumed that the given argument is a PEM/PKCS#8 encoded value. * No input validation is performed to validate it. */ public static PemX509Certificate valueOf(ByteBuf key) { return new PemX509Certificate(key); }
@Override public PemX509Certificate copy() { return replace(content.copy()); }
/** * Return the pointer to a <a href="https://www.openssl.org/docs/crypto/BIO_get_mem_ptr.html">in-memory BIO</a> * or {@code 0} if the {@code certChain} is {@code null}. The BIO contains the content of the {@code certChain}. */ static long toBIO(ByteBufAllocator allocator, X509Certificate... certChain) throws Exception { if (certChain == null) { return 0; } if (certChain.length == 0) { throw new IllegalArgumentException("certChain can't be empty"); } PemEncoded pem = PemX509Certificate.toPEM(allocator, true, certChain); try { return toBIO(allocator, pem.retain()); } finally { pem.release(); } }
/** * Appends the {@link PemEncoded} value to the {@link ByteBuf} (last arg) and returns it. * If the {@link ByteBuf} didn't exist yet it'll create it using the {@link ByteBufAllocator}. */ private static ByteBuf append(ByteBufAllocator allocator, boolean useDirect, PemEncoded encoded, int count, ByteBuf pem) { ByteBuf content = encoded.content(); if (pem == null) { // see the other append() method pem = newBuffer(allocator, useDirect, content.readableBytes() * count); } pem.writeBytes(content.slice()); return pem; }
@Override public ByteBuf content() { int count = refCnt(); if (count <= 0) { throw new IllegalReferenceCountException(count); } return content; }
@Override public PemX509Certificate duplicate() { return replace(content.duplicate()); }
/** * Return the pointer to a <a href="https://www.openssl.org/docs/crypto/BIO_get_mem_ptr.html">in-memory BIO</a> * or {@code 0} if the {@code certChain} is {@code null}. The BIO contains the content of the {@code certChain}. */ static long toBIO(ByteBufAllocator allocator, X509Certificate... certChain) throws Exception { if (certChain == null) { return 0; } if (certChain.length == 0) { throw new IllegalArgumentException("certChain can't be empty"); } PemEncoded pem = PemX509Certificate.toPEM(allocator, true, certChain); try { return toBIO(allocator, pem.retain()); } finally { pem.release(); } }
/** * Appends the {@link X509Certificate} value to the {@link ByteBuf} (last arg) and returns it. * If the {@link ByteBuf} didn't exist yet it'll create it using the {@link ByteBufAllocator}. */ private static ByteBuf append(ByteBufAllocator allocator, boolean useDirect, X509Certificate cert, int count, ByteBuf pem) throws CertificateEncodingException { ByteBuf encoded = Unpooled.wrappedBuffer(cert.getEncoded()); try { ByteBuf base64 = SslUtils.toBase64(allocator, encoded); try { if (pem == null) { // We try to approximate the buffer's initial size. The sizes of // certificates can vary a lot so it'll be off a bit depending // on the number of elements in the array (count argument). pem = newBuffer(allocator, useDirect, (BEGIN_CERT.length + base64.readableBytes() + END_CERT.length) * count); } pem.writeBytes(BEGIN_CERT); pem.writeBytes(base64); pem.writeBytes(END_CERT); } finally { base64.release(); } } finally { encoded.release(); } return pem; }
@Override public ByteBuf content() { int count = refCnt(); if (count <= 0) { throw new IllegalReferenceCountException(count); } return content; }
@Override public PemX509Certificate retainedDuplicate() { return replace(content.retainedDuplicate()); }
try { encoded = PemX509Certificate.toPEM(ByteBufAllocator.DEFAULT, true, keyCertChain); keyCertChainBio = toBIO(ByteBufAllocator.DEFAULT, encoded.retain()); keyCertChainBio2 = toBIO(ByteBufAllocator.DEFAULT, encoded.retain());
@Override public PemX509Certificate replace(ByteBuf content) { return new PemX509Certificate(content); }
/** * Appends the {@link PemEncoded} value to the {@link ByteBuf} (last arg) and returns it. * If the {@link ByteBuf} didn't exist yet it'll create it using the {@link ByteBufAllocator}. */ private static ByteBuf append(ByteBufAllocator allocator, boolean useDirect, PemEncoded encoded, int count, ByteBuf pem) { ByteBuf content = encoded.content(); if (pem == null) { // see the other append() method pem = newBuffer(allocator, useDirect, content.readableBytes() * count); } pem.writeBytes(content.slice()); return pem; }
public static X509Certificate readCertificate(Path path) throws IOException { final byte[] bytes = Files.readAllBytes(path); return PemX509Certificate.valueOf(bytes); }
@Override public ByteBuf content() { int count = refCnt(); if (count <= 0) { throw new IllegalReferenceCountException(count); } return content; }
@Override public PemX509Certificate duplicate() { return replace(content.duplicate()); }
try { encoded = PemX509Certificate.toPEM(ByteBufAllocator.DEFAULT, true, keyCertChain); keyCertChainBio = toBIO(ByteBufAllocator.DEFAULT, encoded.retain()); keyCertChainBio2 = toBIO(ByteBufAllocator.DEFAULT, encoded.retain());