Search in sources :

Example 1 with EncryptedKey

use of com.github.zhenwei.pkix.util.asn1.crmf.EncryptedKey in project LinLong-Java by zhenwei1108.

the class JceKeyAgreeRecipientInfoGenerator method generateRecipientEncryptedKeys.

public ASN1Sequence generateRecipientEncryptedKeys(AlgorithmIdentifier keyAgreeAlgorithm, AlgorithmIdentifier keyEncryptionAlgorithm, GenericKey contentEncryptionKey) throws CMSException {
    if (recipientIDs.isEmpty()) {
        throw new CMSException("No recipients associated with generator - use addRecipient()");
    }
    init(keyAgreeAlgorithm.getAlgorithm());
    PrivateKey senderPrivateKey = this.senderPrivateKey;
    ASN1ObjectIdentifier keyAgreementOID = keyAgreeAlgorithm.getAlgorithm();
    ASN1EncodableVector recipientEncryptedKeys = new ASN1EncodableVector();
    for (int i = 0; i != recipientIDs.size(); i++) {
        PublicKey recipientPublicKey = (PublicKey) recipientKeys.get(i);
        KeyAgreeRecipientIdentifier karId = (KeyAgreeRecipientIdentifier) recipientIDs.get(i);
        try {
            AlgorithmParameterSpec agreementParamSpec;
            ASN1ObjectIdentifier keyEncAlg = keyEncryptionAlgorithm.getAlgorithm();
            if (CMSUtils.isMQV(keyAgreementOID)) {
                agreementParamSpec = new MQVParameterSpec(ephemeralKP, recipientPublicKey, userKeyingMaterial);
            } else if (CMSUtils.isEC(keyAgreementOID)) {
                byte[] ukmKeyingMaterial = ecc_cms_Generator.generateKDFMaterial(keyEncryptionAlgorithm, keySizeProvider.getKeySize(keyEncAlg), userKeyingMaterial);
                agreementParamSpec = new UserKeyingMaterialSpec(ukmKeyingMaterial);
            } else if (CMSUtils.isRFC2631(keyAgreementOID)) {
                if (userKeyingMaterial != null) {
                    agreementParamSpec = new UserKeyingMaterialSpec(userKeyingMaterial);
                } else {
                    if (keyAgreementOID.equals(PKCSObjectIdentifiers.id_alg_SSDH)) {
                        throw new CMSException("User keying material must be set for static keys.");
                    }
                    agreementParamSpec = null;
                }
            } else if (CMSUtils.isGOST(keyAgreementOID)) {
                if (userKeyingMaterial != null) {
                    agreementParamSpec = new UserKeyingMaterialSpec(userKeyingMaterial);
                } else {
                    throw new CMSException("User keying material must be set for static keys.");
                }
            } else {
                throw new CMSException("Unknown key agreement algorithm: " + keyAgreementOID);
            }
            // Use key agreement to choose a wrap key for this recipient
            KeyAgreement keyAgreement = helper.createKeyAgreement(keyAgreementOID);
            keyAgreement.init(senderPrivateKey, agreementParamSpec, random);
            keyAgreement.doPhase(recipientPublicKey, true);
            SecretKey keyEncryptionKey = keyAgreement.generateSecret(keyEncAlg.getId());
            // Wrap the content encryption key with the agreement key
            Cipher keyEncryptionCipher = helper.createCipher(keyEncAlg);
            ASN1OctetString encryptedKey;
            if (keyEncAlg.equals(CryptoProObjectIdentifiers.id_Gost28147_89_None_KeyWrap) || keyEncAlg.equals(CryptoProObjectIdentifiers.id_Gost28147_89_CryptoPro_KeyWrap)) {
                keyEncryptionCipher.init(Cipher.WRAP_MODE, keyEncryptionKey, new GOST28147WrapParameterSpec(CryptoProObjectIdentifiers.id_Gost28147_89_CryptoPro_A_ParamSet, userKeyingMaterial));
                byte[] encKeyBytes = keyEncryptionCipher.wrap(helper.getJceKey(contentEncryptionKey));
                Gost2814789EncryptedKey encKey = new Gost2814789EncryptedKey(Arrays.copyOfRange(encKeyBytes, 0, encKeyBytes.length - 4), Arrays.copyOfRange(encKeyBytes, encKeyBytes.length - 4, encKeyBytes.length));
                encryptedKey = new DEROctetString(encKey.getEncoded(ASN1Encoding.DER));
            } else {
                keyEncryptionCipher.init(Cipher.WRAP_MODE, keyEncryptionKey, random);
                byte[] encryptedKeyBytes = keyEncryptionCipher.wrap(helper.getJceKey(contentEncryptionKey));
                encryptedKey = new DEROctetString(encryptedKeyBytes);
            }
            recipientEncryptedKeys.add(new RecipientEncryptedKey(karId, encryptedKey));
        } catch (GeneralSecurityException e) {
            throw new CMSException("cannot perform agreement step: " + e.getMessage(), e);
        } catch (IOException e) {
            throw new CMSException("unable to encode wrapped key: " + e.getMessage(), e);
        }
    }
    return new DERSequence(recipientEncryptedKeys);
}
Also used : ASN1OctetString(com.github.zhenwei.core.asn1.ASN1OctetString) PrivateKey(java.security.PrivateKey) OriginatorPublicKey(com.github.zhenwei.pkix.util.asn1.cms.OriginatorPublicKey) PublicKey(java.security.PublicKey) GOST28147WrapParameterSpec(com.github.zhenwei.provider.jcajce.spec.GOST28147WrapParameterSpec) RecipientEncryptedKey(com.github.zhenwei.pkix.util.asn1.cms.RecipientEncryptedKey) GeneralSecurityException(java.security.GeneralSecurityException) IOException(java.io.IOException) UserKeyingMaterialSpec(com.github.zhenwei.provider.jcajce.spec.UserKeyingMaterialSpec) DEROctetString(com.github.zhenwei.core.asn1.DEROctetString) Gost2814789EncryptedKey(com.github.zhenwei.core.asn1.cryptopro.Gost2814789EncryptedKey) SecretKey(javax.crypto.SecretKey) DERSequence(com.github.zhenwei.core.asn1.DERSequence) ASN1EncodableVector(com.github.zhenwei.core.asn1.ASN1EncodableVector) Cipher(javax.crypto.Cipher) KeyAgreement(javax.crypto.KeyAgreement) AlgorithmParameterSpec(java.security.spec.AlgorithmParameterSpec) MQVParameterSpec(com.github.zhenwei.provider.jcajce.spec.MQVParameterSpec) ASN1ObjectIdentifier(com.github.zhenwei.core.asn1.ASN1ObjectIdentifier) CMSException(com.github.zhenwei.pkix.cms.CMSException) KeyAgreeRecipientIdentifier(com.github.zhenwei.pkix.util.asn1.cms.KeyAgreeRecipientIdentifier)

