aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorTristan Schwieren <tristan.schwieren@tum.de>2022-06-07 15:23:05 +0200
committerTristan Schwieren <tristan.schwieren@tum.de>2022-06-07 15:23:05 +0200
commit45d01b5d9a1ab852677bc5c482980d9f15e20b79 (patch)
tree82f282f3bb3b1c0e8872477faeae55d3614ac169 /src
parent5ca196a44d74fd8f9af5f3d7ec8cf33ce7aaaa6c (diff)
downloadgnunet-45d01b5d9a1ab852677bc5c482980d9f15e20b79.tar.gz
gnunet-45d01b5d9a1ab852677bc5c482980d9f15e20b79.zip
- finished DID helper with tests
Diffstat (limited to 'src')
-rw-r--r--src/reclaim/Makefile.am6
-rw-r--r--src/reclaim/did_helper.c104
-rw-r--r--src/reclaim/did_helper.h26
-rw-r--r--src/reclaim/test_did_helper.c21
-rw-r--r--src/reclaim/test_w3c_ed25519_2020.c69
5 files changed, 78 insertions, 148 deletions
diff --git a/src/reclaim/Makefile.am b/src/reclaim/Makefile.am
index 694ce0598..89c361585 100644
--- a/src/reclaim/Makefile.am
+++ b/src/reclaim/Makefile.am
@@ -214,11 +214,6 @@ gnunet_did_LDADD = \
214 -ljansson 214 -ljansson
215 215
216 216
217test_w3c_ed25519_2020_SOURCES = \
218 test_w3c_ed25519_2020.c
219test_w3c_ed25519_2020_LDADD = \
220 $(top_builddir)/src/util/libgnunetutil.la
221
222test_did_helper_SOURCES = \ 217test_did_helper_SOURCES = \
223 test_did_helper.c 218 test_did_helper.c
224test_did_helper_LDADD = \ 219test_did_helper_LDADD = \
@@ -237,7 +232,6 @@ check_SCRIPTS = \
237 232
238check_PROGRAMS = \ 233check_PROGRAMS = \
239 test_reclaim_attribute \ 234 test_reclaim_attribute \
240 test_w3c_ed25519_2020 \
241 test_did_helper 235 test_did_helper
242 236
243if ENABLE_TEST_RUN 237if ENABLE_TEST_RUN
diff --git a/src/reclaim/did_helper.c b/src/reclaim/did_helper.c
index 8452c9d34..c579a5cca 100644
--- a/src/reclaim/did_helper.c
+++ b/src/reclaim/did_helper.c
@@ -31,12 +31,8 @@
31#include "did_helper.h" 31#include "did_helper.h"
32#include "jansson.h" 32#include "jansson.h"
33 33
34 34#define STR_INDIR(x) #x
35// TODO: GNUNET_DID_key_covert_multibase_base64_to_gnunet 35#define STR(x) STR_INDIR (x)
36// TODO: GNUNET_DID_key_covert_gnunet_to_multibase_base64
37
38// TODO: GNUNET_DID_pkey_to_did_document
39
40 36
41/** 37/**
42 * @brief Generate a DID for a given GNUNET public key 38 * @brief Generate a DID for a given GNUNET public key
@@ -119,7 +115,21 @@ GNUNET_DID_key_covert_gnunet_to_multibase_base64 (struct
119 GNUNET_IDENTITY_PublicKey * 115 GNUNET_IDENTITY_PublicKey *
120 pkey) 116 pkey)
121{ 117{
122 return NULL; 118 struct GNUNET_CRYPTO_EddsaPublicKey pubkey = pkey->eddsa_key;
119
120 // This is how to convert out pubkeys to W3c Ed25519-2020 multibase (base64url no padding)
121 char *pkey_base_64;
122 char *pkey_multibase;
123 char pkx[34];
124
125 pkx[0] = 0xed;
126 pkx[1] = 0x01;
127 memcpy (pkx + 2, &pubkey, sizeof (pubkey));
128 GNUNET_STRINGS_base64url_encode (pkx, sizeof (pkx), &pkey_base_64);
129 GNUNET_asprintf (&pkey_multibase, "u%s", pkey_base_64);
130
131 GNUNET_free (pkey_base_64);
132 return pkey_multibase;
123} 133}
124 134
125/** 135/**
@@ -131,28 +141,6 @@ GNUNET_DID_key_covert_gnunet_to_multibase_base64 (struct
131char * 141char *
132GNUNET_DID_pkey_to_did_document (struct GNUNET_IDENTITY_PublicKey *pkey) 142GNUNET_DID_pkey_to_did_document (struct GNUNET_IDENTITY_PublicKey *pkey)
133{ 143{
134 char *did_str;
135 char *didd_str;
136 char *verify_id_str;
137 char *pkey_multibase_str;
138
139 /* FIXME-MSC: This screams for a GNUNET_DID_identity_key_to_string() */
140 // char *b64;
141 // char pkx[34];
142 // pkx[0] = 0xed;
143 // pkx[1] = 0x01;
144 // memcpy (pkx + 2, &(pkey->eddsa_key), sizeof(pkey->eddsa_key));
145 // GNUNET_STRINGS_base64_encode (pkx, sizeof(pkx), &b64);
146
147 // GNUNET_asprintf (&pkey_multibase_str, "u%s", b64);
148 pkey_multibase_str = "moin";
149
150 json_t *didd_json;
151
152 did_str = GNUNET_DID_pkey_to_did (pkey);
153 GNUNET_asprintf (&verify_id_str, "%s#key-1", did_str);
154
155 // sprintf(pkey_multibase_str, "V%s", pkey_str); // Convert the public key to MultiBase data format
156 144
157 /* FIXME-MSC: This is effectively creating a DID Document default template for 145 /* FIXME-MSC: This is effectively creating a DID Document default template for
158 * the initial document. 146 * the initial document.
@@ -161,35 +149,45 @@ GNUNET_DID_pkey_to_did_document (struct GNUNET_IDENTITY_PublicKey *pkey)
161 * GNUNET_DID_document_template_from_identity() 149 * GNUNET_DID_document_template_from_identity()
162 */ 150 */
163 151
164 // Add a relative DID URL to reference a verifiation method 152 char *did_str;
165 // https://www.w3.org/TR/did-core/#relative-did-urls` 153 char *verify_id_str;
166 154 char *pkey_multibase_str;
167 didd_json = json_pack ("{s:[ss], s:s, s:[{s:s, s:s, s:s, s:s}], s:[s], s:[s]}", 155 char *didd_str;
168 "@context", 156 json_t *didd_json;
169 "https://www.w3.org/ns/did/v1", 157
170 "https://w3id.org/security/suites/ed25519-2020/v1", 158 did_str = GNUNET_DID_pkey_to_did (pkey);
171 "id", 159 GNUNET_asprintf (&verify_id_str, "%s#key-1", did_str);
172 did_str, 160
173 "verificationMethod", 161 pkey_multibase_str = GNUNET_DID_key_covert_gnunet_to_multibase_base64 (pkey);
174 "id", 162
175 verify_id_str, 163 didd_json = json_pack (
176 "type", 164 "{s:[ss], s:s, s:[{s:s, s:s, s:s, s:s}], s:[s], s:[s]}",
177 "Ed25519VerificationKey2020", 165 "@context",
178 "controller", 166 "https://www.w3.org/ns/did/v1",
179 did_str, 167 "https://w3id.org/security/suites/ed25519-2020/v1",
180 "publicKeyMultibase", 168 "id",
181 pkey_multibase_str, 169 did_str,
182 "authentication", 170 "verificationMethod",
183 "#key-1", 171 "id",
184 "assertionMethod", 172 verify_id_str,
185 "#key-1"); 173 "type",
174 "Ed25519VerificationKey2020",
175 "controller",
176 did_str,
177 "publicKeyMultibase",
178 pkey_multibase_str,
179 "authentication",
180 "#key-1",
181 "assertionMethod",
182 "#key-1");
186 183
187 // Encode DID Document as JSON string 184 // Encode DID Document as JSON string
188 didd_str = json_dumps (didd_json, JSON_INDENT (2)); 185 didd_str = json_dumps (didd_json, JSON_INDENT (2));
189 186
190 // Free 187 // Free
191 // GNUNET_free (pkey_multibase_str); 188 GNUNET_free (did_str);
192 // GNUNET_free (b64); 189 GNUNET_free (verify_id_str);
190 GNUNET_free (pkey_multibase_str);
193 json_decref (didd_json); 191 json_decref (didd_json);
194 192
195 return didd_str; 193 return didd_str;
diff --git a/src/reclaim/did_helper.h b/src/reclaim/did_helper.h
index 2eb3ed8bd..11bab0354 100644
--- a/src/reclaim/did_helper.h
+++ b/src/reclaim/did_helper.h
@@ -24,9 +24,6 @@
24 * @author Tristan Schwieren 24 * @author Tristan Schwieren
25 */ 25 */
26 26
27#define STR_INDIR(x) #x
28#define STR(x) STR_INDIR(x)
29
30#define GNUNET_DID_METHOD_PREFIX "did:reclaim:" 27#define GNUNET_DID_METHOD_PREFIX "did:reclaim:"
31#define MAX_DID_SPECIFIC_IDENTIFIER_LENGTH 59 28#define MAX_DID_SPECIFIC_IDENTIFIER_LENGTH 59
32 29
@@ -34,16 +31,16 @@
34 * @brief Return a DID for a given GNUNET public key 31 * @brief Return a DID for a given GNUNET public key
35 */ 32 */
36char * 33char *
37GNUNET_DID_pkey_to_did(struct GNUNET_IDENTITY_PublicKey *pkey); 34GNUNET_DID_pkey_to_did (struct GNUNET_IDENTITY_PublicKey *pkey);
38 35
39/** 36/**
40 * @brief Generate a DID for a given gnunet EGO 37 * @brief Generate a DID for a given gnunet EGO
41 * 38 *
42 * @param ego 39 * @param ego
43 * @return char * Returns the DID. Caller must free 40 * @return char * Returns the DID. Caller must free
44 */ 41 */
45char * 42char *
46GNUNET_DID_identity_to_did(struct GNUNET_IDENTITY_Ego *ego); 43GNUNET_DID_identity_to_did (struct GNUNET_IDENTITY_Ego *ego);
47 44
48/** 45/**
49 * @brief Return the public key of a DID 46 * @brief Return the public key of a DID
@@ -51,17 +48,18 @@ GNUNET_DID_identity_to_did(struct GNUNET_IDENTITY_Ego *ego);
51int 48int
52GNUNET_DID_did_to_pkey (char *did, struct GNUNET_IDENTITY_PublicKey *pkey); 49GNUNET_DID_did_to_pkey (char *did, struct GNUNET_IDENTITY_PublicKey *pkey);
53 50
54/** 51// /**
55 * @brief Convert a base 64 encoded public key to a GNUNET key 52// * @brief Convert a base 64 encoded public key to a GNUNET key
56 */ 53// */
57struct GNUNET_IDENTITY_PublicKey * 54// struct GNUNET_IDENTITY_PublicKey *
58GNUNET_DID_key_covert_multibase_base64_to_gnunet(char *); 55// GNUNET_DID_key_covert_multibase_base64_to_gnunet(char *);
59 56
60/** 57/**
61 * @brief Convert GNUNET key to a base 64 encoded public key 58 * @brief Convert GNUNET key to a base 64 encoded public key
62 */ 59 */
63char * 60char *
64GNUNET_DID_key_covert_gnunet_to_multibase_base64(struct GNUNET_IDENTITY_PublicKey *); 61GNUNET_DID_key_covert_gnunet_to_multibase_base64 (struct
62 GNUNET_IDENTITY_PublicKey *);
65 63
66/** 64/**
67 * @brief Generate the default DID document for a GNUNET public key 65 * @brief Generate the default DID document for a GNUNET public key
@@ -73,4 +71,4 @@ GNUNET_DID_pkey_to_did_document (struct GNUNET_IDENTITY_PublicKey *pkey);
73 * @brief Generate the default DID document for a GNUNET ego 71 * @brief Generate the default DID document for a GNUNET ego
74 */ 72 */
75char * 73char *
76GNUNET_DID_identity_to_did_document(struct GNUNET_IDENTITY_Ego *ego); \ No newline at end of file 74GNUNET_DID_identity_to_did_document (struct GNUNET_IDENTITY_Ego *ego); \ No newline at end of file
diff --git a/src/reclaim/test_did_helper.c b/src/reclaim/test_did_helper.c
index 563b94e78..c514f17b2 100644
--- a/src/reclaim/test_did_helper.c
+++ b/src/reclaim/test_did_helper.c
@@ -43,8 +43,11 @@ static const char test_skey_bytes[32] = {
43static const char *test_did = 43static const char *test_did =
44 "did:reclaim:000G0509BYD1MPAXVSTNV0KRD1JAT0YZMPJFQNM869B66S72PSF17K4Y8G"; 44 "did:reclaim:000G0509BYD1MPAXVSTNV0KRD1JAT0YZMPJFQNM869B66S72PSF17K4Y8G";
45 45
46static const char *test_multibase_key =
47 "u7QEJX5oaWV3edV2CeGhkrQPfpaT71ogyVmNk4rZeE8yeRA";
48
46static const char *test_did_document_format_str = 49static const char *test_did_document_format_str =
47 "{\"@context\":[\"https://www.w3.org/ns/did/v1\", \ 50 "{\"@context\":[\"https://www.w3.org/ns/did/v1\",\
48 \"https://w3id.org/security/suites/ed25519-2020/v1\"],\ 51 \"https://w3id.org/security/suites/ed25519-2020/v1\"],\
49 \"id\":\"%s\",\ 52 \"id\":\"%s\",\
50 \"verificationMethod\":[{\ 53 \"verificationMethod\":[{\
@@ -55,8 +58,6 @@ static const char *test_did_document_format_str =
55 \"authentication\":[\"#key-1\"],\ 58 \"authentication\":[\"#key-1\"],\
56 \"assertionMethod\":[\"#key-1\"]}"; 59 \"assertionMethod\":[\"#key-1\"]}";
57 60
58static const char *test_multibase_key = "moin";
59
60static struct GNUNET_IDENTITY_PrivateKey test_skey; 61static struct GNUNET_IDENTITY_PrivateKey test_skey;
61static struct GNUNET_IDENTITY_PublicKey test_pkey; 62static struct GNUNET_IDENTITY_PublicKey test_pkey;
62static struct json_t *test_did_document; 63static struct json_t *test_did_document;
@@ -81,11 +82,18 @@ test_GNUNET_DID_did_to_pkey ()
81 test_pkey.eddsa_key.q_y) == 0); 82 test_pkey.eddsa_key.q_y) == 0);
82} 83}
83 84
84void 85// void
85test_GNUNET_DID_key_covert_multibase_base64_to_gnunet (); 86// test_GNUNET_DID_key_covert_multibase_base64_to_gnunet ();
86 87
87void 88void
88test_GNUNET_DID_key_covert_gnunet_to_multibase_base64 (); 89test_GNUNET_DID_key_covert_gnunet_to_multibase_base64 ()
90{
91 char *multibase_key;
92 multibase_key = GNUNET_DID_key_covert_gnunet_to_multibase_base64 (&test_pkey);
93 printf ("%s\n", multibase_key);
94
95 GNUNET_assert (strcmp (test_multibase_key, multibase_key) == 0);
96}
89 97
90void 98void
91test_GNUNET_DID_pkey_to_did_document () 99test_GNUNET_DID_pkey_to_did_document ()
@@ -119,5 +127,6 @@ main ()
119 test_GNUNET_DID_pkey_to_did (); 127 test_GNUNET_DID_pkey_to_did ();
120 test_GNUNET_DID_did_to_pkey (); 128 test_GNUNET_DID_did_to_pkey ();
121 test_GNUNET_DID_pkey_to_did_document (); 129 test_GNUNET_DID_pkey_to_did_document ();
130 test_GNUNET_DID_key_covert_gnunet_to_multibase_base64 ();
122 return 0; 131 return 0;
123} \ No newline at end of file 132} \ No newline at end of file
diff --git a/src/reclaim/test_w3c_ed25519_2020.c b/src/reclaim/test_w3c_ed25519_2020.c
deleted file mode 100644
index e2534e6ab..000000000
--- a/src/reclaim/test_w3c_ed25519_2020.c
+++ /dev/null
@@ -1,69 +0,0 @@
1/*
2 This file is part of GNUnet.
3 Copyright (C) 2012-2021 GNUnet e.V.
4
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.
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 Affero General Public License for more details.
14
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/>.
17
18 SPDX-License-Identifier: AGPL3.0-or-later
19 */
20
21
22/**
23 * @file src/did/test_w3c_ed25519_2020.c
24 * @brief Testcases for the w3c Ed25519 formats for SSIs https://w3c-ccg.github.io/lds-ed25519-2020
25 * @author Martin Schanzenbach
26 */
27
28#include "platform.h"
29#include "gnunet_crypto_lib.h"
30#include "gnunet_strings_lib.h"
31
32static char test_privkey[32] = {
33 0x9b, 0x93, 0x7b, 0x81, 0x32, 0x2d, 0x81, 0x6c,
34 0xfa, 0xb9, 0xd5, 0xa3, 0xba, 0xac, 0xc9, 0xb2,
35 0xa5, 0xfe, 0xbe, 0x4b, 0x14, 0x9f, 0x12, 0x6b,
36 0x36, 0x30, 0xf9, 0x3a, 0x29, 0x52, 0x70, 0x17
37};
38
39static char *targetPublicKeyMultibase = "u7QEJX5oaWV3edV2CeGhkrQPfpaT71ogyVmNk4rZeE8yeRA";
40
41int
42main ()
43{
44 struct GNUNET_CRYPTO_EddsaPrivateKey privkey;
45 struct GNUNET_CRYPTO_EddsaPublicKey pubkey;
46
47 memcpy (&privkey, test_privkey, sizeof (privkey));
48 GNUNET_CRYPTO_eddsa_key_get_public (&privkey, &pubkey);
49
50 //This is how to convert out pubkeys to W3c Ed25519-2020 multibase (base64url no padding)
51 char *b64;
52 char pkx[34];
53 pkx[0] = 0xed;
54 pkx[1] = 0x01;
55 memcpy (pkx+2, &pubkey, sizeof (pubkey));
56 GNUNET_STRINGS_base64url_encode (pkx,
57 sizeof (pkx),
58 &b64);
59 printf ("u%s\n%s\n", b64, targetPublicKeyMultibase);
60 // FIXME convert pubkey to target
61 char *res;
62 GNUNET_asprintf (&res, "u%s", b64);
63 GNUNET_assert (0 == strcmp (res,
64 targetPublicKeyMultibase));
65
66 GNUNET_free (b64);
67 GNUNET_free (res);
68 return 0;
69}