/// <summary>
        /// ReadKey method implementation
        /// </summary>
        public string ReadKey(string upn)
        {
            if (string.IsNullOrEmpty(upn))
            {
                return(null);
            }
            string lupn = upn.ToLower();

            return(KeysStorage.GetUserKey(lupn));
        }
        /// <summary>
        /// RemoveKey method implementation
        /// </summary>
        public bool RemoveKey(string upn)
        {
            if (string.IsNullOrEmpty(upn))
            {
                return(false);
            }
            string lupn = upn.ToLower();

            return(KeysStorage.RemoveUserKey(lupn));
        }
            /// <summary>
            /// ValidateKey method implmentation
            /// </summary>
            public override bool ValidateKey(string upn)
            {
                if (string.IsNullOrEmpty(upn))
                {
                    return(false);
                }
                string lupn = upn.ToLower();
                string key  = ReadKey(lupn);

                if (string.IsNullOrEmpty(key))
                {
                    return(false);
                }
                if (HasStorageInfos(key))
                {
                    using (var prov = new RSAEncryption(_xorsecret))
                    {
                        byte[] crypted = StripStorageInfos(key);
                        if (crypted == null)
                        {
                            return(false);
                        }

                        prov.Certificate = KeysStorage.GetUserCertificate(lupn, true);
                        byte[] cleared = prov.Decrypt(crypted, lupn);

                        if (cleared == null)
                        {
                            return(false); // Key corrupted
                        }
                        if (prov.CheckSum == null)
                        {
                            return(false); // Key corrupted
                        }
                        if (prov.CheckSum.SequenceEqual(CheckSumEncoding.CheckSum(lupn)))
                        {
                            return(true);  // OK RSA
                        }
                        else
                        {
                            return(false); // Key corrupted
                        }
                    }
                }
                else
                {
                    return(false);
                }
            }
Exemplo n.º 4
0
            /// <summary>
            /// ValidateKeyV1 method implmentation
            /// </summary>
            public override bool ValidateKey(string upn)
            {
                if (string.IsNullOrEmpty(upn))
                {
                    return(false);
                }
                string lupn = upn.ToLower();
                string key  = ReadKey(lupn);

                if (HasKeyPrefix(key))
                {
                    using (var prov = new Encryption(_xorsecret))
                    {
                        string xkey    = StripKeyPrefix(key);
                        byte[] crypted = System.Convert.FromBase64CharArray(xkey.ToCharArray(), 0, xkey.Length);
                        if (crypted == null)
                        {
                            return(false);
                        }

                        prov.Certificate = KeysStorage.GetUserCertificate(lupn, false);
                        byte[] cleared = prov.Decrypt(crypted, lupn);

                        if (cleared == null)
                        {
                            return(false); // Key corrupted
                        }
                        if (prov.CheckSum == null)
                        {
                            return(false); // Key corrupted
                        }
                        if (prov.CheckSum.SequenceEqual(Utilities.CheckSum(lupn)))
                        {
                            return(true);  // OK RSA
                        }
                        else
                        {
                            return(false); // Key corrupted
                        }
                    }
                }
                else
                {
                    return(false);
                }
            }
        /// <summary>
        /// NewKey method implementation
        /// </summary>
        public string NewKey(string upn)
        {
            if (string.IsNullOrEmpty(upn))
            {
                return(null);
            }
            string lupn    = upn.ToLower();
            string strcert = string.Empty;

            if (_cryptoRSADataProvider == null)
            {
                _cryptoRSADataProvider = new Encryption();
            }
            _cryptoRSADataProvider.Certificate = CreateCertificate(lupn, _validity, out strcert);
            string crypted = AddKeyPrefix(_cryptoRSADataProvider.Encrypt(lupn));

            return(KeysStorage.NewUserKey(lupn, crypted, strcert));
        }