Example 2 with EncryptedKey

use of com.github.zhenwei.pkix.util.asn1.crmf.EncryptedKey in project LinLong-Java by zhenwei1108.

the class JceKTSKeyUnwrapper method generateUnwrappedKey.

public GenericKey generateUnwrappedKey(AlgorithmIdentifier encryptedKeyAlgorithm, byte[] encryptedKey) throws OperatorException {
    GenericHybridParameters params = GenericHybridParameters.getInstance(this.getAlgorithmIdentifier().getParameters());
    Cipher keyCipher = helper.createAsymmetricWrapper(this.getAlgorithmIdentifier().getAlgorithm(), extraMappings);
    String symmetricWrappingAlg = helper.getWrappingAlgorithmName(params.getDem().getAlgorithm());
    RsaKemParameters kemParameters = RsaKemParameters.getInstance(params.getKem().getParameters());
    int keySizeInBits = kemParameters.getKeyLength().intValue() * 8;
    Key sKey;
    try {
        DEROtherInfo otherInfo = new DEROtherInfo.Builder(params.getDem(), partyUInfo, partyVInfo).build();
        KTSParameterSpec ktsSpec = new KTSParameterSpec.Builder(symmetricWrappingAlg, keySizeInBits, otherInfo.getEncoded()).withKdfAlgorithm(kemParameters.getKeyDerivationFunction()).build();
        keyCipher.init(Cipher.UNWRAP_MODE, privKey, ktsSpec);
        sKey = keyCipher.unwrap(encryptedKey, helper.getKeyAlgorithmName(encryptedKeyAlgorithm.getAlgorithm()), Cipher.SECRET_KEY);
    } catch (Exception e) {
        throw new OperatorException("Unable to unwrap contents key: " + e.getMessage(), e);
    }
    return new JceGenericKey(encryptedKeyAlgorithm, sKey);
}
Also used : DEROtherInfo(com.github.zhenwei.core.crypto.util.DEROtherInfo) GenericHybridParameters(com.github.zhenwei.pkix.util.asn1.cms.GenericHybridParameters) RsaKemParameters(com.github.zhenwei.pkix.util.asn1.cms.RsaKemParameters) OperatorException(com.github.zhenwei.pkix.operator.OperatorException) Cipher(javax.crypto.Cipher) Key(java.security.Key) PrivateKey(java.security.PrivateKey) GenericKey(com.github.zhenwei.pkix.operator.GenericKey) KTSParameterSpec(com.github.zhenwei.provider.jcajce.spec.KTSParameterSpec) OperatorException(com.github.zhenwei.pkix.operator.OperatorException)

