Exemplo n.º 1
0
        private ASN1 SubjectPublicKeyInfo()
        {
            ASN1 aSN = new ASN1(48);

            if (aa is RSA)
            {
                aSN.Add(PKCS7.AlgorithmIdentifier("1.2.840.113549.1.1.1"));
                RSAParameters rSAParameters = (aa as RSA).ExportParameters(includePrivateParameters: false);
                ASN1          aSN2          = new ASN1(48);
                aSN2.Add(ASN1Convert.FromUnsignedBigInteger(rSAParameters.Modulus));
                aSN2.Add(ASN1Convert.FromUnsignedBigInteger(rSAParameters.Exponent));
                aSN.Add(new ASN1(UniqueIdentifier(aSN2.GetBytes())));
            }
            else
            {
                if (!(aa is DSA))
                {
                    throw new NotSupportedException("Unknown Asymmetric Algorithm " + aa.ToString());
                }
                DSAParameters dSAParameters = (aa as DSA).ExportParameters(includePrivateParameters: false);
                ASN1          aSN3          = new ASN1(48);
                aSN3.Add(ASN1Convert.FromUnsignedBigInteger(dSAParameters.P));
                aSN3.Add(ASN1Convert.FromUnsignedBigInteger(dSAParameters.Q));
                aSN3.Add(ASN1Convert.FromUnsignedBigInteger(dSAParameters.G));
                aSN.Add(PKCS7.AlgorithmIdentifier("1.2.840.10040.4.1", aSN3));
                ASN1 aSN4 = aSN.Add(new ASN1(3));
                aSN4.Add(ASN1Convert.FromUnsignedBigInteger(dSAParameters.Y));
            }
            return(aSN);
        }
Exemplo n.º 2
0
            internal ASN1 GetASN1(byte encoding)
            {
                byte encode = encoding;

                if (encode == 0xFF)
                {
                    encode = SelectBestEncoding();
                }

                ASN1 asn1 = new ASN1(0x30);

                asn1.Add(ASN1Convert.FromOid(oid));
                switch (encode)
                {
                case 0x13:
                    // PRINTABLESTRING
                    asn1.Add(new ASN1(0x13, Encoding.ASCII.GetBytes(attrValue)));
                    break;

                case 0x16:
                    // IA5STRING
                    asn1.Add(new ASN1(0x16, Encoding.ASCII.GetBytes(attrValue)));
                    break;

                case 0x1E:
                    // BMPSTRING
                    asn1.Add(new ASN1(0x1E, Encoding.BigEndianUnicode.GetBytes(attrValue)));
                    break;
                }
                return(asn1);
            }
Exemplo n.º 3
0
            public byte[] GetBytes()
            {
                ASN1 privateKeyAlgorithm = new ASN1(0x30);

                privateKeyAlgorithm.Add(ASN1Convert.FromOid(_algorithm));
                privateKeyAlgorithm.Add(new ASN1(0x05));                   // ASN.1 NULL

                ASN1 pki = new ASN1(0x30);

                pki.Add(new ASN1(0x02, new byte [1] {
                    (byte)_version
                }));
                pki.Add(privateKeyAlgorithm);
                pki.Add(new ASN1(0x04, _key));

                if (_list.Count > 0)
                {
                    ASN1 attributes = new ASN1(0xA0);
                    foreach (ASN1 attribute in _list)
                    {
                        attributes.Add(attribute);
                    }
                    pki.Add(attributes);
                }

                return(pki.GetBytes());
            }
Exemplo n.º 4
0
        protected override void Decode()
        {
            this.cA = false;
            this.pathLenConstraint = -1;
            ASN1 asN1_1 = new ASN1(this.extnValue.Value);

            if (asN1_1.Tag != (byte)48)
            {
                throw new ArgumentException("Invalid BasicConstraints extension");
            }
            int  num1   = 0;
            ASN1 asN1_2 = asN1_1;
            int  index1 = num1;
            int  num2   = index1 + 1;
            ASN1 asn1   = asN1_2[index1];

            if (asn1 != null && asn1.Tag == (byte)1)
            {
                this.cA = asn1.Value[0] == byte.MaxValue;
                ASN1 asN1_3 = asN1_1;
                int  index2 = num2;
                int  num3   = index2 + 1;
                asn1 = asN1_3[index2];
            }
            if (asn1 == null || asn1.Tag != (byte)2)
            {
                return;
            }
            this.pathLenConstraint = ASN1Convert.ToInt32(asn1);
        }
