Ejemplo 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);
        }
Ejemplo 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);
        }
Ejemplo 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());
        }
Ejemplo n.º 4
0
        public static ASN1 ToAsn1Key(RSA rsa)
        {
            EnsureNotNull(rsa, "rsa");

            RSAParameters parameters = rsa.ExportParameters(false);

            ASN1 asnKey = new ASN1(0x30);

            asnKey.Add(ASN1Convert.FromUnsignedBigInteger(parameters.Modulus));
            asnKey.Add(ASN1Convert.FromUnsignedBigInteger(parameters.Exponent));
            return(asnKey);
        }
Ejemplo n.º 5
0
        public void ConvertFromUnsignedBigInteger()
        {
            byte[] big = new byte [16];

            big [0] = 1;
            ASN1 bigint = ASN1Convert.FromUnsignedBigInteger(big);

            // no byte is required for the sign (100% positive it's positive ;-)
            Assert.AreEqual(16, bigint.Value.Length, "BigInteger-NotExtended");

            big [0] = 0x80;
            bigint  = ASN1Convert.FromUnsignedBigInteger(big);
            // one byte added as 0x00 to be sure it's not interpreted as a negative
            Assert.AreEqual(17, bigint.Value.Length, "BigInteger-SignExtended");
        }
Ejemplo n.º 6
0
            public static byte[] Encode(RSA rsa)
            {
                RSAParameters rSAParameters = rsa.ExportParameters(includePrivateParameters: true);
                ASN1          aSN           = new ASN1(48);

                aSN.Add(new ASN1(2, new byte[1]));
                aSN.Add(ASN1Convert.FromUnsignedBigInteger(rSAParameters.Modulus));
                aSN.Add(ASN1Convert.FromUnsignedBigInteger(rSAParameters.Exponent));
                aSN.Add(ASN1Convert.FromUnsignedBigInteger(rSAParameters.D));
                aSN.Add(ASN1Convert.FromUnsignedBigInteger(rSAParameters.P));
                aSN.Add(ASN1Convert.FromUnsignedBigInteger(rSAParameters.Q));
                aSN.Add(ASN1Convert.FromUnsignedBigInteger(rSAParameters.DP));
                aSN.Add(ASN1Convert.FromUnsignedBigInteger(rSAParameters.DQ));
                aSN.Add(ASN1Convert.FromUnsignedBigInteger(rSAParameters.InverseQ));
                return(aSN.GetBytes());
            }
Ejemplo n.º 7
0
            public static byte[] Encode(RSA rsa)
            {
                RSAParameters rsaparameters = rsa.ExportParameters(true);
                ASN1          asn           = new ASN1(48);

                asn.Add(new ASN1(2, new byte[1]));
                asn.Add(ASN1Convert.FromUnsignedBigInteger(rsaparameters.Modulus));
                asn.Add(ASN1Convert.FromUnsignedBigInteger(rsaparameters.Exponent));
                asn.Add(ASN1Convert.FromUnsignedBigInteger(rsaparameters.D));
                asn.Add(ASN1Convert.FromUnsignedBigInteger(rsaparameters.P));
                asn.Add(ASN1Convert.FromUnsignedBigInteger(rsaparameters.Q));
                asn.Add(ASN1Convert.FromUnsignedBigInteger(rsaparameters.DP));
                asn.Add(ASN1Convert.FromUnsignedBigInteger(rsaparameters.DQ));
                asn.Add(ASN1Convert.FromUnsignedBigInteger(rsaparameters.InverseQ));
                return(asn.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);
        }
Ejemplo n.º 9
0
            /*
             * RSAPrivateKey ::= SEQUENCE {
             *	version           Version,
             *	modulus           INTEGER,  -- n
             *	publicExponent    INTEGER,  -- e
             *	privateExponent   INTEGER,  -- d
             *	prime1            INTEGER,  -- p
             *	prime2            INTEGER,  -- q
             *	exponent1         INTEGER,  -- d mod (p-1)
             *	exponent2         INTEGER,  -- d mod (q-1)
             *	coefficient       INTEGER,  -- (inverse of q) mod p
             *	otherPrimeInfos   OtherPrimeInfos OPTIONAL
             * }
             */
            static public byte[] Encode(RSA rsa)
            {
                RSAParameters param = rsa.ExportParameters(true);

                ASN1 rsaPrivateKey = new ASN1(0x30);

                rsaPrivateKey.Add(new ASN1(0x02, new byte [1] {
                    0x00
                }));
                rsaPrivateKey.Add(ASN1Convert.FromUnsignedBigInteger(param.Modulus));
                rsaPrivateKey.Add(ASN1Convert.FromUnsignedBigInteger(param.Exponent));
                rsaPrivateKey.Add(ASN1Convert.FromUnsignedBigInteger(param.D));
                rsaPrivateKey.Add(ASN1Convert.FromUnsignedBigInteger(param.P));
                rsaPrivateKey.Add(ASN1Convert.FromUnsignedBigInteger(param.Q));
                rsaPrivateKey.Add(ASN1Convert.FromUnsignedBigInteger(param.DP));
                rsaPrivateKey.Add(ASN1Convert.FromUnsignedBigInteger(param.DQ));
                rsaPrivateKey.Add(ASN1Convert.FromUnsignedBigInteger(param.InverseQ));

                return(rsaPrivateKey.GetBytes());
            }
Ejemplo n.º 10
0
            static public byte[] Encode(DSA dsa)
            {
                DSAParameters param = dsa.ExportParameters(true);

                return(ASN1Convert.FromUnsignedBigInteger(param.X).GetBytes());
            }
Ejemplo n.º 11
0
 public void ConvertFromUnsignedBigInteger_Null()
 {
     ASN1Convert.FromUnsignedBigInteger(null);
 }
Ejemplo n.º 12
0
            public static byte[] Encode(DSA dsa)
            {
                DSAParameters dSAParameters = dsa.ExportParameters(includePrivateParameters: true);

                return(ASN1Convert.FromUnsignedBigInteger(dSAParameters.X).GetBytes());
            }
Ejemplo n.º 13
0
 public static byte[] Encode(DSA dsa)
 {
     return(ASN1Convert.FromUnsignedBigInteger(dsa.ExportParameters(true).X).GetBytes());
 }