Coverage Report

Created: 2026-10-15 06:31

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/FreeRDP/client/common/oauth2.c
Line
Count
Source
1
/**
2
 * FreeRDP: A Remote Desktop Protocol Implementation
3
 * OAuth2 state handling
4
 *
5
 * Copyright 2026 Armin Novak <anovak@thincast.com>
6
 * Copyright 2026 Thincast Technologies GmbH
7
 *
8
 * Licensed under the Apache License, Version 2.0 (the "License");
9
 * you may not use this file except in compliance with the License.
10
 * You may obtain a copy of the License at
11
 *
12
 *     http://www.apache.org/licenses/LICENSE-2.0
13
 *
14
 * Unless required by applicable law or agreed to in writing, software
15
 * distributed under the License is distributed on an "AS IS" BASIS,
16
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17
 * See the License for the specific language governing permissions and
18
 * limitations under the License.
19
 */
20
#include "oauth2.h"
21
22
#include <winpr/string.h>
23
#include <winpr/crypto.h>
24
#include <winpr/print.h>
25
#include <freerdp/crypto/crypto.h>
26
27
#include <freerdp/log.h>
28
29
0
#define TAG CLIENT_TAG("common.oauth2")
30
31
enum pkcs_state
32
{
33
  INVALID_STATE = 0,
34
  INITIAL_CHALLENGE,
35
  TOKEN_VERIFIER
36
};
37
38
struct rdp_client_oauth2
39
{
40
  wLog* log;
41
  const char* pkcsChallengeMethod;
42
  enum pkcs_state pkcsstate;
43
  bool valid;
44
  char* state;
45
  size_t state_len;
46
  char* code_verifier;
47
  size_t code_verifier_len;
48
  char* code_challenge;
49
  size_t code_challenge_len;
50
};
51
52
WINPR_ATTR_MALLOC(free, 1)
53
static char* rfc7636_generate_code_verifier(size_t* plen)
54
0
{
55
0
  WINPR_ASSERT(plen);
56
0
  *plen = 0;
57
58
0
  BYTE random[32] = WINPR_C_ARRAY_INIT;
59
0
  if (winpr_RAND(random, sizeof(random)) < 0)
60
0
    return nullptr;
61
0
  char* str = crypto_base64url_encode_len(random, sizeof(random), plen);
62
0
  if (!str)
63
0
    return nullptr;
64
0
  return str;
65
0
}
66
67
WINPR_ATTR_MALLOC(free, 1)
68
static char* rfc7636_generate_code_challenge(const char* method, const char* code_verifier,
69
                                             size_t len, size_t* plen)