Exemplo n.º 5
0
        private byte[] GetRandomNumber()
        {
            byte[] _randomNum;

            ASN1 _randomTop = (ASN1)KeyInfo.Attributes[0];

            if (_randomTop.Tag != 0x30 || _randomTop.Count < 2)
            {
                throw new ProxyException("invalid random number");
            }

            var _algorithm = ASN1Convert.ToOid(_randomTop[0]);

            if (_algorithm == "1.2.410.200004.10.1.1.3")
            {
                ASN1 _randomSeq = _randomTop[1];
                if (_randomSeq.Tag != 0x31 || _randomSeq.Count < 1)
                {
                    throw new ProxyException("invalid random number");
                }

                byte[] _rmvZero = RemoveLeadingZero(_randomSeq[0].Value);

                var _size = _rmvZero.Length;
                _randomNum = new byte[_size];

                Array.Copy(_rmvZero, 0, _randomNum, 0, _size);
            }
            else
            {
                throw new ProxyException("unknown random algorithm");
            }

            return(_randomNum);
        }
Exemplo n.º 6
0
        // Class(60) {
        //   OID(spnego),
        //   Class(A0) {
        //     Class(30) {
        //       Class(A0) {
        //         Class(30) { OID,OID,OID} },
        //       Class(A2) { OctetStream } } } }
        public byte [] ProcessSpnegoInitialContextTokenRequest()
        {
            Type1Message type1 = new Type1Message(NtlmVersion.Version3);

            type1.Flags  = unchecked ((NtlmFlags)0xE21882B7);
            type1.Domain = "WORKGROUP"; // FIXME: remove it

            ASN1 asn     = new ASN1(0x60);
            ASN1 asn2    = new ASN1(0xA0);
            ASN1 asn21   = new ASN1(0x30);
            ASN1 asn211  = new ASN1(0xA0);
            ASN1 asn2111 = new ASN1(0x30);

            asn211.Add(asn2111);
            asn2111.Add(ASN1Convert.FromOid(Constants.OidNtlmSsp));
            asn2111.Add(ASN1Convert.FromOid(Constants.OidKerberos5));
            asn2111.Add(ASN1Convert.FromOid(Constants.OidMIT));
            ASN1 asn212  = new ASN1(0xA2);
            ASN1 asn2121 = new ASN1(0x4);

            asn2121.Value = type1.GetBytes();
            asn212.Add(asn2121);
            asn21.Add(asn211);
            asn21.Add(asn212);
            asn2.Add(asn21);
            asn.Add(ASN1Convert.FromOid(Constants.OidSpnego));
            asn.Add(asn2);
            return(asn.GetBytes());
        }
        protected override void Decode()
        {
            ASN1 asn = new ASN1(this.extnValue.Value);

            if (asn.Tag != 48)
            {
                throw new ArgumentException("Invalid PrivateKeyUsagePeriod extension");
            }
            for (int i = 0; i < asn.Count; i++)
            {
                byte tag = asn[i].Tag;
                if (tag != 128)
                {
                    if (tag != 129)
                    {
                        throw new ArgumentException("Invalid PrivateKeyUsagePeriod extension");
                    }
                    this.notAfter = ASN1Convert.ToDateTime(asn[i]);
                }
                else
                {
                    this.notBefore = ASN1Convert.ToDateTime(asn[i]);
                }
            }
        }
