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
29 #include "gnunet_util_lib.h"
30 #include "gnunet_reclaim_attribute_lib.h"
31 #include "gnunet_reclaim_service.h"
32 #include "gnunet_signatures.h"
33 #include "oidc_helper.h"
34 // #include "benchmark.h"
37 GNUNET_NETWORK_STRUCT_BEGIN
40 * The signature used to generate the authorization code
42 struct OIDC_Parameters
47 struct GNUNET_RECLAIM_Ticket ticket;
52 uint32_t nonce GNUNET_PACKED;
55 * The length of the PKCE code_challenge
57 uint32_t code_challenge_len GNUNET_PACKED;
60 * The length of the attributes list
62 uint32_t attr_list_len GNUNET_PACKED;
65 GNUNET_NETWORK_STRUCT_END
68 create_jwt_header (void)
73 root = json_object ();
74 json_object_set_new (root, JWT_ALG, json_string (JWT_ALG_VALUE));
75 json_object_set_new (root, JWT_TYP, json_string (JWT_TYP_VALUE));
77 json_str = json_dumps (root, JSON_INDENT (0) | JSON_COMPACT);
84 replace_char (char *str, char find, char replace)
86 char *current_pos = strchr (str, find);
90 *current_pos = replace;
91 current_pos = strchr (current_pos, find);
98 fix_base64 (char *str)
101 replace_char (str, '+', '-');
104 replace_char (str, '/', '_');
109 * Create a JWT from attributes
111 * @param aud_key the public of the audience
112 * @param sub_key the public key of the subject
113 * @param attrs the attribute list
114 * @param expiration_time the validity of the token
115 * @param secret_key the key used to sign the JWT
116 * @return a new base64-encoded JWT string.
119 OIDC_id_token_new (const struct GNUNET_CRYPTO_EcdsaPublicKey *aud_key,
120 const struct GNUNET_CRYPTO_EcdsaPublicKey *sub_key,
121 const struct GNUNET_RECLAIM_ATTRIBUTE_ClaimList *attrs,
122 const struct GNUNET_TIME_Relative *expiration_time,
124 const char *secret_key)
126 struct GNUNET_RECLAIM_ATTRIBUTE_ClaimListEntry *le;
127 struct GNUNET_HashCode signature;
128 struct GNUNET_TIME_Absolute exp_time;
129 struct GNUNET_TIME_Absolute time_now;
137 char *signature_target;
138 char *signature_base64;
142 // iat REQUIRED time now
143 time_now = GNUNET_TIME_absolute_get ();
144 // exp REQUIRED time expired from config
145 exp_time = GNUNET_TIME_absolute_add (time_now, *expiration_time);
146 // auth_time only if max_age
147 // nonce only if nonce
148 // OPTIONAL acr,amr,azp
150 GNUNET_STRINGS_data_to_string_alloc (sub_key,
152 GNUNET_CRYPTO_EcdsaPublicKey));
154 GNUNET_STRINGS_data_to_string_alloc (aud_key,
156 GNUNET_CRYPTO_EcdsaPublicKey));
157 header = create_jwt_header ();
158 body = json_object ();
160 // iss REQUIRED case sensitive server uri with https
161 // The issuer is the local reclaim instance (e.g.
162 // https://reclaim.id/api/openid)
163 json_object_set_new (body, "iss", json_string (SERVER_ADDRESS));
164 // sub REQUIRED public key identity, not exceed 255 ASCII length
165 json_object_set_new (body, "sub", json_string (subject));
166 // aud REQUIRED public key client_id must be there
167 json_object_set_new (body, "aud", json_string (audience));
169 json_object_set_new (body,
171 json_integer (time_now.abs_value_us / (1000 * 1000)));
173 json_object_set_new (body,
175 json_integer (exp_time.abs_value_us / (1000 * 1000)));
177 json_object_set_new (body,
179 json_integer (time_now.abs_value_us / (1000 * 1000)));
182 json_object_set_new (body, "nonce", json_string (nonce));
184 for (le = attrs->list_head; NULL != le; le = le->next)
187 GNUNET_RECLAIM_ATTRIBUTE_value_to_string (le->claim->type,
189 le->claim->data_size);
190 json_object_set_new (body, le->claim->name, json_string (attr_val_str));
191 GNUNET_free (attr_val_str);
193 body_str = json_dumps (body, JSON_INDENT (0) | JSON_COMPACT);
196 GNUNET_STRINGS_base64_encode (header, strlen (header), &header_base64);
197 fix_base64 (header_base64);
199 GNUNET_STRINGS_base64_encode (body_str, strlen (body_str), &body_base64);
200 fix_base64 (body_base64);
202 GNUNET_free (subject);
203 GNUNET_free (audience);
206 * Creating the JWT signature. This might not be
207 * standards compliant, check.
209 GNUNET_asprintf (&signature_target, "%s.%s", header_base64, body_base64);
210 GNUNET_CRYPTO_hmac_raw (secret_key,
213 strlen (signature_target),
215 GNUNET_STRINGS_base64_encode ((const char *) &signature,
216 sizeof(struct GNUNET_HashCode),
218 fix_base64 (signature_base64);
220 GNUNET_asprintf (&result,
226 GNUNET_free (signature_target);
227 GNUNET_free (header);
228 GNUNET_free (body_str);
229 GNUNET_free (signature_base64);
230 GNUNET_free (body_base64);
231 GNUNET_free (header_base64);
236 /* Converts a hex character to its integer value */
240 return isdigit (ch) ? ch - '0' : tolower (ch) - 'a' + 10;
244 /* Converts an integer value to its hex character*/
248 static char hex[] = "0123456789abcdef";
250 return hex[code & 15];
254 /* Returns a url-encoded version of str */
255 /* IMPORTANT: be sure to free() the returned string after use */
257 url_encode (const char *str)
259 char *pstr = (char *) str;
260 char *buf = GNUNET_malloc (strlen (str) * 3 + 1);
265 if (isalnum (*pstr) || (*pstr == '-') || (*pstr == '_') || (*pstr == '.') ||
268 else if (*pstr == ' ')
273 *pbuf++ = to_hex (*pstr >> 4);
274 *pbuf++ = to_hex (*pstr & 15);
283 /* Returns a url-decoded version of str */
284 /* IMPORTANT: be sure to free() the returned string after use */
286 url_decode (const char *str)
288 char *pstr = (char *) str;
289 char *buf = GNUNET_malloc (strlen (str) + 1);
296 if (pstr[1] && pstr[2])
298 *pbuf++ = from_hex (pstr[1]) << 4 | from_hex (pstr[2]);
302 else if (*pstr == '+')
318 * Returns base64 encoded string urlencoded
320 * @param string the string to encode
321 * @return base64 encoded string
324 base64_and_urlencode (const char *data, size_t data_size)
329 GNUNET_STRINGS_base64_encode (data, data_size, &enc);
330 urlenc = url_encode (enc);
337 * Returns base64 encoded string urlencoded
339 * @param string the string to encode
340 * @return base64 encoded string
343 base64url_encode (const char *data, size_t data_size)
348 GNUNET_STRINGS_base64_encode (data, data_size, &enc);
349 // Replace with correct characters for base64url
351 while ('\0' != enc[pos])
369 derive_aes_key (struct GNUNET_CRYPTO_SymmetricSessionKey *key,
370 struct GNUNET_CRYPTO_SymmetricInitializationVector *iv,
371 struct GNUNET_HashCode *key_material)
373 static const char ctx_key[] = "reclaim-aes-ctx-key";
374 static const char ctx_iv[] = "reclaim-aes-ctx-iv";
376 GNUNET_CRYPTO_kdf (key,
377 sizeof(struct GNUNET_CRYPTO_SymmetricSessionKey),
381 sizeof(struct GNUNET_HashCode),
383 GNUNET_CRYPTO_kdf (iv,
385 struct GNUNET_CRYPTO_SymmetricInitializationVector),
389 sizeof(struct GNUNET_HashCode),
395 calculate_key_priv (struct GNUNET_CRYPTO_SymmetricSessionKey *key,
396 struct GNUNET_CRYPTO_SymmetricInitializationVector *iv,
397 const struct GNUNET_CRYPTO_EcdsaPrivateKey *ecdsa_priv,
398 const struct GNUNET_CRYPTO_EcdhePublicKey *ecdh_pub)
400 struct GNUNET_HashCode key_material;
402 GNUNET_CRYPTO_ecdsa_ecdh (ecdsa_priv, ecdh_pub, &key_material);
403 derive_aes_key (key, iv, &key_material);
408 calculate_key_pub (struct GNUNET_CRYPTO_SymmetricSessionKey *key,
409 struct GNUNET_CRYPTO_SymmetricInitializationVector *iv,
410 const struct GNUNET_CRYPTO_EcdsaPublicKey *ecdsa_pub,
411 const struct GNUNET_CRYPTO_EcdhePrivateKey *ecdh_priv)
413 struct GNUNET_HashCode key_material;
415 GNUNET_CRYPTO_ecdh_ecdsa (ecdh_priv, ecdsa_pub, &key_material);
416 derive_aes_key (key, iv, &key_material);
421 decrypt_payload (const struct GNUNET_CRYPTO_EcdsaPrivateKey *ecdsa_priv,
422 const struct GNUNET_CRYPTO_EcdhePublicKey *ecdh_pub,
427 struct GNUNET_CRYPTO_SymmetricSessionKey key;
428 struct GNUNET_CRYPTO_SymmetricInitializationVector iv;
430 calculate_key_priv (&key, &iv, ecdsa_priv, ecdh_pub);
431 GNUNET_break (GNUNET_CRYPTO_symmetric_decrypt (ct, ct_len, &key, &iv, buf));
436 encrypt_payload (const struct GNUNET_CRYPTO_EcdsaPublicKey *ecdsa_pub,
437 const struct GNUNET_CRYPTO_EcdhePrivateKey *ecdh_priv,
442 struct GNUNET_CRYPTO_SymmetricSessionKey key;
443 struct GNUNET_CRYPTO_SymmetricInitializationVector iv;
445 calculate_key_pub (&key, &iv, ecdsa_pub, ecdh_priv);
447 GNUNET_CRYPTO_symmetric_encrypt (payload, payload_len, &key, &iv, buf));
452 * Builds an OIDC authorization code including
453 * a reclaim ticket and nonce
455 * @param issuer the issuer of the ticket, used to sign the ticket and nonce
456 * @param ticket the ticket to include in the code
457 * @param attrs list of attributes which are shared
458 * @param nonce the nonce to include in the code
459 * @param code_challenge PKCE code challenge
460 * @return a new authorization code (caller must free)
463 OIDC_build_authz_code (const struct GNUNET_CRYPTO_EcdsaPrivateKey *issuer,
464 const struct GNUNET_RECLAIM_Ticket *ticket,
465 struct GNUNET_RECLAIM_ATTRIBUTE_ClaimList *attrs,
466 const char *nonce_str,
467 const char *code_challenge)
469 struct OIDC_Parameters params;
474 char *buf_ptr = NULL;
476 size_t code_payload_len;
477 size_t attr_list_len = 0;
478 size_t code_challenge_len = 0;
481 struct GNUNET_CRYPTO_EccSignaturePurpose *purpose;
482 struct GNUNET_CRYPTO_EcdhePrivateKey *ecdh_priv;
483 struct GNUNET_CRYPTO_EcdhePublicKey ecdh_pub;
487 memset (¶ms, 0, sizeof(params));
488 params.ticket = *ticket;
491 payload_len = sizeof(struct OIDC_Parameters);
492 if ((NULL != nonce_str) && (strcmp ("", nonce_str) != 0))
494 if ((1 != sscanf (nonce_str, "%u", &nonce)) || (nonce > UINT32_MAX))
497 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Invalid nonce %s\n", nonce_str);
500 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
501 "Got nonce: %u from %s\n",
505 nonce_tmp = htonl (nonce);
506 params.nonce = nonce_tmp;
507 // Assign code challenge
508 if (NULL != code_challenge)
509 code_challenge_len = strlen (code_challenge);
510 payload_len += code_challenge_len;
511 params.code_challenge_len = htonl (code_challenge_len);
516 attr_list_len = GNUNET_RECLAIM_ATTRIBUTE_list_serialize_get_size (attrs);
517 params.attr_list_len = htonl (attr_list_len);
518 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
519 "Length of serialized attributes: %lu\n",
521 // Get serialized attributes
522 payload_len += attr_list_len;
524 // Get plaintext length
525 payload = GNUNET_malloc (payload_len);
526 memcpy (payload, ¶ms, sizeof(params));
527 tmp = payload + sizeof(params);
528 if (0 < code_challenge_len)
530 memcpy (tmp, code_challenge, code_challenge_len);
531 tmp += code_challenge_len;
533 if (0 < attr_list_len)
534 GNUNET_RECLAIM_ATTRIBUTE_list_serialize (attrs, tmp);
539 code_payload_len = sizeof(struct GNUNET_CRYPTO_EccSignaturePurpose)
540 + sizeof(struct GNUNET_CRYPTO_EcdhePublicKey)
541 + payload_len + sizeof(struct
542 GNUNET_CRYPTO_EcdsaSignature);
543 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
544 "Length of data to encode: %lu\n",
548 ecdh_priv = GNUNET_CRYPTO_ecdhe_key_create ();
549 GNUNET_CRYPTO_ecdhe_key_get_public (ecdh_priv, &ecdh_pub);
550 // Initialize code payload
551 code_payload = GNUNET_malloc (code_payload_len);
552 GNUNET_assert (NULL != code_payload);
553 purpose = (struct GNUNET_CRYPTO_EccSignaturePurpose *) code_payload;
554 purpose->size = htonl (sizeof(struct GNUNET_CRYPTO_EccSignaturePurpose)
555 + sizeof(ecdh_pub) + payload_len);
556 purpose->purpose = htonl (GNUNET_SIGNATURE_PURPOSE_RECLAIM_CODE_SIGN);
558 buf_ptr = (char *) &purpose[1];
559 memcpy (buf_ptr, &ecdh_pub, sizeof(ecdh_pub));
560 buf_ptr += sizeof(ecdh_pub);
561 // Encrypt plaintext and store
562 encrypt_payload (&ticket->audience, ecdh_priv, payload, payload_len, buf_ptr);
563 GNUNET_free (ecdh_priv);
564 GNUNET_free (payload);
565 buf_ptr += payload_len;
566 // Sign and store signature
568 GNUNET_CRYPTO_ecdsa_sign (issuer,
570 (struct GNUNET_CRYPTO_EcdsaSignature *)
574 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Unable to sign code\n");
575 GNUNET_free (code_payload);
578 code_str = base64_and_urlencode (code_payload, code_payload_len);
579 GNUNET_free (code_payload);
585 * Parse reclaim ticket and nonce from
586 * authorization code.
587 * This also verifies the signature in the code.
589 * @param audience the expected audience of the code
590 * @param code the string representation of the code
591 * @param code_verfier PKCE code verifier
592 * @param ticket where to store the ticket
593 * @param attrs the attributes in the code
594 * @param nonce where to store the nonce
595 * @return GNUNET_OK if successful, else GNUNET_SYSERR
598 OIDC_parse_authz_code (const struct GNUNET_CRYPTO_EcdsaPrivateKey *ecdsa_priv,
600 const char *code_verifier,
601 struct GNUNET_RECLAIM_Ticket *ticket,
602 struct GNUNET_RECLAIM_ATTRIBUTE_ClaimList **attrs,
609 char *expected_code_challenge;
610 char *code_challenge;
611 char *code_verifier_hash;
612 struct GNUNET_CRYPTO_EccSignaturePurpose *purpose;
613 struct GNUNET_CRYPTO_EcdsaSignature *signature;
614 struct GNUNET_CRYPTO_EcdsaPublicKey ecdsa_pub;
615 struct GNUNET_CRYPTO_EcdhePublicKey *ecdh_pub;
616 uint32_t code_challenge_len;
617 uint32_t attrs_ser_len;
618 size_t plaintext_len;
619 size_t code_payload_len;
621 struct OIDC_Parameters *params;
623 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Trying to decode `%s'\n", code);
626 GNUNET_STRINGS_base64_decode (code, strlen (code), (void **) &code_payload);
627 if (code_payload_len < sizeof(struct GNUNET_CRYPTO_EccSignaturePurpose)
628 + sizeof(struct GNUNET_CRYPTO_EcdhePublicKey)
629 + sizeof(struct OIDC_Parameters)
630 + sizeof(struct GNUNET_CRYPTO_EcdsaSignature))
632 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Authorization code malformed\n");
633 GNUNET_free_non_null (code_payload);
634 return GNUNET_SYSERR;
637 purpose = (struct GNUNET_CRYPTO_EccSignaturePurpose *) code_payload;
638 plaintext_len = code_payload_len;
639 plaintext_len -= sizeof(struct GNUNET_CRYPTO_EccSignaturePurpose);
640 ptr = (char *) &purpose[1];
642 ecdh_pub = (struct GNUNET_CRYPTO_EcdhePublicKey *) ptr;
643 ptr += sizeof(struct GNUNET_CRYPTO_EcdhePublicKey);
644 plaintext_len -= sizeof(struct GNUNET_CRYPTO_EcdhePublicKey);
646 // Decrypt ciphertext
647 plaintext_len -= sizeof(struct GNUNET_CRYPTO_EcdsaSignature);
648 plaintext = GNUNET_malloc (plaintext_len);
649 decrypt_payload (ecdsa_priv, ecdh_pub, ptr, plaintext_len, plaintext);
651 ptr += plaintext_len;
652 signature = (struct GNUNET_CRYPTO_EcdsaSignature *) ptr;
653 params = (struct OIDC_Parameters *) plaintext;
655 // cmp code_challenge code_verifier
656 code_challenge_len = ntohl (params->code_challenge_len);
657 if (0 != code_challenge_len) /* Only check if this code requires a CV */
659 if (NULL == code_verifier)
661 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
662 "Expected code verifier!\n");
663 GNUNET_free_non_null (code_payload);
664 return GNUNET_SYSERR;
666 code_verifier_hash = GNUNET_malloc (256 / 8);
667 // hash code verifier
668 gcry_md_hash_buffer (GCRY_MD_SHA256,
671 strlen (code_verifier));
672 // encode code verifier
673 expected_code_challenge = base64url_encode (code_verifier_hash, 256 / 8);
674 code_challenge = (char *) ¶ms[1];
675 GNUNET_free (code_verifier_hash);
676 if ((strlen (expected_code_challenge) != code_challenge_len) ||
678 strncmp (expected_code_challenge, code_challenge, code_challenge_len)))
680 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
681 "Invalid code verifier! Expected: %s, Got: %.*s\n",
682 expected_code_challenge,
685 GNUNET_free_non_null (code_payload);
686 GNUNET_free (expected_code_challenge);
687 return GNUNET_SYSERR;
689 GNUNET_free (expected_code_challenge);
692 memcpy (ticket, ¶ms->ticket, sizeof(params->ticket));
694 nonce = ntohl (params->nonce); // ntohl (*((uint32_t *) ptr));
695 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got nonce: %u\n", nonce);
697 GNUNET_CRYPTO_ecdsa_key_get_public (ecdsa_priv, &ecdsa_pub);
698 if (0 != GNUNET_memcmp (&ecdsa_pub, &ticket->audience))
700 GNUNET_free (code_payload);
701 GNUNET_free (plaintext);
702 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
703 "Audience in ticket does not match client!\n");
704 return GNUNET_SYSERR;
707 GNUNET_CRYPTO_ecdsa_verify (GNUNET_SIGNATURE_PURPOSE_RECLAIM_CODE_SIGN,
712 GNUNET_free (code_payload);
713 GNUNET_free (plaintext);
714 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Signature of AuthZ code invalid!\n");
715 return GNUNET_SYSERR;
718 attrs_ser = ((char *) ¶ms[1]) + code_challenge_len;
719 attrs_ser_len = ntohl (params->attr_list_len);
720 *attrs = GNUNET_RECLAIM_ATTRIBUTE_list_deserialize (attrs_ser, attrs_ser_len);
724 GNUNET_asprintf (nonce_str, "%u", nonce);
725 GNUNET_free (code_payload);
726 GNUNET_free (plaintext);
732 * Build a token response for a token request
733 * TODO: Maybe we should add the scope here?
735 * @param access_token the access token to include
736 * @param id_token the id_token to include
737 * @param expiration_time the expiration time of the token(s)
738 * @param token_response where to store the response
741 OIDC_build_token_response (const char *access_token,
742 const char *id_token,
743 const struct GNUNET_TIME_Relative *expiration_time,
744 char **token_response)
748 root_json = json_object ();
750 GNUNET_assert (NULL != access_token);
751 GNUNET_assert (NULL != id_token);
752 GNUNET_assert (NULL != expiration_time);
753 json_object_set_new (root_json, "access_token", json_string (access_token));
754 json_object_set_new (root_json, "token_type", json_string ("Bearer"));
755 json_object_set_new (root_json,
757 json_integer (expiration_time->rel_value_us
759 json_object_set_new (root_json, "id_token", json_string (id_token));
760 *token_response = json_dumps (root_json, JSON_INDENT (0) | JSON_COMPACT);
761 json_decref (root_json);
766 * Generate a new access token
769 OIDC_access_token_new ()
772 uint64_t random_number;
775 GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_NONCE, UINT64_MAX);
776 GNUNET_STRINGS_base64_encode (&random_number,