diff options
Diffstat (limited to 'src/service/reclaim/did_helper.c')
-rw-r--r-- | src/service/reclaim/did_helper.c | 203 |
1 files changed, 203 insertions, 0 deletions
diff --git a/src/service/reclaim/did_helper.c b/src/service/reclaim/did_helper.c new file mode 100644 index 000000000..3aac027ee --- /dev/null +++ b/src/service/reclaim/did_helper.c | |||
@@ -0,0 +1,203 @@ | |||
1 | /* | ||
2 | This file is part of GNUnet | ||
3 | Copyright (C) 2010-2015 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 | * @file reclaim/did_helper.c | ||
23 | * @brief helper library for DID related functions | ||
24 | * @author Tristan Schwieren | ||
25 | */ | ||
26 | #include "platform.h" | ||
27 | #include "gnunet_util_lib.h" | ||
28 | #include "gnunet_namestore_service.h" | ||
29 | #include "gnunet_gns_service.h" | ||
30 | #include "gnunet_gnsrecord_lib.h" | ||
31 | #include "did_helper.h" | ||
32 | #include "jansson.h" | ||
33 | |||
34 | #define STR_INDIR(x) #x | ||
35 | #define STR(x) STR_INDIR (x) | ||
36 | |||
37 | /** | ||
38 | * @brief Generate a DID for a given GNUNET public key | ||
39 | * | ||
40 | * @param pkey | ||
41 | * @return char* Returns the DID. Caller must free | ||
42 | * TODO: Check if EdDSA | ||
43 | */ | ||
44 | char* | ||
45 | DID_pkey_to_did (struct GNUNET_CRYPTO_PublicKey *pkey) | ||
46 | { | ||
47 | char *pkey_str; | ||
48 | char *did_str; | ||
49 | |||
50 | pkey_str = GNUNET_CRYPTO_public_key_to_string (pkey); | ||
51 | GNUNET_asprintf (&did_str, "%s%s", | ||
52 | GNUNET_DID_METHOD_PREFIX, | ||
53 | pkey_str); | ||
54 | |||
55 | GNUNET_free (pkey_str); | ||
56 | return did_str; | ||
57 | } | ||
58 | |||
59 | /** | ||
60 | * @brief Generate a DID for a given gnunet EGO. | ||
61 | * Wrapper around GNUNET_DID_pkey_to_did | ||
62 | * | ||
63 | * @param ego | ||
64 | * @return char* Returns the DID. Caller must free | ||
65 | */ | ||
66 | char* | ||
67 | DID_identity_to_did (struct GNUNET_IDENTITY_Ego *ego) | ||
68 | { | ||
69 | struct GNUNET_CRYPTO_PublicKey pkey; | ||
70 | |||
71 | GNUNET_IDENTITY_ego_get_public_key (ego, &pkey); | ||
72 | return DID_pkey_to_did (&pkey); | ||
73 | } | ||
74 | |||
75 | /** | ||
76 | * @brief Return the public key of a DID | ||
77 | */ | ||
78 | enum GNUNET_GenericReturnValue | ||
79 | DID_did_to_pkey (const char *did, struct GNUNET_CRYPTO_PublicKey *pkey) | ||
80 | { | ||
81 | char pkey_str[MAX_DID_SPECIFIC_IDENTIFIER_LENGTH + 1]; /* 0-term */ | ||
82 | |||
83 | if ((1 != (sscanf (did, | ||
84 | GNUNET_DID_METHOD_PREFIX "%" | ||
85 | STR (MAX_DID_SPECIFIC_IDENTIFIER_LENGTH) | ||
86 | "s", pkey_str))) || | ||
87 | (GNUNET_OK != GNUNET_CRYPTO_public_key_from_string (pkey_str, pkey))) | ||
88 | { | ||
89 | GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Could not decode given DID: %s\n", | ||
90 | did); | ||
91 | return GNUNET_NO; | ||
92 | } | ||
93 | |||
94 | return GNUNET_OK; | ||
95 | } | ||
96 | |||
97 | /** | ||
98 | * @brief Convert a base 64 encoded public key to a GNUNET key | ||
99 | */ | ||
100 | struct GNUNET_CRYPTO_PublicKey * | ||
101 | GNUNET_DID_key_convert_multibase_base64_to_gnunet (char *pkey_str) | ||
102 | { | ||
103 | return NULL; | ||
104 | } | ||
105 | |||
106 | /** | ||
107 | * @brief Convert GNUNET key to a base 64 encoded public key | ||
108 | */ | ||
109 | char * | ||
110 | DID_key_convert_gnunet_to_multibase_base64 (struct | ||
111 | GNUNET_CRYPTO_PublicKey * | ||
112 | pkey) | ||
113 | { | ||
114 | struct GNUNET_CRYPTO_EddsaPublicKey pubkey = pkey->eddsa_key; | ||
115 | |||
116 | // This is how to convert out pubkeys to W3c Ed25519-2020 multibase (base64url no padding) | ||
117 | char *pkey_base_64; | ||
118 | char *pkey_multibase; | ||
119 | char pkx[34]; | ||
120 | |||
121 | pkx[0] = 0xed; | ||
122 | pkx[1] = 0x01; | ||
123 | memcpy (pkx + 2, &pubkey, sizeof (pubkey)); | ||
124 | GNUNET_STRINGS_base64url_encode (pkx, sizeof (pkx), &pkey_base_64); | ||
125 | GNUNET_asprintf (&pkey_multibase, "u%s", pkey_base_64); | ||
126 | |||
127 | GNUNET_free (pkey_base_64); | ||
128 | return pkey_multibase; | ||
129 | } | ||
130 | |||
131 | /** | ||
132 | * @brief Create a did generate did object | ||
133 | * | ||
134 | * @param pkey | ||
135 | * @return void* Return pointer to the DID Document | ||
136 | */ | ||
137 | char * | ||
138 | DID_pkey_to_did_document (struct GNUNET_CRYPTO_PublicKey *pkey) | ||
139 | { | ||
140 | |||
141 | /* FIXME-MSC: This is effectively creating a DID Document default template for | ||
142 | * the initial document. | ||
143 | * Maybe this can be refactored to generate such a template for an identity? | ||
144 | * Even if higher layers add/modify it, there should probably still be a | ||
145 | * GNUNET_DID_document_template_from_identity() | ||
146 | */ | ||
147 | |||
148 | char *did_str; | ||
149 | char *verify_id_str; | ||
150 | char *pkey_multibase_str; | ||
151 | char *didd_str; | ||
152 | json_t *didd_json; | ||
153 | |||
154 | did_str = DID_pkey_to_did (pkey); | ||
155 | GNUNET_asprintf (&verify_id_str, "%s#key-1", did_str); | ||
156 | |||
157 | pkey_multibase_str = DID_key_convert_gnunet_to_multibase_base64 (pkey); | ||
158 | |||
159 | didd_json = json_pack ( | ||
160 | "{s:[ss], s:s, s:[{s:s, s:s, s:s, s:s}], s:[s], s:[s]}", | ||
161 | "@context", | ||
162 | "https://www.w3.org/ns/did/v1", | ||
163 | "https://w3id.org/security/suites/ed25519-2020/v1", | ||
164 | "id", | ||
165 | did_str, | ||
166 | "verificationMethod", | ||
167 | "id", | ||
168 | verify_id_str, | ||
169 | "type", | ||
170 | "Ed25519VerificationKey2020", | ||
171 | "controller", | ||
172 | did_str, | ||
173 | "publicKeyMultibase", | ||
174 | pkey_multibase_str, | ||
175 | "authentication", | ||
176 | "#key-1", | ||
177 | "assertionMethod", | ||
178 | "#key-1"); | ||
179 | |||
180 | // Encode DID Document as JSON string | ||
181 | didd_str = json_dumps (didd_json, JSON_INDENT (2)); | ||
182 | |||
183 | // Free | ||
184 | GNUNET_free (did_str); | ||
185 | GNUNET_free (verify_id_str); | ||
186 | GNUNET_free (pkey_multibase_str); | ||
187 | json_decref (didd_json); | ||
188 | |||
189 | return didd_str; | ||
190 | } | ||
191 | |||
192 | /** | ||
193 | * @brief Generate the default DID document for a GNUNET ego | ||
194 | * Wrapper around GNUNET_DID_pkey_to_did_document | ||
195 | */ | ||
196 | char * | ||
197 | DID_identity_to_did_document (struct GNUNET_IDENTITY_Ego *ego) | ||
198 | { | ||
199 | struct GNUNET_CRYPTO_PublicKey pkey; | ||
200 | |||
201 | GNUNET_IDENTITY_ego_get_public_key (ego, &pkey); | ||
202 | return DID_pkey_to_did (&pkey); | ||
203 | } \ No newline at end of file | ||