70
0
{
71
0
  WINPR_ASSERT(plen);
72
0
  *plen = 0;
73
74
0
  if (!code_verifier || (len == 0) || !method)
75
0
    return nullptr;
76
77
0
  if (strcmp("plain", method) == 0)
78
0
  {
79
0
    char* str = strndup(code_verifier, len);
80
0
    if (!str)
81
0
      return nullptr;
82
0
    *plen = len;
83
0
    return str;
84
0
  }
85
86
0
  if (strcmp("S256", method) == 0)
87
0
  {
88
0
    BYTE hash[WINPR_SHA256_DIGEST_LENGTH] = WINPR_C_ARRAY_INIT;
89
0
    if (!winpr_Digest(WINPR_MD_SHA256, code_verifier, len, hash, sizeof(hash)))
90
0
      return nullptr;
91
0
    char* str = crypto_base64url_encode_len(hash, sizeof(hash), plen);
92
0
    if (!str)
93
0
      return nullptr;
94
0
    return str;
95
0
  }
96
97
0
  return nullptr;
98
0
}
99
100
WINPR_ATTR_MALLOC(free, 1)
101
static char* rfc6749_generate_state(size_t* plen)
102
0
{
103
0
  WINPR_ASSERT(plen);
104
0
  *plen = 0;
105
0
  BYTE random[32] = WINPR_C_ARRAY_INIT;
106
0
  if (winpr_RAND(random, sizeof(random)) < 0)
107
0
    return nullptr;
108
0
  char* str = winpr_BinToHexString(random, sizeof(random), FALSE);
109
0
  if (str)
110
0
    *plen = strlen(str);
111
0
  return str;
112
0
}
113
114
static void oauth2_free(rdpClientOAuth2* oauth2)
115
0
{
116
0
  if (!oauth2)
117
0
    return;
118
119
0
  oauth2->valid = false;
120
121
0
  oauth2->state_len = 0;
122
0
  free(oauth2->state);
123
0
  oauth2->state = nullptr;
124
125
0
  oauth2->code_verifier_len = 0;
126
0
  free(oauth2->code_verifier);
127
0
  oauth2->code_verifier = nullptr;
128
129
0
  oauth2->code_challenge_len = 0;
130
0
  free(oauth2->code_challenge);
131
0
  oauth2->code_challenge = nullptr;
132
0
}
133
134
void freerdp_oauth2_free(rdpClientOAuth2* oauth2)
135
0
{
136
0
  oauth2_free(oauth2);
137
0
  free(oauth2);
138
0
}
139
140
rdpClientOAuth2* freerdp_oauth2_new(void)
141
0
{
142
0
  rdpClientOAuth2* oauth2 = calloc(1, sizeof(rdpClientOAuth2));
143
0
  if (!oauth2)
144
0
    return nullptr;
145
0
  oauth2->log = WLog_Get(TAG);
146
0
  oauth2->pkcsChallengeMethod = "S256"; // alternatively "plain"
147
0
  WINPR_ASSERT(oauth2->log);
148
149
0
  return oauth2;
150
0
}
151
152
BOOL freerdp_oauth2_reset(rdpClientOAuth2* oauth2)
153
0
{
154
0
  WINPR_ASSERT(oauth2);
155
0
  oauth2_free(oauth2);
156
157
0
  oauth2->state = rfc6749_generate_state(&oauth2->state_len);
158
0
  if (!oauth2->state || (oauth2->state_len == 0))
159
0
    return FALSE;
160
161
0
  oauth2->code_verifier = rfc7636_generate_code_verifier(&oauth2->code_verifier_len);
162
0
  if (!oauth2->code_verifier || (oauth2->code_verifier_len == 0))
163
0
    return FALSE;
164
0
  oauth2->code_challenge =
165
0
      rfc7636_generate_code_challenge(oauth2->pkcsChallengeMethod, oauth2->code_verifier,
166
0
                                      oauth2->code_verifier_len, &oauth2->code_challenge_len);
167
0
  if (!oauth2->code_challenge || (oauth2->code_challenge_len == 0))
168
0
    return FALSE;
169
170
0
  oauth2->pkcsstate = INITIAL_CHALLENGE;
171
0
  return TRUE;
172
0
}
173
174
BOOL freerdp_oauth2_check_return_valid(rdpClientOAuth2* oauth2, const char* response, size_t len)
175
0
{
176
0
  WINPR_ASSERT(oauth2);
177
0
  if (!oauth2->valid)
178
0
  {
179
0
    WLog_Print(oauth2->log, WLOG_WARN,
180
0
               "No OAuth2 request generated, but we have a response. "
181
0
               "Discarding response.");
182
0
    return FALSE;
183
0
  }
184
185
  // We did use a state parameter, so check it is there in the response.
186
0
  if (oauth2->state && (oauth2->state_len > 0))
187
0
  {
188
0
    WLog_Print(oauth2->log, WLOG_DEBUG,
189
0
               "OAuth2 state parameter used in request, checking response for mirrored value");
190
    // Check if there is a state argument and it must match the one in the request.
191
0
    const char* state = winpr_strnstr(response, "state=", len);
192
0
    if (!state)
193
0
    {
194
0
      WLog_Print(oauth2->log, WLOG_WARN,
195
0
                 "OAuth2 state parameter used in request, but missing in response. "
196
0
                 "Discarding response.");
197
0
      return FALSE;
198
0
    }
199
0
    if (strncmp(oauth2->state, &state[6], oauth2->state_len) != 0)
200
0
    {
201
0
      WLog_Print(oauth2->log, WLOG_WARN,
202
0
                 "OAuth2 state parameter used in request, but does not match parameter value "
203
0
                 "in response. Discarding response.");
204
0
      return FALSE;
205
0
    }
206
0
  }
207
0
  else
208
0
    WLog_Print(oauth2->log, WLOG_DEBUG,
209
0
               "OAuth2 state parameter was not used in request, skipping response check.");
210
211
0
  return TRUE;
212
0
}
213
214
char* freerdp_oauth2_append_state(rdpClientOAuth2* oauth2, const char* url, size_t len,
215
                                  size_t* plen)
