obsolete due to alpine policy
[oweals/gnunet.git] / src / reclaim / oidc_helper.c
index e686a67a108233d987baae54225651dd749e6c94..6bcae21d420a87bf6fc3640aaea1c5168d58ebe7 100644 (file)
@@ -16,7 +16,7 @@
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
    SPDX-License-Identifier: AGPL3.0-or-later
  */
+ */
 
 /**
  * @file reclaim/oidc_helper.c
@@ -31,7 +31,7 @@
 #include "gnunet_reclaim_service.h"
 #include "gnunet_signatures.h"
 #include "oidc_helper.h"
-//#include "benchmark.h"
+// #include "benchmark.h"
 #include <gcrypt.h>
 
 GNUNET_NETWORK_STRUCT_BEGIN
@@ -44,7 +44,7 @@ struct OIDC_Parameters
   /**
    * The reclaim ticket
    */
-  const struct GNUNET_RECLAIM_Ticket *ticket;
+  struct GNUNET_RECLAIM_Ticket ticket;
 
   /**
    * The nonce
@@ -83,6 +83,7 @@ static void
 replace_char (char *str, char find, char replace)
 {
   char *current_pos = strchr (str, find);
+
   while (current_pos)
   {
     *current_pos = replace;
@@ -144,12 +145,12 @@ OIDC_id_token_new (const struct GNUNET_CRYPTO_EcdsaPublicKey *aud_key,
   // OPTIONAL acr,amr,azp
   subject =
     GNUNET_STRINGS_data_to_string_alloc (sub_key,
-                                         sizeof (struct
-                                                 GNUNET_CRYPTO_EcdsaPublicKey));
+                                         sizeof(struct
+                                                GNUNET_CRYPTO_EcdsaPublicKey));
   audience =
     GNUNET_STRINGS_data_to_string_alloc (aud_key,
-                                         sizeof (struct
-                                                 GNUNET_CRYPTO_EcdsaPublicKey));
+                                         sizeof(struct
+                                                GNUNET_CRYPTO_EcdsaPublicKey));
   header = create_jwt_header ();
   body = json_object ();
 
@@ -209,7 +210,7 @@ OIDC_id_token_new (const struct GNUNET_CRYPTO_EcdsaPublicKey *aud_key,
                           strlen (signature_target),
                           &signature);
   GNUNET_STRINGS_base64_encode ((const char *) &signature,
-                                sizeof (struct GNUNET_HashCode),
+                                sizeof(struct GNUNET_HashCode),
                                 &signature_base64);
   fix_base64 (signature_base64);
 
@@ -240,6 +241,7 @@ static char
 to_hex (char code)
 {
   static char hex[] = "0123456789abcdef";
+
   return hex[code & 15];
 }
 
@@ -251,10 +253,11 @@ url_encode (const char *str)
   char *pstr = (char *) str;
   char *buf = GNUNET_malloc (strlen (str) * 3 + 1);
   char *pbuf = buf;
+
   while (*pstr)
   {
-    if (isalnum (*pstr) || *pstr == '-' || *pstr == '_' || *pstr == '.' ||
-        *pstr == '~')
+    if (isalnum (*pstr) || (*pstr == '-') || (*pstr == '_') || (*pstr == '.') ||
+        (*pstr == '~') )
       *pbuf++ = *pstr;
     else if (*pstr == ' ')
       *pbuf++ = '+';
@@ -279,6 +282,7 @@ url_decode (const char *str)
   char *pstr = (char *) str;
   char *buf = GNUNET_malloc (strlen (str) + 1);
   char *pbuf = buf;
+
   while (*pstr)
   {
     if (*pstr == '%')
@@ -303,7 +307,6 @@ url_decode (const char *str)
   return buf;
 }
 
-
 /**
  * Returns base64 encoded string urlencoded
  *
@@ -311,15 +314,47 @@ url_decode (const char *str)
  * @return base64 encoded string
  */
 static char *
