Пример #1
0
 public static X509Certificate LoadPKCS12Certificate(string certFilename, string password)
 {
     using (var certFile = BIO.File(certFilename, "r"))
     {
         return(X509Certificate.FromPKCS12(certFile, password));
     }
 }
Пример #2
0
 private X509Certificate LoadPKCS12Certificate(string certFilename, string password)
 {
     using (BIO certFile = BIO.File(certFilename, "r"))
     {
         return(X509Certificate.FromPKCS12(certFile, password));
     }
 }
Пример #3
0
        void LoadCSRInfo(string csrfile)
        {
            X509Request req = null;

            using (var bio = BIO.File(csrfile, "r"))
                req = new X509Request(bio);
            CSReq = req;



            //ArrayList s = new ArrayList();
            //certificationRequestInfo = csri;
            //ArrayList oid = csri.Subject.GetOids();
            //ArrayList vals = csri.Subject.GetValues();

            Dictionary <string, string> xvals = new Dictionary <string, string>();

            foreach (string k in req.Subject.OneLine.Split('/'))
            {
                if (!string.IsNullOrEmpty(k))
                {
                    string[] val = k.Split('=');
                    if (!xvals.ContainsKey(val[0]))
                    {
                        xvals.Add(val[0], val[1]);
                    }
                }
            }

            x509NameBind bind = new x509NameBind();

            bind.Vals     = xvals;
            nameFrm1.bind = bind;
            nameFrm1.LoadNames();
        }
Пример #4
0
        public static BIO GetInFile(string infile)
        {
            BIO bio;

            if (string.IsNullOrEmpty(infile))
            {
                bio = BIO.MemoryBuffer();
                var cin = Console.OpenStandardInput();
                var buf = new byte[1024];
                while (true)
                {
                    var len = cin.Read(buf, 0, buf.Length);

                    if (len == 0)
                    {
                        break;
                    }

                    bio.Write(buf, len);
                }

                return(bio);
            }

            return(BIO.File(infile, "r"));
        }
