コード例 #1
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)
        {
            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);
        }
コード例 #2
0
        /// <summary>
        /// Get Ephemeral Public Key Signature.
        /// </summary>
        /// <param name="keyId">Key ID.</param>
        /// <param name="ephemeralKey">Ephemeral key.</param>
        /// <returns>Ephemeral Public Key Signature.</returns>
        public static byte[] GetEphemeralPublicKeyData(int keyId,
                                                       GXPublicKey ephemeralKey)
        {
            GXAsn1BitString tmp = (GXAsn1BitString)((GXAsn1Sequence)GXAsn1Converter.FromByteArray(ephemeralKey.ToEncoded()))[1];
            // Ephemeral public key client
            GXByteBuffer epk = new GXByteBuffer(tmp.Value);

            // First byte is 4 and that is not used. We can override it.
            epk.Data[0] = (byte)keyId;
            return(epk.Array());
        }
コード例 #3
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);
        }