/// <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);
                }
            }
Ejemplo n.º 2
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>
        /// 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));
                }
            }