Search in sources :

Example 1 with RecipientIdentifier

use of com.github.zhenwei.pkix.util.asn1.cms.RecipientIdentifier in project PdfBox-Android by TomRoush.

the class PublicKeySecurityHandler method computeRecipientInfo.

private KeyTransRecipientInfo computeRecipientInfo(X509Certificate x509certificate, byte[] abyte0) throws IOException, CertificateEncodingException, InvalidKeyException, BadPaddingException, IllegalBlockSizeException {
    ASN1InputStream input = new ASN1InputStream(x509certificate.getTBSCertificate());
    TBSCertificate certificate = TBSCertificate.getInstance(input.readObject());
    input.close();
    AlgorithmIdentifier algorithmId = certificate.getSubjectPublicKeyInfo().getAlgorithm();
    IssuerAndSerialNumber serial = new IssuerAndSerialNumber(certificate.getIssuer(), certificate.getSerialNumber().getValue());
    Cipher cipher;
    try {
        cipher = Cipher.getInstance(algorithmId.getAlgorithm().getId(), SecurityProvider.getProvider());
    } catch (NoSuchAlgorithmException e) {
        // should never happen, if this happens throw IOException instead
        throw new RuntimeException("Could not find a suitable javax.crypto provider", e);
    } catch (NoSuchPaddingException e) {
        // should never happen, if this happens throw IOException instead
        throw new RuntimeException("Could not find a suitable javax.crypto provider", e);
    }
    cipher.init(1, x509certificate.getPublicKey());
    DEROctetString octets = new DEROctetString(cipher.doFinal(abyte0));
    RecipientIdentifier recipientId = new RecipientIdentifier(serial);
    return new KeyTransRecipientInfo(recipientId, algorithmId, octets);
}
Also used : IssuerAndSerialNumber(org.bouncycastle.asn1.cms.IssuerAndSerialNumber) ASN1InputStream(org.bouncycastle.asn1.ASN1InputStream) KeyTransRecipientInfo(org.bouncycastle.asn1.cms.KeyTransRecipientInfo) NoSuchPaddingException(javax.crypto.NoSuchPaddingException) Cipher(javax.crypto.Cipher) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) RecipientIdentifier(org.bouncycastle.asn1.cms.RecipientIdentifier) TBSCertificate(org.bouncycastle.asn1.x509.TBSCertificate) DEROctetString(org.bouncycastle.asn1.DEROctetString) AlgorithmIdentifier(org.bouncycastle.asn1.x509.AlgorithmIdentifier)

Example 2 with RecipientIdentifier

use of com.github.zhenwei.pkix.util.asn1.cms.RecipientIdentifier in project pdfbox by apache.

the class PublicKeySecurityHandler method computeRecipientInfo.

private KeyTransRecipientInfo computeRecipientInfo(X509Certificate x509certificate, byte[] abyte0) throws IOException, CertificateEncodingException, InvalidKeyException, BadPaddingException, IllegalBlockSizeException {
    TBSCertificate certificate;
    try (ASN1InputStream input = new ASN1InputStream(x509certificate.getTBSCertificate())) {
        certificate = TBSCertificate.getInstance(input.readObject());
    }
    AlgorithmIdentifier algorithmId = certificate.getSubjectPublicKeyInfo().getAlgorithm();
    IssuerAndSerialNumber serial = new IssuerAndSerialNumber(certificate.getIssuer(), certificate.getSerialNumber().getValue());
    Cipher cipher;
    try {
        cipher = Cipher.getInstance(algorithmId.getAlgorithm().getId(), SecurityProvider.getProvider());
    } catch (NoSuchAlgorithmException | NoSuchPaddingException e) {
        // should never happen, if this happens throw IOException instead
        throw new RuntimeException("Could not find a suitable javax.crypto provider", e);
    }
    cipher.init(1, x509certificate.getPublicKey());
    DEROctetString octets = new DEROctetString(cipher.doFinal(abyte0));
    RecipientIdentifier recipientId = new RecipientIdentifier(serial);
    return new KeyTransRecipientInfo(recipientId, algorithmId, octets);
}
Also used : IssuerAndSerialNumber(org.bouncycastle.asn1.cms.IssuerAndSerialNumber) ASN1InputStream(org.bouncycastle.asn1.ASN1InputStream) KeyTransRecipientInfo(org.bouncycastle.asn1.cms.KeyTransRecipientInfo) NoSuchPaddingException(javax.crypto.NoSuchPaddingException) Cipher(javax.crypto.Cipher) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) RecipientIdentifier(org.bouncycastle.asn1.cms.RecipientIdentifier) TBSCertificate(org.bouncycastle.asn1.x509.TBSCertificate) DEROctetString(org.bouncycastle.asn1.DEROctetString) AlgorithmIdentifier(org.bouncycastle.asn1.x509.AlgorithmIdentifier)

