/// <summary> /// Signs <see cref="ToBeSignedData"/> data by using client-provided message signer. /// </summary> /// <param name="signerInfo">Configured message signer object which is used to sign the data.</param> public void Sign(MessageSigner signerInfo) { var signature = signerInfo.SignData(ToBeSignedData).ToList(); if (signerInfo.SignerCertificate.PublicKey.Oid.Value == AlgorithmOids.RSA) { signature.Insert(0, 0); Signature = new Asn1BitString(Asn1Utils.Encode(signature.ToArray(), 3)); } else { // ECDSA, DSA signature consist of two parts, r and s. Int32 divider = signature.Count / 2; List <Byte> r = signature.Skip(0).Take(divider).ToList(); // check if most significant bit is set to 1. If set, prepend value with extra 0 byte. if (r[0] > 127) { r.Insert(0, 0); } List <Byte> s = signature.Skip(divider).Take(divider).ToList(); // check if most significant bit is set to 1. If set, prepend value with extra 0 byte. if (s[0] > 127) { s.Insert(0, 0); } var builder = new List <Byte>(); builder.AddRange(Asn1Utils.Encode(r.ToArray(), (Byte)Asn1Type.INTEGER)); builder.AddRange(Asn1Utils.Encode(s.ToArray(), (Byte)Asn1Type.INTEGER)); builder = new List <Byte>(Asn1Utils.Encode(builder.ToArray(), 48)); builder.Insert(0, 0); Signature = new Asn1BitString(Asn1Utils.Encode(builder.ToArray(), 3)); } SignatureAlgorithm = signerInfo.GetAlgorithmIdentifier(); BlobType = ContentBlobType.SignedBlob; }
void signRequest(X509Certificate2 signerCert) { List <Byte> tbsRequest = buildTbsRequest(signerCert.SubjectName); Byte[] signature; using (var signerInfo = new MessageSigner(signerCert, new Oid2(signatureAlgID, false))) { signature = signerInfo.SignData(tbsRequest.ToArray()); } SignerCertificate = signerCert; if (includeFullSigChain) { buildSignerCertChain(); } else { _signerChain.Add(signerCert); } AlgorithmIdentifier algId = new AlgorithmIdentifier(signatureAlgID); List <Byte> signatureInfo = new List <Byte>(algId.RawData); signatureInfo.AddRange(new Asn1BitString(signature, false).RawData); signatureInfo.AddRange(Asn1Utils.Encode(_signerChain.Encode(), 0xa0)); tbsRequest.AddRange(Asn1Utils.Encode(Asn1Utils.Encode(signatureInfo.ToArray(), 48), 0xa0)); RawData = Asn1Utils.Encode(tbsRequest.ToArray(), 48); IsReadOnly = true; }