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
115 GNUNET_STRINGS_data_to_string_alloc (sub_key,
117 GNUNET_CRYPTO_EcdsaPublicKey));
119 GNUNET_STRINGS_data_to_string_alloc (aud_key,
121 GNUNET_CRYPTO_EcdsaPublicKey));
122 header = create_jwt_header ();
123 body = json_object ();
125 // iss REQUIRED case sensitive server uri with https
126 // The issuer is the local reclaim instance (e.g.
127 // https://reclaim.id/api/openid)
128 json_object_set_new (body, "iss", json_string (SERVER_ADDRESS));
129 // sub REQUIRED public key identity, not exceed 255 ASCII length
130 json_object_set_new (body, "sub", json_string (subject));
131 // aud REQUIRED public key client_id must be there
132 json_object_set_new (body, "aud", json_string (audience));
134 json_object_set_new (body,
136 json_integer (time_now.abs_value_us / (1000 * 1000)));
138 json_object_set_new (body,
140 json_integer (exp_time.abs_value_us / (1000 * 1000)));
142 json_object_set_new (body,
144 json_integer (time_now.abs_value_us / (1000 * 1000)));
147 json_object_set_new (body, "nonce", json_string (nonce));
149 for (le = attrs->list_head; NULL != le; le = le->next)
152 GNUNET_RECLAIM_ATTRIBUTE_value_to_string (le->claim->type,
154 le->claim->data_size);
155 json_object_set_new (body, le->claim->name, json_string (attr_val_str));
156 GNUNET_free (attr_val_str);
158 body_str = json_dumps (body, JSON_INDENT (0) | JSON_COMPACT);
161 GNUNET_STRINGS_base64_encode (header, strlen (header), &header_base64);
162 fix_base64 (header_base64);
164 GNUNET_STRINGS_base64_encode (body_str, strlen (body_str), &body_base64);
165 fix_base64 (body_base64);
167 GNUNET_free (subject);
168 GNUNET_free (audience);
171 * Creating the JWT signature. This might not be
172 * standards compliant, check.
174 GNUNET_asprintf (&signature_target, "%s.%s", header_base64, body_base64);
175 GNUNET_CRYPTO_hmac_raw (secret_key,
178 strlen (signature_target),
180 GNUNET_STRINGS_base64_encode ((const char *) &signature,
181 sizeof (struct GNUNET_HashCode),
183 fix_base64 (signature_base64);
185 GNUNET_asprintf (&result,
191 GNUNET_free (signature_target);
192 GNUNET_free (header);
193 GNUNET_free (body_str);
194 GNUNET_free (signature_base64);
195 GNUNET_free (body_base64);
196 GNUNET_free (header_base64);
200 /* Converts a hex character to its integer value */
204 return isdigit (ch) ? ch - '0' : tolower (ch) - 'a' + 10;
207 /* Converts an integer value to its hex character*/
211 static char hex[] = "0123456789abcdef";
212 return hex[code & 15];
215 /* Returns a url-encoded version of str */
216 /* IMPORTANT: be sure to free() the returned string after use */
218 url_encode (const char *str)
220 char *pstr = (char *) str;
221 char *buf = GNUNET_malloc (strlen (str) * 3 + 1);
225 if (isalnum (*pstr) || *pstr == '-' || *pstr == '_' || *pstr == '.' ||
228 else if (*pstr == ' ')
233 *pbuf++ = to_hex (*pstr >> 4);
234 *pbuf++ = to_hex (*pstr & 15);
243 /* Returns a url-decoded version of str */
244 /* IMPORTANT: be sure to free() the returned string after use */
246 url_decode (const char *str)
248 char *pstr = (char *) str;
249 char *buf = GNUNET_malloc (strlen (str) + 1);
255 if (pstr[1] && pstr[2])
257 *pbuf++ = from_hex (pstr[1]) << 4 | from_hex (pstr[2]);
261 else if (*pstr == '+')
277 * Returns base64 encoded string urlencoded
279 * @param string the string to encode
280 * @return base64 encoded string
283 base64_encode (const char *data, size_t data_size)
288 GNUNET_STRINGS_base64_encode (data, data_size, &enc);
289 enc_urlencode = url_encode (enc);
291 return enc_urlencode;
296 * Builds an OIDC authorization code including
297 * a reclaim ticket and nonce
299 * @param issuer the issuer of the ticket, used to sign the ticket and nonce
300 * @param ticket the ticket to include in the code
301 * @param attrs list of attributes whicha re shared
302 * @param nonce the nonce to include in the code
303 * @return a new authorization code (caller must free)
306 OIDC_build_authz_code (const struct GNUNET_CRYPTO_EcdsaPrivateKey *issuer,
307 const struct GNUNET_RECLAIM_Ticket *ticket,
308 struct GNUNET_RECLAIM_ATTRIBUTE_ClaimList *attrs,
309 const char *nonce_str)
315 size_t signature_payload_len;
316 size_t attr_list_len;
317 size_t code_payload_len;
319 unsigned int nonce_tmp;
320 struct GNUNET_CRYPTO_EccSignaturePurpose *purpose;
323 signature_payload_len =
324 sizeof (struct GNUNET_RECLAIM_Ticket) + sizeof (unsigned int);
327 attr_list_len = GNUNET_RECLAIM_ATTRIBUTE_list_serialize_get_size (attrs);
328 signature_payload_len += attr_list_len;
329 attrs_ser = GNUNET_malloc (attr_list_len);
330 GNUNET_RECLAIM_ATTRIBUTE_list_serialize (attrs, attrs_ser);
332 code_payload_len = sizeof (struct GNUNET_CRYPTO_EccSignaturePurpose) +
333 signature_payload_len +
334 sizeof (struct GNUNET_CRYPTO_EcdsaSignature);
335 code_payload = GNUNET_malloc (code_payload_len);
336 purpose = (struct GNUNET_CRYPTO_EccSignaturePurpose *) code_payload;
337 purpose->size = htonl (sizeof (struct GNUNET_CRYPTO_EccSignaturePurpose) +
338 signature_payload_len);
339 purpose->purpose = htonl (GNUNET_SIGNATURE_PURPOSE_RECLAIM_CODE_SIGN);
340 // First, copy ticket
341 buf_ptr = (char *) &purpose[1];
342 memcpy (buf_ptr, ticket, sizeof (struct GNUNET_RECLAIM_Ticket));
343 buf_ptr += sizeof (struct GNUNET_RECLAIM_Ticket);
346 if (NULL != nonce_str)
348 if ((1 != SSCANF (nonce_str, "%u", &nonce)) || (nonce > UINT32_MAX))
351 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Invalid nonce %s\n", nonce_str);
352 GNUNET_free (code_payload);
353 GNUNET_free_non_null (attrs_ser);
357 nonce_tmp = htons (nonce);
358 memcpy (buf_ptr, &nonce_tmp, sizeof (unsigned int));
359 buf_ptr += sizeof (unsigned int);
360 // Finally, attributes
361 if (NULL != attrs_ser)
363 memcpy (buf_ptr, attrs_ser, attr_list_len);
364 buf_ptr += attr_list_len;
367 GNUNET_CRYPTO_ecdsa_sign (issuer,
369 (struct GNUNET_CRYPTO_EcdsaSignature *)
373 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Unable to sign code\n");
374 GNUNET_free (code_payload);
375 GNUNET_free_non_null (attrs_ser);
378 code_str = base64_encode ((const char *) &code_payload, code_payload_len);
379 GNUNET_free (code_payload);
380 GNUNET_free_non_null (attrs_ser);
386 * Parse reclaim ticket and nonce from
387 * authorization code.
388 * This also verifies the signature in the code.
390 * @param audience the expected audience of the code
391 * @param code the string representation of the code
392 * @param ticket where to store the ticket
393 * @param attrs the attributes in the code
394 * @param nonce where to store the nonce
395 * @return GNUNET_OK if successful, else GNUNET_SYSERR
398 OIDC_parse_authz_code (const struct GNUNET_CRYPTO_EcdsaPublicKey *audience,
400 struct GNUNET_RECLAIM_Ticket *ticket,
401 struct GNUNET_RECLAIM_ATTRIBUTE_ClaimList **attrs,
406 struct GNUNET_CRYPTO_EccSignaturePurpose *purpose;
407 struct GNUNET_CRYPTO_EcdsaSignature *signature;
408 size_t code_payload_len;
409 size_t attrs_ser_len;
410 size_t signature_offset;
413 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Trying to decode `%s'", code);
416 GNUNET_STRINGS_base64_decode (code, strlen (code), (void **) &code_payload);
418 if (code_payload_len < sizeof (struct GNUNET_CRYPTO_EccSignaturePurpose) +
419 sizeof (struct GNUNET_RECLAIM_Ticket) +
420 sizeof (unsigned int) +
421 sizeof (struct GNUNET_CRYPTO_EcdsaSignature))
423 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Authorization code malformed\n");
424 GNUNET_free_non_null (code_payload);
425 return GNUNET_SYSERR;
428 purpose = (struct GNUNET_CRYPTO_EccSignaturePurpose *) code_payload;
429 attrs_ser_len = code_payload_len;
430 attrs_ser_len -= sizeof (struct GNUNET_CRYPTO_EccSignaturePurpose);
431 ptr = (char *) &purpose[1];
432 *ticket = *((struct GNUNET_RECLAIM_Ticket *) ptr);
433 attrs_ser_len -= sizeof (struct GNUNET_RECLAIM_Ticket);
434 ptr += sizeof (struct GNUNET_RECLAIM_Ticket);
435 nonce = ntohs (*((unsigned int *) ptr));
436 attrs_ser_len -= sizeof (unsigned int);
437 ptr += sizeof (unsigned int);
438 attrs_ser_len -= sizeof (struct GNUNET_CRYPTO_EcdsaSignature);
439 *attrs = GNUNET_RECLAIM_ATTRIBUTE_list_deserialize (ptr, attrs_ser_len);
441 code_payload_len - sizeof (struct GNUNET_CRYPTO_EcdsaSignature);
443 (struct GNUNET_CRYPTO_EcdsaSignature *) &code_payload[signature_offset];
444 if (0 != GNUNET_memcmp (audience, &ticket->audience))
446 GNUNET_RECLAIM_ATTRIBUTE_list_destroy (*attrs);
447 GNUNET_free (code_payload);
448 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
449 "Audience in ticket does not match client!\n");
450 return GNUNET_SYSERR;
453 GNUNET_CRYPTO_ecdsa_verify (GNUNET_SIGNATURE_PURPOSE_RECLAIM_CODE_SIGN,
458 GNUNET_RECLAIM_ATTRIBUTE_list_destroy (*attrs);
459 GNUNET_free (code_payload);
460 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Signature of AuthZ code invalid!\n");
461 return GNUNET_SYSERR;
465 GNUNET_asprintf (nonce_str, "%u", nonce);
471 * Build a token response for a token request
472 * TODO: Maybe we should add the scope here?
474 * @param access_token the access token to include
475 * @param id_token the id_token to include
476 * @param expiration_time the expiration time of the token(s)
477 * @param token_response where to store the response
480 OIDC_build_token_response (const char *access_token,
481 const char *id_token,
482 const struct GNUNET_TIME_Relative *expiration_time,
483 char **token_response)
487 root_json = json_object ();
489 GNUNET_assert (NULL != access_token);
490 GNUNET_assert (NULL != id_token);
491 GNUNET_assert (NULL != expiration_time);
492 json_object_set_new (root_json, "access_token", json_string (access_token));
493 json_object_set_new (root_json, "token_type", json_string ("Bearer"));
494 json_object_set_new (root_json,
496 json_integer (expiration_time->rel_value_us /
498 json_object_set_new (root_json, "id_token", json_string (id_token));
499 *token_response = json_dumps (root_json, JSON_INDENT (0) | JSON_COMPACT);
500 json_decref (root_json);
504 * Generate a new access token
507 OIDC_access_token_new ()
509 char *access_token_number;
511 uint64_t random_number;
514 GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_NONCE, UINT64_MAX);
515 GNUNET_asprintf (&access_token_number, "%" PRIu64, random_number);
516 GNUNET_STRINGS_base64_encode (access_token_number,
517 strlen (access_token_number),