Search in sources :

Example 1 with Section

use of com.google.api.client.util.PemReader.Section in project google-auth-library-java by google.

the class ServiceAccountCredentials method privateKeyFromPkcs8.

/**
 * Helper to convert from a PKCS#8 String to an RSA private key
 */
static PrivateKey privateKeyFromPkcs8(String privateKeyPkcs8) throws IOException {
    Reader reader = new StringReader(privateKeyPkcs8);
    Section section = PemReader.readFirstSectionAndClose(reader, "PRIVATE KEY");
    if (section == null) {
        throw new IOException("Invalid PKCS#8 data.");
    }
    byte[] bytes = section.getBase64DecodedBytes();
    PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(bytes);
    Exception unexpectedException;
    try {
        KeyFactory keyFactory = SecurityUtils.getRsaKeyFactory();
        return keyFactory.generatePrivate(keySpec);
    } catch (NoSuchAlgorithmException | InvalidKeySpecException exception) {
        unexpectedException = exception;
    }
    throw new IOException("Unexpected exception reading PKCS#8 data", unexpectedException);
}
Also used : PKCS8EncodedKeySpec(java.security.spec.PKCS8EncodedKeySpec) StringReader(java.io.StringReader) PemReader(com.google.api.client.util.PemReader) Reader(java.io.Reader) StringReader(java.io.StringReader) IOException(java.io.IOException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) InvalidKeySpecException(java.security.spec.InvalidKeySpecException) Section(com.google.api.client.util.PemReader.Section) InvalidKeySpecException(java.security.spec.InvalidKeySpecException) GeneralSecurityException(java.security.GeneralSecurityException) SignatureException(java.security.SignatureException) IOException(java.io.IOException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) InvalidKeyException(java.security.InvalidKeyException) KeyFactory(java.security.KeyFactory)

Example 2 with Section

use of com.google.api.client.util.PemReader.Section in project google-api-java-client by google.

the class GoogleCredential method privateKeyFromPkcs8.

@Beta
private static PrivateKey privateKeyFromPkcs8(String privateKeyPem) throws IOException {
    Reader reader = new StringReader(privateKeyPem);
    Section section = PemReader.readFirstSectionAndClose(reader, "PRIVATE KEY");
    if (section == null) {
        throw new IOException("Invalid PKCS8 data.");
    }
    byte[] bytes = section.getBase64DecodedBytes();
    PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(bytes);
    Exception unexpectedException = null;
    try {
        KeyFactory keyFactory = SecurityUtils.getRsaKeyFactory();
        PrivateKey privateKey = keyFactory.generatePrivate(keySpec);
        return privateKey;
    } catch (NoSuchAlgorithmException exception) {
        unexpectedException = exception;
    } catch (InvalidKeySpecException exception) {
        unexpectedException = exception;
    }
    throw OAuth2Utils.exceptionWithCause(new IOException("Unexpected exception reading PKCS data"), unexpectedException);
}
Also used : PrivateKey(java.security.PrivateKey) PKCS8EncodedKeySpec(java.security.spec.PKCS8EncodedKeySpec) StringReader(java.io.StringReader) PemReader(com.google.api.client.util.PemReader) Reader(java.io.Reader) StringReader(java.io.StringReader) FileReader(java.io.FileReader) IOException(java.io.IOException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) InvalidKeySpecException(java.security.spec.InvalidKeySpecException) Section(com.google.api.client.util.PemReader.Section) InvalidKeySpecException(java.security.spec.InvalidKeySpecException) GeneralSecurityException(java.security.GeneralSecurityException) IOException(java.io.IOException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) KeyFactory(java.security.KeyFactory) Beta(com.google.api.client.util.Beta)

Example 3 with Section

use of com.google.api.client.util.PemReader.Section in project photon-model by vmware.

the class GCPUtils method privateKeyFromPkcs8.

/**
 * The method validates an input string of private key and generate a java PrivateKey
 * object. The method is non-blocking.
 * @param privateKeyPem The private key in string format.
 * @return The private key in java PrivateKey object format.
 * @throws IOException When input key is not valid.
 */
public static PrivateKey privateKeyFromPkcs8(String privateKeyPem) throws IOException {
    StringReader reader = new StringReader(privateKeyPem);
    Section section = PemReader.readFirstSectionAndClose(reader, PRIVATE_KEY);
    if (section == null) {
        throw new IOException("Invalid PKCS8 data.");
    }
    try {
        byte[] decodedKey = section.getBase64DecodedBytes();
        PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(decodedKey);
        KeyFactory keyFactory = SecurityUtils.getRsaKeyFactory();
        return keyFactory.generatePrivate(keySpec);
    } catch (Exception e) {
        throw new IOException("Unexpected exception reading PKCS data", e);
    }
}
Also used : PKCS8EncodedKeySpec(java.security.spec.PKCS8EncodedKeySpec) StringReader(java.io.StringReader) IOException(java.io.IOException) Section(com.google.api.client.util.PemReader.Section) KeyFactory(java.security.KeyFactory) IOException(java.io.IOException)

Aggregations

Section (com.google.api.client.util.PemReader.Section)3 IOException (java.io.IOException)3 StringReader (java.io.StringReader)3 KeyFactory (java.security.KeyFactory)3 PKCS8EncodedKeySpec (java.security.spec.PKCS8EncodedKeySpec)3 PemReader (com.google.api.client.util.PemReader)2 Reader (java.io.Reader)2 GeneralSecurityException (java.security.GeneralSecurityException)2 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)2 InvalidKeySpecException (java.security.spec.InvalidKeySpecException)2 Beta (com.google.api.client.util.Beta)1 FileReader (java.io.FileReader)1 InvalidKeyException (java.security.InvalidKeyException)1 PrivateKey (java.security.PrivateKey)1 SignatureException (java.security.SignatureException)1