Example #1
0
        /// <summary>
        /// Decodes a ASN.1-encoded byte array that contains revoked certificate information to a collection.
        /// </summary>
        /// <param name="asn">ASN.1 that points to the beginning of the CRL entry collection structure.</param>
        /// <exception cref="Asn1InvalidTagException">The encoded data is not valid.</exception>
        /// <exception cref="ArgumentNullException">The <strong>rawData</strong> parameter is null reference.</exception>
        /// <remarks>This method removes any existing entries in the collection before decoding.</remarks>
        public void Decode(Asn1Reader asn)
        {
            if (asn == null)
            {
                throw new ArgumentNullException(nameof(asn));
            }
            if (asn.Tag != 48)
            {
                throw new Asn1InvalidTagException(asn.Offset);
            }
            Int32 offset = asn.Offset;

            InternalList.Clear();
            InternalList.Capacity = asn.GetNestedNodeCount();
            if (!asn.MoveNext())
            {
                throw new Asn1InvalidTagException(asn.Offset);
            }

            do
            {
                InternalList.Add(new X509CRLEntry(asn));
            } while (asn.MoveNextSibling());

            asn.Seek(offset);
        }
Example #2
0
        void decode(Asn1Reader asn)
        {
            if (asn.Tag != 48)
            {
                throw new Asn1InvalidTagException(asn.Offset);
            }
            RawData = asn.GetTagRawData();
            Int32 offset = asn.Offset;

            asn.MoveNext();
            SerialNumber = Asn1Utils.DecodeInteger(asn.GetTagRawData(), true);
            asn.MoveNextAndExpectTags(Asn1Type.UTCTime, Asn1Type.GeneralizedTime);
            switch (asn.Tag)
            {
            case (Byte)Asn1Type.UTCTime:
                RevocationDate = new Asn1UtcTime(asn.GetTagRawData()).Value;
                break;

            case (Byte)Asn1Type.GeneralizedTime:
                RevocationDate = Asn1Utils.DecodeGeneralizedTime(asn.GetTagRawData());
                break;
            }
            if (asn.MoveNextSibling())
            {
                // use high-performant extension decoder instead of generic one.
                // Since CRLs may store a hundreds of thousands entries, this is
                // pretty reasonable to save loops whenever possible.
                readCrlReasonCode(asn);
            }
            asn.Seek(offset);
        }
Example #3
0
 void readCrlReasonCode(Asn1Reader asn)
 {
     if (asn.Tag != 48)
     {
         return;
     }
     asn.MoveNext();
     do
     {
         Int32 offset = asn.Offset;
         asn.MoveNextAndExpectTags(Asn1Type.OBJECT_IDENTIFIER);
         var oid = new Asn1ObjectIdentifier(asn).Value;
         if (oid.Value == X509ExtensionOid.CRLReasonCode)
         {
             asn.MoveNext();
             if (asn.Tag == (Byte)Asn1Type.BOOLEAN)
             {
                 asn.MoveNext();
             }
             if (asn.Tag == (Byte)Asn1Type.OCTET_STRING)
             {
                 asn.MoveNext();
                 if (asn.PayloadLength > 0)
                 {
                     ReasonCode = asn[asn.PayloadStartOffset];
                     break;
                 }
             }
         }
         asn.Seek(offset);
     } while (asn.MoveNextSibling());
 }
        /// <summary>
        /// Decodes ASN.1-encoded byte array to an instance of <see cref="X509Extension"/> class.
        /// </summary>
        /// <param name="asn">ASN.1 reader that points to the beginning of the X.509 extension structure.</param>
        /// <exception cref="ArgumentNullException"><strong>asn</strong> parameter is null.</exception>
        /// <exception cref="Asn1InvalidTagException">Decoder encountered an unexpected ASN.1 type identifier.</exception>
        /// <returns>Decoded extension object.</returns>
        public static X509Extension Decode(Asn1Reader asn)
        {
            if (asn.Tag != 48)
            {
                throw new Asn1InvalidTagException(asn.Offset);
            }
            Int32 offset = asn.Offset;

            asn.MoveNextAndExpectTags((Byte)Asn1Type.OBJECT_IDENTIFIER);
            Oid     oid      = new Asn1ObjectIdentifier(asn).Value;
            Boolean critical = false;

            asn.MoveNextAndExpectTags((Byte)Asn1Type.BOOLEAN, (Byte)Asn1Type.OCTET_STRING);
            if (asn.Tag == (Byte)Asn1Type.BOOLEAN)
            {
                critical = Asn1Utils.DecodeBoolean(asn.GetTagRawData());
                asn.MoveNextAndExpectTags((Byte)Asn1Type.OCTET_STRING);
            }
            // at this point ASN points to OCTET_STRING

            X509Extension retValue = new X509Extension(oid, asn.GetPayload(), critical).ConvertExtension();

            asn.Seek(offset);
            return(retValue);
        }
        /// <summary>
        /// Decodes ASN.1-encoded byte array that represents a collection of <see cref="X509Extension"/> objects.
        /// </summary>
        /// <param name="extensions">Destination collection where decoded extensions will be added.</param>
        /// <param name="asn">ASN.1 reader which points to the beginning of the extenstion collection structure.</param>
        /// <exception cref="Asn1InvalidTagException">Decoder encountered an unexpected ASN.1 type identifier.</exception>
        /// <exception cref="ArgumentNullException">
        /// <strong>extensions</strong> and/or <strong>asn</strong> parameter is null.
        /// </exception>
        /// <remarks> If current collection contains items, decoded items will be appended to existing items.</remarks>
        public static void Decode(this X509ExtensionCollection extensions, Asn1Reader asn)
        {
            if (extensions == null)
            {
                throw new ArgumentNullException(nameof(extensions));
            }
            if (asn == null)
            {
                throw new ArgumentNullException(nameof(asn));
            }
            Int32 offset = asn.Offset;

            if (!asn.MoveNext() || asn.PayloadLength == 0)
            {
                return;
            }

            do
            {
                extensions.Add(X509ExtensionExtensions.Decode(asn));
            } while (asn.MoveNextSibling());
            asn.Seek(offset);
        }