216
0
{
217
0
  WINPR_ASSERT(oauth2);
218
219
0
  if (plen)
220
0
    *plen = 0;
221
222
0
  if (!url || (len == 0))
223
0
    return nullptr;
224
225
0
  if (strnlen(url, len + 1) > len)
226
0
    return nullptr;
227
228
0
  char* safeurl = nullptr;
229
0
  size_t safeurllen = 0;
230
0
  if (oauth2->pkcsChallengeMethod)
231
0
  {
232
0
    switch (oauth2->pkcsstate)
233
0
    {
234
0
      case INITIAL_CHALLENGE:
235
0
      {
236
0
        winpr_asprintf(&safeurl, &safeurllen,
237
0
                       "%s&state=%s&code_challenge=%s&code_challenge_method=%s", url,
238
0
                       oauth2->state, oauth2->code_challenge, oauth2->pkcsChallengeMethod);
239
0
        oauth2->pkcsstate = TOKEN_VERIFIER;
240
0
      }
241
0
      break;
242
0
      case TOKEN_VERIFIER:
243
0
      {
244
0
        winpr_asprintf(&safeurl, &safeurllen, "%s&state=%s&code_verifier=%s", url,
245
0
                       oauth2->state, oauth2->code_verifier);
246
0
        oauth2->pkcsstate = INVALID_STATE;
247
0
      }
248
0
      break;
249
0
      default:
250
0
        WLog_Print(
251
0
            oauth2->log, WLOG_ERROR,
252
0
            "Invalid pkcs state. OAuth2 call sequence of your client is wrong. Aborting.");
253
0
        return nullptr;
254
0
    }
255
0
  }
256
0
  else
257
0
    winpr_asprintf(&safeurl, &safeurllen, "%s&state=%s", url, oauth2->state);
258
259
0
  oauth2->valid = true;
260
0
  if (plen)
261
0
    *plen = safeurllen;
262
0
  return safeurl;
263
0
}
264
265
char* freerdp_oauth2_extract_code(rdpClientOAuth2* oauth2, const char* response, size_t len)
266
0
{
267
0
  if (!freerdp_oauth2_check_return_valid(oauth2, response, len))
268
0
    return nullptr;
269
270
0
  const char* token = winpr_strnstr(response, "code=", len);
271
0
  if (!token)
272
0
    return nullptr;
273
274
0
  const char* start = &token[5];
275
0
  const size_t olen = WINPR_ASSERTING_INT_CAST(size_t, start - response);
276
0
  if (olen > len)
277
0
    return nullptr;
278
0
  const size_t rlen = len - olen;
279
0
  char* str = strndup(start, rlen);
280
0
  if (!str)
281
0
    return nullptr;
282
0
  char* end = winpr_strnstr(str, "&", rlen);
283
0
  if (end)
284
0
    *end = '\0';
285
0
  return str;
286
0
}