Exemplo n.º 8
0
        protected override void Decode()
        {
            ASN1 sequence = new ASN1(extnValue.Value);

            if (sequence.Tag != 0x30)
            {
                throw new ArgumentException("Invalid PrivateKeyUsagePeriod extension");
            }
            for (int i = 0; i < sequence.Count; i++)
            {
                switch (sequence [i].Tag)
                {
                case 0x80:
                    notBefore = ASN1Convert.ToDateTime(sequence [i]);
                    break;

                case 0x81:
                    notAfter = ASN1Convert.ToDateTime(sequence [i]);
                    break;

                default:
                    throw new ArgumentException("Invalid PrivateKeyUsagePeriod extension");
                }
            }
        }
Exemplo n.º 9
0
        private ASN1 SubjectPublicKeyInfo()
        {
            ASN1 asn = new ASN1(48);

            if (this.aa is RSA)
            {
                asn.Add(PKCS7.AlgorithmIdentifier("1.2.840.113549.1.1.1"));
                RSAParameters rsaparameters = (this.aa as RSA).ExportParameters(false);
                ASN1          asn2          = new ASN1(48);
                asn2.Add(ASN1Convert.FromUnsignedBigInteger(rsaparameters.Modulus));
                asn2.Add(ASN1Convert.FromUnsignedBigInteger(rsaparameters.Exponent));
                asn.Add(new ASN1(this.UniqueIdentifier(asn2.GetBytes())));
            }
            else
            {
                if (!(this.aa is DSA))
                {
                    throw new NotSupportedException("Unknown Asymmetric Algorithm " + this.aa.ToString());
                }
                DSAParameters dsaparameters = (this.aa as DSA).ExportParameters(false);
                ASN1          asn3          = new ASN1(48);
                asn3.Add(ASN1Convert.FromUnsignedBigInteger(dsaparameters.P));
                asn3.Add(ASN1Convert.FromUnsignedBigInteger(dsaparameters.Q));
                asn3.Add(ASN1Convert.FromUnsignedBigInteger(dsaparameters.G));
                asn.Add(PKCS7.AlgorithmIdentifier("1.2.840.10040.4.1", asn3));
                ASN1 asn4 = asn.Add(new ASN1(3));
                asn4.Add(ASN1Convert.FromUnsignedBigInteger(dsaparameters.Y));
            }
            return(asn);
        }
Exemplo n.º 10
0
        private byte[] Header(byte[] fileHash, string hashAlgorithm)
        {
            string oid  = CryptoConfig.MapNameToOID(hashAlgorithm);
            ASN1   aSN  = new ASN1(48);
            ASN1   aSN2 = aSN.Add(new ASN1(48));

            aSN2.Add(ASN1Convert.FromOid("1.3.6.1.4.1.311.2.1.15"));
            aSN2.Add(new ASN1(48, obsolete));
            ASN1 aSN3 = aSN.Add(new ASN1(48));

            aSN3.Add(AlgorithmIdentifier(oid));
            aSN3.Add(new ASN1(4, fileHash));
            pkcs7.HashName = hashAlgorithm;
            pkcs7.Certificates.AddRange(certs);
            pkcs7.ContentInfo.ContentType = "1.3.6.1.4.1.311.2.1.4";
            pkcs7.ContentInfo.Content.Add(aSN);
            pkcs7.SignerInfo.Certificate = certs[0];
            pkcs7.SignerInfo.Key         = rsa;
            ASN1 aSN4 = null;

            aSN4 = ((!(url == null)) ? Attribute("1.3.6.1.4.1.311.2.1.12", Opus(description, url.ToString())) : Attribute("1.3.6.1.4.1.311.2.1.12", Opus(description, null)));
            pkcs7.SignerInfo.AuthenticatedAttributes.Add(aSN4);
            pkcs7.GetASN1();
            return(pkcs7.SignerInfo.Signature);
        }
