Esempio n. 1
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);
        }
Esempio n. 2
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);
        }
Esempio n. 3
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());
        }
        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);
        }
Esempio n. 5
0
        private byte[] Build(ASN1 tbs, string hashoid, byte[] signature)
        {
            ASN1 asn = new ASN1(48);

            asn.Add(tbs);
            asn.Add(PKCS7.AlgorithmIdentifier(hashoid));
            byte[] array = new byte[signature.Length + 1];
            Buffer.BlockCopy(signature, 0, array, 1, signature.Length);
            asn.Add(new ASN1(3, array));
            return(asn.GetBytes());
        }
Esempio n. 6
0
        private byte[] Build(ASN1 tbs, string hashoid, byte[] signature)
        {
            ASN1 builder = new ASN1(0x30);

            builder.Add(tbs);
            builder.Add(PKCS7.AlgorithmIdentifier(hashoid));
            // first byte of BITSTRING is the number of unused bits in the first byte
            byte[] bitstring = new byte [signature.Length + 1];
            Buffer.BlockCopy(signature, 0, bitstring, 1, signature.Length);
            builder.Add(new ASN1(0x03, bitstring));
            return(builder.GetBytes());
        }
        /* SubjectPublicKeyInfo  ::=  SEQUENCE  {
         *      algorithm            AlgorithmIdentifier,
         *      subjectPublicKey     BIT STRING  }
         */
        private ASN1 SubjectPublicKeyInfo()
        {
            ASN1 keyInfo = new ASN1(0x30);

            if (aa is RSA)
            {
                keyInfo.Add(PKCS7.AlgorithmIdentifier("1.2.840.113549.1.1.1"));
                RSAParameters p = (aa as RSA).ExportParameters(false);

                /* RSAPublicKey ::= SEQUENCE {
                 *       modulus            INTEGER,    -- n
                 *       publicExponent     INTEGER  }  -- e
                 */
                ASN1 key = new ASN1(0x30);
                key.Add(ASN1Convert.FromUnsignedBigInteger(p.Modulus));
                key.Add(ASN1Convert.FromUnsignedBigInteger(p.Exponent));
                keyInfo.Add(new ASN1(UniqueIdentifier(key.GetBytes())));
            }
            else if (aa is DSA)
            {
                DSAParameters p = (aa as DSA).ExportParameters(false);

                /* Dss-Parms  ::=  SEQUENCE  {
                 *       p             INTEGER,
                 *       q             INTEGER,
                 *       g             INTEGER  }
                 */
                ASN1 param = new ASN1(0x30);
                param.Add(ASN1Convert.FromUnsignedBigInteger(p.P));
                param.Add(ASN1Convert.FromUnsignedBigInteger(p.Q));
                param.Add(ASN1Convert.FromUnsignedBigInteger(p.G));
                keyInfo.Add(PKCS7.AlgorithmIdentifier("1.2.840.10040.4.1", param));
                ASN1 key = keyInfo.Add(new ASN1(0x03));
                // DSAPublicKey ::= INTEGER  -- public key, y
                key.Add(ASN1Convert.FromUnsignedBigInteger(p.Y));
            }
            else
            {
                throw new NotSupportedException("Unknown Asymmetric Algorithm " + aa.ToString());
            }
            return(keyInfo);
        }
Esempio n. 8
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);
        }
Esempio 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);
        }
Esempio n. 10
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);
        }
Esempio n. 11
0
 public ASN1 TimestampRequest(byte[] signature)
 {
     PKCS7.ContentInfo ci = new PKCS7.ContentInfo(PKCS7.Oid.data);
     ci.Content.Add(new ASN1(0x04, signature));
     return(PKCS7.AlgorithmIdentifier(timestampCountersignature, ci.ASN1));
 }
 public ASN1 TimestampRequest(byte[] signature)
 {
     PKCS7.ContentInfo contentInfo = new PKCS7.ContentInfo("1.2.840.113549.1.7.1");
     contentInfo.Content.Add(new ASN1(4, signature));
     return(PKCS7.AlgorithmIdentifier("1.3.6.1.4.1.311.3.2.1", contentInfo.ASN1));
 }