aboutsummaryrefslogtreecommitdiff
path: root/src/util/auth.cpp
blob: 912987259623c9f4c35b887b7a5684565b35bec6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
/*
Minetest
Copyright (C) 2015, 2016 est31 <MTest31@outlook.com>

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2.1 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/

#include <algorithm>
#include <string>
#include "auth.h"
#include "base64.h"
#include "sha1.h"
#include "srp.h"
#include "string.h"
#include "debug.h"

// Get an sha-1 hash of the player's name combined with
// the password entered. That's what the server uses as
// their password. (Exception : if the password field is
// blank, we send a blank password - this is for backwards
// compatibility with password-less players).
std::string translate_password(const std::string &name,
	const std::string &password)
{
	if (password.length() == 0)
		return "";

	std::string slt = name + password;
	SHA1 sha1;
	sha1.addBytes(slt.c_str(), slt.length());
	unsigned char *digest = sha1.getDigest();
	std::string pwd = base64_encode(digest, 20);
	free(digest);
	return pwd;
}

// Call lower level SRP code to generate a verifier with the
// given pointers. Contains the preparations, call parameters
// and error checking common to all srp verifier generation code.
// See docs of srp_create_salted_verification_key for more info.
static inline void gen_srp_v(const std::string &name,
	const std::string &password, char **salt, size_t *salt_len,
	char **bytes_v, size_t *len_v)
{
	std::string n_name = lowercase(name);
	SRP_Result res = srp_create_salted_verification_key(SRP_SHA256, SRP_NG_2048,
		n_name.c_str(), (const unsigned char *)password.c_str(),
		password.size(), (unsigned char **)salt, salt_len,
		(unsigned char **)bytes_v, len_v, NULL, NULL);
	FATAL_ERROR_IF(res != SRP_OK, "Couldn't create salted SRP verifier");
}

/// Creates a verification key with given salt and password.
std::string generate_srp_verifier(const std::string &name,
	const std::string &password, const std::string &salt)
{
	size_t salt_len = salt.size();
	// The API promises us that the salt doesn't
	// get modified if &salt_ptr isn't NULL.
	char *salt_ptr = (char *)salt.c_str();

	char *bytes_v = NULL;
	size_t verifier_len = 0;
	gen_srp_v(name, password, &salt_ptr, &salt_len, &bytes_v, &verifier_len);
	std::string verifier = std::string(bytes_v, verifier_len);
	free(bytes_v);
	return verifier;
}

/// Creates a verification key and salt with given password.
void generate_srp_verifier_and_salt(const std::string &name,
	const std::string &password, std::string *verifier,
	std::string *salt)
{
	char *bytes_v = NULL;
	size_t verifier_len;
	char *salt_ptr = NULL;
	size_t salt_len;
	gen_srp_v(name, password, &salt_ptr, &salt_len, &bytes_v, &verifier_len);
	*verifier = std::string(bytes_v, verifier_len);
	*salt = std::string(salt_ptr, salt_len);
	free(bytes_v);
	free(salt_ptr);
}

/// Gets an SRP verifier, generating a salt,
/// and encodes it as DB-ready string.
std::string get_encoded_srp_verifier(const std::string &name,
	const std::string &password)
{
	std::string verifier;
	std::string salt;
	generate_srp_verifier_and_salt(name, password, &verifier, &salt);
	return encode_srp_verifier(verifier, salt);
}

/// Converts the passed SRP verifier into a DB-ready format.
std::string encode_srp_verifier(const std::string &verifier,
	const std::string &salt)
{
	std::ostringstream ret_str;
	ret_str << "#1#"
		<< base64_encode((unsigned char *)salt.c_str(), salt.size()) << "#"
		<< base64_encode((unsigned char *)verifier.c_str(), verifier.size());
	return ret_str.str();
}

/// Reads the DB-formatted SRP verifier and gets the verifier
/// and salt components.
bool decode_srp_verifier_and_salt(const std::string &encoded,
	std::string *verifier, std::string *salt)
{
	std::vector<std::string> components = str_split(encoded, '#');

	if ((components.size() != 4)
			|| (components[1] != "1") // 1 means srp
			|| !base64_is_valid(components[2])
			|| !base64_is_valid(components[3]))
		return false;

	*salt = base64_decode(components[2]);
	*verifier = base64_decode(components[3]);
	return true;

}
ppc">#endif #include "serverenvironment.h" #include "server/serveractiveobject.h" #include "util/timetaker.h" #include "profiler.h" #ifdef __FAST_MATH__ #warning "-ffast-math is known to cause bugs in collision code, do not use!" #endif struct NearbyCollisionInfo { // node NearbyCollisionInfo(bool is_ul, int bouncy, const v3s16 &pos, const aabb3f &box) : is_unloaded(is_ul), obj(nullptr), bouncy(bouncy), position(pos), box(box) {} // object NearbyCollisionInfo(ActiveObject *obj, int bouncy, const aabb3f &box) : is_unloaded(false), obj(obj), bouncy(bouncy), box(box) {} inline bool isObject() const { return obj != nullptr; } bool is_unloaded; bool is_step_up = false; ActiveObject *obj; int bouncy; v3s16 position; aabb3f box; }; // Helper functions: // Truncate floating point numbers to specified number of decimal places // in order to move all the floating point error to one side of the correct value static inline f32 truncate(const f32 val, const f32 factor) { return truncf(val * factor) / factor; } static inline v3f truncate(const v3f& vec, const f32 factor) { return v3f( truncate(vec.X, factor), truncate(vec.Y, factor), truncate(vec.Z, factor) ); } // Helper function: // Checks for collision of a moving aabbox with a static aabbox // Returns -1 if no collision, 0 if X collision, 1 if Y collision, 2 if Z collision // The time after which the collision occurs is stored in dtime. CollisionAxis axisAlignedCollision( const aabb3f &staticbox, const aabb3f &movingbox, const v3f &speed, f32 *dtime) { //TimeTaker tt("axisAlignedCollision"); aabb3f relbox( (movingbox.MaxEdge.X - movingbox.MinEdge.X) + (staticbox.MaxEdge.X - staticbox.MinEdge.X), // sum of the widths (movingbox.MaxEdge.Y - movingbox.MinEdge.Y) + (staticbox.MaxEdge.Y - staticbox.MinEdge.Y), (movingbox.MaxEdge.Z - movingbox.MinEdge.Z) + (staticbox.MaxEdge.Z - staticbox.MinEdge.Z), std::max(movingbox.MaxEdge.X, staticbox.MaxEdge.X) - std::min(movingbox.MinEdge.X, staticbox.MinEdge.X), //outer bounding 'box' dimensions std::max(movingbox.MaxEdge.Y, staticbox.MaxEdge.Y) - std::min(movingbox.MinEdge.Y, staticbox.MinEdge.Y), std::max(movingbox.MaxEdge.Z, staticbox.MaxEdge.Z) - std::min(movingbox.MinEdge.Z, staticbox.MinEdge.Z) ); const f32 dtime_max = *dtime; f32 inner_margin; // the distance of clipping recovery f32 distance; f32 time; if (speed.Y) { distance = relbox.MaxEdge.Y - relbox.MinEdge.Y; *dtime = distance / std::abs(speed.Y); time = std::max(*dtime, 0.0f); if (*dtime <= dtime_max) { inner_margin = std::max(-0.5f * (staticbox.MaxEdge.Y - staticbox.MinEdge.Y), -2.0f); if ((speed.Y > 0 && staticbox.MinEdge.Y - movingbox.MaxEdge.Y > inner_margin) || (speed.Y < 0 && movingbox.MinEdge.Y - staticbox.MaxEdge.Y > inner_margin)) { if ( (std::max(movingbox.MaxEdge.X + speed.X * time, staticbox.MaxEdge.X) - std::min(movingbox.MinEdge.X + speed.X * time, staticbox.MinEdge.X) - relbox.MinEdge.X < 0) && (std::max(movingbox.MaxEdge.Z + speed.Z * time, staticbox.MaxEdge.Z) - std::min(movingbox.MinEdge.Z + speed.Z * time, staticbox.MinEdge.Z) - relbox.MinEdge.Z < 0) ) return COLLISION_AXIS_Y; } } else { return COLLISION_AXIS_NONE; } } // NO else if here if (speed.X) { distance = relbox.MaxEdge.X - relbox.MinEdge.X; *dtime = distance / std::abs(speed.X); time = std::max(*dtime, 0.0f); if (*dtime <= dtime_max) { inner_margin = std::max(-0.5f * (staticbox.MaxEdge.X - staticbox.MinEdge.X), -2.0f); if ((speed.X > 0 && staticbox.MinEdge.X - movingbox.MaxEdge.X > inner_margin) || (speed.X < 0 && movingbox.MinEdge.X - staticbox.MaxEdge.X > inner_margin)) { if ( (std::max(movingbox.MaxEdge.Y + speed.Y * time, staticbox.MaxEdge.Y)