Example #1
0
        public static X509ExtensionCollection DecodeX509Extensions(Byte[] rawData)
        {
            var extensions = new X509ExtensionCollection();

            extensions.Decode(rawData);
            return(extensions);
        }
        void decode(Asn1Reader asn)
        {
            asn.MoveNextAndExpectTags((Byte)Asn1Type.INTEGER);
            Version = (Int32) new Asn1Integer(asn).Value;
            asn.MoveNextAndExpectTags(48);
            RequestMessage = new TspMessageImprint(asn.GetTagRawData());
            while (asn.MoveNextCurrentLevel())
            {
                switch (asn.Tag)
                {
                case (Byte)Asn1Type.OBJECT_IDENTIFIER:
                    PolicyID = new Asn1ObjectIdentifier(asn).Value;
                    break;

                case (Byte)Asn1Type.INTEGER:
                    UseNonce = true;
                    nonce    = new Asn1Integer(asn).Value.ToByteArray();
                    break;

                case (Byte)Asn1Type.BOOLEAN:
                    RequestCertificates = new Asn1Boolean(asn).Value;
                    break;

                case 0xa0:
                    var extList = new X509ExtensionCollection();
                    extList.Decode(asn.GetTagRawData());
                    foreach (X509Extension extension in extList)
                    {
                        _extensions.Add(extension);
                    }
                    break;
                }
            }
        }
 void getAttributes(Asn1Reader asn)
 {
     asn.MoveNext();
     if (asn.PayloadLength == 0)
     {
         return;
     }
     do
     {
         X509Attribute attribute = X509Attribute.Decode(asn.GetTagRawData());
         if (attribute.Oid.Value == X509ExtensionOid.CertificateExtensions)
         {
             //Extensions
             var extensions = new X509ExtensionCollection();
             extensions.Decode(attribute.RawData);
             foreach (X509Extension extension in extensions)
             {
                 _extensions.Add(extension);
             }
         }
         else
         {
             _attributes.Add(attribute);
         }
     } while (asn.MoveNextCurrentLevel());
 }
Example #4
0
        void decodeExtensions(Asn1Reader asn)
        {
            var extensions = new X509ExtensionCollection();

            extensions.Decode(asn.GetTagRawData());
            foreach (X509Extension extension in extensions)
            {
                _extensions.Add(extension);
            }
        }
Example #5
0
        void decodeOptionalFields(Asn1Reader asn)
        {
            while (asn.MoveNextCurrentLevel())
            {
                switch (asn.Tag)
                {
                case (Byte)Asn1Type.BOOLEAN:
                    Ordering = new Asn1Boolean(asn).Value;
                    break;

                case (Byte)Asn1Type.INTEGER:
                    NonceReceived = true;
                    nonce         = asn.GetPayload();
                    break;

                case 48:
                    break;

                case 0xa0:
                    Byte[] nameBytes = asn.GetTagRawData();
                    nameBytes[0] = 48;
                    _tsaName.Decode(nameBytes);
                    break;

                case 0xa1:
                    Byte[] extBytes = asn.GetTagRawData();
                    extBytes[0] = 48;
                    var extList = new X509ExtensionCollection();
                    extList.Decode(asn.GetTagRawData());
                    foreach (X509Extension extension in extList)
                    {
                        _extensions.Add(extension);
                    }
                    break;
                }
            }
        }
Example #6
0
        void decodeTbsResponse(Asn1Reader tbsResponseData)
        {
            tbsResponseData.MoveNext();
            if (tbsResponseData.Tag == 160)
            {
                //Asn1Reader aversion = new Asn1Reader(tbsResponseData.RawData, tbsResponseData.PayloadStartOffset);
                Asn1Reader aversion = new Asn1Reader(tbsResponseData);
                aversion.MoveNext();
                Version = aversion.GetPayload()[0] + 1;
                tbsResponseData.MoveNextCurrentLevel();
            }
            else
            {
                Version = 1;
            }
            //responderID
            switch (tbsResponseData.Tag)
            {
            case 161:
                ResponderNameId = new X500DistinguishedName(tbsResponseData.GetPayload());
                tbsResponseData.MoveNextCurrentLevel();
                break;

            case 162:
                tbsResponseData.MoveNext();
                StringBuilder SB = new StringBuilder();
                foreach (Byte element in tbsResponseData.GetPayload())
                {
                    SB.Append(element.ToString("X2"));
                }
                ResponderKeyId = SB.ToString();
                tbsResponseData.MoveNext();
                break;

            default:
                throw new Exception("Invalid tag at responderID. Expected 161 (byName) or 162 (byKey).");
            }
            //tbsResponseData.MoveNextCurrentLevel();
            ProducedAt = Asn1Utils.DecodeGeneralizedTime(tbsResponseData.GetTagRawData());
            if (DateTime.Now < ProducedAt.AddMinutes(-10))
            {
                ResponseErrorInformation += (Int32)OCSPResponseComplianceError.ResponseNotTimeValid;
            }
            //responses
            tbsResponseData.MoveNext();
            //single response
            Asn1Reader responses = new Asn1Reader(tbsResponseData.GetTagRawData());

            responses.MoveNext();
            Int32 Offset;

            Responses = new OCSPSingleResponseCollection();
            do
            {
                Asn1Reader response = new Asn1Reader(responses);
                Offset = response.NextCurrentLevelOffset;
                Responses.Add(new OCSPSingleResponse(response));
                if (Request != null)
                {
                    foreach (OCSPSingleResponse item in Responses)
                    {
                        Boolean certidmatch = Request.RequestList.Any(x => x.CertId.Equals(item.CertId));
                        if (!certidmatch)
                        {
                            ResponseErrorInformation += (Int32)OCSPResponseComplianceError.CertIdMismatch;
                        }
                    }
                }
            } while (Offset != 0);
            if (tbsResponseData.NextCurrentLevelOffset != 0)
            {
                tbsResponseData.MoveNextCurrentLevel();
                if (tbsResponseData.Tag == 161)
                {
                    X509ExtensionCollection exts = new X509ExtensionCollection();
                    exts.Decode(tbsResponseData.GetPayload());
                    foreach (X509Extension item in exts)
                    {
                        _listExtensions.Add(CryptographyUtils.ConvertExtension(item));
                        if (_listExtensions[_listExtensions.Count - 1].Oid.Value == X509CertExtensions.X509OcspNonce)
                        {
                            NonceReceived = true;
                            NonceValue    = _listExtensions[_listExtensions.Count - 1].Format(false);
                        }
                    }
                }
                else
                {
                    throw new Exception("Unexpected tag at responseExtensions. Expected 161.");
                }
            }
        }