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 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292
|
/* librist. Copyright © 2020 SipRadius LLC. All right reserved.
* Author: Gijs Peskens <gijs@in2ip.nl>
* Author: Sergio Ammirata, Ph.D. <sergio@ammirata.net>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include "config.h"
#include "srp_shared.h"
#include <librist/librist_config.h>
#if HAVE_MBEDTLS
#include <mbedtls/base64.h>
#elif HAVE_NETTLE
#include <nettle/base64.h>
#endif
#include <stddef.h>
#include <stdbool.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <stdint.h>
#include <sys/stat.h>
#ifdef _WIN32
#include <windows.h>
#endif
#if defined(_WIN32) && !defined(stat)
#define stat _stat
#endif
typedef enum {
IN_USERNAME = 0,
IN_VERIFIER,
IN_SALT,
IN_HASH_ALGO,
IN_HASH_VERSION,
} user_verifier_state_e;
#define READ_VERIFIER_LEN 1025
#define READ_SALT_LEN 1025
#define MAX_LINE_LEN (READ_VERIFIER_LEN + READ_SALT_LEN + 1)
int srp_base64_decode(char *string, size_t string_len, size_t *out_len, uint8_t **out) {
//Add padding if needed
if ((string_len % 4) != 0)
{
size_t needed_padding = 4 - (string_len % 4);
for (size_t i = 0; i < needed_padding; i++)
string[(string_len + i)] = '=';
string_len += needed_padding;
string[string_len] = '\0';
}
#if HAVE_MBEDTLS
size_t len = 0;
int ret =mbedtls_base64_decode(NULL, 0, &len, (unsigned char *)string, string_len);
if (ret != 0 && ret != MBEDTLS_ERR_BASE64_BUFFER_TOO_SMALL)
return -1;
*out = malloc(len);
if (mbedtls_base64_decode(*out, len, out_len, (unsigned char *)string, string_len) != 0)
goto fail_decode;
return 0;
#elif HAVE_NETTLE
struct base64_decode_ctx ctx;
nettle_base64_decode_init(&ctx);
size_t len = BASE64_ENCODE_LENGTH(string_len);
*out = malloc(len);
if (nettle_base64_decode_update(&ctx, out_len, *out, string_len, string) != 1)
goto fail_decode;
if (nettle_base64_decode_final(&ctx) != 1)
goto fail_decode;
return 0;
#endif
fail_decode:
free(*out);
*out = NULL;
return -1;
}
int parse_line(const char *line, size_t line_len, uint8_t **decoded_verifier, size_t *decoded_verifier_len, uint8_t **decoded_salt, size_t *decoded_salt_len) {
char read = '\0';
user_verifier_state_e state = IN_VERIFIER;
int ret = -1;
size_t read_verifier_len = 0;
char *read_verifier = calloc(READ_VERIFIER_LEN, 1);
size_t read_salt_len = 0;
char *read_salt = calloc(READ_SALT_LEN, 1);
if (!read_salt || !read_verifier)
goto out;
for (size_t i=0; i < line_len; i++) {
read = line[i];
if (read == ':')
{
if (state >= IN_SALT && read == '\n')
break;
if (state == IN_VERIFIER)
read_verifier[read_verifier_len+1] = '\0';
else if (state == IN_SALT)
{
read_salt[read_salt_len +1] = '\0';
}
state++;
} else if (state == IN_VERIFIER)
{
if (read_verifier_len == READ_VERIFIER_LEN)
return -1;
read_verifier[read_verifier_len] = read;
read_verifier_len++;
} else if (state == IN_SALT)
{
if (read_salt_len == READ_SALT_LEN)
return -1;
read_salt[read_salt_len] = read;
read_salt_len++;
}
}
ret = srp_base64_decode(read_verifier, read_verifier_len, decoded_verifier_len, decoded_verifier);
if (ret != 0)
goto out;
ret = srp_base64_decode(read_salt, read_salt_len, decoded_salt_len, decoded_salt);
if (ret != 0)
free(*decoded_verifier);
out:
free(read_verifier);
free(read_salt);
return ret;
}
void user_verifier_lookup(char * username,
librist_verifier_lookup_data_t *lookup_data,
int *hashversion,
uint64_t *generation,
void *user_data)
{
#if HAVE_NETTLE
struct base64_decode_ctx ctx;
nettle_base64_decode_init(&ctx);
#endif
if (user_data == NULL)
return;
char *srpfile = user_data;
if (!generation)
return;
#ifdef _WIN32
HANDLE hfile = CreateFile(
srpfile,
GENERIC_READ,
FILE_SHARE_READ,
NULL,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
NULL);
FILETIME mtime;
if (hfile == INVALID_HANDLE_VALUE)
return;
if (!GetFileTime(hfile, NULL, NULL, &mtime))
return;
CloseHandle(hfile);
*generation = ((uint64_t)mtime.dwHighDateTime << 32) | mtime.dwLowDateTime;
#else
struct stat buf;
if (stat(srpfile, &buf) != 0)
return;
#ifdef _DARWIN_C_SOURCE
*generation = ((uint64_t)buf.st_mtimespec.tv_sec << 32) | buf.st_mtimespec.tv_nsec;
#else
*generation = ((uint64_t)buf.st_mtim.tv_sec << 32) | buf.st_mtim.tv_nsec;
#endif
#endif
if (!lookup_data || !hashversion)
return;
FILE *fh = fopen(srpfile, "r");
if (!fh)
return;
size_t username_offset = 0;
char read_hashver[3] = {0};
int read_hashver_len = 0;
user_verifier_state_e state = IN_USERNAME;
bool skipnextline = false;
int read = getc(fh);
//expected format: username:verifier:salt:3
int maxhashversion = *hashversion;
size_t username_len = strlen(username);
char *line_even = malloc(MAX_LINE_LEN);
size_t line_even_len =0;
bool line_one_done = false;
char *line_odd = malloc(MAX_LINE_LEN);
size_t line_odd_len = 0;
size_t line_len = 0;
while (read != EOF)
{
char *line = line_one_done? line_odd : line_even;
if (skipnextline)
{
if (read == '\n')
skipnextline = false;
}
else if (state >= IN_SALT && read == '\n') {
if (state >= IN_HASH_VERSION) {
state = IN_USERNAME;
username_offset = 0;
*hashversion = atoi(read_hashver);
if (line_one_done) {
line_odd_len = line_len;
} else {
line_even_len = line_len;
line_len = 0;
line_one_done = true;
if (*hashversion < maxhashversion) {
read = getc(fh);
continue;
}
}
}
break;
} else if (read == ':')
{
if (state == IN_VERIFIER)
line[line_len++] = read;
if (state == IN_USERNAME && username_offset != username_len) {
if (line_one_done) {
break;
}
skipnextline = true;
username_offset = 0;
continue;
}
state++;
}
else if (state == IN_USERNAME)
{
if (username_offset == username_len) {
username_offset = 0;
skipnextline = true;
continue;
}
if (username[username_offset] != read)
{
username_offset = 0;
skipnextline = true;
} else
username_offset++;
} else if (state >= IN_VERIFIER && state <= IN_SALT) {
if (line_len >= MAX_LINE_LEN) {
if (!line_one_done)
goto out;
break;
}
line[line_len++] = read;
} else if (state == IN_HASH_VERSION && read_hashver_len < 3) {
read_hashver[read_hashver_len] = read;
read_hashver_len++;
}
read = getc(fh);
}
*hashversion = atoi(read_hashver);
if (state < IN_SALT && !line_one_done)
goto out;
char *line = line_even;
line_len = line_even_len;
if (line_one_done && line_odd_len != 0) {
line = line_odd;
line_len = line_odd_len;
}
parse_line(line, line_len, &lookup_data->verifier, &lookup_data->verifier_len, &lookup_data->salt, &lookup_data->salt_len);
lookup_data->default_ng = true;
out:
free(line_even);
free(line_odd);
fclose(fh);
return;
}
|