Beispiel #1
0
        private static unsafe Task <(TimestampResult, byte[])> SubmitTimestampRequest(Uri timestampUri, Oid digestOid, TimestampNonce nonce, TimeSpan timeout, byte[] digest)
        {
            var parameters = new CRYPT_TIMESTAMP_PARA
            {
                cExtension    = 0,
                fRequestCerts = true
            };

            using (var cNonce = nonce.Nonce.Pin())
            {
                parameters.Nonce.cbData   = (uint)nonce.Nonce.Length;
                parameters.Nonce.pbData   = new IntPtr(cNonce.Pointer);
                parameters.pszTSAPolicyId = null;
                var winResult = Crypt32.CryptRetrieveTimeStamp(
                    timestampUri.AbsoluteUri,
                    CryptRetrieveTimeStampRetrievalFlags.TIMESTAMP_DONT_HASH_DATA,
                    (uint)timeout.TotalMilliseconds,
                    digestOid.Value,
                    ref parameters,
                    digest,
                    (uint)digest.Length,
                    out var context,
                    IntPtr.Zero,
                    IntPtr.Zero
                    );
                if (!winResult)
                {
                    return(Task.FromResult <(TimestampResult, byte[])>((TimestampResult.Failed, null)));
                }
                using (context)
                {
                    var refSuccess = false;
                    try
                    {
                        context.DangerousAddRef(ref refSuccess);
                        if (!refSuccess)
                        {
                            return(Task.FromResult <(TimestampResult, byte[])>((TimestampResult.Failed, null)));
                        }
                        var structure = Marshal.PtrToStructure <CRYPT_TIMESTAMP_CONTEXT>(context.DangerousGetHandle());
                        var encoded   = new byte[structure.cbEncoded];
                        Marshal.Copy(structure.pbEncoded, encoded, 0, encoded.Length);
                        return(Task.FromResult <(TimestampResult, byte[])>((TimestampResult.Success, encoded)));
                    }
                    finally
                    {
                        if (refSuccess)
                        {
                            context.DangerousRelease();
                        }
                    }
                }
            }
        }
Beispiel #2
0
        private static async Task <(TimestampResult, byte[])> SubmitTimestampRequest(Uri timestampUri, Oid digestOid, TimestampNonce nonce, TimeSpan timeout, byte[] digest)
        {
            var timestampRequest = Rfc3161TimestampRequest.CreateFromHash(digest, digestOid, nonce: nonce.Nonce, requestSignerCertificates: true);
            var encodedRequest   = timestampRequest.Encode();
            var client           = new HttpClient
            {
                Timeout = timeout
            };
            var content = new ByteArrayContent(encodedRequest);

            content.Headers.Add("Content-Type", "application/timestamp-query");
            var post = await client.PostAsync(timestampUri, content);

            if (post.StatusCode != HttpStatusCode.OK)
            {
                return(TimestampResult.Failed, null);
            }
            var responseBytes = await post.Content.ReadAsByteArrayAsync();

            var token     = timestampRequest.ProcessResponse(responseBytes, out _);
            var tokenInfo = token.AsSignedCms().Encode();

            return(TimestampResult.Success, tokenInfo);
        }
        public static Task <(TimestampResult, byte[])> RequestTimestamp(Uri timestampUri, HashAlgorithmName timestampAlgorithm, TimestampNonce nonce, TimeSpan timeout, byte[] content)
        {
            var info = new HashAlgorithmInfo(timestampAlgorithm);

            byte[] digest;
            using (var hash = info.Create())
            {
                digest = hash.ComputeHash(content);
            }
            return(SubmitTimestampRequest(timestampUri, info.Oid, nonce, timeout, digest));
        }
        public static Task <(TimestampResult, byte[])> RequestTimestamp(Uri timestampUri, HashAlgorithmName timestampAlgorithm, TimestampNonce nonce, TimeSpan timeout, byte[] content)
        {
            var digestOid = HashAlgorithmTranslator.TranslateFromNameToOid(timestampAlgorithm);

            byte[] digest;
            using (var hash = HashAlgorithmTranslator.TranslateFromNameToXmlDSigUri(timestampAlgorithm, out _))
            {
                digest = hash.ComputeHash(content);
            }
            return(SubmitTimestampRequest(timestampUri, digestOid, nonce, timeout, digest));
        }