Exemplo n.º 6
0
            /// <summary>
            /// NewKey method implementation
            /// </summary>
            public override string NewKey(string upn)
            {
                if (string.IsNullOrEmpty(upn))
                {
                    return(null);
                }
                string lupn = upn.ToLower();
                RandomNumberGenerator cryptoRandomDataGenerator = new RNGCryptoServiceProvider();

                byte[] buffer  = null;
                string crypted = string.Empty;

                switch (_mode)
                {
                case KeyGeneratorMode.ClientSecret128:
                    buffer = new byte[16];
                    cryptoRandomDataGenerator.GetBytes(buffer);
                    break;

                case KeyGeneratorMode.ClientSecret256:
                    buffer = new byte[32];
                    cryptoRandomDataGenerator.GetBytes(buffer);
                    break;

                case KeyGeneratorMode.ClientSecret384:
                    buffer = new byte[48];
                    cryptoRandomDataGenerator.GetBytes(buffer);
                    break;

                case KeyGeneratorMode.ClientSecret512:
                    buffer = new byte[64];
                    cryptoRandomDataGenerator.GetBytes(buffer);
                    break;

                default:
                    buffer = Guid.NewGuid().ToByteArray();
                    cryptoRandomDataGenerator.GetBytes(buffer);
                    break;
                }
                crypted = AddKeyPrefix(Convert.ToBase64String(buffer));
                KeysStorage.NewUserKey(lupn, crypted);
                return(crypted);
            }
        /// <summary>
        /// ValidateKey method implmentation
        /// </summary>
        public bool ValidateKey(string upn)
        {
            if (string.IsNullOrEmpty(upn))
            {
                return(false);
            }
            string lupn = upn.ToLower();

            if (!KeysStorage.HasStoredCertificate(upn))
            {
                return(false);
            }
            string key = ReadKey(lupn);

            if (HasKeyPrefix(key))
            {
                if (_cryptoRSADataProvider == null)
                {
                    _cryptoRSADataProvider = new Encryption();
                }

                key = StripKeyPrefix(key);
                _cryptoRSADataProvider.Certificate = KeysStorage.GetUserCertificate(lupn);
                string user = _cryptoRSADataProvider.Decrypt(key);
                if (string.IsNullOrEmpty(user))
                {
                    return(false);  // Key corrupted
                }
                if (user.ToLower().Equals(lupn))
                {
                    return(true);   // OK
                }
                else
                {
                    return(false);  // Key corrupted
                }
            }
            else
            {
                return(false);
            }
        }
            /// <summary>
            /// ProbeKey method implmentation
            /// </summary>
            public override byte[] ProbeKey(string upn)
            {
                if (string.IsNullOrEmpty(upn))
                {
                    return(null);
                }
                string lupn = upn.ToLower();
                string key  = ReadKey(lupn);

                if (string.IsNullOrEmpty(key))
                {
                    return(null);
                }

                byte[] probed = null;
                using (var prov = new RSAEncryption(_xorsecret))
                {
                    byte[] crypted = StripStorageInfos(key);
                    if (crypted == null)
                    {
                        return(null);
                    }

                    prov.Certificate = KeysStorage.GetUserCertificate(lupn, true);
                    probed           = prov.Decrypt(crypted, lupn);
                    if (probed == null)
                    {
                        return(null);
                    }
                }
                if (probed.Length > MAX_PROBE_LEN)
                {
                    byte[] buffer = new byte[MAX_PROBE_LEN];
                    Buffer.BlockCopy(probed, 0, buffer, 0, MAX_PROBE_LEN);
                    return(buffer);
                }
                else
                {
                    return(probed);
                }
            }
            /// <summary>
            /// EncodedKey method implementation
            /// </summary>
            public override string EncodedKey(string upn)
            {
                if (string.IsNullOrEmpty(upn))
                {
                    return(null);
                }
                string lupn = upn.ToLower();
                string key  = ReadKey(lupn);

                if (string.IsNullOrEmpty(key))
                {
                    return(null);
                }

                byte[] cleared = null;
                using (var prov = new RSAEncryption(XORSecret))
                {
                    byte[] crypted = StripStorageInfos(key);
                    if (crypted == null)
                    {
                        return(null);
                    }
                    string pass = CheckSumEncoding.CheckSumAsString(lupn);
                    prov.Certificate = KeysStorage.GetUserCertificate(lupn, pass);
                    cleared          = prov.GetDecryptedKey(crypted, lupn);
                    if (cleared == null)
                    {
                        return(null);
                    }
                }
                if (cleared.Length > MAX_PROBE_LEN)
                {
                    byte[] buffer = new byte[MAX_PROBE_LEN];
                    Buffer.BlockCopy(cleared, 0, buffer, 0, MAX_PROBE_LEN);
                    return(Base32.Encode(buffer));
                }
                else
                {
                    return(Base32.Encode(cleared));
                }
            }
            /// <summary>
            /// NewKey method implementation
            /// </summary>
            public override string NewKey(string upn)
            {
                if (string.IsNullOrEmpty(upn))
                {
                    return(null);
                }
                string lupn = upn.ToLower();

                byte[] crypted = null;
                using (var prov = new RSAEncryption(_xorsecret))
                {
                    prov.Certificate = KeysStorage.CreateCertificate(lupn, _validity, true);
                    crypted          = prov.Encrypt(lupn);
                    if (crypted == null)
                    {
                        return(null);
                    }
                    string outkey = AddStorageInfos(crypted);
                    return(KeysStorage.NewUserKey(lupn, outkey, prov.Certificate));
                }
            }