Exemplo n.º 11
0
        public X509Extension(ASN1 asn1)
        {
            if ((asn1.Tag != 0x30) || (asn1.Count < 2))
            {
                throw new ArgumentException(Locale.GetText("Invalid X.509 extension."));
            }
            if (asn1[0].Tag != 0x06)
            {
                throw new ArgumentException(Locale.GetText("Invalid X.509 extension."));
            }

            extnOid      = ASN1Convert.ToOid(asn1[0]);
            extnCritical = ((asn1[1].Tag == 0x01) && (asn1[1].Value[0] == 0xFF));
            // last element is an octet string which may need to be decoded
            extnValue = asn1 [asn1.Count - 1];
            if ((extnValue.Tag == 0x04) && (extnValue.Length > 0) && (extnValue.Count == 0))
            {
                try {
                    ASN1 encapsulated = new ASN1(extnValue.Value);
                    extnValue.Value = null;
                    extnValue.Add(encapsulated);
                }
                catch {
                    // data isn't ASN.1
                }
            }
            Decode();
        }
Exemplo n.º 12
0
        public static ASN1 ToAsn1(RSA rsa)
        {
            EnsureNotNull(rsa, "rsa");

            ASN1 asn    = new ASN1(0x30);
            ASN1 asnOid = new ASN1(0x30);

            // {iso(1) member-body(2) us(840) rsadsi(113549) pkcs(1) pkcs-1(1) rsaEncryption(1)}
            // http://www.oid-info.com/get/1.2.840.113549.1.1.1
            asnOid.Add(ASN1Convert.FromOid("1.2.840.113549.1.1.1"));

            asnOid.Add(new ASN1(0x05));
            asn.Add(asnOid);

            ASN1 asnBits = new ASN1(0x03, new byte[1]);

            byte[] intermediate = ToAsn1Key(rsa).GetBytes();
            byte[] key          = new byte[intermediate.Length + 1];
            intermediate.CopyTo(key, 1);
            asnBits.Value = key;

            asn.Add(asnBits);

            return(asn);
        }
Exemplo n.º 13
0
            public byte[] GetBytes()
            {
                if (this._algorithm == null)
                {
                    throw new CryptographicException("No algorithm OID specified");
                }
                ASN1 asn1_1 = new ASN1((byte)48);

                asn1_1.Add(ASN1Convert.FromOid(this._algorithm));
                if (this._iterations > 0 || this._salt != null)
                {
                    ASN1 asn1_2 = new ASN1((byte)4, this._salt);
                    ASN1 asn1_3 = ASN1Convert.FromInt32(this._iterations);
                    ASN1 asn1_4 = new ASN1((byte)48);
                    asn1_4.Add(asn1_2);
                    asn1_4.Add(asn1_3);
                    asn1_1.Add(asn1_4);
                }
                ASN1 asn1_5 = new ASN1((byte)4, this._data);
                ASN1 asN1   = new ASN1((byte)48);

                asN1.Add(asn1_1);
                asN1.Add(asn1_5);
                return(asN1.GetBytes());
            }
Exemplo n.º 14
0
            public byte[] GetBytes()
            {
                ASN1 asn1_1 = new ASN1((byte)48);

                asn1_1.Add(ASN1Convert.FromOid(this._algorithm));
                asn1_1.Add(new ASN1((byte)5));
                ASN1 asN1 = new ASN1((byte)48);

                asN1.Add(new ASN1((byte)2, new byte[1]
                {
                    (byte)this._version
                }));
                asN1.Add(asn1_1);
                asN1.Add(new ASN1((byte)4, this._key));
                if (this._list.Count > 0)
                {
                    ASN1 asn1_2 = new ASN1((byte)160);
                    foreach (ASN1 asn1_3 in this._list)
                    {
                        asn1_2.Add(asn1_3);
                    }
                    asN1.Add(asn1_2);
                }
                return(asN1.GetBytes());
            }
