List of usage examples for org.bouncycastle.asn1.cms EncryptedContentInfo getInstance
public static EncryptedContentInfo getInstance(Object obj)
From source file:es.gob.afirma.applet.CMSInformation.java
License:Open Source License
/** * Obtiene la información de diferentes tipos de formatos. * @param doj Etiqueta ASN.1 de la que se obtienen los datos. * @param envelopeType Tipo de formato: * <li>0: EnvelopedData</li> * <li>1: AuthenticatedData</li> * <li>2: AuthEnvelopedData</li> * <li>3: SignedAndEnvelopedData</li> * <li>4: SignedData</li>/*from w w w. j a v a 2 s . c o m*/ * <li>5: Encrypted</li> * @param tipoDetalle Tipo de datos (literal) * @param signBinaryType Tipo de firmado binario (CADES o CMS) * @return Representación de los datos. */ private static String extractData(final ASN1TaggedObject doj, final int envelopeType, final String tipoDetalle, final int signBinaryType) { String detalle = ""; //$NON-NLS-1$ detalle = detalle + tipoDetalle + CR; ASN1Set rins = null; EncryptedContentInfo encryptedContentInfo = null; ASN1Set unprotectedAttrs = null; ASN1Integer version = null; AlgorithmIdentifier aid = null; ContentInfo ci = null; ASN1Set authAttrs = null; ASN1Set ds = null; ASN1Set signerInfosSd = null; switch (envelopeType) { case TYPE_ENVELOPED_DATA: final EnvelopedData enveloped = EnvelopedData.getInstance(doj.getObject()); version = enveloped.getVersion(); rins = enveloped.getRecipientInfos(); encryptedContentInfo = enveloped.getEncryptedContentInfo(); unprotectedAttrs = enveloped.getUnprotectedAttrs(); break; case TYPE_AUTHENTICATED_DATA: final AuthenticatedData authenticated = AuthenticatedData.getInstance(doj.getObject()); version = authenticated.getVersion(); rins = authenticated.getRecipientInfos(); aid = authenticated.getMacAlgorithm(); ci = authenticated.getEncapsulatedContentInfo(); authAttrs = authenticated.getAuthAttrs(); unprotectedAttrs = authenticated.getUnauthAttrs(); break; case TYPE_AUTHENTICATED_ENVELOPED_DATA: final AuthEnvelopedData authEnveloped = AuthEnvelopedData.getInstance(doj.getObject()); version = authEnveloped.getVersion(); rins = authEnveloped.getRecipientInfos(); encryptedContentInfo = authEnveloped.getAuthEncryptedContentInfo(); authAttrs = authEnveloped.getAuthAttrs(); unprotectedAttrs = authEnveloped.getUnauthAttrs(); break; case TYPE_SIGNED_ENVELOPED_DATA: final SignedAndEnvelopedData signedEnv = new SignedAndEnvelopedData((ASN1Sequence) doj.getObject()); version = signedEnv.getVersion(); rins = signedEnv.getRecipientInfos(); encryptedContentInfo = signedEnv.getEncryptedContentInfo(); signerInfosSd = signedEnv.getSignerInfos(); break; case TYPE_SIGNED_DATA: final SignedData signed = SignedData.getInstance(doj.getObject()); version = signed.getVersion(); ds = signed.getDigestAlgorithms(); ci = signed.getEncapContentInfo(); signerInfosSd = signed.getSignerInfos(); break; case TYPE_ENCRYPTED_DATA: final ASN1Sequence encrypted = (ASN1Sequence) doj.getObject(); version = ASN1Integer.getInstance(encrypted.getObjectAt(0)); encryptedContentInfo = EncryptedContentInfo.getInstance(encrypted.getObjectAt(1)); if (encrypted.size() == 3) { unprotectedAttrs = (ASN1Set) encrypted.getObjectAt(2); } break; default: throw new IllegalArgumentException("Tipo de sobre no soportado: " + envelopeType); //$NON-NLS-1$ } //obtenemos la version detalle = detalle + AppletMessages.getString("CMSInformation.1") + SP + version + CR; //$NON-NLS-1$ //recipientInfo if (rins != null) { if (envelopeType != TYPE_SIGNED_DATA && envelopeType != TYPE_ENCRYPTED_DATA && rins.size() > 0) { detalle = detalle + AppletMessages.getString("CMSInformation.13") + CR; //$NON-NLS-1$ } for (int i = 0; i < rins.size(); i++) { final KeyTransRecipientInfo kti = KeyTransRecipientInfo .getInstance(RecipientInfo.getInstance(rins.getObjectAt(i)).getInfo()); detalle = detalle + AppletMessages.getString("CMSInformation.14") + SP + (i + 1) + ":" + CR; //$NON-NLS-1$//$NON-NLS-2$ final AlgorithmIdentifier diAlg = kti.getKeyEncryptionAlgorithm(); //issuer y serial final IssuerAndSerialNumber iss = (IssuerAndSerialNumber) SignerIdentifier .getInstance(kti.getRecipientIdentifier().getId()).getId(); detalle = detalle + TB + AppletMessages.getString("CMSInformation.15") + SP //$NON-NLS-1$ + iss.getName().toString() + CR; detalle = detalle + TB + AppletMessages.getString("CMSInformation.16") + SP + iss.getSerialNumber() //$NON-NLS-1$ + CR; // el algoritmo de cifrado de los datos AOCipherAlgorithm algorithm = null; final AOCipherAlgorithm[] algos = AOCipherAlgorithm.values(); // obtenemos el algoritmo usado para cifrar la pass for (final AOCipherAlgorithm algo : algos) { if (algo.getOid().equals(diAlg.getAlgorithm().toString())) { algorithm = algo; } } if (algorithm != null) { detalle = detalle + TB + AppletMessages.getString("CMSInformation.17") + SP //$NON-NLS-1$ + algorithm.getName() + CR; } else { detalle = detalle + TB + AppletMessages.getString("CMSInformation.18") + SP //$NON-NLS-1$ + diAlg.getAlgorithm() + CR; } } } if (envelopeType == TYPE_ENVELOPED_DATA || envelopeType == TYPE_ENCRYPTED_DATA) { //obtenemos datos de los datos cifrados. detalle = detalle + AppletMessages.getString("CMSInformation.19") + CR; //$NON-NLS-1$ detalle = detalle + getEncryptedContentInfo(encryptedContentInfo); } else if (envelopeType == TYPE_AUTHENTICATED_DATA && aid != null && ci != null) { // mac algorithm detalle = detalle + AppletMessages.getString("CMSInformation.20") + SP + aid.getAlgorithm() + CR; //$NON-NLS-1$ //digestAlgorithm final ASN1Sequence seq = (ASN1Sequence) doj.getObject(); final ASN1TaggedObject da = (ASN1TaggedObject) seq.getObjectAt(4); final AlgorithmIdentifier dai = AlgorithmIdentifier.getInstance(da.getObject()); detalle = detalle + AppletMessages.getString("CMSInformation.21") + SP + dai.getAlgorithm() + CR; //$NON-NLS-1$ //obtenemos datos de los datos cifrados. detalle = detalle + AppletMessages.getString("CMSInformation.22") + SP + ci.getContentType() + CR; //$NON-NLS-1$ detalle = getObligatorieAtrib(signBinaryType, detalle, authAttrs); } else if (envelopeType == TYPE_AUTHENTICATED_ENVELOPED_DATA) { detalle = detalle + AppletMessages.getString("CMSInformation.19") + CR; //$NON-NLS-1$ detalle = detalle + getEncryptedContentInfo(encryptedContentInfo); detalle = getObligatorieAtrib(signBinaryType, detalle, authAttrs); } else if (envelopeType == TYPE_SIGNED_ENVELOPED_DATA) { //algoritmo de firma final ASN1Sequence seq = (ASN1Sequence) doj.getObject(); final ASN1Set da = (ASN1Set) seq.getObjectAt(2); final AlgorithmIdentifier dai = AlgorithmIdentifier.getInstance(da.getObjectAt(0)); detalle = detalle + AppletMessages.getString("CMSInformation.21") + SP + dai.getAlgorithm() + CR; //$NON-NLS-1$ //obtenemos datos de los datos cifrados. detalle = detalle + AppletMessages.getString("CMSInformation.19") + CR; //$NON-NLS-1$ detalle = detalle + getEncryptedContentInfo(encryptedContentInfo); } else if (envelopeType == TYPE_SIGNED_DATA && ci != null && ds != null) { //algoritmo de firma final AlgorithmIdentifier dai = AlgorithmIdentifier.getInstance(ds.getObjectAt(0)); detalle = detalle + AppletMessages.getString("CMSInformation.21") + SP + dai.getAlgorithm() + CR; //$NON-NLS-1$ detalle = detalle + AppletMessages.getString("CMSInformation.22") + SP + ci.getContentType() + CR; //$NON-NLS-1$ } //obtenemos lo atributos opcionales if (envelopeType != TYPE_SIGNED_ENVELOPED_DATA) { if (unprotectedAttrs == null) { detalle = detalle + AppletMessages.getString("CMSInformation.28") + CR; //$NON-NLS-1$ } else { final String atributos = getUnSignedAttributes(unprotectedAttrs.getObjects()); detalle = detalle + AppletMessages.getString("CMSInformation.29") + CR; //$NON-NLS-1$ detalle = detalle + atributos; } } else if ((envelopeType == TYPE_SIGNED_ENVELOPED_DATA || envelopeType == TYPE_SIGNED_DATA) && signerInfosSd != null) { //obtenemos el(los) firmate(s) if (signerInfosSd.size() > 0) { detalle = detalle + AppletMessages.getString("CMSInformation.30") + CR; //$NON-NLS-1$ } for (int i = 0; i < signerInfosSd.size(); i++) { final SignerInfo si = SignerInfo.getInstance(signerInfosSd.getObjectAt(i)); detalle = detalle + AppletMessages.getString("CMSInformation.31") + SP + (i + 1) + ":" + CR; //$NON-NLS-1$//$NON-NLS-2$ // version detalle = detalle + TB + AppletMessages.getString("CMSInformation.1") + SP + si.getVersion() + CR; //$NON-NLS-1$ //signerIdentifier final SignerIdentifier sident = si.getSID(); final IssuerAndSerialNumber iss = IssuerAndSerialNumber.getInstance(sident.getId()); detalle = detalle + TB + AppletMessages.getString("CMSInformation.15") + SP //$NON-NLS-1$ + iss.getName().toString() + CR; detalle = detalle + TB + AppletMessages.getString("CMSInformation.16") + SP + iss.getSerialNumber() //$NON-NLS-1$ + CR; //digestAlgorithm final AlgorithmIdentifier algId = si.getDigestAlgorithm(); detalle = detalle + TB + AppletMessages.getString("CMSInformation.35") + SP + algId.getAlgorithm() //$NON-NLS-1$ + CR; //obtenemos lo atributos obligatorios final ASN1Set sa = si.getAuthenticatedAttributes(); String satributes = ""; //$NON-NLS-1$ if (sa != null) { satributes = getsignedAttributes(sa, signBinaryType); } detalle = detalle + TB + AppletMessages.getString("CMSInformation.36") + CR; //$NON-NLS-1$ detalle = detalle + satributes; } } return detalle; }
From source file:es.gob.afirma.envelopers.cms.CMSDecipherEncryptedData.java
License:Open Source License
/** Método principal que descifra datos del tipo de EncryptedData. * @param encryptedData/* w w w. j a va2 s.co m*/ * Datos del tipo CMS EncryptedData. * @param pass * Contraseña o clave que se uso para cifrar los datos. * @return Datos sin encriptar. * @throws AOException * Cuando ocurre un error durante el proceso de descifrado * (formato o clave incorrecto,...) * @throws InvalidKeyException * Cuando se proporciona una clave incorrecta para el * descifrado. * @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 NoSuchAlgorithmException Si el JRE no soporta algún algoritmo necesario * @throws IOException En caso de error en la lectura o tratamiento de datos * @throws InvalidKeySpecException Cuando ocurren problemas relacionados con la estructura interna de las claves */ @SuppressWarnings("unused") byte[] dechiperEncryptedData(final byte[] encryptedData, final String pass) throws AOException, InvalidKeyException, NoSuchAlgorithmException, NoSuchPaddingException, InvalidAlgorithmParameterException, IllegalBlockSizeException, BadPaddingException, InvalidKeySpecException, IOException { AlgorithmIdentifier alg = null; EncryptedContentInfo eci = null; // donde se guardara el resultad. final byte[] deciphered; try { final ASN1Sequence contentEncryptedData = Utils.fetchWrappedData(encryptedData); // Obtenemos los datos del encryptedData. final Enumeration<?> e2 = contentEncryptedData.getObjects(); // version e2.nextElement(); // EncryptedContentInfo. donde esta lo que necesitamos. eci = EncryptedContentInfo.getInstance(e2.nextElement()); // Obtenemos el agoritmo de cifrado alg = eci.getContentEncryptionAlgorithm(); // Se intenta obtener el encrypted data. // Si no puede convertirse, dara error. // "EncryptedData EncryptedData" no se usara. solo es para verificar // que es de este tipo. new EncryptedData(eci); } catch (final Exception ex) { throw new AOException("El fichero no contiene un tipo EncryptedData", ex); //$NON-NLS-1$ } // asignamos la clave de descifrado a partir del algoritmo. assignKey(alg, pass); // Obtenemos el contenido cifrado. final byte[] contCifrado = eci.getEncryptedContent().getOctets(); // Desciframos. return Utils.deCipherContent(contCifrado, this.config, this.cipherKey); }
From source file:es.gob.afirma.envelopers.cms.ValidateCMS.java
License:Open Source License
/** Método que verifica que es una firma de tipo "Encrypted data" * @param data//from w w w . j a v a 2s.co m * Datos CMS. * @return si es de este tipo. */ static boolean isCMSEncryptedData(final byte[] data) { boolean isValid = true; try { final ASN1InputStream is = new ASN1InputStream(data); 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.encryptedData)) { isValid = false; } else { // Contenido de Data final ASN1TaggedObject doj = (ASN1TaggedObject) e.nextElement(); final ASN1Sequence asq = (ASN1Sequence) doj.getObject(); /* * Si no es de tipo EncryptedData se pasa al manejo de la * excepcion */ ASN1Integer.getInstance(asq.getObjectAt(0)); EncryptedContentInfo.getInstance(asq.getObjectAt(1)); } } catch (final Exception ex) { isValid = false; } return isValid; }
From source file:es.gob.afirma.signers.cades.CAdESValidator.java
License:Open Source License
/** Verifica si los datos proporcionados se corresponden con una estructura de tipo <i>EncryptedData</i>. * @param data Datos PKCS#7/CMS/CAdES./* w ww .jav a 2 s . com*/ * @return <code>true</code> si los datos proporcionados se corresponden con una estructura de tipo <i>EncryptedData</i>, * <code>false</code> en caso contrario. * @throws IOException Si ocurren problemas relacionados con la lectura de los datos */ static boolean isCAdESEncryptedData(final byte[] data) throws IOException { boolean isValid = false; // LEEMOS EL FICHERO QUE NOS INTRODUCEN final ASN1InputStream is = new ASN1InputStream(data); final ASN1Sequence dsq; try { dsq = (ASN1Sequence) is.readObject(); } catch (final Exception e) { // No es una secuencia valida return false; } finally { is.close(); } final Enumeration<?> e = dsq.getObjects(); // Elementos que contienen los elementos OID Data final ASN1ObjectIdentifier doi = (ASN1ObjectIdentifier) e.nextElement(); if (doi.equals(PKCSObjectIdentifiers.encryptedData)) { isValid = true; } // Contenido de Data final ASN1TaggedObject doj = (ASN1TaggedObject) e.nextElement(); final ASN1Sequence asq = (ASN1Sequence) doj.getObject(); try { /* Los resultados de las llamadas no se usan, solo es para verificar que la * conversion ha sido correcta. De no ser asi, se pasaria al manejo * de la excepcion. */ ASN1Integer.getInstance(asq.getObjectAt(0)); EncryptedContentInfo.getInstance(asq.getObjectAt(1)); if (asq.size() == 3) { asq.getObjectAt(2); } } catch (final Exception ex) { LOGGER.info("Los datos proporcionados no son de tipo EncryptedData: " + ex); //$NON-NLS-1$ return false; } return isValid; }
From source file:es.gob.afirma.signers.pkcs7.SignedAndEnvelopedData.java
License:Open Source License
/** Crea un objecto CMS SignedAndEnvelopedData a partir de una Secuencia ASN.1. * @param seq Secuencia ASN.1 origen/*from w w w. j a v a 2s . c o m*/ */ public SignedAndEnvelopedData(final ASN1Sequence seq) { int index = 0; this.version = (ASN1Integer) seq.getObjectAt(index++); this.recipientInfos = ASN1Set.getInstance(seq.getObjectAt(index++)); // Los DigestAlgorithmIdentifiers pueden ser SET o SEQUENCE, probamos ambos final ASN1Encodable dai = seq.getObjectAt(index++); try { this.digestAlgorithms = ASN1Set.getInstance(dai); } catch (final IllegalArgumentException e) { this.digestAlgorithms = ASN1Sequence.getInstance(dai); } this.encryptedContentInfo = EncryptedContentInfo.getInstance(seq.getObjectAt(index++)); if (seq.size() > 5) { if (seq.size() == 6) { this.certificates = ASN1Set.getInstance((ASN1TaggedObject) seq.getObjectAt(index++), false); } else { this.certificates = ASN1Set.getInstance((ASN1TaggedObject) seq.getObjectAt(index++), false); this.crls = ASN1Set.getInstance((ASN1TaggedObject) seq.getObjectAt(index++), false); } } this.signerInfos = ASN1Set.getInstance(seq.getObjectAt(index++)); }