List of usage examples for org.bouncycastle.asn1.cms EnvelopedData EnvelopedData
public EnvelopedData(OriginatorInfo originatorInfo, ASN1Set recipientInfos,
EncryptedContentInfo encryptedContentInfo, Attributes unprotectedAttrs)
From source file:cljpdf.text.pdf.PdfPublicKeySecurityHandler.java
License:Mozilla Public License
private DERObject 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); DERObject derobject = asn1inputstream.readObject(); KeyGenerator keygenerator = KeyGenerator.getInstance(s); keygenerator.init(128);//from w ww . java 2 s . c o m 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 DERObjectIdentifier(s), derobject); EncryptedContentInfo encryptedcontentinfo = new EncryptedContentInfo(PKCSObjectIdentifiers.data, algorithmidentifier, deroctetstring); EnvelopedData env = new EnvelopedData(null, derset, encryptedcontentinfo, null); ContentInfo contentinfo = new ContentInfo(PKCSObjectIdentifiers.envelopedData, env); return contentinfo.getDERObject(); }
From source file:com.itextpdf.kernel.crypto.securityhandler.PubKeySecurityHandler.java
License:Open Source License
private ASN1Primitive createDERForRecipient(byte[] in, X509Certificate cert) throws IOException, GeneralSecurityException { EncryptionUtils.DERForRecipientParams parameters = EncryptionUtils.calculateDERForRecipientParams(in); KeyTransRecipientInfo keytransrecipientinfo = computeRecipientInfo(cert, parameters.abyte0); DEROctetString deroctetstring = new DEROctetString(parameters.abyte1); DERSet derset = new DERSet(new RecipientInfo(keytransrecipientinfo)); EncryptedContentInfo encryptedcontentinfo = new EncryptedContentInfo(PKCSObjectIdentifiers.data, parameters.algorithmIdentifier, deroctetstring); EnvelopedData env = new EnvelopedData(null, derset, encryptedcontentinfo, (ASN1Set) null); ContentInfo contentinfo = new ContentInfo(PKCSObjectIdentifiers.envelopedData, env); return contentinfo.toASN1Primitive(); }
From source file:com.itextpdf.text.pdf.PdfPublicKeySecurityHandler.java
License:Open Source License
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);/*ww w .j av a2s .co m*/ 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); ASN1Set set = null; EnvelopedData env = new EnvelopedData(null, derset, encryptedcontentinfo, set); ContentInfo contentinfo = new ContentInfo(PKCSObjectIdentifiers.envelopedData, env); return contentinfo.toASN1Primitive(); }
From source file:es.gob.afirma.envelopers.cades.CAdESEnvelopedData.java
License:Open Source License
/** Método que genera la firma de tipo EnvelopedData. * @param parameters//from w ww .j a v a 2 s . c om * Parámetros necesarios para la generación de este * tipo. * @param config * Configuración del algoritmo para firmar * @param certDest * Certificado del destino al cual va dirigido la firma. * @param dataType * Identifica el tipo del contenido a firmar. * @return la firma de tipo EnvelopedData. * @throws java.io.IOException * Si ocurre algún problema leyendo o escribiendo los * datos * @throws java.security.cert.CertificateEncodingException * Si se produce alguna excepción con los certificados de * firma. * @throws java.security.NoSuchAlgorithmException * Si no se soporta alguno de los algoritmos de firma o huella * digital */ byte[] genEnvelopedData(final P7ContentSignerParameters parameters, final X509Certificate[] signerCertificateChain, final AOCipherConfig config, final X509Certificate[] certDest, final String dataType) throws IOException, CertificateEncodingException, NoSuchAlgorithmException { this.cipherKey = CAdESUtils.initEnvelopedData(config, certDest); // Datos previos útiles final String digestAlgorithm = AOSignConstants.getDigestAlgorithmName(parameters.getSignatureAlgorithm()); // 1. ORIGINATORINFO // obtenemos la lista de certificados final ASN1Set certificates = CAdESUtils.fetchCertificatesList(signerCertificateChain); final ASN1Set certrevlist = null; OriginatorInfo origInfo = null; if (signerCertificateChain.length != 0) { origInfo = new OriginatorInfo(certificates, certrevlist); } // 2. RECIPIENTINFOS final Info infos = CAdESUtils.getEnvelopeInfo(parameters.getContent(), config, certDest, this.cipherKey); // 3. ATRIBUTOS final ASN1Set unprotectedAttrs = SigUtils.getAttributeSet(new AttributeTable( CAdESUtils.initContexExpecific(digestAlgorithm, parameters.getContent(), dataType, null))); // construimos el Enveloped Data y lo devolvemos return new ContentInfo(PKCSObjectIdentifiers.envelopedData, new EnvelopedData(origInfo, new DERSet(infos.getRecipientInfos()), infos.getEncInfo(), unprotectedAttrs)) .getEncoded(ASN1Encoding.DER); }
From source file:es.gob.afirma.envelopers.cades.CAdESEnvelopedData.java
License:Open Source License
/** Método que genera la firma de tipo EnvelopedData. * @param data/* w w w . java2 s.co m*/ * Datos binarios a firmar * @param digestAlg * Algoritmo de hash * @param config * Configuración del algoritmo para firmar * @param certDest * Certificado del destino al cual va dirigido la firma. * @param dataType * Identifica el tipo del contenido a firmar. * @return la firma de tipo EnvelopedData. * @throws java.io.IOException * Si hay problemas en la lectura de datos * @throws java.security.cert.CertificateEncodingException * Cuando el certificado proporcionado no está codificado * adecuadamente * @throws java.security.NoSuchAlgorithmException * Si no se soporta alguno de los algoritmos indicados */ byte[] genEnvelopedData(final byte[] data, final String digestAlg, final AOCipherConfig config, final X509Certificate[] certDest, final String dataType) throws IOException, CertificateEncodingException, NoSuchAlgorithmException { this.cipherKey = CAdESUtils.initEnvelopedData(config, certDest); // Datos previos útiles final String digestAlgorithm = AOSignConstants.getDigestAlgorithmName(digestAlg); // 1. ORIGINATORINFO final OriginatorInfo origInfo = null; // 2. RECIPIENTINFOS final Info infos = CAdESUtils.getEnvelopeInfo(data, config, certDest, this.cipherKey); // 3. ATRIBUTOS final ASN1Set unprotectedAttrs = SigUtils.getAttributeSet( new AttributeTable(CAdESUtils.initContexExpecific(digestAlgorithm, data, dataType, null))); // construimos el Enveloped Data y lo devolvemos return new ContentInfo(PKCSObjectIdentifiers.envelopedData, new EnvelopedData(origInfo, new DERSet(infos.getRecipientInfos()), infos.getEncInfo(), unprotectedAttrs)) .getEncoded(ASN1Encoding.DER); }
From source file:es.gob.afirma.envelopers.cms.CMSEnvelopedData.java
License:Open Source License
/** Genera una estructura PKCS#7 <code>EnvelopedData</code>. * @param parameters Parámetros necesarios para la generación de este * tipo./*from w w w .j a v a 2s .co m*/ * @param signerCertificateChain Cadena de certificados del firmante. * @param config Configuración del algoritmo para firmar * @param certDest Certificado del destino al cual va dirigido la firma. * @param dataType Identifica el tipo del contenido a firmar. * @param uatrib Conjunto de atributos no firmados. * @param keySize Tamaño de la clave AES. * @return la firma de tipo EnvelopedData. * @throws java.io.IOException Si ocurre algún problema leyendo o escribiendo los * datos * @throws java.security.cert.CertificateEncodingException Si se produce alguna excepción con los certificados de * firma. * @throws java.security.NoSuchAlgorithmException Si no se soporta alguno de los algoritmos de firma o huella * digital * @throws BadPaddingException Cuando hay problemas con un relleno de datos. * @throws IllegalBlockSizeException Cuando hay problemas internos con los tamaños de bloque de cifrado. * @throws InvalidAlgorithmParameterException Si no se soporta un parámetro necesario para un algoritmo. * @throws NoSuchPaddingException Cuando no se soporta un tipo de relleno necesario. * @throws InvalidKeyException Cuando hay problemas de adecuación de la clave. */ byte[] genEnvelopedData(final P7ContentSignerParameters parameters, final X509Certificate[] signerCertificateChain, final AOCipherConfig config, final X509Certificate[] certDest, final String dataType, final Map<String, byte[]> uatrib, final Integer keySize) throws IOException, CertificateEncodingException, NoSuchAlgorithmException, InvalidKeyException, NoSuchPaddingException, InvalidAlgorithmParameterException, IllegalBlockSizeException, BadPaddingException { this.cipherKey = Utils.initEnvelopedData(config, keySize); // Ya que el contenido puede ser grande, lo recuperamos solo una vez final byte[] content2 = parameters.getContent(); // Datos previos útiles final String digestAlgorithm = AOSignConstants.getDigestAlgorithmName(parameters.getSignatureAlgorithm()); // 1. ORIGINATORINFO // obtenemos la lista de certificados final ASN1Set certificates = Utils.fetchCertificatesList(signerCertificateChain); final ASN1Set certrevlist = null; OriginatorInfo origInfo = null; if (signerCertificateChain.length != 0) { origInfo = new OriginatorInfo(certificates, certrevlist); } // 2. RECIPIENTINFOS final Info infos = Utils.initVariables(content2, config, certDest, this.cipherKey); // 4. ATRIBUTOS final ASN1Set unprotectedAttrs = Utils.generateSignerInfo(digestAlgorithm, content2, dataType, uatrib); // construimos el Enveloped Data y lo devolvemos return new ContentInfo(PKCSObjectIdentifiers.envelopedData, new EnvelopedData(origInfo, new DERSet(infos.getRecipientInfos()), infos.getEncInfo(), unprotectedAttrs)) .getEncoded(ASN1Encoding.DER); }
From source file:es.gob.afirma.envelopers.cms.CMSEnvelopedData.java
License:Open Source License
/** Genera una estructura PKCS#7 <code>EnvelopedData</code>. * @param data Datos binarios a firmar.// ww w. j av a 2 s. com * @param digestAlg Algoritmo de huella digital. * @param config Configuración del algoritmo para firmar. * @param certDest Certificado del destino al cual va dirigido la firma. * @param dataType Identifica el tipo del contenido a firmar. * @param uatrib Conjunto de atributos no firmados. * @param keySize Tamaño de la clave AES. * @return la firma de tipo EnvelopedData. * @throws java.io.IOException En caso de error en la lectura o tratamiento de datos * @throws java.security.cert.CertificateEncodingException Cuando hay problemas relacionados con la * codificación de los certificados X.509. * @throws java.security.NoSuchAlgorithmException Si el JRE no soporta algún algoritmo necesario * @throws BadPaddingException Cuando hay problemas con un relleno de datos. * @throws IllegalBlockSizeException Cuando hay problemas internos con los tamaños de bloque de cifrado. * @throws InvalidAlgorithmParameterException Si no se soporta un parámetro necesario para un algoritmo. * @throws NoSuchPaddingException Cuando no se soporta un tipo de relleno necesario. * @throws InvalidKeyException Cuando hay problemas de adecuación de la clave. */ byte[] genEnvelopedData(final byte[] data, final String digestAlg, final AOCipherConfig config, final X509Certificate[] certDest, final String dataType, final Map<String, byte[]> uatrib, final Integer keySize) throws IOException, CertificateEncodingException, NoSuchAlgorithmException, InvalidKeyException, NoSuchPaddingException, InvalidAlgorithmParameterException, IllegalBlockSizeException, BadPaddingException { this.cipherKey = Utils.initEnvelopedData(config, keySize); // Datos previos utiles final String digestAlgorithm = AOSignConstants.getDigestAlgorithmName(digestAlg); // 1. ORIGINATORINFO final OriginatorInfo origInfo = null; // 2. RECIPIENTINFOS final Info infos = Utils.initVariables(data, config, certDest, this.cipherKey); // 4. ATRIBUTOS final ASN1Set unprotectedAttrs = Utils.generateSignerInfo(digestAlgorithm, data, dataType, uatrib); // construimos el Enveloped Data y lo devolvemos return new ContentInfo(PKCSObjectIdentifiers.envelopedData, new EnvelopedData(origInfo, new DERSet(infos.getRecipientInfos()), infos.getEncInfo(), unprotectedAttrs)) .getEncoded(ASN1Encoding.DER); }
From source file:es.gob.afirma.envelopers.cms.CMSEnvelopedData.java
License:Open Source License
/** Método que inserta remitentes en el "OriginatorInfo" de un sobre * de tipo envelopedData.//w w w .j av a 2s . c o m * @param data * Datos CMS que admiten multiples remitentes/firmantes. * @param signerCertificateChain * Cadena de certificados a agregar. * @return La nueva firma enveloped con los remitentes que tenía (si * los tuviera) con la cadena de certificados nueva. * @throws IOException Si hay errores de lectura de datos * @throws CertificateEncodingException Cuando el certificado proporcionado es inválido */ public static byte[] addOriginatorInfo(final byte[] data, final X509Certificate[] signerCertificateChain) throws IOException, CertificateEncodingException { final ASN1InputStream is = new ASN1InputStream(data); // LEEMOS EL FICHERO QUE NOS INTRODUCEN final ASN1Sequence dsq = (ASN1Sequence) is.readObject(); is.close(); final Enumeration<?> e = dsq.getObjects(); // Elementos que contienen los elementos OID Data final ASN1ObjectIdentifier doi = (ASN1ObjectIdentifier) e.nextElement(); if (doi.equals(PKCSObjectIdentifiers.envelopedData)) { // Contenido de Data final ASN1TaggedObject doj = (ASN1TaggedObject) e.nextElement(); final EnvelopedData ed = EnvelopedData.getInstance(doj.getObject()); // Obtenemos los originatorInfo OriginatorInfo origInfo = ed.getOriginatorInfo(); ASN1Set certs = null; if (origInfo != null) { certs = origInfo.getCertificates(); } // Si no hay certificados, se deja como esta. final OriginatorInfo origInfoChecked = Utils.checkCertificates(signerCertificateChain, certs); if (origInfoChecked != null) { origInfo = origInfoChecked; } // Se crea un nuevo EnvelopedData a partir de los datos // anteriores con los nuevos originantes. return new ContentInfo(PKCSObjectIdentifiers.envelopedData, new EnvelopedData(origInfo, ed.getRecipientInfos(), ed.getEncryptedContentInfo(), ed.getUnprotectedAttrs())) .getEncoded(ASN1Encoding.DER); } return null; }