Search in sources :

Example 6 with RecipientInfo

use of com.github.zhenwei.pkix.util.asn1.cms.RecipientInfo 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)

Example 7 with RecipientInfo

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

the class CMSEnvelopedHelper method buildRecipientInformationStore.

static RecipientInformationStore buildRecipientInformationStore(ASN1Set recipientInfos, AlgorithmIdentifier messageAlgorithm, CMSSecureReadable secureReadable, AuthAttributesProvider additionalData) {
    List infos = new ArrayList();
    for (int i = 0; i != recipientInfos.size(); i++) {
        RecipientInfo info = RecipientInfo.getInstance(recipientInfos.getObjectAt(i));
        readRecipientInfo(infos, info, messageAlgorithm, secureReadable, additionalData);
    }
    return new RecipientInformationStore(infos);
}
Also used : ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) PasswordRecipientInfo(com.github.zhenwei.pkix.util.asn1.cms.PasswordRecipientInfo) RecipientInfo(com.github.zhenwei.pkix.util.asn1.cms.RecipientInfo) KeyTransRecipientInfo(com.github.zhenwei.pkix.util.asn1.cms.KeyTransRecipientInfo) KeyAgreeRecipientInfo(com.github.zhenwei.pkix.util.asn1.cms.KeyAgreeRecipientInfo) KEKRecipientInfo(com.github.zhenwei.pkix.util.asn1.cms.KEKRecipientInfo)

Example 8 with RecipientInfo

use of com.github.zhenwei.pkix.util.asn1.cms.RecipientInfo in project itext2 by albfernandez.

the class PdfPublicKeySecurityHandler method createDERForRecipient.

private ASN1Primitive createDERForRecipient(byte[] in, X509Certificate cert) throws IOException, GeneralSecurityException {
    String s = "1.2.840.113549.3.2";
    AlgorithmParameterGenerator algorithmparametergenerator = AlgorithmParameterGenerator.getInstance(s);
    AlgorithmParameters algorithmparameters = algorithmparametergenerator.generateParameters();
    ByteArrayInputStream bytearrayinputstream = new ByteArrayInputStream(algorithmparameters.getEncoded("ASN.1"));
    ASN1InputStream asn1inputstream = new ASN1InputStream(bytearrayinputstream);
    ASN1Primitive derobject = asn1inputstream.readObject();
    KeyGenerator keygenerator = KeyGenerator.getInstance(s);
    keygenerator.init(128);
    SecretKey secretkey = keygenerator.generateKey();
    Cipher cipher = Cipher.getInstance(s);
    cipher.init(1, secretkey, algorithmparameters);
    byte[] abyte1 = cipher.doFinal(in);
    DEROctetString deroctetstring = new DEROctetString(abyte1);
    KeyTransRecipientInfo keytransrecipientinfo = computeRecipientInfo(cert, secretkey.getEncoded());
    DERSet derset = new DERSet(new RecipientInfo(keytransrecipientinfo));
    AlgorithmIdentifier algorithmidentifier = new AlgorithmIdentifier(new ASN1ObjectIdentifier(s), derobject);
    EncryptedContentInfo encryptedcontentinfo = new EncryptedContentInfo(PKCSObjectIdentifiers.data, algorithmidentifier, deroctetstring);
    EnvelopedData env = new EnvelopedData(null, derset, encryptedcontentinfo, (org.bouncycastle.asn1.ASN1Set) null);
    ContentInfo contentinfo = new ContentInfo(PKCSObjectIdentifiers.envelopedData, env);
    return contentinfo.toASN1Primitive();
}
Also used : ASN1InputStream(org.bouncycastle.asn1.ASN1InputStream) KeyTransRecipientInfo(org.bouncycastle.asn1.cms.KeyTransRecipientInfo) AlgorithmParameterGenerator(java.security.AlgorithmParameterGenerator) DEROctetString(org.bouncycastle.asn1.DEROctetString) DERSet(org.bouncycastle.asn1.DERSet) DEROctetString(org.bouncycastle.asn1.DEROctetString) AlgorithmIdentifier(org.bouncycastle.asn1.x509.AlgorithmIdentifier) SecretKey(javax.crypto.SecretKey) ByteArrayInputStream(java.io.ByteArrayInputStream) EncryptedContentInfo(org.bouncycastle.asn1.cms.EncryptedContentInfo) ContentInfo(org.bouncycastle.asn1.cms.ContentInfo) Cipher(javax.crypto.Cipher) ASN1Primitive(org.bouncycastle.asn1.ASN1Primitive) KeyGenerator(javax.crypto.KeyGenerator) KeyTransRecipientInfo(org.bouncycastle.asn1.cms.KeyTransRecipientInfo) RecipientInfo(org.bouncycastle.asn1.cms.RecipientInfo) ASN1ObjectIdentifier(org.bouncycastle.asn1.ASN1ObjectIdentifier) EnvelopedData(org.bouncycastle.asn1.cms.EnvelopedData) AlgorithmParameters(java.security.AlgorithmParameters) EncryptedContentInfo(org.bouncycastle.asn1.cms.EncryptedContentInfo)

Aggregations

RecipientInfo (com.github.zhenwei.pkix.util.asn1.cms.RecipientInfo)4 AlgorithmParameterGenerator (java.security.AlgorithmParameterGenerator)4 AlgorithmParameters (java.security.AlgorithmParameters)4 Cipher (javax.crypto.Cipher)4 KeyGenerator (javax.crypto.KeyGenerator)4 SecretKey (javax.crypto.SecretKey)4 ASN1InputStream (org.bouncycastle.asn1.ASN1InputStream)4 ASN1ObjectIdentifier (org.bouncycastle.asn1.ASN1ObjectIdentifier)4 ASN1Primitive (org.bouncycastle.asn1.ASN1Primitive)4 DEROctetString (org.bouncycastle.asn1.DEROctetString)4 DERSet (org.bouncycastle.asn1.DERSet)4 ContentInfo (org.bouncycastle.asn1.cms.ContentInfo)4 EncryptedContentInfo (org.bouncycastle.asn1.cms.EncryptedContentInfo)4 EnvelopedData (org.bouncycastle.asn1.cms.EnvelopedData)4 KeyTransRecipientInfo (org.bouncycastle.asn1.cms.KeyTransRecipientInfo)4 RecipientInfo (org.bouncycastle.asn1.cms.RecipientInfo)4 AlgorithmIdentifier (org.bouncycastle.asn1.x509.AlgorithmIdentifier)4 DEROctetString (com.github.zhenwei.core.asn1.DEROctetString)3 AlgorithmIdentifier (com.github.zhenwei.core.asn1.x509.AlgorithmIdentifier)2 KeyAgreeRecipientInfo (com.github.zhenwei.pkix.util.asn1.cms.KeyAgreeRecipientInfo)2