Esempio n. 1
0
        public void ToOid()
        {
            foreach (HashAlgorithmName name in this.GetNames())
            {
                string oid = HashAlgorithmConvertor.ToOid(name);

                Assert.IsNotNull(oid);
                Assert.AreNotEqual(string.Empty, oid);
            }
        }
        private static void ConstructAsn1(HashAlgorithmName hashAlgorithmName, ReadOnlySpan <byte> digest, AsnWriter asnWriter)
        {
            // Digest info
            asnWriter.PushSequence();

            // AlgorithmIdetifier
            string oid = HashAlgorithmConvertor.ToOid(hashAlgorithmName);

            asnWriter.PushSequence();
            asnWriter.WriteObjectIdentifier(oid);
            asnWriter.WriteNull();
            asnWriter.PopSequence();

            asnWriter.WriteOctetString(digest);
            asnWriter.PopSequence();
        }
        private static byte[] CreateRawAsn1(string policyOid, HashAlgorithmName algorithmNameForPolicy, ReadOnlySpan <byte> policyHashValue)
        {
            if (policyOid == null)
            {
                throw new ArgumentNullException(nameof(policyOid));
            }

            using AsnWriter asnWriter = new AsnWriter(AsnEncodingRules.DER);
            asnWriter.PushSequence();
            asnWriter.WriteObjectIdentifier(policyOid);
            asnWriter.PushSequence();
            asnWriter.WriteObjectIdentifier(HashAlgorithmConvertor.ToOid(algorithmNameForPolicy));
            asnWriter.WriteOctetString(policyHashValue);
            asnWriter.PopSequence();
            asnWriter.PopSequence();
            return(asnWriter.Encode());
        }
        private static byte[] CreateRawAsn1(string policyOid, HashAlgorithmName algorithmNameForPolicy, ReadOnlySpan <byte> policyHashValue)
        {
            ThrowHelpers.CheckNullOrEempty(nameof(policyOid), policyOid);

            using AsnWriter asnWriter = new AsnWriter(AsnEncodingRules.DER);
            asnWriter.PushSequence();
            asnWriter.WriteObjectIdentifier(policyOid);
            asnWriter.PushSequence();
            asnWriter.PushSequence();
            asnWriter.WriteObjectIdentifier(HashAlgorithmConvertor.ToOid(algorithmNameForPolicy));
            asnWriter.WriteNull();
            asnWriter.PopSequence();
            asnWriter.WriteOctetString(policyHashValue);
            asnWriter.PopSequence();
            asnWriter.PopSequence();
            return(asnWriter.Encode());
        }
Esempio n. 5
0
        private static void ConstructAsn1(HashAlgorithmName hashAlgorithmName, ReadOnlySpan <byte> digest, AsnWriter asnWriter)
        {
            string oid  = HashAlgorithmConvertor.ToOid(hashAlgorithmName);
            int    size = HashAlgorithmConvertor.ToHashSizeInBytes(hashAlgorithmName);

            if (digest.Length != size)
            {
                throw new ArgumentOutOfRangeException(nameof(digest), $"digest has bad length. Expected size of {hashAlgorithmName.Name} digest is {size}B, actual length is {digest.Length}.");
            }

            // Digest info
            asnWriter.PushSequence();

            // AlgorithmIdetifier
            asnWriter.PushSequence();
            asnWriter.WriteObjectIdentifier(oid);
            asnWriter.WriteNull();
            asnWriter.PopSequence();

            asnWriter.WriteOctetString(digest);
            asnWriter.PopSequence();
        }
        private static byte[] CreateRawAsn1(IEnumerable <X509Certificate2> signingCertificates, HashAlgorithmName hashAlgorithmName)
        {
            if (signingCertificates == null)
            {
                throw new ArgumentNullException(nameof(signingCertificates));
            }

            using HashAlgorithm hasher = HashAlgorithmConvertor.ToHashAlgorithm(hashAlgorithmName);
            Span <byte> hash = stackalloc byte[hasher.HashSize / 8];


            using AsnWriter asnWriter = new AsnWriter(AsnEncodingRules.DER);
            asnWriter.PushSequence();
            asnWriter.PushSequence();

            foreach (X509Certificate2 signingCertificate in signingCertificates)
            {
                hasher.TryComputeHash(signingCertificate.RawData, hash, out _);

                // Begin essCertIDv2
                asnWriter.PushSequence();

                // Begin algorithm identifier
                asnWriter.PushSequence();
                asnWriter.WriteObjectIdentifier(HashAlgorithmConvertor.ToOid(hashAlgorithmName));
                asnWriter.WriteNull();
                asnWriter.PopSequence();
                // End Algorithm identifier

                asnWriter.WriteOctetString(hash);
                asnWriter.PopSequence();
                // End essCertIDv2
            }

            asnWriter.PopSequence();
            asnWriter.PopSequence();

            return(asnWriter.Encode());
        }