Пример #5
0
        private void buttonX1_Click(object sender, EventArgs e)
        {
            try
            {
                if (cadlg.ShowDialog() == DialogResult.OK)
                {
                    if (cadlg.FileName.EndsWith(".p12") || cadlg.FileName.EndsWith(".pfx"))
                    {
                        PassForm frm = new PassForm();
                        frm.ShowDialog();
                        if (frm.pass != null)
                        {
                            textBoxX1.Text = cadlg.FileName;

                            using (var bio = BIO.File(cadlg.FileName, "r"))
                                PFX = new PKCS12(bio, frm.pass);
                            CACert            = new System.Security.Cryptography.X509Certificates.X509Certificate2(PFX.Certificate.DER);
                            stepItem5.Value   = 50;
                            textBoxX3.Enabled = true;
                            buttonX3.Enabled  = true;
                            buttonX2.Enabled  = true;
                        }
                        else
                        {
                            throw new ArgumentException("PKCS12 password was not defined");
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBoxEx.Show(ex.Message, "CA Failed", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }
        /// <summary>
        /// Creates a writable BIO object stream from the specified path.
        /// </summary>
        /// <param name="fileName">The full path where the file will be created.</param>
        /// <returns>A writable BIO object that for use with write methods.</returns>
        private static BIO Write(string fileName)
        {
            if (string.IsNullOrEmpty(fileName))
            {
                throw new ArgumentNullException("fileName", "fileName is null or empty");
            }

            return(BIO.File(fileName, "w"));
        }
 public void CanLoadFromPKCS7_DER()
 {
     using (BIO bio = BIO.File(Paths.CaChainP7c, "r"))
     {
         using (X509Certificate cert = X509Certificate.FromPKCS7_DER(bio))
         {
             TestCert(cert, "CN=Root", "CN=Root", 1234);
         }
     }
 }
 public void CanLoadFromPEM()
 {
     using (BIO bio = BIO.File(Paths.CaCrt, "r"))
     {
         using (X509Certificate cert = new X509Certificate(bio))
         {
             TestCert(cert, "CN=Root", "CN=Root", 1234);
         }
     }
 }
Пример #9
0
 public static byte[] EncodeRSA(string Data)
 {
     byte[] l_res = null;
     using (RSA l_rsa = RSA.FromPrivateKey(BIO.File("D:\\Work\\Other\\ForexStars\\_incomming\\project1074.ppk", "r")))
     {
         byte[] l_bdata = StrToBytes(Data);
         l_res = l_rsa.PrivateEncrypt(l_bdata, RSA.Padding.PKCS1);
     };
     return(l_res);
 }
 public void CanLoadFromPCKS12()
 {
     using (BIO bio = BIO.File(Paths.ServerPfx, "r"))
     {
         using (X509Certificate cert = X509Certificate.FromPKCS12(bio, password))
         {
             TestCert(cert, "CN=localhost", "CN=Root", 1235);
         }
     }
 }
 public void CanCreatePKCS12()
 {
     using (BIO bio = BIO.File(Paths.ServerPfx, "r")) {
         using (var pfx = new PKCS12(bio, password)) {
             using (var new_pfx = new PKCS12(password, pfx.PrivateKey, pfx.Certificate, pfx.CACertificates)) {
                 using (BIO bout = BIO.File(Paths.ServerOutPfx, "w")) {
                     new_pfx.Write(bout);
                 }
                 TestCert(new_pfx.Certificate, "CN=localhost", "CN=Root", 1235);
             }
         }
     }
 }
Пример #12
0
        private void buttonX1_Click(object sender, EventArgs e)
        {
            try
            {
                superValidator1.SetValidator1(textBoxX1, null);
                superValidator1.SetValidator1(c, null);
                SaveFileDialog sfd = new SaveFileDialog();
                sfd.Filter = "Origisign Temp Certificate|*.ogtc";
                if (pem.Checked)
                {
                    superValidator1.SetValidator1(textBoxX1, new DevComponents.DotNetBar.Validator.RequiredFieldValidator("Password is required for Key Encryption"));
                    superValidator1.SetValidator1(c, new DevComponents.DotNetBar.Validator.RequiredFieldValidator("Cipher is required for Key Encryption"));
                }
                if (superValidator1.Validate())
                {
                    superValidator1.SetValidator1(textBoxX1, null);
                    superValidator1.SetValidator1(c, null);

                    if (sfd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                    {
                        FileName = sfd.FileName;
                        //if (pkcs7.Checked)
                        //    File.WriteAllBytes(Path.ChangeExtension(FileName, ".p7b"), certificate.Export(System.Security.Cryptography.X509Certificates.X509ContentType.Pkcs7));



                        if (cert.Checked)
                        {
                            using (var bio = BIO.File(Path.ChangeExtension(FileName, ".csr"), "w"))
                                certificate.Write(bio);
                        }
                        if (pem.Checked)
                        {
                            ComboItem ci = (ComboItem)c.SelectedItem;

                            using (var bio = BIO.File(Path.ChangeExtension(FileName, ".key"), "w"))
                                Key.WritePrivateKey(bio, (Cipher)ci.Value, textBoxX1.Text);
                        }



                        this.Close();
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBoxEx.Show(ex.Message, "Export Failed", MessageBoxButtons.OK, MessageBoxIcon.Information);
                this.Close();
            }
        }
 public void CanSaveAsDER()
 {
     using (BIO bio = BIO.File(Paths.CaDer, "r")) {
         byte[] expected = File.ReadAllBytes(Paths.CaDer);
         using (var cert = X509Certificate.FromDER(bio)) {
             byte[] der = cert.DER;
             Assert.AreEqual(expected.Length, der.Length);
             for (int i = 0; i < expected.Length; i++)
             {
                 Assert.AreEqual(expected[i], der[i]);
             }
         }
     }
 }
        public void CanGetAsPEM()
        {
            using (BIO bio = BIO.File(Paths.CaCrt, "r"))
            {
                string expected = File.ReadAllText(Paths.CaCrt).Replace("\r\n", "\n");
                using (X509Certificate cert = new X509Certificate(bio))
                {
                    string pem  = cert.PEM;
                    string text = cert.ToString();

                    Assert.AreEqual(expected, text + pem);
                }
            }
        }
Пример #15
0
        static void Main(string[] args)
        {
            X509Name issuer = new X509Name("issuer");

            X509Name subject = new X509Name("subject");
            RSA      rsa     = new RSA();

            rsa.GenerateKeys(512, 0x10021, null, null);
            CryptoKey key = new CryptoKey(rsa);

            X509Certificate cert = new X509Certificate(123, subject, issuer, key, DateTime.Now,
                                                       DateTime.Now.AddDays(200));

            File.WriteAllText(@"C:\Users\artik\Desktop\public.txt", rsa.PublicKeyAsPEM);
            File.WriteAllText(@"C:\Users\artik\Desktop\private.txt", rsa.PrivateKeyAsPEM);

            BIO bio = BIO.File("C:/temp/cert.cer", "w");

            cert.Write(bio);
        }
Пример #16
0
        private void buttonX1_Click(object sender, EventArgs e)
        {
            try
            {
                superValidator1.SetValidator1(textBoxX1, null);
                superValidator1.SetValidator1(c, null);
                if (pem.Checked)
                {
                    //       superValidator1.SetValidator1(textBoxX1, new DevComponents.DotNetBar.Validator.RequiredFieldValidator("Password is required for Key Encryption"));
                    superValidator1.SetValidator1(c, new DevComponents.DotNetBar.Validator.RequiredFieldValidator("Cipher is required for Key Encryption"));
                }
                //else if(pfx.Checked)
                //    superValidator1.SetValidator1(textBoxX1, new DevComponents.DotNetBar.Validator.RequiredFieldValidator("Password is required for Key Encryption"));

                if (superValidator1.Validate())
                {
                    superValidator1.SetValidator1(textBoxX1, null);
                    superValidator1.SetValidator1(c, null);
                    string devpath = "";
                    if (MainForm.cai != null)
                    {
                        if (MainForm.cai.DevPath != null)
                        {
                            devpath = MainForm.cai.DevPath;
                        }
                    }
                    SaveFileDialog sfd = new SaveFileDialog();
                    sfd.Filter = "Origisign Temp Certificate|*.ogtc";
                    if (devpath != "")
                    {
                        sfd.InitialDirectory = devpath;
                    }

                    if (sfd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                    {
                        FileName = sfd.FileName;
                        //if (pkcs7.Checked)
                        //    File.WriteAllBytes(Path.ChangeExtension(FileName, ".p7b"), certificate.Export(System.Security.Cryptography.X509Certificates.X509ContentType.Pkcs7));


                        if (pfx.Checked)
                        {
                            using (var bio = BIO.File(Path.ChangeExtension(FileName, ".pfx"), "w"))
                            {
                                OpenSSL.Core.Stack <X509Certificate> caStack = new OpenSSL.Core.Stack <X509Certificate>();
                                //    caStack.Add(certificate);

                                using (var pfx12 = new PKCS12(textBoxX1.Text, Key, certificate, caStack))
                                    pfx12.Write(bio);
                            }
                        }
                        if (cert.Checked)
                        {
                            using (var bio = BIO.File(Path.ChangeExtension(FileName, ".cer"), "w"))
                                certificate.Write(bio);
                        }
                        if (pem.Checked)
                        {
                            ComboItem ci = (ComboItem)c.SelectedItem;

                            using (var bio = BIO.File(Path.ChangeExtension(FileName, ".key"), "w"))
                                Key.WritePrivateKey(bio, (Cipher)ci.Value, textBoxX1.Text);
                        }



                        this.Close();
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBoxEx.Show(ex.Message, "Export Failed", MessageBoxButtons.OK, MessageBoxIcon.Information);
                this.Close();
            }
        }
Пример #17
0
 public RSAWrapper(string keyfn)
 {
     using (BIO b = BIO.File(keyfn, "r"))
         Rsa = RSA.FromPrivateKey(b);
 }
Пример #18
0
 private X509Chain LoadCACertificateChain(string caFilename)
 {
     using (BIO bio = BIO.File(caFilename, "r")) {
         return(new X509Chain(bio));
     }
 }
Пример #19
0
        public void Execute(string[] args)
        {
            try
            {
                options.ParseArguments(args);
            }
            catch (Exception)
            {
                Usage();
                return;
            }

            string infile = this.options.GetString("infile");
            BIO    bin    = Program.GetInFile(options.GetString("infile"));

            DH     dh;
            string inform = this.options["inform"] as string;

            if (inform == "PEM")
            {
                dh = DH.FromParametersPEM(bin);
            }
            else if (inform == "DER")
            {
                dh = DH.FromParametersDER(bin);
            }
            else
            {
                Usage();
                return;
            }

            if (this.options.IsSet("text"))
            {
                Console.WriteLine(dh);
            }

            if (this.options.IsSet("check"))
            {
                DH.CheckCode check = dh.Check();
                if ((check & DH.CheckCode.NotSuitableGenerator) != 0)
                {
                    Console.WriteLine("the g value is not a generator");
                }
                if ((check & DH.CheckCode.CheckP_NotPrime) != 0)
                {
                    Console.WriteLine("p value is not prime");
                }
                if ((check & DH.CheckCode.CheckP_NotSafePrime) != 0)
                {
                    Console.WriteLine("p value is not a safe prime");
                }
                if ((check & DH.CheckCode.UnableToCheckGenerator) != 0)
                {
                    Console.WriteLine("unable to check the generator value");
                }
                if (check == 0)
                {
                    Console.WriteLine("DH parameters appear to be ok");
                }
            }

            if (this.options.IsSet("code"))
            {
                Console.WriteLine("-code is currently not implemented.");
            }

            if (!this.options.IsSet("noout"))
            {
                string outfile = this.options["outfile"] as string;
                BIO    bout;
                bool   outmem = false;
                if (string.IsNullOrEmpty(outfile))
                {
                    bout   = BIO.MemoryBuffer();
                    outmem = true;
                }
                else
                {
                    bout = BIO.File(outfile, "w");
                }

                string outform = this.options["outform"] as string;
                if (outform == "DER")
                {
                    dh.WriteParametersDER(bout);
                }
                else if (outform == "PEM")
                {
                    dh.WriteParametersPEM(bout);
                }
                else
                {
                    Usage();
                    return;
                }

                if (outmem)
                {
                    Stream cout = Console.OpenStandardOutput();
                    ArraySegment <byte> segment = bout.ReadBytes((int)bout.NumberWritten);
                    cout.Write(segment.Array, segment.Offset, segment.Count);
                }
            }
        }