GNUnet  0.10.x
Data Structures | Functions
oidc_helper.c File Reference

helper library for OIDC related functions More...

#include "platform.h"
#include <inttypes.h>
#include <jansson.h>
#include "gnunet_util_lib.h"
#include "gnunet_reclaim_attribute_lib.h"
#include "gnunet_reclaim_service.h"
#include "gnunet_signatures.h"
#include "oidc_helper.h"
#include <gcrypt.h>
Include dependency graph for oidc_helper.c:

Go to the source code of this file.

Data Structures

struct  OIDC_Parameters
 The signature used to generate the authorization code. More...
 

Functions

static GNUNET_NETWORK_STRUCT_END char * create_jwt_header (void)
 
static void replace_char (char *str, char find, char replace)
 
static void fix_base64 (char *str)
 
char * OIDC_id_token_new (const struct GNUNET_CRYPTO_EcdsaPublicKey *aud_key, const struct GNUNET_CRYPTO_EcdsaPublicKey *sub_key, const struct GNUNET_RECLAIM_ATTRIBUTE_ClaimList *attrs, const struct GNUNET_TIME_Relative *expiration_time, const char *nonce, const char *secret_key)
 Create a JWT from attributes. More...
 
static char from_hex (char ch)
 
static char to_hex (char code)
 
static char * url_encode (const char *str)
 
static char * url_decode (const char *str)
 
static char * base64_and_urlencode (const char *data, size_t data_size)
 Returns base64 encoded string urlencoded. More...
 
static char * base64url_encode (const char *data, size_t data_size)
 Returns base64 encoded string urlencoded. More...
 
static void derive_aes_key (struct GNUNET_CRYPTO_SymmetricSessionKey *key, struct GNUNET_CRYPTO_SymmetricInitializationVector *iv, struct GNUNET_HashCode *key_material)
 
static void calculate_key_priv (struct GNUNET_CRYPTO_SymmetricSessionKey *key, struct GNUNET_CRYPTO_SymmetricInitializationVector *iv, const struct GNUNET_CRYPTO_EcdsaPrivateKey *ecdsa_priv, const struct GNUNET_CRYPTO_EcdhePublicKey *ecdh_pub)
 
static void calculate_key_pub (struct GNUNET_CRYPTO_SymmetricSessionKey *key, struct GNUNET_CRYPTO_SymmetricInitializationVector *iv, const struct GNUNET_CRYPTO_EcdsaPublicKey *ecdsa_pub, const struct GNUNET_CRYPTO_EcdhePrivateKey *ecdh_priv)
 
static void decrypt_payload (const struct GNUNET_CRYPTO_EcdsaPrivateKey *ecdsa_priv, const struct GNUNET_CRYPTO_EcdhePublicKey *ecdh_pub, const char *ct, size_t ct_len, char *buf)
 
static void encrypt_payload (const struct GNUNET_CRYPTO_EcdsaPublicKey *ecdsa_pub, const struct GNUNET_CRYPTO_EcdhePrivateKey *ecdh_priv, const char *payload, size_t payload_len, char *buf)
 
char * OIDC_build_authz_code (const struct GNUNET_CRYPTO_EcdsaPrivateKey *issuer, const struct GNUNET_RECLAIM_Ticket *ticket, struct GNUNET_RECLAIM_ATTRIBUTE_ClaimList *attrs, const char *nonce_str, const char *code_challenge)
 Builds an OIDC authorization code including a reclaim ticket and nonce. More...
 
int OIDC_parse_authz_code (const struct GNUNET_CRYPTO_EcdsaPrivateKey *ecdsa_priv, const char *code, const char *code_verifier, struct GNUNET_RECLAIM_Ticket *ticket, struct GNUNET_RECLAIM_ATTRIBUTE_ClaimList **attrs, char **nonce_str)
 Parse reclaim ticket and nonce from authorization code. More...
 
void OIDC_build_token_response (const char *access_token, const char *id_token, const struct GNUNET_TIME_Relative *expiration_time, char **token_response)
 Build a token response for a token request TODO: Maybe we should add the scope here? More...
 
char * OIDC_access_token_new ()
 Generate a new access token. More...
 

Detailed Description

helper library for OIDC related functions

Author
Martin Schanzenbach

Definition in file oidc_helper.c.

Function Documentation

◆ create_jwt_header()

static GNUNET_NETWORK_STRUCT_END char* create_jwt_header ( void  )
static

Definition at line 67 of file oidc_helper.c.

References JWT_ALG, JWT_ALG_VALUE, JWT_TYP, and JWT_TYP_VALUE.

Referenced by OIDC_id_token_new().

68 {
69  json_t *root;
70  char *json_str;
71 
72  root = json_object();
73  json_object_set_new(root, JWT_ALG, json_string(JWT_ALG_VALUE));
74  json_object_set_new(root, JWT_TYP, json_string(JWT_TYP_VALUE));
75 
76  json_str = json_dumps(root, JSON_INDENT(0) | JSON_COMPACT);
77  json_decref(root);
78  return json_str;
79 }
#define JWT_ALG_VALUE
Definition: oidc_helper.h:33
#define JWT_TYP
Definition: oidc_helper.h:35
#define JWT_ALG
Definition: oidc_helper.h:30
#define JWT_TYP_VALUE
Definition: oidc_helper.h:37
Here is the caller graph for this function:

◆ replace_char()

static void replace_char ( char *  str,
char  find,
char  replace 
)
static

Definition at line 82 of file oidc_helper.c.

Referenced by fix_base64().

83 {
84  char *current_pos = strchr(str, find);
85 
86  while (current_pos)
87  {
88  *current_pos = replace;
89  current_pos = strchr(current_pos, find);
90  }
91 }
Here is the caller graph for this function:

◆ fix_base64()

static void fix_base64 ( char *  str)
static

Definition at line 95 of file oidc_helper.c.

References replace_char().

Referenced by OIDC_id_token_new().

96 {
97  // Replace + with -
98  replace_char(str, '+', '-');
99 
100  // Replace / with _
101  replace_char(str, '/', '_');
102 }
static void replace_char(char *str, char find, char replace)
Definition: oidc_helper.c:82
Here is the call graph for this function:
Here is the caller graph for this function:

