Exemplo n.º 1
0
        public static string ToString(ASN1 seq, bool reversed, string separator, bool quotes)
        {
            StringBuilder stringBuilder = new StringBuilder();

            if (reversed)
            {
                for (int i = seq.Count - 1; i >= 0; i--)
                {
                    ASN1 entry = seq[i];
                    X501.AppendEntry(stringBuilder, entry, quotes);
                    if (i > 0)
                    {
                        stringBuilder.Append(separator);
                    }
                }
            }
            else
            {
                for (int j = 0; j < seq.Count; j++)
                {
                    ASN1 entry2 = seq[j];
                    X501.AppendEntry(stringBuilder, entry2, quotes);
                    if (j < seq.Count - 1)
                    {
                        stringBuilder.Append(separator);
                    }
                }
            }
            return(stringBuilder.ToString());
        }
Exemplo n.º 2
0
        private static X520.AttributeTypeAndValue GetAttributeFromOid(string attributeType)
        {
            string text  = attributeType.ToUpper(CultureInfo.InvariantCulture).Trim();
            string text2 = text;

            switch (text2)
            {
            case "C":
                return(new X520.CountryName());

            case "O":
                return(new X520.OrganizationName());

            case "OU":
                return(new X520.OrganizationalUnitName());

            case "CN":
                return(new X520.CommonName());

            case "L":
                return(new X520.LocalityName());

            case "S":
            case "ST":
                return(new X520.StateOrProvinceName());

            case "E":
                return(new X520.EmailAddress());

            case "DC":
                return(new X520.DomainComponent());

            case "UID":
                return(new X520.UserId());

            case "DNQUALIFIER":
                return(new X520.DnQualifier());

            case "T":
                return(new X520.Title());

            case "SN":
                return(new X520.Surname());

            case "G":
                return(new X520.GivenName());

            case "I":
                return(new X520.Initial());
            }
            if (text.StartsWith("OID."))
            {
                return(new X520.Oid(text.Substring(4)));
            }
            if (X501.IsOid(text))
            {
                return(new X520.Oid(text));
            }
            return(null);
        }
Exemplo n.º 3
0
        private static int ReadEscaped(StringBuilder sb, string value, int pos)
        {
            char c = value[pos];

            switch (c)
            {
            case ';':
            case '<':
            case '=':
            case '>':
                break;

            default:
                if (c != '"' && c != '#' && c != '+' && c != ',' && c != '\\')
                {
                    if (pos >= value.Length - 2)
                    {
                        string text = Locale.GetText("Malformed escaped value '{0}'.");
                        throw new FormatException(string.Format(text, value.Substring(pos)));
                    }
                    sb.Append(X501.ReadHex(value, ref pos));
                    return(pos);
                }
                break;
            }
            sb.Append(value[pos]);
            return(pos);
        }
        protected override ASN1 ToBeSigned(string oid)
        {
            // TBSCertificate
            ASN1 tbsCert = new ASN1(0x30);

            if (version > 1)
            {
                // TBSCertificate / [0] Version DEFAULT v1,
                byte[] ver = { (byte)(version - 1) };
                ASN1   v   = tbsCert.Add(new ASN1(0xA0));
                v.Add(new ASN1(0x02, ver));
            }

            // TBSCertificate / CertificateSerialNumber,
            tbsCert.Add(new ASN1(0x02, sn));

            // TBSCertificate / AlgorithmIdentifier,
            tbsCert.Add(PKCS7.AlgorithmIdentifier(oid));

            // TBSCertificate / Name
            tbsCert.Add(X501.FromString(issuer));

            // TBSCertificate / Validity
            ASN1 validity = tbsCert.Add(new ASN1(0x30));

            // TBSCertificate / Validity / Time
            validity.Add(ASN1Convert.FromDateTime(notBefore));
            // TBSCertificate / Validity / Time
            validity.Add(ASN1Convert.FromDateTime(notAfter));

            // TBSCertificate / Name
            tbsCert.Add(X501.FromString(subject));

            // TBSCertificate / SubjectPublicKeyInfo
            tbsCert.Add(SubjectPublicKeyInfo());

            if (version > 1)
            {
                // TBSCertificate / [1]  IMPLICIT UniqueIdentifier OPTIONAL
                if (issuerUniqueID != null)
                {
                    tbsCert.Add(new ASN1(0xA1, UniqueIdentifier(issuerUniqueID)));
                }

                // TBSCertificate / [2]  IMPLICIT UniqueIdentifier OPTIONAL
                if (subjectUniqueID != null)
                {
                    tbsCert.Add(new ASN1(0xA1, UniqueIdentifier(subjectUniqueID)));
                }

                // TBSCertificate / [3]  Extensions OPTIONAL
                if ((version > 2) && (extensions.Count > 0))
                {
                    tbsCert.Add(new ASN1(0xA3, extensions.GetBytes()));
                }
            }

            return(tbsCert);
        }