Exemplo n.º 15
0
            public byte[] GetBytes()
            {
                ASN1 aSN = new ASN1(48);

                aSN.Add(ASN1Convert.FromOid(_algorithm));
                aSN.Add(new ASN1(5));
                ASN1 aSN2 = new ASN1(48);

                aSN2.Add(new ASN1(2, new byte[1]
                {
                    (byte)_version
                }));
                aSN2.Add(aSN);
                aSN2.Add(new ASN1(4, _key));
                if (_list.Count > 0)
                {
                    ASN1 aSN3 = new ASN1(160);
                    foreach (ASN1 item in _list)
                    {
                        aSN3.Add(item);
                    }
                    aSN2.Add(aSN3);
                }
                return(aSN2.GetBytes());
            }
        protected override void Decode()
        {
            ASN1 asN1 = new ASN1(this.extnValue.Value);

            if (asN1.Tag != (byte)48)
            {
                throw new ArgumentException("Invalid PrivateKeyUsagePeriod extension");
            }
            for (int index = 0; index < asN1.Count; ++index)
            {
                switch (asN1[index].Tag)
                {
                case 128:
                    this.notBefore = ASN1Convert.ToDateTime(asN1[index]);
                    break;

                case 129:
                    this.notAfter = ASN1Convert.ToDateTime(asN1[index]);
                    break;

                default:
                    throw new ArgumentException("Invalid PrivateKeyUsagePeriod extension");
                }
            }
        }
Exemplo n.º 17
0
            public byte[] GetBytes()
            {
                if (_algorithm == null)
                {
                    throw new CryptographicException("No algorithm OID specified");
                }
                ASN1 aSN = new ASN1(48);

                aSN.Add(ASN1Convert.FromOid(_algorithm));
                if (_iterations > 0 || _salt != null)
                {
                    ASN1 asn  = new ASN1(4, _salt);
                    ASN1 asn2 = ASN1Convert.FromInt32(_iterations);
                    ASN1 aSN2 = new ASN1(48);
                    aSN2.Add(asn);
                    aSN2.Add(asn2);
                    aSN.Add(aSN2);
                }
                ASN1 asn3 = new ASN1(4, _data);
                ASN1 aSN3 = new ASN1(48);

                aSN3.Add(aSN);
                aSN3.Add(asn3);
                return(aSN3.GetBytes());
            }
Exemplo n.º 18
0
            public byte[] GetBytes()
            {
                ASN1 asn = new ASN1(48);

                asn.Add(ASN1Convert.FromOid(this._algorithm));
                asn.Add(new ASN1(5));
                ASN1 asn2 = new ASN1(48);

                asn2.Add(new ASN1(2, new byte[]
                {
                    (byte)this._version
                }));
                asn2.Add(asn);
                asn2.Add(new ASN1(4, this._key));
                if (this._list.Count > 0)
                {
                    ASN1 asn3 = new ASN1(160);
                    foreach (object obj in this._list)
                    {
                        ASN1 asn4 = (ASN1)obj;
                        asn3.Add(asn4);
                    }
                    asn2.Add(asn3);
                }
                return(asn2.GetBytes());
            }
Exemplo n.º 19
0
        protected override void Decode()
        {
            // default values
            cA = false;
            pathLenConstraint = NoPathLengthConstraint;

            ASN1 sequence = new ASN1(extnValue.Value);

            if (sequence.Tag != 0x30)
            {
                throw new ArgumentException("Invalid BasicConstraints extension");
            }
            int  n = 0;
            ASN1 a = sequence [n++];

            if ((a != null) && (a.Tag == 0x01))
            {
                cA = (a.Value [0] == 0xFF);
                a  = sequence [n++];
            }
            if ((a != null) && (a.Tag == 0x02))
            {
                pathLenConstraint = ASN1Convert.ToInt32(a);
            }
        }
Exemplo n.º 20
0
            public byte[] GetBytes()
            {
                if (this._algorithm == null)
                {
                    throw new CryptographicException("No algorithm OID specified");
                }
                ASN1 asn = new ASN1(48);

                asn.Add(ASN1Convert.FromOid(this._algorithm));
                if (this._iterations > 0 || this._salt != null)
                {
                    ASN1 asn2 = new ASN1(4, this._salt);
                    ASN1 asn3 = ASN1Convert.FromInt32(this._iterations);
                    ASN1 asn4 = new ASN1(48);
                    asn4.Add(asn2);
                    asn4.Add(asn3);
                    asn.Add(asn4);
                }
                ASN1 asn5 = new ASN1(4, this._data);
                ASN1 asn6 = new ASN1(48);

                asn6.Add(asn);
                asn6.Add(asn5);
                return(asn6.GetBytes());
            }
