void signContent(MessageSigner messageSigner, Byte[] content) { hashAlgId = new AlgorithmIdentifier(messageSigner.HashingAlgorithm.ToOid(), new Byte[0]); pubKeyAlgId = new AlgorithmIdentifier(messageSigner.PublicKeyAlgorithm, new Byte[0]); prepareSigning(content); SignedContentBlob signedBlob; if (_authAttributes.Any()) { // auth attributes are encoded as IMPLICIT (OPTIONAL), but RFC2315 §9.3 requires signature computation for SET var attrBytes = _authAttributes.Encode(); attrBytes[0] = 0x31; signedBlob = new SignedContentBlob(attrBytes, ContentBlobType.ToBeSignedBlob); } else { if (content == null) { throw new ArgumentException("'content' parameter cannot be null if no authenticated attributes present."); } signedBlob = new SignedContentBlob(content, ContentBlobType.ToBeSignedBlob); } signerCert = new PkcsSubjectIdentifier(messageSigner.SignerCertificate, SubjectIdentifier); signedBlob.Sign(messageSigner); hashValue = signedBlob.Signature.Value; }
/// <summary> /// Encodes and signs the content using the signer object used in /// </summary> /// <returns> /// An instance of <see cref="PkcsSignerInfo"/> class. /// </returns> /// <remarks> /// Before signing, the method adds two authenticated attributes: content type and message digest. Authenticated attributes are then /// signed with signer's private key. /// </remarks> public PkcsSignerInfo Encode() { if (_authAttributes.All(x => x.Oid.Value != MESSAGE_DIGEST)) { throw new InvalidOperationException(); } // version var builder = new Asn1Builder().AddInteger(Version); // signerIdentifier builder.AddDerData(signerCert.Encode()); // digestAlgorithm builder.AddDerData(hashAlgId.RawData); // authenticatedAttributes if (_authAttributes.Any()) { builder.AddExplicit(0, _authAttributes.Encode(), false); } // digestEncryptionAlgorithm builder.AddDerData(pubKeyAlgId.RawData); // encryptedDigest builder.AddOctetString(hashValue); // unauthenticatedAttributes if (_unauthAttributes.Any()) { builder.AddExplicit(1, UnauthenticatedAttributes.Encode(), false); } // wrap return(new PkcsSignerInfo(builder.GetEncoded())); }