Exemplo n.º 5
0
        private static string ReadValue(string value, ref int pos)
        {
            int           startIndex    = pos;
            StringBuilder stringBuilder = new StringBuilder();

            while (pos < value.Length)
            {
                char c = value[pos];
                switch (c)
                {
                case ';':
                case '<':
                case '=':
                case '>':
                {
                    string text = Locale.GetText("Malformed value '{0}' contains '{1}' outside quotes.");
                    throw new FormatException(string.Format(text, value.Substring(startIndex), value[pos]));
                }

                default:
                    if (c != '"')
                    {
                        if (c == '#' || c == '+')
                        {
                            throw new NotImplementedException();
                        }
                        if (c == ',')
                        {
                            pos++;
                            return(stringBuilder.ToString());
                        }
                        if (c != '\\')
                        {
                            stringBuilder.Append(value[pos]);
                        }
                        else
                        {
                            pos = X501.ReadEscaped(stringBuilder, value, ++pos);
                        }
                    }
                    else
                    {
                        pos = X501.ReadQuoted(stringBuilder, value, ++pos);
                    }
                    pos++;
                    break;
                }
            }
            return(stringBuilder.ToString());
        }
Exemplo n.º 6
0
        private static string ReadHex(string value, ref int pos)
        {
            StringBuilder stringBuilder = new StringBuilder();

            stringBuilder.Append(value[pos++]);
            stringBuilder.Append(value[pos]);
            if (pos < value.Length - 4 && value[pos + 1] == '\\' && X501.IsHex(value[pos + 2]))
            {
                pos += 2;
                stringBuilder.Append(value[pos++]);
                stringBuilder.Append(value[pos]);
            }
            byte[] bytes = CryptoConvert.FromHex(stringBuilder.ToString());
            return(Encoding.UTF8.GetString(bytes));
        }
Exemplo n.º 7
0
        public static string ToString(ASN1 seq)
        {
            StringBuilder stringBuilder = new StringBuilder();

            for (int i = 0; i < seq.Count; i++)
            {
                ASN1 entry = seq[i];
                X501.AppendEntry(stringBuilder, entry, true);
                if (i < seq.Count - 1)
                {
                    stringBuilder.Append(", ");
                }
            }
            return(stringBuilder.ToString());
        }
Exemplo n.º 8
0
        public static ASN1 FromString(string rdn)
        {
            if (rdn == null)
            {
                throw new ArgumentNullException("rdn");
            }
            int  i   = 0;
            ASN1 asn = new ASN1(48);

            while (i < rdn.Length)
            {
                X520.AttributeTypeAndValue attributeTypeAndValue = X501.ReadAttribute(rdn, ref i);
                attributeTypeAndValue.Value = X501.ReadValue(rdn, ref i);
                ASN1 asn2 = new ASN1(49);
                asn2.Add(attributeTypeAndValue.GetASN1());
                asn.Add(asn2);
            }
            return(asn);
        }
Exemplo n.º 9
0
        protected override ASN1 ToBeSigned(string oid)
        {
            ASN1 aSN = new ASN1(48);

            if (version > 1)
            {
                byte[] data = new byte[1]
                {
                    (byte)(version - 1)
                };
                ASN1 aSN2 = aSN.Add(new ASN1(160));
                aSN2.Add(new ASN1(2, data));
            }
            aSN.Add(new ASN1(2, sn));
            aSN.Add(PKCS7.AlgorithmIdentifier(oid));
            aSN.Add(X501.FromString(issuer));
            ASN1 aSN3 = aSN.Add(new ASN1(48));

            aSN3.Add(ASN1Convert.FromDateTime(notBefore));
            aSN3.Add(ASN1Convert.FromDateTime(notAfter));
            aSN.Add(X501.FromString(subject));
            aSN.Add(SubjectPublicKeyInfo());
            if (version > 1)
            {
                if (issuerUniqueID != null)
                {
                    aSN.Add(new ASN1(161, UniqueIdentifier(issuerUniqueID)));
                }
                if (subjectUniqueID != null)
                {
                    aSN.Add(new ASN1(161, UniqueIdentifier(subjectUniqueID)));
                }
                if (version > 2 && extensions.Count > 0)
                {
                    aSN.Add(new ASN1(163, extensions.GetBytes()));
                }
            }
            return(aSN);
        }
Exemplo n.º 10
0
        private static int ReadQuoted(StringBuilder sb, string value, int pos)
        {
            int startIndex = pos;

            while (pos <= value.Length)
            {
                char c = value[pos];
                if (c == '"')
                {
                    return(pos);
                }
                if (c == '\\')
                {
                    return(X501.ReadEscaped(sb, value, pos));
                }
                sb.Append(value[pos]);
                pos++;
            }
            string text = Locale.GetText("Malformed quoted value '{0}'.");

            throw new FormatException(string.Format(text, value.Substring(startIndex)));
        }
