Ejemplo n.º 1
0
        public RsaKey GetKey(byte[] content, AsymmetricKeyType keyType)
        {
            AsymmetricKeyParameter key = CreateKey(content, keyType);
            int keyLength = GetKeyLength(key);

            return(new RsaKey(content, keyType, keyLength));
        }
Ejemplo n.º 2
0
 protected AsymmetricKey(byte[] content, AsymmetricKeyType keyType, int keyLength, CipherType cipherType)
 {
     Content    = content;
     KeyType    = keyType;
     KeySize    = keyLength;
     CipherType = cipherType;
 }
Ejemplo n.º 3
0
        public EcKey GetKey(byte[] content, AsymmetricKeyType keyType)
        {
            AsymmetricKeyParameter key = CreateKey(content, keyType);
            int keyLength = GetKeyLength(key);

            ECCurve curve     = ((ECKeyParameters)key).Parameters.Curve;
            string  curveName = curveNameMapper.MapCurveToName(curve);

            return(new EcKey(content, keyType, keyLength, curveName));
        }
Ejemplo n.º 4
0
 protected T GetKeyParameter <T>(byte[] keyContent, AsymmetricKeyType keyType) where T : AsymmetricKeyParameter
 {
     try
     {
         return((T)CreateKey(keyContent, keyType));
     }
     catch (Exception exception) when(exception is ArgumentNullException ||
                                      exception is IOException ||
                                      exception is ArgumentException ||
                                      exception is SecurityUtilityException ||
                                      exception is NullReferenceException ||
                                      exception is InvalidCastException)
     {
         throw new CryptographicException();
     }
 }
Ejemplo n.º 5
0
        protected AsymmetricKeyParameter CreateKey(byte[] content, AsymmetricKeyType keyType)
        {
            AsymmetricKeyParameter key;

            try
            {
                key = keyType == AsymmetricKeyType.Public
                          ? PublicKeyFactory.CreateKey(content)
                          : PrivateKeyFactory.CreateKey(content);
            }
            catch (ArgumentException)
            {
                throw new ArgumentException("Key type mismatch.");
            }

            return(key);
        }
        /// <summary>
        /// Creates new instance of Pkcs11X509CertificateInfo class
        /// </summary>
        /// <param name="ckaId">Value of CKA_ID attribute</param>
        /// <param name="ckaLabel">Value of CKA_LABEL attribute</param>
        /// <param name="ckaValue">Value of CKA_VALUE attribute</param>
        internal Pkcs11X509CertificateInfo(byte[] ckaId, string ckaLabel, byte[] ckaValue)
        {
            _id                = ConvertUtils.BytesToHexString(ckaId);
            _label             = ckaLabel;
            _rawData           = ckaValue ?? throw new ArgumentNullException(nameof(ckaValue));
            _parsedCertificate = new X509Certificate2(_rawData);

            if (_parsedCertificate.PublicKey.Oid.Value == "1.2.840.113549.1.1.1")
            {
                _keyType = AsymmetricKeyType.RSA;
            }
            else if (_parsedCertificate.PublicKey.Oid.Value == "1.2.840.10045.2.1")
            {
                _keyType = AsymmetricKeyType.EC;
            }
            else
            {
                _keyType = AsymmetricKeyType.Other;
            }
        }
Ejemplo n.º 7
0
        private IAsymmetricKey GetKey(byte[] keyContent, string algorithmId, AsymmetricKeyType keyType)
        {
            var cipherType = cipherTypeMapper.MapOidToCipherType(algorithmId);

            switch (cipherType)
            {
            case CipherType.Rsa:
                return(rsaKeyProvider.GetKey(keyContent, keyType));

            case CipherType.Dsa:
                return(dsaKeyProvider.GetKey(keyContent, keyType));

            case CipherType.Ec:
                return(ecKeyProvider.GetKey(keyContent, keyType));

            case CipherType.ElGamal:
                return(elGamalKeyProvider.GetKey(keyContent, keyType));

            default:
                throw new ArgumentException("Key type not supported or key is corrupted.");
            }
        }
