List of usage examples for org.bouncycastle.asn1.cms RecipientInfo RecipientInfo
public RecipientInfo(ASN1Primitive info)
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);/*ww w. j a v a2 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);/*from ww w .ja va 2s . 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 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.CAdESUtils.java
License:Open Source License
/** Obtiene un <code>Info</code> que contiene los RecipientInfos y el EncryptedContentInfo. * @param data Datos a incluir en el sobre * @param config Configuración de cifrado a aplicar * @param certDest Certificados de los destinatarios * @param cipherKey Clave de cifrado//from w w w . ja va 2 s . co m * @return <code>Info</code> que contiene los RecipientInfos y el EncryptedContentInfo * @throws IOException en caso de error de entrada / salida * @throws CertificateEncodingException en caso de errores de codificación en los certificados */ static Info getEnvelopeInfo(final byte[] data, final AOCipherConfig config, final X509Certificate[] certDest, final SecretKey cipherKey) throws IOException, CertificateEncodingException { // Reiniciamos las dos variables final Info infos = new Info(); final ASN1EncodableVector recipientInfos = new ASN1EncodableVector(); X509Certificate cert; TBSCertificateStructure tbs; IssuerAndSerialNumber isse; RecipientIdentifier rid; PublicKey pubKey; AlgorithmIdentifier keyEncAlg; SubjectPublicKeyInfo info; // Cifrado de la clave byte[] encryptedKey = null; // generamos el contenedor de cifrado RecipientInfo recipient = null; for (final X509Certificate element : certDest) { cert = element; tbs = TBSCertificateStructure.getInstance(ASN1Primitive.fromByteArray(cert.getTBSCertificate())); // Obtenemos el Isuer & serial number isse = new IssuerAndSerialNumber(X500Name.getInstance(tbs.getIssuer()), tbs.getSerialNumber().getValue()); // Creamos el recipientInfo rid = new RecipientIdentifier(isse); // Obtenemos la clave publica pubKey = cert.getPublicKey(); // obtenemos la informacion de la clave publica info = tbs.getSubjectPublicKeyInfo(); // obtenemos el algoritmo de cifrado. keyEncAlg = info.getAlgorithm(); try { // ciframos la clave encryptedKey = cipherKey(pubKey, cipherKey); } catch (final Exception e) { LOGGER.severe("Error durante el proceso cifrado de la clave: " + e); //$NON-NLS-1$ } // creamos el recipiente con los datos del destinatario. final KeyTransRecipientInfo keyTransRecipientInfo = new KeyTransRecipientInfo(rid, keyEncAlg, new DEROctetString(encryptedKey)); recipient = new RecipientInfo(keyTransRecipientInfo); // Lo añadimos al recipiente de destinatarios. recipientInfos.add(recipient); } // 3. ENCRIPTEDCONTENTINFO try { infos.setEncInfo(getEncryptedContentInfo(data, config, cipherKey)); } catch (final Exception e) { LOGGER.severe("Error durante el proceso cifrado de la clave: " + e); //$NON-NLS-1$ } infos.setRecipientInfos(recipientInfos); return infos; }
From source file:es.gob.afirma.envelopers.cms.Utils.java
License:Open Source License
/** * Crea la estructura interna para el ensobrado de datos. * @param data Datos que se desean ensobrar. * @param config Configración para el cifrado. * @param certDest Certificados de los destinatarios del sobre. * @param cipherKey Clave para la identificación del remitente.. * @return Objeto con la información para la generación del sobre. * @throws IOException Si ocurre algún problema leyendo o escribiendo los * datos./*from ww w .j a v a 2s .co m*/ * @throws CertificateEncodingException Si se produce alguna excepción * con los certificados de los usuarios. * @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 NoSuchAlgorithmException Cuando el JRE no soporta algún algoritmo necesario. * @throws InvalidKeyException Cuando hay problemas de adecuación de la clave. * @throws BadPaddingException Cuando hay problemas con un relleno de datos. */ static Info initVariables(final byte[] data, final AOCipherConfig config, final X509Certificate[] certDest, final SecretKey cipherKey) throws CertificateEncodingException, IOException, InvalidKeyException, NoSuchAlgorithmException, NoSuchPaddingException, InvalidAlgorithmParameterException, IllegalBlockSizeException, BadPaddingException { // Reiniciamos las dos variables final Info infos = new Info(); final ASN1EncodableVector recipientInfos = new ASN1EncodableVector(); for (final X509Certificate element : certDest) { final TBSCertificateStructure tbs = TBSCertificateStructure .getInstance(ASN1Primitive.fromByteArray(element.getTBSCertificate())); // creamos el recipiente con los datos del destinatario. final KeyTransRecipientInfo keyTransRecipientInfo = new KeyTransRecipientInfo( // Creamos el recipientInfo new RecipientIdentifier( // Obtenemos el issuer & serial number new IssuerAndSerialNumber(X500Name.getInstance(tbs.getIssuer()), tbs.getSerialNumber().getValue())), // obtenemos el algoritmo de cifrado (RSA / DSA). tbs.getSubjectPublicKeyInfo().getAlgorithm(), new DEROctetString(cipherKey( // Obtenemos la clave publica element.getPublicKey(), cipherKey))); // Lo anadimos al recipiente de destinatarios. recipientInfos.add(new RecipientInfo(keyTransRecipientInfo)); } // 3. ENCRIPTEDCONTENTINFO infos.setEncInfo(getEncryptedContentInfo(data, config, cipherKey)); infos.setRecipientInfos(recipientInfos); return infos; }