use of javax.crypto.EncryptedPrivateKeyInfo in project netty by netty.
the class SslContext method generateKeySpec.
/**
* Generates a key specification for an (encrypted) private key.
*
* @param password characters, if {@code null} an unencrypted key is assumed
* @param key bytes of the DER encoded private key
*
* @return a key specification
*
* @throws IOException if parsing {@code key} fails
* @throws NoSuchAlgorithmException if the algorithm used to encrypt {@code key} is unkown
* @throws NoSuchPaddingException if the padding scheme specified in the decryption algorithm is unkown
* @throws InvalidKeySpecException if the decryption key based on {@code password} cannot be generated
* @throws InvalidKeyException if the decryption key based on {@code password} cannot be used to decrypt
* {@code key}
* @throws InvalidAlgorithmParameterException if decryption algorithm parameters are somehow faulty
*/
protected static PKCS8EncodedKeySpec generateKeySpec(char[] password, byte[] key) throws IOException, NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeySpecException, InvalidKeyException, InvalidAlgorithmParameterException {
if (password == null) {
return new PKCS8EncodedKeySpec(key);
}
EncryptedPrivateKeyInfo encryptedPrivateKeyInfo = new EncryptedPrivateKeyInfo(key);
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(encryptedPrivateKeyInfo.getAlgName());
PBEKeySpec pbeKeySpec = new PBEKeySpec(password);
SecretKey pbeKey = keyFactory.generateSecret(pbeKeySpec);
Cipher cipher = Cipher.getInstance(encryptedPrivateKeyInfo.getAlgName());
cipher.init(Cipher.DECRYPT_MODE, pbeKey, encryptedPrivateKeyInfo.getAlgParameters());
return encryptedPrivateKeyInfo.getKeySpec(cipher);
}
use of javax.crypto.EncryptedPrivateKeyInfo in project robovm by robovm.
the class EncryptedPrivateKeyInfoTest method testGetKeySpecKey01.
public final void testGetKeySpecKey01() {
boolean performed = false;
for (int i = 0; i < EncryptedPrivateKeyInfoData.algName0.length; i++) {
try {
EncryptedPrivateKeyInfo epki = new EncryptedPrivateKeyInfo(EncryptedPrivateKeyInfoData.algName0[i][0], EncryptedPrivateKeyInfoData.encryptedData);
try {
// check that method under test throws NPE
epki.getKeySpec((Key) null);
fail(getName() + "NullPointerException has not been thrown");
} catch (NullPointerException ok) {
} catch (InvalidKeyException e) {
fail(getName() + "Unexpected exception: " + e);
}
performed = true;
} catch (NoSuchAlgorithmException allowedFailure) {
}
}
assertTrue("Test not performed", performed);
}
use of javax.crypto.EncryptedPrivateKeyInfo in project robovm by robovm.
the class EncryptedPrivateKeyInfoTest method test_ROUNDTRIP_GetKeySpecKeyString02.
/**
* Encrypted data contains invalid PKCS8 key info encoding
*/
public final void test_ROUNDTRIP_GetKeySpecKeyString02() throws Exception {
boolean performed = false;
for (int i = 0; i < algName.length; i++) {
for (int l = 0; l < provider.length; l++) {
if (provider[l] == null) {
continue;
}
TestDataGenerator g;
try {
// generate test data
g = new TestDataGenerator(algName[i][0], algName[i][1], privateKeyInfoDamaged, provider[l]);
} catch (TestDataGenerator.AllowedFailure allowedFailure) {
continue;
}
try {
// create test object
EncryptedPrivateKeyInfo epki;
if (g.ap() == null) {
epki = new EncryptedPrivateKeyInfo(algName[i][0], g.ct());
} else {
epki = new EncryptedPrivateKeyInfo(g.ap(), g.ct());
}
try {
epki.getKeySpec(g.pubK() == null ? g.k() : g.pubK(), provider[l].getName());
fail(algName[i][0] + ", " + algName[i][1]);
} catch (InvalidKeyException e) {
}
performed = true;
} catch (NoSuchAlgorithmException allowedFailure) {
}
}
}
assertTrue("Test not performed", performed);
}
use of javax.crypto.EncryptedPrivateKeyInfo in project robovm by robovm.
the class EncryptedPrivateKeyInfoTest method testGetEncoded01.
/**
* Test #1 for <code>getEncoded()</code> method <br>
* Assertion: returns the ASN.1 encoding of this object <br>
* Test preconditions: test object created using ctor which takes encoded
* form as the only parameter <br>
* Expected: equivalent encoded form must be returned
*
* @throws IOException
*/
public final void testGetEncoded01() throws IOException {
boolean performed = false;
for (int i = 0; i < EncryptedPrivateKeyInfoData.algName0.length; i++) {
try {
byte[] enc = EncryptedPrivateKeyInfoData.getValidEncryptedPrivateKeyInfoEncoding(EncryptedPrivateKeyInfoData.algName0[i][0]);
EncryptedPrivateKeyInfo epki = new EncryptedPrivateKeyInfo(enc);
// check that method under test returns
// valid encoded form
assertTrue(Arrays.equals(enc, epki.getEncoded()));
performed = true;
} catch (NoSuchAlgorithmException allowedFailure) {
}
}
assertTrue("Test not performed", performed);
}
use of javax.crypto.EncryptedPrivateKeyInfo in project robovm by robovm.
the class EncryptedPrivateKeyInfoTest method test_ROUNDTRIP_GetKeySpecKey01.
/**
* Encrypted data contains valid PKCS8 key info encoding
*/
public final void test_ROUNDTRIP_GetKeySpecKey01() {
boolean performed = false;
for (int i = 0; i < algName.length; i++) {
try {
// generate test data
TestDataGenerator g = new TestDataGenerator(algName[i][0], algName[i][1], privateKeyInfo, null);
// create test object
EncryptedPrivateKeyInfo epki;
if (g.ap() == null) {
epki = new EncryptedPrivateKeyInfo(algName[i][0], g.ct());
} else {
epki = new EncryptedPrivateKeyInfo(g.ap(), g.ct());
}
try {
PKCS8EncodedKeySpec eks = epki.getKeySpec(g.pubK() == null ? g.k() : g.pubK());
if (!Arrays.equals(privateKeyInfo, eks.getEncoded())) {
fail(algName[i][0] + " != " + algName[i][1]);
}
} catch (InvalidKeyException e) {
fail(algName[i][0] + ", " + algName[i][1] + ": " + e);
}
performed = true;
} catch (TestDataGenerator.AllowedFailure allowedFailure) {
} catch (NoSuchAlgorithmException allowedFailure) {
}
}
assertTrue("Test not performed", performed);
}
Aggregations