Example 3 with RecipientIdentifier

use of com.github.zhenwei.pkix.util.asn1.cms.RecipientIdentifier in project OpenPDF by LibrePDF.

the class PdfPublicKeySecurityHandler method computeRecipientInfo.

private KeyTransRecipientInfo computeRecipientInfo(X509Certificate x509certificate, byte[] abyte0) throws GeneralSecurityException, IOException {
    ASN1InputStream asn1inputstream = new ASN1InputStream(new ByteArrayInputStream(x509certificate.getTBSCertificate()));
    TBSCertificate tbsCertificate = TBSCertificate.getInstance(asn1inputstream.readObject());
    AlgorithmIdentifier algorithmidentifier = tbsCertificate.getSubjectPublicKeyInfo().getAlgorithm();
    IssuerAndSerialNumber issuerandserialnumber = new IssuerAndSerialNumber(tbsCertificate.getIssuer(), tbsCertificate.getSerialNumber().getValue());
    Cipher cipher = Cipher.getInstance(algorithmidentifier.getAlgorithm().getId());
    cipher.init(1, x509certificate);
    DEROctetString deroctetstring = new DEROctetString(cipher.doFinal(abyte0));
    RecipientIdentifier recipId = new RecipientIdentifier(issuerandserialnumber);
    return new KeyTransRecipientInfo(recipId, algorithmidentifier, deroctetstring);
}
Also used : IssuerAndSerialNumber(org.bouncycastle.asn1.cms.IssuerAndSerialNumber) ASN1InputStream(org.bouncycastle.asn1.ASN1InputStream) KeyTransRecipientInfo(org.bouncycastle.asn1.cms.KeyTransRecipientInfo) ByteArrayInputStream(java.io.ByteArrayInputStream) Cipher(javax.crypto.Cipher) RecipientIdentifier(org.bouncycastle.asn1.cms.RecipientIdentifier) TBSCertificate(org.bouncycastle.asn1.x509.TBSCertificate) DEROctetString(org.bouncycastle.asn1.DEROctetString) AlgorithmIdentifier(org.bouncycastle.asn1.x509.AlgorithmIdentifier)

Example 4 with RecipientIdentifier

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

the class KeyTransRecipientInfoGenerator method generate.

public final RecipientInfo generate(GenericKey contentEncryptionKey) throws CMSException {
    byte[] encryptedKeyBytes;
    try {
        encryptedKeyBytes = wrapper.generateWrappedKey(contentEncryptionKey);
    } catch (OperatorException e) {
        throw new CMSException("exception wrapping content key: " + e.getMessage(), e);
    }
    RecipientIdentifier recipId;
    if (issuerAndSerial != null) {
        recipId = new RecipientIdentifier(issuerAndSerial);
    } else {
        recipId = new RecipientIdentifier(new DEROctetString(subjectKeyIdentifier));
    }
    return new RecipientInfo(new KeyTransRecipientInfo(recipId, wrapper.getAlgorithmIdentifier(), new DEROctetString(encryptedKeyBytes)));
}
Also used : KeyTransRecipientInfo(com.github.zhenwei.pkix.util.asn1.cms.KeyTransRecipientInfo) RecipientIdentifier(com.github.zhenwei.pkix.util.asn1.cms.RecipientIdentifier) KeyTransRecipientInfo(com.github.zhenwei.pkix.util.asn1.cms.KeyTransRecipientInfo) RecipientInfo(com.github.zhenwei.pkix.util.asn1.cms.RecipientInfo) OperatorException(com.github.zhenwei.pkix.operator.OperatorException) DEROctetString(com.github.zhenwei.core.asn1.DEROctetString)

