2 This file is part of GNUnet
3 Copyright (C) 2010-2015 GNUnet e.V.
5 GNUnet is free software: you can redistribute it and/or modify it
6 under the terms of the GNU Affero General Public License as published
7 by the Free Software Foundation, either version 3 of the License,
8 or (at your option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Affero General Public License for more details.
15 You should have received a copy of the GNU Affero General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
18 SPDX-License-Identifier: AGPL3.0-or-later
22 * @file reclaim/oidc_helper.c
23 * @brief helper library for OIDC related functions
24 * @author Martin Schanzenbach
30 #include "gnunet_util_lib.h"
32 #include "gnunet_reclaim_attribute_lib.h"
33 #include "gnunet_reclaim_service.h"
34 #include "gnunet_signatures.h"
35 #include "oidc_helper.h"
37 create_jwt_header (void)
42 root = json_object ();
43 json_object_set_new (root, JWT_ALG, json_string (JWT_ALG_VALUE));
44 json_object_set_new (root, JWT_TYP, json_string (JWT_TYP_VALUE));
46 json_str = json_dumps (root, JSON_INDENT (0) | JSON_COMPACT);
52 replace_char (char *str, char find, char replace)
54 char *current_pos = strchr (str, find);
57 *current_pos = replace;
58 current_pos = strchr (current_pos, find);
64 fix_base64 (char *str)
67 replace_char (str, '+', '-');
70 replace_char (str, '/', '_');
74 * Create a JWT from attributes
76 * @param aud_key the public of the audience
77 * @param sub_key the public key of the subject
78 * @param attrs the attribute list
79 * @param expiration_time the validity of the token
80 * @param secret_key the key used to sign the JWT
81 * @return a new base64-encoded JWT string.
84 OIDC_id_token_new (const struct GNUNET_CRYPTO_EcdsaPublicKey *aud_key,
85 const struct GNUNET_CRYPTO_EcdsaPublicKey *sub_key,
86 const struct GNUNET_RECLAIM_ATTRIBUTE_ClaimList *attrs,
87 const struct GNUNET_TIME_Relative *expiration_time,
89 const char *secret_key)
91 struct GNUNET_RECLAIM_ATTRIBUTE_ClaimListEntry *le;
92 struct GNUNET_HashCode signature;
93 struct GNUNET_TIME_Absolute exp_time;
94 struct GNUNET_TIME_Absolute time_now;
102 char *signature_target;
103 char *signature_base64;
107 // iat REQUIRED time now
108 time_now = GNUNET_TIME_absolute_get ();
109 // exp REQUIRED time expired from config
110 exp_time = GNUNET_TIME_absolute_add (time_now, *expiration_time);
111 // auth_time only if max_age
112 // nonce only if nonce
113 // OPTIONAL acr,amr,azp
114 subject = GNUNET_STRINGS_data_to_string_alloc (
116 sizeof (struct GNUNET_CRYPTO_EcdsaPublicKey));
117 audience = GNUNET_STRINGS_data_to_string_alloc (
119 sizeof (struct GNUNET_CRYPTO_EcdsaPublicKey));
120 header = create_jwt_header ();
121 body = json_object ();
123 // iss REQUIRED case sensitive server uri with https
124 // The issuer is the local reclaim instance (e.g.
125 // https://reclaim.id/api/openid)
126 json_object_set_new (body, "iss", json_string (SERVER_ADDRESS));
127 // sub REQUIRED public key identity, not exceed 255 ASCII length
128 json_object_set_new (body, "sub", json_string (subject));
129 // aud REQUIRED public key client_id must be there
130 json_object_set_new (body, "aud", json_string (audience));
132 json_object_set_new (body,
134 json_integer (time_now.abs_value_us / (1000 * 1000)));
136 json_object_set_new (body,
138 json_integer (exp_time.abs_value_us / (1000 * 1000)));
140 json_object_set_new (body,
142 json_integer (time_now.abs_value_us / (1000 * 1000)));
145 json_object_set_new (body, "nonce", json_string (nonce));
147 for (le = attrs->list_head; NULL != le; le = le->next)
150 GNUNET_RECLAIM_ATTRIBUTE_value_to_string (le->claim->type,
152 le->claim->data_size);
153 json_object_set_new (body, le->claim->name, json_string (attr_val_str));
154 GNUNET_free (attr_val_str);
156 body_str = json_dumps (body, JSON_INDENT (0) | JSON_COMPACT);
159 GNUNET_STRINGS_base64_encode (header, strlen (header), &header_base64);
160 fix_base64 (header_base64);
162 GNUNET_STRINGS_base64_encode (body_str, strlen (body_str), &body_base64);
163 fix_base64 (body_base64);
165 GNUNET_free (subject);
166 GNUNET_free (audience);
169 * Creating the JWT signature. This might not be
170 * standards compliant, check.
172 GNUNET_asprintf (&signature_target, "%s.%s", header_base64, body_base64);
173 GNUNET_CRYPTO_hmac_raw (secret_key,
176 strlen (signature_target),
178 GNUNET_STRINGS_base64_encode ((const char *) &signature,
179 sizeof (struct GNUNET_HashCode),
181 fix_base64 (signature_base64);
183 GNUNET_asprintf (&result,
189 GNUNET_free (signature_target);
190 GNUNET_free (header);
191 GNUNET_free (body_str);
192 GNUNET_free (signature_base64);
193 GNUNET_free (body_base64);
194 GNUNET_free (header_base64);
198 /* Converts a hex character to its integer value */
202 return isdigit (ch) ? ch - '0' : tolower (ch) - 'a' + 10;
205 /* Converts an integer value to its hex character*/
209 static char hex[] = "0123456789abcdef";
210 return hex[code & 15];
213 /* Returns a url-encoded version of str */
214 /* IMPORTANT: be sure to free() the returned string after use */
216 url_encode (const char *str)
218 char *pstr = (char *) str;
219 char *buf = GNUNET_malloc (strlen (str) * 3 + 1);
223 if (isalnum (*pstr) || *pstr == '-' || *pstr == '_' || *pstr == '.' ||
226 else if (*pstr == ' ')
231 *pbuf++ = to_hex (*pstr >> 4);
232 *pbuf++ = to_hex (*pstr & 15);
241 /* Returns a url-decoded version of str */
242 /* IMPORTANT: be sure to free() the returned string after use */
244 url_decode (const char *str)
246 char *pstr = (char *) str;
247 char *buf = GNUNET_malloc (strlen (str) + 1);
253 if (pstr[1] && pstr[2])
255 *pbuf++ = from_hex (pstr[1]) << 4 | from_hex (pstr[2]);
259 else if (*pstr == '+')
275 * Returns base64 encoded string urlencoded
277 * @param string the string to encode
278 * @return base64 encoded string
281 base64_encode (const char *data, size_t data_size)
286 GNUNET_STRINGS_base64_encode (data, data_size, &enc);
287 enc_urlencode = url_encode (enc);
289 return enc_urlencode;
294 * Builds an OIDC authorization code including
295 * a reclaim ticket and nonce
297 * @param issuer the issuer of the ticket, used to sign the ticket and nonce
298 * @param ticket the ticket to include in the code
299 * @param attrs list of attributes whicha re shared
300 * @param nonce the nonce to include in the code
301 * @return a new authorization code (caller must free)
304 OIDC_build_authz_code (const struct GNUNET_CRYPTO_EcdsaPrivateKey *issuer,
305 const struct GNUNET_RECLAIM_Ticket *ticket,
306 struct GNUNET_RECLAIM_ATTRIBUTE_ClaimList *attrs,
307 const char *nonce_str)
313 size_t signature_payload_len;
314 size_t attr_list_len;
315 size_t code_payload_len;
317 unsigned int nonce_tmp;
318 struct GNUNET_CRYPTO_EcdsaSignature signature;
319 struct GNUNET_CRYPTO_EccSignaturePurpose *purpose;
322 signature_payload_len =
323 sizeof (struct GNUNET_RECLAIM_Ticket) + sizeof (unsigned int);
326 attr_list_len = GNUNET_RECLAIM_ATTRIBUTE_list_serialize_get_size (attrs);
327 signature_payload_len += attr_list_len;
328 attrs_ser = GNUNET_malloc (attr_list_len);
329 GNUNET_RECLAIM_ATTRIBUTE_list_serialize (attrs, attrs_ser);
331 code_payload_len = sizeof (struct GNUNET_CRYPTO_EccSignaturePurpose) +
332 signature_payload_len + sizeof (signature);
333 code_payload = GNUNET_malloc (code_payload_len);
334 purpose = (struct GNUNET_CRYPTO_EccSignaturePurpose *) code_payload;
335 purpose->size = htonl (sizeof (struct GNUNET_CRYPTO_EccSignaturePurpose) +
336 signature_payload_len);
337 purpose->purpose = htonl (GNUNET_SIGNATURE_PURPOSE_RECLAIM_CODE_SIGN);
338 // First, copy ticket
339 buf_ptr = (char *) &purpose[1];
340 memcpy (buf_ptr, ticket, sizeof (struct GNUNET_RECLAIM_Ticket));
341 buf_ptr += sizeof (struct GNUNET_RECLAIM_Ticket);
344 if (NULL != nonce_str)
346 if ((1 != SSCANF (nonce_str, "%u", &nonce)) || (nonce > UINT32_MAX))
349 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
350 "Invalid nonce %s\n", nonce_str);
351 GNUNET_free (code_payload);
352 GNUNET_free_non_null (attrs_ser);
356 nonce_tmp = htons (nonce);
357 memcpy (buf_ptr, &nonce_tmp, sizeof (unsigned int));
358 buf_ptr += sizeof (unsigned int);
359 // Finally, attributes
360 if (NULL != attrs_ser)
362 memcpy (buf_ptr, attrs_ser, attr_list_len);
363 buf_ptr += attr_list_len;
365 if (GNUNET_SYSERR == GNUNET_CRYPTO_ecdsa_sign (issuer, purpose, &signature))
368 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
369 "Unable to sign code\n");
370 GNUNET_free (code_payload);
371 GNUNET_free_non_null (attrs_ser);
374 memcpy (buf_ptr, &signature, sizeof (signature));
375 code_str = base64_encode ((const char *) &code_payload, code_payload_len);
376 GNUNET_free (code_payload);
377 GNUNET_free_non_null (attrs_ser);
383 * Parse reclaim ticket and nonce from
384 * authorization code.
385 * This also verifies the signature in the code.
387 * @param audience the expected audience of the code
388 * @param code the string representation of the code
389 * @param ticket where to store the ticket
390 * @param attrs the attributes in the code
391 * @param nonce where to store the nonce
392 * @return GNUNET_OK if successful, else GNUNET_SYSERR
395 OIDC_parse_authz_code (const struct GNUNET_CRYPTO_EcdsaPublicKey *audience,
397 struct GNUNET_RECLAIM_Ticket *ticket,
398 struct GNUNET_RECLAIM_ATTRIBUTE_ClaimList **attrs,
404 struct GNUNET_CRYPTO_EccSignaturePurpose *purpose;
405 struct GNUNET_CRYPTO_EcdsaSignature *signature;
406 size_t code_payload_len;
407 size_t attrs_ser_len;
408 size_t signature_offset;
411 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
412 "Trying to decode `%s'", code);
415 GNUNET_STRINGS_base64_decode (code, strlen (code), (void **) &code_payload);
416 purpose = (struct GNUNET_CRYPTO_EccSignaturePurpose *) code_payload;
417 attrs_ser_len = code_payload_len;
418 attrs_ser_len -= sizeof (struct GNUNET_CRYPTO_EccSignaturePurpose);
419 *ticket = *((struct GNUNET_RECLAIM_Ticket *) &purpose[1]);
420 attrs_ser_len -= sizeof (struct GNUNET_RECLAIM_Ticket);
421 nonce = ntohs (*((unsigned int *) &ticket[1]));
422 attrs_ser_len -= sizeof (unsigned int);
425 code_payload_len - sizeof (struct GNUNET_CRYPTO_EcdsaSignature);
426 signature = (struct GNUNET_CRYPTO_EcdsaSignature *) &ptr[signature_offset];
427 attrs_ser_len -= sizeof (struct GNUNET_CRYPTO_EcdsaSignature);
428 attrs_ser = ((char *) &ticket[1]) + sizeof (unsigned int);
429 *attrs = GNUNET_RECLAIM_ATTRIBUTE_list_deserialize (attrs_ser, attrs_ser_len);
430 if (0 != GNUNET_memcmp (audience, &ticket->audience))
432 GNUNET_RECLAIM_ATTRIBUTE_list_destroy (*attrs);
433 GNUNET_free (code_payload);
434 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
435 "Audience in ticket does not match client!\n");
436 return GNUNET_SYSERR;
439 GNUNET_CRYPTO_ecdsa_verify (GNUNET_SIGNATURE_PURPOSE_RECLAIM_CODE_SIGN,
444 GNUNET_RECLAIM_ATTRIBUTE_list_destroy (*attrs);
445 GNUNET_free (code_payload);
446 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Signature of AuthZ code invalid!\n");
447 return GNUNET_SYSERR;
451 GNUNET_asprintf (nonce_str, "%u", nonce);
457 * Build a token response for a token request
458 * TODO: Maybe we should add the scope here?
460 * @param access_token the access token to include
461 * @param id_token the id_token to include
462 * @param expiration_time the expiration time of the token(s)
463 * @param token_response where to store the response
466 OIDC_build_token_response (const char *access_token,
467 const char *id_token,
468 const struct GNUNET_TIME_Relative *expiration_time,
469 char **token_response)
473 root_json = json_object ();
475 GNUNET_assert (NULL != access_token);
476 GNUNET_assert (NULL != id_token);
477 GNUNET_assert (NULL != expiration_time);
478 json_object_set_new (root_json, "access_token", json_string (access_token));
479 json_object_set_new (root_json, "token_type", json_string ("Bearer"));
480 json_object_set_new (
483 json_integer (expiration_time->rel_value_us / (1000 * 1000)));
484 json_object_set_new (root_json, "id_token", json_string (id_token));
485 *token_response = json_dumps (root_json, JSON_INDENT (0) | JSON_COMPACT);
486 json_decref (root_json);
490 * Generate a new access token
493 OIDC_access_token_new ()
495 char *access_token_number;
497 uint64_t random_number;
500 GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_NONCE, UINT64_MAX);
501 GNUNET_asprintf (&access_token_number, "%" PRIu64, random_number);
502 GNUNET_STRINGS_base64_encode (access_token_number,
503 strlen (access_token_number),