Exemplo n.º 11
0
        protected override ASN1 ToBeSigned(string oid)
        {
            ASN1 asn = new ASN1(48);

            if (this.version > 1)
            {
                byte[] data = new byte[]
                {
                    this.version - 1
                };
                ASN1 asn2 = asn.Add(new ASN1(160));
                asn2.Add(new ASN1(2, data));
            }
            asn.Add(new ASN1(2, this.sn));
            asn.Add(PKCS7.AlgorithmIdentifier(oid));
            asn.Add(X501.FromString(this.issuer));
            ASN1 asn3 = asn.Add(new ASN1(48));

            asn3.Add(ASN1Convert.FromDateTime(this.notBefore));
            asn3.Add(ASN1Convert.FromDateTime(this.notAfter));
            asn.Add(X501.FromString(this.subject));
            asn.Add(this.SubjectPublicKeyInfo());
            if (this.version > 1)
            {
                if (this.issuerUniqueID != null)
                {
                    asn.Add(new ASN1(161, this.UniqueIdentifier(this.issuerUniqueID)));
                }
                if (this.subjectUniqueID != null)
                {
                    asn.Add(new ASN1(161, this.UniqueIdentifier(this.subjectUniqueID)));
                }
                if (this.version > 2 && this.extensions.Count > 0)
                {
                    asn.Add(new ASN1(163, this.extensions.GetBytes()));
                }
            }
            return(asn);
        }
Exemplo n.º 12
0
        protected override ASN1 ToBeSigned(string oid)
        {
            ASN1 asN1_1 = new ASN1((byte)48);

            if (this.version > (byte)1)
            {
                byte[] data = new byte[1]
                {
                    (byte)((uint)this.version - 1U)
                };
                asN1_1.Add(new ASN1((byte)160)).Add(new ASN1((byte)2, data));
            }
            asN1_1.Add(new ASN1((byte)2, this.sn));
            asN1_1.Add(PKCS7.AlgorithmIdentifier(oid));
            asN1_1.Add(X501.FromString(this.issuer));
            ASN1 asN1_2 = asN1_1.Add(new ASN1((byte)48));

            asN1_2.Add(ASN1Convert.FromDateTime(this.notBefore));
            asN1_2.Add(ASN1Convert.FromDateTime(this.notAfter));
            asN1_1.Add(X501.FromString(this.subject));
            asN1_1.Add(this.SubjectPublicKeyInfo());
            if (this.version > (byte)1)
            {
                if (this.issuerUniqueID != null)
                {
                    asN1_1.Add(new ASN1((byte)161, this.UniqueIdentifier(this.issuerUniqueID)));
                }
                if (this.subjectUniqueID != null)
                {
                    asN1_1.Add(new ASN1((byte)161, this.UniqueIdentifier(this.subjectUniqueID)));
                }
                if (this.version > (byte)2 && this.extensions.Count > 0)
                {
                    asN1_1.Add(new ASN1((byte)163, this.extensions.GetBytes()));
                }
            }
            return(asN1_1);
        }
Exemplo n.º 13
0
        private static X520.AttributeTypeAndValue ReadAttribute(string value, ref int pos)
        {
            while (value[pos] == ' ' && pos < value.Length)
            {
                pos++;
            }
            int num = value.IndexOf('=', pos);

            if (num == -1)
            {
                string text = Locale.GetText("No attribute found.");
                throw new FormatException(text);
            }
            string text2 = value.Substring(pos, num - pos);

            X520.AttributeTypeAndValue attributeFromOid = X501.GetAttributeFromOid(text2);
            if (attributeFromOid == null)
            {
                string text3 = Locale.GetText("Unknown attribute '{0}'.");
                throw new FormatException(string.Format(text3, text2));
            }
            pos = num + 1;
            return(attributeFromOid);
        }
