use of com.github.zhenwei.core.asn1.pkcs.PKCS12PBEParams in project LinLong-Java by zhenwei1108.
the class BcPKCS12PBEInputDecryptorProviderBuilder method build.
public InputDecryptorProvider build(final char[] password) {
return new InputDecryptorProvider() {
public InputDecryptor get(final AlgorithmIdentifier algorithmIdentifier) {
final PaddedBufferedBlockCipher engine = PKCS12PBEUtils.getEngine(algorithmIdentifier.getAlgorithm());
PKCS12PBEParams pbeParams = PKCS12PBEParams.getInstance(algorithmIdentifier.getParameters());
CipherParameters params = PKCS12PBEUtils.createCipherParameters(algorithmIdentifier.getAlgorithm(), digest, engine.getBlockSize(), pbeParams, password);
engine.init(false, params);
return new InputDecryptor() {
public AlgorithmIdentifier getAlgorithmIdentifier() {
return algorithmIdentifier;
}
public InputStream getInputStream(InputStream input) {
return new CipherInputStream(input, engine);
}
public GenericKey getKey() {
return new GenericKey(PKCS12ParametersGenerator.PKCS12PasswordToBytes(password));
}
};
}
};
}
use of com.github.zhenwei.core.asn1.pkcs.PKCS12PBEParams in project LinLong-Java by zhenwei1108.
the class BcPKCS12PBEOutputEncryptorBuilder method build.
public OutputEncryptor build(final char[] password) {
if (random == null) {
random = new SecureRandom();
}
final byte[] salt = new byte[20];
random.nextBytes(salt);
final PKCS12PBEParams pbeParams = new PKCS12PBEParams(salt, iterationCount);
CipherParameters params = PKCS12PBEUtils.createCipherParameters(algorithm, digest, engine.getBlockSize(), pbeParams, password);
engine.init(true, params);
return new OutputEncryptor() {
public AlgorithmIdentifier getAlgorithmIdentifier() {
return new AlgorithmIdentifier(algorithm, pbeParams);
}
public OutputStream getOutputStream(OutputStream out) {
return new CipherOutputStream(out, engine);
}
public GenericKey getKey() {
return new GenericKey(new AlgorithmIdentifier(algorithm, pbeParams), PKCS12ParametersGenerator.PKCS12PasswordToBytes(password));
}
};
}
use of com.github.zhenwei.core.asn1.pkcs.PKCS12PBEParams in project LinLong-Java by zhenwei1108.
the class PKCS12KeyStoreSpi method unwrapKey.
protected PrivateKey unwrapKey(AlgorithmIdentifier algId, byte[] data, char[] password, boolean wrongPKCS12Zero) throws IOException {
ASN1ObjectIdentifier algorithm = algId.getAlgorithm();
try {
if (algorithm.on(PKCSObjectIdentifiers.pkcs_12PbeIds)) {
PKCS12PBEParams pbeParams = PKCS12PBEParams.getInstance(algId.getParameters());
PBEParameterSpec defParams = new PBEParameterSpec(pbeParams.getIV(), validateIterationCount(pbeParams.getIterations()));
Cipher cipher = helper.createCipher(algorithm.getId());
PKCS12Key key = new PKCS12Key(password, wrongPKCS12Zero);
cipher.init(Cipher.UNWRAP_MODE, key, defParams);
// we pass "" as the key algorithm type as it is unknown at this point
return (PrivateKey) cipher.unwrap(data, "", Cipher.PRIVATE_KEY);
} else if (algorithm.equals(PKCSObjectIdentifiers.id_PBES2)) {
Cipher cipher = createCipher(Cipher.UNWRAP_MODE, password, algId);
// we pass "" as the key algorithm type as it is unknown at this point
return (PrivateKey) cipher.unwrap(data, "", Cipher.PRIVATE_KEY);
}
} catch (Exception e) {
throw new IOException("exception unwrapping private key - " + e.toString());
}
throw new IOException("exception unwrapping private key - cannot recognise: " + algorithm);
}
use of com.github.zhenwei.core.asn1.pkcs.PKCS12PBEParams in project LinLong-Java by zhenwei1108.
the class PKCS12PfxPdu method isMacValid.
/**
* Verify the MacData attached to the PFX is consistent with what is expected.
*
* @param macCalcProviderBuilder provider builder for the calculator for the MAC
* @param password password to use
* @return true if mac data is valid, false otherwise.
* @throws PKCSException if there is a problem evaluating the MAC.
* @throws IllegalStateException if no MAC is actually present
*/
public boolean isMacValid(PKCS12MacCalculatorBuilderProvider macCalcProviderBuilder, char[] password) throws PKCSException {
if (hasMac()) {
MacData pfxmData = pfx.getMacData();
MacDataGenerator mdGen = new MacDataGenerator(macCalcProviderBuilder.get(new AlgorithmIdentifier(pfxmData.getMac().getAlgorithmId().getAlgorithm(), new PKCS12PBEParams(pfxmData.getSalt(), pfxmData.getIterationCount().intValue()))));
try {
MacData mData = mdGen.build(password, ASN1OctetString.getInstance(pfx.getAuthSafe().getContent()).getOctets());
return Arrays.constantTimeAreEqual(mData.getEncoded(), pfx.getMacData().getEncoded());
} catch (IOException e) {
throw new PKCSException("unable to process AuthSafe: " + e.getMessage());
}
}
throw new IllegalStateException("no MAC present on PFX");
}
use of com.github.zhenwei.core.asn1.pkcs.PKCS12PBEParams in project LinLong-Java by zhenwei1108.
the class JceOpenSSLPKCS8DecryptorProviderBuilder method build.
public InputDecryptorProvider build(final char[] password) throws OperatorCreationException {
return new InputDecryptorProvider() {
public InputDecryptor get(final AlgorithmIdentifier algorithm) throws OperatorCreationException {
final Cipher cipher;
try {
if (PEMUtilities.isPKCS5Scheme2(algorithm.getAlgorithm())) {
PBES2Parameters params = PBES2Parameters.getInstance(algorithm.getParameters());
KeyDerivationFunc func = params.getKeyDerivationFunc();
EncryptionScheme scheme = params.getEncryptionScheme();
PBKDF2Params defParams = (PBKDF2Params) func.getParameters();
int iterationCount = defParams.getIterationCount().intValue();
byte[] salt = defParams.getSalt();
String oid = scheme.getAlgorithm().getId();
SecretKey key;
if (PEMUtilities.isHmacSHA1(defParams.getPrf())) {
key = PEMUtilities.generateSecretKeyForPKCS5Scheme2(helper, oid, password, salt, iterationCount);
} else {
key = PEMUtilities.generateSecretKeyForPKCS5Scheme2(helper, oid, password, salt, iterationCount, defParams.getPrf());
}
cipher = helper.createCipher(oid);
AlgorithmParameters algParams = helper.createAlgorithmParameters(oid);
algParams.init(scheme.getParameters().toASN1Primitive().getEncoded());
cipher.init(Cipher.DECRYPT_MODE, key, algParams);
} else if (PEMUtilities.isPKCS12(algorithm.getAlgorithm())) {
PKCS12PBEParams params = PKCS12PBEParams.getInstance(algorithm.getParameters());
cipher = helper.createCipher(algorithm.getAlgorithm().getId());
cipher.init(Cipher.DECRYPT_MODE, new PKCS12KeyWithParameters(password, params.getIV(), params.getIterations().intValue()));
} else if (PEMUtilities.isPKCS5Scheme1(algorithm.getAlgorithm())) {
PBEParameter params = PBEParameter.getInstance(algorithm.getParameters());
cipher = helper.createCipher(algorithm.getAlgorithm().getId());
cipher.init(Cipher.DECRYPT_MODE, new PBKDF1KeyWithParameters(password, new CharToByteConverter() {
public String getType() {
return "ASCII";
}
public byte[] convert(char[] password) {
// just drop hi-order byte.
return Strings.toByteArray(password);
}
}, params.getSalt(), params.getIterationCount().intValue()));
} else {
throw new PEMException("Unknown algorithm: " + algorithm.getAlgorithm());
}
return new InputDecryptor() {
public AlgorithmIdentifier getAlgorithmIdentifier() {
return algorithm;
}
public InputStream getInputStream(InputStream encIn) {
return new CipherInputStream(encIn, cipher);
}
};
} catch (IOException e) {
throw new OperatorCreationException(algorithm.getAlgorithm() + " not available: " + e.getMessage(), e);
} catch (GeneralSecurityException e) {
throw new OperatorCreationException(algorithm.getAlgorithm() + " not available: " + e.getMessage(), e);
}
}
};
}
Aggregations