/// <summary>
        /// Create the private key from DER.
        /// </summary>
        /// <param name="key">DER Base64 coded string.</param>
        /// <returns></returns>
        public static GXPrivateKey FromDer(string der)
        {
            byte[]       key   = GXCommon.FromBase64(der);
            object[]     tmp   = (object[])GXAsn1Converter.FromByteArray(key);
            GXPrivateKey value = new GXPrivateKey();

            //If private key is given
            if (key.Length == 32)
            {
                value.Scheme   = Ecc.P256;
                value.RawValue = key;
            }
            else if (key.Length == 48)
            {
                value.Scheme   = Ecc.P384;
                value.RawValue = key;
            }
            else if (key.Length == 65)
            {
                value.Scheme   = Ecc.P256;
                value.RawValue = key;
            }
            else if (key.Length == 97)
            {
                value.Scheme   = Ecc.P384;
                value.RawValue = key;
            }
            else
            {
                throw new ArgumentOutOfRangeException("Invalid key.");
            }
            return(value);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Create PKCS #8 from DER Base64 encoded string.
        /// </summary>
        /// <param name="der">Base64 DER string.</param>
        /// <returns></returns>
        public static GXPkcs8 FromDer(string der)
        {
            der = der.Replace("\r\n", "");
            der = der.Replace("\n", "");
            GXPkcs8 cert = new GXPkcs8();

            cert.Init(GXCommon.FromBase64(der));
            return(cert);
        }
Ejemplo n.º 3
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);
        }
        /// <summary>
        /// Constructor.
        /// </summary>
        /// <param name="data">Base64 string. </param>
        public GXPkcs8(string data)
        {
            string tmp = data.Replace("-----BEGIN PRIVATE KEY-----", "");

            tmp = tmp.Replace("-----END PRIVATE KEY-----", "");
            tmp = tmp.Replace("-----BEGIN EC PRIVATE KEY-----", "");
            tmp = tmp.Replace("-----END EC PRIVATE KEY-----", "");
            Init(GXCommon.FromBase64(tmp.Trim()));
        }
        ///
        /// <summary>
        /// Constructor.
        /// </summary>
        /// <param name="data">Base64 string. </param>
        public GXx509Certificate(string data)
        {
            string tmp = data.Replace("-----BEGIN EC CERTIFICATE-----", "");

            tmp = tmp.Replace("-----END EC CERTIFICATE-----", "");
            tmp = data.Replace("-----BEGIN CERTIFICATE-----", "");
            tmp = tmp.Replace("-----END CERTIFICATE-----", "");
            Init(GXCommon.FromBase64(tmp.Trim()));
        }
Ejemplo n.º 6
0
 /// <summary>
 /// Convert ASN1 PDU bytes to XML.
 /// </summary>
 /// <param name="value">Bytes to convert. </param>
 /// <returns> Converted XML. </returns>
 public static string PduToXml(string value)
 {
     if (value == null || value.Length == 0)
     {
         return("");
     }
     if (!GXCommon.IsHexString(value))
     {
         return(PduToXml(GXCommon.FromBase64(value)));
     }
     return(PduToXml(new GXByteBuffer(GXCommon.HexToBytes(value))));
 }
Ejemplo n.º 7
0
        /// <summary>
        /// Create the private key from DER.
        /// </summary>
        /// <param name="key">DER Base64 coded string.</param>
        /// <returns></returns>
        public static GXPrivateKey FromDer(string der)
        {
            der = der.Replace("\r\n", "");
            der = der.Replace("\n", "");
            byte[]         key = GXCommon.FromBase64(der);
            GXAsn1Sequence seq = (GXAsn1Sequence)GXAsn1Converter.FromByteArray(key);

            if ((sbyte)seq[0] > 3)
            {
                throw new ArgumentOutOfRangeException("Invalid private key version.");
            }
            List <object>      tmp   = (List <object>)seq[2];
            GXPrivateKey       value = new GXPrivateKey();
            X9ObjectIdentifier id    = X9ObjectIdentifierConverter.FromString(tmp[0].ToString());

            switch (id)
            {
            case X9ObjectIdentifier.Prime256v1:
                value.Scheme = Ecc.P256;
                break;

            case X9ObjectIdentifier.Secp384r1:
                value.Scheme = Ecc.P384;
                break;

            default:
                if (id == X9ObjectIdentifier.None)
                {
                    throw new ArgumentOutOfRangeException("Invalid private key " + tmp[0].ToString() + ".");
                }
                else
                {
                    throw new ArgumentOutOfRangeException("Invalid private key " + id + " " + tmp[0].ToString() + ".");
                }
            }
            value.RawValue = (byte[])seq[1];
            if (seq[3] is byte[])
            {
                value.publicKey = GXPublicKey.FromRawBytes((byte[])seq[3]);
            }
            else
            {
                //Open SSL PEM.
                value.publicKey = GXPublicKey.FromRawBytes(((GXAsn1BitString)((List <object>)seq[3])[0]).Value);
            }
            return(value);
        }
Ejemplo n.º 8
0
        public GXPkcs8(string data)
        {
            const string START = "PRIVATE KEY-----\n";
            const string END   = "-----END";

            data = data.Replace("\r\n", "\n");
            int start = data.IndexOf(START);

            if (start == -1)
            {
                throw new ArgumentException("Invalid PEM file.");
            }
            data = data.Substring(start + START.Length);
            int end = data.IndexOf(END);

            if (end == -1)
            {
                throw new ArgumentException("Invalid PEM file.");
            }
            Init(GXCommon.FromBase64(data.Substring(0, end)));
        }