Example 3 with EncryptedKey

use of com.github.zhenwei.pkix.util.asn1.crmf.EncryptedKey in project LinLong-Java by zhenwei1108.

the class PKIArchiveControl method getEnvelopedData.

/**
 * Return the enveloped data structure contained in this control.
 *
 * @return a CMSEnvelopedData object.
 */
public CMSEnvelopedData getEnvelopedData() throws CRMFException {
    try {
        EncryptedKey encKey = EncryptedKey.getInstance(pkiArchiveOptions.getValue());
        EnvelopedData data = EnvelopedData.getInstance(encKey.getValue());
        return new CMSEnvelopedData(new ContentInfo(CMSObjectIdentifiers.envelopedData, data));
    } catch (CMSException e) {
        throw new CRMFException("CMS parsing error: " + e.getMessage(), e.getCause());
    } catch (Exception e) {
        throw new CRMFException("CRMF parsing error: " + e.getMessage(), e);
    }
}
Also used : CMSEnvelopedData(com.github.zhenwei.pkix.cms.CMSEnvelopedData) EncryptedKey(com.github.zhenwei.pkix.util.asn1.crmf.EncryptedKey) ContentInfo(com.github.zhenwei.pkix.util.asn1.cms.ContentInfo) CMSEnvelopedData(com.github.zhenwei.pkix.cms.CMSEnvelopedData) EnvelopedData(com.github.zhenwei.pkix.util.asn1.cms.EnvelopedData) CMSException(com.github.zhenwei.pkix.cms.CMSException) CMSException(com.github.zhenwei.pkix.cms.CMSException)

Example 4 with EncryptedKey

use of com.github.zhenwei.pkix.util.asn1.crmf.EncryptedKey in project LinLong-Java by zhenwei1108.

the class PKIArchiveControlBuilder method build.

/**
 * Build the PKIArchiveControl using the passed in encryptor to encrypt its contents.
 *
 * @param contentEncryptor a suitable content encryptor.
 * @return a PKIArchiveControl object.
 * @throws CMSException in the event the build fails.
 */
public PKIArchiveControl build(OutputEncryptor contentEncryptor) throws CMSException {
    CMSEnvelopedData envContent = envGen.generate(keyContent, contentEncryptor);
    EnvelopedData envD = EnvelopedData.getInstance(envContent.toASN1Structure().getContent());
    return new PKIArchiveControl(new PKIArchiveOptions(new EncryptedKey(envD)));
}
Also used : CMSEnvelopedData(com.github.zhenwei.pkix.cms.CMSEnvelopedData) EncryptedKey(com.github.zhenwei.pkix.util.asn1.crmf.EncryptedKey) PKIArchiveOptions(com.github.zhenwei.pkix.util.asn1.crmf.PKIArchiveOptions) CMSEnvelopedData(com.github.zhenwei.pkix.cms.CMSEnvelopedData) EnvelopedData(com.github.zhenwei.pkix.util.asn1.cms.EnvelopedData)

Example 5 with EncryptedKey

use of com.github.zhenwei.pkix.util.asn1.crmf.EncryptedKey in project LinLong-Java by zhenwei1108.

the class PasswordRecipientInfoGenerator method generate.

