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);
}
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);
}
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);
}
}
Aggregations