Example 5 with RecipientIdentifier

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

the class PdfPublicKeySecurityHandler method computeRecipientInfo.

private KeyTransRecipientInfo computeRecipientInfo(X509Certificate x509certificate, byte[] abyte0) throws GeneralSecurityException, IOException {
    ASN1InputStream asn1inputstream = new ASN1InputStream(new ByteArrayInputStream(x509certificate.getTBSCertificate()));
    TBSCertificateStructure tbscertificatestructure = TBSCertificateStructure.getInstance(asn1inputstream.readObject());
    AlgorithmIdentifier algorithmidentifier = tbscertificatestructure.getSubjectPublicKeyInfo().getAlgorithm();
    IssuerAndSerialNumber issuerandserialnumber = new IssuerAndSerialNumber(tbscertificatestructure.getIssuer(), tbscertificatestructure.getSerialNumber().getValue());
    Cipher cipher = Cipher.getInstance(algorithmidentifier.getAlgorithm().getId());
    cipher.init(1, x509certificate);
    DEROctetString deroctetstring = new DEROctetString(cipher.doFinal(abyte0));
    RecipientIdentifier recipId = new RecipientIdentifier(issuerandserialnumber);
    return new KeyTransRecipientInfo(recipId, algorithmidentifier, deroctetstring);
}
Also used : IssuerAndSerialNumber(org.bouncycastle.asn1.cms.IssuerAndSerialNumber) ASN1InputStream(org.bouncycastle.asn1.ASN1InputStream) KeyTransRecipientInfo(org.bouncycastle.asn1.cms.KeyTransRecipientInfo) ByteArrayInputStream(java.io.ByteArrayInputStream) TBSCertificateStructure(org.bouncycastle.asn1.x509.TBSCertificateStructure) Cipher(javax.crypto.Cipher) RecipientIdentifier(org.bouncycastle.asn1.cms.RecipientIdentifier) DEROctetString(org.bouncycastle.asn1.DEROctetString) AlgorithmIdentifier(org.bouncycastle.asn1.x509.AlgorithmIdentifier)

Aggregations

Cipher (javax.crypto.Cipher)4 ASN1InputStream (org.bouncycastle.asn1.ASN1InputStream)4 DEROctetString (org.bouncycastle.asn1.DEROctetString)4 IssuerAndSerialNumber (org.bouncycastle.asn1.cms.IssuerAndSerialNumber)4 KeyTransRecipientInfo (org.bouncycastle.asn1.cms.KeyTransRecipientInfo)4 RecipientIdentifier (org.bouncycastle.asn1.cms.RecipientIdentifier)4 AlgorithmIdentifier (org.bouncycastle.asn1.x509.AlgorithmIdentifier)4 TBSCertificate (org.bouncycastle.asn1.x509.TBSCertificate)3 ByteArrayInputStream (java.io.ByteArrayInputStream)2 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)2 NoSuchPaddingException (javax.crypto.NoSuchPaddingException)2 DEROctetString (com.github.zhenwei.core.asn1.DEROctetString)1 OperatorException (com.github.zhenwei.pkix.operator.OperatorException)1 KeyTransRecipientInfo (com.github.zhenwei.pkix.util.asn1.cms.KeyTransRecipientInfo)1 RecipientIdentifier (com.github.zhenwei.pkix.util.asn1.cms.RecipientIdentifier)1 RecipientInfo (com.github.zhenwei.pkix.util.asn1.cms.RecipientInfo)1 TBSCertificateStructure (org.bouncycastle.asn1.x509.TBSCertificateStructure)1