/afirma-crypto-core-pkcs7/src/main/java/es/gob/afirma/signers/pkcs7/SignedAndEnvelopedData.java

https://gitlab.com/edgardo001/clienteafirma · Java · 218 lines · 101 code · 29 blank · 88 comment · 12 complexity · 10042dddb8b76a873f60bf0be467a54e MD5 · raw file

  1. /* Copyright (C) 2011 [Gobierno de Espana]
  2. * This file is part of "Cliente @Firma".
  3. * "Cliente @Firma" is free software; you can redistribute it and/or modify it under the terms of:
  4. * - the GNU General Public License as published by the Free Software Foundation;
  5. * either version 2 of the License, or (at your option) any later version.
  6. * - or The European Software License; either version 1.1 or (at your option) any later version.
  7. * Date: 11/01/11
  8. * You may contact the copyright holder at: soporte.afirma5@mpt.es
  9. */
  10. package es.gob.afirma.signers.pkcs7;
  11. import org.spongycastle.asn1.ASN1Encodable;
  12. import org.spongycastle.asn1.ASN1EncodableVector;
  13. import org.spongycastle.asn1.ASN1Integer;
  14. import org.spongycastle.asn1.ASN1Object;
  15. import org.spongycastle.asn1.ASN1Primitive;
  16. import org.spongycastle.asn1.ASN1Sequence;
  17. import org.spongycastle.asn1.ASN1Set;
  18. import org.spongycastle.asn1.ASN1TaggedObject;
  19. import org.spongycastle.asn1.BERSequence;
  20. import org.spongycastle.asn1.DERTaggedObject;
  21. import org.spongycastle.asn1.cms.EncryptedContentInfo;
  22. /** Método base para la generación deun tipo Signed and Enveloped
  23. * Data del estandar pkcs#7 basado en la RFC-2315.
  24. * La implementación del código ha seguido los pasos necesarios
  25. * para crear un SignedAndEnvelopedData desarrollado en SpongyCastle.
  26. * <pre>
  27. * SignedAndEnvelopedData ::= SEQUENCE {
  28. * version Version,
  29. * recipientInfos RecipientInfos,
  30. * digestAlgorithms DigestAlgorithmIdentifiers,
  31. * encryptedContentInfo EncryptedContentInfo,
  32. * certificates [0] IMPLICIT Certificates OPTIONAL,
  33. * crls [1] IMPLICIT CertificateRevocationLists OPTIONAL,
  34. * signerInfos SignerInfos }
  35. *
  36. * RecipientInfos ::= SET OF RecipientInfo
  37. *
  38. * DigestAlgorithmIdentifiers ::= CHOICE {
  39. * daSet SET OF DigestAlgorithmIdentifier,
  40. * daSequence SEQUENCE OF DigestAlgorithmIdentifier }
  41. *
  42. * EncryptedContentInfo ::= SEQUENCE {
  43. * contentType ContentType,
  44. * contentEncryptionAlgorithm ContentEncryptionAlgorithmIdentifier,
  45. * encryptedContent [0] IMPLICIT EncryptedContent OPTIONAL }
  46. *
  47. * </pre> */
  48. public final class SignedAndEnvelopedData extends ASN1Object {
  49. private final ASN1Integer version;
  50. private final ASN1Set recipientInfos;
  51. /** <code>DigestAlgorithmIdentifiers</code>, que puede ser tanto un <code>SET</code> como una <code>SEQUENCE</code>
  52. * ASN.1 de estructuras <code>DigestAlgorithmIdentifier</code>. */
  53. private ASN1Primitive digestAlgorithms;
  54. private final EncryptedContentInfo encryptedContentInfo;
  55. private ASN1Set certificates;
  56. private ASN1Set crls;
  57. private final ASN1Set signerInfos;
  58. /** Crea un objecto CMS SignedAndEnvelopedData.
  59. * @param recipientInfos RecipientInfo
  60. * @param digestAlgorithms ALgoritmos de huella digital
  61. * @param encryptedContentInfo EncryptedContentInfo
  62. * @param certificates Certificados
  63. * @param crls Listas de revoaci&oacute;n de certificados
  64. * @param signerInfos SignerInfo
  65. */
  66. public SignedAndEnvelopedData(final ASN1Set recipientInfos,
  67. final ASN1Primitive digestAlgorithms,
  68. final EncryptedContentInfo encryptedContentInfo,
  69. final ASN1Set certificates,
  70. final ASN1Set crls,
  71. final ASN1Set signerInfos) {
  72. this.version = new ASN1Integer(1); // Siempre 1
  73. this.recipientInfos = recipientInfos;
  74. this.digestAlgorithms = digestAlgorithms;
  75. this.encryptedContentInfo = encryptedContentInfo;
  76. this.certificates = certificates;
  77. this.crls = crls;
  78. this.signerInfos = signerInfos;
  79. }
  80. /** Crea un objecto CMS SignedAndEnvelopedData a partir de una Secuencia ASN.1.
  81. * @param seq Secuencia ASN.1 origen
  82. */
  83. public SignedAndEnvelopedData(final ASN1Sequence seq) {
  84. int index = 0;
  85. this.version = (ASN1Integer) seq.getObjectAt(index++);
  86. this.recipientInfos = ASN1Set.getInstance(seq.getObjectAt(index++));
  87. // Los DigestAlgorithmIdentifiers pueden ser SET o SEQUENCE, probamos ambos
  88. final ASN1Encodable dai = seq.getObjectAt(index++);
  89. try {
  90. this.digestAlgorithms = ASN1Set.getInstance(dai);
  91. }
  92. catch(final IllegalArgumentException e) {
  93. this.digestAlgorithms = ASN1Sequence.getInstance(dai);
  94. }
  95. this.encryptedContentInfo = EncryptedContentInfo.getInstance(seq.getObjectAt(index++));
  96. if (seq.size() > 5) {
  97. if (seq.size() == 6) {
  98. this.certificates = ASN1Set.getInstance((ASN1TaggedObject) seq.getObjectAt(index++), false);
  99. }
  100. else {
  101. this.certificates = ASN1Set.getInstance((ASN1TaggedObject) seq.getObjectAt(index++), false);
  102. this.crls = ASN1Set.getInstance((ASN1TaggedObject) seq.getObjectAt(index++), false);
  103. }
  104. }
  105. this.signerInfos = ASN1Set.getInstance(seq.getObjectAt(index++));
  106. }
  107. /** Obtiene un SignedAndEnvelopedData a partir de un objeto dado (que puede ser una secuencia ASN.1 o un mismo SignedAndEnvelopedData)
  108. * @param obj
  109. * El objeto que queremos convertir en un SignedAndEnvelopedData
  110. * @return SignedAndEnvelopedData creado a partir del objeto proporcionado
  111. * @exception IllegalArgumentException
  112. * si el objeto de entrada no puede ser convertido */
  113. public static SignedAndEnvelopedData getInstance(final Object obj) {
  114. if (obj == null || obj instanceof SignedAndEnvelopedData) {
  115. return (SignedAndEnvelopedData) obj;
  116. }
  117. if (obj instanceof ASN1Sequence) {
  118. return new SignedAndEnvelopedData((ASN1Sequence) obj);
  119. }
  120. throw new IllegalArgumentException("EnvelopedData invalido: " + obj.getClass().getName()); //$NON-NLS-1$
  121. }
  122. /** Obtiene la versi&oacute;n de la especificaci&oacute;n usada.
  123. * @return La versi&oacute;n es siempre <code>1</code>*/
  124. public ASN1Integer getVersion() {
  125. return this.version;
  126. }
  127. /** Obtiene los RecipientInfo en forma de Set ASN.1.
  128. * @return RecipientInfos
  129. */
  130. public ASN1Set getRecipientInfos() {
  131. return this.recipientInfos;
  132. }
  133. /** Obtiene los algoritmos de huella digital en forma de Set ASN.1.
  134. * @return Algoritmos de huella digital
  135. */
  136. public ASN1Primitive getDigestAlgorithms() {
  137. return this.digestAlgorithms;
  138. }
  139. /** Obtiene el EncryptedContentInfo.
  140. * @return EncryptedContentInfo
  141. */
  142. public EncryptedContentInfo getEncryptedContentInfo() {
  143. return this.encryptedContentInfo;
  144. }
  145. /** Obtiene los Certificados en forma de Set ASN.1.
  146. * @return Certificados
  147. */
  148. public ASN1Set getCertificates() {
  149. return this.certificates;
  150. }
  151. /** Obtiene los SignerInfo en forma de Set ASN.1.
  152. * @return SignerInfos
  153. */
  154. public ASN1Set getSignerInfos() {
  155. return this.signerInfos;
  156. }
  157. /** Produce an object suitable for an ASN1OutputStream.
  158. *
  159. * <pre>
  160. * SignedAndEnvelopedData ::= SEQUENCE {
  161. * version Version,
  162. * recipientInfos RecipientInfos,
  163. * digestAlgorithms DigestAlgorithmIdentifiers,
  164. * encryptedContentInfo EncryptedContentInfo,
  165. * certificates
  166. * [0] IMPLICIT ExtendedCertificatesAndCertificates
  167. * OPTIONAL,
  168. * crls
  169. * [1] IMPLICIT CertificateRevocationLists OPTIONAL,
  170. * signerInfos SignerInfos }
  171. *
  172. * </pre> */
  173. @Override
  174. public ASN1Primitive toASN1Primitive() {
  175. final ASN1EncodableVector v = new ASN1EncodableVector();
  176. v.add(this.version);
  177. v.add(this.recipientInfos);
  178. v.add(this.digestAlgorithms);
  179. v.add(this.encryptedContentInfo);
  180. if (this.certificates != null) {
  181. v.add(new DERTaggedObject(false, 1, this.certificates));
  182. }
  183. if (this.crls != null) {
  184. v.add(new DERTaggedObject(false, 1, this.crls));
  185. }
  186. v.add(this.signerInfos);
  187. return new BERSequence(v);
  188. }
  189. }