Exemplo n.º 14
0
        private void Parse(byte[] crl)
        {
            string message = "Input data cannot be coded as a valid CRL.";

            try
            {
                ASN1 asn = new ASN1(this.encoded);
                if ((asn.Tag != 0x30) || (asn.Count != 3))
                {
                    throw new CryptographicException(message);
                }
                ASN1 asn2 = asn[0];
                if ((asn2.Tag != 0x30) || (asn2.Count < 3))
                {
                    throw new CryptographicException(message);
                }
                int num = 0;
                if (asn2[num].Tag == 2)
                {
                    this.version = (byte)(asn2[num++].Value[0] + 1);
                }
                else
                {
                    this.version = 1;
                }
                this.signatureOID = ASN1Convert.ToOid(asn2[num++][0]);
                this.issuer       = X501.ToString(asn2[num++]);
                this.thisUpdate   = ASN1Convert.ToDateTime(asn2[num++]);
                ASN1 time = asn2[num++];
                if ((time.Tag == 0x17) || (time.Tag == 0x18))
                {
                    this.nextUpdate = ASN1Convert.ToDateTime(time);
                    time            = asn2[num++];
                }
                this.entries = new ArrayList();
                if ((time != null) && (time.Tag == 0x30))
                {
                    ASN1 asn4 = time;
                    for (int i = 0; i < asn4.Count; i++)
                    {
                        this.entries.Add(new X509CrlEntry(asn4[i]));
                    }
                }
                else
                {
                    num--;
                }
                ASN1 asn5 = asn2[num];
                if (((asn5 != null) && (asn5.Tag == 160)) && (asn5.Count == 1))
                {
                    this.extensions = new X509ExtensionCollection(asn5[0]);
                }
                else
                {
                    this.extensions = new X509ExtensionCollection(null);
                }
                string str2 = ASN1Convert.ToOid(asn[1][0]);
                if (this.signatureOID != str2)
                {
                    throw new CryptographicException(message + " [Non-matching signature algorithms in CRL]");
                }
                byte[] src = asn[2].Value;
                this.signature = new byte[src.Length - 1];
                Buffer.BlockCopy(src, 1, this.signature, 0, this.signature.Length);
            }
            catch
            {
                throw new CryptographicException(message);
            }
        }
Exemplo n.º 15
0
        private void Parse(byte[] crl)
        {
            string text = "Input data cannot be coded as a valid CRL.";

            try
            {
                ASN1 asn = new ASN1(this.encoded);
                if (asn.Tag != 48 || asn.Count != 3)
                {
                    throw new CryptographicException(text);
                }
                ASN1 asn2 = asn[0];
                if (asn2.Tag != 48 || asn2.Count < 3)
                {
                    throw new CryptographicException(text);
                }
                int num = 0;
                if (asn2[num].Tag == 2)
                {
                    this.version = asn2[num++].Value[0] + 1;
                }
                else
                {
                    this.version = 1;
                }
                this.signatureOID = ASN1Convert.ToOid(asn2[num++][0]);
                this.issuer       = X501.ToString(asn2[num++]);
                this.thisUpdate   = ASN1Convert.ToDateTime(asn2[num++]);
                ASN1 asn3 = asn2[num++];
                if (asn3.Tag == 23 || asn3.Tag == 24)
                {
                    this.nextUpdate = ASN1Convert.ToDateTime(asn3);
                    asn3            = asn2[num++];
                }
                this.entries = new ArrayList();
                if (asn3 != null && asn3.Tag == 48)
                {
                    ASN1 asn4 = asn3;
                    for (int i = 0; i < asn4.Count; i++)
                    {
                        this.entries.Add(new X509Crl.X509CrlEntry(asn4[i]));
                    }
                }
                else
                {
                    num--;
                }
                ASN1 asn5 = asn2[num];
                if (asn5 != null && asn5.Tag == 160 && asn5.Count == 1)
                {
                    this.extensions = new X509ExtensionCollection(asn5[0]);
                }
                else
                {
                    this.extensions = new X509ExtensionCollection(null);
                }
                string b = ASN1Convert.ToOid(asn[1][0]);
                if (this.signatureOID != b)
                {
                    throw new CryptographicException(text + " [Non-matching signature algorithms in CRL]");
                }
                byte[] value = asn[2].Value;
                this.signature = new byte[value.Length - 1];
                Buffer.BlockCopy(value, 1, this.signature, 0, this.signature.Length);
            }
            catch
            {
                throw new CryptographicException(text);
            }
        }
