LCOV - code coverage report
Current view: top level - src/util - auth.cpp (source / functions) Hit Total Coverage
Test: report Lines: 3 48 6.2 %
Date: 2015-07-11 18:23:49 Functions: 1 7 14.3 %

          Line data    Source code
       1             : /*
       2             : Minetest
       3             : Copyright (C) 2015 est31 <MTest31@outlook.com>
       4             : 
       5             : This program is free software; you can redistribute it and/or modify
       6             : it under the terms of the GNU Lesser General Public License as published by
       7             : the Free Software Foundation; either version 2.1 of the License, or
       8             : (at your option) any later version.
       9             : 
      10             : This program is distributed in the hope that it will be useful,
      11             : but WITHOUT ANY WARRANTY; without even the implied warranty of
      12             : MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      13             : GNU Lesser General Public License for more details.
      14             : 
      15             : You should have received a copy of the GNU Lesser General Public License along
      16             : with this program; if not, write to the Free Software Foundation, Inc.,
      17             : 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
      18             : */
      19             : 
      20             : #include <algorithm>
      21             : #include <string>
      22             : #include "auth.h"
      23             : #include "base64.h"
      24             : #include "sha1.h"
      25             : #include "srp.h"
      26             : #include "string.h"
      27             : 
      28             : // Get an sha-1 hash of the player's name combined with
      29             : // the password entered. That's what the server uses as
      30             : // their password. (Exception : if the password field is
      31             : // blank, we send a blank password - this is for backwards
      32             : // compatibility with password-less players).
      33           1 : std::string translatePassword(const std::string &name,
      34             :         const std::string &password)
      35             : {
      36           1 :         if (password.length() == 0)
      37           1 :                 return "";
      38             : 
      39           0 :         std::string slt = name + password;
      40           0 :         SHA1 sha1;
      41           0 :         sha1.addBytes(slt.c_str(), slt.length());
      42           0 :         unsigned char *digest = sha1.getDigest();
      43           0 :         std::string pwd = base64_encode(digest, 20);
      44           0 :         free(digest);
      45           0 :         return pwd;
      46             : }
      47             : 
      48           0 : void getSRPVerifier(const std::string &name,
      49             :         const std::string &password, char **salt, size_t *salt_len,
      50             :         char **bytes_v, size_t *len_v)
      51             : {
      52           0 :         std::string n_name = lowercase(name);
      53           0 :         srp_create_salted_verification_key(SRP_SHA256, SRP_NG_2048,
      54           0 :                 n_name.c_str(), (const unsigned char *)password.c_str(),
      55             :                 password.size(), (unsigned char **)salt, salt_len,
      56           0 :                 (unsigned char **)bytes_v, len_v, NULL, NULL);
      57           0 : }
      58             : 
      59             : // Get a db-ready SRP verifier
      60             : // The salt param is only modifyable by this method so that you can free it
      61             : // if it was allocated. You shouldn't use it for other purposes, as you will
      62             : // need the contents of salt_len too.
      63           0 : inline static std::string getSRPVerifier(const std::string &name,
      64             :         const std::string &password, char ** salt, size_t salt_len)
      65             : {
      66           0 :         char * bytes_v = NULL;
      67             :         size_t len_v;
      68             :         getSRPVerifier(name, password, salt, &salt_len,
      69           0 :                 &bytes_v, &len_v);
      70             :         std::string ret_val = encodeSRPVerifier(std::string(bytes_v, len_v),
      71           0 :                 std::string(*salt, salt_len));
      72           0 :         free(bytes_v);
      73           0 :         return ret_val;
      74             : }
      75             : 
      76             : // Get a db-ready SRP verifier
      77           0 : std::string getSRPVerifier(const std::string &name,
      78             :         const std::string &password)
      79             : {
      80           0 :         char * salt = NULL;
      81             :         std::string ret_val = getSRPVerifier(name,
      82           0 :                 password, &salt, 0);
      83           0 :         free(salt);
      84           0 :         return ret_val;
      85             : }
      86             : 
      87             : // Get a db-ready SRP verifier
      88           0 : std::string getSRPVerifier(const std::string &name,
      89             :         const std::string &password, const std::string &salt)
      90             : {
      91             :         // The implementation won't change the salt if its set,
      92             :         // therefore we can cast.
      93           0 :         char *salt_cstr = (char *)salt.c_str();
      94             :         return getSRPVerifier(name, password,
      95           0 :                 &salt_cstr, salt.size());
      96             : }
      97             : 
      98             : // Make a SRP verifier db-ready
      99           0 : std::string encodeSRPVerifier(const std::string &verifier,
     100             :         const std::string &salt)
     101             : {
     102           0 :         std::ostringstream ret_str;
     103           0 :         ret_str << "#1#"
     104           0 :                 << base64_encode((unsigned char*) salt.c_str(), salt.size()) << "#"
     105           0 :                 << base64_encode((unsigned char*) verifier.c_str(), verifier.size());
     106           0 :         return ret_str.str();
     107             : }
     108             : 
     109           0 : bool decodeSRPVerifier(const std::string &enc_pwd,
     110             :         std::string *salt, std::string *bytes_v)
     111             : {
     112           0 :         std::vector<std::string> pwd_components = str_split(enc_pwd, '#');
     113             : 
     114           0 :         if ((pwd_components.size() != 4)
     115           0 :                         || (pwd_components[1] != "1") // 1 means srp
     116           0 :                         || !base64_is_valid(pwd_components[2])
     117           0 :                         || !base64_is_valid(pwd_components[3]))
     118           0 :                 return false;
     119             : 
     120           0 :         std::string salt_str = base64_decode(pwd_components[2]);
     121           0 :         std::string bytes_v_str = base64_decode(pwd_components[3]);
     122           0 :         *salt = salt_str;
     123           0 :         *bytes_v = bytes_v_str;
     124           0 :         return true;
     125             : 
     126             : }

Generated by: LCOV version 1.11