| 1 |
. |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
#include "eap.h" |
| 9 |
#include "common/attributes.h" |
| 10 |
#include "config.h" |
| 11 |
#include "crypto/psk.h" |
| 12 |
#include "endian-shim.h" |
| 13 |
#include "crypto/crypto-private.h" |
| 14 |
#include "crypto/srp.h" |
| 15 |
#include "crypto/srp_constants.h" |
| 16 |
#include "librist_srp.h" |
| 17 |
#include "rist-private.h" |
| 18 |
#include "udp-private.h" |
| 19 |
#include "transport-private.h" |
| 20 |
#include "log-private.h" |
| 21 |
#include "proto/rist_time.h" |
| 22 |
#include "peer.h" |
| 23 |
#include "protocol_gre.h" |
| 24 |
|
| 25 |
#include <stddef.h> |
| 26 |
#include <string.h> |
| 27 |
#include <stdlib.h> |
| 28 |
#include <stdbool.h> |
| 29 |
#include <assert.h> |
| 30 |
#include <limits.h> |
| 31 |
|
| 32 |
#define HASH_ALGO SRP_SHA256 |
| 33 |
#define DIGEST_LENGTH SHA256_DIGEST_LENGTH |
| 34 |
#define EAP_LOG_PREFIX "[EAP-SRP] " |
| 35 |
#define EAP_AUTH_RETRY_MAX 3 |
| 36 |
#define EAP_AUTH_TIMEOUT_RETRY_MAX 5 |
| 37 |
#define EAP_AUTH_TIMEOUT 500//ms |
| 38 |
#define EAP_REAUTH_PERIOD 60000 // ms |
| 39 |
#define EAP_AUTH_FAILED_RECOVERY 30000 // ms, soft-FAILED -> UNAUTH after this quiet |
| 40 |
#define EAP_IDENTITY_REPLY_INTERVAL 200 // ms, rate-limit pre-auth IDENTITY replies |
| 41 |
#define EAP_MAX_MODULUS_BYTES 1024 // largest RFC 5054 group (NG_8192) is 1024 bytes |
| 42 |
|
| 43 |
|
| 44 |
#define EAP_AUTH_TRIES_PERMANENT UINT_MAX |
| 45 |
|
| 46 |
static int eap_request_passphrase(struct eapsrp_ctx *ctx, bool start); |
| 47 |
|
| 48 |
struct eapsrp_ctx |
| 49 |
{ |
| 50 |
pthread_mutex_t eap_lock; |
| 51 |
struct { |
| 52 |
char username[256]; |
| 53 |
char password[256]; |
| 54 |
user_verifier_lookup_t lookup_func_old; |
| 55 |
user_verifier_lookup_2_t lookup_func; |
| 56 |
void *lookup_func_userdata_old; |
| 57 |
void *lookup_func_userdata; |
| 58 |
struct rist_logging_settings *logging_settings; |
| 59 |
bool use_key_as_passphrase; |
| 60 |
uint8_t role; |
| 61 |
} config; |
| 62 |
|
| 63 |
int authentication_state; |
| 64 |
uint8_t last_identifier; |
| 65 |
unsigned int tries; |
| 66 |
bool may_rollover_passphrase; |
| 67 |
bool did_first_auth; |
| 68 |
|
| 69 |
uint64_t failed_state_timestamp; |
| 70 |
uint64_t last_identity_reply_timestamp; |
| 71 |
|
| 72 |
uint64_t passphrase_request_timer; |
| 73 |
int passphrase_request_times; |
| 74 |
uint8_t passphrase_request_identifier; |
| 75 |
|
| 76 |
uint64_t unsollicited_passphrase_response_timer; |
| 77 |
int unsollicited_passphrase_response_times; |
| 78 |
uint8_t unsollicited_passphrase_response_identifier; |
| 79 |
uint8_t unsollicited_passphrase[128]; |
| 80 |
size_t unsollicited_passphrase_len; |
| 81 |
int unsollicited_passphrase_state; |
| 82 |
|
| 83 |
uint8_t *last_pkt; |
| 84 |
size_t last_pkt_size; |
| 85 |
uint8_t timeout_retries; |
| 86 |
uint64_t last_timestamp; |
| 87 |
uint64_t last_auth_timestamp; |
| 88 |
|
| 89 |
uint64_t generation; |
| 90 |
struct librist_crypto_srp_authenticator_ctx *auth_ctx; |
| 91 |
struct librist_crypto_srp_client_ctx *client_ctx; |
| 92 |
bool authenticated; |
| 93 |
|
| 94 |
struct rist_peer *peer; |
| 95 |
char ip_string[46]; |
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
char authenticator_username[256]; |
| 101 |
#if HAVE_MBEDTLS |
| 102 |
size_t authenticator_len_verifier_old; |
| 103 |
uint8_t *authenticator_bytes_verifier_old; |
| 104 |
size_t authenticator_len_salt_old; |
| 105 |
uint8_t *authenticator_bytes_salt_old; |
| 106 |
#endif |
| 107 |
size_t authenticator_len_verifier; |
| 108 |
uint8_t *authenticator_bytes_verifier; |
| 109 |
size_t authenticator_len_salt; |
| 110 |
uint8_t *authenticator_bytes_salt; |
| 111 |
|
| 112 |
bool eapversion3; |
| 113 |
|
| 114 |
bool srp_legacy_pad; |
| 115 |
bool srp_legacy_peer_warned; |
| 116 |
}; |
| 117 |
|
| 118 |
static inline void eap_tries_inc(struct eapsrp_ctx *ctx) |
| 119 |
{ |
| 120 |
|
| 121 |
if (ctx->tries < EAP_AUTH_TRIES_PERMANENT) |
| 122 |
ctx->tries++; |
| 123 |
} |
| 124 |
|
| 125 |
void eap_reset_data(struct eapsrp_ctx *ctx) |
| 126 |
{ |
| 127 |
if (ctx->config.role == EAP_ROLE_AUTHENTICATOR) |
| 128 |
{ |
| 129 |
#if HAVE_MBEDTLS |
| 130 |
free(ctx->authenticator_bytes_salt_old); |
| 131 |
free(ctx->authenticator_bytes_verifier_old); |
| 132 |
ctx->authenticator_bytes_salt_old = NULL; |
| 133 |
ctx->authenticator_bytes_verifier_old = NULL; |
| 134 |
#endif |
| 135 |
free(ctx->authenticator_bytes_salt); |
| 136 |
free(ctx->authenticator_bytes_verifier); |
| 137 |
ctx->authenticator_bytes_salt = NULL; |
| 138 |
ctx->authenticator_bytes_verifier = NULL; |
| 139 |
} |
| 140 |
librist_crypto_srp_authenticator_ctx_free(ctx->auth_ctx); |
| 141 |
ctx->auth_ctx = NULL; |
| 142 |
librist_crypto_srp_client_ctx_free(ctx->client_ctx); |
| 143 |
ctx->client_ctx = NULL; |
| 144 |
free(ctx->last_pkt); |
| 145 |
|
| 146 |
ctx->last_pkt = NULL; |
| 147 |
|
| 148 |
ctx->authenticated = false; |
| 149 |
} |
| 150 |
|
| 151 |
static int send_eapol_pkt(struct eapsrp_ctx *ctx, uint8_t eapoltype, uint8_t eapcode, uint8_t identifier, size_t payload_len, uint8_t buf[], uint8_t eap_version) |
| 152 |
{ |
| 153 |
size_t offset = 0; |
| 154 |
struct eapol_hdr *eapol_hdr = (struct eapol_hdr *)&buf[offset]; |
| 155 |
offset += sizeof(*eapol_hdr); |
| 156 |
struct eap_hdr *eap_hdr = (struct eap_hdr *)&buf[offset]; |
| 157 |
offset += sizeof(*eap_hdr); |
| 158 |
eapol_hdr->eapversion = eap_version; |
| 159 |
eapol_hdr->eaptype = eapoltype; |
| 160 |
eap_hdr->code = eapcode; |
| 161 |
eap_hdr->identifier = identifier; |
| 162 |
eapol_hdr->length = eap_hdr->length = htobe16(payload_len + sizeof(*eap_hdr)); |
| 163 |
|
| 164 |
|
| 165 |
if (identifier == ctx->last_identifier) |
| 166 |
{ |
| 167 |
free(ctx->last_pkt); |
| 168 |
ctx->last_pkt = malloc((payload_len + EAPOL_EAP_HDRS_OFFSET)); |
| 169 |
if (ctx->last_pkt == NULL) { |
| 170 |
|
| 171 |
ctx->last_pkt_size = 0; |
| 172 |
} else { |
| 173 |
memcpy(ctx->last_pkt, buf, (payload_len + EAPOL_EAP_HDRS_OFFSET)); |
| 174 |
ctx->last_pkt_size = (payload_len + EAPOL_EAP_HDRS_OFFSET); |
| 175 |
ctx->last_timestamp = timestampNTP_u64(); |
| 176 |
ctx->timeout_retries = 0; |
| 177 |
} |
| 178 |
} |
| 179 |
if (_librist_proto_gre_send_data(ctx->peer, 0, RIST_GRE_PROTOCOL_TYPE_EAPOL, buf, (EAPOL_EAP_HDRS_OFFSET + payload_len), 0, 0, ctx->peer->rist_gre_version) < 0) |
| 180 |
return -1; |
| 181 |
|
| 182 |
return 0; |
| 183 |
} |
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
static int process_eap_request_identity(struct eapsrp_ctx *ctx, uint8_t identifier) |
| 188 |
{ |
| 189 |
if (ctx->config.role == EAP_ROLE_AUTHENTICATOR) |
| 190 |
return EAP_UNEXPECTEDREQUEST; |
| 191 |
|
| 192 |
if (ctx->authentication_state >= EAP_AUTH_STATE_SUCCESS) |
| 193 |
return EAP_UNEXPECTEDREQUEST; |
| 194 |
|
| 195 |
uint64_t now = timestampNTP_u64(); |
| 196 |
if (ctx->last_identity_reply_timestamp != 0 && |
| 197 |
now < ctx->last_identity_reply_timestamp + (uint64_t)EAP_IDENTITY_REPLY_INTERVAL * RIST_CLOCK) { |
| 198 |
rist_log_priv2(ctx->config.logging_settings, RIST_LOG_DEBUG, |
| 199 |
EAP_LOG_PREFIX"Rate-limiting EAP IDENTITY response (last sent %u ms ago)\n", |
| 200 |
(unsigned)((now - ctx->last_identity_reply_timestamp) / RIST_CLOCK)); |
| 201 |
return 0; |
| 202 |
} |
| 203 |
ctx->last_identity_reply_timestamp = now; |
| 204 |
eap_reset_data(ctx); |
| 205 |
uint8_t eapolpkt[512]; |
| 206 |
size_t offset = EAPOL_EAP_HDRS_OFFSET; |
| 207 |
eapolpkt[offset] = EAP_TYPE_IDENTITY; |
| 208 |
offset += 1; |
| 209 |
memcpy(&eapolpkt[offset], ctx->config.username, strlen(ctx->config.username)); |
| 210 |
offset += strlen(ctx->config.username); |
| 211 |
size_t len = offset; |
| 212 |
len -= EAPOL_EAP_HDRS_OFFSET; |
| 213 |
return send_eapol_pkt(ctx, EAPOL_TYPE_EAP, EAP_CODE_RESPONSE, identifier, len, eapolpkt, ctx->eapversion3? 3 :2); |
| 214 |
} |
| 215 |
|
| 216 |
static int process_eap_request_srp_challenge(struct eapsrp_ctx *ctx, uint8_t identifier, size_t len, uint8_t pkt[], uint8_t eap_version) |
| 217 |
{ |
| 218 |
if (len < 6) |
| 219 |
return EAP_LENERR; |
| 220 |
|
| 221 |
#if HAVE_MBEDTLS |
| 222 |
ctx->eapversion3 = (eap_version >= 3); |
| 223 |
#elif HAVE_NETTLE |
| 224 |
(void)(eap_version); |
| 225 |
#endif |
| 226 |
|
| 227 |
size_t offset = 0; |
| 228 |
if (offset + 2 > len) |
| 229 |
return EAP_LENERR; |
| 230 |
uint16_t *tmp_swap = (uint16_t *)&pkt[offset]; |
| 231 |
size_t name_len = be16toh(*tmp_swap); |
| 232 |
offset += 2; |
| 233 |
|
| 234 |
if (name_len > len - offset) |
| 235 |
return EAP_LENERR; |
| 236 |
offset += name_len; |
| 237 |
if (offset + 2 > len) |
| 238 |
return EAP_LENERR; |
| 239 |
tmp_swap = (uint16_t *)&pkt[offset]; |
| 240 |
size_t salt_len = be16toh(*tmp_swap); |
| 241 |
offset += 2; |
| 242 |
if (salt_len > len - offset) |
| 243 |
return EAP_LENERR; |
| 244 |
|
| 245 |
uint8_t *salt = &pkt[offset]; |
| 246 |
uint8_t *g = NULL; |
| 247 |
uint8_t *N = NULL; |
| 248 |
size_t N_len = 0; |
| 249 |
offset += salt_len; |
| 250 |
if (offset + 2 > len) |
| 251 |
return EAP_LENERR; |
| 252 |
tmp_swap = (uint16_t *)&pkt[offset]; |
| 253 |
size_t generator_len = be16toh(*tmp_swap); |
| 254 |
offset += 2; |
| 255 |
if (generator_len != 0) |
| 256 |
{ |
| 257 |
if (generator_len > len - offset) |
| 258 |
return EAP_LENERR; |
| 259 |
if (generator_len > EAP_MAX_MODULUS_BYTES) |
| 260 |
return EAP_LENERR; |
| 261 |
|
| 262 |
g = &pkt[offset]; |
| 263 |
offset += generator_len; |
| 264 |
N = &pkt[offset]; |
| 265 |
N_len = len - offset; |
| 266 |
if (N_len > EAP_MAX_MODULUS_BYTES) |
| 267 |
return EAP_LENERR; |
| 268 |
} |
| 269 |
bool use_default_ng = (generator_len == 0); |
| 270 |
librist_crypto_srp_client_ctx_free(ctx->client_ctx); |
| 271 |
ctx->client_ctx = librist_crypto_srp_client_ctx_create(use_default_ng, N, N_len, g, generator_len, salt, salt_len, ctx->eapversion3, ctx->srp_legacy_pad); |
| 272 |
if (ctx->client_ctx == NULL) |
| 273 |
return EAP_INTERNALERR; |
| 274 |
uint8_t response[1500] = {0}; |
| 275 |
struct eap_srp_hdr *hdr = (struct eap_srp_hdr *)&response[EAPOL_EAP_HDRS_OFFSET]; |
| 276 |
hdr->type = EAP_TYPE_SRP_SHA1; |
| 277 |
hdr->subtype = EAP_SRP_SUBTYPE_CHALLENGE; |
| 278 |
int len_A = librist_crypto_srp_client_write_A_bytes(ctx->client_ctx, &response[EAPOL_EAP_HDRS_OFFSET + sizeof(*hdr)], sizeof(response) -(EAPOL_EAP_HDRS_OFFSET + sizeof(*hdr))); |
| 279 |
if (len_A < 0) |
| 280 |
return -1; |
| 281 |
return send_eapol_pkt(ctx, EAPOL_TYPE_EAP, EAP_CODE_RESPONSE, identifier, ((size_t)len_A + sizeof(*hdr)), response, ctx->eapversion3? 3 :2); |
| 282 |
} |
| 283 |
|
| 284 |
static int process_eap_request_srp_server_key(struct eapsrp_ctx *ctx, uint8_t identifier, size_t len, uint8_t pkt[]) |
| 285 |
{ |
| 286 |
if (librist_crypto_srp_client_handle_B(ctx->client_ctx, pkt, len, ctx->config.username, ctx->config.password) != 0) |
| 287 |
{ |
| 288 |
ctx->authentication_state = EAP_AUTH_STATE_FAILED; |
| 289 |
|
| 290 |
ctx->tries = EAP_AUTH_TRIES_PERMANENT; |
| 291 |
return EAP_AUTH_TERMINATED; |
| 292 |
} |
| 293 |
size_t out_len = sizeof(struct eap_srp_hdr) + 4 + DIGEST_LENGTH; |
| 294 |
uint8_t response[(EAPOL_EAP_HDRS_OFFSET + sizeof(struct eap_srp_hdr) + 4 + DIGEST_LENGTH)]; |
| 295 |
size_t offset = EAPOL_EAP_HDRS_OFFSET; |
| 296 |
struct eap_srp_hdr *hdr = (struct eap_srp_hdr *)&response[offset]; |
| 297 |
offset += sizeof(*hdr); |
| 298 |
memset(&response[offset], 0, 4); |
| 299 |
offset += 4; |
| 300 |
hdr->type = EAP_TYPE_SRP_SHA1; |
| 301 |
hdr->subtype = EAP_SRP_SUBTYPE_SERVER_KEY; |
| 302 |
if (ctx->config.use_key_as_passphrase && ctx->eapversion3) { |
| 303 |
SET_BIT(response[(EAPOL_EAP_HDRS_OFFSET + sizeof(*hdr) + 3)], 0); |
| 304 |
librist_peer_update_tx_passphrase(ctx->peer, librist_crypto_srp_client_get_key(ctx->client_ctx), SHA256_DIGEST_LENGTH, !ctx->did_first_auth); |
| 305 |
ctx->did_first_auth = true; |
| 306 |
} |
| 307 |
librist_crypto_srp_client_write_M1_bytes(ctx->client_ctx, &response[offset]); |
| 308 |
int ret = send_eapol_pkt(ctx, EAPOL_TYPE_EAP, EAP_CODE_RESPONSE, identifier, out_len, response, ctx->eapversion3? 3 :2); |
| 309 |
return ret; |
| 310 |
} |
| 311 |
|
| 312 |
static int process_eap_request_srp_server_validator(struct eapsrp_ctx *ctx, uint8_t identifier, size_t len, uint8_t pkt[]) |
| 313 |
{ |
| 314 |
if (len < (4 + DIGEST_LENGTH)) |
| 315 |
return EAP_LENERR; |
| 316 |
if (librist_crypto_srp_client_verify_m2(ctx->client_ctx, &pkt[4]) == 0) |
| 317 |
{ |
| 318 |
if (ctx->authentication_state < EAP_AUTH_STATE_SUCCESS) |
| 319 |
rist_log_priv2(ctx->config.logging_settings, RIST_LOG_INFO, EAP_LOG_PREFIX"Successfully authenticated\n"); |
| 320 |
bool set_passphrase = CHECK_BIT(pkt[3], 0); |
| 321 |
if (set_passphrase && ctx->eapversion3) { |
| 322 |
librist_peer_update_rx_passphrase(ctx->peer, librist_crypto_srp_client_get_key(ctx->client_ctx), SHA256_DIGEST_LENGTH, !ctx->did_first_auth); |
| 323 |
} |
| 324 |
|
| 325 |
if (ctx->config.use_key_as_passphrase && ctx->eapversion3) |
| 326 |
ctx->may_rollover_passphrase = true; |
| 327 |
|
| 328 |
ctx->did_first_auth = true; |
| 329 |
ctx->authentication_state = EAP_AUTH_STATE_SUCCESS; |
| 330 |
ctx->last_auth_timestamp = timestampNTP_u64(); |
| 331 |
ctx->tries = 0; |
| 332 |
ctx->failed_state_timestamp = 0; |
| 333 |
uint8_t outpkt[(EAPOL_EAP_HDRS_OFFSET + sizeof(struct eap_srp_hdr))] = {0}; |
| 334 |
struct eap_srp_hdr *hdr = (struct eap_srp_hdr *)&outpkt[EAPOL_EAP_HDRS_OFFSET]; |
| 335 |
if (ctx->eapversion3) { |
| 336 |
int ret = send_eapol_pkt(ctx, EAPOL_TYPE_EAP, EAP_CODE_SUCCESS, identifier, sizeof(*hdr), outpkt, 3); |
| 337 |
eap_request_passphrase(ctx, true); |
| 338 |
return ret; |
| 339 |
} |
| 340 |
hdr->type = EAP_TYPE_SRP_SHA1; |
| 341 |
hdr->subtype = EAP_SRP_SUBTYPE_SERVER_VALIDATOR; |
| 342 |
return send_eapol_pkt(ctx, EAPOL_TYPE_EAP, EAP_CODE_RESPONSE, identifier, sizeof(*hdr), outpkt, ctx->eapversion3? 3 :2); |
| 343 |
} |
| 344 |
|
| 345 |
rist_log_priv2(ctx->config.logging_settings, RIST_LOG_WARN, |
| 346 |
EAP_LOG_PREFIX"Server M2 verification failed for server@%s\n", ctx->ip_string); |
| 347 |
|
| 348 |
if (!ctx->srp_legacy_peer_warned) { |
| 349 |
ctx->srp_legacy_peer_warned = true; |
| 350 |
if (ctx->srp_legacy_pad) { |
| 351 |
rist_log_priv2(ctx->config.logging_settings, RIST_LOG_INFO, |
| 352 |
EAP_LOG_PREFIX" Hint: this side is configured with srp-compat=1. If the server is running librist 0.2.16+ (PAD-compliant, the default), drop ?srp-compat=1 on both sides.\n"); |
| 353 |
} else { |
| 354 |
rist_log_priv2(ctx->config.logging_settings, RIST_LOG_INFO, |
| 355 |
EAP_LOG_PREFIX" Hint: if the server is running librist 0.2.15 or earlier, the SRP wire format changed in 0.2.16 for RFC 5054 / TR-06-2 compliance. To interoperate with an older server, add ?srp-compat=1 on BOTH URLs. Otherwise check the password.\n"); |
| 356 |
} |
| 357 |
} |
| 358 |
ctx->authentication_state = EAP_AUTH_STATE_FAILED; |
| 359 |
ctx->tries = EAP_AUTH_TRIES_PERMANENT; |
| 360 |
|
| 361 |
return -1; |
| 362 |
} |
| 363 |
|
| 364 |
static int eap_srp_send_password(struct eapsrp_ctx *ctx, uint8_t identifier, const uint8_t *password, size_t password_len) { |
| 365 |
if (password_len > (1500 - (EAPOL_EAP_HDRS_OFFSET + sizeof(struct eap_srp_hdr) + 1))) |
| 366 |
return -1; |
| 367 |
uint8_t outpkt[1500] = {0}; |
| 368 |
struct eap_srp_hdr *hdr = (struct eap_srp_hdr *)&outpkt[EAPOL_EAP_HDRS_OFFSET]; |
| 369 |
hdr->type = EAP_TYPE_SRP_SHA1; |
| 370 |
hdr->subtype = EAP_SRP_SUBTYPE_PASSWORD_REQUEST_RESPONSE; |
| 371 |
if (password_len == 0) { |
| 372 |
SET_BIT(outpkt[EAPOL_EAP_HDRS_OFFSET + sizeof(*hdr)], 7); |
| 373 |
} else { |
| 374 |
SET_BIT(outpkt[EAPOL_EAP_HDRS_OFFSET + sizeof(*hdr)], 6); |
| 375 |
const uint8_t *key; |
| 376 |
if (ctx->config.role == EAP_ROLE_AUTHENTICATOR) |
| 377 |
key = librist_crypto_srp_authenticator_get_key(ctx->auth_ctx); |
| 378 |
else |
| 379 |
key = librist_crypto_srp_client_get_key(ctx->client_ctx); |
| 380 |
|
| 381 |
uint8_t iv[AES_BLOCK_SIZE] = {0}; |
| 382 |
iv[AES_BLOCK_SIZE-1] = identifier; |
| 383 |
_librist_crypto_aes_ctr(key, 256, iv, password, &outpkt[EAPOL_EAP_HDRS_OFFSET + sizeof(*hdr) +1], password_len); |
| 384 |
} |
| 385 |
return send_eapol_pkt(ctx, EAPOL_TYPE_EAP, EAP_CODE_RESPONSE, identifier, sizeof(*hdr) +1 + password_len, outpkt, ctx->eapversion3? 3 :2); |
| 386 |
} |
| 387 |
|
| 388 |
static int process_eap_request_srp_passphrase(struct eapsrp_ctx *ctx, uint8_t identifier) { |
| 389 |
if (ctx->authentication_state == EAP_AUTH_STATE_SUCCESS) { |
| 390 |
if (ctx->config.use_key_as_passphrase) |
| 391 |
return eap_srp_send_password(ctx, identifier, NULL, 0); |
| 392 |
|
| 393 |
const uint8_t *passphrase = NULL; |
| 394 |
size_t passphrase_len = 0; |
| 395 |
librist_peer_get_current_tx_passphrase(ctx->peer, &passphrase, &passphrase_len); |
| 396 |
return eap_srp_send_password(ctx, identifier, passphrase, passphrase_len); |
| 397 |
} |
| 398 |
|
| 399 |
uint8_t buf[EAPOL_EAP_HDRS_OFFSET]; |
| 400 |
return send_eapol_pkt(ctx, EAPOL_TYPE_EAP, EAP_CODE_FAILURE, identifier, 0, buf, ctx->eapversion3? 3 :2); |
| 401 |
} |
| 402 |
|
| 403 |
static int process_eap_request(struct eapsrp_ctx *ctx, uint8_t pkt[], size_t len, uint8_t identifier, uint8_t eap_version) |
| 404 |
{ |
| 405 |
if (len < 1) |
| 406 |
return EAP_LENERR; |
| 407 |
uint8_t type = pkt[0]; |
| 408 |
if (type == EAP_TYPE_IDENTITY) |
| 409 |
return process_eap_request_identity(ctx, identifier); |
| 410 |
if (type == EAP_TYPE_SRP_SHA1) |
| 411 |
{ |
| 412 |
if (len < 2) |
| 413 |
return EAP_LENERR; |
| 414 |
uint8_t subtype = pkt[1]; |
| 415 |
if (subtype != EAP_SRP_SUBTYPE_PASSWORD_REQUEST_RESPONSE && ctx->config.role == EAP_ROLE_AUTHENTICATOR) |
| 416 |
return EAP_UNEXPECTEDREQUEST; |
| 417 |
|
| 418 |
|
| 419 |
|
| 420 |
|
| 421 |
|
| 422 |
ctx->last_identifier = identifier; |
| 423 |
switch (subtype) |
| 424 |
{ |
| 425 |
case EAP_SRP_SUBTYPE_CHALLENGE: |
| 426 |
return process_eap_request_srp_challenge(ctx, identifier, (len -2), &pkt[2], eap_version); |
| 427 |
break; |
| 428 |
case EAP_SRP_SUBTYPE_SERVER_KEY: |
| 429 |
return process_eap_request_srp_server_key(ctx, identifier, (len -2), &pkt[2]); |
| 430 |
break; |
| 431 |
case EAP_SRP_SUBTYPE_SERVER_VALIDATOR: |
| 432 |
return process_eap_request_srp_server_validator(ctx, identifier, (len -2), &pkt[2]); |
| 433 |
break; |
| 434 |
case EAP_SRP_SUBTYPE_LWRECHALLENGE: |
| 435 |
|
| 436 |
break; |
| 437 |
case EAP_SRP_SUBTYPE_PASSWORD_REQUEST_RESPONSE: |
| 438 |
return process_eap_request_srp_passphrase(ctx, identifier); |
| 439 |
break; |
| 440 |
default: |
| 441 |
return EAP_SRP_WRONGSUBTYPE; |
| 442 |
} |
| 443 |
} |
| 444 |
return -1; |
| 445 |
} |
| 446 |
|
| 447 |
|
| 448 |
|
| 449 |
static int process_eap_response_identity(struct eapsrp_ctx *ctx, size_t len, uint8_t pkt[], uint8_t eap_version) |
| 450 |
{ |
| 451 |
if (len > 255) |
| 452 |
return -1; |
| 453 |
|
| 454 |
|
| 455 |
|
| 456 |
if (ctx->config.role != EAP_ROLE_AUTHENTICATOR || !ctx->config.lookup_func) |
| 457 |
return -1; |
| 458 |
memcpy(ctx->config.username, pkt, len); |
| 459 |
ctx->config.username[len] = '\0'; |
| 460 |
#if HAVE_MBEDTLS |
| 461 |
int hashversion = eap_version >= 3 ? 1 : 0; |
| 462 |
#elif HAVE_NETTLE |
| 463 |
(void)eap_version; |
| 464 |
int hashversion = 1; |
| 465 |
#endif |
| 466 |
uint64_t generation = 0; |
| 467 |
librist_verifier_lookup_data_t verifier_data = {0}; |
| 468 |
ctx->config.lookup_func(ctx->config.username, &verifier_data, &hashversion, &generation, ctx->config.lookup_func_userdata); |
| 469 |
#if HAVE_NETTLE |
| 470 |
if (hashversion == 0) { |
| 471 |
rist_log_priv2(ctx->config.logging_settings, RIST_LOG_ERROR, EAP_LOG_PREFIX"Lookup from SRP File got hashversion 0 response, Nettle backend does not support this, authentication likely to fail\n"); |
| 472 |
} |
| 473 |
hashversion = 1; |
| 474 |
#endif |
| 475 |
ctx->generation = generation; |
| 476 |
ctx->eapversion3 = (hashversion >= 1); |
| 477 |
const char *n_hex = verifier_data.n_modulus_ascii; |
| 478 |
const char *g_hex = verifier_data.generator_ascii; |
| 479 |
bool found = (verifier_data.verifier_len != 0 && verifier_data.verifier && verifier_data.salt_len != 0 && verifier_data.salt); |
| 480 |
uint8_t outpkt[1500] = { 0 }; |
| 481 |
size_t offset = EAPOL_EAP_HDRS_OFFSET; |
| 482 |
struct eap_srp_hdr *hdr = (struct eap_srp_hdr *)&outpkt[offset]; |
| 483 |
offset += sizeof(*hdr); |
| 484 |
hdr->type = EAP_TYPE_SRP_SHA1; |
| 485 |
hdr->subtype = EAP_SRP_SUBTYPE_CHALLENGE; |
| 486 |
int rc = -1; |
| 487 |
if (found) |
| 488 |
{ |
| 489 |
struct librist_crypto_srp_authenticator_ctx *auth_ctx = NULL; |
| 490 |
if (verifier_data.default_ng) |
| 491 |
librist_get_ng_constants(LIBRIST_SRP_NG_DEFAULT, &n_hex, &g_hex); |
| 492 |
|
| 493 |
auth_ctx = librist_crypto_srp_authenticator_ctx_create(n_hex, g_hex, verifier_data.verifier, verifier_data.verifier_len, verifier_data.salt, verifier_data.salt_len, ctx->eapversion3, ctx->srp_legacy_pad); |
| 494 |
if (!auth_ctx) |
| 495 |
goto out; |
| 496 |
librist_crypto_srp_authenticator_ctx_free(ctx->auth_ctx); |
| 497 |
ctx->auth_ctx = auth_ctx; |
| 498 |
memset(&outpkt[offset], 0, 2); |
| 499 |
offset += 2; |
| 500 |
uint16_t *tmp_swap = (uint16_t *)&outpkt[offset]; |
| 501 |
*tmp_swap = htobe16(verifier_data.salt_len); |
| 502 |
offset += 2; |
| 503 |
memcpy(&outpkt[offset], verifier_data.salt, verifier_data.salt_len); |
| 504 |
offset += verifier_data.salt_len; |
| 505 |
if (verifier_data.default_ng) |
| 506 |
{ |
| 507 |
memset(&outpkt[offset], 0, 2); |
| 508 |
offset += 2; |
| 509 |
} else { |
| 510 |
tmp_swap = (uint16_t *)&outpkt[offset]; |
| 511 |
offset += 2; |
| 512 |
int g_size = librist_crypto_srp_authenticator_write_g_bytes(auth_ctx, &outpkt[offset], sizeof(outpkt) -offset); |
| 513 |
if (g_size < 0) |
| 514 |
goto out; |
| 515 |
*tmp_swap = htobe16(g_size); |
| 516 |
offset += g_size; |
| 517 |
|
| 518 |
int n_len = librist_crypto_srp_authenticator_write_n_bytes(auth_ctx, &outpkt[offset], sizeof(outpkt) -offset); |
| 519 |
if (n_len < 0) |
| 520 |
goto out; |
| 521 |
offset += n_len; |
| 522 |
} |
| 523 |
ctx->last_identifier++; |
| 524 |
size_t out_len = offset - EAPOL_EAP_HDRS_OFFSET; |
| 525 |
rc = send_eapol_pkt(ctx, EAPOL_TYPE_EAP, EAP_CODE_REQUEST, ctx->last_identifier, out_len, outpkt, ctx->eapversion3? 3 :2); |
| 526 |
} |
| 527 |
out: |
| 528 |
free(verifier_data.verifier); |
| 529 |
free(verifier_data.salt); |
| 530 |
free(verifier_data.generator_ascii); |
| 531 |
free(verifier_data.n_modulus_ascii); |
| 532 |
return rc; |
| 533 |
} |
| 534 |
|
| 535 |
static int process_eap_response_client_key(struct eapsrp_ctx *ctx, size_t len, uint8_t pkt[]) |
| 536 |
{ |
| 537 |
if (!ctx->auth_ctx) { |
| 538 |
ctx->authentication_state = EAP_AUTH_STATE_FAILED; |
| 539 |
return EAP_INTERNALERR; |
| 540 |
} |
| 541 |
|
| 542 |
if (librist_crypto_srp_authenticator_handle_A(ctx->auth_ctx, pkt, len) != 0) { |
| 543 |
ctx->authentication_state = EAP_AUTH_STATE_FAILED; |
| 544 |
ctx->tries = EAP_AUTH_TRIES_PERMANENT; |
| 545 |
return EAP_AUTH_TERMINATED; |
| 546 |
} |
| 547 |
|
| 548 |
uint8_t outpkt[1500]; |
| 549 |
struct eap_srp_hdr *hdr = (struct eap_srp_hdr *)&outpkt[EAPOL_EAP_HDRS_OFFSET]; |
| 550 |
hdr->type = EAP_TYPE_SRP_SHA1; |
| 551 |
hdr->subtype = EAP_SRP_SUBTYPE_SERVER_KEY; |
| 552 |
int len_B = librist_crypto_srp_authenticator_write_B_bytes(ctx->auth_ctx, &outpkt[(EAPOL_EAP_HDRS_OFFSET + sizeof(*hdr))], sizeof(outpkt) - (EAPOL_EAP_HDRS_OFFSET + sizeof(*hdr))); |
| 553 |
if (len_B < 0) |
| 554 |
return -1; |
| 555 |
ctx->last_identifier++; |
| 556 |
return send_eapol_pkt(ctx, EAPOL_TYPE_EAP, EAP_CODE_REQUEST, ctx->last_identifier, (sizeof(struct eap_srp_hdr) + (size_t)len_B), outpkt, ctx->eapversion3? 3 :2); |
| 557 |
} |
| 558 |
|
| 559 |
static int process_eap_response_client_validator(struct eapsrp_ctx *ctx, size_t len, uint8_t pkt[]) |
| 560 |
{ |
| 561 |
if (len < (4 + DIGEST_LENGTH)) |
| 562 |
return EAP_LENERR; |
| 563 |
|
| 564 |
if (!ctx->auth_ctx) { |
| 565 |
ctx->authentication_state = EAP_AUTH_STATE_FAILED; |
| 566 |
return EAP_INTERNALERR; |
| 567 |
} |
| 568 |
|
| 569 |
if (librist_crypto_srp_authenticator_verify_m1(ctx->auth_ctx, ctx->config.username, &pkt[4]) != 0) { |
| 570 |
rist_log_priv2(ctx->config.logging_settings, RIST_LOG_WARN, EAP_LOG_PREFIX"Authentication failed for %s@%s\n", ctx->config.username, ctx->ip_string); |
| 571 |
|
| 572 |
if (!ctx->srp_legacy_peer_warned) { |
| 573 |
ctx->srp_legacy_peer_warned = true; |
| 574 |
if (ctx->srp_legacy_pad) { |
| 575 |
rist_log_priv2(ctx->config.logging_settings, RIST_LOG_INFO, |
| 576 |
EAP_LOG_PREFIX" Hint: this side is configured with srp-compat=1. If %s is running librist 0.2.16+ (PAD-compliant, the default), drop ?srp-compat=1 on both sides.\n", |
| 577 |
ctx->ip_string); |
| 578 |
} else { |
| 579 |
rist_log_priv2(ctx->config.logging_settings, RIST_LOG_INFO, |
| 580 |
EAP_LOG_PREFIX" Hint: if %s is running librist 0.2.15 or earlier, the SRP wire format changed in 0.2.16 for RFC 5054 / TR-06-2 compliance. To interoperate with an older peer, add ?srp-compat=1 on BOTH URLs. Otherwise check the password.\n", |
| 581 |
ctx->ip_string); |
| 582 |
} |
| 583 |
} |
| 584 |
ctx->authentication_state = EAP_AUTH_STATE_FAILED; |
| 585 |
eap_tries_inc(ctx); |
| 586 |
int ret = EAP_AUTH_FAILED; |
| 587 |
if (ctx->tries > EAP_AUTH_RETRY_MAX) { |
| 588 |
rist_log_priv2(ctx->config.logging_settings, RIST_LOG_ERROR, EAP_LOG_PREFIX"Authentication retry count exceeded\n"); |
| 589 |
ret = EAP_AUTH_TERMINATED; |
| 590 |
} |
| 591 |
uint8_t buf[EAPOL_EAP_HDRS_OFFSET]; |
| 592 |
send_eapol_pkt(ctx, EAPOL_TYPE_EAP, EAP_CODE_FAILURE, ctx->last_identifier, 0, buf, ctx->eapversion3? 3 :2); |
| 593 |
eap_reset_data(ctx); |
| 594 |
return ret; |
| 595 |
} |
| 596 |
ctx->authenticated = true; |
| 597 |
bool set_passphrase = CHECK_BIT(pkt[3], 0); |
| 598 |
if (set_passphrase) { |
| 599 |
librist_peer_update_rx_passphrase(ctx->peer, librist_crypto_srp_authenticator_get_key(ctx->auth_ctx), SHA256_DIGEST_LENGTH, !ctx->did_first_auth); |
| 600 |
} |
| 601 |
ctx->did_first_auth = true; |
| 602 |
uint8_t outpkt[(EAPOL_EAP_HDRS_OFFSET + sizeof(struct eap_srp_hdr) + 4 + DIGEST_LENGTH)]; |
| 603 |
struct eap_srp_hdr *hdr = (struct eap_srp_hdr *)&outpkt[EAPOL_EAP_HDRS_OFFSET]; |
| 604 |
hdr->type = EAP_TYPE_SRP_SHA1; |
| 605 |
hdr->subtype = EAP_SRP_SUBTYPE_SERVER_VALIDATOR; |
| 606 |
memset(&outpkt[EAPOL_EAP_HDRS_OFFSET + sizeof(struct eap_srp_hdr)], 0, 4); |
| 607 |
if (ctx->config.use_key_as_passphrase) { |
| 608 |
SET_BIT(outpkt[(EAPOL_EAP_HDRS_OFFSET + sizeof(*hdr) + 3)], 0); |
| 609 |
librist_peer_update_tx_passphrase(ctx->peer, librist_crypto_srp_authenticator_get_key(ctx->auth_ctx), SHA256_DIGEST_LENGTH, !ctx->did_first_auth); |
| 610 |
ctx->did_first_auth = true; |
| 611 |
} |
| 612 |
librist_crypto_srp_authenticator_write_M2_bytes(ctx->auth_ctx, &outpkt[(EAPOL_EAP_HDRS_OFFSET + sizeof(*hdr) + 4)]); |
| 613 |
ctx->last_identifier++; |
| 614 |
return send_eapol_pkt(ctx, EAPOL_TYPE_EAP, EAP_CODE_REQUEST, ctx->last_identifier, (sizeof(*hdr) + 4 + DIGEST_LENGTH), outpkt, ctx->eapversion3? 3 :2); |
| 615 |
} |
| 616 |
|
| 617 |
static int process_eap_response_srp_server_validator(struct eapsrp_ctx *ctx) |
| 618 |
{ |
| 619 |
if (ctx->authenticated) |
| 620 |
{ |
| 621 |
if (ctx->authentication_state < EAP_AUTH_STATE_SUCCESS) |
| 622 |
rist_log_priv2(ctx->config.logging_settings, RIST_LOG_INFO, EAP_LOG_PREFIX"Successfully authenticated %s@%s\n", ctx->config.username, ctx->ip_string); |
| 623 |
|
| 624 |
if (ctx->config.use_key_as_passphrase && ctx->eapversion3) |
| 625 |
ctx->may_rollover_passphrase = true; |
| 626 |
|
| 627 |
ctx->authentication_state = EAP_AUTH_STATE_SUCCESS; |
| 628 |
ctx->last_auth_timestamp = timestampNTP_u64(); |
| 629 |
ctx->tries = 0; |
| 630 |
ctx->failed_state_timestamp = 0; |
| 631 |
ctx->last_identifier++; |
| 632 |
} |
| 633 |
return 0; |
| 634 |
} |
| 635 |
|
| 636 |
static int process_eap_response_passphrase(struct eapsrp_ctx *ctx, uint8_t identifier, size_t len, uint8_t pkt[]) { |
| 637 |
if (ctx->authentication_state != EAP_AUTH_STATE_SUCCESS) |
| 638 |
return 0; |
| 639 |
|
| 640 |
|
| 641 |
|
| 642 |
|
| 643 |
|
| 644 |
|
| 645 |
ee |
| 646 |
|
| 647 |
|
| 648 |
bool matches_request = (ctx->passphrase_request_timer != 0 && |
| 649 |
identifier == ctx->passphrase_request_identifier); |
| 650 |
if (ctx->passphrase_request_timer != 0 && !matches_request) |
| 651 |
return 0; |
| 652 |
|
| 653 |
if (len < 1) |
| 654 |
return EAP_LENERR; |
| 655 |
bool use_derived_key = CHECK_BIT(pkt[0], 7); |
| 656 |
const uint8_t *key = NULL; |
| 657 |
if (ctx->config.role == EAP_ROLE_AUTHENTICATOR) |
| 658 |
key = librist_crypto_srp_authenticator_get_key(ctx->auth_ctx); |
| 659 |
else |
| 660 |
key = librist_crypto_srp_client_get_key(ctx->client_ctx); |
| 661 |
if (use_derived_key) { |
| 662 |
librist_peer_update_rx_passphrase(ctx->peer, key, SHA256_DIGEST_LENGTH, matches_request); |
| 663 |
} else { |
| 664 |
bool aes_256 = CHECK_BIT(pkt[0], 6); |
| 665 |
uint8_t iv[16] = {0}; |
| 666 |
iv[15] = identifier; |
| 667 |
_librist_crypto_aes_ctr(key, aes_256? 256: 128, iv, &pkt[1], &pkt[1], len -1); |
| 668 |
librist_peer_update_rx_passphrase(ctx->peer, &pkt[1], len-1, matches_request); |
| 669 |
} |
| 670 |
uint8_t buf[EAPOL_EAP_HDRS_OFFSET]; |
| 671 |
if (ctx->passphrase_request_timer) |
| 672 |
ctx->passphrase_request_timer = 0; |
| 673 |
return send_eapol_pkt(ctx, EAPOL_TYPE_EAP, EAP_CODE_SUCCESS, identifier, 0, buf, ctx->eapversion3? 3 :2); |
| 674 |
} |
| 675 |
|
| 676 |
static int eap_request_passphrase(struct eapsrp_ctx *ctx, bool start) { |
| 677 |
if (start) { |
| 678 |
ctx->unsollicited_passphrase_response_times = 0; |
| 679 |
ctx->passphrase_request_identifier++; |
| 680 |
if (ctx->config.role == EAP_ROLE_AUTHENTICATEE) |
| 681 |
SET_BIT(ctx->passphrase_request_identifier, 7); |
| 682 |
SET_BIT(ctx->passphrase_request_identifier, 6); |
| 683 |
} |
| 684 |
uint8_t outpkt[(EAPOL_EAP_HDRS_OFFSET + sizeof(struct eap_srp_hdr))]; |
| 685 |
struct eap_srp_hdr *hdr = (struct eap_srp_hdr *)&outpkt[EAPOL_EAP_HDRS_OFFSET]; |
| 686 |
hdr->type = EAP_TYPE_SRP_SHA1; |
| 687 |
hdr->subtype = EAP_SRP_SUBTYPE_PASSWORD_REQUEST_RESPONSE; |
| 688 |
ctx->passphrase_request_times++; |
| 689 |
ctx->passphrase_request_timer = timestampNTP_u64(); |
| 690 |
return send_eapol_pkt(ctx, EAPOL_TYPE_EAP, EAP_CODE_REQUEST, ctx->passphrase_request_identifier, sizeof(*hdr), outpkt, ctx->eapversion3? 3 :2); |
| 691 |
} |
| 692 |
|
| 693 |
static int process_eap_response(struct eapsrp_ctx *ctx, uint8_t pkt[], size_t len, uint8_t identifier, uint8_t eap_version) |
| 694 |
{ |
| 695 |
if (len < 1) |
| 696 |
return EAP_LENERR; |
| 697 |
uint8_t type = pkt[0]; |
| 698 |
ctx->timeout_retries = 0; |
| 699 |
free(ctx->last_pkt); |
| 700 |
ctx->last_pkt_size = 0; |
| 701 |
ctx->last_pkt = NULL; |
| 702 |
if (type == EAP_TYPE_IDENTITY) { |
| 703 |
|
| 704 |
|
| 705 |
|
| 706 |
|
| 707 |
if (ctx->config.role != EAP_ROLE_AUTHENTICATOR || !ctx->config.lookup_func) |
| 708 |
return EAP_UNEXPECTEDRESPONSE; |
| 709 |
if (identifier != ctx->last_identifier) |
| 710 |
return EAP_WRONGIDENTIFIER; |
| 711 |
return process_eap_response_identity(ctx, (len -1), &pkt[1], eap_version); |
| 712 |
} |
| 713 |
if (type == EAP_TYPE_SRP_SHA1) |
| 714 |
{ |
| 715 |
if (len < 2) |
| 716 |
return EAP_LENERR; |
| 717 |
uint8_t subtype = pkt[1]; |
| 718 |
|
| 719 |
if (subtype != EAP_SRP_SUBTYPE_PASSWORD_REQUEST_RESPONSE && ctx->config.role == EAP_ROLE_AUTHENTICATEE) |
| 720 |
return EAP_UNEXPECTEDRESPONSE; |
| 721 |
|
| 722 |
|
| 723 |
if (subtype != EAP_SRP_SUBTYPE_PASSWORD_REQUEST_RESPONSE && identifier != ctx->last_identifier) |
| 724 |
return EAP_WRONGIDENTIFIER; |
| 725 |
|
| 726 |
switch (subtype) |
| 727 |
{ |
| 728 |
case EAP_SRP_SUBTYPE_CLIENT_KEY: |
| 729 |
return process_eap_response_client_key(ctx, (len -2), &pkt[2]); |
| 730 |
break; |
| 731 |
case EAP_SRP_SYPTYPE_CLIENT_VALIDATOR: |
| 732 |
return process_eap_response_client_validator(ctx, (len -2), &pkt[2]); |
| 733 |
break; |
| 734 |
case EAP_SRP_SUBTYPE_SERVER_VALIDATOR: |
| 735 |
return process_eap_response_srp_server_validator(ctx); |
| 736 |
break; |
| 737 |
case EAP_SRP_SUBTYPE_LWRECHALLENGE: |
| 738 |
|
| 739 |
break; |
| 740 |
case EAP_SRP_SUBTYPE_PASSWORD_REQUEST_RESPONSE: |
| 741 |
return process_eap_response_passphrase(ctx, identifier, (len -2), &pkt[2]); |
| 742 |
break; |
| 743 |
default: |
| 744 |
return EAP_SRP_WRONGSUBTYPE; |
| 745 |
} |
| 746 |
} |
| 747 |
return -1; |
| 748 |
} |
| 749 |
|
| 750 |
static int process_eap_succes(struct eapsrp_ctx *ctx, uint8_t identifier) { |
| 751 |
if (identifier == ctx->unsollicited_passphrase_response_identifier && ctx->unsollicited_passphrase_response_timer != 0) { |
| 752 |
ctx->unsollicited_passphrase_state = EAP_PASSPHRASE_STATE_SUCCESS; |
| 753 |
ctx->unsollicited_passphrase_response_identifier = 0; |
| 754 |
ctx->unsollicited_passphrase_response_timer = 0; |
| 755 |
ctx->unsollicited_passphrase_response_times = 0; |
| 756 |
return 0; |
| 757 |
} |
| 758 |
if (identifier ==ctx->last_identifier) |
| 759 |
return process_eap_response_srp_server_validator(ctx); |
| 760 |
return 0; |
| 761 |
} |
| 762 |
|
| 763 |
static int process_eap_pkt(struct eapsrp_ctx *ctx, uint8_t pkt[], size_t len, uint8_t eap_version) |
| 764 |
{ |
| 765 |
if (ctx == NULL) |
| 766 |
return -1; |
| 767 |
if (ctx->authentication_state == EAP_AUTH_STATE_FAILED && ctx->tries >EAP_AUTH_RETRY_MAX) |
| 768 |
return EAP_AUTH_TERMINATED; |
| 769 |
if (len < sizeof(struct eap_hdr)) |
| 770 |
return EAP_LENERR; |
| 771 |
struct eap_hdr *hdr = (struct eap_hdr *)pkt; |
| 772 |
uint8_t code = hdr->code; |
| 773 |
uint8_t identifier = hdr->identifier; |
| 774 |
uint16_t length = be16toh(hdr->length); |
| 775 |
if (length != len) |
| 776 |
return EAP_LENERR; |
| 777 |
|
| 778 |
switch (code) |
| 779 |
{ |
| 780 |
case EAP_CODE_REQUEST: |
| 781 |
return process_eap_request(ctx, &pkt[sizeof(*hdr)], (len - sizeof(*hdr)), identifier, eap_version); |
| 782 |
break; |
| 783 |
case EAP_CODE_RESPONSE: |
| 784 |
return process_eap_response(ctx, &pkt[sizeof(*hdr)], (len - sizeof(*hdr)),identifier, eap_version); |
| 785 |
break; |
| 786 |
case EAP_CODE_SUCCESS: |
| 787 |
return process_eap_succes(ctx, identifier); |
| 788 |
break; |
| 789 |
case EAP_CODE_FAILURE: |
| 790 |
|
| 791 |
if (identifier != ctx->last_identifier) { |
| 792 |
rist_log_priv2(ctx->config.logging_settings, RIST_LOG_DEBUG, |
| 793 |
EAP_LOG_PREFIX"Dropping FAILURE with stale identifier %u (expected %u)\n", |
| 794 |
identifier, ctx->last_identifier); |
| 795 |
return 0; |
| 796 |
} |
| 797 |
|
| 798 |
eap_tries_inc(ctx); |
| 799 |
eap_reset_data(ctx); |
| 800 |
rist_log_priv2(ctx->config.logging_settings, RIST_LOG_ERROR, EAP_LOG_PREFIX"Authentication failed\n"); |
| 801 |
if (ctx->tries > EAP_AUTH_RETRY_MAX) { |
| 802 |
ctx->authentication_state = EAP_AUTH_STATE_FAILED; |
| 803 |
ctx->failed_state_timestamp = timestampNTP_u64(); |
| 804 |
return EAP_AUTH_TERMINATED; |
| 805 |
} |
| 806 |
return _librist_proto_eap_start(ctx); |
| 807 |
default: |
| 808 |
return -1; |
| 809 |
} |
| 810 |
return -1; |
| 811 |
} |
| 812 |
|
| 813 |
int eap_request_identity(struct eapsrp_ctx *ctx) |
| 814 |
{ |
| 815 |
uint8_t outpkt[EAPOL_EAP_HDRS_OFFSET +1]; |
| 816 |
outpkt[EAPOL_EAP_HDRS_OFFSET] = EAP_TYPE_IDENTITY; |
| 817 |
uint32_t id_rand; |
| 818 |
if (_librist_crypto_random_u32(&id_rand) != 0) |
| 819 |
return -1; |
| 820 |
ctx->last_identifier = (uint8_t)(id_rand >> 24); |
| 821 |
return send_eapol_pkt(ctx, EAPOL_TYPE_EAP, EAP_CODE_REQUEST, ctx->last_identifier, 1, outpkt, ctx->eapversion3? 3 :2); |
| 822 |
} |
| 823 |
|
| 824 |
int _librist_proto_eap_start(struct eapsrp_ctx *ctx) |
| 825 |
{ |
| 826 |
if (ctx->authentication_state == EAP_AUTH_STATE_SUCCESS) |
| 827 |
ctx->authentication_state = EAP_AUTH_STATE_REAUTH; |
| 828 |
struct eapol_hdr eapol; |
| 829 |
eapol.eapversion = 3; |
| 830 |
eapol.eaptype = EAPOL_TYPE_START; |
| 831 |
eapol.length = 0; |
| 832 |
if (_librist_proto_gre_send_data(ctx->peer, 0, RIST_GRE_PROTOCOL_TYPE_EAPOL, (uint8_t*)&eapol, sizeof(eapol), 0, 0, ctx->peer->rist_gre_version) < 0) |
| 833 |
return -1; |
| 834 |
return 0; |
| 835 |
} |
| 836 |
|
| 837 |
void eap_set_ip_string(struct eapsrp_ctx *ctx, char ip_string[]) |
| 838 |
{ |
| 839 |
if (ctx != NULL) { |
| 840 |
strncpy(ctx->ip_string, ip_string, sizeof(ctx->ip_string) - 1); |
| 841 |
ctx->ip_string[sizeof(ctx->ip_string) - 1] = '\0'; |
| 842 |
} |
| 843 |
} |
| 844 |
|
| 845 |
int eap_clone_ctx(struct eapsrp_ctx *in, struct rist_peer *peer) |
| 846 |
{ |
| 847 |
if (in == NULL) |
| 848 |
return 0; |
| 849 |
if (peer->eap_ctx != NULL) |
| 850 |
return -1; |
| 851 |
struct eapsrp_ctx *ctx = calloc(1, sizeof(*ctx)); |
| 852 |
if (!ctx) |
| 853 |
return -1; |
| 854 |
|
| 855 |
if (pthread_mutex_init(&ctx->eap_lock, NULL) != 0) { |
| 856 |
free(ctx); |
| 857 |
return -1; |
| 858 |
} |
| 859 |
memcpy(&ctx->config, &in->config, sizeof(in->config)); |
| 860 |
ctx->srp_legacy_pad = in->srp_legacy_pad; |
| 861 |
peer->eap_ctx = ctx; |
| 862 |
ctx->peer = peer; |
| 863 |
ctx->eapversion3 = true; |
| 864 |
return 0; |
| 865 |
} |
| 866 |
|
| 867 |
void eap_delete_ctx(struct eapsrp_ctx **in) |
| 868 |
{ |
| 869 |
if (*in == NULL) |
| 870 |
return; |
| 871 |
|
| 872 |
struct eapsrp_ctx *ctx = *in; |
| 873 |
eap_reset_data(ctx); |
| 874 |
|
| 875 |
free(ctx); |
| 876 |
*in = NULL; |
| 877 |
} |
| 878 |
|
| 879 |
int eap_process_eapol(struct eapsrp_ctx* ctx, uint8_t pkt[], size_t len) |
| 880 |
{ |
| 881 |
assert(ctx != NULL); |
| 882 |
if (len < sizeof(struct eapol_hdr)) |
| 883 |
return EAP_LENERR; |
| 884 |
struct eapol_hdr *hdr = (struct eapol_hdr *)pkt; |
| 885 |
uint8_t eap_version = hdr->eapversion; |
| 886 |
size_t body_len = be16toh(hdr->length); |
| 887 |
|
| 888 |
if (body_len + sizeof(struct eapol_hdr) > len) |
| 889 |
return EAP_LENERR; |
| 890 |
|
| 891 |
pthread_mutex_lock(&ctx->eap_lock); |
| 892 |
int ret = -1; |
| 893 |
switch (hdr->eaptype) |
| 894 |
{ |
| 895 |
case EAPOL_TYPE_EAP: |
| 896 |
ret = process_eap_pkt(ctx, &pkt[sizeof(*hdr)], body_len, eap_version); |
| 897 |
break; |
| 898 |
case EAPOL_TYPE_START: |
| 899 |
if (ctx->config.role == EAP_ROLE_AUTHENTICATOR && !ctx->last_pkt) |
| 900 |
ret = eap_request_identity(ctx); |
| 901 |
if (ctx->config.role == EAP_ROLE_AUTHENTICATOR && ctx->authentication_state == EAP_AUTH_STATE_SUCCESS) { |
| 902 |
ctx->authentication_state = EAP_AUTH_STATE_REAUTH; |
| 903 |
ret = eap_request_identity(ctx); |
| 904 |
} else |
| 905 |
ret = 0; |
| 906 |
break; |
| 907 |
case EAPOL_TYPE_LOGOFF: |
| 908 |
|
| 909 |
if (ctx->authentication_state >= EAP_AUTH_STATE_SUCCESS) { |
| 910 |
ret = EAP_UNEXPECTEDREQUEST; |
| 911 |
break; |
| 912 |
} |
| 913 |
ctx->authentication_state = EAP_AUTH_STATE_UNAUTH; |
| 914 |
ctx->tries = 0; |
| 915 |
ctx->failed_state_timestamp = 0; |
| 916 |
ret = 0; |
| 917 |
break; |
| 918 |
default: |
| 919 |
break; |
| 920 |
} |
| 921 |
pthread_mutex_unlock(&ctx->eap_lock); |
| 922 |
return ret; |
| 923 |
} |
| 924 |
|
| 925 |
bool eap_is_authenticated(struct eapsrp_ctx *ctx) |
| 926 |
{ |
| 927 |
if (ctx == NULL) |
| 928 |
return true; |
| 929 |
|
| 930 |
pthread_mutex_lock(&ctx->eap_lock); |
| 931 |
bool authenticated = (ctx->authentication_state >= EAP_AUTH_STATE_SUCCESS); |
| 932 |
pthread_mutex_unlock(&ctx->eap_lock); |
| 933 |
return authenticated; |
| 934 |
} |
| 935 |
|
| 936 |
static void eap_periodic_impl(struct eapsrp_ctx *ctx) |
| 937 |
{ |
| 938 |
uint64_t now = timestampNTP_u64(); |
| 939 |
uint64_t retry_period = EAP_AUTH_TIMEOUT * RIST_CLOCK; |
| 940 |
uint64_t reauth_period = EAP_REAUTH_PERIOD * RIST_CLOCK; |
| 941 |
if (ctx->authentication_state == EAP_AUTH_STATE_SUCCESS && ctx->passphrase_request_timer != 0 && ctx->passphrase_request_timer + retry_period < now) { |
| 942 |
if (ctx->passphrase_request_times > EAP_AUTH_TIMEOUT_RETRY_MAX) { |
| 943 |
rist_log_priv2(ctx->config.logging_settings, RIST_LOG_WARN, EAP_LOG_PREFIX"Failed to receive requested passphrase in a timely manner\n"); |
| 944 |
ctx->passphrase_request_timer = 0; |
| 945 |
} else { |
| 946 |
eap_request_passphrase(ctx, false); |
| 947 |
} |
| 948 |
} |
| 949 |
|
| 950 |
if (ctx->authentication_state == EAP_AUTH_STATE_SUCCESS && ctx->unsollicited_passphrase_response_timer != 0 && ctx->unsollicited_passphrase_response_timer + retry_period < now) { |
| 951 |
if (ctx->unsollicited_passphrase_response_times > EAP_AUTH_TIMEOUT_RETRY_MAX) { |
| 952 |
rist_log(ctx->config.logging_settings, RIST_LOG_ERROR, EAP_LOG_PREFIX"Failed to update passphrase for client\n"); |
| 953 |
ctx->unsollicited_passphrase_response_timer = 0; |
| 954 |
ctx->unsollicited_passphrase_state = EAP_PASSPHRASE_STATE_FAILED; |
| 955 |
} else { |
| 956 |
eap_srp_send_password(ctx, ctx->unsollicited_passphrase_response_identifier, ctx->unsollicited_passphrase, ctx->unsollicited_passphrase_len); |
| 957 |
ctx->unsollicited_passphrase_response_timer = timestampNTP_u64(); |
| 958 |
ctx->unsollicited_passphrase_response_times++; |
| 959 |
} |
| 960 |
} |
| 961 |
|
| 962 |
if (ctx->unsollicited_passphrase_response_timer == 0 && |
| 963 |
ctx->passphrase_request_timer && |
| 964 |
((ctx->unsollicited_passphrase_response_identifier & 0x3f) == 0x3f || |
| 965 |
(ctx->unsollicited_passphrase_response_identifier & 0x3f) == 0x3f)) { |
| 966 |
if (ctx->config.role == EAP_ROLE_AUTHENTICATEE) |
| 967 |
_librist_proto_eap_start(ctx); |
| 968 |
else |
| 969 |
eap_request_identity(ctx); |
| 970 |
} |
| 971 |
if (ctx->config.role == EAP_ROLE_AUTHENTICATOR && ctx->authentication_state != 1 && ctx->last_timestamp + retry_period < now && |
| 972 |
ctx->timeout_retries < EAP_AUTH_TIMEOUT_RETRY_MAX && ctx->tries <= EAP_AUTH_RETRY_MAX) |
| 973 |
{ |
| 974 |
if (ctx->last_pkt) |
| 975 |
{ |
| 976 |
rist_transport_sendto(ctx->peer, ctx->last_pkt, ctx->last_pkt_size, 0); |
| 977 |
|
| 978 |
ctx->timeout_retries++; |
| 979 |
ctx->last_timestamp = now; |
| 980 |
return; |
| 981 |
} else { |
| 982 |
eap_request_identity(ctx); |
| 983 |
return; |
| 984 |
} |
| 985 |
} else if (ctx->config.role == EAP_ROLE_AUTHENTICATOR && ctx->authentication_state == EAP_AUTH_STATE_SUCCESS && |
| 986 |
now > ctx->last_auth_timestamp + reauth_period) { |
| 987 |
if (ctx->generation > 0) { |
| 988 |
uint64_t generation = ctx->generation; |
| 989 |
int hashversion = ctx->eapversion3? 1 : 0; |
| 990 |
|
| 991 |
ctx->config.lookup_func(ctx->config.username, NULL, &hashversion, &generation, ctx->config.lookup_func_userdata); |
| 992 |
if (generation == ctx->generation) { |
| 993 |
ctx->last_auth_timestamp = now; |
| 994 |
return; |
| 995 |
} |
| 996 |
} |
| 997 |
ctx->authentication_state = EAP_AUTH_STATE_REAUTH; |
| 998 |
eap_request_identity(ctx); |
| 999 |
return; |
| 1000 |
} |
| 1001 |
uint64_t reauth_time_out = ctx->last_auth_timestamp + reauth_period + EAP_AUTH_RETRY_MAX * retry_period; |
| 1002 |
if (ctx->authentication_state == EAP_AUTH_STATE_REAUTH && now > reauth_time_out) { |
| 1003 |
ctx->authentication_state = EAP_AUTH_STATE_UNAUTH; |
| 1004 |
return; |
| 1005 |
} |
| 1006 |
|
| 1007 |
if (ctx->authentication_state == EAP_AUTH_STATE_FAILED && |
| 1008 |
ctx->tries > EAP_AUTH_RETRY_MAX && |
| 1009 |
ctx->tries < EAP_AUTH_TRIES_PERMANENT && |
| 1010 |
ctx->failed_state_timestamp != 0 && |
| 1011 |
now > ctx->failed_state_timestamp + (uint64_t)EAP_AUTH_FAILED_RECOVERY * RIST_CLOCK) { |
| 1012 |
rist_log_priv2(ctx->config.logging_settings, RIST_LOG_INFO, |
| 1013 |
EAP_LOG_PREFIX"Recovered from soft FAILED state after %d ms of quiet\n", |
| 1014 |
EAP_AUTH_FAILED_RECOVERY); |
| 1015 |
ctx->tries = 0; |
| 1016 |
ctx->failed_state_timestamp = 0; |
| 1017 |
ctx->authentication_state = EAP_AUTH_STATE_UNAUTH; |
| 1018 |
} |
| 1019 |
} |
| 1020 |
void eap_periodic(struct eapsrp_ctx *ctx) { |
| 1021 |
if (ctx == NULL) |
| 1022 |
return; |
| 1023 |
pthread_mutex_lock(&ctx->eap_lock); |
| 1024 |
eap_periodic_impl(ctx); |
| 1025 |
pthread_mutex_unlock(&ctx->eap_lock); |
| 1026 |
} |
| 1027 |
|
| 1028 |
static void internal_user_verifier_lookup(char * username, |
| 1029 |
librist_verifier_lookup_data_t *lookup_data, |
| 1030 |
int *hashversion, |
| 1031 |
uint64_t *generation, |
| 1032 |
void *user_data) |
| 1033 |
{ |
| 1034 |
if (user_data == NULL) |
| 1035 |
return; |
| 1036 |
|
| 1037 |
if (*generation == 1) |
| 1038 |
return; |
| 1039 |
|
| 1040 |
*generation = 1; |
| 1041 |
|
| 1042 |
struct eapsrp_ctx *ctx = (struct eapsrp_ctx *)user_data; |
| 1043 |
|
| 1044 |
uint8_t *decoded_verifier = NULL; |
| 1045 |
uint8_t *decoded_salt = NULL; |
| 1046 |
|
| 1047 |
if (strcmp(username, ctx->authenticator_username) != 0) |
| 1048 |
goto fail_decode; |
| 1049 |
|
| 1050 |
if (*hashversion == 0 && HAVE_MBEDTLS) { |
| 1051 |
#if HAVE_MBEDTLS |
| 1052 |
decoded_verifier = malloc(ctx->authenticator_len_verifier_old); |
| 1053 |
decoded_salt = malloc(ctx->authenticator_len_salt_old); |
| 1054 |
if (!decoded_verifier || !decoded_salt) |
| 1055 |
goto fail_decode; |
| 1056 |
memcpy(decoded_verifier, ctx->authenticator_bytes_verifier_old, ctx->authenticator_len_verifier_old); |
| 1057 |
memcpy(decoded_salt, ctx->authenticator_bytes_salt_old, ctx->authenticator_len_salt_old); |
| 1058 |
lookup_data->verifier_len = ctx->authenticator_len_verifier_old; |
| 1059 |
lookup_data->salt_len = ctx->authenticator_len_salt_old; |
| 1060 |
#endif |
| 1061 |
} else { |
| 1062 |
decoded_verifier = malloc(ctx->authenticator_len_verifier); |
| 1063 |
decoded_salt = malloc(ctx->authenticator_len_salt); |
| 1064 |
if (!decoded_verifier || !decoded_salt) |
| 1065 |
goto fail_decode; |
| 1066 |
memcpy(decoded_verifier, ctx->authenticator_bytes_verifier, ctx->authenticator_len_verifier); |
| 1067 |
memcpy(decoded_salt, ctx->authenticator_bytes_salt, ctx->authenticator_len_salt); |
| 1068 |
lookup_data->verifier_len = ctx->authenticator_len_verifier; |
| 1069 |
lookup_data->salt_len = ctx->authenticator_len_salt; |
| 1070 |
} |
| 1071 |
lookup_data->verifier = decoded_verifier; |
| 1072 |
lookup_data->salt = decoded_salt; |
| 1073 |
|
| 1074 |
lookup_data->default_ng = true; |
| 1075 |
goto out; |
| 1076 |
|
| 1077 |
fail_decode: |
| 1078 |
lookup_data->verifier_len = 0; |
| 1079 |
lookup_data->salt_len = 0; |
| 1080 |
free(decoded_verifier); |
| 1081 |
free(decoded_salt); |
| 1082 |
out: |
| 1083 |
return; |
| 1084 |
} |
| 1085 |
|
| 1086 |
static void old_user_verifier_lookup_wrapper(char * username, |
| 1087 |
librist_verifier_lookup_data_t *lookup_data, |
| 1088 |
int *hashversion, |
| 1089 |
uint64_t *generation, |
| 1090 |
void *user_data) |
| 1091 |
{ |
| 1092 |
*hashversion = 0; |
| 1093 |
*generation = 0; |
| 1094 |
struct eapsrp_ctx *ctx = (struct eapsrp_ctx *)user_data; |
| 1095 |
ctx->config.lookup_func_old(username, &lookup_data->verifier_len,(char **)&lookup_data->verifier, &lookup_data->salt_len, (char **)&lookup_data->salt, &lookup_data->default_ng, &lookup_data->n_modulus_ascii, &lookup_data->generator_ascii, user_data); |
| 1096 |
} |
| 1097 |
|
| 1098 |
|
| 1099 |
int rist_enable_eap_srp_2(struct rist_peer *peer, const char *username, const char *password, user_verifier_lookup_2_t lookup_func, void *userdata) { |
| 1100 |
if (!peer) |
| 1101 |
return RIST_ERR_NULL_PEER; |
| 1102 |
struct rist_common_ctx *cctx = get_cctx(peer); |
| 1103 |
if (cctx->profile == RIST_PROFILE_SIMPLE) |
| 1104 |
return RIST_ERR_INVALID_PROFILE; |
| 1105 |
if ((peer->listening && !peer->multicast_receiver) || peer->multicast_sender) |
| 1106 |
{ |
| 1107 |
struct eapsrp_ctx *ctx = calloc(1, sizeof(*ctx)); |
| 1108 |
if (ctx == NULL) |
| 1109 |
return RIST_ERR_MALLOC; |
| 1110 |
ctx->config.logging_settings = get_cctx(peer)->logging_settings; |
| 1111 |
if (pthread_mutex_init(&ctx->eap_lock, NULL) != 0) { |
| 1112 |
free(ctx); |
| 1113 |
return RIST_ERR_MALLOC; |
| 1114 |
} |
| 1115 |
if (lookup_func == NULL && username != NULL && password != NULL) |
| 1116 |
{ |
| 1117 |
size_t u_len = strlen(username); |
| 1118 |
size_t p_len = strlen(password); |
| 1119 |
if (u_len == 0 || u_len > 255 || p_len == 0 || p_len > 255) { |
| 1120 |
free(ctx); |
| 1121 |
return RIST_ERR_INVALID_STRING_LENGTH; |
| 1122 |
} |
| 1123 |
lookup_func = internal_user_verifier_lookup; |
| 1124 |
const char *n = NULL; |
| 1125 |
const char *g = NULL; |
| 1126 |
int ret; |
| 1127 |
ret = librist_get_ng_constants(LIBRIST_SRP_NG_2048, &n, &g); |
| 1128 |
assert(ret == 0); |
| 1129 |
#if HAVE_MBEDTLS |
| 1130 |
ret = librist_crypto_srp_create_verifier(n, g, username, password, &ctx->authenticator_bytes_salt_old, &ctx->authenticator_len_salt_old, &ctx->authenticator_bytes_verifier_old, &ctx->authenticator_len_verifier_old, false); |
| 1131 |
assert(ret == 0); |
| 1132 |
#endif |
| 1133 |
ret = librist_crypto_srp_create_verifier(n, g, username, password, &ctx->authenticator_bytes_salt, &ctx->authenticator_len_salt, &ctx->authenticator_bytes_verifier, &ctx->authenticator_len_verifier, true); |
| 1134 |
assert(ret == 0); |
| 1135 |
strcpy(ctx->authenticator_username, username); |
| 1136 |
userdata = (void *)ctx; |
| 1137 |
rist_log_priv2(ctx->config.logging_settings, RIST_LOG_INFO, EAP_LOG_PREFIX"EAP Authentication enabled, role = authenticator, single user\n"); |
| 1138 |
} |
| 1139 |
else if (lookup_func == NULL) { |
| 1140 |
free(ctx); |
| 1141 |
return RIST_ERR_MISSING_CALLBACK_FUNCTION; |
| 1142 |
} |
| 1143 |
else |
| 1144 |
rist_log_priv2(ctx->config.logging_settings, RIST_LOG_INFO, EAP_LOG_PREFIX"EAP Authentication enabled, role = authenticator, srp file\n"); |
| 1145 |
ctx->config.lookup_func = lookup_func; |
| 1146 |
ctx->config.lookup_func_userdata = userdata; |
| 1147 |
ctx->config.role = EAP_ROLE_AUTHENTICATOR; |
| 1148 |
ctx->config.use_key_as_passphrase = peer->key_tx.password_len == 0; |
| 1149 |
ctx->eapversion3 = true; |
| 1150 |
ctx->srp_legacy_pad = (peer->config.srp_compat_legacy != 0); |
| 1151 |
if (ctx->srp_legacy_pad) |
| 1152 |
rist_log_priv2(ctx->config.logging_settings, RIST_LOG_WARN, |
| 1153 |
EAP_LOG_PREFIX"SRP legacy compat mode ACTIVE on this authenticator (srp-compat=1). Wire format is the pre-0.2.16 unpadded form — NOT TR-06-2 / RFC 5054 compliant. For transitional interop only.\n"); |
| 1154 |
peer->eap_ctx = ctx; |
| 1155 |
struct rist_peer *child = peer->child; |
| 1156 |
peer->eap_authentication_state = 1; |
| 1157 |
ctx->peer = peer; |
| 1158 |
while (child != NULL) |
| 1159 |
{ |
| 1160 |
eap_clone_ctx(ctx, child); |
| 1161 |
child = child->sibling_next; |
| 1162 |
} |
| 1163 |
return 0; |
| 1164 |
} |
| 1165 |
if (username == NULL || password == NULL) |
| 1166 |
return RIST_ERR_NULL_CREDENTIALS; |
| 1167 |
size_t u_len = strlen(username); |
| 1168 |
size_t p_len = strlen(password); |
| 1169 |
if (u_len == 0 || u_len > 255 || p_len == 0 || p_len > 255) |
| 1170 |
return RIST_ERR_INVALID_STRING_LENGTH; |
| 1171 |
struct eapsrp_ctx *ctx = calloc(1, sizeof(*ctx)); |
| 1172 |
if (ctx == NULL) |
| 1173 |
return RIST_ERR_MALLOC; |
| 1174 |
if (pthread_mutex_init(&ctx->eap_lock, NULL) != 0) { |
| 1175 |
free(ctx); |
| 1176 |
return RIST_ERR_MALLOC; |
| 1177 |
} |
| 1178 |
ctx->peer = peer; |
| 1179 |
ctx->config.logging_settings = get_cctx(peer)->logging_settings; |
| 1180 |
ctx->config.role = EAP_ROLE_AUTHENTICATEE; |
| 1181 |
strcpy(ctx->config.username, username); |
| 1182 |
strcpy(ctx->config.password, password); |
| 1183 |
ctx->srp_legacy_pad = (peer->config.srp_compat_legacy != 0); |
| 1184 |
peer->eap_ctx = ctx; |
| 1185 |
rist_log_priv2(ctx->config.logging_settings, RIST_LOG_INFO, EAP_LOG_PREFIX"EAP Authentication enabled, role = authenticatee\n"); |
| 1186 |
if (ctx->srp_legacy_pad) |
| 1187 |
rist_log_priv2(ctx->config.logging_settings, RIST_LOG_WARN, |
| 1188 |
EAP_LOG_PREFIX"SRP legacy compat mode ACTIVE on this client (srp-compat=1). Wire format is the pre-0.2.16 unpadded form — NOT TR-06-2 / RFC 5054 compliant. For transitional interop only.\n"); |
| 1189 |
ctx->eapversion3 = true; |
| 1190 |
if (!peer->multicast_receiver) |
| 1191 |
_librist_proto_eap_start(ctx); |
| 1192 |
return 0; |
| 1193 |
} |
| 1194 |
|
| 1195 |
int rist_enable_eap_srp(struct rist_peer *peer, const char *username, const char *password, user_verifier_lookup_t lookup_func, void *userdata) |
| 1196 |
{ |
| 1197 |
if (!peer) |
| 1198 |
return RIST_ERR_NULL_PEER; |
| 1199 |
|
| 1200 |
user_verifier_lookup_2_t pass_lookup = lookup_func? old_user_verifier_lookup_wrapper : NULL; |
| 1201 |
int ret = rist_enable_eap_srp_2(peer, username, password, pass_lookup, NULL); |
| 1202 |
if (ret == 0) { |
| 1203 |
peer->eap_ctx->config.lookup_func_old = lookup_func; |
| 1204 |
peer->eap_ctx->config.lookup_func_userdata = peer->eap_ctx; |
| 1205 |
peer->eap_ctx->config.lookup_func_userdata_old = userdata; |
| 1206 |
} |
| 1207 |
return ret; |
| 1208 |
} |
| 1209 |
|
| 1210 |
|
| 1211 |
bool rist_eap_password_sending_done(struct eapsrp_ctx *ctx) { |
| 1212 |
pthread_mutex_lock(&ctx->eap_lock); |
| 1213 |
bool success = ctx->unsollicited_passphrase_state >= EAP_PASSPHRASE_STATE_SUCCESS; |
| 1214 |
pthread_mutex_unlock(&ctx->eap_lock); |
| 1215 |
return success; |
| 1216 |
} |
| 1217 |
|
| 1218 |
bool rist_eap_may_rollover_tx(struct eapsrp_ctx *ctx) { |
| 1219 |
pthread_mutex_lock(&ctx->eap_lock); |
| 1220 |
bool rollover = ctx->may_rollover_passphrase; |
| 1221 |
ctx->may_rollover_passphrase = false; |
| 1222 |
pthread_mutex_unlock(&ctx->eap_lock); |
| 1223 |
return rollover; |
| 1224 |
} |
| 1225 |
|
| 1226 |
void rist_eap_send_passphrase(struct eapsrp_ctx *ctx, const char *passphrase) { |
| 1227 |
pthread_mutex_lock(&ctx->eap_lock); |
| 1228 |
size_t plen = strlen(passphrase); |
| 1229 |
if (plen > sizeof(ctx->unsollicited_passphrase)) { |
| 1230 |
rist_log_priv2(ctx->config.logging_settings, RIST_LOG_ERROR, |
| 1231 |
EAP_LOG_PREFIX"Passphrase too long (%zu > %zu), dropping\n", |
| 1232 |
plen, sizeof(ctx->unsollicited_passphrase)); |
| 1233 |
pthread_mutex_unlock(&ctx->eap_lock); |
| 1234 |
return; |
| 1235 |
} |
| 1236 |
ctx->unsollicited_passphrase_len = plen; |
| 1237 |
memcpy(ctx->unsollicited_passphrase, passphrase, plen); |
| 1238 |
ctx->unsollicited_passphrase_response_timer = timestampNTP_u64(); |
| 1239 |
ctx->unsollicited_passphrase_response_times = 1; |
| 1240 |
ctx->unsollicited_passphrase_response_identifier++; |
| 1241 |
ctx->unsollicited_passphrase_state = 0; |
| 1242 |
if (ctx->config.role == EAP_ROLE_AUTHENTICATOR) |
| 1243 |
SET_BIT(ctx->unsollicited_passphrase_response_identifier, 7); |
| 1244 |
UNSET_BIT(ctx->unsollicited_passphrase_response_identifier, 6); |
| 1245 |
eap_srp_send_password(ctx, ctx->unsollicited_passphrase_response_identifier, ctx->unsollicited_passphrase, ctx->unsollicited_passphrase_len); |
| 1246 |
pthread_mutex_unlock(&ctx->eap_lock); |
| 1247 |
} |