/// <summary><inheritDoc></inheritDoc></summary> /// <exception cref="System.IO.IOException"></exception> public virtual Document SignDocument(Document document, SignatureParameters parameters , byte[] signatureValue) { if (parameters.SignaturePackaging != SignaturePackaging.ENVELOPING && parameters .SignaturePackaging != SignaturePackaging.DETACHED) { throw new ArgumentException("Unsupported signature packaging " + parameters.SignaturePackaging); } try { //jbonilla - No aplica para C# //string jsAlgorithm = parameters.GetSignatureAlgorithm().GetJavaSignatureAlgorithm // (parameters.GetDigestAlgorithm()); //PreComputedContentSigner cs = new PreComputedContentSigner(jsAlgorithm, signatureValue // ); PreComputedSigner s = new PreComputedSigner(signatureValue); //DigestCalculatorProvider digestCalculatorProvider = new BcDigestCalculatorProvider // (); //CMSSignedDataGenerator generator = CreateCMSSignedDataGenerator(cs, digestCalculatorProvider // , parameters, GetSigningProfile(parameters), true, null); CmsSignedDataGenerator generator = CreateCMSSignedDataGenerator(s, parameters , GetSigningProfile(parameters), true, null); byte[] toBeSigned = Streams.ReadAll(document.OpenStream()); CmsProcessableByteArray content = new CmsProcessableByteArray(toBeSigned); bool includeContent = true; if (parameters.SignaturePackaging == SignaturePackaging.DETACHED) { includeContent = false; } CmsSignedData data = generator.Generate(content, includeContent); Document signedDocument = new CMSSignedDocument(data); CAdESSignatureExtension extension = GetExtensionProfile(parameters); if (extension != null) { signedDocument = extension.ExtendSignatures(new CMSSignedDocument(data), document , parameters); } return signedDocument; } catch (CmsException e) { throw new RuntimeException(e); } }
/// <summary>Add a signature to the already CMS signed data document.</summary> /// <remarks>Add a signature to the already CMS signed data document.</remarks> /// <param name="_signedDocument"></param> /// <param name="parameters"></param> /// <param name="signatureValue"></param> /// <returns></returns> /// <exception cref="System.IO.IOException">System.IO.IOException</exception> public virtual Document AddASignatureToDocument(Document _signedDocument, SignatureParameters parameters, byte[] signatureValue) { if (parameters.SignaturePackaging != SignaturePackaging.ENVELOPING) { throw new ArgumentException("Unsupported signature packaging " + parameters.SignaturePackaging); } try { CmsSignedData originalSignedData = null; using (var stream = _signedDocument.OpenStream()) { originalSignedData = new CmsSignedData(stream); } //jbonilla - No aplica para C# //string jsAlgorithm = parameters.GetSignatureAlgorithm().GetJavaSignatureAlgorithm // (parameters.GetDigestAlgorithm()); //PreComputedContentSigner cs = new PreComputedContentSigner(jsAlgorithm, signatureValue // ); PreComputedSigner s = new PreComputedSigner(signatureValue); //DigestCalculatorProvider digestCalculatorProvider = new BcDigestCalculatorProvider // (); //CMSSignedDataGenerator generator = CreateCMSSignedDataGenerator(cs, digestCalculatorProvider // , parameters, GetSigningProfile(parameters), true, originalSignedData); CmsSignedDataGenerator generator = CreateCMSSignedDataGenerator(s, parameters , GetSigningProfile(parameters), true, originalSignedData); //if (originalSignedData == null || originalSignedData.SignedContent.GetContent // () == null) if (originalSignedData == null || originalSignedData.SignedContent == null) { throw new RuntimeException("Cannot retrieve orignal content"); } //byte[] octetString = (byte[])originalSignedData.SignedContent.GetContent(); //CmsProcessableByteArray content = new CmsProcessableByteArray(octetString); CmsProcessable content = originalSignedData.SignedContent; CmsSignedData data = generator.Generate(content, true); Document signedDocument = new CMSSignedDocument(data); CAdESSignatureExtension extension = GetExtensionProfile(parameters); if (extension != null) { signedDocument = extension.ExtendSignatures(new CMSSignedDocument(data), null, parameters); } return signedDocument; } catch (CmsException e) { throw new RuntimeException(e); } }
/// <exception cref="System.IO.IOException"></exception> public virtual Stream ToBeSigned(Document document, SignatureParameters parameters ) { if (parameters.SignaturePackaging != SignaturePackaging.ENVELOPING && parameters .SignaturePackaging != SignaturePackaging.DETACHED) { throw new ArgumentException("Unsupported signature packaging " + parameters.SignaturePackaging); } //jbonilla - No aplica para C# //SignatureInterceptorProvider provider = new SignatureInterceptorProvider(); //Security.AddProvider(provider); //string jsAlgorithm = parameters.GetSignatureAlgorithm().GetJavaSignatureAlgorithm // (parameters.GetDigestAlgorithm()); //PreComputedContentSigner contentSigner = new PreComputedContentSigner(jsAlgorithm // ); PreComputedSigner signer = new PreComputedSigner(); //CmsSignedDataGenerator generator = CreateCMSSignedDataGenerator(contentSigner, digestCalculatorProvider // , parameters, GetSigningProfile(parameters), false, null); CmsSignedDataGenerator generator = CreateCMSSignedDataGenerator (signer, parameters, GetSigningProfile(parameters), false, null); byte[] toBeSigned = Streams.ReadAll(document.OpenStream()); CmsProcessableByteArray content = new CmsProcessableByteArray(toBeSigned); try { bool includeContent = true; if (parameters.SignaturePackaging == SignaturePackaging.DETACHED) { includeContent = false; } CmsSignedData signed = generator.Generate(content, includeContent); //jbonilla - El ISigner devuelve el mismo hash sin firmar para permitir //la generación de la firma por un medio externo, como un token. /*return new ByteArrayInputStream(contentSigner.GetByteOutputStream().ToByteArray());*/ return new MemoryStream(signer.CurrentSignature()); } catch (CmsException e) { throw new IOException("CmsException", e); } }