Exemplo n.º 11
0
            /// <summary>
            /// NewKey method implementation
            /// </summary>
            public override string NewKey(string upn)
            {
                if (string.IsNullOrEmpty(upn))
                {
                    return(null);
                }
                string lupn = upn.ToLower();

                byte[] crypted = null;
                using (var prov = new RNGEncryption(_xorsecret, _mode))
                {
                    crypted = prov.Encrypt(lupn);
                    if (crypted == null)
                    {
                        return(null);
                    }
                }
                string outkey = AddStorageInfos(crypted);

                return(KeysStorage.NewUserKey(lupn, outkey));
            }
            /// <summary>
            /// NewKey method implementation
            /// </summary>
            public override string NewKey(string upn)
            {
                if (string.IsNullOrEmpty(upn))
                {
                    return(null);
                }
                string lupn = upn.ToLower();

                byte[] crypted = null;
                using (var prov = new RSAEncryption(_xorsecret, _certificatethumbprint))
                {
                    crypted = prov.Encrypt(lupn);
                    if (crypted == null)
                    {
                        return(null);
                    }
                }
                string outkey = AddKeyPrefix(System.Convert.ToBase64String(crypted));

                return(KeysStorage.NewUserKey(lupn, outkey));
            }
            /// <summary>
            /// NewKey method implementation
            /// </summary>
            public override string NewKey(string upn)
            {
                if (string.IsNullOrEmpty(upn))
                {
                    return(null);
                }
                string lupn = upn.ToLower();

                byte[] crypted = null;
                using (var prov = new RSAEncryption(XORSecret, CertificateThumbprint))
                {
                    crypted = prov.NewEncryptedKey(lupn);
                    if (crypted == null)
                    {
                        return(null);
                    }
                }
                string outkey = AddStorageInfos(crypted);

                return(KeysStorage.NewUserKey(lupn, outkey));
            }
        /// <summary>
        /// NewKey method implementation
        /// </summary>
        public string NewKey(string upn)
        {
            if (string.IsNullOrEmpty(upn))
            {
                return(null);
            }
            string lupn = upn.ToLower();

            byte[] crypted = null;
            using (var prov = new AES256CNGEncryption(XORSecret))
            {
                crypted = prov.NewEncryptedKey(lupn);
                if (crypted == null)
                {
                    return(null);
                }
            }
            string outkey = AddStorageInfos(crypted);

            return(KeysStorage.NewUserKey(lupn, outkey));
        }
            /// <summary>
            /// NewKey method implementation
            /// </summary>
            public override string NewKey(string upn)
            {
                if (string.IsNullOrEmpty(upn))
                {
                    return(null);
                }
                string lupn = upn.ToLower();

                byte[] crypted = null;
                using (var prov = new Encryption(_xorsecret))
                {
                    prov.Certificate = KeysStorage.CreateCertificate(lupn, _validity, false);
                    crypted          = prov.Encrypt(lupn);
                    if (crypted == null)
                    {
                        return(null);
                    }
                    string outkey = AddKeyPrefix(System.Convert.ToBase64String(crypted));
                    return(KeysStorage.NewUserKey(lupn, outkey, prov.Certificate));
                }
            }
            /// <summary>
            /// NewKey method implementation
            /// </summary>
            public override string NewKey(string upn)
            {
                if (string.IsNullOrEmpty(upn))
                {
                    return(null);
                }
                string lupn = upn.ToLower();

                byte[] crypted = null;
                using (var prov = new RSAEncryption(XORSecret))
                {
                    string pass = CheckSumEncoding.CheckSumAsString(lupn);
                    prov.Certificate = KeysStorage.CreateCertificate(lupn, pass, Validity);
                    crypted          = prov.NewEncryptedKey(lupn);
                    if (crypted == null)
                    {
                        return(null);
                    }
                    string outkey = AddStorageInfos(crypted);
                    return(KeysStorage.NewUserKey(lupn, outkey, prov.Certificate));
                }
            }