Example #1
0
        private static byte[] EncodeBagValue(string certificateType, ReadOnlyMemory <byte> encodedCertificate)
        {
            // Read to ensure that there is precisely one legally encoded value.
            if (!AsnDecoder.TryReadEncodedValue(
                    encodedCertificate.Span,
                    AsnEncodingRules.BER,
                    out _,
                    out _,
                    out _,
                    out int consumed) ||
                consumed != encodedCertificate.Length)
            {
                throw new CryptographicException(SR.Cryptography_Der_Invalid_Encoding);
            }

            // No need to copy encodedCertificate here, because it will be copied into the
            // return value.
            CertBagAsn certBagAsn = new CertBagAsn
            {
                CertId    = certificateType,
                CertValue = encodedCertificate,
            };

            AsnWriter writer = new AsnWriter(AsnEncodingRules.BER);

            certBagAsn.Encode(writer);
            return(writer.Encode());
        }
Example #2
0
        private static byte[] EncodeBagValue(string certificateType, ReadOnlyMemory <byte> encodedCertificate)
        {
            // Read to ensure that there is precisely one legally encoded value.
            AsnReader reader = new AsnReader(encodedCertificate, AsnEncodingRules.BER);

            reader.ReadEncodedValue();
            reader.ThrowIfNotEmpty();

            // No need to copy encodedCertificate here, because it will be copied into the
            // return value.
            CertBagAsn certBagAsn = new CertBagAsn
            {
                CertId    = certificateType,
                CertValue = encodedCertificate,
            };

            using (AsnWriter writer = new AsnWriter(AsnEncodingRules.BER))
            {
                certBagAsn.Encode(writer);
                return(writer.Encode());
            }
        }