Exemplo n.º 21
0
        public void FromString_EscapingNonAscii()
        {
            // adapted from rfc2253
            ASN1 asn = X501.FromString(@"CN=Lu\C4\8Di\C4\87,O=Test,C=GB");

            Assert.AreEqual(0x30, asn.Tag, "Tag");
            Assert.AreEqual(3, asn.Count, "Count");

            Assert.AreEqual(0x31, asn[0].Tag, "Tag-0");
            Assert.AreEqual(1, asn[0].Count, "Count-0");
            Assert.AreEqual("2.5.4.3", ASN1Convert.ToOid(asn[0][0][0]), "OID-0");
            char[] value = Encoding.BigEndianUnicode.GetChars(asn[0][0][1].Value);
            Assert.AreEqual(0x004C, value[0], "Value-0[0]");
            Assert.AreEqual(0x0075, value[1], "Value-0[1]");
            Assert.AreEqual(0x010D, value[2], "Value-0[2]");
            Assert.AreEqual(0x0069, value[3], "Value-0[3]");
            Assert.AreEqual(0x0107, value[4], "Value-0[4]");

            Assert.AreEqual(0x31, asn[1].Tag, "Tag-1");
            Assert.AreEqual(1, asn[1].Count, "Count-1");
            Assert.AreEqual("2.5.4.10", ASN1Convert.ToOid(asn[1][0][0]), "OID-1");
            Assert.AreEqual("Test", Encoding.ASCII.GetString(asn[1][0][1].Value), "Value-1");

            Assert.AreEqual(0x31, asn[2].Tag, "Tag-2");
            Assert.AreEqual(1, asn[2].Count, "Count-2");
            Assert.AreEqual("2.5.4.6", ASN1Convert.ToOid(asn[2][0][0]), "OID-2");
            Assert.AreEqual("GB", Encoding.ASCII.GetString(asn[2][0][1].Value), "Value-2");
        }
        // internal

        internal AsnDecodeStatus Decode(byte[] extension)
        {
            if ((extension == null) || (extension.Length == 0))
            {
                return(AsnDecodeStatus.BadAsn);
            }
            if (extension [0] != 0x30)
            {
                return(AsnDecodeStatus.BadTag);
            }

            if (_enhKeyUsage == null)
            {
                _enhKeyUsage = new OidCollection();
            }

            try {
                ASN1 ex = new ASN1(extension);
                if (ex.Tag != 0x30)
                {
                    throw new CryptographicException(Locale.GetText("Invalid ASN.1 Tag"));
                }
                for (int i = 0; i < ex.Count; i++)
                {
                    _enhKeyUsage.Add(new Oid(ASN1Convert.ToOid(ex [i])));
                }
            }
            catch {
                return(AsnDecodeStatus.BadAsn);
            }

            return(AsnDecodeStatus.Ok);
        }
Exemplo n.º 23
0
            internal ASN1 GetASN1(byte encoding)
            {
                byte b = encoding;

                if (b == byte.MaxValue)
                {
                    b = SelectBestEncoding();
                }
                ASN1 aSN = new ASN1(48);

                aSN.Add(ASN1Convert.FromOid(oid));
                switch (b)
                {
                case 19:
                    aSN.Add(new ASN1(19, Encoding.ASCII.GetBytes(attrValue)));
                    break;

                case 22:
                    aSN.Add(new ASN1(22, Encoding.ASCII.GetBytes(attrValue)));
                    break;

                case 30:
                    aSN.Add(new ASN1(30, Encoding.BigEndianUnicode.GetBytes(attrValue)));
                    break;
                }
                return(aSN);
            }
Exemplo n.º 24
0
 public X509Extension(ASN1 asn1)
 {
     if (asn1.Tag != 48 || asn1.Count < 2)
     {
         throw new ArgumentException(Locale.GetText("Invalid X.509 extension."));
     }
     if (asn1[0].Tag != 6)
     {
         throw new ArgumentException(Locale.GetText("Invalid X.509 extension."));
     }
     extnOid      = ASN1Convert.ToOid(asn1[0]);
     extnCritical = (asn1[1].Tag == 1 && asn1[1].Value[0] == byte.MaxValue);
     extnValue    = asn1[asn1.Count - 1];
     if (extnValue.Tag == 4 && extnValue.Length > 0 && extnValue.Count == 0)
     {
         try
         {
             ASN1 asn2 = new ASN1(extnValue.Value);
             extnValue.Value = null;
             extnValue.Add(asn2);
         }
         catch
         {
         }
     }
     Decode();
 }
