Skip to content

Commit bc084da

Browse files
rojercesantabot
authored andcommitted
Add SHA256 support for digest auth
https://tools.ietf.org/html/rfc7616 PUBLISHED_FROM=c2f045511b68d7dbcf4765cf32a312361a966eeb
1 parent ba4b8ac commit bc084da

File tree

1 file changed

+40
-6
lines changed

1 file changed

+40
-6
lines changed

include/mongoose.h

Lines changed: 40 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4704,13 +4704,16 @@ extern void mg_hash_md5_v(size_t num_msgs, const uint8_t *msgs[],
47044704
const size_t *msg_lens, uint8_t *digest);
47054705
extern void mg_hash_sha1_v(size_t num_msgs, const uint8_t *msgs[],
47064706
const size_t *msg_lens, uint8_t *digest);
4707+
extern void mg_hash_sha256_v(size_t num_msgs, const uint8_t *msgs[],
4708+
const size_t *msg_lens, uint8_t *digest);
47074709

47084710
/*
47094711
* Flags for `mg_http_is_authorized()`.
47104712
*/
47114713
#define MG_AUTH_FLAG_IS_DIRECTORY (1 << 0)
47124714
#define MG_AUTH_FLAG_IS_GLOBAL_PASS_FILE (1 << 1)
47134715
#define MG_AUTH_FLAG_ALLOW_MISSING_FILE (1 << 2)
4716+
#define MG_AUTH_FLAG_ALGO(algo) ((((int) algo) & 3) << 8)
47144717

47154718
/*
47164719
* Checks whether an http request is authorized. `domain` is the authentication
@@ -4723,12 +4726,6 @@ int mg_http_is_authorized(struct http_message *hm, struct mg_str path,
47234726
const char *domain, const char *passwords_file,
47244727
int flags);
47254728

4726-
/*
4727-
* Sends 401 Unauthorized response.
4728-
*/
4729-
void mg_http_send_digest_auth_request(struct mg_connection *c,
4730-
const char *domain);
4731-
47324729
#ifdef __cplusplus
47334730
}
47344731
#endif /* __cplusplus */
@@ -4879,6 +4876,14 @@ size_t mg_parse_multipart(const char *buf, size_t buf_len, char *var_name,
48794876
int mg_get_http_var(const struct mg_str *buf, const char *name, char *dst,
48804877
size_t dst_len);
48814878

4879+
/*
4880+
* Supported digest auth algorithms.
4881+
*/
4882+
enum mg_auth_algo {
4883+
MG_AUTH_ALGO_MD5 = 0,
4884+
MG_AUTH_ALGO_SHA256 = 1,
4885+
};
4886+
48824887
#if MG_ENABLE_FILESYSTEM
48834888
/*
48844889
* This structure defines how `mg_serve_http()` works.
@@ -4915,6 +4920,11 @@ struct mg_serve_http_opts {
49154920
*/
49164921
const char *global_auth_file;
49174922

4923+
/*
4924+
* Password hashing algorithm used by the password files.
4925+
*/
4926+
enum mg_auth_algo auth_algo;
4927+
49184928
/* Set to "no" to disable directory listing. Enabled by default. */
49194929
const char *enable_directory_listing;
49204930

@@ -5166,19 +5176,31 @@ struct mg_http_endpoint_opts {
51665176
/* Authorization domain (realm) */
51675177
const char *auth_domain;
51685178
const char *auth_file;
5179+
enum mg_auth_algo auth_algo;
51695180
};
51705181

51715182
void mg_register_http_endpoint_opt(struct mg_connection *nc,
51725183
const char *uri_path,
51735184
mg_event_handler_t handler,
51745185
struct mg_http_endpoint_opts opts);
51755186

5187+
/*
5188+
* Sends 401 Unauthorized response.
5189+
*/
5190+
void mg_http_send_digest_auth_request(struct mg_connection *c,
5191+
const char *domain);
5192+
void mg_http_send_digest_auth_request_algo(struct mg_connection *c,
5193+
const char *domain,
5194+
enum mg_auth_algo algo);
5195+
51765196
/*
51775197
* Authenticates a HTTP request against an opened password file.
51785198
* Returns 1 if authenticated, 0 otherwise.
51795199
*/
51805200
int mg_http_check_digest_auth(struct http_message *hm, const char *auth_domain,
51815201
FILE *fp);
5202+
int mg_http_check_digest_auth_algo(struct http_message *hm, const char *auth_domain,
5203+
enum mg_auth_algo fp_algo, FILE *fp);
51825204

51835205
/*
51845206
* Authenticates given response params against an opened password file.
@@ -5191,6 +5213,12 @@ int mg_check_digest_auth(struct mg_str method, struct mg_str uri,
51915213
struct mg_str response, struct mg_str qop,
51925214
struct mg_str nc, struct mg_str nonce,
51935215
struct mg_str auth_domain, FILE *fp);
5216+
int mg_check_digest_auth_algo(struct mg_str method, struct mg_str uri,
5217+
struct mg_str username, struct mg_str cnonce,
5218+
struct mg_str response, struct mg_str qop,
5219+
struct mg_str nc, struct mg_str nonce,
5220+
struct mg_str auth_domain, enum mg_auth_algo algo,
5221+
FILE *fp);
51945222

51955223
/*
51965224
* Sends buffer `buf` of size `len` to the client using chunked HTTP encoding.
@@ -5360,6 +5388,12 @@ int mg_http_create_digest_auth_header(char *buf, size_t buf_len,
53605388
const char *method, const char *uri,
53615389
const char *auth_domain, const char *user,
53625390
const char *passwd, const char *nonce);
5391+
int mg_http_create_digest_auth_header_algo(char *buf, size_t buf_len,
5392+
const char *method, const char *uri,
5393+
const char *auth_domain,
5394+
const char *user, const char *passwd,
5395+
const char *nonce,
5396+
enum mg_auth_algo algo);
53635397

53645398
#ifdef __cplusplus
53655399
}

0 commit comments

Comments
 (0)