public static CertificateChoices Decode(AsnReader reader, bool validateEmpty = false)
        {
            // The CertificateChoices type gives either
            // - a PKCS #6 extended certificate [PKCS#6],
            // - an X.509 certificate,
            // - a version 1 X.509 attribute certificate (ACv1) [X.509-97],
            // - a version 2 X.509 attribute certificate (ACv2) [X.509-00],
            // - or any other certificate format.

            /*  CertificateChoices ::= CHOICE {
             *      certificate Certificate,
             *      extendedCertificate [0] IMPLICIT ExtendedCertificate,  -- Obsolete
             *      v1AttrCert [1] IMPLICIT AttributeCertificateV1,        -- Obsolete
             *      v2AttrCert [2] IMPLICIT AttributeCertificateV2,
             *      other [3] IMPLICIT OtherCertificateFormat } */
            var tag = reader.PeekTag();

            if (tag == Asn1Tag.Sequence)
            {
                var result = new CertificateChoices()
                {
                    Certificate = Certificate.Decode(reader),
                };

                if (validateEmpty)
                {
                    reader.ThrowIfNotEmpty();
                }
                return(result);
            }

            throw new NotImplementedException("CertificateChoices");
        }
Ejemplo n.º 2
0
        public static SignedData Decode(AsnReader reader)
        {
            /* SignedData ::= SEQUENCE {
             *  version CMSVersion,
             *  digestAlgorithms DigestAlgorithmIdentifiers,
             *  encapContentInfo EncapsulatedContentInfo,
             *  certificates [0] IMPLICIT CertificateSet OPTIONAL,
             *  crls [1] IMPLICIT RevocationInfoChoices OPTIONAL,
             *  signerInfos SignerInfos }*/
            var sequence = reader.ReadSequence();

            // version CMSVersion,
            if (!sequence.TryReadInt32(out var version))
            {
                sequence.ThrowIfNotEmpty();
            }

            // digestAlgorithms DigestAlgorithmIdentifiers
            // DigestAlgorithmIdentifiers ::= SET OF DigestAlgorithmIdentifier
            var digestAlgorithms = sequence.ReadSetOf(set => AlgorithmIdentifier.Decode(set));

            // encapContentInfo EncapsulatedContentInfo
            var encapContentInfo = EncapsulatedContentInfo.Decode(sequence);

            // certificates [0] IMPLICIT CertificateSet OPTIONAL,
            var certificates = sequence.ReadOptionalSetOf(0, set => CertificateChoices.Decode(set));

            // crls [1] IMPLICIT RevocationInfoChoices OPTIONAL,
            var crls = sequence.ReadOptionalSetOf(1, set => set.ReadEncodedValue());

            // signerInfos SignerInfos
            // SignerInfos ::= SET OF SignerInfo
            var signerInfos = sequence.ReadSetOf(set => SignerInfo.Decode(set));

            sequence.ThrowIfNotEmpty();
            return(new()
            {
                Version = version,
                DigestAlgorithms = digestAlgorithms,
                EncapContentInfo = encapContentInfo,
                Certificates = certificates,
                Crls = crls,
                SignerInfos = signerInfos,
            });
        }