◆ OIDC_id_token_new()

char* OIDC_id_token_new ( const struct GNUNET_CRYPTO_EcdsaPublicKey aud_key,
const struct GNUNET_CRYPTO_EcdsaPublicKey sub_key,
const struct GNUNET_RECLAIM_ATTRIBUTE_ClaimList attrs,
const struct GNUNET_TIME_Relative expiration_time,
const char *  nonce,
const char *  secret_key 
)

Create a JWT from attributes.

Parameters
aud_keythe public of the audience
sub_keythe public key of the subject
attrsthe attribute list
expiration_timethe validity of the token
secret_keythe key used to sign the JWT
Returns
a new base64-encoded JWT string.

Creating the JWT signature. This might not be standards compliant, check.

Definition at line 115 of file oidc_helper.c.

References GNUNET_TIME_Absolute::abs_value_us, GNUNET_RECLAIM_ATTRIBUTE_ClaimListEntry::claim, create_jwt_header(), GNUNET_RECLAIM_ATTRIBUTE_Claim::data, GNUNET_RECLAIM_ATTRIBUTE_Claim::data_size, fix_base64(), GNUNET_asprintf(), GNUNET_CRYPTO_hmac_raw(), GNUNET_free, GNUNET_RECLAIM_ATTRIBUTE_value_to_string(), GNUNET_STRINGS_base64_encode(), GNUNET_STRINGS_data_to_string_alloc(), GNUNET_TIME_absolute_add(), GNUNET_TIME_absolute_get(), GNUNET_RECLAIM_ATTRIBUTE_ClaimList::list_head, GNUNET_RECLAIM_ATTRIBUTE_Claim::name, GNUNET_RECLAIM_ATTRIBUTE_ClaimListEntry::next, result, SERVER_ADDRESS, and GNUNET_RECLAIM_ATTRIBUTE_Claim::type.

Referenced by token_endpoint().

