]> granicus.if.org Git - icinga2/commitdiff
Move new password functions into tlsutility
authorJean Flach <jean-marcel.flach@icinga.com>
Fri, 22 Dec 2017 11:14:31 +0000 (12:14 +0100)
committerJean Flach <jean-marcel.flach@icinga.com>
Fri, 23 Feb 2018 12:06:22 +0000 (13:06 +0100)
lib/base/tlsutility.cpp
lib/base/tlsutility.hpp
lib/cli/apiusercommand.cpp
lib/remote/apiuser.cpp
lib/remote/apiuser.hpp
lib/remote/httpserverconnection.cpp
test/remote-user.cpp

index f8bc136ade93fafe4c79c9a488fb463b4cf46f47..04d8ea49f1fae9aba1988784a9ce893cf0ff777d 100644 (file)
@@ -761,4 +761,28 @@ bool VerifyCertificate(const boost::shared_ptr<X509>& caCertificate, const boost
        return rc == 1;
 }
 
+bool ComparePassword(const String hash, const String password, const String salt)
+{
+       String otherHash = HashPassword(password, salt);
+
+       const char *p1 = otherHash.CStr();
+       const char *p2 = hash.CStr();
+
+       volatile char c = 0;
+
+       for (size_t i=0; i<64; ++i)
+               c |= p1[i] ^ p2[i];
+
+       return (c == 0);
+}
+
+String HashPassword(const String& password, const String& salt, const bool shadow)
+{
+       if (shadow)
+               //Using /etc/shadow password format. The 5 means SHA256 is being used
+               return String("$5$" + salt + "$" + PBKDF2_SHA256(password, salt, 1000));
+       else
+               return PBKDF2_SHA256(password, salt, 1000);
+}
+
 }
index 2ab230f6c36ff6db39eef1698b54a9ea165e8e99..96bdc63b0f70730026569f8044c0b8da625822ba 100644 (file)
@@ -57,6 +57,8 @@ String I2_BASE_API SHA1(const String& s, bool binary = false);
 String I2_BASE_API SHA256(const String& s);
 String I2_BASE_API RandomString(int length);
 bool I2_BASE_API VerifyCertificate(const boost::shared_ptr<X509>& caCertificate, const boost::shared_ptr<X509>& certificate);
+bool I2_BASE_API ComparePassword(const String hash, const String password, const String Salt);
+String I2_BASE_API HashPassword(const String& password, const String& salt, const bool shadow = false);
 
 class I2_BASE_API openssl_error : virtual public std::exception, virtual public boost::exception { };
 
index 9d43e120fa3d8b3434bcbc5a49ea17c2a373dda7..1cd5b4858e6dc67f818f00eec9abfa3969ca1c7b 100644 (file)
@@ -68,7 +68,7 @@ int ApiUserCommand::Run(const boost::program_options::variables_map& vm, const s
        String passwd = vm["passwd"].as<std::string>();
        String salt = vm.count("salt") ? String(vm["salt"].as<std::string>()) : RandomString(8);
 
-       String hashedPassword = ApiUser::CreateHashedPasswordString(passwd, salt, true);
+       String hashedPassword = HashPassword(passwd, salt, true);
 
        std::cout
                << "object ApiUser \"" << user << "\" {\n"
index fb847f9bb9a0cb3f696151742ded1b6013414e0c..0caac19ff396781e9f9665727c5847b8b1e0216d 100644 (file)
@@ -32,7 +32,7 @@ void ApiUser::OnConfigLoaded(void)
        ObjectImpl<ApiUser>::OnConfigLoaded();
 
        if (this->GetPasswordHash().IsEmpty())
-               SetPasswordHash(CreateHashedPasswordString(GetPassword(), RandomString(8), true));
+               SetPasswordHash(HashPassword(GetPassword(), RandomString(8), true));
 }
 
 ApiUser::Ptr ApiUser::GetByClientCN(const String& cn)
