use of java.security.PrivateKey in project netty by netty.
the class SslContextTest method testUnencryptedEmptyPassword.
@Test(expected = IOException.class)
public void testUnencryptedEmptyPassword() throws Exception {
PrivateKey key = SslContext.toPrivateKey(new File(getClass().getResource("test2_unencrypted.pem").getFile()), "");
Assert.assertNotNull(key);
}
use of java.security.PrivateKey in project platformlayer by platformlayer.
the class KeyParser method parse.
public Object parse(String s) {
Object key = null;
if (key == null) {
if (s.contains(BEGIN_PRIVATE_KEY)) {
String payload = s.substring(s.indexOf(BEGIN_PRIVATE_KEY) + BEGIN_PRIVATE_KEY.length());
if (payload.contains(END_PRIVATE_KEY)) {
payload = payload.substring(0, payload.indexOf(END_PRIVATE_KEY));
key = tryParsePemFormat(payload);
}
}
}
if (key == null) {
try {
PemReader reader = new PemReader(new StringReader(s));
PemObject pemObject = reader.readPemObject();
reader.close();
PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(pemObject.getContent());
KeyFactory kf = KeyFactory.getInstance("RSA");
PrivateKey privateKey = kf.generatePrivate(keySpec);
if (privateKey instanceof RSAPrivateCrtKey) {
RSAPrivateCrtKey rsaPrivateCrtKey = (RSAPrivateCrtKey) privateKey;
RSAPublicKeySpec publicKeySpec = new java.security.spec.RSAPublicKeySpec(rsaPrivateCrtKey.getModulus(), rsaPrivateCrtKey.getPublicExponent());
PublicKey publicKey = kf.generatePublic(publicKeySpec);
key = new KeyPair(publicKey, privateKey);
} else {
key = privateKey;
}
} catch (Exception e) {
log.debug("Error reading pem data", e);
return null;
}
}
if (key == null) {
try {
// TODO: Check if looks like base64??
byte[] fromBase64 = Base64.decode(s);
key = parse(fromBase64);
} catch (Exception e) {
log.debug("Cannot decode as base64", e);
}
}
return key;
}
use of java.security.PrivateKey in project platformlayer by platformlayer.
the class KeyStoreEncryptionStore method main.
public static void main(String[] args) throws Exception {
if (!args[0].equals("explode")) {
throw new IllegalStateException();
}
char[] password = "notasecret".toCharArray();
ProtectionParameter protParam = new KeyStore.PasswordProtection(password);
KeyStore keyStore = KeyStoreUtils.load(new File(args[1]));
File dest = new File(args[2]);
dest.mkdirs();
Enumeration<String> aliases = keyStore.aliases();
while (aliases.hasMoreElements()) {
String alias = aliases.nextElement();
if (keyStore.isKeyEntry(alias)) {
Entry entry = keyStore.getEntry(alias, protParam);
PrivateKeyEntry privateKeyEntry = (PrivateKeyEntry) entry;
{
X509Certificate[] certificateChain = toX509(privateKeyEntry.getCertificateChain());
String encoded = CertificateUtils.toPem(certificateChain);
File out = new File(dest, alias + ".crt");
Files.write(encoded, out, Charsets.UTF_8);
}
{
PrivateKey key = privateKeyEntry.getPrivateKey();
String encoded = PrivateKeys.toPem(key);
File out = new File(dest, alias + ".key");
Files.write(encoded, out, Charsets.UTF_8);
}
}
if (keyStore.isCertificateEntry(alias)) {
Entry entry = keyStore.getEntry(alias, null);
TrustedCertificateEntry trustedCertificateEntry = (TrustedCertificateEntry) entry;
X509Certificate cert = (X509Certificate) trustedCertificateEntry.getTrustedCertificate();
String encoded = CertificateUtils.toPem(cert);
File out = new File(dest, alias + ".crt");
Files.write(encoded, out, Charsets.UTF_8);
}
}
}
use of java.security.PrivateKey in project platformlayer by platformlayer.
the class DirectoryEncryptionStore method getCertificateAndKey.
@Override
public CertificateAndKey getCertificateAndKey(String alias) {
CertificateAndKey certificateAndKey;
Preconditions.checkNotNull(alias);
// Path to file
File certPath = new File(base, alias + ".crt");
List<X509Certificate> certificate;
try {
certificate = CertificateUtils.fromPem(certPath);
} catch (IOException e) {
throw new IllegalArgumentException("Error reading certificate: " + certPath, e);
}
File keyPath = new File(base, alias + ".key");
PrivateKey privateKey;
try {
privateKey = PrivateKeys.fromPem(keyPath);
} catch (IOException e) {
throw new IllegalArgumentException("Error reading private key: " + keyPath, e);
}
certificateAndKey = new SimpleCertificateAndKey(certificate, privateKey);
return certificateAndKey;
}
use of java.security.PrivateKey in project platformlayer by platformlayer.
the class SecretStore method getSecretFromUser.
public CryptoKey getSecretFromUser(final UserEntity user) {
SecretStoreDecoder visitor = new SecretStoreDecoder() {
@Override
public void visitUserKey(int userId, byte[] data) {
if (userId == user.getId()) {
setSecretKey(decryptSymetricKey(user.getUserSecret(), data));
}
}
@Override
public void visitAsymetricUserKey(int userId, byte[] data) {
if (userId == user.getId()) {
PrivateKey privateKey = user.getPrivateKey();
setSecretKey(decryptAsymetricKey(privateKey, data));
}
}
};
try {
read(encoded, visitor);
} catch (IOException e) {
throw new IllegalArgumentException("Secret data is corrupted", e);
}
return visitor.getSecretKey();
}
Aggregations