Beispiel #1
0
        public uint GetCertPublicKeySize(X509Certificate cert)
        {
            byte[] publickeyblob;
            byte[] encodedpubkey = cert.GetPublicKey(); //asn.1 encoded public key

            uint blobbytes = 0;

            if (verbose)
            {
                Console.WriteLine();
                showBytes("Encoded publickey", encodedpubkey);
                Console.WriteLine();
            }
            if (Win32.CryptDecodeObject(ENCODING_TYPE, RSA_CSP_PUBLICKEYBLOB, encodedpubkey, (uint)encodedpubkey.Length, 0, null, ref blobbytes))
            {
                publickeyblob = new byte[blobbytes];
                if (Win32.CryptDecodeObject(ENCODING_TYPE, RSA_CSP_PUBLICKEYBLOB, encodedpubkey, (uint)encodedpubkey.Length, 0, publickeyblob, ref blobbytes))
                {
                    if (verbose)
                    {
                        showBytes("CryptoAPI publickeyblob", publickeyblob);
                    }
                }
            }
            else
            {
                Console.WriteLine("Couldn't decode publickeyblob from certificate publickey");
                return(0);
            }

            PUBKEYBLOBHEADERS pkheaders = new PUBKEYBLOBHEADERS();
            int    headerslength        = Marshal.SizeOf(pkheaders);
            IntPtr buffer = Marshal.AllocHGlobal(headerslength);

            Marshal.Copy(publickeyblob, 0, buffer, headerslength);
            pkheaders = (PUBKEYBLOBHEADERS)Marshal.PtrToStructure(buffer, typeof(PUBKEYBLOBHEADERS));
            Marshal.FreeHGlobal(buffer);

            if (verbose)
            {
                Console.WriteLine("\n ---- PUBLICKEYBLOB headers ------");
                Console.WriteLine("  btype     {0}", pkheaders.bType);
                Console.WriteLine("  bversion  {0}", pkheaders.bVersion);
                Console.WriteLine("  reserved  {0}", pkheaders.reserved);
                Console.WriteLine("  aiKeyAlg  0x{0:x8}", pkheaders.aiKeyAlg);
                String magicstring = (new ASCIIEncoding()).GetString(BitConverter.GetBytes(pkheaders.magic));
                Console.WriteLine("  magic     0x{0:x8}     '{1}'", pkheaders.magic, magicstring);
                Console.WriteLine("  bitlen    {0}", pkheaders.bitlen);
                Console.WriteLine("  pubexp    {0}", pkheaders.pubexp);
                Console.WriteLine(" --------------------------------");
            }
            //-----  Get public key size in bits -------------
            this.certkeysize = pkheaders.bitlen;

            return(this.certkeysize);
        }
        public uint GetCertPublicKeySize(X509Certificate cert)
        {
            byte[] publickeyblob;
            byte[] encodedpubkey = cert.GetPublicKey(); //asn.1 encoded public key

            uint blobbytes = 0;
            if (verbose)
            {
                Console.WriteLine();
                showBytes("Encoded publickey", encodedpubkey);
                Console.WriteLine();
            }
            if (Win32.CryptDecodeObject(ENCODING_TYPE, RSA_CSP_PUBLICKEYBLOB, encodedpubkey, (uint)encodedpubkey.Length, 0, null, ref blobbytes))
            {
                publickeyblob = new byte[blobbytes];
                if (Win32.CryptDecodeObject(ENCODING_TYPE, RSA_CSP_PUBLICKEYBLOB, encodedpubkey, (uint)encodedpubkey.Length, 0, publickeyblob, ref blobbytes))
                    if (verbose)
                        showBytes("CryptoAPI publickeyblob", publickeyblob);
            }
            else
            {
                Console.WriteLine("Couldn't decode publickeyblob from certificate publickey");
                return 0;
            }

            PUBKEYBLOBHEADERS pkheaders = new PUBKEYBLOBHEADERS();
            int headerslength = Marshal.SizeOf(pkheaders);
            IntPtr buffer = Marshal.AllocHGlobal(headerslength);
            Marshal.Copy(publickeyblob, 0, buffer, headerslength);
            pkheaders = (PUBKEYBLOBHEADERS)Marshal.PtrToStructure(buffer, typeof(PUBKEYBLOBHEADERS));
            Marshal.FreeHGlobal(buffer);

            if (verbose)
            {
                Console.WriteLine("\n ---- PUBLICKEYBLOB headers ------");
                Console.WriteLine("  btype     {0}", pkheaders.bType);
                Console.WriteLine("  bversion  {0}", pkheaders.bVersion);
                Console.WriteLine("  reserved  {0}", pkheaders.reserved);
                Console.WriteLine("  aiKeyAlg  0x{0:x8}", pkheaders.aiKeyAlg);
                String magicstring = (new ASCIIEncoding()).GetString(BitConverter.GetBytes(pkheaders.magic));
                Console.WriteLine("  magic     0x{0:x8}     '{1}'", pkheaders.magic, magicstring);
                Console.WriteLine("  bitlen    {0}", pkheaders.bitlen);
                Console.WriteLine("  pubexp    {0}", pkheaders.pubexp);
                Console.WriteLine(" --------------------------------");
            }
            //-----  Get public key size in bits -------------
            this.certkeysize = pkheaders.bitlen;

            return this.certkeysize;
        }