Exemplo n.º 16
0
 private void Parse(byte[] data)
 {
     try
     {
         this.decoder = new ASN1(data);
         if (this.decoder.Tag != 0x30)
         {
             throw new CryptographicException(encoding_error);
         }
         if (this.decoder[0].Tag != 0x30)
         {
             throw new CryptographicException(encoding_error);
         }
         ASN1 asn   = this.decoder[0];
         int  index = 0;
         ASN1 asn2  = this.decoder[0][index];
         this.version = 1;
         if ((asn2.Tag == 160) && (asn2.Count > 0))
         {
             this.version += asn2[0].Value[0];
             index++;
         }
         ASN1 asn3 = this.decoder[0][index++];
         if (asn3.Tag != 2)
         {
             throw new CryptographicException(encoding_error);
         }
         this.serialnumber = asn3.Value;
         Array.Reverse(this.serialnumber, 0, this.serialnumber.Length);
         index++;
         this.issuer       = asn.Element(index++, 0x30);
         this.m_issuername = X501.ToString(this.issuer);
         ASN1 asn4 = asn.Element(index++, 0x30);
         ASN1 time = asn4[0];
         this.m_from = ASN1Convert.ToDateTime(time);
         ASN1 asn6 = asn4[1];
         this.m_until   = ASN1Convert.ToDateTime(asn6);
         this.subject   = asn.Element(index++, 0x30);
         this.m_subject = X501.ToString(this.subject);
         ASN1 asn7 = asn.Element(index++, 0x30);
         ASN1 asn8 = asn7.Element(0, 0x30);
         ASN1 asn9 = asn8.Element(0, 6);
         this.m_keyalgo = ASN1Convert.ToOid(asn9);
         ASN1 asn10 = asn8[1];
         this.m_keyalgoparams = (asn8.Count <= 1) ? null : asn10.GetBytes();
         ASN1 asn11 = asn7.Element(1, 3);
         int  count = asn11.Length - 1;
         this.m_publickey = new byte[count];
         Buffer.BlockCopy(asn11.Value, 1, this.m_publickey, 0, count);
         byte[] src = this.decoder[2].Value;
         this.signature = new byte[src.Length - 1];
         Buffer.BlockCopy(src, 1, this.signature, 0, this.signature.Length);
         asn8 = this.decoder[1];
         asn9 = asn8.Element(0, 6);
         this.m_signaturealgo = ASN1Convert.ToOid(asn9);
         asn10 = asn8[1];
         if (asn10 != null)
         {
             this.m_signaturealgoparams = asn10.GetBytes();
         }
         else
         {
             this.m_signaturealgoparams = null;
         }
         ASN1 asn12 = asn.Element(index, 0x81);
         if (asn12 != null)
         {
             index++;
             this.issuerUniqueID = asn12.Value;
         }
         ASN1 asn13 = asn.Element(index, 130);
         if (asn13 != null)
         {
             index++;
             this.subjectUniqueID = asn13.Value;
         }
         ASN1 asn14 = asn.Element(index, 0xa3);
         if ((asn14 != null) && (asn14.Count == 1))
         {
             this.extensions = new X509ExtensionCollection(asn14[0]);
         }
         else
         {
             this.extensions = new X509ExtensionCollection(null);
         }
         this.m_encodedcert = (byte[])data.Clone();
     }
     catch (Exception exception)
     {
         throw new CryptographicException(encoding_error, exception);
     }
 }
Exemplo n.º 17
0
 private void Parse(byte[] data)
 {
     try
     {
         this.decoder = new ASN1(data);
         if (this.decoder.Tag != 48)
         {
             throw new CryptographicException(X509Certificate.encoding_error);
         }
         if (this.decoder[0].Tag != 48)
         {
             throw new CryptographicException(X509Certificate.encoding_error);
         }
         ASN1 asn  = this.decoder[0];
         int  num  = 0;
         ASN1 asn2 = this.decoder[0][num];
         this.version = 1;
         if (asn2.Tag == 160 && asn2.Count > 0)
         {
             this.version += (int)asn2[0].Value[0];
             num++;
         }
         ASN1 asn3 = this.decoder[0][num++];
         if (asn3.Tag != 2)
         {
             throw new CryptographicException(X509Certificate.encoding_error);
         }
         this.serialnumber = asn3.Value;
         Array.Reverse(this.serialnumber, 0, this.serialnumber.Length);
         num++;
         this.issuer       = asn.Element(num++, 48);
         this.m_issuername = X501.ToString(this.issuer);
         ASN1 asn4 = asn.Element(num++, 48);
         ASN1 time = asn4[0];
         this.m_from = ASN1Convert.ToDateTime(time);
         ASN1 time2 = asn4[1];
         this.m_until   = ASN1Convert.ToDateTime(time2);
         this.subject   = asn.Element(num++, 48);
         this.m_subject = X501.ToString(this.subject);
         ASN1 asn5 = asn.Element(num++, 48);
         ASN1 asn6 = asn5.Element(0, 48);
         ASN1 asn7 = asn6.Element(0, 6);
         this.m_keyalgo = ASN1Convert.ToOid(asn7);
         ASN1 asn8 = asn6[1];
         this.m_keyalgoparams = ((asn6.Count <= 1) ? null : asn8.GetBytes());
         ASN1 asn9 = asn5.Element(1, 3);
         int  num2 = asn9.Length - 1;
         this.m_publickey = new byte[num2];
         Buffer.BlockCopy(asn9.Value, 1, this.m_publickey, 0, num2);
         byte[] value = this.decoder[2].Value;
         this.signature = new byte[value.Length - 1];
         Buffer.BlockCopy(value, 1, this.signature, 0, this.signature.Length);
         asn6 = this.decoder[1];
         asn7 = asn6.Element(0, 6);
         this.m_signaturealgo = ASN1Convert.ToOid(asn7);
         asn8 = asn6[1];
         if (asn8 != null)
         {
             this.m_signaturealgoparams = asn8.GetBytes();
         }
         else
         {
             this.m_signaturealgoparams = null;
         }
         ASN1 asn10 = asn.Element(num, 129);
         if (asn10 != null)
         {
             num++;
             this.issuerUniqueID = asn10.Value;
         }
         ASN1 asn11 = asn.Element(num, 130);
         if (asn11 != null)
         {
             num++;
             this.subjectUniqueID = asn11.Value;
         }
         ASN1 asn12 = asn.Element(num, 163);
         if (asn12 != null && asn12.Count == 1)
         {
             this.extensions = new X509ExtensionCollection(asn12[0]);
         }
         else
         {
             this.extensions = new X509ExtensionCollection(null);
         }
         this.m_encodedcert = (byte[])data.Clone();
     }
     catch (Exception inner)
     {
         throw new CryptographicException(X509Certificate.encoding_error, inner);
     }
 }
