Go to the documentation of this file.
21 #define AUTH_TOKEN_SESSION_ID_LEN 12
22 #define AUTH_TOKEN_SESSION_ID_BASE64_LEN (AUTH_TOKEN_SESSION_ID_LEN * 8 / 6)
24 #if AUTH_TOKEN_SESSION_ID_LEN % 3
25 #error AUTH_TOKEN_SESSION_ID_LEN needs to be multiple a 3
29 #define TOKEN_DATA_LEN (2 * sizeof(int64_t) + AUTH_TOKEN_SESSION_ID_LEN + 32)
34 return create_kt(
"none",
"SHA256",
"auth-gen-token");
59 state =
"Authenticated";
67 state =
"AuthenticatedEmptyUser";
71 state =
"ExpiredEmptyUser";
87 const char *session_id_source;
131 bool key_loaded =
false;
136 key_file, key_inline);
146 msg(
M_FATAL,
"ERROR: Cannot load auth-token secret");
153 msg(
M_FATAL,
"ERROR: not enough data in auth-token secret");
165 int64_t timestamp =
htonll((uint64_t)
now);
166 int64_t initial_timestamp = timestamp;
179 char old_tstamp_decode[9];
191 old_tsamp_initial[12] =
'\0';
199 uint64_t *tstamp_ptr = (uint64_t *) old_tstamp_decode;
200 initial_timestamp = *tstamp_ptr;
202 old_tsamp_initial[0] =
'\0';
207 msg(
M_FATAL,
"Failed to get enough randomness for "
208 "authentication token");
216 uint8_t hmac_output[256/8];
235 hmac_ctx_update(ctx, (uint8_t *) &initial_timestamp,
sizeof(initial_timestamp));
244 ASSERT(
buf_write(&token, &initial_timestamp,
sizeof(initial_timestamp)));
248 char *b64output = NULL;
285 uint8_t hmac_output[256/8];
314 msg(
M_WARN,
"ERROR: --auth-token wrong size (%d!=%d)",
319 unsigned int ret = 0;
321 const uint8_t *sessid = b64decoded;
323 const uint8_t *tstamp = tstamp_initial +
sizeof(int64_t);
327 uint64_t timestamp = 0, timestamp_initial = 0;
328 memcpy(×tamp, tstamp,
sizeof(uint64_t));
329 timestamp =
ntohll(timestamp);
331 memcpy(×tamp_initial, tstamp_initial,
sizeof(uint64_t));
332 timestamp_initial =
ntohll(timestamp_initial);
348 msg(
M_WARN,
"--auth-gen-token: HMAC on token from client failed (%s)",
355 bool in_renegotiation_time =
now >= timestamp
356 &&
now < timestamp + 2 *
session->opt->auth_token_renewal;
358 if (!in_renegotiation_time)
360 msg(
M_WARN,
"Timestamp (%" PRIu64
") of auth-token is out of the renewal window",
366 if (timestamp < timestamp_initial)
368 msg(
M_WARN,
"Initial timestamp (%" PRIu64
") in token from client earlier than "
369 "current timestamp %" PRIu64
". Broken/unsynchronised clock?",
370 timestamp_initial, timestamp);
384 msg(
M_INFO,
"--auth-gen-token: auth-token from client expired");
395 msg(
M_WARN,
"--auth-gen-token: session id in token changed (Rejecting "
442 msg(
D_SHOW_KEYS,
"initial auth-token not generated yet, skipping "
443 "auth-token renewal.");
449 msg(
D_SHOW_KEYS,
"username not locked, skipping auth-token renewal.");
#define SESSION_ID_PREFIX
The prefix given to auth tokens start with, this prefix is special cased to not show up in log files ...
int openvpn_base64_decode(const char *str, void *data, int size)
#define S_GENERATED_KEYS
The data channel keys have been generated The TLS session is fully authenticated when reaching this s...
char * auth_token
If server sends a generated auth-token, this is the token to use for future user/pass authentications...
unsigned int auth_token_state_flags
The state of the auth-token sent from the client.
static bool buf_read(struct buffer *src, void *dest, int size)
void send_push_reply_auth_token(struct tls_multi *multi)
Sends a push reply message only containin the auth-token to update the auth-token on the client.
static struct gc_arena gc_new(void)
mbedtls_md_context_t hmac_ctx_t
Generic HMAC context.
unsigned int verify_auth_token(struct user_pass *up, struct tls_multi *multi, struct tls_session *session)
Verifies the auth token to be in the format that generate_auth_token create and checks if the token i...
struct tls_multi * tls_multi
TLS state structure for this VPN tunnel.
static bool is_auth_token(const char *password)
Return if the password string has the format of a password.
#define KS_PRIMARY
Primary key state index.
Contains all state information for one tunnel.
void resend_auth_token_renegotiation(struct tls_multi *multi, struct tls_session *session)
Checks if a client should be sent a new auth token to update its current auth-token.
struct tls_session session[TM_SIZE]
Array of tls_session objects representing control channel sessions with the remote peer.
void add_session_token_env(struct tls_session *session, struct tls_multi *multi, const struct user_pass *up)
Put the session id, and auth token status into the environment if auth-token is enabled.
struct key_ctx auth_token_key
char username[USER_PASS_LEN]
struct buffer alloc_buf_gc(size_t size, struct gc_arena *gc)
int openvpn_base64_encode(const void *data, int size, char **str)
static const struct key_state * get_primary_key(const struct tls_multi *multi)
gets an item of key_state objects in the order they should be scanned by data channel modules.
void hmac_ctx_update(hmac_ctx_t *ctx, const uint8_t *src, int src_len)
void check_send_auth_token(struct context *c)
Checks if the timer to resend the auth-token has expired and if a new auth-token should be send to th...
Security parameter state for a single VPN tunnel.
Security parameter state of one TLS and data channel key session.
Container for unidirectional cipher and HMAC key material.
bool auth_token_generate
Generate auth-tokens on successful user/pass auth,seet via options->auth_token_generate.
static bool check_hmac_token(hmac_ctx_t *ctx, const uint8_t *b64decoded, const char *username)
#define AUTH_TOKEN_VALID_EMPTYUSER
Auth-token is only valid for an empty username and not the username actually supplied from the client...
struct context_2 c2
Level 2 context.
#define TM_ACTIVE
Active tls_session.
char * string_alloc(const char *str, struct gc_arena *gc)
static void secure_memzero(void *data, size_t len)
Securely zeroise memory.
#define AUTH_TOKEN_SESSION_ID_LEN
static bool buf_write_u8(struct buffer *dest, uint8_t data)
struct key_state key[KS_SIZE]
void init_key_ctx(struct key_ctx *ctx, const struct key *key, const struct key_type *kt, int enc, const char *prefix)
Container for one set of cipher and/or HMAC contexts.
#define AUTH_TOKEN_SESSION_ID_BASE64_LEN
bool generate_ephemeral_key(struct buffer *key, const char *key_name)
Generate ephermal key material into the key structure.
void hmac_ctx_final(hmac_ctx_t *ctx, uint8_t *dst)
@ KS_AUTH_TRUE
Key state is authenticated.
void auth_token_init_secret(struct key_ctx *key_ctx, const char *key_file, bool key_inline)
Loads an HMAC secret from a file or if no file is present generates a epheremal secret for the run ti...
Wrapper structure for dynamically allocated memory.
void hmac_ctx_reset(hmac_ctx_t *ctx)
int rand_bytes(uint8_t *output, int len)
Wrapper for secure random number generator.
static struct key_type auth_token_kt(void)
static bool buf_write(struct buffer *dest, const void *src, size_t size)
void auth_token_write_server_key_file(const char *filename)
Generate a auth-token server secret key, and write to file.
Security parameter state of a single session within a VPN tunnel.
void generate_auth_token(const struct user_pass *up, struct tls_multi *multi)
Generate an auth token based on username and timestamp.
static struct key_type create_kt(const char *cipher, const char *md, const char *optname)
Creates and validates an instance of struct key_type with the provided algs.
const char * auth_token_pem_name
Garbage collection arena used to keep track of dynamically allocated memory.
void setenv_str(struct env_set *es, const char *name, const char *value)
static void strncpynt(char *dest, const char *src, size_t maxlen)
unsigned int auth_token_lifetime
void free_buf(struct buffer *buf)
int memcmp_constant_time(const void *a, const void *b, size_t size)
As memcmp(), but constant-time.
#define AUTH_TOKEN_HMAC_OK
Auth-token sent from client has valid hmac.
void write_pem_key_file(const char *filename, const char *pem_name)
Generate a server key with enough randomness to fill a key struct and write to file.
#define AUTH_TOKEN_EXPIRED
Auth-token sent from client has expired.
void auth_set_client_reason(struct tls_multi *multi, const char *client_reason)
Sets the reason why authentication of a client failed.
static void gc_free(struct gc_arena *a)
void wipe_auth_token(struct tls_multi *multi)
Wipes the authentication token out of the memory, frees and cleans up related buffers and flags.
bool read_pem_key_file(struct buffer *key, const char *pem_name, const char *key_file, bool key_inline)
Read key material from a PEM encoded files into the key structure.
char password[USER_PASS_LEN]
hmac_ctx_t * hmac
Generic HMAC context.
char * auth_token_initial
The first auth-token we sent to a client.
struct buffer alloc_buf(size_t size)
int hmac_ctx_size(hmac_ctx_t *ctx)