public RecipientInfo generate(GenericKey contentEncryptionKey) throws CMSException {
    // / TODO: set IV size properly!
    byte[] iv = new byte[blockSize];
    if (random == null) {
        random = new SecureRandom();
    }
    random.nextBytes(iv);
    if (salt == null) {
        salt = new byte[20];
        random.nextBytes(salt);
    }
    keyDerivationAlgorithm = new AlgorithmIdentifier(PKCSObjectIdentifiers.id_PBKDF2, new PBKDF2Params(salt, iterationCount, prf.prfAlgID));
    byte[] derivedKey = calculateDerivedKey(schemeID, keyDerivationAlgorithm, keySize);
    AlgorithmIdentifier kekAlgorithmId = new AlgorithmIdentifier(kekAlgorithm, new DEROctetString(iv));
    byte[] encryptedKeyBytes = generateEncryptedBytes(kekAlgorithmId, derivedKey, contentEncryptionKey);
    ASN1OctetString encryptedKey = new DEROctetString(encryptedKeyBytes);
    ASN1EncodableVector v = new ASN1EncodableVector();
    v.add(kekAlgorithm);
    v.add(new DEROctetString(iv));
    AlgorithmIdentifier keyEncryptionAlgorithm = new AlgorithmIdentifier(PKCSObjectIdentifiers.id_alg_PWRI_KEK, new DERSequence(v));
    return new RecipientInfo(new PasswordRecipientInfo(keyDerivationAlgorithm, keyEncryptionAlgorithm, encryptedKey));
}
Also used : ASN1OctetString(com.github.zhenwei.core.asn1.ASN1OctetString) DERSequence(com.github.zhenwei.core.asn1.DERSequence) PasswordRecipientInfo(com.github.zhenwei.pkix.util.asn1.cms.PasswordRecipientInfo) PBKDF2Params(com.github.zhenwei.core.asn1.pkcs.PBKDF2Params) SecureRandom(java.security.SecureRandom) ASN1EncodableVector(com.github.zhenwei.core.asn1.ASN1EncodableVector) PasswordRecipientInfo(com.github.zhenwei.pkix.util.asn1.cms.PasswordRecipientInfo) RecipientInfo(com.github.zhenwei.pkix.util.asn1.cms.RecipientInfo) DEROctetString(com.github.zhenwei.core.asn1.DEROctetString) AlgorithmIdentifier(com.github.zhenwei.core.asn1.x509.AlgorithmIdentifier)

Aggregations

ASN1EncodableVector (com.github.zhenwei.core.asn1.ASN1EncodableVector)2 ASN1OctetString (com.github.zhenwei.core.asn1.ASN1OctetString)2 DEROctetString (com.github.zhenwei.core.asn1.DEROctetString)2 DERSequence (com.github.zhenwei.core.asn1.DERSequence)2 CMSEnvelopedData (com.github.zhenwei.pkix.cms.CMSEnvelopedData)2 CMSException (com.github.zhenwei.pkix.cms.CMSException)2 EnvelopedData (com.github.zhenwei.pkix.util.asn1.cms.EnvelopedData)2 EncryptedKey (com.github.zhenwei.pkix.util.asn1.crmf.EncryptedKey)2 PrivateKey (java.security.PrivateKey)2 Cipher (javax.crypto.Cipher)2 ASN1ObjectIdentifier (com.github.zhenwei.core.asn1.ASN1ObjectIdentifier)1 Gost2814789EncryptedKey (com.github.zhenwei.core.asn1.cryptopro.Gost2814789EncryptedKey)1 PBKDF2Params (com.github.zhenwei.core.asn1.pkcs.PBKDF2Params)1 AlgorithmIdentifier (com.github.zhenwei.core.asn1.x509.AlgorithmIdentifier)1 DEROtherInfo (com.github.zhenwei.core.crypto.util.DEROtherInfo)1 GenericKey (com.github.zhenwei.pkix.operator.GenericKey)1 OperatorException (com.github.zhenwei.pkix.operator.OperatorException)1 ContentInfo (com.github.zhenwei.pkix.util.asn1.cms.ContentInfo)1 GenericHybridParameters (com.github.zhenwei.pkix.util.asn1.cms.GenericHybridParameters)1 KeyAgreeRecipientIdentifier (com.github.zhenwei.pkix.util.asn1.cms.KeyAgreeRecipientIdentifier)1