예제 #1
0
        /// <summary>
        /// Create x509Certificate from hex string.
        /// </summary>
        /// <param name="data">Hex string.</param>
        /// <returns>x509 certificate</returns>
        public static GXx509Certificate FromHexString(string data)
        {
            GXx509Certificate cert = new GXx509Certificate();

            cert.Init(GXCommon.HexToBytes(data));
            return(cert);
        }
예제 #2
0
        /// <summary>
        /// Create x509Certificate from DER Base64 encoded string.
        /// </summary>
        /// <param name="der">Base64 DER string.</param>
        /// <returns>x509 certificate</returns>
        public static GXx509Certificate FromDer(string der)
        {
            der = der.Replace("\r\n", "");
            der = der.Replace("\n", "");
            GXx509Certificate cert = new GXx509Certificate();

            cert.Init(GXCommon.FromBase64(der));
            return(cert);
        }
예제 #3
0
        /// <summary>
        /// Ask Gurux certificate server to generate the new certificate.
        /// </summary>
        /// <param name="address">Certificate server address.</param>
        /// <param name="certifications">List of certification types and PKCS #10 certificates.</param>
        /// <returns>Generated certificate(s).</returns>
        public static GXx509Certificate[] GetCertificate(string address, List <KeyValuePair <CertificateType, GXPkcs10> > certifications)
        {
            StringBuilder usage = new StringBuilder();

            foreach (KeyValuePair <CertificateType, GXPkcs10> it in certifications)
            {
                if (usage.Length != 0)
                {
                    usage.Append(", ");
                }
                usage.Append("{\"KeyUsage\":");
                switch (it.Key)
                {
                case CertificateType.DigitalSignature:
                    usage.Append(Convert.ToString((int)KeyUsage.DigitalSignature));
                    break;

                case CertificateType.KeyAgreement:
                    usage.Append(Convert.ToString((int)KeyUsage.KeyAgreement));
                    break;

                default:
                    throw new Exception("Invalid type.");
                }
                usage.Append(", \"CSR\":\"");
                usage.Append(it.Value.ToDer());
                usage.Append("\"}");
            }
            HttpWebRequest request = HttpWebRequest.Create(address) as HttpWebRequest;
            string         der     = "{\"Certificates\":[" + usage.ToString() + "]}";

            request.ContentType = "application/json";
            request.Method      = "POST";
            using (var streamWriter = new StreamWriter(request.GetRequestStream()))
            {
                streamWriter.Write(der);
                streamWriter.Flush();
                streamWriter.Close();
            }
            try
            {
                using (HttpWebResponse webresponse = request.GetResponse() as HttpWebResponse)
                {
                    using (StreamReader reader = new StreamReader(webresponse.GetResponseStream()))
                    {
                        string str = reader.ReadToEnd();
                        int    pos = str.IndexOf("[");
                        if (pos == -1)
                        {
                            throw new Exception("Certificates are missing.");
                        }
                        str = str.Substring(pos + 2);
                        pos = str.IndexOf("]");
                        if (pos == -1)
                        {
                            throw new Exception("Certificates are missing.");
                        }
                        str = str.Substring(0, pos - 1);
                        List <GXx509Certificate> list = new List <GXx509Certificate>();
                        string[] tmp = str.Split(new string[] { "\"", "," }, StringSplitOptions.RemoveEmptyEntries);
                        pos = 0;
                        foreach (string it in tmp)
                        {
                            GXx509Certificate x509 = GXx509Certificate.FromDer(it);
                            if (!GXCommon.Compare(certifications[pos].Value.PublicKey.RawValue, x509.PublicKey.RawValue))
                            {
                                throw new Exception("Create certificate signingRequest generated wrong public key.");
                            }
                            ++pos;
                            list.Add(x509);
                        }
                        return(list.ToArray());
                    }
                }
            }
            catch (WebException ex)
            {
                throw new Exception(new StreamReader(ex.Response.GetResponseStream()).ReadToEnd());
            }
        }
예제 #4
0
 /// <summary>
 /// Load x509 certificate from the PEM file.
 /// </summary>
 /// <param name="path">File path. </param>
 /// <returns> Created GXx509Certificate object. </returns>
 public static GXx509Certificate Load(string path)
 {
     return(GXx509Certificate.FromPem(File.ReadAllText(path)));
 }