Exemplo n.º 25
0
 internal X509CrlEntry(ASN1 entry)
 {
     sn = entry[0].Value;
     Array.Reverse(sn);
     revocationDate = ASN1Convert.ToDateTime(entry[1]);
     extensions     = new X509ExtensionCollection(entry[2]);
 }
Exemplo n.º 26
0
            internal ASN1 GetASN1(byte encoding)
            {
                byte b = encoding;

                if (b == 255)
                {
                    b = this.SelectBestEncoding();
                }
                ASN1 asn = new ASN1(48);

                asn.Add(ASN1Convert.FromOid(this.oid));
                byte b2 = b;

                switch (b2)
                {
                case 19:
                    asn.Add(new ASN1(19, Encoding.ASCII.GetBytes(this.attrValue)));
                    break;

                default:
                    if (b2 == 30)
                    {
                        asn.Add(new ASN1(30, Encoding.BigEndianUnicode.GetBytes(this.attrValue)));
                    }
                    break;

                case 22:
                    asn.Add(new ASN1(22, Encoding.ASCII.GetBytes(this.attrValue)));
                    break;
                }
                return(asn);
            }
Exemplo n.º 27
0
 public X509Extension(ASN1 asn1)
 {
     if (asn1.Tag != (byte)48 || asn1.Count < 2)
     {
         throw new ArgumentException(Locale.GetText("Invalid X.509 extension."));
     }
     if (asn1[0].Tag != (byte)6)
     {
         throw new ArgumentException(Locale.GetText("Invalid X.509 extension."));
     }
     this.extnOid      = ASN1Convert.ToOid(asn1[0]);
     this.extnCritical = asn1[1].Tag == (byte)1 && asn1[1].Value[0] == byte.MaxValue;
     this.extnValue    = asn1[asn1.Count - 1];
     if (this.extnValue.Tag == (byte)4 && this.extnValue.Length > 0)
     {
         if (this.extnValue.Count == 0)
         {
             try
             {
                 ASN1 asn1_1 = new ASN1(this.extnValue.Value);
                 this.extnValue.Value = (byte[])null;
                 this.extnValue.Add(asn1_1);
             }
             catch
             {
             }
         }
     }
     this.Decode();
 }
        private byte[] Header(byte[] fileHash, string hashAlgorithm)
        {
            string oid  = CryptoConfig.MapNameToOID(hashAlgorithm);
            ASN1   asn  = new ASN1(48);
            ASN1   asn2 = asn.Add(new ASN1(48));

            asn2.Add(ASN1Convert.FromOid("1.3.6.1.4.1.311.2.1.15"));
            asn2.Add(new ASN1(48, AuthenticodeFormatter.obsolete));
            ASN1 asn3 = asn.Add(new ASN1(48));

            asn3.Add(this.AlgorithmIdentifier(oid));
            asn3.Add(new ASN1(4, fileHash));
            this.pkcs7.HashName = hashAlgorithm;
            this.pkcs7.Certificates.AddRange(this.certs);
            this.pkcs7.ContentInfo.ContentType = "1.3.6.1.4.1.311.2.1.4";
            this.pkcs7.ContentInfo.Content.Add(asn);
            this.pkcs7.SignerInfo.Certificate = this.certs[0];
            this.pkcs7.SignerInfo.Key         = this.rsa;
            ASN1 value;

            if (this.url == null)
            {
                value = this.Attribute("1.3.6.1.4.1.311.2.1.12", this.Opus(this.description, null));
            }
            else
            {
                value = this.Attribute("1.3.6.1.4.1.311.2.1.12", this.Opus(this.description, this.url.ToString()));
            }
            this.pkcs7.SignerInfo.AuthenticatedAttributes.Add(value);
            this.pkcs7.GetASN1();
            return(this.pkcs7.SignerInfo.Signature);
        }
