OpenVPN
httpdigest.c
Go to the documentation of this file.
1/*
2 * OpenVPN -- An application to securely tunnel IP networks
3 * over a single TCP/UDP port, with support for SSL/TLS-based
4 * session authentication and key exchange,
5 * packet encryption, packet authentication, and
6 * packet compression.
7 *
8 * Copyright (C) 2002-2026 OpenVPN Inc <sales@openvpn.net>
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License version 2
12 * as published by the Free Software Foundation.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, see <https://www.gnu.org/licenses/>.
21 */
22
23#ifdef HAVE_CONFIG_H
24#include "config.h"
25#endif
26
27#include "syshead.h"
28
29#include "crypto.h"
30#include "httpdigest.h"
31
32static void
33CvtHex(const HASH Bin, HASHHEX Hex)
34{
35 for (unsigned short i = 0; i < HASHLEN; i++)
36 {
37 unsigned char j = (Bin[i] >> 4) & 0xf;
38 if (j <= 9)
39 {
40 Hex[i * 2] = (j + '0');
41 }
42 else
43 {
44 Hex[i * 2] = (unsigned char)(j + 'a' - 10);
45 }
46 j = Bin[i] & 0xf;
47 if (j <= 9)
48 {
49 Hex[i * 2 + 1] = (j + '0');
50 }
51 else
52 {
53 Hex[i * 2 + 1] = (unsigned char)(j + 'a' - 10);
54 }
55 }
56 Hex[HASHHEXLEN] = '\0';
57}
58
59/* calculate H(A1) as per spec */
60void
61DigestCalcHA1(const char *pszAlg, const char *pszUserName, const char *pszRealm, const char *pszPassword,
62 const char *pszNonce, const char *pszCNonce, HASHHEX SessionKey)
63{
64 HASH HA1;
65 md_ctx_t *md5_ctx = md_ctx_new();
66
67 md_ctx_init(md5_ctx, "MD5");
68 md_ctx_update(md5_ctx, (const uint8_t *)pszUserName, strlen(pszUserName));
69 md_ctx_update(md5_ctx, (const uint8_t *)":", 1);
70 md_ctx_update(md5_ctx, (const uint8_t *)pszRealm, strlen(pszRealm));
71 md_ctx_update(md5_ctx, (const uint8_t *)":", 1);
72 md_ctx_update(md5_ctx, (const uint8_t *)pszPassword, strlen(pszPassword));
73 md_ctx_final(md5_ctx, HA1);
74 if (pszAlg && strcasecmp(pszAlg, "md5-sess") == 0)
75 {
76 md_ctx_init(md5_ctx, "MD5");
77 md_ctx_update(md5_ctx, HA1, HASHLEN);
78 md_ctx_update(md5_ctx, (const uint8_t *)":", 1);
79 md_ctx_update(md5_ctx, (const uint8_t *)pszNonce, strlen(pszNonce));
80 md_ctx_update(md5_ctx, (const uint8_t *)":", 1);
81 md_ctx_update(md5_ctx, (const uint8_t *)pszCNonce, strlen(pszCNonce));
82 md_ctx_final(md5_ctx, HA1);
83 }
84 md_ctx_cleanup(md5_ctx);
85 md_ctx_free(md5_ctx);
86 CvtHex(HA1, SessionKey);
87}
88
89/* calculate request-digest/response-digest as per HTTP Digest spec */
90void
91DigestCalcResponse(const HASHHEX HA1, /* H(A1) */
92 const char *pszNonce, /* nonce from server */
93 const char *pszNonceCount, /* 8 hex digits */
94 const char *pszCNonce, /* client nonce */
95 const char *pszQop, /* qop-value: "", "auth", "auth-int" */
96 const char *pszMethod, /* method from the request */
97 const char *pszDigestUri, /* requested URL */
98 const HASHHEX HEntity, /* H(entity body) if qop="auth-int" */
99 HASHHEX Response /* request-digest or response-digest */
100)
101{
102 HASH HA2;
103 HASH RespHash;
104 HASHHEX HA2Hex;
105
106 md_ctx_t *md5_ctx = md_ctx_new();
107
108 /* calculate H(A2) */
109 md_ctx_init(md5_ctx, "MD5");
110 md_ctx_update(md5_ctx, (const uint8_t *)pszMethod, strlen(pszMethod));
111 md_ctx_update(md5_ctx, (const uint8_t *)":", 1);
112 md_ctx_update(md5_ctx, (const uint8_t *)pszDigestUri, strlen(pszDigestUri));
113 if (strcasecmp(pszQop, "auth-int") == 0)
114 {
115 md_ctx_update(md5_ctx, (const uint8_t *)":", 1);
116 md_ctx_update(md5_ctx, HEntity, HASHHEXLEN);
117 }
118 md_ctx_final(md5_ctx, HA2);
119 CvtHex(HA2, HA2Hex);
120
121 /* calculate response */
122 md_ctx_init(md5_ctx, "MD5");
123 md_ctx_update(md5_ctx, HA1, HASHHEXLEN);
124 md_ctx_update(md5_ctx, (const uint8_t *)":", 1);
125 md_ctx_update(md5_ctx, (const uint8_t *)pszNonce, strlen(pszNonce));
126 md_ctx_update(md5_ctx, (const uint8_t *)":", 1);
127 if (*pszQop)
128 {
129 md_ctx_update(md5_ctx, (const uint8_t *)pszNonceCount, strlen(pszNonceCount));
130 md_ctx_update(md5_ctx, (const uint8_t *)":", 1);
131 md_ctx_update(md5_ctx, (const uint8_t *)pszCNonce, strlen(pszCNonce));
132 md_ctx_update(md5_ctx, (const uint8_t *)":", 1);
133 md_ctx_update(md5_ctx, (const uint8_t *)pszQop, strlen(pszQop));
134 md_ctx_update(md5_ctx, (const uint8_t *)":", 1);
135 }
136 md_ctx_update(md5_ctx, HA2Hex, HASHHEXLEN);
137 md_ctx_final(md5_ctx, RespHash);
138 md_ctx_cleanup(md5_ctx);
139 md_ctx_free(md5_ctx);
140 CvtHex(RespHash, Response);
141}
Data Channel Cryptography Module.
md_ctx_t * md_ctx_new(void)
void md_ctx_update(md_ctx_t *ctx, const uint8_t *src, size_t src_len)
void md_ctx_cleanup(md_ctx_t *ctx)
void md_ctx_final(md_ctx_t *ctx, uint8_t *dst)
void md_ctx_init(md_ctx_t *ctx, const char *mdname)
Initialises the given message digest context.
void md_ctx_free(md_ctx_t *ctx)
static void CvtHex(const HASH Bin, HASHHEX Hex)
Definition httpdigest.c:33
void DigestCalcResponse(const HASHHEX HA1, const char *pszNonce, const char *pszNonceCount, const char *pszCNonce, const char *pszQop, const char *pszMethod, const char *pszDigestUri, const HASHHEX HEntity, HASHHEX Response)
Definition httpdigest.c:91
void DigestCalcHA1(const char *pszAlg, const char *pszUserName, const char *pszRealm, const char *pszPassword, const char *pszNonce, const char *pszCNonce, HASHHEX SessionKey)
Definition httpdigest.c:61
#define HASHLEN
Definition httpdigest.h:23
#define HASHHEXLEN
Definition httpdigest.h:25
unsigned char HASHHEX[HASHHEXLEN+1]
Definition httpdigest.h:26
unsigned char HASH[HASHLEN]
Definition httpdigest.h:24