Beispiel #3
0
        //----- decode public key and extract modulus and exponent ----
        private bool GetCertPublicKey(X509Certificate cert)
        {
            byte[] publickeyblob;
            byte[] encodedpubkey = cert.GetPublicKey(); //asn.1 encoded public key

            uint blobbytes = 0;

            if (verbose)
            {
                Console.WriteLine();
                showBytes("Encoded publickey", encodedpubkey);
                Console.WriteLine();
            }
            if (Win32.CryptDecodeObject(ENCODING_TYPE, RSA_CSP_PUBLICKEYBLOB, encodedpubkey, (uint)encodedpubkey.Length, 0, null, ref blobbytes))
            {
                publickeyblob = new byte[blobbytes];
                if (Win32.CryptDecodeObject(ENCODING_TYPE, RSA_CSP_PUBLICKEYBLOB, encodedpubkey, (uint)encodedpubkey.Length, 0, publickeyblob, ref blobbytes))
                {
                    if (verbose)
                    {
                        showBytes("CryptoAPI publickeyblob", publickeyblob);
                    }
                }
            }
            else
            {
                Console.WriteLine("Couldn't decode publickeyblob from certificate publickey");
                return(false);
            }

            PUBKEYBLOBHEADERS pkheaders = new PUBKEYBLOBHEADERS();
            int    headerslength        = Marshal.SizeOf(pkheaders);
            IntPtr buffer = Marshal.AllocHGlobal(headerslength);

            Marshal.Copy(publickeyblob, 0, buffer, headerslength);
            pkheaders = (PUBKEYBLOBHEADERS)Marshal.PtrToStructure(buffer, typeof(PUBKEYBLOBHEADERS));
            Marshal.FreeHGlobal(buffer);

            if (verbose)
            {
                Console.WriteLine("\n ---- PUBLICKEYBLOB headers ------");
                Console.WriteLine("  btype     {0}", pkheaders.bType);
                Console.WriteLine("  bversion  {0}", pkheaders.bVersion);
                Console.WriteLine("  reserved  {0}", pkheaders.reserved);
                Console.WriteLine("  aiKeyAlg  0x{0:x8}", pkheaders.aiKeyAlg);
                String magicstring = (new ASCIIEncoding()).GetString(BitConverter.GetBytes(pkheaders.magic));
                Console.WriteLine("  magic     0x{0:x8}     '{1}'", pkheaders.magic, magicstring);
                Console.WriteLine("  bitlen    {0}", pkheaders.bitlen);
                Console.WriteLine("  pubexp    {0}", pkheaders.pubexp);
                Console.WriteLine(" --------------------------------");
            }
            //-----  Get public key size in bits -------------
            this.certkeysize = pkheaders.bitlen;

            //-----  Get public exponent -------------
            byte[] exponent = BitConverter.GetBytes(pkheaders.pubexp); //little-endian ordered
            Array.Reverse(exponent);                                   //convert to big-endian order
            this.certkeyexponent = exponent;
            if (verbose)
            {
                showBytes("\nPublic key exponent (big-endian order):", exponent);
            }

            //-----  Get modulus  -------------
            int modulusbytes = (int)pkheaders.bitlen / 8;

            byte[] modulus = new byte[modulusbytes];
            try
            {
                Array.Copy(publickeyblob, headerslength, modulus, 0, modulusbytes);
                Array.Reverse(modulus);   //convert from little to big-endian ordering.
                this.certkeymodulus = modulus;
                if (verbose)
                {
                    showBytes("\nPublic key modulus  (big-endian order):", modulus);
                }
            }
            catch (Exception)
            {
                Console.WriteLine("Problem getting modulus from publickeyblob");
                return(false);
            }
            return(true);
        }
        //----- decode public key and extract modulus and exponent ----
        private bool GetCertPublicKey(X509Certificate cert)
        {
            byte[] publickeyblob;
            byte[] encodedpubkey = cert.GetPublicKey(); //asn.1 encoded public key

            uint blobbytes = 0;
            if (verbose)
            {
                Console.WriteLine();
                showBytes("Encoded publickey", encodedpubkey);
                Console.WriteLine();
            }
            if (Win32.CryptDecodeObject(ENCODING_TYPE, RSA_CSP_PUBLICKEYBLOB, encodedpubkey, (uint)encodedpubkey.Length, 0, null, ref blobbytes))
            {
                publickeyblob = new byte[blobbytes];
                if (Win32.CryptDecodeObject(ENCODING_TYPE, RSA_CSP_PUBLICKEYBLOB, encodedpubkey, (uint)encodedpubkey.Length, 0, publickeyblob, ref blobbytes))
                    if (verbose)
                        showBytes("CryptoAPI publickeyblob", publickeyblob);
            }
            else
            {
                Console.WriteLine("Couldn't decode publickeyblob from certificate publickey");
                return false;
            }

            PUBKEYBLOBHEADERS pkheaders = new PUBKEYBLOBHEADERS();
            int headerslength = Marshal.SizeOf(pkheaders);
            IntPtr buffer = Marshal.AllocHGlobal(headerslength);
            Marshal.Copy(publickeyblob, 0, buffer, headerslength);
            pkheaders = (PUBKEYBLOBHEADERS)Marshal.PtrToStructure(buffer, typeof(PUBKEYBLOBHEADERS));
            Marshal.FreeHGlobal(buffer);

            if (verbose)
            {
                Console.WriteLine("\n ---- PUBLICKEYBLOB headers ------");
                Console.WriteLine("  btype     {0}", pkheaders.bType);
                Console.WriteLine("  bversion  {0}", pkheaders.bVersion);
                Console.WriteLine("  reserved  {0}", pkheaders.reserved);
                Console.WriteLine("  aiKeyAlg  0x{0:x8}", pkheaders.aiKeyAlg);
                String magicstring = (new ASCIIEncoding()).GetString(BitConverter.GetBytes(pkheaders.magic));
                Console.WriteLine("  magic     0x{0:x8}     '{1}'", pkheaders.magic, magicstring);
                Console.WriteLine("  bitlen    {0}", pkheaders.bitlen);
                Console.WriteLine("  pubexp    {0}", pkheaders.pubexp);
                Console.WriteLine(" --------------------------------");
            }
            //-----  Get public key size in bits -------------
            this.certkeysize = pkheaders.bitlen;

            //-----  Get public exponent -------------
            byte[] exponent = BitConverter.GetBytes(pkheaders.pubexp); //little-endian ordered
            Array.Reverse(exponent);    //convert to big-endian order
            this.certkeyexponent = exponent;
            if (verbose)
                showBytes("\nPublic key exponent (big-endian order):", exponent);

            //-----  Get modulus  -------------
            int modulusbytes = (int)pkheaders.bitlen / 8;
            byte[] modulus = new byte[modulusbytes];
            try
            {
                Array.Copy(publickeyblob, headerslength, modulus, 0, modulusbytes);
                Array.Reverse(modulus);   //convert from little to big-endian ordering.
                this.certkeymodulus = modulus;
                if (verbose)
                    showBytes("\nPublic key modulus  (big-endian order):", modulus);
            }
            catch (Exception)
            {
                Console.WriteLine("Problem getting modulus from publickeyblob");
                return false;
            }
            return true;
        }