@@ -65,31 +65,17 @@ ApiUser::Ptr ApiUser::GetByAuthHeader(const String& auth_header)
        const ApiUser::Ptr& user = ApiUser::GetByName(username);
 
        /* Deny authentication if 1) given password is empty 2) configured password does not match. */
-       if (password.IsEmpty())
-               return nullptr;
-       else if (user && user->GetPassword() != password)
+       if (!user || password.IsEmpty())
                return nullptr;
+       else {
+               Dictionary::Ptr passwordDict = user->GetPasswordDict();
+               if (!ComparePassword(passwordDict->Get("password"), password, passwordDict->Get("salt")))
+                       return nullptr;
+       }
 
        return user;
 }
 
-bool ApiUser::ComparePassword(String password) const
-{
-       Dictionary::Ptr passwordDict = this->GetPasswordDict();
-       String thisPassword = passwordDict->Get("password");
-       String otherPassword = CreateHashedPasswordString(password, passwordDict->Get("salt"), false);
-
-       const char *p1 = otherPassword.CStr();
-       const char *p2 = thisPassword.CStr();
-
-       volatile char c = 0;
-
-       for (size_t i=0; i<64; ++i)
-               c |= p1[i] ^ p2[i];
-
-       return (c == 0);
-}
-
 Dictionary::Ptr ApiUser::GetPasswordDict(void) const
 {
        String password = this->GetPasswordHash();
@@ -109,13 +95,3 @@ Dictionary::Ptr ApiUser::GetPasswordDict(void) const
 
        return passwordDict;
 }
-
-String ApiUser::CreateHashedPasswordString(const String& password, const String& salt, const bool shadow)
-{
-       if (shadow)
-               //Using /etc/shadow password format. The 5 means SHA256 is being used
-               return String("$5$" + salt + "$" + PBKDF2_SHA256(password, salt, 1000));
-       else
-               return PBKDF2_SHA256(password, salt, 1000);
-
-}
index 09ee1a4d04ce5654d26b3682909116c71fe349b9..5aacbae0b96c931f2cf0ec7454abb0cdf365d565 100644 (file)
@@ -39,10 +39,8 @@ public:
 
        static ApiUser::Ptr GetByClientCN(const String& cn);
        static ApiUser::Ptr GetByAuthHeader(const String& auth_header);
-       static String CreateHashedPasswordString(const String& password, const String& salt, const bool shadow = false);
 
        Dictionary::Ptr GetPasswordDict(void) const;
-       bool ComparePassword(String password) const;
 };
 
 }
index 57f44f22bf749a1d7f81790f9385ff7f06a6baaf..8a14b00e1edff5d089e37fb4927b0712a951eece 100644 (file)
@@ -30,6 +30,7 @@
 #include "base/logger.hpp"
 #include "base/objectlock.hpp"
 #include "base/timer.hpp"
+#include "base/tlsutility.hpp"
 #include "base/utility.hpp"
 #include <boost/thread/once.hpp>
 
index eafa0e72153e989fc6ae7d987517502506bf2230..1c327bacbe664fcfda890c5d1a97d38eb54b03b1 100644 (file)
@@ -36,7 +36,7 @@ BOOST_AUTO_TEST_CASE(password)
        String passwd = RandomString(16);
        String salt = RandomString(8);
        user->SetPassword("ThisShouldBeIgnored");
-       user->SetPasswordHash(ApiUser::CreateHashedPasswordString(passwd, salt, true));
+       user->SetPasswordHash(HashPassword(passwd, salt, true));
 
        BOOST_CHECK(user->GetPasswordHash() != passwd);
 
@@ -44,8 +44,8 @@ BOOST_AUTO_TEST_CASE(password)
 
        BOOST_CHECK(passwdd);
        BOOST_CHECK(passwdd->Get("salt") == salt);
-       BOOST_CHECK(user->ComparePassword(passwd));
-       BOOST_CHECK(!user->ComparePassword("wrong password uwu!"));
+       BOOST_CHECK(ComparePassword(passwdd->Get("password"), passwd, salt));
+       BOOST_CHECK(!ComparePassword(passwdd->Get("password"), "wrong password uwu!", salt));
 #endif
 }