Exemplo n.º 29
0
            // Note: PKCS#8 doesn't define how to generate the key required for encryption
            // so you're on your own. Just don't try to copy the big guys too much ;)
            // Netscape:	http://www.cs.auckland.ac.nz/~pgut001/pubs/netscape.txt
            // Microsoft:	http://www.cs.auckland.ac.nz/~pgut001/pubs/breakms.txt
            public byte[] GetBytes()
            {
                if (_algorithm == null)
                {
                    throw new CryptographicException("No algorithm OID specified");
                }

                ASN1 encryptionAlgorithm = new ASN1(0x30);

                encryptionAlgorithm.Add(ASN1Convert.FromOid(_algorithm));

                // parameters ANY DEFINED BY algorithm OPTIONAL
                if ((_iterations > 0) || (_salt != null))
                {
                    ASN1 salt       = new ASN1(0x04, _salt);
                    ASN1 iterations = ASN1Convert.FromInt32(_iterations);

                    ASN1 parameters = new ASN1(0x30);
                    parameters.Add(salt);
                    parameters.Add(iterations);
                    encryptionAlgorithm.Add(parameters);
                }

                // encapsulates EncryptedData into an OCTET STRING
                ASN1 encryptedData = new ASN1(0x04, _data);

                ASN1 encryptedPrivateKeyInfo = new ASN1(0x30);

                encryptedPrivateKeyInfo.Add(encryptionAlgorithm);
                encryptedPrivateKeyInfo.Add(encryptedData);

                return(encryptedPrivateKeyInfo.GetBytes());
            }
Exemplo n.º 30
0
        ///
        /// SEQUENCE (a)
        ///  +- INTEGER (V)              // Version - 0 (v1998)
        ///  +- SEQUENCE (b)
        ///  |   +- OID (oid)            // 1.2.840.113549.1.1.1
        ///  |   +- Nil (c)
        ///  +- OCTETSTRING(PRVKY) (os)  // Private Key Parameter
        ///
        ///  However, OCTETSTRING(PRVKY) wraps
        ///    SEQUENCE(
        ///      INTEGER(0)              // Version - 0 (v1998)
        ///      INTEGER(N)
        ///      INTEGER(E)
        ///      INTEGER(D)
        ///      INTEGER(P)
        ///      INTEGER(Q)
        ///      INTEGER(DP)
        ///      INTEGER(DQ)
        ///      INTEGER(InvQ)
        ///    )
        public static byte[] RSAKeyToASN1(RSAParameters PrivateKey)
        {
            ASN1 v = ASN1Convert.FromUnsignedBigInteger(new byte[] { 0 });

            ASN1 b = PKCS7.AlgorithmIdentifier("1.2.840.113549.1.1.1");

            ASN1 os = new ASN1(0x30);

            os.Add(ASN1Convert.FromUnsignedBigInteger(new byte[] { 0 }));
            os.Add(ASN1Convert.FromUnsignedBigInteger(PrivateKey.Modulus));
            os.Add(ASN1Convert.FromUnsignedBigInteger(PrivateKey.Exponent));
            os.Add(ASN1Convert.FromUnsignedBigInteger(PrivateKey.D));
            os.Add(ASN1Convert.FromUnsignedBigInteger(PrivateKey.P));
            os.Add(ASN1Convert.FromUnsignedBigInteger(PrivateKey.Q));
            os.Add(ASN1Convert.FromUnsignedBigInteger(PrivateKey.DP));
            os.Add(ASN1Convert.FromUnsignedBigInteger(PrivateKey.DQ));
            os.Add(ASN1Convert.FromUnsignedBigInteger(PrivateKey.InverseQ));

            ASN1 pem = new ASN1(0x30);

            pem.Add(v);
            pem.Add(b);
            // Make this into an OCTET string
            pem.Add(new ASN1(0x04, os.GetBytes()));
            return(pem.GetBytes());
        }