Exemplo n.º 18
0
        private void Parse(byte[] crl)
        {
            string e = "Input data cannot be coded as a valid CRL.";

            try {
                // CertificateList  ::=  SEQUENCE  {
                ASN1 encodedCRL = new ASN1(encoded);
                if ((encodedCRL.Tag != 0x30) || (encodedCRL.Count != 3))
                {
                    throw new CryptographicException(e);
                }

                // CertificateList / TBSCertList,
                ASN1 toBeSigned = encodedCRL [0];
                if ((toBeSigned.Tag != 0x30) || (toBeSigned.Count < 3))
                {
                    throw new CryptographicException(e);
                }

                int n = 0;
                // CertificateList / TBSCertList / Version OPTIONAL, -- if present, MUST be v2
                if (toBeSigned [n].Tag == 0x02)
                {
                    version = (byte)(toBeSigned [n++].Value [0] + 1);
                }
                else
                {
                    version = 1;                     // DEFAULT
                }
                // CertificateList / TBSCertList / AlgorithmIdentifier,
                signatureOID = ASN1Convert.ToOid(toBeSigned [n++][0]);
                // CertificateList / TBSCertList / Name,
                issuer = X501.ToString(toBeSigned [n++]);
                // CertificateList / TBSCertList / Time,
                thisUpdate = ASN1Convert.ToDateTime(toBeSigned [n++]);
                // CertificateList / TBSCertList / Time OPTIONAL,
                ASN1 next = toBeSigned [n++];
                if ((next.Tag == 0x17) || (next.Tag == 0x18))
                {
                    nextUpdate = ASN1Convert.ToDateTime(next);
                    next       = toBeSigned [n++];
                }
                // CertificateList / TBSCertList / revokedCertificates	SEQUENCE OF SEQUENCE  {
                entries = new ArrayList();
                // this is OPTIONAL so it may not be present if no entries exists
                if ((next != null) && (next.Tag == 0x30))
                {
                    ASN1 revokedCertificates = next;
                    for (int i = 0; i < revokedCertificates.Count; i++)
                    {
                        entries.Add(new X509CrlEntry(revokedCertificates [i]));
                    }
                }
                else
                {
                    n--;
                }
                // CertificateList / TBSCertList / crlExtensions [0] Extensions OPTIONAL }
                ASN1 extns = toBeSigned [n];
                if ((extns != null) && (extns.Tag == 0xA0) && (extns.Count == 1))
                {
                    extensions = new X509ExtensionCollection(extns [0]);
                }
                else
                {
                    extensions = new X509ExtensionCollection(null);                      // result in a read only object
                }
                // CertificateList / AlgorithmIdentifier
                string signatureAlgorithm = ASN1Convert.ToOid(encodedCRL [1][0]);
                if (signatureOID != signatureAlgorithm)
                {
                    throw new CryptographicException(e + " [Non-matching signature algorithms in CRL]");
                }

                // CertificateList / BIT STRING
                byte[] bitstring = encodedCRL [2].Value;
                // first byte contains unused bits in first byte
                signature = new byte [bitstring.Length - 1];
                Buffer.BlockCopy(bitstring, 1, signature, 0, signature.Length);
            }
            catch {
                throw new CryptographicException(e);
            }
        }