Ejemplo n.º 8
0
 public RsaKey(byte[] content, AsymmetricKeyType keyType, int keySize) : base(content, keyType, keySize, CipherType.Rsa)
 {
 }
Ejemplo n.º 9
0
        /// <summary>
        /// Converts a public or private RSA key from a <see cref="RSAParameters" /> structure to its equivalent DER representation.
        /// </summary>
        /// <param name="key">A <see cref="RSAParameters" /> structure with the key.</param>
        /// <param name="keyType">An <see cref="AsymmetricKeyType" /> value specifying whether to create a public or a private RSA key <see cref="byte" />[].</param>
        /// <returns>
        /// A new <see cref="byte" />[] with the converted key.
        /// </returns>
        public static byte[] ConvertToDer(RSAParameters key, AsymmetricKeyType keyType)
        {
            using MemoryStream memoryStream = new MemoryStream();

            using (BinaryWriter writer = new BinaryWriter(memoryStream))
            {
                writer.Write((byte)0x30);

                using (MemoryStream innerStream = new MemoryStream())
                    using (BinaryWriter innerWriter = new BinaryWriter(innerStream))
                    {
                        if (keyType == AsymmetricKeyType.Public)
                        {
                            innerWriter.Write((byte)0x30);
                            EncodeLength(innerWriter, 13);
                            innerWriter.Write((byte)6);

                            byte[] oid = { 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x01 };
                            EncodeLength(innerWriter, oid.Length);
                            innerWriter.Write(oid);

                            innerWriter.Write((byte)5);
                            EncodeLength(innerWriter, 0);
                            innerWriter.Write((byte)3);

                            using (MemoryStream bitStringStream = new MemoryStream())
                                using (BinaryWriter bitStringWriter = new BinaryWriter(bitStringStream))
                                {
                                    bitStringWriter.Write((byte)0);
                                    bitStringWriter.Write((byte)0x30);

                                    using (MemoryStream paramsStream = new MemoryStream())
                                        using (BinaryWriter paramsWriter = new BinaryWriter(paramsStream))
                                        {
                                            EncodeIntegerBigEndian(paramsWriter, key.Modulus);
                                            EncodeIntegerBigEndian(paramsWriter, key.Exponent);

                                            EncodeLength(bitStringWriter, (int)paramsStream.Length);
                                            paramsStream.WriteTo(bitStringStream);
                                        }

                                    EncodeLength(innerWriter, (int)bitStringStream.Length);
                                    bitStringStream.WriteTo(innerStream);
                                }
                        }
                        else if (keyType == AsymmetricKeyType.Private)
                        {
                            EncodeIntegerBigEndian(innerWriter, new byte[] { 0 });
                            EncodeIntegerBigEndian(innerWriter, key.Modulus);
                            EncodeIntegerBigEndian(innerWriter, key.Exponent);
                            EncodeIntegerBigEndian(innerWriter, key.D);
                            EncodeIntegerBigEndian(innerWriter, key.P);
                            EncodeIntegerBigEndian(innerWriter, key.Q);
                            EncodeIntegerBigEndian(innerWriter, key.DP);
                            EncodeIntegerBigEndian(innerWriter, key.DQ);
                            EncodeIntegerBigEndian(innerWriter, key.InverseQ);
                        }
                        else
                        {
                            throw Throw.InvalidEnumArgument(nameof(keyType), keyType);
                        }

                        EncodeLength(writer, (int)innerStream.Length);
                        innerStream.WriteTo(memoryStream);
                    }
            }

            return(memoryStream.ToArray());
Ejemplo n.º 10
0
 public ElGamalKey(byte[] content, AsymmetricKeyType keyType, int keyLength) : base(content, keyType, keyLength, CipherType.ElGamal)
 {
 }
Ejemplo n.º 11
0
 public EcKey(byte[] content, AsymmetricKeyType keyType, int keyLength, string curve) : base(content, keyType, keyLength, CipherType.Ec)
 {
     Curve = curve;
 }