-base64_encode (const char *data, size_t data_size)
+base64_and_urlencode (const char *data, size_t data_size)
 {
   char *enc;
-  char *enc_urlencode;
+  char *urlenc;
 
   GNUNET_STRINGS_base64_encode (data, data_size, &enc);
-  enc_urlencode = url_encode (enc);
+  urlenc = url_encode (enc);
   GNUNET_free (enc);
-  return enc_urlencode;
+  return urlenc;
+}
+
+
+/**
+ * Returns base64 encoded string urlencoded
+ *
+ * @param string the string to encode
+ * @return base64 encoded string
+ */
+static char *
+base64url_encode (const char *data, size_t data_size)
+{
+  char *enc;
+  size_t pos;
+
+  GNUNET_STRINGS_base64_encode (data, data_size, &enc);
+  // Replace with correct characters for base64url
+  pos = 0;
+  while ('\0' != enc[pos])
+  {
+    if ('+' == enc[pos])
+      enc[pos] = '-';
+    if ('/' == enc[pos])
+      enc[pos] = '_';
+    if ('=' == enc[pos])
+    {
+      enc[pos] = '\0';
+      break;
+    }
+    pos++;
+  }
+  return enc;
 }
 
 
@@ -330,20 +365,21 @@ derive_aes_key (struct GNUNET_CRYPTO_SymmetricSessionKey *key,
 {
   static const char ctx_key[] = "reclaim-aes-ctx-key";
   static const char ctx_iv[] = "reclaim-aes-ctx-iv";
+
   GNUNET_CRYPTO_kdf (key,
-                     sizeof (struct GNUNET_CRYPTO_SymmetricSessionKey),
+                     sizeof(struct GNUNET_CRYPTO_SymmetricSessionKey),
                      ctx_key,
                      strlen (ctx_key),
                      key_material,
-                     sizeof (struct GNUNET_HashCode),
+                     sizeof(struct GNUNET_HashCode),
                      NULL);
   GNUNET_CRYPTO_kdf (iv,
-                     sizeof (
+                     sizeof(
                        struct GNUNET_CRYPTO_SymmetricInitializationVector),
                      ctx_iv,
                      strlen (ctx_iv),
                      key_material,
-                     sizeof (struct GNUNET_HashCode),
+                     sizeof(struct GNUNET_HashCode),
                      NULL);
 }
 
@@ -355,6 +391,7 @@ calculate_key_priv (struct GNUNET_CRYPTO_SymmetricSessionKey *key,
                     const struct GNUNET_CRYPTO_EcdhePublicKey *ecdh_pub)
 {
   struct GNUNET_HashCode key_material;
+
   GNUNET_CRYPTO_ecdsa_ecdh (ecdsa_priv, ecdh_pub, &key_material);
   derive_aes_key (key, iv, &key_material);
 }
@@ -367,6 +404,7 @@ calculate_key_pub (struct GNUNET_CRYPTO_SymmetricSessionKey *key,
                    const struct GNUNET_CRYPTO_EcdhePrivateKey *ecdh_priv)
 {
   struct GNUNET_HashCode key_material;
+
   GNUNET_CRYPTO_ecdh_ecdsa (ecdh_priv, ecdsa_pub, &key_material);
   derive_aes_key (key, iv, &key_material);
 }
@@ -429,6 +467,7 @@ OIDC_build_authz_code (const struct GNUNET_CRYPTO_EcdsaPrivateKey *issuer,
   size_t payload_len;
   size_t code_payload_len;
   size_t attr_list_len = 0;
+  size_t code_challenge_len = 0;
   uint32_t nonce;
   uint32_t nonce_tmp;
   struct GNUNET_CRYPTO_EccSignaturePurpose *purpose;
@@ -437,14 +476,14 @@ OIDC_build_authz_code (const struct GNUNET_CRYPTO_EcdsaPrivateKey *issuer,
 
   /** PLAINTEXT **/
   // Assign ticket
-  memset (&params, 0, sizeof (params));
-  params.ticket = ticket;
+  memset (&params, 0, sizeof(params));
+  params.ticket = *ticket;
   // Assign nonce
   nonce = 0;
-  payload_len = sizeof (struct OIDC_Parameters);
-  if (NULL != nonce_str && strcmp ("", nonce_str) != 0)
+  payload_len = sizeof(struct OIDC_Parameters);
+  if ((NULL != nonce_str)&& (strcmp ("", nonce_str) != 0))
   {
-    if ((1 != SSCANF (nonce_str, "%u", &nonce)) || (nonce > UINT32_MAX))
+    if ((1 != sscanf (nonce_str, "%u", &nonce)) || (nonce > UINT32_MAX))
     {
       GNUNET_break (0);
       GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Invalid nonce %s\n", nonce_str);
@@ -458,14 +497,10 @@ OIDC_build_authz_code (const struct GNUNET_CRYPTO_EcdsaPrivateKey *issuer,
   nonce_tmp = htonl (nonce);
   params.nonce = nonce_tmp;
   // Assign code challenge
-  if (NULL == code_challenge || strcmp ("", code_challenge) == 0)
-  {
-    GNUNET_break (0);
-    GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "PKCE: Code challenge missing");
-    return NULL;
-  }
-  payload_len += strlen (code_challenge);
-  params.code_challenge_len = htonl (strlen (code_challenge));
+  if (NULL != code_challenge)
+    code_challenge_len = strlen (code_challenge);
+  payload_len += code_challenge_len;
+  params.code_challenge_len = htonl (code_challenge_len);
   // Assign attributes
   if (NULL != attrs)
   {
@@ -480,19 +515,23 @@ OIDC_build_authz_code (const struct GNUNET_CRYPTO_EcdsaPrivateKey *issuer,
   }
   // Get plaintext length
   payload = GNUNET_malloc (payload_len);
-  memcpy (payload, &params, sizeof (params));
-  tmp = payload + sizeof (params);
-  memcpy (tmp, code_challenge, strlen (code_challenge));
-  tmp += strlen (code_challenge);
+  memcpy (payload, &params, sizeof(params));
+  tmp = payload + sizeof(params);
+  if (0 < code_challenge_len)
+  {
+    memcpy (tmp, code_challenge, code_challenge_len);
+    tmp += code_challenge_len;
+  }
   if (0 < attr_list_len)
     GNUNET_RECLAIM_ATTRIBUTE_list_serialize (attrs, tmp);
   /** END **/
 
   /** ENCRYPT **/
   // Get length
-  code_payload_len = sizeof (struct GNUNET_CRYPTO_EccSignaturePurpose) +
-                     sizeof (struct GNUNET_CRYPTO_EcdhePublicKey) +
-                     payload_len + sizeof (struct GNUNET_CRYPTO_EcdsaSignature);
+  code_payload_len = sizeof(struct GNUNET_CRYPTO_EccSignaturePurpose)
+                     + sizeof(struct GNUNET_CRYPTO_EcdhePublicKey)
+                     + payload_len + sizeof(struct
+                                            GNUNET_CRYPTO_EcdsaSignature);
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
               "Length of data to encode: %lu\n",
               code_payload_len);
@@ -504,19 +543,15 @@ OIDC_build_authz_code (const struct GNUNET_CRYPTO_EcdsaPrivateKey *issuer,
   code_payload = GNUNET_malloc (code_payload_len);
   GNUNET_assert (NULL != code_payload);
   purpose = (struct GNUNET_CRYPTO_EccSignaturePurpose *) code_payload;
-  purpose->size = htonl (sizeof (struct GNUNET_CRYPTO_EccSignaturePurpose) +
-                         sizeof (ecdh_pub) + payload_len);
+  purpose->size = htonl (sizeof(struct GNUNET_CRYPTO_EccSignaturePurpose)
+                         + sizeof(ecdh_pub) + payload_len);
   purpose->purpose = htonl (GNUNET_SIGNATURE_PURPOSE_RECLAIM_CODE_SIGN);
   // Store pubkey
   buf_ptr = (char *) &purpose[1];
-  memcpy (buf_ptr, &ecdh_pub, sizeof (ecdh_pub));
-  buf_ptr += sizeof (ecdh_pub);
+  memcpy (buf_ptr, &ecdh_pub, sizeof(ecdh_pub));
+  buf_ptr += sizeof(ecdh_pub);
   // Encrypt plaintext and store
-  encrypt_payload (&ticket->audience,
-                   ecdh_priv,
-                   payload,
-                   payload_len,
-                   buf_ptr);
+  encrypt_payload (&ticket->audience, ecdh_priv, payload, payload_len, buf_ptr);
   GNUNET_free (ecdh_priv);
   GNUNET_free (payload);
   buf_ptr += payload_len;
@@ -525,14 +560,14 @@ OIDC_build_authz_code (const struct GNUNET_CRYPTO_EcdsaPrivateKey *issuer,
       GNUNET_CRYPTO_ecdsa_sign (issuer,
                                 purpose,
                                 (struct GNUNET_CRYPTO_EcdsaSignature *)
-                                  buf_ptr))
+                                buf_ptr))
   {
     GNUNET_break (0);
     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Unable to sign code\n");
     GNUNET_free (code_payload);
     return NULL;
   }
-  code_str = base64_encode (code_payload, code_payload_len);
+  code_str = base64_and_urlencode (code_payload, code_payload_len);
   GNUNET_free (code_payload);
   return code_str;
 }
@@ -570,8 +605,8 @@ OIDC_parse_authz_code (const struct GNUNET_CRYPTO_EcdsaPrivateKey *ecdsa_priv,
   struct GNUNET_CRYPTO_EcdsaSignature *signature;
   struct GNUNET_CRYPTO_EcdsaPublicKey ecdsa_pub;
   struct GNUNET_CRYPTO_EcdhePublicKey *ecdh_pub;
-  size_t code_challenge_len;
-  size_t attrs_ser_len;
+  uint32_t code_challenge_len;
+  uint32_t attrs_ser_len;
   size_t plaintext_len;
   size_t code_payload_len;
   uint32_t nonce = 0;
@@ -581,10 +616,10 @@ OIDC_parse_authz_code (const struct GNUNET_CRYPTO_EcdsaPrivateKey *ecdsa_priv,
   code_payload = NULL;
   code_payload_len =
     GNUNET_STRINGS_base64_decode (code, strlen (code), (void **) &code_payload);
-  if (code_payload_len < sizeof (struct GNUNET_CRYPTO_EccSignaturePurpose) +
-                           sizeof (struct GNUNET_CRYPTO_EcdhePublicKey) +
-                           sizeof (struct OIDC_Parameters) +
-                           sizeof (struct GNUNET_CRYPTO_EcdsaSignature))
+  if (code_payload_len < sizeof(struct GNUNET_CRYPTO_EccSignaturePurpose)
+      + sizeof(struct GNUNET_CRYPTO_EcdhePublicKey)
+      + sizeof(struct OIDC_Parameters)
+      + sizeof(struct GNUNET_CRYPTO_EcdsaSignature))
   {
     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Authorization code malformed\n");
     GNUNET_free_non_null (code_payload);
@@ -593,60 +628,67 @@ OIDC_parse_authz_code (const struct GNUNET_CRYPTO_EcdsaPrivateKey *ecdsa_priv,
 
   purpose = (struct GNUNET_CRYPTO_EccSignaturePurpose *) code_payload;
   plaintext_len = code_payload_len;
-  plaintext_len -= sizeof (struct GNUNET_CRYPTO_EccSignaturePurpose);
+  plaintext_len -= sizeof(struct GNUNET_CRYPTO_EccSignaturePurpose);
   ptr = (char *) &purpose[1];
   // Public ECDH key
   ecdh_pub = (struct GNUNET_CRYPTO_EcdhePublicKey *) ptr;
-  ptr += sizeof (struct GNUNET_CRYPTO_EcdhePublicKey);
-  plaintext_len -= sizeof (struct GNUNET_CRYPTO_EcdhePublicKey);
+  ptr += sizeof(struct GNUNET_CRYPTO_EcdhePublicKey);
+  plaintext_len -= sizeof(struct GNUNET_CRYPTO_EcdhePublicKey);
 
   // Decrypt ciphertext
-  plaintext_len -= sizeof (struct GNUNET_CRYPTO_EcdsaSignature);
+  plaintext_len -= sizeof(struct GNUNET_CRYPTO_EcdsaSignature);
   plaintext = GNUNET_malloc (plaintext_len);
   decrypt_payload (ecdsa_priv, ecdh_pub, ptr, plaintext_len, plaintext);
-  //ptr = plaintext;
+  // ptr = plaintext;
+  ptr += plaintext_len;
+  signature = (struct GNUNET_CRYPTO_EcdsaSignature *) ptr;
   params = (struct OIDC_Parameters *) plaintext;
 
   // cmp code_challenge code_verifier
-  code_verifier_hash = GNUNET_malloc (256 / 8);
-  // hash code verifier
-  gcry_md_hash_buffer (GCRY_MD_SHA256,
-                       code_verifier_hash,
-                       code_verifier,
-                       strlen (code_verifier));
-  // encode code verifier
-  expected_code_challenge =
-    base64_encode (code_verifier_hash, 256 / 8);
-  code_challenge = (char*)&params[1];
   code_challenge_len = ntohl (params->code_challenge_len);
-  GNUNET_free (code_verifier_hash);
-  if ((strlen (expected_code_challenge) != code_challenge_len) ||
-      (0 !=
-       strncmp (expected_code_challenge, code_challenge, code_challenge_len)))
+  if (0 != code_challenge_len) /* Only check if this code requires a CV */
   {
-    GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Invalid code verifier\n");
-    GNUNET_free_non_null (code_payload);
+    if (NULL == code_verifier)
+    {
+      GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
+                  "Expected code verifier!\n");
+      GNUNET_free_non_null (code_payload);
+      return GNUNET_SYSERR;
+    }
+    code_verifier_hash = GNUNET_malloc (256 / 8);
+    // hash code verifier
+    gcry_md_hash_buffer (GCRY_MD_SHA256,
+                         code_verifier_hash,
+                         code_verifier,
+                         strlen (code_verifier));
+    // encode code verifier
+    expected_code_challenge = base64url_encode (code_verifier_hash, 256 / 8);
+    code_challenge = (char *) &params[1];
+    GNUNET_free (code_verifier_hash);
+    if ((strlen (expected_code_challenge) != code_challenge_len) ||
+        (0 !=
+         strncmp (expected_code_challenge, code_challenge, code_challenge_len)))
+    {
+      GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
+                  "Invalid code verifier! Expected: %s, Got: %.*s\n",
+                  expected_code_challenge,
+                  code_challenge_len,
+                  code_challenge);
+      GNUNET_free_non_null (code_payload);
+      GNUNET_free (expected_code_challenge);
+      return GNUNET_SYSERR;
+    }
     GNUNET_free (expected_code_challenge);
-    return GNUNET_SYSERR;
   }
-  GNUNET_free (expected_code_challenge);
   // Ticket
-  memcpy (ticket, params->ticket, sizeof (*ticket));
+  memcpy (ticket, &params->ticket, sizeof(params->ticket));
   // Nonce
-  nonce = ntohl (params->nonce); //ntohl (*((uint32_t *) ptr));
+  nonce = ntohl (params->nonce);  // ntohl (*((uint32_t *) ptr));
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got nonce: %u\n", nonce);
-  // Attributes
-  attrs_ser = ((char *) &params[1]) + code_challenge_len;
-  attrs_ser_len = ntohl (params->attr_list_len);
-  *attrs =
-    GNUNET_RECLAIM_ATTRIBUTE_list_deserialize (attrs_ser,
-                                               attrs_ser_len);
   // Signature
-  signature = (struct GNUNET_CRYPTO_EcdsaSignature *) attrs_ser + attrs_ser_len;
   GNUNET_CRYPTO_ecdsa_key_get_public (ecdsa_priv, &ecdsa_pub);
   if (0 != GNUNET_memcmp (&ecdsa_pub, &ticket->audience))
   {
-    GNUNET_RECLAIM_ATTRIBUTE_list_destroy (*attrs);
     GNUNET_free (code_payload);
     GNUNET_free (plaintext);
     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
@@ -659,12 +701,16 @@ OIDC_parse_authz_code (const struct GNUNET_CRYPTO_EcdsaPrivateKey *ecdsa_priv,
                                   signature,
                                   &ticket->identity))
   {
-    GNUNET_RECLAIM_ATTRIBUTE_list_destroy (*attrs);
     GNUNET_free (code_payload);
     GNUNET_free (plaintext);
     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Signature of AuthZ code invalid!\n");
     return GNUNET_SYSERR;
   }
+  // Attributes
+  attrs_ser = ((char *) &params[1]) + code_challenge_len;
+  attrs_ser_len = ntohl (params->attr_list_len);
+  *attrs = GNUNET_RECLAIM_ATTRIBUTE_list_deserialize (attrs_ser, attrs_ser_len);
+
   *nonce_str = NULL;
   if (nonce != 0)
     GNUNET_asprintf (nonce_str, "%u", nonce);
@@ -700,8 +746,8 @@ OIDC_build_token_response (const char *access_token,
   json_object_set_new (root_json, "token_type", json_string ("Bearer"));
   json_object_set_new (root_json,
                        "expires_in",
-                       json_integer (expiration_time->rel_value_us /
-                                     (1000 * 1000)));
+                       json_integer (expiration_time->rel_value_us
+                                     (1000 * 1000)));
   json_object_set_new (root_json, "id_token", json_string (id_token));
   *token_response = json_dumps (root_json, JSON_INDENT (0) | JSON_COMPACT);
   json_decref (root_json);
@@ -719,8 +765,7 @@ OIDC_access_token_new ()
   random_number =
     GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_NONCE, UINT64_MAX);
   GNUNET_STRINGS_base64_encode (&random_number,
-                                sizeof (uint64_t),
+                                sizeof(uint64_t),
                                 &access_token);
   return access_token;
 }
-