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);
        }
示例#2
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());
            }
示例#3
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);
        }
示例#4
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());
            }
示例#5
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());
            }
示例#6
0
        /// <summary>
        /// Creates a authority key identifier extension.
        /// </summary>
        /// <param name="keyIdentifier">The key identifier.</param>
        /// <returns>
        /// A authority key identifier extension that contains the key identifier.
        /// </returns>
        /// <exception cref="ArgumentNullException"><paramref name="keyIdentifier"/> is <c>null</c>.</exception>
        static X509Extension CreateAuthorityKeyIdentifier(byte[] keyIdentifier)
        {
            if (keyIdentifier == null)
            {
                throw new ArgumentNullException("keyIdentifier");
            }

            var asn = new ASN1(0x30);

            asn.Add(ASN1Convert.FromOid("2.5.29.35"));

            var binaryKeyIdentifier = EncodeOctetString(keyIdentifier);

            var buffer = new byte[binaryKeyIdentifier.Length + 1];

            buffer[0] = 0x80;

            Buffer.BlockCopy(
                binaryKeyIdentifier,
                0,
                buffer,
                1,
                binaryKeyIdentifier.Length
                );

            asn.Add(new ASN1(4, new ASN1(0x30, buffer).GetBytes()));

            return(new X509Extension(asn));
        }
示例#7
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());
            }
        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);
        }
示例#9
0
            internal ASN1 GetASN1(byte encoding)
            {
                byte num1 = encoding;

                if (num1 == byte.MaxValue)
                {
                    num1 = this.SelectBestEncoding();
                }
                ASN1 asN1 = new ASN1((byte)48);

                asN1.Add(ASN1Convert.FromOid(this.oid));
                byte num2 = num1;

                switch (num2)
                {
                case 19:
                    asN1.Add(new ASN1((byte)19, Encoding.ASCII.GetBytes(this.attrValue)));
                    break;

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

                default:
                    if (num2 == (byte)30)
                    {
                        asN1.Add(new ASN1((byte)30, Encoding.BigEndianUnicode.GetBytes(this.attrValue)));
                        break;
                    }
                    break;
                }
                return(asN1);
            }
示例#10
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());
            }
示例#11
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);
            }
示例#12
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);
            }
示例#13
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);
            }
示例#14
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());
            }
示例#15
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());
            }
示例#16
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());
            }
示例#17
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());
        }
        private ASN1 AlgorithmIdentifier(string oid)
        {
            ASN1 asn = new ASN1(48);

            asn.Add(ASN1Convert.FromOid(oid));
            asn.Add(new ASN1(5));
            return(asn);
        }
示例#19
0
        public void ConvertOID()
        {
            string expected = "1.2.840.113549.1.7.6";
            ASN1   oid      = ASN1Convert.FromOid(expected);
            string actual   = ASN1Convert.ToOid(oid);

            Assert.AreEqual(expected, actual, "OID");
        }
        private ASN1 Attribute(string oid, ASN1 value)
        {
            ASN1 asn = new ASN1(0x30);

            asn.Add(ASN1Convert.FromOid(oid));
            asn.Add(new ASN1(0x31)).Add(value);
            return(asn);
        }
示例#21
0
        static public ASN1 AlgorithmIdentifier(string oid, ASN1 parameters)
        {
            ASN1 ai = new ASN1(0x30);

            ai.Add(ASN1Convert.FromOid(oid));
            ai.Add(parameters);
            return(ai);
        }
示例#22
0
 internal byte[] Encode()
 {
     if (_contentType == null)
     {
         return(null);
     }
     return(ASN1Convert.FromOid(_contentType.Value).GetBytes());
 }
示例#23
0
        static public ASN1 AlgorithmIdentifier(string oid)
        {
            ASN1 ai = new ASN1(0x30);

            ai.Add(ASN1Convert.FromOid(oid));
            ai.Add(new ASN1(0x05));                     // NULL
            return(ai);
        }
示例#24
0
        public void ConvertOID_LargeX()
        {
            ASN1   asn = new ASN1(0x06, new byte [] { 0xA8, 0x00, 0x00 });
            string oid = ASN1Convert.ToOid(asn);

            Assert.AreEqual("2.88.0.0", oid, "ToOID");
            Assert.AreEqual(BitConverter.ToString(asn.GetBytes()),
                            BitConverter.ToString(ASN1Convert.FromOid(oid).GetBytes()), "FromOID");
        }
示例#25
0
        private ASN1 Attribute(string oid, ASN1 value)
        {
            ASN1 aSN = new ASN1(48);

            aSN.Add(ASN1Convert.FromOid(oid));
            ASN1 aSN2 = aSN.Add(new ASN1(49));

            aSN2.Add(value);
            return(aSN);
        }
示例#26
0
        internal byte[] Encode()
        {
            ASN1 asn = new ASN1(48);

            foreach (Oid oid in this._enhKeyUsage)
            {
                asn.Add(ASN1Convert.FromOid(oid.Value));
            }
            return(asn.GetBytes());
        }
        internal byte[] Encode()
        {
            ASN1 ex = new ASN1(0x30);

            foreach (Oid oid in _enhKeyUsage)
            {
                ex.Add(ASN1Convert.FromOid(oid.Value));
            }
            return(ex.GetBytes());
        }
示例#28
0
        static public ASN1 Attribute(string oid, ASN1 value)
        {
            ASN1 attr = new ASN1(0x30);

            attr.Add(ASN1Convert.FromOid(oid));
            ASN1 aset = attr.Add(new ASN1(0x31));

            aset.Add(value);
            return(attr);
        }
        private ASN1 Attribute(string oid, ASN1 value)
        {
            ASN1 asn = new ASN1(48);

            asn.Add(ASN1Convert.FromOid(oid));
            ASN1 asn2 = asn.Add(new ASN1(49));

            asn2.Add(value);
            return(asn);
        }
示例#30
0
 static private bool IsOid(string oid)
 {
     try {
         ASN1 asn = ASN1Convert.FromOid(oid);
         return(asn.Tag == 0x06);
     }
     catch {
         return(false);
     }
 }