-merge
[oweals/gnunet.git] / src / identity-provider / identity_token.h
1 /*
2    This file is part of GNUnet.
3    Copyright (C) 2012-2015 GNUnet e.V.
4
5    GNUnet is free software; you can redistribute it and/or modify
6    it under the terms of the GNU General Public License as published
7    by the Free Software Foundation; either version 3, or (at your
8    option) any later version.
9
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    General Public License for more details.
14
15    You should have received a copy of the GNU General Public License
16    along with GNUnet; see the file COPYING.  If not, write to the
17    Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18    Boston, MA 02110-1301, USA.
19    */
20 /**
21  * @author Martin Schanzenbach
22  * @file identity-provider/identity_token.h
23  * @brief GNUnet Identity Provider library
24  *
25  */
26 #ifndef IDENTITY_TOKEN_H
27 #define IDENTITY_TOKEN_H
28
29 #include "gnunet_crypto_lib.h"
30 #include <jansson.h>
31
32 struct IdentityToken
33 {
34   /**
35    * DLL
36    */
37   struct TokenAttr *attr_head;
38
39   /**
40    * DLL
41    */
42   struct TokenAttr *attr_tail;
43
44   /**
45    * Token Signature
46    */
47   struct GNUNET_CRYPTO_EcdsaSignature signature;
48   
49   /**
50    * Audience Pubkey
51    */
52   struct GNUNET_CRYPTO_EcdsaPublicKey aud_key;
53 };
54
55 struct TokenAttr
56 {
57   /**
58    * DLL
59    */
60   struct TokenAttr *next;
61
62   /**
63    * DLL
64    */
65   struct TokenAttr *prev;
66
67   /**
68    * Attribute name
69    */
70   char *name;
71
72   /**
73    * Attribute value DLL
74    */
75   struct TokenAttrValue *val_head;
76
77   /**
78    * Attribute value DLL
79    */
80   struct TokenAttrValue *val_tail;
81
82 };
83
84 struct TokenAttrValue
85 {
86   /**
87    * DLL
88    */
89   struct TokenAttrValue *next;
90
91   /**
92    * DLL
93    */
94   struct TokenAttrValue *prev;
95
96   /**
97    * Attribute value
98    */
99   char *value;
100
101   /**
102    * Attribute int value
103    * used if NULL == value
104    */
105   uint64_t int_value;
106
107   /**
108    * Json value
109    */
110   json_t *json_value;
111 };
112
113 struct TokenTicketPayload
114 {
115   /**
116    * Nonce
117    */
118   uint64_t nonce;
119
120   /**
121    * Label
122    */
123   char *label;
124
125   /**
126    * Issuing Identity
127    */
128   struct GNUNET_CRYPTO_EcdsaPublicKey identity_key;
129 };
130
131
132 struct TokenTicket
133 {
134   /**
135    * Meta info
136    */
137   struct TokenTicketPayload *payload;
138
139   /**
140    * ECDH Pubkey
141    */
142   struct GNUNET_CRYPTO_EcdhePublicKey ecdh_pubkey;
143
144   /**
145    * Signature
146    */
147   struct GNUNET_CRYPTO_EcdsaSignature signature;
148
149   /**
150    * Target identity
151    */
152   struct GNUNET_CRYPTO_EcdsaPublicKey aud_key;
153 };
154
155
156
157 /**
158  * Create an identity token
159  *
160  * @param iss the issuer string for the token
161  * @param aud the audience of the token
162  *
163  * @return a new token
164  */
165 struct IdentityToken*
166 token_create (const struct GNUNET_CRYPTO_EcdsaPublicKey *iss,
167                                        const struct GNUNET_CRYPTO_EcdsaPublicKey* aud);
168
169 /**
170  * Destroy an identity token
171  *
172  * @param token the token to destroy
173  */
174 void
175 token_destroy (struct IdentityToken*token);
176
177 /**
178  * Add a new key value pair to the token
179  * 
180  * @param token the token to modify
181  * @param key the key
182  * @param value the value
183  */
184 void
185 token_add_attr (struct IdentityToken *token,
186                 const char* key,
187                 const char* value);
188
189 /**
190  * Add a new key value pair to the token
191  * 
192  * @param token the token to modify
193  * @param key the key
194  * @param value the value
195  */
196 void
197 token_add_attr_int (struct IdentityToken *token,
198                       const char* key,
199                       uint64_t value);
200
201
202
203 /**
204  * Add a value to a TokenAttribute
205  *
206  * @param attr the token attribute
207  * @param value value to add
208  */
209   void
210   token_attr_add_value (const struct TokenAttr *attr,
211                         const char *value);
212
213 /**
214  * Add a new key value pair to the token with the value as json
215  *
216  * @param the token to modify
217  * @param key the key
218  * @param value the value
219  *
220  */
221 void
222 token_add_attr_json (struct IdentityToken *token,
223                      const char* key,
224                      json_t* value);
225
226 /**
227  * Serialize a token. The token will be signed and base64 according to the
228  * JWT format. The signature is base32-encoded ECDSA.
229  * The resulting JWT is encrypted using 
230  * ECDHE for the audience and Base64
231  * encoded in result. The audience requires the ECDHE public key P 
232  * to decrypt the token T. The key P is included in the result and prepended
233  * before the token
234  *
235  * @param token the token to serialize
236  * @param priv_key the private key used to sign the token
237  * @param ecdhe_privkey the ECDHE private key used to encrypt the token
238  * @param result P,Base64(E(T))
239  *
240  * @return GNUNET_OK on success
241  */
242 int 
243 token_serialize (const struct IdentityToken*token,
244                  const struct GNUNET_CRYPTO_EcdsaPrivateKey *priv_key,
245                  struct GNUNET_CRYPTO_EcdhePrivateKey **ecdhe_privkey,
246                  char **result);
247
248 /**
249  * Parses the serialized token and returns a token
250  *
251  * @param data the serialized token
252  * @param priv_key the private key of the audience
253  * @param result the token
254  *
255  * @return GNUNET_OK on success
256  */
257 int
258 token_parse (const char* data,
259              const struct GNUNET_CRYPTO_EcdsaPrivateKey *priv_key,
260              struct IdentityToken **result);
261
262 /**
263  * Parses the serialized token and returns a token
264  * This variant is intended for the party that issued the token and also
265  * wants to decrypt the serialized token.
266  *
267  * @param data the serialized token
268  * @param priv_key the private (!) ECDHE key
269  * @param aud_key the identity of the audience
270  * @param result the token
271  *
272  * @return GNUNET_OK on success
273  */
274 int
275 token_parse2 (const char* data,
276               const struct GNUNET_CRYPTO_EcdhePrivateKey *priv_key,
277               const struct GNUNET_CRYPTO_EcdsaPublicKey *aud_key,
278               struct IdentityToken **result);
279
280
281 /**
282  *
283  * Returns a JWT-string representation of the token
284  *
285  * @param token the token
286  * @param priv_key the private key used to sign the JWT
287  * @param result the JWT
288  *
289  * @return GNUNET_OK on success
290  */
291 int
292 token_to_string (const struct IdentityToken *token,
293                  const struct GNUNET_CRYPTO_EcdsaPrivateKey *priv_key,
294                  char **result);
295
296 /**
297  *
298  * Creates a ticket that can be exchanged by the audience for 
299  * the token. The token must be placed under the label
300  *
301  * @param nonce nonce provided by the audience that requested the ticket
302  * @param iss_pkey the issuer pubkey used to sign the ticket
303  * @param label the label encoded in the ticket
304  * @param aud_ley the audience pubkey used to encrypt the ticket payload
305  *
306  * @return the ticket
307  */
308 struct TokenTicket*
309 ticket_create (uint64_t nonce,
310                const struct GNUNET_CRYPTO_EcdsaPublicKey* iss_pkey,
311                const char* lbl_str,
312                const struct GNUNET_CRYPTO_EcdsaPublicKey *aud_key);
313
314 /**
315  * Serialize a ticket. Returns the Base64 representation of the ticket.
316  * Format: Base64( { payload: E(Payload), ecdhe: K, signature: signature } )
317  *
318  * @param ticket the ticket to serialize
319  * @param priv_key the issuer private key to sign the ticket payload
320  * @param result the serialized ticket
321  *
322  * @return GNUNET_OK on success
323  */
324 int
325 ticket_serialize (struct TokenTicket *ticket,
326                   const struct GNUNET_CRYPTO_EcdsaPrivateKey *priv_key,
327                   char **result);
328
329 /**
330  * Destroys a ticket
331  *
332  * @param the ticket to destroy
333  */
334 void
335 ticket_destroy (struct TokenTicket *ticket);
336
337 /**
338  * Parses a serialized ticket
339  *
340  * @param data the serialized ticket
341  * @param priv_key the audience private key
342  * @param ticket the ticket
343  *
344  * @return GNUNET_OK on success
345  */
346 int
347 ticket_parse (const char* raw_data,
348               const struct GNUNET_CRYPTO_EcdsaPrivateKey *priv_key,
349               struct TokenTicket **ticket);
350
351 #endif