Top Description Inners Fields Constructors Methods
com.oracle.bmc.http.client.pki

public abstract Class Pem

extends Object
Class Inheritance
Imports
java.io.ByteArrayInputStream, .IOException, .InputStream, java.nio.ByteBuffer, java.nio.channels.Channels, .ReadableByteChannel, .WritableByteChannel, java.nio.charset.StandardCharsets, java.security.InvalidAlgorithmParameterException, .InvalidKeyException, .KeyFactory, .NoSuchAlgorithmException, .PrivateKey, .PublicKey, .SecureRandom, java.security.cert.Certificate, .CertificateEncodingException, .CertificateException, .CertificateFactory, java.security.spec.InvalidKeySpecException, .PKCS8EncodedKeySpec, .X509EncodedKeySpec, java.util.Arrays, .Base64, .Collection, .Iterator, .Objects, java.util.function.Function, java.util.regex.Matcher, .Pattern, javax.crypto.BadPaddingException, .Cipher, .EncryptedPrivateKeyInfo, .IllegalBlockSizeException, .NoSuchPaddingException, .SecretKey, .SecretKeyFactory, javax.crypto.spec.PBEKeySpec

Services for working with PEM encoded private keys, public keys and certificates.

Read a public key

PublicKey readPublicKey(Path path) {
 try (ReadableByteChannel pem = Files.newByteChannel(path)) {
  return Pem.decoder()
            .decodePublicKey(pem);
  }
}

Read an unencrypted private key

PrivateKey readPrivateKey(Path path) {
 try (ReadableByteChannel pem = Files.newByteChannel(path)) {
  return Pem.decoder()
            .decodePrivateKey(pem);
  }
}

Read an encrypted private key

PrivateKey readPrivateKey(Path path, char[] passphrase) {
 try (ReadableByteChannel pem = Files.newByteChannel(path)) {
  return Pem.decoder()
            .with(passphrase)
            .decodePrivateKey(pem);
  }
}

Write a public key

void writePublicKey(Path path,final PublicKey publicKey) {
 try (WritableByteChannel pem = Files.newByteChannel(path)) {
  return Pem.encoder()
            .write(pem,publicKey);
  }
}

Write an unencrypted private key

void writePrivateKey(Path path,final PrivateKey privateKey) {
 try (WritableByteChannel pem = Files.newByteChannel(path)) {
  return Pem.encoder()
            .write(pem,privateKey);
  }
}

Write an encrypted private key in PKCS1 format

void writePkcs1PrivateKey(Path path,final PrivateKey privateKey, char[] passphrase) {
 try (WritableByteChannel pem = Files.newByteChannel(path)) {
  return Pem.encoder()
            .with(Pem.Format.LEGACY)
            .with(passphrase)
            .write(pem,privateKey);
  }
}

Nested and Inner Type Summary

Modifier and TypeClass and Description
private static class
public static class
Pem.Decoder

Decodes PEM encoded text streams into the desired format

public static class
Pem.Encoder

Encode certificates, public keys and private keys into PEM encoded format

public static class
Pem.Encryption

Configures how private keys are encrypted

public static enum
Pem.Format

Denotes the specific syntax used for encoding public and private keys (Does not affect the encoding of certificates)

private static class
public static interface
Pem.Passphrase

Holds a passphrase using a try-with-resources model to assure the passphrase plain-text is erased once it has been used

pack-priv static enum
Pem.Type

Denotes the type of content encoded in the PEM message

Field Summary

Modifier and TypeField and Description
private static final String
private static final Pem.Decoder
private static final Pem.Encoder
private static final String
private static final int
private static final int

Constructor Summary

AccessConstructor and Description
private
Pem()

Method Summary

Modifier and TypeMethod and Description
private static byte[]

Returns:

PKCS8 encoded bytes
adaptPkcs1PrivateKey
(final byte[]
PKCS1 encoded bytes
pkcs1
)

Converts PKCS1 to PKCS8 by adding an ASN.1 header to identify the algorithm used to encode the payload (PKCS1 supports RSA only, PKCS8 supports different algorithms).

private static byte[]

Returns:

X509 Public KeyInfo der encoded bytes
adaptPkcs1PublicKey
(final byte[]
PKCS1 encoded bytes
pkcs1
)

Converts PKCS1 to X509 Public KeyInfo by adding an ASN.1 header to identify the algorithm used to encode the payload (PKCS1 supports RSA only, X509 supports different algorithms).

private static byte[]

Returns:

PKCS1 encoded representation of the key
asPkcs1PrivateKey
(byte[]
encoded bytes
pkcs8
)

All Java private keys are PKCS8 encoded.

private static byte[]

Returns:

PKCS1 encoded representation of the key
asPkcs1PublicKey
(byte[]
X509 encoded public key bytes
x509
)

All Java public keys are X509 encoded.

private static byte[]
cryptPkcs1(int mode, byte[] content, Pkcs1EncryptedPrivateKeyInfo encryptedPrivateKeyInfo, Pem.Passphrase passphrase)

private static Pem.Decoder
decoder(final String certificateType, final String keyPairAlgorithm)

public static Pem.Decoder

Returns:

Decoder instance
decoder
()

Return a decoder for certificates, public keys and unencrypted private keys.

public static Pem.Encoder

Returns:

Encoder instance
encoder
()

Return an encoder that does not encrypt private keys and formats private keys in PKCS8, and public keys or certificates in X509.

private static byte[]
encryptPkcs1(byte[] der, Pem.Encryption encryption)

private static void
erase(final byte[]
The byte array to erase
bytes
)

Erase (zero out) the contents of a byte array

private static byte[]

Returns:

The concatenated array
join
(final byte[]
The first array
first
,
final byte[]
The second array
second
)

Concatenate two byte arrays

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait