예제 #1
0
        public static void BuildExpectedRequest_FromData(bool viaSpan)
        {
            Rfc3161TimestampRequest request = Rfc3161TimestampRequest.CreateFromData(
                System.Text.Encoding.ASCII.GetBytes("Hello, world!!"),
                HashAlgorithmName.SHA256,
                requestSignerCertificates: true);

            VerifyExpectedRequest(request, viaSpan);
        }
예제 #2
0
        public static void BuildWithAllOptions()
        {
            byte[] data = { 1, 9, 7, 5, 0, 4, 0, 4 };
            Oid    requestedPolicyOid = new Oid("1.2.3", "1.2.3");

            byte[] nonce = "0123456789".HexToByteArray();

            X509ExtensionCollection extensionsIn = new X509ExtensionCollection
            {
                new X509Extension("1.2.3.4.5", new byte[] { 0x05, 0x00 }, false),
                new X509Extension("0.1.2", new byte[] { 0x04, 0x00 }, false),
            };

            Rfc3161TimestampRequest req = Rfc3161TimestampRequest.CreateFromData(
                data,
                HashAlgorithmName.SHA512,
                requestedPolicyOid,
                nonce,
                true,
                extensionsIn);

            Assert.NotNull(req);
            Assert.Equal(512 / 8, req.GetMessageHash().Length);
            Assert.Equal(Oids.Sha512, req.HashAlgorithmId.Value);
            Assert.NotNull(req.RequestedPolicyId);
            Assert.NotSame(requestedPolicyOid, req.RequestedPolicyId);
            Assert.Equal(requestedPolicyOid.Value, req.RequestedPolicyId.Value);
            Assert.True(req.GetNonce().HasValue, "req.GetNonce().HasValue");
            Assert.Equal(nonce.ByteArrayToHex(), req.GetNonce().Value.ByteArrayToHex());
            Assert.True(req.RequestSignerCertificate, "req.RequestSignerCertificate");
            Assert.True(req.HasExtensions, "req.HasExtensions");

            X509ExtensionCollection extensionsOut = req.GetExtensions();

            Assert.NotSame(extensionsIn, extensionsOut);
            Assert.Equal(extensionsIn.Count, extensionsOut.Count);
            Assert.NotSame(extensionsIn[0], extensionsOut[0]);
            Assert.NotSame(extensionsIn[0], extensionsOut[1]);
            Assert.NotSame(extensionsIn[1], extensionsOut[0]);
            Assert.NotSame(extensionsIn[1], extensionsOut[1]);

            // Extensions is order-preserving
            Assert.Equal(extensionsIn[0].Oid.Value, extensionsOut[0].Oid.Value);
            Assert.Equal(extensionsIn[0].RawData, extensionsOut[0].RawData);

            Assert.Equal(extensionsIn[1].Oid.Value, extensionsOut[1].Oid.Value);
            Assert.Equal(extensionsIn[1].RawData, extensionsOut[1].RawData);
        }
예제 #3
0
        public static void BuildFromSignerInfo()
        {
            ContentInfo content = new ContentInfo(new byte[] { 1, 2, 3, 4 });
            SignedCms   cms     = new SignedCms(content, false);

            using (X509Certificate2 signerCert = Certificates.RSAKeyTransferCapi1.TryGetCertificateWithPrivateKey())
            {
                CmsSigner signer = new CmsSigner(SubjectIdentifierType.SubjectKeyIdentifier, signerCert);
                signer.SignedAttributes.Add(new Pkcs9SigningTime());
                cms.ComputeSignature(signer);
            }

            SignerInfo signerInfo = cms.SignerInfos[0];

            byte[] sig = signerInfo.GetSignature();

            Rfc3161TimestampRequest fromSigner = Rfc3161TimestampRequest.CreateFromSignerInfo(signerInfo, HashAlgorithmName.SHA256);
            Rfc3161TimestampRequest fromData   = Rfc3161TimestampRequest.CreateFromData(sig, HashAlgorithmName.SHA256);

            Assert.Equal(fromData.Encode().ByteArrayToHex(), fromSigner.Encode().ByteArrayToHex());
        }