Пример #1
0
        public static AsnOid FromFriendlyName(string name)
        {
            AsnOid val = null;

            try
            {
                val = new AsnOid(Oid.FromFriendlyName(name, OidGroup.All));
            }
            catch
            {
            }

            if (val == null)
            {
                switch (name)
                {
                case "X509v3 Key Usage":
                    val = new AsnOid(new Oid("2.5.29.15"));
                    break;

                case "X509v3 Subject Key Identifier":
                    val = new AsnOid(Oid.FromFriendlyName("Subject Key Identifier", OidGroup.All));
                    break;
                }
            }

            return(val);
        }
Пример #2
0
        public static AsnOid Decode(byte[] source, ref int pos)
        {
            AsnOid instance = new AsnOid();

            //CheckContextTag(source, ref pos);
            pos++;
            //Console.WriteLine("Object ID");
            // length and value in subsequent bytes
            int length = instance.GetLength(source, ref pos);

            byte[] raw = new byte[length];
            Array.Copy(source, pos, raw, 0, length);

            // special case, first byte
            int    id1    = (raw[0] / 40);
            int    id2    = (raw[0] % 40);
            bool   cont   = false;
            long   biggun = 0;
            string oid    = id1.ToString() + "." + id2.ToString();

            for (int i = 1; i < raw.Length; i++)
            {
                if ((raw[i] & 0x80) == 0x80)
                {
                    cont     = true;
                    biggun <<= 7;
                    biggun  += raw[i] & 0x7f;
                }
                else
                {
                    if (cont)
                    {
                        biggun <<= 7;
                        biggun  += raw[i] & 0x7f;
                        oid     += "." + biggun.ToString();
                        cont     = false;
                        biggun   = 0;
                    }
                    else
                    {
                        oid += "." + raw[i].ToString();
                    }
                }
            }

            instance.value = new System.Security.Cryptography.Oid(oid);
            //Console.WriteLine(oid);
            //Console.WriteLine(value.FriendlyName);

            pos += length;

            return(instance);
        }
Пример #3
0
        public static AsnAlgorithmIdentifier Decode(byte[] source, ref int pos)
        {
            AsnAlgorithmIdentifier instance = new AsnAlgorithmIdentifier();

            pos++;

            long len = instance.GetLength(source, ref pos);

            instance.algorithmID = AsnOid.Decode(source, ref pos);
            instance.parameters  = AsnNull.Decode(source, ref pos);

            return(instance);
        }
Пример #4
0
        public static AsnAttributeTypeAndValue Decode(byte[] source, ref int pos)
        {
            AsnAttributeTypeAndValue instance = new AsnAttributeTypeAndValue();

            // skip the 0x30 (SEQUENCE)
            pos++;

            long length = instance.GetLength(source, ref pos);

            instance.elements.Add(AsnOid.Decode(source, ref pos));
            instance.elements.Add(AsnString.Decode(source, ref pos));

            return(instance);
        }
Пример #5
0
        public static AsnExtension Decode(byte[] source, ref int pos)
        {
            AsnExtension instance = new AsnExtension();

            pos++;

            long len = instance.GetLength(source, ref pos);

            instance.extnID = AsnOid.Decode(source, ref pos);

            if (source[pos] == 0x1)
            {
                instance.critical = AsnBoolean.Decode(source, ref pos);
            }

            instance.extnValue = AsnOctetstring.Decode(source, ref pos);

            return(instance);
        }
Пример #6
0
        public void ExtensionAuthorityKeyIdentifier()
        {
            if (extensions == null)
            {
                extensions = new AsnExtensions();
            }
            AsnOid oid = new AsnOid("2.5.29.35");

            subjectPKInfo.Encode();
            byte[] hash = SHA1.Create().ComputeHash(subjectPKInfo.publicKey.value);

            byte[] der = new byte[4 + hash.Length];
            der[0] = 0x30; //it's a sequence
            der[1] = (byte)(hash.Length + 2);
            der[2] = 0x80; // context tag indicating option 0 (see 4.2.1.1 of RFC 5280)
            // I feel there should be a 0x04 here to indicate octet string but OpenSSL omits this
            der[3] = (byte)hash.Length;
            Array.Copy(der, 4, hash, 0, hash.Length);

            AsnExtension extension = new AsnExtension(oid, false, der);

            extensions.extensions.Add(extension);
        }
Пример #7
0
 public AsnAttributeTypeAndValue(AsnOid oid, AsnString newValue)
 {
     elements.Add(oid);
     elements.Add(newValue);
 }
Пример #8
0
        public void ExtensionExtendedKeyUsage(bool serverAuth, bool clientAuth, bool codeSigning, bool emailProtection, bool timeStamping, bool ocspSigning)
        {
            if (extensions == null)
            {
                extensions = new AsnExtensions();
            }

            AsnOid oid = new AsnOid("2.5.29.37");

            // maintain a list of OIDs for the uses
            List <AsnOid> uses = new List <AsnOid>();

            if (serverAuth)
            {
                uses.Add(new AsnOid("1.3.6.1.5.5.7.3.1"));
            }
            if (clientAuth)
            {
                uses.Add(new AsnOid("1.3.6.1.5.5.7.3.2"));
            }
            if (codeSigning)
            {
                uses.Add(new AsnOid("1.3.6.1.5.5.7.3.3"));
            }
            if (emailProtection)
            {
                uses.Add(new AsnOid("1.3.6.1.5.5.7.3.4"));
            }
            if (timeStamping)
            {
                uses.Add(new AsnOid("1.3.6.1.5.5.7.3.8"));
            }
            if (ocspSigning)
            {
                uses.Add(new AsnOid("1.3.6.1.5.5.7.3.9"));
            }

            int length = 0;

            foreach (AsnOid use in uses)
            {
                length += use.Encode();
            }

            byte[] lengthBytes = EncodeLength(length);

            byte[] der = new byte[1 + lengthBytes.Length + length];
            der[0] = 0x30; // it's a sequence
            int pos = 1;

            Array.Copy(lengthBytes, 0, der, pos, lengthBytes.Length);
            pos += lengthBytes.Length;
            foreach (AsnOid use in uses)
            {
                Array.Copy(use.derValue, 0, der, pos, use.derValue.Length);
                pos += use.derValue.Length;
            }

            AsnExtension extension = new AsnExtension(oid, false, der);

            extensions.extensions.Add(extension);
        }
Пример #9
0
 public AsnExtension(string oid, bool isCritical, byte[] value)
 {
     extnID    = new AsnOid(oid);
     critical  = new AsnBoolean(isCritical);
     extnValue = new AsnOctetstring(value);
 }