use of org.bouncycastle.asn1.pkcs.RSAESOAEPparams in project xipki by xipki.
the class CmpAgentUtil method decrypt.
private static byte[] decrypt(EncryptedValue ev, PrivateKey decKey) throws XiSecurityException {
AlgorithmIdentifier keyAlg = ev.getKeyAlg();
ASN1ObjectIdentifier keyOid = keyAlg.getAlgorithm();
byte[] symmKey;
try {
if (decKey instanceof RSAPrivateKey) {
Cipher keyCipher;
if (keyOid.equals(PKCSObjectIdentifiers.id_RSAES_OAEP)) {
// Currently we only support the default RSAESOAEPparams
if (keyAlg.getParameters() != null) {
RSAESOAEPparams params = RSAESOAEPparams.getInstance(keyAlg.getParameters());
ASN1ObjectIdentifier oid = params.getHashAlgorithm().getAlgorithm();
if (!oid.equals(RSAESOAEPparams.DEFAULT_HASH_ALGORITHM.getAlgorithm())) {
throw new XiSecurityException("unsupported RSAESOAEPparams.HashAlgorithm " + oid.getId());
}
oid = params.getMaskGenAlgorithm().getAlgorithm();
if (!oid.equals(RSAESOAEPparams.DEFAULT_MASK_GEN_FUNCTION.getAlgorithm())) {
throw new XiSecurityException("unsupported RSAESOAEPparams.MaskGenAlgorithm " + oid.getId());
}
oid = params.getPSourceAlgorithm().getAlgorithm();
if (!params.getPSourceAlgorithm().equals(RSAESOAEPparams.DEFAULT_P_SOURCE_ALGORITHM)) {
throw new XiSecurityException("unsupported RSAESOAEPparams.PSourceAlgorithm " + oid.getId());
}
}
keyCipher = Cipher.getInstance("RSA/NONE/OAEPPADDING");
} else if (keyOid.equals(PKCSObjectIdentifiers.rsaEncryption)) {
keyCipher = Cipher.getInstance("RSA/NONE/PKCS1PADDING");
} else {
throw new XiSecurityException("unsupported keyAlg " + keyOid.getId());
}
keyCipher.init(Cipher.DECRYPT_MODE, decKey);
symmKey = keyCipher.doFinal(ev.getEncSymmKey().getOctets());
} else if (decKey instanceof ECPrivateKey) {
ASN1Sequence params = ASN1Sequence.getInstance(keyAlg.getParameters());
final int n = params.size();
for (int i = 0; i < n; i++) {
if (!keyOid.equals(ObjectIdentifiers.Secg.id_ecies_specifiedParameters)) {
throw new XiSecurityException("unsupported keyAlg " + keyOid.getId());
}
ASN1TaggedObject to = (ASN1TaggedObject) params.getObjectAt(i);
int tag = to.getTagNo();
if (tag == 0) {
// KDF
AlgorithmIdentifier algId = AlgorithmIdentifier.getInstance(to.getObject());
if (ObjectIdentifiers.Misc.id_iso18033_kdf2.equals(algId.getAlgorithm())) {
AlgorithmIdentifier hashAlgorithm = AlgorithmIdentifier.getInstance(algId.getParameters());
if (!hashAlgorithm.getAlgorithm().equals(HashAlgo.SHA1.getOid())) {
throw new XiSecurityException("unsupported KeyDerivationFunction.HashAlgorithm " + hashAlgorithm.getAlgorithm().getId());
}
} else {
throw new XiSecurityException("unsupported KeyDerivationFunction " + algId.getAlgorithm().getId());
}
} else if (tag == 1) {
// SymmetricEncryption
AlgorithmIdentifier algId = AlgorithmIdentifier.getInstance(to.getObject());
if (!ObjectIdentifiers.Secg.id_aes128_cbc_in_ecies.equals(algId.getAlgorithm())) {
throw new XiSecurityException("unsupported SymmetricEncryption " + algId.getAlgorithm().getId());
}
} else if (tag == 2) {
// MessageAuthenticationCode
AlgorithmIdentifier algId = AlgorithmIdentifier.getInstance(to.getObject());
if (ObjectIdentifiers.Secg.id_hmac_full_ecies.equals(algId.getAlgorithm())) {
AlgorithmIdentifier hashAlgorithm = AlgorithmIdentifier.getInstance(algId.getParameters());
if (!hashAlgorithm.getAlgorithm().equals(HashAlgo.SHA1.getOid())) {
throw new XiSecurityException("unsupported MessageAuthenticationCode.HashAlgorithm " + hashAlgorithm.getAlgorithm().getId());
}
} else {
throw new XiSecurityException("unsupported MessageAuthenticationCode " + algId.getAlgorithm().getId());
}
}
}
int aesKeySize = 128;
byte[] iv = new byte[16];
AlgorithmParameterSpec spec = new IESParameterSpec(null, null, aesKeySize, aesKeySize, iv);
BlockCipher cbcCipher = new CBCBlockCipher(new AESEngine());
IESCipher keyCipher = new IESCipher(new IESEngine(new ECDHBasicAgreement(), new KDF2BytesGenerator(DigestFactory.createSHA1()), new HMac(DigestFactory.createSHA1()), new PaddedBufferedBlockCipher(cbcCipher)), 16);
// no random is required
keyCipher.engineInit(Cipher.DECRYPT_MODE, decKey, spec, null);
byte[] encSymmKey = ev.getEncSymmKey().getOctets();
/*
* BouncyCastle expects the input ephemeralPublicKey | symmetricCiphertext | macTag.
* So we have to convert it from the following ASN.1 structure
* <pre>
* ECIES-Ciphertext-Value ::= SEQUENCE {
* ephemeralPublicKey ECPoint,
* symmetricCiphertext OCTET STRING,
* macTag OCTET STRING
* }
*
* ECPoint ::= OCTET STRING
* </pre>
*/
ASN1Sequence seq = DERSequence.getInstance(encSymmKey);
byte[] ephemeralPublicKey = DEROctetString.getInstance(seq.getObjectAt(0)).getOctets();
byte[] symmetricCiphertext = DEROctetString.getInstance(seq.getObjectAt(1)).getOctets();
byte[] macTag = DEROctetString.getInstance(seq.getObjectAt(2)).getOctets();
byte[] bcInput = new byte[ephemeralPublicKey.length + symmetricCiphertext.length + macTag.length];
System.arraycopy(ephemeralPublicKey, 0, bcInput, 0, ephemeralPublicKey.length);
int offset = ephemeralPublicKey.length;
System.arraycopy(symmetricCiphertext, 0, bcInput, offset, symmetricCiphertext.length);
offset += symmetricCiphertext.length;
System.arraycopy(macTag, 0, bcInput, offset, macTag.length);
symmKey = keyCipher.engineDoFinal(bcInput, 0, bcInput.length);
} else {
throw new XiSecurityException("unsupported decryption key type " + decKey.getClass().getName());
}
AlgorithmIdentifier symmAlg = ev.getSymmAlg();
ASN1ObjectIdentifier symmAlgOid = symmAlg.getAlgorithm();
if (!symmAlgOid.equals(NISTObjectIdentifiers.id_aes128_GCM)) {
// currently we only support AES128-GCM
throw new XiSecurityException("unsupported symmAlg " + symmAlgOid.getId());
}
GCMParameters params = GCMParameters.getInstance(symmAlg.getParameters());
Cipher dataCipher = Cipher.getInstance(symmAlgOid.getId());
AlgorithmParameterSpec algParams = new GCMParameterSpec(params.getIcvLen() << 3, params.getNonce());
dataCipher.init(Cipher.DECRYPT_MODE, new SecretKeySpec(symmKey, "AES"), algParams);
byte[] encValue = ev.getEncValue().getOctets();
return dataCipher.doFinal(encValue);
} catch (NoSuchAlgorithmException | NoSuchPaddingException | InvalidKeyException | InvalidAlgorithmParameterException | IllegalBlockSizeException | BadPaddingException ex) {
throw new XiSecurityException("Error while decrypting the EncryptedValue", ex);
}
}
use of org.bouncycastle.asn1.pkcs.RSAESOAEPparams in project xipki by xipki.
the class SignatureCmpCaClient method decrypt.
private byte[] decrypt(EncryptedValue ev) throws Exception {
AlgorithmIdentifier keyAlg = ev.getKeyAlg();
ASN1ObjectIdentifier keyOid = keyAlg.getAlgorithm();
byte[] symmKey;
try {
if (requestorKey instanceof RSAPrivateKey) {
Cipher keyCipher;
if (keyOid.equals(PKCSObjectIdentifiers.id_RSAES_OAEP)) {
// Currently we only support the default RSAESOAEPparams
if (keyAlg.getParameters() != null) {
RSAESOAEPparams params = RSAESOAEPparams.getInstance(keyAlg.getParameters());
ASN1ObjectIdentifier oid = params.getHashAlgorithm().getAlgorithm();
if (!oid.equals(RSAESOAEPparams.DEFAULT_HASH_ALGORITHM.getAlgorithm())) {
throw new Exception("unsupported RSAESOAEPparams.HashAlgorithm " + oid.getId());
}
oid = params.getMaskGenAlgorithm().getAlgorithm();
if (!oid.equals(RSAESOAEPparams.DEFAULT_MASK_GEN_FUNCTION.getAlgorithm())) {
throw new Exception("unsupported RSAESOAEPparams.MaskGenAlgorithm " + oid.getId());
}
oid = params.getPSourceAlgorithm().getAlgorithm();
if (!params.getPSourceAlgorithm().equals(RSAESOAEPparams.DEFAULT_P_SOURCE_ALGORITHM)) {
throw new Exception("unsupported RSAESOAEPparams.PSourceAlgorithm " + oid.getId());
}
}
keyCipher = Cipher.getInstance("RSA/NONE/OAEPPADDING");
} else if (keyOid.equals(PKCSObjectIdentifiers.rsaEncryption)) {
keyCipher = Cipher.getInstance("RSA/NONE/PKCS1PADDING");
} else {
throw new Exception("unsupported keyAlg " + keyOid.getId());
}
keyCipher.init(Cipher.DECRYPT_MODE, requestorKey);
symmKey = keyCipher.doFinal(ev.getEncSymmKey().getOctets());
} else if (requestorKey instanceof ECPrivateKey) {
ASN1Sequence params = ASN1Sequence.getInstance(keyAlg.getParameters());
final int n = params.size();
for (int i = 0; i < n; i++) {
if (!keyOid.equals(ObjectIdentifiers.id_ecies_specifiedParameters)) {
throw new Exception("unsupported keyAlg " + keyOid.getId());
}
ASN1TaggedObject to = (ASN1TaggedObject) params.getObjectAt(i);
int tag = to.getTagNo();
if (tag == 0) {
// KDF
AlgorithmIdentifier algId = AlgorithmIdentifier.getInstance(to.getObject());
if (ObjectIdentifiers.id_iso18033_kdf2.equals(algId.getAlgorithm())) {
AlgorithmIdentifier hashAlgorithm = AlgorithmIdentifier.getInstance(algId.getParameters());
if (!hashAlgorithm.getAlgorithm().equals(ObjectIdentifiers.id_sha1)) {
throw new Exception("unsupported KeyDerivationFunction.HashAlgorithm " + hashAlgorithm.getAlgorithm().getId());
}
} else {
throw new Exception("unsupported KeyDerivationFunction " + algId.getAlgorithm().getId());
}
} else if (tag == 1) {
// SymmetricEncryption
AlgorithmIdentifier algId = AlgorithmIdentifier.getInstance(to.getObject());
if (!ObjectIdentifiers.id_aes128_cbc_in_ecies.equals(algId.getAlgorithm())) {
throw new Exception("unsupported SymmetricEncryption " + algId.getAlgorithm().getId());
}
} else if (tag == 2) {
// MessageAuthenticationCode
AlgorithmIdentifier algId = AlgorithmIdentifier.getInstance(to.getObject());
if (ObjectIdentifiers.id_hmac_full_ecies.equals(algId.getAlgorithm())) {
AlgorithmIdentifier hashAlgorithm = AlgorithmIdentifier.getInstance(algId.getParameters());
if (!hashAlgorithm.getAlgorithm().equals(ObjectIdentifiers.id_sha1)) {
throw new Exception("unsupported MessageAuthenticationCode.HashAlgorithm " + hashAlgorithm.getAlgorithm().getId());
}
} else {
throw new Exception("unsupported MessageAuthenticationCode " + algId.getAlgorithm().getId());
}
}
}
int aesKeySize = 128;
byte[] iv = new byte[16];
AlgorithmParameterSpec spec = new IESParameterSpec(null, null, aesKeySize, aesKeySize, iv);
BlockCipher cbcCipher = new CBCBlockCipher(new AESEngine());
IESCipher keyCipher = new IESCipher(new IESEngine(new ECDHBasicAgreement(), new KDF2BytesGenerator(DigestFactory.createSHA1()), new HMac(DigestFactory.createSHA1()), new PaddedBufferedBlockCipher(cbcCipher)), 16);
// no random is required
keyCipher.engineInit(Cipher.DECRYPT_MODE, requestorKey, spec, null);
byte[] encSymmKey = ev.getEncSymmKey().getOctets();
/*
* BouncyCastle expects the input ephemeralPublicKey | symmetricCiphertext | macTag.
* So we have to convert it from the following ASN.1 structure
* <pre>
* ECIES-Ciphertext-Value ::= SEQUENCE {
* ephemeralPublicKey ECPoint,
* symmetricCiphertext OCTET STRING,
* macTag OCTET STRING
* }
*
* ECPoint ::= OCTET STRING
* </pre>
*/
ASN1Sequence seq = DERSequence.getInstance(encSymmKey);
byte[] ephemeralPublicKey = DEROctetString.getInstance(seq.getObjectAt(0)).getOctets();
byte[] symmetricCiphertext = DEROctetString.getInstance(seq.getObjectAt(1)).getOctets();
byte[] macTag = DEROctetString.getInstance(seq.getObjectAt(2)).getOctets();
byte[] bcInput = new byte[ephemeralPublicKey.length + symmetricCiphertext.length + macTag.length];
System.arraycopy(ephemeralPublicKey, 0, bcInput, 0, ephemeralPublicKey.length);
int offset = ephemeralPublicKey.length;
System.arraycopy(symmetricCiphertext, 0, bcInput, offset, symmetricCiphertext.length);
offset += symmetricCiphertext.length;
System.arraycopy(macTag, 0, bcInput, offset, macTag.length);
symmKey = keyCipher.engineDoFinal(bcInput, 0, bcInput.length);
} else {
throw new Exception("unsupported decryption key type " + requestorKey.getClass().getName());
}
AlgorithmIdentifier symmAlg = ev.getSymmAlg();
if (!symmAlg.getAlgorithm().equals(NISTObjectIdentifiers.id_aes128_GCM)) {
// currently we only support AES128-GCM
throw new Exception("unsupported symmAlg " + symmAlg.getAlgorithm().getId());
}
Cipher dataCipher = Cipher.getInstance(NISTObjectIdentifiers.id_aes128_GCM.getId());
GCMParameters gcmParams = GCMParameters.getInstance(symmAlg.getParameters());
GCMParameterSpec spec = new GCMParameterSpec(gcmParams.getIcvLen() * 8, gcmParams.getNonce());
dataCipher.init(Cipher.DECRYPT_MODE, new SecretKeySpec(symmKey, "AES"), spec);
byte[] encValue = ev.getEncValue().getOctets();
return dataCipher.doFinal(encValue);
} catch (NoSuchAlgorithmException | NoSuchPaddingException | InvalidKeyException | InvalidAlgorithmParameterException | IllegalBlockSizeException | BadPaddingException ex) {
throw new Exception("Error while decrypting the EncryptedValue", ex);
}
}
use of org.bouncycastle.asn1.pkcs.RSAESOAEPparams in project xipki by xipki.
the class CmsEnveloperTest method testRsaOaep.
@Test
public void testRsaOaep() throws Exception {
HashAlgo hashAlgo = HashAlgo.SHA256;
KeyStore ks = KeyStore.getInstance("PKCS12");
char[] password = "1234".toCharArray();
ks.load(new FileInputStream("src/test/resources/pkcs12test/test1.p12"), password);
X509Certificate reciCert = (X509Certificate) ks.getCertificate("main");
PrivateKey reciKey = (PrivateKey) ks.getKey("main", password);
byte[] data = Hex.decode("1234567890abcdef");
RSAESOAEPparams rsaOaepParams = new RSAESOAEPparams(hashAlgo.getAlgorithmIdentifier(), RSAESOAEPparams.DEFAULT_MASK_GEN_FUNCTION, RSAESOAEPparams.DEFAULT_P_SOURCE_ALGORITHM);
AlgorithmIdentifier oaepAlgId = new AlgorithmIdentifier(PKCSObjectIdentifiers.id_RSAES_OAEP, rsaOaepParams);
CMSEnvelopedDataGenerator edGen = new CMSEnvelopedDataGenerator();
final String bc = "BC";
edGen.addRecipientInfoGenerator(new JceKeyTransRecipientInfoGenerator(reciCert, oaepAlgId).setProvider(bc));
CMSEnvelopedData ed = edGen.generate(new CMSProcessableByteArray(data), new JceCMSContentEncryptorBuilder(CMSAlgorithm.AES128_GCM).setProvider(bc).build());
RecipientInformationStore recipients = ed.getRecipientInfos();
Iterator<RecipientInformation> it = recipients.getRecipients().iterator();
RecipientInformation recipient = it.next();
byte[] recData = recipient.getContent(new JceKeyTransEnvelopedRecipient(reciKey).setProvider(bc));
Assert.assertArrayEquals(recData, data);
}
use of org.bouncycastle.asn1.pkcs.RSAESOAEPparams in project LinLong-Java by zhenwei1108.
the class JcaAlgorithmParametersConverter method getAlgorithmIdentifier.
public AlgorithmIdentifier getAlgorithmIdentifier(ASN1ObjectIdentifier algorithm, AlgorithmParameterSpec algorithmSpec) throws InvalidAlgorithmParameterException {
if (algorithmSpec instanceof OAEPParameterSpec) {
if (algorithmSpec.equals(OAEPParameterSpec.DEFAULT)) {
return new AlgorithmIdentifier(algorithm, new RSAESOAEPparams(RSAESOAEPparams.DEFAULT_HASH_ALGORITHM, RSAESOAEPparams.DEFAULT_MASK_GEN_FUNCTION, RSAESOAEPparams.DEFAULT_P_SOURCE_ALGORITHM));
} else {
OAEPParameterSpec oaepSpec = (OAEPParameterSpec) algorithmSpec;
PSource pSource = oaepSpec.getPSource();
if (!oaepSpec.getMGFAlgorithm().equals(OAEPParameterSpec.DEFAULT.getMGFAlgorithm())) {
throw new InvalidAlgorithmParameterException("only " + OAEPParameterSpec.DEFAULT.getMGFAlgorithm() + " mask generator supported.");
}
AlgorithmIdentifier hashAlgorithm = new DefaultDigestAlgorithmIdentifierFinder().find(oaepSpec.getDigestAlgorithm());
if (hashAlgorithm.getParameters() == null) {
hashAlgorithm = new AlgorithmIdentifier(hashAlgorithm.getAlgorithm(), DERNull.INSTANCE);
}
AlgorithmIdentifier mgf1HashAlgorithm = new DefaultDigestAlgorithmIdentifierFinder().find((((MGF1ParameterSpec) oaepSpec.getMGFParameters()).getDigestAlgorithm()));
if (mgf1HashAlgorithm.getParameters() == null) {
mgf1HashAlgorithm = new AlgorithmIdentifier(mgf1HashAlgorithm.getAlgorithm(), DERNull.INSTANCE);
}
return new AlgorithmIdentifier(algorithm, new RSAESOAEPparams(hashAlgorithm, new AlgorithmIdentifier(PKCSObjectIdentifiers.id_mgf1, mgf1HashAlgorithm), new AlgorithmIdentifier(PKCSObjectIdentifiers.id_pSpecified, new DEROctetString(((PSource.PSpecified) pSource).getValue()))));
}
}
throw new InvalidAlgorithmParameterException("unknown parameter spec passed.");
}
use of org.bouncycastle.asn1.pkcs.RSAESOAEPparams in project efm-integrasjonspunkt by felleslosninger.
the class CmsUtil method rsaesOaepIdentifier.
private AlgorithmIdentifier rsaesOaepIdentifier() {
AlgorithmIdentifier hash = new AlgorithmIdentifier(NISTObjectIdentifiers.id_sha256, DERNull.INSTANCE);
AlgorithmIdentifier mask = new AlgorithmIdentifier(PKCSObjectIdentifiers.id_mgf1, hash);
AlgorithmIdentifier pSource = new AlgorithmIdentifier(PKCSObjectIdentifiers.id_pSpecified, new DEROctetString(new byte[0]));
ASN1Encodable parameters = new RSAESOAEPparams(hash, mask, pSource);
return new AlgorithmIdentifier(PKCSObjectIdentifiers.id_RSAES_OAEP, parameters);
}
Aggregations