Exemplo n.º 19
0
        // that's were the real job is!
        private void Parse(byte[] data)
        {
            try {
                decoder = new ASN1(data);
                // Certificate
                if (decoder.Tag != 0x30)
                {
                    throw new CryptographicException(encoding_error);
                }
                // Certificate / TBSCertificate
                if (decoder [0].Tag != 0x30)
                {
                    throw new CryptographicException(encoding_error);
                }

                ASN1 tbsCertificate = decoder [0];

                int tbs = 0;
                // Certificate / TBSCertificate / Version
                ASN1 v = decoder [0][tbs];
                version = 1;                                    // DEFAULT v1
                if ((v.Tag == 0xA0) && (v.Count > 0))
                {
                    // version (optional) is present only in v2+ certs
                    version += v [0].Value [0];                         // zero based
                    tbs++;
                }

                // Certificate / TBSCertificate / CertificateSerialNumber
                ASN1 sn = decoder [0][tbs++];
                if (sn.Tag != 0x02)
                {
                    throw new CryptographicException(encoding_error);
                }
                serialnumber = sn.Value;
                Array.Reverse(serialnumber, 0, serialnumber.Length);

                // Certificate / TBSCertificate / AlgorithmIdentifier
                tbs++;
                // ASN1 signatureAlgo = tbsCertificate.Element (tbs++, 0x30);

                issuer       = tbsCertificate.Element(tbs++, 0x30);
                m_issuername = X501.ToString(issuer);

                ASN1 validity  = tbsCertificate.Element(tbs++, 0x30);
                ASN1 notBefore = validity [0];
                m_from = ASN1Convert.ToDateTime(notBefore);
                ASN1 notAfter = validity [1];
                m_until = ASN1Convert.ToDateTime(notAfter);

                subject   = tbsCertificate.Element(tbs++, 0x30);
                m_subject = X501.ToString(subject);

                ASN1 subjectPublicKeyInfo = tbsCertificate.Element(tbs++, 0x30);

                ASN1 algorithm = subjectPublicKeyInfo.Element(0, 0x30);
                ASN1 algo      = algorithm.Element(0, 0x06);
                m_keyalgo = ASN1Convert.ToOid(algo);
                // parameters ANY DEFINED BY algorithm OPTIONAL
                // so we dont ask for a specific (Element) type and return DER
                ASN1 parameters = algorithm [1];
                m_keyalgoparams = ((algorithm.Count > 1) ? parameters.GetBytes() : null);

                ASN1 subjectPublicKey = subjectPublicKeyInfo.Element(1, 0x03);
                // we must drop th first byte (which is the number of unused bits
                // in the BITSTRING)
                int n = subjectPublicKey.Length - 1;
                m_publickey = new byte [n];
                Buffer.BlockCopy(subjectPublicKey.Value, 1, m_publickey, 0, n);

                // signature processing
                byte[] bitstring = decoder [2].Value;
                // first byte contains unused bits in first byte
                signature = new byte [bitstring.Length - 1];
                Buffer.BlockCopy(bitstring, 1, signature, 0, signature.Length);

                algorithm       = decoder [1];
                algo            = algorithm.Element(0, 0x06);
                m_signaturealgo = ASN1Convert.ToOid(algo);
                parameters      = algorithm [1];
                if (parameters != null)
                {
                    m_signaturealgoparams = parameters.GetBytes();
                }
                else
                {
                    m_signaturealgoparams = null;
                }

                // Certificate / TBSCertificate / issuerUniqueID
                ASN1 issuerUID = tbsCertificate.Element(tbs, 0x81);
                if (issuerUID != null)
                {
                    tbs++;
                    issuerUniqueID = issuerUID.Value;
                }

                // Certificate / TBSCertificate / subjectUniqueID
                ASN1 subjectUID = tbsCertificate.Element(tbs, 0x82);
                if (subjectUID != null)
                {
                    tbs++;
                    subjectUniqueID = subjectUID.Value;
                }

                // Certificate / TBSCertificate / Extensions
                ASN1 extns = tbsCertificate.Element(tbs, 0xA3);
                if ((extns != null) && (extns.Count == 1))
                {
                    extensions = new X509ExtensionCollection(extns [0]);
                }
                else
                {
                    extensions = new X509ExtensionCollection(null);
                }

                // keep a copy of the original data
                m_encodedcert = (byte[])data.Clone();
            }
            catch (Exception ex) {
                throw new CryptographicException(encoding_error, ex);
            }
        }
Exemplo n.º 20
0
        private void Parse(byte[] crl)
        {
            string text = "Input data cannot be coded as a valid CRL.";

            try
            {
                ASN1 aSN = new ASN1(encoded);
                if (aSN.Tag != 48 || aSN.Count != 3)
                {
                    throw new CryptographicException(text);
                }
                ASN1 aSN2 = aSN[0];
                if (aSN2.Tag != 48 || aSN2.Count < 3)
                {
                    throw new CryptographicException(text);
                }
                int num = 0;
                if (aSN2[num].Tag == 2)
                {
                    version = (byte)(aSN2[num++].Value[0] + 1);
                }
                else
                {
                    version = 1;
                }
                signatureOID = ASN1Convert.ToOid(aSN2[num++][0]);
                issuer       = X501.ToString(aSN2[num++]);
                thisUpdate   = ASN1Convert.ToDateTime(aSN2[num++]);
                ASN1 aSN3 = aSN2[num++];
                if (aSN3.Tag == 23 || aSN3.Tag == 24)
                {
                    nextUpdate = ASN1Convert.ToDateTime(aSN3);
                    aSN3       = aSN2[num++];
                }
                entries = new ArrayList();
                if (aSN3 != null && aSN3.Tag == 48)
                {
                    ASN1 aSN4 = aSN3;
                    for (int i = 0; i < aSN4.Count; i++)
                    {
                        entries.Add(new X509CrlEntry(aSN4[i]));
                    }
                }
                else
                {
                    num--;
                }
                ASN1 aSN5 = aSN2[num];
                if (aSN5 != null && aSN5.Tag == 160 && aSN5.Count == 1)
                {
                    extensions = new X509ExtensionCollection(aSN5[0]);
                }
                else
                {
                    extensions = new X509ExtensionCollection(null);
                }
                string b = ASN1Convert.ToOid(aSN[1][0]);
                if (signatureOID != b)
                {
                    throw new CryptographicException(text + " [Non-matching signature algorithms in CRL]");
                }
                byte[] value = aSN[2].Value;
                signature = new byte[value.Length - 1];
                Buffer.BlockCopy(value, 1, signature, 0, signature.Length);
            }
            catch
            {
                throw new CryptographicException(text);
                IL_024f :;
            }
        }
Exemplo n.º 21
0
 private void Parse(byte[] data)
 {
     try
     {
         decoder = new ASN1(data);
         if (decoder.Tag != 48)
         {
             throw new CryptographicException(encoding_error);
         }
         if (decoder[0].Tag != 48)
         {
             throw new CryptographicException(encoding_error);
         }
         ASN1 aSN  = decoder[0];
         int  num  = 0;
         ASN1 aSN2 = decoder[0][num];
         version = 1;
         if (aSN2.Tag == 160 && aSN2.Count > 0)
         {
             version += aSN2[0].Value[0];
             num++;
         }
         ASN1 aSN3 = decoder[0][num++];
         if (aSN3.Tag != 2)
         {
             throw new CryptographicException(encoding_error);
         }
         serialnumber = aSN3.Value;
         Array.Reverse(serialnumber, 0, serialnumber.Length);
         num++;
         issuer       = aSN.Element(num++, 48);
         m_issuername = X501.ToString(issuer);
         ASN1 aSN4 = aSN.Element(num++, 48);
         ASN1 time = aSN4[0];
         m_from = ASN1Convert.ToDateTime(time);
         ASN1 time2 = aSN4[1];
         m_until   = ASN1Convert.ToDateTime(time2);
         subject   = aSN.Element(num++, 48);
         m_subject = X501.ToString(subject);
         ASN1 aSN5 = aSN.Element(num++, 48);
         ASN1 aSN6 = aSN5.Element(0, 48);
         ASN1 asn  = aSN6.Element(0, 6);
         m_keyalgo = ASN1Convert.ToOid(asn);
         ASN1 aSN7 = aSN6[1];
         m_keyalgoparams = ((aSN6.Count <= 1) ? null : aSN7.GetBytes());
         ASN1 aSN8 = aSN5.Element(1, 3);
         int  num7 = aSN8.Length - 1;
         m_publickey = new byte[num7];
         Buffer.BlockCopy(aSN8.Value, 1, m_publickey, 0, num7);
         byte[] value = decoder[2].Value;
         signature = new byte[value.Length - 1];
         Buffer.BlockCopy(value, 1, signature, 0, signature.Length);
         aSN6            = decoder[1];
         asn             = aSN6.Element(0, 6);
         m_signaturealgo = ASN1Convert.ToOid(asn);
         aSN7            = aSN6[1];
         if (aSN7 != null)
         {
             m_signaturealgoparams = aSN7.GetBytes();
         }
         else
         {
             m_signaturealgoparams = null;
         }
         ASN1 aSN9 = aSN.Element(num, 129);
         if (aSN9 != null)
         {
             num++;
             issuerUniqueID = aSN9.Value;
         }
         ASN1 aSN10 = aSN.Element(num, 130);
         if (aSN10 != null)
         {
             num++;
             subjectUniqueID = aSN10.Value;
         }
         ASN1 aSN11 = aSN.Element(num, 163);
         if (aSN11 != null && aSN11.Count == 1)
         {
             extensions = new X509ExtensionCollection(aSN11[0]);
         }
         else
         {
             extensions = new X509ExtensionCollection(null);
         }
         m_encodedcert = (byte[])data.Clone();
     }
     catch (Exception inner)
     {
         throw new CryptographicException(encoding_error, inner);
         IL_035f :;
     }
 }