121 {
123  struct GNUNET_HashCode signature;
124  struct GNUNET_TIME_Absolute exp_time;
125  struct GNUNET_TIME_Absolute time_now;
126  char *audience;
127  char *subject;
128  char *header;
129  char *body_str;
130  char *result;
131  char *header_base64;
132  char *body_base64;
133  char *signature_target;
134  char *signature_base64;
135  char *attr_val_str;
136  json_t *body;
137 
138  // iat REQUIRED time now
139  time_now = GNUNET_TIME_absolute_get();
140  // exp REQUIRED time expired from config
141  exp_time = GNUNET_TIME_absolute_add(time_now, *expiration_time);
142  // auth_time only if max_age
143  // nonce only if nonce
144  // OPTIONAL acr,amr,azp
145  subject =
147  sizeof(struct
149  audience =
151  sizeof(struct
153  header = create_jwt_header();
154  body = json_object();
155 
156  // iss REQUIRED case sensitive server uri with https
157  // The issuer is the local reclaim instance (e.g.
158  // https://reclaim.id/api/openid)
159  json_object_set_new(body, "iss", json_string(SERVER_ADDRESS));
160  // sub REQUIRED public key identity, not exceed 255 ASCII length
161  json_object_set_new(body, "sub", json_string(subject));
162  // aud REQUIRED public key client_id must be there
163  json_object_set_new(body, "aud", json_string(audience));
164  // iat
165  json_object_set_new(body,
166  "iat",
167  json_integer(time_now.abs_value_us / (1000 * 1000)));
168  // exp
169  json_object_set_new(body,
170  "exp",
171  json_integer(exp_time.abs_value_us / (1000 * 1000)));
172  // nbf
173  json_object_set_new(body,
174  "nbf",
175  json_integer(time_now.abs_value_us / (1000 * 1000)));
176  // nonce
177  if (NULL != nonce)
178  json_object_set_new(body, "nonce", json_string(nonce));
179 
180  for (le = attrs->list_head; NULL != le; le = le->next)
181  {
182  attr_val_str =
184  le->claim->data,
185  le->claim->data_size);
186  json_object_set_new(body, le->claim->name, json_string(attr_val_str));
187  GNUNET_free(attr_val_str);
188  }
189  body_str = json_dumps(body, JSON_INDENT(0) | JSON_COMPACT);
190  json_decref(body);
191 
192  GNUNET_STRINGS_base64_encode(header, strlen(header), &header_base64);
193  fix_base64(header_base64);
194 
195  GNUNET_STRINGS_base64_encode(body_str, strlen(body_str), &body_base64);
196  fix_base64(body_base64);
197 
198  GNUNET_free(subject);
199  GNUNET_free(audience);
200 
205  GNUNET_asprintf(&signature_target, "%s.%s", header_base64, body_base64);
206  GNUNET_CRYPTO_hmac_raw(secret_key,
207  strlen(secret_key),
208  signature_target,
209  strlen(signature_target),
210  &signature);
211  GNUNET_STRINGS_base64_encode((const char *)&signature,
212  sizeof(struct GNUNET_HashCode),
213  &signature_base64);
214  fix_base64(signature_base64);
215 
216  GNUNET_asprintf(&result,
217  "%s.%s.%s",
218  header_base64,
219  body_base64,
220  signature_base64);
221 
222  GNUNET_free(signature_target);
223  GNUNET_free(header);
224  GNUNET_free(body_str);
225  GNUNET_free(signature_base64);
226  GNUNET_free(body_base64);
227  GNUNET_free(header_base64);
228  return result;
229 }
const char * name
The name of the attribute.
struct GNUNET_RECLAIM_ATTRIBUTE_Claim * claim
The attribute claim.
char * GNUNET_RECLAIM_ATTRIBUTE_value_to_string(uint32_t type, const void *data, size_t data_size)
Convert the &#39;claim&#39; of an attribute to a string.
size_t data_size
Number of bytes in data.
void GNUNET_CRYPTO_hmac_raw(const void *key, size_t key_len, const void *plaintext, size_t plaintext_len, struct GNUNET_HashCode *hmac)
Calculate HMAC of a message (RFC 2104) TODO: Shouldn&#39; this be the standard hmac function and the abov...
Definition: crypto_hash.c:383
struct GNUNET_TIME_Absolute GNUNET_TIME_absolute_add(struct GNUNET_TIME_Absolute start, struct GNUNET_TIME_Relative duration)
Add a given relative duration to the given start time.
Definition: time.c:393
#define SERVER_ADDRESS
Definition: oidc_helper.h:39
const void * data
Binary value stored as attribute value.
int GNUNET_asprintf(char **buf, const char *format,...)
Like asprintf, just portable.
static int result
Global testing status.
A 512-bit hashcode.
struct GNUNET_TIME_Absolute GNUNET_TIME_absolute_get(void)
Get the current time.
Definition: time.c:118
static void fix_base64(char *str)
Definition: oidc_helper.c:95
size_t GNUNET_STRINGS_base64_encode(const void *in, size_t len, char **output)
Encode into Base64.
Definition: strings.c:1856
static GNUNET_NETWORK_STRUCT_END char * create_jwt_header(void)
Definition: oidc_helper.c:67
Public ECC key (always for Curve25519) encoded in a format suitable for network transmission and ECDS...
Time for absolute times used by GNUnet, in microseconds.
char * GNUNET_STRINGS_data_to_string_alloc(const void *buf, size_t size)
Return the base32crockford encoding of the given buffer.
Definition: strings.c:921
struct GNUNET_RECLAIM_ATTRIBUTE_ClaimListEntry * next
DLL.
struct GNUNET_RECLAIM_ATTRIBUTE_ClaimListEntry * list_head
List head.
#define GNUNET_free(ptr)
Wrapper around free.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ from_hex()

static char from_hex ( char  ch)
static

Definition at line 233 of file oidc_helper.c.

Referenced by url_decode().

234 {
235  return isdigit(ch) ? ch - '0' : tolower(ch) - 'a' + 10;
236 }
static struct GNUNET_CADET_Channel * ch
Channel handle.
Definition: gnunet-cadet.c:117
Here is the caller graph for this function:

◆ to_hex()

static char to_hex ( char  code)
static

Definition at line 240 of file oidc_helper.c.

Referenced by url_encode().

241 {
242  static char hex[] = "0123456789abcdef";
243 
244  return hex[code & 15];
245 }
Here is the caller graph for this function:

◆ url_encode()

static char* url_encode ( const char *  str)
static

Definition at line 250 of file oidc_helper.c.

References buf, GNUNET_malloc, and to_hex().

Referenced by base64_and_urlencode().

251 {
252  char *pstr = (char *)str;
253  char *buf = GNUNET_malloc(strlen(str) * 3 + 1);
254  char *pbuf = buf;
255 
256  while (*pstr)
257  {
258  if (isalnum(*pstr) || *pstr == '-' || *pstr == '_' || *pstr == '.' ||
259  *pstr == '~')
260  *pbuf++ = *pstr;
261  else if (*pstr == ' ')
262  *pbuf++ = '+';
263  else
264  {
265  *pbuf++ = '%';
266  *pbuf++ = to_hex(*pstr >> 4);
267  *pbuf++ = to_hex(*pstr & 15);
268  }
269  pstr++;
270  }
271  *pbuf = '\0';
272  return buf;
273 }
static char to_hex(char code)
Definition: oidc_helper.c:240
static char buf[2048]
#define GNUNET_malloc(size)
Wrapper around malloc.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ url_decode()

static char* url_decode ( const char *  str)
static

Definition at line 279 of file oidc_helper.c.

References buf, from_hex(), and GNUNET_malloc.

280 {
281  char *pstr = (char *)str;
282  char *buf = GNUNET_malloc(strlen(str) + 1);
283  char *pbuf = buf;
284 
285  while (*pstr)
286  {
287  if (*pstr == '%')
288  {
289  if (pstr[1] && pstr[2])
290  {
291  *pbuf++ = from_hex(pstr[1]) << 4 | from_hex(pstr[2]);
292  pstr += 2;
293  }
294  }
295  else if (*pstr == '+')
296  {
297  *pbuf++ = ' ';
298  }
299  else
300  {
301  *pbuf++ = *pstr;
302  }
303  pstr++;
304  }
305  *pbuf = '\0';
306  return buf;
307 }
static char buf[2048]
static char from_hex(char ch)
Definition: oidc_helper.c:233
#define GNUNET_malloc(size)
Wrapper around malloc.
Here is the call graph for this function:

◆ base64_and_urlencode()

static char* base64_and_urlencode ( const char *  data,
size_t  data_size 
)
static

Returns base64 encoded string urlencoded.

Parameters
stringthe string to encode
Returns
base64 encoded string

Definition at line 316 of file oidc_helper.c.

References enc, GNUNET_free, GNUNET_STRINGS_base64_encode(), and url_encode().

Referenced by OIDC_build_authz_code().

317 {
318  char *enc;
319  char *urlenc;
320 
322  urlenc = url_encode(enc);
323  GNUNET_free(enc);
324  return urlenc;
325 }
size_t GNUNET_STRINGS_base64_encode(const void *in, size_t len, char **output)
Encode into Base64.
Definition: strings.c:1856
static char * url_encode(const char *str)
Definition: oidc_helper.c:250
static OpusEncoder * enc
OPUS encoder.
uint32_t data
The data value.
static size_t data_size
Number of bytes in data.
#define GNUNET_free(ptr)
Wrapper around free.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ base64url_encode()

static char* base64url_encode ( const char *  data,
size_t  data_size 
)
static

Returns base64 encoded string urlencoded.

Parameters
stringthe string to encode
Returns
base64 encoded string

Definition at line 335 of file oidc_helper.c.

References enc, and GNUNET_STRINGS_base64_encode().

Referenced by OIDC_parse_authz_code().

336 {
337  char *enc;
338  size_t pos;
339 
341  //Replace with correct characters for base64url
342  pos = 0;
343  while ('\0' != enc[pos])
344  {
345  if ('+' == enc[pos])
346  enc[pos] = '-';
347  if ('/' == enc[pos])
348  enc[pos] = '_';
349  if ('=' == enc[pos])
350  {
351  enc[pos] = '\0';
352  break;
353  }
354  pos++;
355  }
356  return enc;
357 }
size_t GNUNET_STRINGS_base64_encode(const void *in, size_t len, char **output)
Encode into Base64.
Definition: strings.c:1856
static OpusEncoder * enc
OPUS encoder.
uint32_t data
The data value.
static size_t data_size
Number of bytes in data.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ derive_aes_key()

static void derive_aes_key ( struct GNUNET_CRYPTO_SymmetricSessionKey key,
struct GNUNET_CRYPTO_SymmetricInitializationVector iv,
struct GNUNET_HashCode key_material 
)
static

Definition at line 361 of file oidc_helper.c.

References GNUNET_CRYPTO_kdf().

Referenced by calculate_key_priv(), and calculate_key_pub().

364 {
365  static const char ctx_key[] = "reclaim-aes-ctx-key";
366  static const char ctx_iv[] = "reclaim-aes-ctx-iv";
367 
368  GNUNET_CRYPTO_kdf(key,
369  sizeof(struct GNUNET_CRYPTO_SymmetricSessionKey),
370  ctx_key,
371  strlen(ctx_key),
372  key_material,
373  sizeof(struct GNUNET_HashCode),
374  NULL);
376  sizeof(
378  ctx_iv,
379  strlen(ctx_iv),
380  key_material,
381  sizeof(struct GNUNET_HashCode),
382  NULL);
383 }
A 512-bit hashcode.
int GNUNET_CRYPTO_kdf(void *result, size_t out_len, const void *xts, size_t xts_len, const void *skm, size_t skm_len,...)
Derive key.
Definition: crypto_kdf.c:91
Here is the call graph for this function:
Here is the caller graph for this function:

◆ calculate_key_priv()

static void calculate_key_priv ( struct GNUNET_CRYPTO_SymmetricSessionKey key,
struct GNUNET_CRYPTO_SymmetricInitializationVector iv,
const struct GNUNET_CRYPTO_EcdsaPrivateKey ecdsa_priv,
const struct GNUNET_CRYPTO_EcdhePublicKey ecdh_pub 
)
static

Definition at line 387 of file oidc_helper.c.

References derive_aes_key(), and GNUNET_CRYPTO_ecdsa_ecdh().

Referenced by decrypt_payload().

391 {
392  struct GNUNET_HashCode key_material;
393 
394  GNUNET_CRYPTO_ecdsa_ecdh(ecdsa_priv, ecdh_pub, &key_material);
395  derive_aes_key(key, iv, &key_material);
396 }
int GNUNET_CRYPTO_ecdsa_ecdh(const struct GNUNET_CRYPTO_EcdsaPrivateKey *priv, const struct GNUNET_CRYPTO_EcdhePublicKey *pub, struct GNUNET_HashCode *key_material)
Derive key material from a ECDH public key and a private ECDSA key.
Definition: crypto_ecc.c:1562
static void derive_aes_key(struct GNUNET_CRYPTO_SymmetricSessionKey *key, struct GNUNET_CRYPTO_SymmetricInitializationVector *iv, struct GNUNET_HashCode *key_material)
Definition: oidc_helper.c:361
A 512-bit hashcode.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ calculate_key_pub()

static void calculate_key_pub ( struct GNUNET_CRYPTO_SymmetricSessionKey key,
struct GNUNET_CRYPTO_SymmetricInitializationVector iv,
const struct GNUNET_CRYPTO_EcdsaPublicKey ecdsa_pub,
const struct GNUNET_CRYPTO_EcdhePrivateKey ecdh_priv 
)
static

Definition at line 400 of file oidc_helper.c.

References derive_aes_key(), and GNUNET_CRYPTO_ecdh_ecdsa().

Referenced by encrypt_payload().

404 {
405  struct GNUNET_HashCode key_material;
406 
407  GNUNET_CRYPTO_ecdh_ecdsa(ecdh_priv, ecdsa_pub, &key_material);
408  derive_aes_key(key, iv, &key_material);
409 }
int GNUNET_CRYPTO_ecdh_ecdsa(const struct GNUNET_CRYPTO_EcdhePrivateKey *priv, const struct GNUNET_CRYPTO_EcdsaPublicKey *pub, struct GNUNET_HashCode *key_material)
Derive key material from a EcDSA public key and a private ECDH key.
Definition: crypto_ecc.c:1667
static void derive_aes_key(struct GNUNET_CRYPTO_SymmetricSessionKey *key, struct GNUNET_CRYPTO_SymmetricInitializationVector *iv, struct GNUNET_HashCode *key_material)
Definition: oidc_helper.c:361
A 512-bit hashcode.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ decrypt_payload()

static void decrypt_payload ( const struct GNUNET_CRYPTO_EcdsaPrivateKey ecdsa_priv,
const struct GNUNET_CRYPTO_EcdhePublicKey ecdh_pub,
const char *  ct,
size_t  ct_len,
char *  buf 
)
static

Definition at line 413 of file oidc_helper.c.

References calculate_key_priv(), GNUNET_break, and GNUNET_CRYPTO_symmetric_decrypt().

Referenced by OIDC_parse_authz_code().

418 {
421 
422  calculate_key_priv(&key, &iv, ecdsa_priv, ecdh_pub);
424 }
#define GNUNET_break(cond)
Use this for internal assertion violations that are not fatal (can be handled) but should not occur...
static char buf[2048]
struct GNUNET_HashCode key
The key used in the DHT.
static void calculate_key_priv(struct GNUNET_CRYPTO_SymmetricSessionKey *key, struct GNUNET_CRYPTO_SymmetricInitializationVector *iv, const struct GNUNET_CRYPTO_EcdsaPrivateKey *ecdsa_priv, const struct GNUNET_CRYPTO_EcdhePublicKey *ecdh_pub)
Definition: oidc_helper.c:387
ssize_t GNUNET_CRYPTO_symmetric_decrypt(const void *block, size_t size, const struct GNUNET_CRYPTO_SymmetricSessionKey *sessionkey, const struct GNUNET_CRYPTO_SymmetricInitializationVector *iv, void *result)
Decrypt a given block using a symmetric sessionkey.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ encrypt_payload()

static void encrypt_payload ( const struct GNUNET_CRYPTO_EcdsaPublicKey ecdsa_pub,
const struct GNUNET_CRYPTO_EcdhePrivateKey ecdh_priv,
const char *  payload,
size_t  payload_len,
char *  buf 
)
static

Definition at line 428 of file oidc_helper.c.

References calculate_key_pub(), GNUNET_break, and GNUNET_CRYPTO_symmetric_encrypt().

Referenced by OIDC_build_authz_code().

433 {
436 
437  calculate_key_pub(&key, &iv, ecdsa_pub, ecdh_priv);
438  GNUNET_break(
439  GNUNET_CRYPTO_symmetric_encrypt(payload, payload_len, &key, &iv, buf));
440 }
static void calculate_key_pub(struct GNUNET_CRYPTO_SymmetricSessionKey *key, struct GNUNET_CRYPTO_SymmetricInitializationVector *iv, const struct GNUNET_CRYPTO_EcdsaPublicKey *ecdsa_pub, const struct GNUNET_CRYPTO_EcdhePrivateKey *ecdh_priv)
Definition: oidc_helper.c:400
#define GNUNET_break(cond)
Use this for internal assertion violations that are not fatal (can be handled) but should not occur...
ssize_t GNUNET_CRYPTO_symmetric_encrypt(const void *block, size_t size, const struct GNUNET_CRYPTO_SymmetricSessionKey *sessionkey, const struct GNUNET_CRYPTO_SymmetricInitializationVector *iv, void *result)
Encrypt a block using a symmetric sessionkey.
static char buf[2048]
struct GNUNET_HashCode key
The key used in the DHT.
static unsigned long long payload
How much data are we currently storing in the database?
Here is the call graph for this function:
Here is the caller graph for this function:

◆ OIDC_build_authz_code()

char* OIDC_build_authz_code ( const struct GNUNET_CRYPTO_EcdsaPrivateKey issuer,
const struct GNUNET_RECLAIM_Ticket ticket,
struct GNUNET_RECLAIM_ATTRIBUTE_ClaimList attrs,
const char *  nonce_str,
const char *  code_challenge 
)

Builds an OIDC authorization code including a reclaim ticket and nonce.

Parameters
issuerthe issuer of the ticket, used to sign the ticket and nonce
ticketthe ticket to include in the code
attrslist of attributes which are shared
noncethe nonce to include in the code
code_challengePKCE code challenge
Returns
a new authorization code (caller must free)

PLAINTEXT

END

ENCRYPT

Definition at line 454 of file oidc_helper.c.

References OIDC_Parameters::attr_list_len, GNUNET_RECLAIM_Ticket::audience, base64_and_urlencode(), OIDC_Parameters::code_challenge_len, encrypt_payload(), GNUNET_assert, GNUNET_break, GNUNET_CRYPTO_ecdhe_key_create(), GNUNET_CRYPTO_ecdhe_key_get_public(), GNUNET_CRYPTO_ecdsa_sign(), GNUNET_ERROR_TYPE_DEBUG, GNUNET_ERROR_TYPE_ERROR, GNUNET_free, GNUNET_log, GNUNET_malloc, GNUNET_RECLAIM_ATTRIBUTE_list_serialize(), GNUNET_RECLAIM_ATTRIBUTE_list_serialize_get_size(), GNUNET_SIGNATURE_PURPOSE_RECLAIM_CODE_SIGN, GNUNET_SYSERR, OIDC_Parameters::nonce, payload, GNUNET_CRYPTO_EccSignaturePurpose::purpose, GNUNET_CRYPTO_EccSignaturePurpose::size, and OIDC_Parameters::ticket.

Referenced by oidc_ticket_issue_cb().

459 {
460  struct OIDC_Parameters params;
461  char *code_payload;
462  char *payload;
463  char *tmp;
464  char *code_str;
465  char *buf_ptr = NULL;
466  size_t payload_len;
467  size_t code_payload_len;
468  size_t attr_list_len = 0;
469  size_t code_challenge_len = 0;
470  uint32_t nonce;
471  uint32_t nonce_tmp;
473  struct GNUNET_CRYPTO_EcdhePrivateKey *ecdh_priv;
474  struct GNUNET_CRYPTO_EcdhePublicKey ecdh_pub;
475 
477  // Assign ticket
478  memset(&params, 0, sizeof(params));
479  params.ticket = *ticket;
480  // Assign nonce
481  nonce = 0;
482  payload_len = sizeof(struct OIDC_Parameters);
483  if (NULL != nonce_str && strcmp("", nonce_str) != 0)
484  {
485  if ((1 != sscanf(nonce_str, "%u", &nonce)) || (nonce > UINT32_MAX))
486  {
487  GNUNET_break(0);
488  GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Invalid nonce %s\n", nonce_str);
489  return NULL;
490  }
492  "Got nonce: %u from %s\n",
493  nonce,
494  nonce_str);
495  }
496  nonce_tmp = htonl(nonce);
497  params.nonce = nonce_tmp;
498  // Assign code challenge
499  if (NULL != code_challenge)
500  code_challenge_len = strlen(code_challenge);
501  payload_len += code_challenge_len;
502  params.code_challenge_len = htonl(code_challenge_len);
503  // Assign attributes
504  if (NULL != attrs)
505  {
506  // Get length
508  params.attr_list_len = htonl(attr_list_len);
510  "Length of serialized attributes: %lu\n",
511  attr_list_len);
512  // Get serialized attributes
513  payload_len += attr_list_len;
514  }
515  // Get plaintext length
516  payload = GNUNET_malloc(payload_len);
517  memcpy(payload, &params, sizeof(params));
518  tmp = payload + sizeof(params);
519  if (0 < code_challenge_len)
520  {
521  memcpy(tmp, code_challenge, code_challenge_len);
522  tmp += code_challenge_len;
523  }
524  if (0 < attr_list_len)
529  // Get length
530  code_payload_len = sizeof(struct GNUNET_CRYPTO_EccSignaturePurpose) +
531  sizeof(struct GNUNET_CRYPTO_EcdhePublicKey) +
532  payload_len + sizeof(struct GNUNET_CRYPTO_EcdsaSignature);
534  "Length of data to encode: %lu\n",
535  code_payload_len);
536 
537  // Generate ECDH key
538  ecdh_priv = GNUNET_CRYPTO_ecdhe_key_create();
539  GNUNET_CRYPTO_ecdhe_key_get_public(ecdh_priv, &ecdh_pub);
540  // Initialize code payload
541  code_payload = GNUNET_malloc(code_payload_len);
542  GNUNET_assert(NULL != code_payload);
543  purpose = (struct GNUNET_CRYPTO_EccSignaturePurpose *)code_payload;
544  purpose->size = htonl(sizeof(struct GNUNET_CRYPTO_EccSignaturePurpose) +
545  sizeof(ecdh_pub) + payload_len);
547  // Store pubkey
548  buf_ptr = (char *)&purpose[1];
549  memcpy(buf_ptr, &ecdh_pub, sizeof(ecdh_pub));
550  buf_ptr += sizeof(ecdh_pub);
551  // Encrypt plaintext and store
552  encrypt_payload(&ticket->audience, ecdh_priv, payload, payload_len, buf_ptr);
553  GNUNET_free(ecdh_priv);
554  GNUNET_free(payload);
555  buf_ptr += payload_len;
556  // Sign and store signature
557  if (GNUNET_SYSERR ==
559  purpose,
561  buf_ptr))
562  {
563  GNUNET_break(0);
564  GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Unable to sign code\n");
565  GNUNET_free(code_payload);
566  return NULL;
567  }
568  code_str = base64_and_urlencode(code_payload, code_payload_len);
569  GNUNET_free(code_payload);
570  return code_str;
571 }
static void encrypt_payload(const struct GNUNET_CRYPTO_EcdsaPublicKey *ecdsa_pub, const struct GNUNET_CRYPTO_EcdhePrivateKey *ecdh_priv, const char *payload, size_t payload_len, char *buf)
Definition: oidc_helper.c:428
uint32_t purpose
What does this signature vouch for? This must contain a GNUNET_SIGNATURE_PURPOSE_XXX constant (from g...
uint32_t nonce
The nonce.
Definition: oidc_helper.c:51
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.
uint32_t attr_list_len
The length of the attributes list.
Definition: oidc_helper.c:61
struct GNUNET_CRYPTO_EcdhePrivateKey * GNUNET_CRYPTO_ecdhe_key_create(void)
Create a new private key.
Definition: crypto_ecc.c:610
#define GNUNET_break(cond)
Use this for internal assertion violations that are not fatal (can be handled) but should not occur...
header of what an ECC signature signs this must be followed by "size - 8" bytes of the actual signed ...
static struct GNUNET_RECLAIM_Ticket ticket
Ticket to consume.
int GNUNET_CRYPTO_ecdsa_sign(const struct GNUNET_CRYPTO_EcdsaPrivateKey *priv, const struct GNUNET_CRYPTO_EccSignaturePurpose *purpose, struct GNUNET_CRYPTO_EcdsaSignature *sig)
ECDSA Sign a given block.
Definition: crypto_ecc.c:931
#define GNUNET_SIGNATURE_PURPOSE_RECLAIM_CODE_SIGN
Signature for a GNUid Ticket.
uint32_t code_challenge_len
The length of the PKCE code_challenge.
Definition: oidc_helper.c:56
an ECC signature using ECDSA
uint32_t size
How many bytes does this signature sign? (including this purpose header); in network byte order (!)...
Private ECC key encoded for transmission.
#define GNUNET_SYSERR
Definition: gnunet_common.h:76
void GNUNET_CRYPTO_ecdhe_key_get_public(const struct GNUNET_CRYPTO_EcdhePrivateKey *priv, struct GNUNET_CRYPTO_EcdhePublicKey *pub)
Extract the public key for the given private key.
Definition: crypto_ecc.c:303
static unsigned long long payload
How much data are we currently storing in the database?
struct GNUNET_CRYPTO_EcdsaPublicKey audience
The ticket audience (= relying party)
static char * base64_and_urlencode(const char *data, size_t data_size)
Returns base64 encoded string urlencoded.
Definition: oidc_helper.c:316
Public ECC key (always for Curve25519) encoded in a format suitable for network transmission and encr...
#define GNUNET_log(kind,...)
size_t GNUNET_RECLAIM_ATTRIBUTE_list_serialize_get_size(const struct GNUNET_RECLAIM_ATTRIBUTE_ClaimList *attrs)
Get required size for serialization buffer.
size_t GNUNET_RECLAIM_ATTRIBUTE_list_serialize(const struct GNUNET_RECLAIM_ATTRIBUTE_ClaimList *attrs, char *result)
Serialize an attribute list.
#define GNUNET_malloc(size)
Wrapper around malloc.
#define GNUNET_free(ptr)
Wrapper around free.
The signature used to generate the authorization code.
Definition: oidc_helper.c:42
Here is the call graph for this function:
Here is the caller graph for this function:

◆ OIDC_parse_authz_code()

int OIDC_parse_authz_code ( const struct GNUNET_CRYPTO_EcdsaPrivateKey ecdsa_priv,
const char *  code,
const char *  code_verifier,
struct GNUNET_RECLAIM_Ticket ticket,
struct GNUNET_RECLAIM_ATTRIBUTE_ClaimList **  attrs,
char **  nonce_str 
)

Parse reclaim ticket and nonce from authorization code.

This also verifies the signature in the code.

Parameters
audiencethe expected audience of the code
codethe string representation of the code
code_verfierPKCE code verifier
ticketwhere to store the ticket
attrsthe attributes in the code
noncewhere to store the nonce
Returns
GNUNET_OK if successful, else GNUNET_SYSERR

Definition at line 588 of file oidc_helper.c.

References OIDC_Parameters::attr_list_len, GNUNET_RECLAIM_Ticket::audience, base64url_encode(), OIDC_Parameters::code_challenge_len, decrypt_payload(), GNUNET_asprintf(), GNUNET_CRYPTO_ecdsa_key_get_public(), GNUNET_CRYPTO_ecdsa_verify(), GNUNET_ERROR_TYPE_DEBUG, GNUNET_ERROR_TYPE_ERROR, GNUNET_free, GNUNET_free_non_null, GNUNET_log, GNUNET_malloc, GNUNET_memcmp, GNUNET_OK, GNUNET_RECLAIM_ATTRIBUTE_list_deserialize(), GNUNET_SIGNATURE_PURPOSE_RECLAIM_CODE_SIGN, GNUNET_STRINGS_base64_decode(), GNUNET_SYSERR, GNUNET_RECLAIM_Ticket::identity, OIDC_Parameters::nonce, GNUNET_CRYPTO_EccSignaturePurpose::purpose, and OIDC_Parameters::ticket.

Referenced by token_endpoint().

594 {
595  char *code_payload;
596  char *ptr;
597  char *plaintext;
598  char *attrs_ser;
599  char *expected_code_challenge;
600  char *code_challenge;
601  char *code_verifier_hash;
603  struct GNUNET_CRYPTO_EcdsaSignature *signature;
604  struct GNUNET_CRYPTO_EcdsaPublicKey ecdsa_pub;
605  struct GNUNET_CRYPTO_EcdhePublicKey *ecdh_pub;
606  uint32_t code_challenge_len;
607  uint32_t attrs_ser_len;
608  size_t plaintext_len;
609  size_t code_payload_len;
610  uint32_t nonce = 0;
611  struct OIDC_Parameters *params;
612 
613  GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Trying to decode `%s'\n", code);
614  code_payload = NULL;
615  code_payload_len =
616  GNUNET_STRINGS_base64_decode(code, strlen(code), (void **)&code_payload);
617  if (code_payload_len < sizeof(struct GNUNET_CRYPTO_EccSignaturePurpose) +
618  sizeof(struct GNUNET_CRYPTO_EcdhePublicKey) +
619  sizeof(struct OIDC_Parameters) +
620  sizeof(struct GNUNET_CRYPTO_EcdsaSignature))
621  {
622  GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Authorization code malformed\n");
623  GNUNET_free_non_null(code_payload);
624  return GNUNET_SYSERR;
625  }
626 
627  purpose = (struct GNUNET_CRYPTO_EccSignaturePurpose *)code_payload;
628  plaintext_len = code_payload_len;
629  plaintext_len -= sizeof(struct GNUNET_CRYPTO_EccSignaturePurpose);
630  ptr = (char *)&purpose[1];
631  // Public ECDH key
632  ecdh_pub = (struct GNUNET_CRYPTO_EcdhePublicKey *)ptr;
633  ptr += sizeof(struct GNUNET_CRYPTO_EcdhePublicKey);
634  plaintext_len -= sizeof(struct GNUNET_CRYPTO_EcdhePublicKey);
635 
636  // Decrypt ciphertext
637  plaintext_len -= sizeof(struct GNUNET_CRYPTO_EcdsaSignature);
638  plaintext = GNUNET_malloc(plaintext_len);
639  decrypt_payload(ecdsa_priv, ecdh_pub, ptr, plaintext_len, plaintext);
640  //ptr = plaintext;
641  ptr += plaintext_len;
642  signature = (struct GNUNET_CRYPTO_EcdsaSignature *)ptr;
643  params = (struct OIDC_Parameters *)plaintext;
644 
645  // cmp code_challenge code_verifier
646  code_challenge_len = ntohl(params->code_challenge_len);
647  if (0 != code_challenge_len) /* Only check if this code requires a CV */
648  {
649  code_verifier_hash = GNUNET_malloc(256 / 8);
650  // hash code verifier
651  gcry_md_hash_buffer(GCRY_MD_SHA256,
652  code_verifier_hash,
653  code_verifier,
654  strlen(code_verifier));
655  // encode code verifier
656  expected_code_challenge = base64url_encode(code_verifier_hash, 256 / 8);
657  code_challenge = (char *)&params[1];
658  GNUNET_free(code_verifier_hash);
659  if ((strlen(expected_code_challenge) != code_challenge_len) ||
660  (0 !=
661  strncmp(expected_code_challenge, code_challenge, code_challenge_len)))
662  {
664  "Invalid code verifier! Expected: %s, Got: %.*s\n",
665  expected_code_challenge,
666  code_challenge_len,
667  code_challenge);
668  GNUNET_free_non_null(code_payload);
669  GNUNET_free(expected_code_challenge);
670  return GNUNET_SYSERR;
671  }
672  GNUNET_free(expected_code_challenge);
673  }
674  // Ticket
675  memcpy(ticket, &params->ticket, sizeof(params->ticket));
676  // Nonce
677  nonce = ntohl(params->nonce); //ntohl (*((uint32_t *) ptr));
678  GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Got nonce: %u\n", nonce);
679  // Signature
680  GNUNET_CRYPTO_ecdsa_key_get_public(ecdsa_priv, &ecdsa_pub);
681  if (0 != GNUNET_memcmp(&ecdsa_pub, &ticket->audience))
682  {
683  GNUNET_free(code_payload);
684  GNUNET_free(plaintext);
686  "Audience in ticket does not match client!\n");
687  return GNUNET_SYSERR;
688  }
689  if (GNUNET_OK !=
691  purpose,
692  signature,
693  &ticket->identity))
694  {
695  GNUNET_free(code_payload);
696  GNUNET_free(plaintext);
697  GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Signature of AuthZ code invalid!\n");
698  return GNUNET_SYSERR;
699  }
700  // Attributes
701  attrs_ser = ((char *)&params[1]) + code_challenge_len;
702  attrs_ser_len = ntohl(params->attr_list_len);
703  *attrs = GNUNET_RECLAIM_ATTRIBUTE_list_deserialize(attrs_ser, attrs_ser_len);
704 
705  *nonce_str = NULL;
706  if (nonce != 0)
707  GNUNET_asprintf(nonce_str, "%u", nonce);
708  GNUNET_free(code_payload);
709  GNUNET_free(plaintext);
710  return GNUNET_OK;
711 }
int GNUNET_CRYPTO_ecdsa_verify(uint32_t purpose, const struct GNUNET_CRYPTO_EccSignaturePurpose *validate, const struct GNUNET_CRYPTO_EcdsaSignature *sig, const struct GNUNET_CRYPTO_EcdsaPublicKey *pub)
Verify ECDSA signature.
Definition: crypto_ecc.c:1048
uint32_t purpose
What does this signature vouch for? This must contain a GNUNET_SIGNATURE_PURPOSE_XXX constant (from g...
uint32_t nonce
The nonce.
Definition: oidc_helper.c:51
uint32_t attr_list_len
The length of the attributes list.
Definition: oidc_helper.c:61
#define GNUNET_OK
Named constants for return values.
Definition: gnunet_common.h:75
#define GNUNET_free_non_null(ptr)
Free the memory pointed to by ptr if ptr is not NULL.
static void decrypt_payload(const struct GNUNET_CRYPTO_EcdsaPrivateKey *ecdsa_priv, const struct GNUNET_CRYPTO_EcdhePublicKey *ecdh_pub, const char *ct, size_t ct_len, char *buf)
Definition: oidc_helper.c:413
header of what an ECC signature signs this must be followed by "size - 8" bytes of the actual signed ...
int GNUNET_asprintf(char **buf, const char *format,...)
Like asprintf, just portable.
#define GNUNET_SIGNATURE_PURPOSE_RECLAIM_CODE_SIGN
Signature for a GNUid Ticket.
uint32_t code_challenge_len
The length of the PKCE code_challenge.
Definition: oidc_helper.c:56
an ECC signature using ECDSA
size_t GNUNET_STRINGS_base64_decode(const char *data, size_t len, void **output)
Decode from Base64.
Definition: strings.c:1920
struct GNUNET_CRYPTO_EcdsaPublicKey identity
The ticket issuer (= the user)
#define GNUNET_SYSERR
Definition: gnunet_common.h:76
void GNUNET_CRYPTO_ecdsa_key_get_public(const struct GNUNET_CRYPTO_EcdsaPrivateKey *priv, struct GNUNET_CRYPTO_EcdsaPublicKey *pub)
Extract the public key for the given private key.
Definition: crypto_ecc.c:241
#define GNUNET_memcmp(a, b)
Compare memory in a and b, where both must be of the same pointer type.
struct GNUNET_CRYPTO_EcdsaPublicKey audience
The ticket audience (= relying party)
Public ECC key (always for Curve25519) encoded in a format suitable for network transmission and ECDS...
static char * base64url_encode(const char *data, size_t data_size)
Returns base64 encoded string urlencoded.
Definition: oidc_helper.c:335
Public ECC key (always for Curve25519) encoded in a format suitable for network transmission and encr...
#define GNUNET_log(kind,...)
struct GNUNET_RECLAIM_Ticket ticket
The reclaim ticket.
Definition: oidc_helper.c:46
struct GNUNET_RECLAIM_ATTRIBUTE_ClaimList * GNUNET_RECLAIM_ATTRIBUTE_list_deserialize(const char *data, size_t data_size)
Deserialize an attribute list.
#define GNUNET_malloc(size)
Wrapper around malloc.
#define GNUNET_free(ptr)
Wrapper around free.
The signature used to generate the authorization code.
Definition: oidc_helper.c:42
Here is the call graph for this function:
Here is the caller graph for this function:

◆ OIDC_build_token_response()

void OIDC_build_token_response ( const char *  access_token,
const char *  id_token,
const struct GNUNET_TIME_Relative expiration_time,
char **  token_response 
)

Build a token response for a token request TODO: Maybe we should add the scope here?

Parameters
access_tokenthe access token to include
id_tokenthe id_token to include
expiration_timethe expiration time of the token(s)
token_responsewhere to store the response

Definition at line 724 of file oidc_helper.c.

References GNUNET_assert, and GNUNET_TIME_Relative::rel_value_us.

Referenced by token_endpoint().

728 {
729  json_t *root_json;
730 
731  root_json = json_object();
732 
733  GNUNET_assert(NULL != access_token);
734  GNUNET_assert(NULL != id_token);
735  GNUNET_assert(NULL != expiration_time);
736  json_object_set_new(root_json, "access_token", json_string(access_token));
737  json_object_set_new(root_json, "token_type", json_string("Bearer"));
738  json_object_set_new(root_json,
739  "expires_in",
740  json_integer(expiration_time->rel_value_us /
741  (1000 * 1000)));
742  json_object_set_new(root_json, "id_token", json_string(id_token));
743  *token_response = json_dumps(root_json, JSON_INDENT(0) | JSON_COMPACT);
744  json_decref(root_json);
745 }
uint64_t rel_value_us
The actual value.
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.
Here is the caller graph for this function:

◆ OIDC_access_token_new()

char* OIDC_access_token_new ( )

Generate a new access token.

Definition at line 751 of file oidc_helper.c.

References GNUNET_CRYPTO_QUALITY_NONCE, GNUNET_CRYPTO_random_u64(), and GNUNET_STRINGS_base64_encode().

Referenced by token_endpoint().

752 {
753  char *access_token;
754  uint64_t random_number;
755 
756  random_number =
758  GNUNET_STRINGS_base64_encode(&random_number,
759  sizeof(uint64_t),
760  &access_token);
761  return access_token;
762 }
uint64_t GNUNET_CRYPTO_random_u64(enum GNUNET_CRYPTO_Quality mode, uint64_t max)
Random on unsigned 64-bit values.
Randomness for IVs etc.
size_t GNUNET_STRINGS_base64_encode(const void *in, size_t len, char **output)
Encode into Base64.
Definition: strings.c:1856
Here is the call graph for this function:
Here is the caller graph for this function: