1 // TODO key loading, generating, saving
10 void *crypto_mutex = NULL;
12 cvar_t crypto_developer = {CVAR_SAVE, "crypto_developer", "0", "print extra info about crypto handshake"};
13 cvar_t crypto_servercpupercent = {CVAR_SAVE, "crypto_servercpupercent", "10", "allowed crypto CPU load in percent for server operation (0 = no limit, faster)"};
14 cvar_t crypto_servercpumaxtime = {CVAR_SAVE, "crypto_servercpumaxtime", "0.01", "maximum allowed crypto CPU time per frame (0 = no limit)"};
15 cvar_t crypto_servercpudebug = {CVAR_SAVE, "crypto_servercpudebug", "0", "print statistics about time usage by crypto"};
16 static double crypto_servercpu_accumulator = 0;
17 static double crypto_servercpu_lastrealtime = 0;
18 cvar_t crypto_aeslevel = {CVAR_SAVE, "crypto_aeslevel", "1", "whether to support AES encryption in authenticated connections (0 = no, 1 = supported, 2 = requested, 3 = required)"};
19 int crypto_keyfp_recommended_length;
20 static const char *crypto_idstring = NULL;
21 static char crypto_idstring_buf[512];
23 #define PROTOCOL_D0_BLIND_ID FOURCC_D0PK
24 #define PROTOCOL_VLEN (('v' << 0) | ('l' << 8) | ('e' << 16) | ('n' << 24))
26 // BEGIN stuff shared with crypto-keygen-standalone
27 #define FOURCC_D0PK (('d' << 0) | ('0' << 8) | ('p' << 16) | ('k' << 24))
28 #define FOURCC_D0SK (('d' << 0) | ('0' << 8) | ('s' << 16) | ('k' << 24))
29 #define FOURCC_D0PI (('d' << 0) | ('0' << 8) | ('p' << 16) | ('i' << 24))
30 #define FOURCC_D0SI (('d' << 0) | ('0' << 8) | ('s' << 16) | ('i' << 24))
31 #define FOURCC_D0IQ (('d' << 0) | ('0' << 8) | ('i' << 16) | ('q' << 24))
32 #define FOURCC_D0IR (('d' << 0) | ('0' << 8) | ('i' << 16) | ('r' << 24))
33 #define FOURCC_D0ER (('d' << 0) | ('0' << 8) | ('e' << 16) | ('r' << 24))
34 #define FOURCC_D0IC (('d' << 0) | ('0' << 8) | ('i' << 16) | ('c' << 24))
36 static unsigned long Crypto_LittleLong(const char *data)
39 ((unsigned char) data[0]) |
40 (((unsigned char) data[1]) << 8) |
41 (((unsigned char) data[2]) << 16) |
42 (((unsigned char) data[3]) << 24);
45 static void Crypto_UnLittleLong(char *data, unsigned long l)
48 data[1] = (l >> 8) & 0xFF;
49 data[2] = (l >> 16) & 0xFF;
50 data[3] = (l >> 24) & 0xFF;
53 static size_t Crypto_ParsePack(const char *buf, size_t len, unsigned long header, const char **lumps, size_t *lumpsize, size_t nlumps)
62 if(Crypto_LittleLong(buf) != header)
66 for(i = 0; i < nlumps; ++i)
70 lumpsize[i] = Crypto_LittleLong(&buf[pos]);
72 if(pos + lumpsize[i] > len)
80 static size_t Crypto_UnParsePack(char *buf, size_t len, unsigned long header, const char *const *lumps, const size_t *lumpsize, size_t nlumps)
89 Crypto_UnLittleLong(buf, header);
92 for(i = 0; i < nlumps; ++i)
94 if(pos + 4 + lumpsize[i] > len)
96 Crypto_UnLittleLong(&buf[pos], lumpsize[i]);
98 memcpy(&buf[pos], lumps[i], lumpsize[i]);
103 // END stuff shared with xonotic-keygen
109 #include <d0_blind_id/d0_blind_id.h>
111 #define d0_blind_id_dll 1
112 #define Crypto_OpenLibrary() true
113 #define Crypto_CloseLibrary()
115 #define qd0_blind_id_new d0_blind_id_new
116 #define qd0_blind_id_free d0_blind_id_free
117 //#define qd0_blind_id_clear d0_blind_id_clear
118 #define qd0_blind_id_copy d0_blind_id_copy
119 //#define qd0_blind_id_generate_private_key d0_blind_id_generate_private_key
120 //#define qd0_blind_id_generate_private_key_fastreject d0_blind_id_generate_private_key_fastreject
121 //#define qd0_blind_id_read_private_key d0_blind_id_read_private_key
122 #define qd0_blind_id_read_public_key d0_blind_id_read_public_key
123 //#define qd0_blind_id_write_private_key d0_blind_id_write_private_key
124 //#define qd0_blind_id_write_public_key d0_blind_id_write_public_key
125 #define qd0_blind_id_fingerprint64_public_key d0_blind_id_fingerprint64_public_key
126 //#define qd0_blind_id_generate_private_id_modulus d0_blind_id_generate_private_id_modulus
127 #define qd0_blind_id_read_private_id_modulus d0_blind_id_read_private_id_modulus
128 //#define qd0_blind_id_write_private_id_modulus d0_blind_id_write_private_id_modulus
129 #define qd0_blind_id_generate_private_id_start d0_blind_id_generate_private_id_start
130 #define qd0_blind_id_generate_private_id_request d0_blind_id_generate_private_id_request
131 //#define qd0_blind_id_answer_private_id_request d0_blind_id_answer_private_id_request
132 #define qd0_blind_id_finish_private_id_request d0_blind_id_finish_private_id_request
133 //#define qd0_blind_id_read_private_id_request_camouflage d0_blind_id_read_private_id_request_camouflage
134 //#define qd0_blind_id_write_private_id_request_camouflage d0_blind_id_write_private_id_request_camouflage
135 #define qd0_blind_id_read_private_id d0_blind_id_read_private_id
136 //#define qd0_blind_id_read_public_id d0_blind_id_read_public_id
137 #define qd0_blind_id_write_private_id d0_blind_id_write_private_id
138 //#define qd0_blind_id_write_public_id d0_blind_id_write_public_id
139 #define qd0_blind_id_authenticate_with_private_id_start d0_blind_id_authenticate_with_private_id_start
140 #define qd0_blind_id_authenticate_with_private_id_challenge d0_blind_id_authenticate_with_private_id_challenge
141 #define qd0_blind_id_authenticate_with_private_id_response d0_blind_id_authenticate_with_private_id_response
142 #define qd0_blind_id_authenticate_with_private_id_verify d0_blind_id_authenticate_with_private_id_verify
143 #define qd0_blind_id_fingerprint64_public_id d0_blind_id_fingerprint64_public_id
144 #define qd0_blind_id_sessionkey_public_id d0_blind_id_sessionkey_public_id
145 #define qd0_blind_id_INITIALIZE d0_blind_id_INITIALIZE
146 #define qd0_blind_id_SHUTDOWN d0_blind_id_SHUTDOWN
147 #define qd0_blind_id_util_sha256 d0_blind_id_util_sha256
148 #define qd0_blind_id_sign_with_private_id_sign d0_blind_id_sign_with_private_id_sign
149 #define qd0_blind_id_sign_with_private_id_sign_detached d0_blind_id_sign_with_private_id_sign_detached
150 #define qd0_blind_id_setmallocfuncs d0_blind_id_setmallocfuncs
151 #define qd0_blind_id_setmutexfuncs d0_blind_id_setmutexfuncs
155 // d0_blind_id interface
158 #define D0_WARN_UNUSED_RESULT __attribute__((warn_unused_result))
160 #define D0_WARN_UNUSED_RESULT
164 typedef void *(d0_malloc_t)(size_t len);
165 typedef void (d0_free_t)(void *p);
166 typedef void *(d0_createmutex_t)(void);
167 typedef void (d0_destroymutex_t)(void *);
168 typedef int (d0_lockmutex_t)(void *); // zero on success
169 typedef int (d0_unlockmutex_t)(void *); // zero on success
171 typedef struct d0_blind_id_s d0_blind_id_t;
172 typedef D0_BOOL (*d0_fastreject_function) (const d0_blind_id_t *ctx, void *pass);
173 static D0_EXPORT D0_WARN_UNUSED_RESULT d0_blind_id_t *(*qd0_blind_id_new) (void);
174 static D0_EXPORT void (*qd0_blind_id_free) (d0_blind_id_t *a);
175 //static D0_EXPORT void (*qd0_blind_id_clear) (d0_blind_id_t *ctx);
176 static D0_EXPORT D0_WARN_UNUSED_RESULT D0_BOOL (*qd0_blind_id_copy) (d0_blind_id_t *ctx, const d0_blind_id_t *src);
177 //static D0_EXPORT D0_WARN_UNUSED_RESULT D0_BOOL (*qd0_blind_id_generate_private_key) (d0_blind_id_t *ctx, int k);
178 //static D0_EXPORT D0_WARN_UNUSED_RESULT D0_BOOL (*qd0_blind_id_generate_private_key_fastreject) (d0_blind_id_t *ctx, int k, d0_fastreject_function reject, void *pass);
179 //static D0_EXPORT D0_WARN_UNUSED_RESULT D0_BOOL (*qd0_blind_id_read_private_key) (d0_blind_id_t *ctx, const char *inbuf, size_t inbuflen);
180 static D0_EXPORT D0_WARN_UNUSED_RESULT D0_BOOL (*qd0_blind_id_read_public_key) (d0_blind_id_t *ctx, const char *inbuf, size_t inbuflen);
181 //static D0_EXPORT D0_WARN_UNUSED_RESULT D0_BOOL (*qd0_blind_id_write_private_key) (const d0_blind_id_t *ctx, char *outbuf, size_t *outbuflen);
182 //static D0_EXPORT D0_WARN_UNUSED_RESULT D0_BOOL (*qd0_blind_id_write_public_key) (const d0_blind_id_t *ctx, char *outbuf, size_t *outbuflen);
183 static D0_EXPORT D0_WARN_UNUSED_RESULT D0_BOOL (*qd0_blind_id_fingerprint64_public_key) (const d0_blind_id_t *ctx, char *outbuf, size_t *outbuflen);
184 //static D0_EXPORT D0_WARN_UNUSED_RESULT D0_BOOL (*qd0_blind_id_generate_private_id_modulus) (d0_blind_id_t *ctx);
185 static D0_EXPORT D0_WARN_UNUSED_RESULT D0_BOOL (*qd0_blind_id_read_private_id_modulus) (d0_blind_id_t *ctx, const char *inbuf, size_t inbuflen);
186 //static D0_EXPORT D0_WARN_UNUSED_RESULT D0_BOOL (*qd0_blind_id_write_private_id_modulus) (const d0_blind_id_t *ctx, char *outbuf, size_t *outbuflen);
187 static D0_EXPORT D0_WARN_UNUSED_RESULT D0_BOOL (*qd0_blind_id_generate_private_id_start) (d0_blind_id_t *ctx);
188 static D0_EXPORT D0_WARN_UNUSED_RESULT D0_BOOL (*qd0_blind_id_generate_private_id_request) (d0_blind_id_t *ctx, char *outbuf, size_t *outbuflen);
189 //static D0_EXPORT D0_WARN_UNUSED_RESULT D0_BOOL (*qd0_blind_id_answer_private_id_request) (const d0_blind_id_t *ctx, const char *inbuf, size_t inbuflen, char *outbuf, size_t *outbuflen);
190 static D0_EXPORT D0_WARN_UNUSED_RESULT D0_BOOL (*qd0_blind_id_finish_private_id_request) (d0_blind_id_t *ctx, const char *inbuf, size_t inbuflen);
191 //static D0_EXPORT D0_WARN_UNUSED_RESULT D0_BOOL (*qd0_blind_id_read_private_id_request_camouflage) (d0_blind_id_t *ctx, const char *inbuf, size_t inbuflen);
192 //static D0_EXPORT D0_WARN_UNUSED_RESULT D0_BOOL (*qd0_blind_id_write_private_id_request_camouflage) (const d0_blind_id_t *ctx, char *outbuf, size_t *outbuflen);
193 static D0_EXPORT D0_WARN_UNUSED_RESULT D0_BOOL (*qd0_blind_id_read_private_id) (d0_blind_id_t *ctx, const char *inbuf, size_t inbuflen);
194 //static D0_EXPORT D0_WARN_UNUSED_RESULT D0_BOOL (*qd0_blind_id_read_public_id) (d0_blind_id_t *ctx, const char *inbuf, size_t inbuflen);
195 static D0_EXPORT D0_WARN_UNUSED_RESULT D0_BOOL (*qd0_blind_id_write_private_id) (const d0_blind_id_t *ctx, char *outbuf, size_t *outbuflen);
196 //static D0_EXPORT D0_WARN_UNUSED_RESULT D0_BOOL (*qd0_blind_id_write_public_id) (const d0_blind_id_t *ctx, char *outbuf, size_t *outbuflen);
197 static D0_EXPORT D0_WARN_UNUSED_RESULT D0_BOOL (*qd0_blind_id_authenticate_with_private_id_start) (d0_blind_id_t *ctx, D0_BOOL is_first, D0_BOOL send_modulus, const char *message, size_t msglen, char *outbuf, size_t *outbuflen);
198 static D0_EXPORT D0_WARN_UNUSED_RESULT D0_BOOL (*qd0_blind_id_authenticate_with_private_id_challenge) (d0_blind_id_t *ctx, D0_BOOL is_first, D0_BOOL recv_modulus, const char *inbuf, size_t inbuflen, char *outbuf, size_t *outbuflen, D0_BOOL *status);
199 static D0_EXPORT D0_WARN_UNUSED_RESULT D0_BOOL (*qd0_blind_id_authenticate_with_private_id_response) (d0_blind_id_t *ctx, const char *inbuf, size_t inbuflen, char *outbuf, size_t *outbuflen);
200 static D0_EXPORT D0_WARN_UNUSED_RESULT D0_BOOL (*qd0_blind_id_authenticate_with_private_id_verify) (d0_blind_id_t *ctx, const char *inbuf, size_t inbuflen, char *msg, size_t *msglen, D0_BOOL *status);
201 static D0_EXPORT D0_WARN_UNUSED_RESULT D0_BOOL (*qd0_blind_id_fingerprint64_public_id) (const d0_blind_id_t *ctx, char *outbuf, size_t *outbuflen);
202 static D0_EXPORT D0_WARN_UNUSED_RESULT D0_BOOL (*qd0_blind_id_sessionkey_public_id) (const d0_blind_id_t *ctx, char *outbuf, size_t *outbuflen); // can only be done after successful key exchange, this performs a modpow; key length is limited by SHA_DIGESTSIZE for now; also ONLY valid after successful d0_blind_id_authenticate_with_private_id_verify/d0_blind_id_fingerprint64_public_id
203 static D0_EXPORT D0_WARN_UNUSED_RESULT D0_BOOL (*qd0_blind_id_INITIALIZE) (void);
204 static D0_EXPORT void (*qd0_blind_id_SHUTDOWN) (void);
205 static D0_EXPORT void (*qd0_blind_id_util_sha256) (char *out, const char *in, size_t n);
206 static D0_EXPORT D0_WARN_UNUSED_RESULT D0_BOOL (*qd0_blind_id_sign_with_private_id_sign) (d0_blind_id_t *ctx, D0_BOOL is_first, D0_BOOL send_modulus, const char *message, size_t msglen, char *outbuf, size_t *outbuflen);
207 static D0_EXPORT D0_WARN_UNUSED_RESULT D0_BOOL (*qd0_blind_id_sign_with_private_id_sign_detached) (d0_blind_id_t *ctx, D0_BOOL is_first, D0_BOOL send_modulus, const char *message, size_t msglen, char *outbuf, size_t *outbuflen);
208 static D0_EXPORT void (*qd0_blind_id_setmallocfuncs)(d0_malloc_t *m, d0_free_t *f);
209 static D0_EXPORT void (*qd0_blind_id_setmutexfuncs)(d0_createmutex_t *c, d0_destroymutex_t *d, d0_lockmutex_t *l, d0_unlockmutex_t *u);
210 static dllfunction_t d0_blind_id_funcs[] =
212 {"d0_blind_id_new", (void **) &qd0_blind_id_new},
213 {"d0_blind_id_free", (void **) &qd0_blind_id_free},
214 //{"d0_blind_id_clear", (void **) &qd0_blind_id_clear},
215 {"d0_blind_id_copy", (void **) &qd0_blind_id_copy},
216 //{"d0_blind_id_generate_private_key", (void **) &qd0_blind_id_generate_private_key},
217 //{"d0_blind_id_generate_private_key_fastreject", (void **) &qd0_blind_id_generate_private_key_fastreject},
218 //{"d0_blind_id_read_private_key", (void **) &qd0_blind_id_read_private_key},
219 {"d0_blind_id_read_public_key", (void **) &qd0_blind_id_read_public_key},
220 //{"d0_blind_id_write_private_key", (void **) &qd0_blind_id_write_private_key},
221 //{"d0_blind_id_write_public_key", (void **) &qd0_blind_id_write_public_key},
222 {"d0_blind_id_fingerprint64_public_key", (void **) &qd0_blind_id_fingerprint64_public_key},
223 //{"d0_blind_id_generate_private_id_modulus", (void **) &qd0_blind_id_generate_private_id_modulus},
224 {"d0_blind_id_read_private_id_modulus", (void **) &qd0_blind_id_read_private_id_modulus},
225 //{"d0_blind_id_write_private_id_modulus", (void **) &qd0_blind_id_write_private_id_modulus},
226 {"d0_blind_id_generate_private_id_start", (void **) &qd0_blind_id_generate_private_id_start},
227 {"d0_blind_id_generate_private_id_request", (void **) &qd0_blind_id_generate_private_id_request},
228 //{"d0_blind_id_answer_private_id_request", (void **) &qd0_blind_id_answer_private_id_request},
229 {"d0_blind_id_finish_private_id_request", (void **) &qd0_blind_id_finish_private_id_request},
230 //{"d0_blind_id_read_private_id_request_camouflage", (void **) &qd0_blind_id_read_private_id_request_camouflage},
231 //{"d0_blind_id_write_private_id_request_camouflage", (void **) &qd0_blind_id_write_private_id_request_camouflage},
232 {"d0_blind_id_read_private_id", (void **) &qd0_blind_id_read_private_id},
233 //{"d0_blind_id_read_public_id", (void **) &qd0_blind_id_read_public_id},
234 {"d0_blind_id_write_private_id", (void **) &qd0_blind_id_write_private_id},
235 //{"d0_blind_id_write_public_id", (void **) &qd0_blind_id_write_public_id},
236 {"d0_blind_id_authenticate_with_private_id_start", (void **) &qd0_blind_id_authenticate_with_private_id_start},
237 {"d0_blind_id_authenticate_with_private_id_challenge", (void **) &qd0_blind_id_authenticate_with_private_id_challenge},
238 {"d0_blind_id_authenticate_with_private_id_response", (void **) &qd0_blind_id_authenticate_with_private_id_response},
239 {"d0_blind_id_authenticate_with_private_id_verify", (void **) &qd0_blind_id_authenticate_with_private_id_verify},
240 {"d0_blind_id_fingerprint64_public_id", (void **) &qd0_blind_id_fingerprint64_public_id},
241 {"d0_blind_id_sessionkey_public_id", (void **) &qd0_blind_id_sessionkey_public_id},
242 {"d0_blind_id_INITIALIZE", (void **) &qd0_blind_id_INITIALIZE},
243 {"d0_blind_id_SHUTDOWN", (void **) &qd0_blind_id_SHUTDOWN},
244 {"d0_blind_id_util_sha256", (void **) &qd0_blind_id_util_sha256},
245 {"d0_blind_id_sign_with_private_id_sign", (void **) &qd0_blind_id_sign_with_private_id_sign},
246 {"d0_blind_id_sign_with_private_id_sign_detached", (void **) &qd0_blind_id_sign_with_private_id_sign_detached},
247 {"d0_blind_id_setmallocfuncs", (void **) &qd0_blind_id_setmallocfuncs},
248 {"d0_blind_id_setmutexfuncs", (void **) &qd0_blind_id_setmutexfuncs},
251 // end of d0_blind_id interface
253 static dllhandle_t d0_blind_id_dll = NULL;
254 static qboolean Crypto_OpenLibrary (void)
256 const char* dllnames [] =
259 "libd0_blind_id-0.dll",
260 #elif defined(MACOSX)
261 "libd0_blind_id.0.dylib",
263 "libd0_blind_id.so.0",
264 "libd0_blind_id.so", // FreeBSD
274 return Sys_LoadLibrary (dllnames, &d0_blind_id_dll, d0_blind_id_funcs);
277 static void Crypto_CloseLibrary (void)
279 Sys_UnloadLibrary (&d0_blind_id_dll);
284 #ifdef CRYPTO_RIJNDAEL_STATIC
286 #include <d0_blind_id/d0_rijndael.h>
288 #define d0_rijndael_dll 1
289 #define Crypto_Rijndael_OpenLibrary() true
290 #define Crypto_Rijndael_CloseLibrary()
292 #define qd0_rijndael_setup_encrypt d0_rijndael_setup_encrypt
293 #define qd0_rijndael_setup_decrypt d0_rijndael_setup_decrypt
294 #define qd0_rijndael_encrypt d0_rijndael_encrypt
295 #define qd0_rijndael_decrypt d0_rijndael_decrypt
299 // no need to do the #define dance here, as the upper part declares out macros either way
301 D0_EXPORT int (*qd0_rijndael_setup_encrypt) (unsigned long *rk, const unsigned char *key,
303 D0_EXPORT int (*qd0_rijndael_setup_decrypt) (unsigned long *rk, const unsigned char *key,
305 D0_EXPORT void (*qd0_rijndael_encrypt) (const unsigned long *rk, int nrounds,
306 const unsigned char plaintext[16], unsigned char ciphertext[16]);
307 D0_EXPORT void (*qd0_rijndael_decrypt) (const unsigned long *rk, int nrounds,
308 const unsigned char ciphertext[16], unsigned char plaintext[16]);
309 #define D0_RIJNDAEL_KEYLENGTH(keybits) ((keybits)/8)
310 #define D0_RIJNDAEL_RKLENGTH(keybits) ((keybits)/8+28)
311 #define D0_RIJNDAEL_NROUNDS(keybits) ((keybits)/32+6)
312 static dllfunction_t d0_rijndael_funcs[] =
314 {"d0_rijndael_setup_decrypt", (void **) &qd0_rijndael_setup_decrypt},
315 {"d0_rijndael_setup_encrypt", (void **) &qd0_rijndael_setup_encrypt},
316 {"d0_rijndael_decrypt", (void **) &qd0_rijndael_decrypt},
317 {"d0_rijndael_encrypt", (void **) &qd0_rijndael_encrypt},
320 // end of d0_blind_id interface
322 static dllhandle_t d0_rijndael_dll = NULL;
323 static qboolean Crypto_Rijndael_OpenLibrary (void)
325 const char* dllnames [] =
328 "libd0_rijndael-0.dll",
329 #elif defined(MACOSX)
330 "libd0_rijndael.0.dylib",
332 "libd0_rijndael.so.0",
333 "libd0_rijndael.so", // FreeBSD
343 return Sys_LoadLibrary (dllnames, &d0_rijndael_dll, d0_rijndael_funcs);
346 static void Crypto_Rijndael_CloseLibrary (void)
348 Sys_UnloadLibrary (&d0_rijndael_dll);
354 void sha256(unsigned char *out, const unsigned char *in, int n)
356 qd0_blind_id_util_sha256((char *) out, (const char *) in, n);
359 static size_t Crypto_LoadFile(const char *path, char *buf, size_t nmax, qboolean inuserdir)
365 f = FS_SysOpen(va(vabuf, sizeof(vabuf), "%s%s", *fs_userdir ? fs_userdir : fs_basedir, path), "rb", false);
367 f = FS_SysOpen(va(vabuf, sizeof(vabuf), "%s%s", fs_basedir, path), "rb", false);
370 n = FS_Read(f, buf, nmax);
377 static qboolean PutWithNul(char **data, size_t *len, const char *str)
379 // invariant: data points to insertion point
380 size_t l = strlen(str);
383 memcpy(*data, str, l+1);
389 static const char *GetUntilNul(const char **data, size_t *len)
391 // invariant: data points to next character to take
392 const char *data_save = *data;
405 p = (const char *) memchr(*data, 0, *len);
406 if(!p) // no terminating NUL
419 return (const char *) data_save;
426 static d0_blind_id_t *Crypto_ReadPublicKey(char *buf, size_t len)
428 d0_blind_id_t *pk = NULL;
431 if(Crypto_ParsePack(buf, len, FOURCC_D0PK, p, l, 2))
433 pk = qd0_blind_id_new();
435 if(qd0_blind_id_read_public_key(pk, p[0], l[0]))
436 if(qd0_blind_id_read_private_id_modulus(pk, p[1], l[1]))
440 qd0_blind_id_free(pk);
445 static qboolean Crypto_AddPrivateKey(d0_blind_id_t *pk, char *buf, size_t len)
449 if(Crypto_ParsePack(buf, len, FOURCC_D0SI, p, l, 1))
451 if(qd0_blind_id_read_private_id(pk, p[0], l[0]))
457 #define MAX_PUBKEYS 16
458 static d0_blind_id_t *pubkeys[MAX_PUBKEYS];
459 static char pubkeys_fp64[MAX_PUBKEYS][FP64_SIZE+1];
460 static qboolean pubkeys_havepriv[MAX_PUBKEYS];
461 static char pubkeys_priv_fp64[MAX_PUBKEYS][FP64_SIZE+1];
462 static char challenge_append[1400];
463 static size_t challenge_append_length;
465 static int keygen_i = -1;
466 static char keygen_buf[8192];
468 #define MAX_CRYPTOCONNECTS 16
469 #define CRYPTOCONNECT_NONE 0
470 #define CRYPTOCONNECT_PRECONNECT 1
471 #define CRYPTOCONNECT_CONNECT 2
472 #define CRYPTOCONNECT_RECONNECT 3
473 #define CRYPTOCONNECT_DUPLICATE 4
474 typedef struct server_cryptoconnect_s
477 lhnetaddress_t address;
481 server_cryptoconnect_t;
482 static server_cryptoconnect_t cryptoconnects[MAX_CRYPTOCONNECTS];
484 static int cdata_id = 0;
490 char challenge[2048];
491 char wantserver_idfp[FP64_SIZE+1];
492 qboolean wantserver_aes;
497 // crypto specific helpers
498 #define CDATA ((crypto_data_t *) crypto->data)
499 #define MAKE_CDATA if(!crypto->data) crypto->data = Z_Malloc(sizeof(crypto_data_t))
500 #define CLEAR_CDATA if(crypto->data) { if(CDATA->id) qd0_blind_id_free(CDATA->id); Z_Free(crypto->data); } crypto->data = NULL
502 static crypto_t *Crypto_ServerFindInstance(lhnetaddress_t *peeraddress, qboolean allow_create)
508 return NULL; // no support
510 for(i = 0; i < MAX_CRYPTOCONNECTS; ++i)
511 if(LHNETADDRESS_Compare(peeraddress, &cryptoconnects[i].address))
513 if(i < MAX_CRYPTOCONNECTS && (allow_create || cryptoconnects[i].crypto.data))
515 crypto = &cryptoconnects[i].crypto;
516 cryptoconnects[i].lasttime = realtime;
522 for(i = 1; i < MAX_CRYPTOCONNECTS; ++i)
523 if(cryptoconnects[i].lasttime < cryptoconnects[best].lasttime)
525 crypto = &cryptoconnects[best].crypto;
526 cryptoconnects[best].lasttime = realtime;
527 memcpy(&cryptoconnects[best].address, peeraddress, sizeof(cryptoconnects[best].address));
532 qboolean Crypto_ServerFinishInstance(crypto_t *out, crypto_t *crypto)
534 // no check needed here (returned pointers are only used in prefilled fields)
535 if(!crypto || !crypto->authenticated)
537 Con_Printf("Passed an invalid crypto connect instance\n");
538 memset(out, 0, sizeof(*out));
542 memcpy(out, crypto, sizeof(*out));
543 memset(crypto, 0, sizeof(crypto));
547 crypto_t *Crypto_ServerGetInstance(lhnetaddress_t *peeraddress)
549 // no check needed here (returned pointers are only used in prefilled fields)
550 return Crypto_ServerFindInstance(peeraddress, false);
553 typedef struct crypto_storedhostkey_s
555 struct crypto_storedhostkey_s *next;
558 char idfp[FP64_SIZE+1];
561 crypto_storedhostkey_t;
562 static crypto_storedhostkey_t *crypto_storedhostkey_hashtable[CRYPTO_HOSTKEY_HASHSIZE];
564 static void Crypto_InitHostKeys(void)
567 for(i = 0; i < CRYPTO_HOSTKEY_HASHSIZE; ++i)
568 crypto_storedhostkey_hashtable[i] = NULL;
571 static void Crypto_ClearHostKeys(void)
574 crypto_storedhostkey_t *hk, *hkn;
575 for(i = 0; i < CRYPTO_HOSTKEY_HASHSIZE; ++i)
577 for(hk = crypto_storedhostkey_hashtable[i]; hk; hk = hkn)
582 crypto_storedhostkey_hashtable[i] = NULL;
586 static qboolean Crypto_ClearHostKey(lhnetaddress_t *peeraddress)
590 crypto_storedhostkey_t **hkp;
591 qboolean found = false;
593 LHNETADDRESS_ToString(peeraddress, buf, sizeof(buf), 1);
594 hashindex = CRC_Block((const unsigned char *) buf, strlen(buf)) % CRYPTO_HOSTKEY_HASHSIZE;
595 for(hkp = &crypto_storedhostkey_hashtable[hashindex]; *hkp && LHNETADDRESS_Compare(&((*hkp)->addr), peeraddress); hkp = &((*hkp)->next));
599 crypto_storedhostkey_t *hk = *hkp;
608 static void Crypto_StoreHostKey(lhnetaddress_t *peeraddress, const char *keystring, qboolean complain)
612 crypto_storedhostkey_t *hk;
614 char idfp[FP64_SIZE+1];
620 // syntax of keystring:
621 // aeslevel id@key id@key ...
625 aeslevel = bound(0, *keystring - '0', 3);
626 while(*keystring && *keystring != ' ')
630 while(*keystring && keyid < 0)
633 const char *idstart, *idend, *keystart, *keyend;
634 ++keystring; // skip the space
636 while(*keystring && *keystring != ' ' && *keystring != '@')
642 keystart = keystring;
643 while(*keystring && *keystring != ' ')
647 if(idend - idstart == FP64_SIZE && keyend - keystart == FP64_SIZE)
649 for(keyid = 0; keyid < MAX_PUBKEYS; ++keyid)
651 if(!memcmp(pubkeys_fp64[keyid], keystart, FP64_SIZE))
653 memcpy(idfp, idstart, FP64_SIZE);
657 if(keyid >= MAX_PUBKEYS)
665 LHNETADDRESS_ToString(peeraddress, buf, sizeof(buf), 1);
666 hashindex = CRC_Block((const unsigned char *) buf, strlen(buf)) % CRYPTO_HOSTKEY_HASHSIZE;
667 for(hk = crypto_storedhostkey_hashtable[hashindex]; hk && LHNETADDRESS_Compare(&hk->addr, peeraddress); hk = hk->next);
673 if(hk->keyid != keyid || memcmp(hk->idfp, idfp, FP64_SIZE+1))
674 Con_Printf("Server %s tried to change the host key to a value not in the host cache. Connecting to it will fail. To accept the new host key, do crypto_hostkey_clear %s\n", buf, buf);
675 if(hk->aeslevel > aeslevel)
676 Con_Printf("Server %s tried to reduce encryption status, not accepted. Connecting to it will fail. To accept, do crypto_hostkey_clear %s\n", buf, buf);
678 hk->aeslevel = max(aeslevel, hk->aeslevel);
682 // great, we did NOT have it yet
683 hk = (crypto_storedhostkey_t *) Z_Malloc(sizeof(*hk));
684 memcpy(&hk->addr, peeraddress, sizeof(hk->addr));
686 memcpy(hk->idfp, idfp, FP64_SIZE+1);
687 hk->next = crypto_storedhostkey_hashtable[hashindex];
688 hk->aeslevel = aeslevel;
689 crypto_storedhostkey_hashtable[hashindex] = hk;
692 qboolean Crypto_RetrieveHostKey(lhnetaddress_t *peeraddress, int *keyid, char *keyfp, size_t keyfplen, char *idfp, size_t idfplen, int *aeslevel)
696 crypto_storedhostkey_t *hk;
701 LHNETADDRESS_ToString(peeraddress, buf, sizeof(buf), 1);
702 hashindex = CRC_Block((const unsigned char *) buf, strlen(buf)) % CRYPTO_HOSTKEY_HASHSIZE;
703 for(hk = crypto_storedhostkey_hashtable[hashindex]; hk && LHNETADDRESS_Compare(&hk->addr, peeraddress); hk = hk->next);
711 strlcpy(keyfp, pubkeys_fp64[hk->keyid], keyfplen);
713 strlcpy(idfp, hk->idfp, idfplen);
715 *aeslevel = hk->aeslevel;
719 int Crypto_RetrieveLocalKey(int keyid, char *keyfp, size_t keyfplen, char *idfp, size_t idfplen) // return value: -1 if more to come, +1 if valid, 0 if end of list
721 if(keyid < 0 || keyid >= MAX_PUBKEYS)
730 strlcpy(keyfp, pubkeys_fp64[keyid], keyfplen);
732 if(pubkeys_havepriv[keyid])
733 strlcpy(idfp, pubkeys_priv_fp64[keyid], keyfplen);
738 // init/shutdown code
739 static void Crypto_BuildChallengeAppend(void)
741 char *p, *lengthptr, *startptr;
744 p = challenge_append;
745 n = sizeof(challenge_append);
746 Crypto_UnLittleLong(p, PROTOCOL_VLEN);
750 Crypto_UnLittleLong(p, 0);
753 Crypto_UnLittleLong(p, PROTOCOL_D0_BLIND_ID);
757 for(i = 0; i < MAX_PUBKEYS; ++i)
758 if(pubkeys_havepriv[i])
759 PutWithNul(&p, &n, pubkeys_fp64[i]);
760 PutWithNul(&p, &n, "");
761 for(i = 0; i < MAX_PUBKEYS; ++i)
762 if(!pubkeys_havepriv[i] && pubkeys[i])
763 PutWithNul(&p, &n, pubkeys_fp64[i]);
764 Crypto_UnLittleLong(lengthptr, p - startptr);
765 challenge_append_length = p - challenge_append;
768 void Crypto_LoadKeys(void)
775 if(!d0_blind_id_dll) // don't if we can't
778 if(crypto_idstring) // already loaded? then not
781 Host_LockSession(); // we use the session ID here
784 // note: we are just a CLIENT
786 // PUBLIC KEYS to accept (including modulus)
787 // PRIVATE KEY of user
789 crypto_idstring = NULL;
790 dpsnprintf(crypto_idstring_buf, sizeof(crypto_idstring_buf), "%d", d0_rijndael_dll ? crypto_aeslevel.integer : 0);
791 for(i = 0; i < MAX_PUBKEYS; ++i)
793 memset(pubkeys_fp64[i], 0, sizeof(pubkeys_fp64[i]));
794 memset(pubkeys_priv_fp64[i], 0, sizeof(pubkeys_fp64[i]));
795 pubkeys_havepriv[i] = false;
796 len = Crypto_LoadFile(va(vabuf, sizeof(vabuf), "key_%d.d0pk", i), buf, sizeof(buf), false);
797 if((pubkeys[i] = Crypto_ReadPublicKey(buf, len)))
800 if(qd0_blind_id_fingerprint64_public_key(pubkeys[i], pubkeys_fp64[i], &len2)) // keeps final NUL
802 Con_Printf("Loaded public key key_%d.d0pk (fingerprint: %s)\n", i, pubkeys_fp64[i]);
803 len = Crypto_LoadFile(va(vabuf, sizeof(vabuf), "key_%d.d0si%s", i, sessionid.string), buf, sizeof(buf), true);
806 if(Crypto_AddPrivateKey(pubkeys[i], buf, len))
809 if(qd0_blind_id_fingerprint64_public_id(pubkeys[i], pubkeys_priv_fp64[i], &len2)) // keeps final NUL
811 Con_Printf("Loaded private ID key_%d.d0si%s for key_%d.d0pk (public key fingerprint: %s)\n", i, sessionid.string, i, pubkeys_priv_fp64[i]);
812 pubkeys_havepriv[i] = true;
813 strlcat(crypto_idstring_buf, va(vabuf, sizeof(vabuf), " %s@%s", pubkeys_priv_fp64[i], pubkeys_fp64[i]), sizeof(crypto_idstring_buf));
817 // can't really happen
818 // but nothing leaked here
825 // can't really happen
826 qd0_blind_id_free(pubkeys[i]);
831 crypto_idstring = crypto_idstring_buf;
834 Crypto_BuildChallengeAppend();
836 // find a good prefix length for all the keys we know (yes, algorithm is not perfect yet, may yield too long prefix length)
837 crypto_keyfp_recommended_length = 0;
838 memset(buf+256, 0, MAX_PUBKEYS + MAX_PUBKEYS);
839 while(crypto_keyfp_recommended_length < FP64_SIZE)
842 for(i = 0; i < MAX_PUBKEYS; ++i)
846 ++buf[(unsigned char) pubkeys_fp64[i][crypto_keyfp_recommended_length]];
847 if(pubkeys_havepriv[i])
848 if(!buf[256 + MAX_PUBKEYS + i])
849 ++buf[(unsigned char) pubkeys_priv_fp64[i][crypto_keyfp_recommended_length]];
851 for(i = 0; i < MAX_PUBKEYS; ++i)
855 if(buf[(unsigned char) pubkeys_fp64[i][crypto_keyfp_recommended_length]] < 2)
857 if(pubkeys_havepriv[i])
858 if(!buf[256 + MAX_PUBKEYS + i])
859 if(buf[(unsigned char) pubkeys_priv_fp64[i][crypto_keyfp_recommended_length]] < 2)
860 buf[256 + MAX_PUBKEYS + i] = 1;
862 ++crypto_keyfp_recommended_length;
863 for(i = 0; i < MAX_PUBKEYS; ++i)
868 if(pubkeys_havepriv[i])
869 if(!buf[256 + MAX_PUBKEYS + i])
875 if(crypto_keyfp_recommended_length < 7)
876 crypto_keyfp_recommended_length = 7;
879 static void Crypto_UnloadKeys(void)
883 for(i = 0; i < MAX_PUBKEYS; ++i)
886 qd0_blind_id_free(pubkeys[i]);
888 pubkeys_havepriv[i] = false;
889 memset(pubkeys_fp64[i], 0, sizeof(pubkeys_fp64[i]));
890 memset(pubkeys_priv_fp64[i], 0, sizeof(pubkeys_fp64[i]));
891 challenge_append_length = 0;
893 crypto_idstring = NULL;
896 static mempool_t *cryptomempool;
902 static void *Crypto_d0_malloc(size_t len)
904 return Mem_Alloc(cryptomempool, len);
907 static void Crypto_d0_free(void *p)
912 static void *Crypto_d0_createmutex(void)
914 return Thread_CreateMutex();
917 static void Crypto_d0_destroymutex(void *m)
919 Thread_DestroyMutex(m);
922 static int Crypto_d0_lockmutex(void *m)
924 return Thread_LockMutex(m);
927 static int Crypto_d0_unlockmutex(void *m)
929 return Thread_UnlockMutex(m);
935 void Crypto_Shutdown(void)
941 Thread_DestroyMutex(crypto_mutex);
944 Crypto_Rijndael_CloseLibrary();
949 for(i = 0; i < MAX_CRYPTOCONNECTS; ++i)
951 crypto = &cryptoconnects[i].crypto;
954 memset(cryptoconnects, 0, sizeof(cryptoconnects));
955 crypto = &cls.crypto;
960 qd0_blind_id_SHUTDOWN();
962 Crypto_CloseLibrary();
965 Mem_FreePool(&cryptomempool);
968 void Crypto_Init(void)
970 cryptomempool = Mem_AllocPool("crypto", 0, NULL);
972 if(!Crypto_OpenLibrary())
975 qd0_blind_id_setmallocfuncs(Crypto_d0_malloc, Crypto_d0_free);
976 if (Thread_HasThreads())
978 crypto_mutex = Thread_CreateMutex();
979 qd0_blind_id_setmutexfuncs(Crypto_d0_createmutex, Crypto_d0_destroymutex, Crypto_d0_lockmutex, Crypto_d0_unlockmutex);
982 if(!qd0_blind_id_INITIALIZE())
984 Crypto_Rijndael_CloseLibrary();
985 Crypto_CloseLibrary();
986 Con_Printf("libd0_blind_id initialization FAILED, cryptography support has been disabled\n");
990 Crypto_Rijndael_OpenLibrary(); // if this fails, it's uncritical
992 Crypto_InitHostKeys();
996 qboolean Crypto_Available(void)
1004 static void Crypto_KeyGen_Finished(int code, size_t length_received, unsigned char *buffer, void *cbdata)
1008 static char buf[8192];
1009 static char buf2[8192];
1010 size_t bufsize, buf2size;
1012 d0_blind_id_t *ctx, *ctx2;
1017 if (crypto_mutex) Thread_LockMutex(crypto_mutex);
1019 if(!d0_blind_id_dll)
1021 Con_Print("libd0_blind_id DLL not found, this command is inactive.\n");
1023 if (crypto_mutex) Thread_UnlockMutex(crypto_mutex);
1027 if(keygen_i >= MAX_PUBKEYS || !pubkeys[keygen_i])
1029 Con_Printf("overflow of keygen_i\n");
1031 if (crypto_mutex) Thread_UnlockMutex(crypto_mutex);
1036 Con_Printf("Unexpected response from keygen server:\n");
1037 Com_HexDumpToConsole(buffer, length_received);
1038 if (crypto_mutex) Thread_UnlockMutex(crypto_mutex);
1041 if(!Crypto_ParsePack((const char *) buffer, length_received, FOURCC_D0IR, p, l, 1))
1043 if(length_received >= 5 && Crypto_LittleLong((const char *) buffer) == FOURCC_D0ER)
1045 Con_Printf("Error response from keygen server: %.*s\n", (int)(length_received - 5), buffer + 5);
1049 Con_Printf("Invalid response from keygen server:\n");
1050 Com_HexDumpToConsole(buffer, length_received);
1053 if (crypto_mutex) Thread_UnlockMutex(crypto_mutex);
1056 if(!qd0_blind_id_finish_private_id_request(pubkeys[keygen_i], p[0], l[0]))
1058 Con_Printf("d0_blind_id_finish_private_id_request failed\n");
1060 if (crypto_mutex) Thread_UnlockMutex(crypto_mutex);
1064 // verify the key we just got (just in case)
1065 ctx = qd0_blind_id_new();
1068 Con_Printf("d0_blind_id_new failed\n");
1070 if (crypto_mutex) Thread_UnlockMutex(crypto_mutex);
1073 ctx2 = qd0_blind_id_new();
1076 Con_Printf("d0_blind_id_new failed\n");
1077 qd0_blind_id_free(ctx);
1079 if (crypto_mutex) Thread_UnlockMutex(crypto_mutex);
1082 if(!qd0_blind_id_copy(ctx, pubkeys[keygen_i]))
1084 Con_Printf("d0_blind_id_copy failed\n");
1085 qd0_blind_id_free(ctx);
1086 qd0_blind_id_free(ctx2);
1088 if (crypto_mutex) Thread_UnlockMutex(crypto_mutex);
1091 if(!qd0_blind_id_copy(ctx2, pubkeys[keygen_i]))
1093 Con_Printf("d0_blind_id_copy failed\n");
1094 qd0_blind_id_free(ctx);
1095 qd0_blind_id_free(ctx2);
1097 if (crypto_mutex) Thread_UnlockMutex(crypto_mutex);
1100 bufsize = sizeof(buf);
1101 if(!qd0_blind_id_authenticate_with_private_id_start(ctx, 1, 1, "hello world", 11, buf, &bufsize))
1103 Con_Printf("d0_blind_id_authenticate_with_private_id_start failed\n");
1104 qd0_blind_id_free(ctx);
1105 qd0_blind_id_free(ctx2);
1107 if (crypto_mutex) Thread_UnlockMutex(crypto_mutex);
1110 buf2size = sizeof(buf2);
1111 if(!qd0_blind_id_authenticate_with_private_id_challenge(ctx2, 1, 1, buf, bufsize, buf2, &buf2size, &status) || !status)
1113 Con_Printf("d0_blind_id_authenticate_with_private_id_challenge failed (server does not have the requested private key)\n");
1114 qd0_blind_id_free(ctx);
1115 qd0_blind_id_free(ctx2);
1117 if (crypto_mutex) Thread_UnlockMutex(crypto_mutex);
1120 bufsize = sizeof(buf);
1121 if(!qd0_blind_id_authenticate_with_private_id_response(ctx, buf2, buf2size, buf, &bufsize))
1123 Con_Printf("d0_blind_id_authenticate_with_private_id_response failed\n");
1124 qd0_blind_id_free(ctx);
1125 qd0_blind_id_free(ctx2);
1127 if (crypto_mutex) Thread_UnlockMutex(crypto_mutex);
1130 buf2size = sizeof(buf2);
1131 if(!qd0_blind_id_authenticate_with_private_id_verify(ctx2, buf, bufsize, buf2, &buf2size, &status) || !status)
1133 Con_Printf("d0_blind_id_authenticate_with_private_id_verify failed (server does not have the requested private key)\n");
1134 qd0_blind_id_free(ctx);
1135 qd0_blind_id_free(ctx2);
1137 if (crypto_mutex) Thread_UnlockMutex(crypto_mutex);
1140 qd0_blind_id_free(ctx);
1141 qd0_blind_id_free(ctx2);
1143 // we have a valid key now!
1144 // make the rest of crypto.c know that
1146 if(qd0_blind_id_fingerprint64_public_id(pubkeys[keygen_i], pubkeys_priv_fp64[keygen_i], &len2)) // keeps final NUL
1148 Con_Printf("Received private ID key_%d.d0pk (public key fingerprint: %s)\n", keygen_i, pubkeys_priv_fp64[keygen_i]);
1149 pubkeys_havepriv[keygen_i] = true;
1150 strlcat(crypto_idstring_buf, va(vabuf, sizeof(vabuf), " %s@%s", pubkeys_priv_fp64[keygen_i], pubkeys_fp64[keygen_i]), sizeof(crypto_idstring_buf));
1151 crypto_idstring = crypto_idstring_buf;
1152 Crypto_BuildChallengeAppend();
1154 // write the key to disk
1157 if(!qd0_blind_id_write_private_id(pubkeys[keygen_i], buf, &l[0]))
1159 Con_Printf("d0_blind_id_write_private_id failed\n");
1161 if (crypto_mutex) Thread_UnlockMutex(crypto_mutex);
1164 if(!(buf2size = Crypto_UnParsePack(buf2, sizeof(buf2), FOURCC_D0SI, p, l, 1)))
1166 Con_Printf("Crypto_UnParsePack failed\n");
1168 if (crypto_mutex) Thread_UnlockMutex(crypto_mutex);
1172 FS_CreatePath(va(vabuf, sizeof(vabuf), "%skey_%d.d0si%s", *fs_userdir ? fs_userdir : fs_basedir, keygen_i, sessionid.string));
1173 f = FS_SysOpen(va(vabuf, sizeof(vabuf), "%skey_%d.d0si%s", *fs_userdir ? fs_userdir : fs_basedir, keygen_i, sessionid.string), "wb", false);
1176 Con_Printf("Cannot open key_%d.d0si%s\n", keygen_i, sessionid.string);
1178 if (crypto_mutex) Thread_UnlockMutex(crypto_mutex);
1181 FS_Write(f, buf2, buf2size);
1184 Con_Printf("Saved to key_%d.d0si%s\n", keygen_i, sessionid.string);
1186 if (crypto_mutex) Thread_UnlockMutex(crypto_mutex);
1189 static void Crypto_KeyGen_f(void)
1194 static char buf[8192];
1195 static char buf2[8192];
1196 size_t buf2l, buf2pos;
1197 if(!d0_blind_id_dll)
1199 Con_Print("libd0_blind_id DLL not found, this command is inactive.\n");
1204 Con_Printf("usage:\n%s id url\n", Cmd_Argv(0));
1207 if (crypto_mutex) Thread_LockMutex(crypto_mutex);
1209 i = atoi(Cmd_Argv(1));
1212 Con_Printf("there is no public key %d\n", i);
1213 if (crypto_mutex) Thread_UnlockMutex(crypto_mutex);
1216 if(pubkeys_havepriv[i])
1218 Con_Printf("there is already a private key for %d\n", i);
1219 if (crypto_mutex) Thread_UnlockMutex(crypto_mutex);
1224 Con_Printf("there is already a keygen run on the way\n");
1225 if (crypto_mutex) Thread_UnlockMutex(crypto_mutex);
1229 if(!qd0_blind_id_generate_private_id_start(pubkeys[keygen_i]))
1231 Con_Printf("d0_blind_id_start failed\n");
1233 if (crypto_mutex) Thread_UnlockMutex(crypto_mutex);
1238 if(!qd0_blind_id_generate_private_id_request(pubkeys[keygen_i], buf, &l[0]))
1240 Con_Printf("d0_blind_id_generate_private_id_request failed\n");
1242 if (crypto_mutex) Thread_UnlockMutex(crypto_mutex);
1245 buf2pos = strlen(Cmd_Argv(2));
1246 memcpy(buf2, Cmd_Argv(2), buf2pos);
1247 if(!(buf2l = Crypto_UnParsePack(buf2 + buf2pos, sizeof(buf2) - buf2pos - 1, FOURCC_D0IQ, p, l, 1)))
1249 Con_Printf("Crypto_UnParsePack failed\n");
1251 if (crypto_mutex) Thread_UnlockMutex(crypto_mutex);
1254 if(!(buf2l = base64_encode((unsigned char *) (buf2 + buf2pos), buf2l, sizeof(buf2) - buf2pos - 1)))
1256 Con_Printf("base64_encode failed\n");
1258 if (crypto_mutex) Thread_UnlockMutex(crypto_mutex);
1263 if(!Curl_Begin_ToMemory(buf2, 0, (unsigned char *) keygen_buf, sizeof(keygen_buf), Crypto_KeyGen_Finished, NULL))
1265 Con_Printf("curl failed\n");
1267 if (crypto_mutex) Thread_UnlockMutex(crypto_mutex);
1270 Con_Printf("key generation in progress\n");
1271 if (crypto_mutex) Thread_UnlockMutex(crypto_mutex);
1276 static void Crypto_Reload_f(void)
1278 Crypto_ClearHostKeys();
1279 Crypto_UnloadKeys();
1283 static void Crypto_Keys_f(void)
1286 if(!d0_blind_id_dll)
1288 Con_Print("libd0_blind_id DLL not found, this command is inactive.\n");
1291 for(i = 0; i < MAX_PUBKEYS; ++i)
1295 Con_Printf("%2d: public key key_%d.d0pk (fingerprint: %s)\n", i, i, pubkeys_fp64[i]);
1296 if(pubkeys_havepriv[i])
1297 Con_Printf(" private ID key_%d.d0si%s (public key fingerprint: %s)\n", i, sessionid.string, pubkeys_priv_fp64[i]);
1302 static void Crypto_HostKeys_f(void)
1305 crypto_storedhostkey_t *hk;
1308 if(!d0_blind_id_dll)
1310 Con_Print("libd0_blind_id DLL not found, this command is inactive.\n");
1313 for(i = 0; i < CRYPTO_HOSTKEY_HASHSIZE; ++i)
1315 for(hk = crypto_storedhostkey_hashtable[i]; hk; hk = hk->next)
1317 LHNETADDRESS_ToString(&hk->addr, buf, sizeof(buf), 1);
1318 Con_Printf("%d %s@%.*s %s\n",
1321 crypto_keyfp_recommended_length, pubkeys_fp64[hk->keyid],
1327 static void Crypto_HostKey_Clear_f(void)
1329 lhnetaddress_t addr;
1332 if(!d0_blind_id_dll)
1334 Con_Print("libd0_blind_id DLL not found, this command is inactive.\n");
1338 for(i = 1; i < Cmd_Argc(); ++i)
1340 LHNETADDRESS_FromString(&addr, Cmd_Argv(i), 26000);
1341 if(Crypto_ClearHostKey(&addr))
1343 Con_Printf("cleared host key for %s\n", Cmd_Argv(i));
1348 void Crypto_Init_Commands(void)
1352 Cmd_AddCommand("crypto_reload", Crypto_Reload_f, "reloads cryptographic keys");
1353 Cmd_AddCommand("crypto_keygen", Crypto_KeyGen_f, "generates and saves a cryptographic key");
1354 Cmd_AddCommand("crypto_keys", Crypto_Keys_f, "lists the loaded keys");
1355 Cmd_AddCommand("crypto_hostkeys", Crypto_HostKeys_f, "lists the cached host keys");
1356 Cmd_AddCommand("crypto_hostkey_clear", Crypto_HostKey_Clear_f, "clears a cached host key");
1357 Cvar_RegisterVariable(&crypto_developer);
1359 Cvar_RegisterVariable(&crypto_aeslevel);
1361 crypto_aeslevel.integer = 0; // make sure
1362 Cvar_RegisterVariable(&crypto_servercpupercent);
1363 Cvar_RegisterVariable(&crypto_servercpumaxtime);
1364 Cvar_RegisterVariable(&crypto_servercpudebug);
1370 static void aescpy(unsigned char *key, const unsigned char *iv, unsigned char *dst, const unsigned char *src, size_t len)
1372 const unsigned char *xorpos = iv;
1373 unsigned char xorbuf[16];
1374 unsigned long rk[D0_RIJNDAEL_RKLENGTH(DHKEY_SIZE * 8)];
1376 qd0_rijndael_setup_encrypt(rk, key, DHKEY_SIZE * 8);
1379 for(i = 0; i < 16; ++i)
1380 xorbuf[i] = src[i] ^ xorpos[i];
1381 qd0_rijndael_encrypt(rk, D0_RIJNDAEL_NROUNDS(DHKEY_SIZE * 8), xorbuf, dst);
1389 for(i = 0; i < len; ++i)
1390 xorbuf[i] = src[i] ^ xorpos[i];
1392 xorbuf[i] = xorpos[i];
1393 qd0_rijndael_encrypt(rk, D0_RIJNDAEL_NROUNDS(DHKEY_SIZE * 8), xorbuf, dst);
1396 static void seacpy(unsigned char *key, const unsigned char *iv, unsigned char *dst, const unsigned char *src, size_t len)
1398 const unsigned char *xorpos = iv;
1399 unsigned char xorbuf[16];
1400 unsigned long rk[D0_RIJNDAEL_RKLENGTH(DHKEY_SIZE * 8)];
1402 qd0_rijndael_setup_decrypt(rk, key, DHKEY_SIZE * 8);
1405 qd0_rijndael_decrypt(rk, D0_RIJNDAEL_NROUNDS(DHKEY_SIZE * 8), src, xorbuf);
1406 for(i = 0; i < 16; ++i)
1407 dst[i] = xorbuf[i] ^ xorpos[i];
1415 qd0_rijndael_decrypt(rk, D0_RIJNDAEL_NROUNDS(DHKEY_SIZE * 8), src, xorbuf);
1416 for(i = 0; i < len; ++i)
1417 dst[i] = xorbuf[i] ^ xorpos[i];
1421 // NOTE: we MUST avoid the following begins of the packet:
1422 // 1. 0xFF, 0xFF, 0xFF, 0xFF
1423 // 2. 0x80, 0x00, length/256, length%256
1424 // this luckily does NOT affect AES mode, where the first byte always is in the range from 0x00 to 0x0F
1425 const void *Crypto_EncryptPacket(crypto_t *crypto, const void *data_src, size_t len_src, void *data_dst, size_t *len_dst, size_t len)
1427 unsigned char h[32];
1429 if(crypto->authenticated)
1433 // AES packet = 1 byte length overhead, 15 bytes from HMAC-SHA-256, data, 0..15 bytes padding
1434 // 15 bytes HMAC-SHA-256 (112bit) suffice as the attacker can't do more than forge a random-looking packet
1435 // HMAC is needed to not leak information about packet content
1436 if(developer_networking.integer)
1438 Con_Print("To be encrypted:\n");
1439 Com_HexDumpToConsole((const unsigned char *) data_src, len_src);
1441 if(len_src + 32 > len || !HMAC_SHA256_32BYTES(h, (const unsigned char *) data_src, len_src, crypto->dhkey, DHKEY_SIZE))
1443 Con_Printf("Crypto_EncryptPacket failed (not enough space: %d bytes in, %d bytes out)\n", (int) len_src, (int) len);
1446 *len_dst = ((len_src + 15) / 16) * 16 + 16; // add 16 for HMAC, then round to 16-size for AES
1447 ((unsigned char *) data_dst)[0] = *len_dst - len_src;
1448 memcpy(((unsigned char *) data_dst)+1, h, 15);
1449 aescpy(crypto->dhkey, (const unsigned char *) data_dst, ((unsigned char *) data_dst) + 16, (const unsigned char *) data_src, len_src);
1454 // HMAC packet = 16 bytes HMAC-SHA-256 (truncated to 128 bits), data
1455 if(len_src + 16 > len || !HMAC_SHA256_32BYTES(h, (const unsigned char *) data_src, len_src, crypto->dhkey, DHKEY_SIZE))
1457 Con_Printf("Crypto_EncryptPacket failed (not enough space: %d bytes in, %d bytes out)\n", (int) len_src, (int) len);
1460 *len_dst = len_src + 16;
1461 memcpy(data_dst, h, 16);
1462 memcpy(((unsigned char *) data_dst) + 16, (unsigned char *) data_src, len_src);
1464 // handle the "avoid" conditions:
1465 i = BuffBigLong((unsigned char *) data_dst);
1467 (i == (int)0xFFFFFFFF) // avoid QW control packet
1469 (i == (int)0x80000000 + (int)*len_dst) // avoid NQ control packet
1471 *(unsigned char *)data_dst ^= 0x80; // this will ALWAYS fix it
1482 const void *Crypto_DecryptPacket(crypto_t *crypto, const void *data_src, size_t len_src, void *data_dst, size_t *len_dst, size_t len)
1484 unsigned char h[32];
1487 // silently handle non-crypto packets
1488 i = BuffBigLong((unsigned char *) data_src);
1490 (i == (int)0xFFFFFFFF) // avoid QW control packet
1492 (i == (int)0x80000000 + (int)len_src) // avoid NQ control packet
1496 if(crypto->authenticated)
1500 if(len_src < 16 || ((len_src - 16) % 16))
1502 Con_Printf("Crypto_DecryptPacket failed (not enough space: %d bytes in, %d bytes out)\n", (int) len_src, (int) len);
1505 *len_dst = len_src - ((unsigned char *) data_src)[0];
1506 if(len < *len_dst || *len_dst > len_src - 16)
1508 Con_Printf("Crypto_DecryptPacket failed (not enough space: %d bytes in, %d->%d bytes out)\n", (int) len_src, (int) *len_dst, (int) len);
1511 seacpy(crypto->dhkey, (unsigned char *) data_src, (unsigned char *) data_dst, ((const unsigned char *) data_src) + 16, *len_dst);
1513 if(!HMAC_SHA256_32BYTES(h, (const unsigned char *) data_dst, *len_dst, crypto->dhkey, DHKEY_SIZE))
1515 Con_Printf("HMAC fail\n");
1518 if(memcmp(((const unsigned char *) data_src)+1, h, 15)) // ignore first byte, used for length
1520 Con_Printf("HMAC mismatch\n");
1523 if(developer_networking.integer)
1525 Con_Print("Decrypted:\n");
1526 Com_HexDumpToConsole((const unsigned char *) data_dst, *len_dst);
1528 return data_dst; // no need to copy
1534 Con_Printf("Crypto_DecryptPacket failed (not enough space: %d bytes in, %d bytes out)\n", (int) len_src, (int) len);
1537 *len_dst = len_src - 16;
1540 Con_Printf("Crypto_DecryptPacket failed (not enough space: %d bytes in, %d->%d bytes out)\n", (int) len_src, (int) *len_dst, (int) len);
1543 //memcpy(data_dst, data_src + 16, *len_dst);
1544 if(!HMAC_SHA256_32BYTES(h, ((const unsigned char *) data_src) + 16, *len_dst, crypto->dhkey, DHKEY_SIZE))
1546 Con_Printf("HMAC fail\n");
1547 Com_HexDumpToConsole((const unsigned char *) data_src, len_src);
1551 if(memcmp((const unsigned char *) data_src, h, 16)) // ignore first byte, used for length
1553 // undo the "avoid conditions"
1555 (i == (int)0x7FFFFFFF) // avoided QW control packet
1557 (i == (int)0x00000000 + (int)len_src) // avoided NQ control packet
1560 // do the avoidance on the hash too
1562 if(memcmp((const unsigned char *) data_src, h, 16)) // ignore first byte, used for length
1564 Con_Printf("HMAC mismatch\n");
1565 Com_HexDumpToConsole((const unsigned char *) data_src, len_src);
1571 Con_Printf("HMAC mismatch\n");
1572 Com_HexDumpToConsole((const unsigned char *) data_src, len_src);
1576 return ((const unsigned char *) data_src) + 16; // no need to copy, so data_dst is not used
1587 const char *Crypto_GetInfoResponseDataString(void)
1589 crypto_idstring_buf[0] = '0' + crypto_aeslevel.integer;
1590 return crypto_idstring;
1594 qboolean Crypto_ServerAppendToChallenge(const char *data_in, size_t len_in, char *data_out, size_t *len_out, size_t maxlen_out)
1596 // cheap op, all is precomputed
1597 if(!d0_blind_id_dll)
1598 return false; // no support
1600 if(maxlen_out <= *len_out + challenge_append_length)
1602 memcpy(data_out + *len_out, challenge_append, challenge_append_length);
1603 *len_out += challenge_append_length;
1607 static int Crypto_ServerError(char *data_out, size_t *len_out, const char *msg, const char *msg_client)
1611 Con_DPrintf("rejecting client: %s\n", msg);
1613 dpsnprintf(data_out, *len_out, "reject %s", msg_client);
1614 *len_out = strlen(data_out);
1615 return CRYPTO_DISCARD;
1618 static int Crypto_SoftServerError(char *data_out, size_t *len_out, const char *msg)
1621 Con_DPrintf("%s\n", msg);
1622 return CRYPTO_DISCARD;
1625 static int Crypto_ServerParsePacket_Internal(const char *data_in, size_t len_in, char *data_out, size_t *len_out, lhnetaddress_t *peeraddress)
1627 // if "connect": reject if in the middle of crypto handshake
1628 crypto_t *crypto = NULL;
1629 char *data_out_p = data_out;
1630 const char *string = data_in;
1634 char infostringvalue[MAX_INPUTLINE];
1637 if(!d0_blind_id_dll)
1638 return CRYPTO_NOMATCH; // no support
1640 if (len_in > 8 && !memcmp(string, "connect\\", 8) && d0_rijndael_dll && crypto_aeslevel.integer >= 3)
1644 // sorry, we have to verify the challenge here to not reflect network spam
1646 if (!(s = InfoString_GetValue(string + 4, "challenge", infostringvalue, sizeof(infostringvalue))))
1647 return CRYPTO_NOMATCH; // will be later accepted if encryption was set up
1648 // validate the challenge
1649 for (i = 0;i < MAX_CHALLENGES;i++)
1650 if(challenge[i].time > 0)
1651 if (!LHNETADDRESS_Compare(peeraddress, &challenge[i].address) && !strcmp(challenge[i].string, s))
1653 // if the challenge is not recognized, drop the packet
1654 if (i == MAX_CHALLENGES) // challenge mismatch is silent
1655 return CRYPTO_DISCARD; // pre-challenge: rather be silent
1657 crypto = Crypto_ServerFindInstance(peeraddress, false);
1658 if(!crypto || !crypto->authenticated)
1659 return Crypto_ServerError(data_out, len_out, "This server requires authentication and encryption to be supported by your client", NULL);
1661 else if(len_in > 5 && !memcmp(string, "d0pk\\", 5) && ((LHNETADDRESS_GetAddressType(peeraddress) == LHNETADDRESSTYPE_LOOP) || sv_public.integer > -3))
1663 const char *cnt, *s, *p;
1665 int clientid = -1, serverid = -1;
1666 cnt = InfoString_GetValue(string + 4, "id", infostringvalue, sizeof(infostringvalue));
1667 id = (cnt ? atoi(cnt) : -1);
1668 cnt = InfoString_GetValue(string + 4, "cnt", infostringvalue, sizeof(infostringvalue));
1670 return CRYPTO_DISCARD; // pre-challenge: rather be silent
1671 GetUntilNul(&data_in, &len_in);
1673 return CRYPTO_DISCARD; // pre-challenge: rather be silent
1674 if(!strcmp(cnt, "0"))
1677 if (!(s = InfoString_GetValue(string + 4, "challenge", infostringvalue, sizeof(infostringvalue))))
1678 return CRYPTO_DISCARD; // pre-challenge: rather be silent
1679 // validate the challenge
1680 for (i = 0;i < MAX_CHALLENGES;i++)
1681 if(challenge[i].time > 0)
1682 if (!LHNETADDRESS_Compare(peeraddress, &challenge[i].address) && !strcmp(challenge[i].string, s))
1684 // if the challenge is not recognized, drop the packet
1685 if (i == MAX_CHALLENGES) // challenge mismatch is silent
1686 return CRYPTO_DISCARD; // pre-challenge: rather be silent
1688 if (!(s = InfoString_GetValue(string + 4, "aeslevel", infostringvalue, sizeof(infostringvalue))))
1689 aeslevel = 0; // not supported
1691 aeslevel = bound(0, atoi(s), 3);
1692 switch(bound(0, d0_rijndael_dll ? crypto_aeslevel.integer : 0, 3))
1694 default: // dummy, never happens, but to make gcc happy...
1697 return Crypto_ServerError(data_out, len_out, "This server requires encryption to be not required (crypto_aeslevel <= 2)", NULL);
1701 aes = (aeslevel >= 2);
1704 aes = (aeslevel >= 1);
1708 return Crypto_ServerError(data_out, len_out, "This server requires encryption to be supported (crypto_aeslevel >= 1, and d0_rijndael library must be present)", NULL);
1713 p = GetUntilNul(&data_in, &len_in);
1716 for(i = 0; i < MAX_PUBKEYS; ++i)
1719 if(!strcmp(p, pubkeys_fp64[i]))
1720 if(pubkeys_havepriv[i])
1725 return Crypto_ServerError(data_out, len_out, "Invalid server key", NULL);
1727 p = GetUntilNul(&data_in, &len_in);
1730 for(i = 0; i < MAX_PUBKEYS; ++i)
1733 if(!strcmp(p, pubkeys_fp64[i]))
1738 return Crypto_ServerError(data_out, len_out, "Invalid client key", NULL);
1741 crypto = Crypto_ServerFindInstance(peeraddress, true);
1743 return Crypto_ServerError(data_out, len_out, "Could not create a crypto connect instance", NULL);
1745 CDATA->cdata_id = id;
1746 CDATA->s = serverid;
1747 CDATA->c = clientid;
1748 memset(crypto->dhkey, 0, sizeof(crypto->dhkey));
1749 CDATA->challenge[0] = 0;
1750 crypto->client_keyfp[0] = 0;
1751 crypto->client_idfp[0] = 0;
1752 crypto->server_keyfp[0] = 0;
1753 crypto->server_idfp[0] = 0;
1754 crypto->use_aes = aes != 0;
1758 // I am the server, and my key is ok... so let's set server_keyfp and server_idfp
1759 strlcpy(crypto->server_keyfp, pubkeys_fp64[CDATA->s], sizeof(crypto->server_keyfp));
1760 strlcpy(crypto->server_idfp, pubkeys_priv_fp64[CDATA->s], sizeof(crypto->server_idfp));
1763 CDATA->id = qd0_blind_id_new();
1767 return Crypto_ServerError(data_out, len_out, "d0_blind_id_new failed", "Internal error");
1769 if(!qd0_blind_id_copy(CDATA->id, pubkeys[CDATA->s]))
1772 return Crypto_ServerError(data_out, len_out, "d0_blind_id_copy failed", "Internal error");
1774 PutWithNul(&data_out_p, len_out, va(vabuf, sizeof(vabuf), "d0pk\\cnt\\1\\id\\%d\\aes\\%d", CDATA->cdata_id, crypto->use_aes));
1775 if(!qd0_blind_id_authenticate_with_private_id_start(CDATA->id, true, false, "XONOTIC", 8, data_out_p, len_out)) // len_out receives used size by this op
1778 return Crypto_ServerError(data_out, len_out, "d0_blind_id_authenticate_with_private_id_start failed", "Internal error");
1780 CDATA->next_step = 2;
1781 data_out_p += *len_out;
1782 *len_out = data_out_p - data_out;
1783 return CRYPTO_DISCARD;
1785 else if(CDATA->c >= 0)
1788 CDATA->id = qd0_blind_id_new();
1792 return Crypto_ServerError(data_out, len_out, "d0_blind_id_new failed", "Internal error");
1794 if(!qd0_blind_id_copy(CDATA->id, pubkeys[CDATA->c]))
1797 return Crypto_ServerError(data_out, len_out, "d0_blind_id_copy failed", "Internal error");
1799 PutWithNul(&data_out_p, len_out, va(vabuf, sizeof(vabuf), "d0pk\\cnt\\5\\id\\%d\\aes\\%d", CDATA->cdata_id, crypto->use_aes));
1800 if(!qd0_blind_id_authenticate_with_private_id_challenge(CDATA->id, true, false, data_in, len_in, data_out_p, len_out, &status))
1803 return Crypto_ServerError(data_out, len_out, "d0_blind_id_authenticate_with_private_id_challenge failed", "Internal error");
1805 CDATA->next_step = 6;
1806 data_out_p += *len_out;
1807 *len_out = data_out_p - data_out;
1808 return CRYPTO_DISCARD;
1813 return Crypto_ServerError(data_out, len_out, "Missing client and server key", NULL);
1816 else if(!strcmp(cnt, "2"))
1819 crypto = Crypto_ServerFindInstance(peeraddress, false);
1821 return CRYPTO_NOMATCH; // pre-challenge, rather be silent
1823 if(CDATA->cdata_id != id)
1824 return Crypto_SoftServerError(data_out, len_out, va(vabuf, sizeof(vabuf), "Got d0pk\\id\\%d when expecting %d", id, CDATA->cdata_id));
1825 if(CDATA->next_step != 2)
1826 return Crypto_SoftServerError(data_out, len_out, va(vabuf, sizeof(vabuf), "Got d0pk\\cnt\\%s when expecting %d", cnt, CDATA->next_step));
1828 PutWithNul(&data_out_p, len_out, va(vabuf, sizeof(vabuf), "d0pk\\cnt\\3\\id\\%d", CDATA->cdata_id));
1829 if(!qd0_blind_id_authenticate_with_private_id_response(CDATA->id, data_in, len_in, data_out_p, len_out))
1832 return Crypto_ServerError(data_out, len_out, "d0_blind_id_authenticate_with_private_id_response failed", "Internal error");
1834 fpbuflen = DHKEY_SIZE;
1835 if(!qd0_blind_id_sessionkey_public_id(CDATA->id, (char *) crypto->dhkey, &fpbuflen))
1838 return Crypto_ServerError(data_out, len_out, "d0_blind_id_sessionkey_public_id failed", "Internal error");
1842 if(!qd0_blind_id_copy(CDATA->id, pubkeys[CDATA->c]))
1845 return Crypto_ServerError(data_out, len_out, "d0_blind_id_copy failed", "Internal error");
1847 CDATA->next_step = 4;
1851 // session key is FINISHED (no server part is to be expected)! By this, all keys are set up
1852 crypto->authenticated = true;
1853 CDATA->next_step = 0;
1855 data_out_p += *len_out;
1856 *len_out = data_out_p - data_out;
1857 return CRYPTO_DISCARD;
1859 else if(!strcmp(cnt, "4"))
1861 crypto = Crypto_ServerFindInstance(peeraddress, false);
1863 return CRYPTO_NOMATCH; // pre-challenge, rather be silent
1865 if(CDATA->cdata_id != id)
1866 return Crypto_SoftServerError(data_out, len_out, va(vabuf, sizeof(vabuf), "Got d0pk\\id\\%d when expecting %d", id, CDATA->cdata_id));
1867 if(CDATA->next_step != 4)
1868 return Crypto_SoftServerError(data_out, len_out, va(vabuf, sizeof(vabuf), "Got d0pk\\cnt\\%s when expecting %d", cnt, CDATA->next_step));
1869 PutWithNul(&data_out_p, len_out, va(vabuf, sizeof(vabuf), "d0pk\\cnt\\5\\id\\%d", CDATA->cdata_id));
1870 if(!qd0_blind_id_authenticate_with_private_id_challenge(CDATA->id, true, false, data_in, len_in, data_out_p, len_out, &status))
1873 return Crypto_ServerError(data_out, len_out, "d0_blind_id_authenticate_with_private_id_challenge failed", "Internal error");
1875 CDATA->next_step = 6;
1876 data_out_p += *len_out;
1877 *len_out = data_out_p - data_out;
1878 return CRYPTO_DISCARD;
1880 else if(!strcmp(cnt, "6"))
1882 static char msgbuf[32];
1883 size_t msgbuflen = sizeof(msgbuf);
1886 unsigned char dhkey[DHKEY_SIZE];
1887 crypto = Crypto_ServerFindInstance(peeraddress, false);
1889 return CRYPTO_NOMATCH; // pre-challenge, rather be silent
1891 if(CDATA->cdata_id != id)
1892 return Crypto_SoftServerError(data_out, len_out, va(vabuf, sizeof(vabuf), "Got d0pk\\id\\%d when expecting %d", id, CDATA->cdata_id));
1893 if(CDATA->next_step != 6)
1894 return Crypto_SoftServerError(data_out, len_out, va(vabuf, sizeof(vabuf), "Got d0pk\\cnt\\%s when expecting %d", cnt, CDATA->next_step));
1896 if(!qd0_blind_id_authenticate_with_private_id_verify(CDATA->id, data_in, len_in, msgbuf, &msgbuflen, &status))
1899 return Crypto_ServerError(data_out, len_out, "d0_blind_id_authenticate_with_private_id_verify failed (authentication error)", "Authentication error");
1902 strlcpy(crypto->client_keyfp, pubkeys_fp64[CDATA->c], sizeof(crypto->client_keyfp));
1904 crypto->client_keyfp[0] = 0;
1905 memset(crypto->client_idfp, 0, sizeof(crypto->client_idfp));
1906 fpbuflen = FP64_SIZE;
1907 if(!qd0_blind_id_fingerprint64_public_id(CDATA->id, crypto->client_idfp, &fpbuflen))
1910 return Crypto_ServerError(data_out, len_out, "d0_blind_id_fingerprint64_public_id failed", "Internal error");
1912 fpbuflen = DHKEY_SIZE;
1913 if(!qd0_blind_id_sessionkey_public_id(CDATA->id, (char *) dhkey, &fpbuflen))
1916 return Crypto_ServerError(data_out, len_out, "d0_blind_id_sessionkey_public_id failed", "Internal error");
1918 // XOR the two DH keys together to make one
1919 for(i = 0; i < DHKEY_SIZE; ++i)
1920 crypto->dhkey[i] ^= dhkey[i];
1922 // session key is FINISHED (no server part is to be expected)! By this, all keys are set up
1923 crypto->authenticated = true;
1924 CDATA->next_step = 0;
1925 // send a challenge-less challenge
1926 PutWithNul(&data_out_p, len_out, "challenge ");
1927 *len_out = data_out_p - data_out;
1928 --*len_out; // remove NUL terminator
1929 return CRYPTO_MATCH;
1931 return CRYPTO_NOMATCH; // pre-challenge, rather be silent
1933 return CRYPTO_NOMATCH;
1936 int Crypto_ServerParsePacket(const char *data_in, size_t len_in, char *data_out, size_t *len_out, lhnetaddress_t *peeraddress)
1940 static double complain_time = 0;
1942 qboolean do_time = false;
1943 qboolean do_reject = false;
1944 char infostringvalue[MAX_INPUTLINE];
1945 if(crypto_servercpupercent.value > 0 || crypto_servercpumaxtime.value > 0)
1946 if(len_in > 5 && !memcmp(data_in, "d0pk\\", 5))
1949 cnt = InfoString_GetValue(data_in + 4, "cnt", infostringvalue, sizeof(infostringvalue));
1951 if(!strcmp(cnt, "0"))
1956 // check if we may perform crypto...
1957 if(crypto_servercpupercent.value > 0)
1959 crypto_servercpu_accumulator += (realtime - crypto_servercpu_lastrealtime) * crypto_servercpupercent.value * 0.01;
1960 if(crypto_servercpumaxtime.value)
1961 if(crypto_servercpu_accumulator > crypto_servercpumaxtime.value)
1962 crypto_servercpu_accumulator = crypto_servercpumaxtime.value;
1966 if(crypto_servercpumaxtime.value > 0)
1967 if(realtime != crypto_servercpu_lastrealtime)
1968 crypto_servercpu_accumulator = crypto_servercpumaxtime.value;
1970 crypto_servercpu_lastrealtime = realtime;
1971 if(do_reject && crypto_servercpu_accumulator < 0)
1973 if(realtime > complain_time + 5)
1974 Con_Printf("crypto: cannot perform requested crypto operations; denial service attack or crypto_servercpupercent/crypto_servercpumaxtime are too low\n");
1976 return CRYPTO_DISCARD;
1978 t = Sys_DirtyTime();
1980 ret = Crypto_ServerParsePacket_Internal(data_in, len_in, data_out, len_out, peeraddress);
1983 t = Sys_DirtyTime() - t;if (t < 0.0) t = 0.0; // dirtytime can step backwards
1984 if(crypto_servercpudebug.integer)
1985 Con_Printf("crypto: accumulator was %.1f ms, used %.1f ms for crypto, ", crypto_servercpu_accumulator * 1000, t * 1000);
1986 crypto_servercpu_accumulator -= t;
1987 if(crypto_servercpudebug.integer)
1988 Con_Printf("is %.1f ms\n", crypto_servercpu_accumulator * 1000);
1993 static int Crypto_ClientError(char *data_out, size_t *len_out, const char *msg)
1995 dpsnprintf(data_out, *len_out, "reject %s", msg);
1996 *len_out = strlen(data_out);
1997 return CRYPTO_REPLACE;
2000 static int Crypto_SoftClientError(char *data_out, size_t *len_out, const char *msg)
2003 Con_Printf("%s\n", msg);
2004 return CRYPTO_DISCARD;
2007 int Crypto_ClientParsePacket(const char *data_in, size_t len_in, char *data_out, size_t *len_out, lhnetaddress_t *peeraddress)
2009 crypto_t *crypto = &cls.crypto;
2010 const char *string = data_in;
2013 char *data_out_p = data_out;
2015 char infostringvalue[MAX_INPUTLINE];
2018 if(!d0_blind_id_dll)
2019 return CRYPTO_NOMATCH; // no support
2021 // if "challenge": verify challenge, and discard message, send next crypto protocol message instead
2022 // otherwise, just handle actual protocol messages
2024 if (len_in == 6 && !memcmp(string, "accept", 6) && cls.connect_trying && d0_rijndael_dll)
2026 int wantserverid = -1;
2027 Crypto_RetrieveHostKey(&cls.connect_address, &wantserverid, NULL, 0, NULL, 0, NULL);
2028 if(!crypto || !crypto->authenticated)
2030 if(wantserverid >= 0)
2031 return Crypto_ClientError(data_out, len_out, "Server tried an unauthenticated connection even though a host key is present");
2032 if(crypto_aeslevel.integer >= 3)
2033 return Crypto_ClientError(data_out, len_out, "This server requires encryption to be not required (crypto_aeslevel <= 2)");
2035 return CRYPTO_NOMATCH;
2037 else if (len_in >= 1 && string[0] == 'j' && cls.connect_trying && d0_rijndael_dll && crypto_aeslevel.integer >= 3)
2039 int wantserverid = -1;
2040 Crypto_RetrieveHostKey(&cls.connect_address, &wantserverid, NULL, 0, NULL, 0, NULL);
2041 if(!crypto || !crypto->authenticated)
2043 if(wantserverid >= 0)
2044 return Crypto_ClientError(data_out, len_out, "Server tried an unauthenticated connection even though a host key is present");
2045 if(crypto_aeslevel.integer >= 3)
2046 return Crypto_ClientError(data_out, len_out, "This server requires encryption to be not required (crypto_aeslevel <= 2)");
2048 return CRYPTO_NOMATCH;
2050 else if (len_in >= 13 && !memcmp(string, "infoResponse\x0A", 13))
2052 s = InfoString_GetValue(string + 13, "d0_blind_id", infostringvalue, sizeof(infostringvalue));
2054 Crypto_StoreHostKey(peeraddress, s, true);
2055 return CRYPTO_NOMATCH;
2057 else if (len_in >= 15 && !memcmp(string, "statusResponse\x0A", 15))
2061 p = strchr(string + 15, '\n');
2065 * (char *) p = 0; // cut off the string there
2067 s = InfoString_GetValue(string + 15, "d0_blind_id", infostringvalue, sizeof(infostringvalue));
2069 Crypto_StoreHostKey(peeraddress, s, true);
2072 * (char *) p = save;
2073 // invoking those nasal demons again (do not run this on the DS9k)
2075 return CRYPTO_NOMATCH;
2077 else if(len_in > 10 && !memcmp(string, "challenge ", 10) && cls.connect_trying)
2079 const char *vlen_blind_id_ptr = NULL;
2080 size_t len_blind_id_ptr = 0;
2082 const char *challenge = data_in + 10;
2085 int clientid = -1, serverid = -1, wantserverid = -1;
2086 qboolean server_can_auth = true;
2087 char wantserver_idfp[FP64_SIZE+1];
2088 int wantserver_aeslevel;
2090 // if we have a stored host key for the server, assume serverid to already be selected!
2091 // (the loop will refuse to overwrite this one then)
2092 wantserver_idfp[0] = 0;
2093 Crypto_RetrieveHostKey(&cls.connect_address, &wantserverid, NULL, 0, wantserver_idfp, sizeof(wantserver_idfp), &wantserver_aeslevel);
2094 // requirement: wantserver_idfp is a full ID if wantserverid set
2096 // if we leave, we have to consider the connection
2097 // unauthenticated; NOTE: this may be faked by a clever
2098 // attacker to force an unauthenticated connection; so we have
2099 // a safeguard check in place when encryption is required too
2100 // in place, or when authentication is required by the server
2101 crypto->authenticated = false;
2103 GetUntilNul(&data_in, &len_in);
2105 return (wantserverid >= 0) ? Crypto_ClientError(data_out, len_out, "Server tried an unauthenticated connection even though a host key is present") :
2106 (d0_rijndael_dll && crypto_aeslevel.integer >= 3) ? Crypto_ServerError(data_out, len_out, "This server requires encryption to be not required (crypto_aeslevel <= 2)", NULL) :
2109 // FTEQW extension protocol
2112 k = Crypto_LittleLong(data_in);
2113 v = Crypto_LittleLong(data_in + 4);
2121 k = Crypto_LittleLong(data_in);
2126 case PROTOCOL_D0_BLIND_ID:
2127 vlen_blind_id_ptr = data_in;
2128 len_blind_id_ptr = v;
2140 if(!vlen_blind_id_ptr)
2141 return (wantserverid >= 0) ? Crypto_ClientError(data_out, len_out, "Server tried an unauthenticated connection even though authentication is required") :
2142 (d0_rijndael_dll && crypto_aeslevel.integer >= 3) ? Crypto_ServerError(data_out, len_out, "This server requires encryption to be not required (crypto_aeslevel <= 2)", NULL) :
2145 data_in = vlen_blind_id_ptr;
2146 len_in = len_blind_id_ptr;
2148 // parse fingerprints
2149 // once we found a fingerprint we can auth to (ANY), select it as clientfp
2150 // once we found a fingerprint in the first list that we know, select it as serverfp
2154 p = GetUntilNul(&data_in, &len_in);
2159 if(!server_can_auth)
2160 break; // other protocol message may follow
2161 server_can_auth = false;
2166 for(i = 0; i < MAX_PUBKEYS; ++i)
2169 if(!strcmp(p, pubkeys_fp64[i]))
2171 if(pubkeys_havepriv[i])
2176 if(wantserverid < 0 || i == wantserverid)
2180 if(clientid >= 0 && serverid >= 0)
2184 // if stored host key is not found:
2185 if(wantserverid >= 0 && serverid < 0)
2186 return Crypto_ClientError(data_out, len_out, "Server CA does not match stored host key, refusing to connect");
2188 if(serverid >= 0 || clientid >= 0)
2190 // TODO at this point, fill clientside crypto struct!
2192 CDATA->cdata_id = ++cdata_id;
2193 CDATA->s = serverid;
2194 CDATA->c = clientid;
2195 memset(crypto->dhkey, 0, sizeof(crypto->dhkey));
2196 strlcpy(CDATA->challenge, challenge, sizeof(CDATA->challenge));
2197 crypto->client_keyfp[0] = 0;
2198 crypto->client_idfp[0] = 0;
2199 crypto->server_keyfp[0] = 0;
2200 crypto->server_idfp[0] = 0;
2201 memcpy(CDATA->wantserver_idfp, wantserver_idfp, sizeof(crypto->server_idfp));
2203 if(CDATA->wantserver_idfp[0]) // if we know a host key, honor its encryption setting
2204 switch(bound(0, d0_rijndael_dll ? crypto_aeslevel.integer : 0, 3))
2206 default: // dummy, never happens, but to make gcc happy...
2208 if(wantserver_aeslevel >= 3)
2209 return Crypto_ServerError(data_out, len_out, "This server requires encryption to be not required (crypto_aeslevel <= 2)", NULL);
2210 CDATA->wantserver_aes = false;
2213 CDATA->wantserver_aes = (wantserver_aeslevel >= 2);
2216 CDATA->wantserver_aes = (wantserver_aeslevel >= 1);
2219 if(wantserver_aeslevel <= 0)
2220 return Crypto_ServerError(data_out, len_out, "This server requires encryption to be supported (crypto_aeslevel >= 1, and d0_rijndael library must be present)", NULL);
2221 CDATA->wantserver_aes = true;
2225 // build outgoing message
2226 // append regular stuff
2227 PutWithNul(&data_out_p, len_out, va(vabuf, sizeof(vabuf), "d0pk\\cnt\\0\\id\\%d\\aeslevel\\%d\\challenge\\%s", CDATA->cdata_id, d0_rijndael_dll ? crypto_aeslevel.integer : 0, challenge));
2228 PutWithNul(&data_out_p, len_out, serverid >= 0 ? pubkeys_fp64[serverid] : "");
2229 PutWithNul(&data_out_p, len_out, clientid >= 0 ? pubkeys_fp64[clientid] : "");
2233 // I am the client, and my key is ok... so let's set client_keyfp and client_idfp
2234 strlcpy(crypto->client_keyfp, pubkeys_fp64[CDATA->c], sizeof(crypto->client_keyfp));
2235 strlcpy(crypto->client_idfp, pubkeys_priv_fp64[CDATA->c], sizeof(crypto->client_idfp));
2241 CDATA->id = qd0_blind_id_new();
2245 return Crypto_ClientError(data_out, len_out, "d0_blind_id_new failed");
2247 if(!qd0_blind_id_copy(CDATA->id, pubkeys[CDATA->s]))
2250 return Crypto_ClientError(data_out, len_out, "d0_blind_id_copy failed");
2252 CDATA->next_step = 1;
2253 *len_out = data_out_p - data_out;
2255 else if(clientid >= 0)
2257 // skip over server auth, perform client auth only
2259 CDATA->id = qd0_blind_id_new();
2263 return Crypto_ClientError(data_out, len_out, "d0_blind_id_new failed");
2265 if(!qd0_blind_id_copy(CDATA->id, pubkeys[CDATA->c]))
2268 return Crypto_ClientError(data_out, len_out, "d0_blind_id_copy failed");
2270 if(!qd0_blind_id_authenticate_with_private_id_start(CDATA->id, true, false, "XONOTIC", 8, data_out_p, len_out)) // len_out receives used size by this op
2273 return Crypto_ClientError(data_out, len_out, "d0_blind_id_authenticate_with_private_id_start failed");
2275 CDATA->next_step = 5;
2276 data_out_p += *len_out;
2277 *len_out = data_out_p - data_out;
2280 *len_out = data_out_p - data_out;
2282 return CRYPTO_DISCARD;
2286 if(wantserver_idfp[0]) // if we know a host key, honor its encryption setting
2287 if(wantserver_aeslevel >= 3)
2288 return Crypto_ClientError(data_out, len_out, "Server insists on encryption, but neither can authenticate to the other");
2289 return (d0_rijndael_dll && crypto_aeslevel.integer >= 3) ? Crypto_ServerError(data_out, len_out, "This server requires encryption to be not required (crypto_aeslevel <= 2)", NULL) :
2293 else if(len_in > 5 && !memcmp(string, "d0pk\\", 5) && cls.connect_trying)
2297 cnt = InfoString_GetValue(string + 4, "id", infostringvalue, sizeof(infostringvalue));
2298 id = (cnt ? atoi(cnt) : -1);
2299 cnt = InfoString_GetValue(string + 4, "cnt", infostringvalue, sizeof(infostringvalue));
2301 return Crypto_ClientError(data_out, len_out, "d0pk\\ message without cnt");
2302 GetUntilNul(&data_in, &len_in);
2304 return Crypto_ClientError(data_out, len_out, "d0pk\\ message without attachment");
2306 if(!strcmp(cnt, "1"))
2309 if(CDATA->cdata_id != id)
2310 return Crypto_SoftServerError(data_out, len_out, va(vabuf, sizeof(vabuf), "Got d0pk\\id\\%d when expecting %d", id, CDATA->cdata_id));
2311 if(CDATA->next_step != 1)
2312 return Crypto_SoftClientError(data_out, len_out, va(vabuf, sizeof(vabuf), "Got d0pk\\cnt\\%s when expecting %d", cnt, CDATA->next_step));
2314 cls.connect_nextsendtime = max(cls.connect_nextsendtime, realtime + 1); // prevent "hammering"
2316 if((s = InfoString_GetValue(string + 4, "aes", infostringvalue, sizeof(infostringvalue))))
2320 // we CANNOT toggle the AES status any more!
2321 // as the server already decided
2322 if(CDATA->wantserver_idfp[0]) // if we know a host key, honor its encryption setting
2323 if(!aes && CDATA->wantserver_aes)
2326 return Crypto_ClientError(data_out, len_out, "Stored host key requires encryption, but server did not enable encryption");
2328 if(aes && (!d0_rijndael_dll || crypto_aeslevel.integer <= 0))
2331 return Crypto_ClientError(data_out, len_out, "Server insists on encryption too hard");
2333 if(!aes && (d0_rijndael_dll && crypto_aeslevel.integer >= 3))
2336 return Crypto_ClientError(data_out, len_out, "Server insists on plaintext too hard");
2338 crypto->use_aes = aes != 0;
2340 PutWithNul(&data_out_p, len_out, va(vabuf, sizeof(vabuf), "d0pk\\cnt\\2\\id\\%d", CDATA->cdata_id));
2341 if(!qd0_blind_id_authenticate_with_private_id_challenge(CDATA->id, true, false, data_in, len_in, data_out_p, len_out, &status))
2344 return Crypto_ClientError(data_out, len_out, "d0_blind_id_authenticate_with_private_id_challenge failed");
2346 CDATA->next_step = 3;
2347 data_out_p += *len_out;
2348 *len_out = data_out_p - data_out;
2349 return CRYPTO_DISCARD;
2351 else if(!strcmp(cnt, "3"))
2353 static char msgbuf[32];
2354 size_t msgbuflen = sizeof(msgbuf);
2358 if(CDATA->cdata_id != id)
2359 return Crypto_SoftServerError(data_out, len_out, va(vabuf, sizeof(vabuf), "Got d0pk\\id\\%d when expecting %d", id, CDATA->cdata_id));
2360 if(CDATA->next_step != 3)
2361 return Crypto_SoftClientError(data_out, len_out, va(vabuf, sizeof(vabuf), "Got d0pk\\cnt\\%s when expecting %d", cnt, CDATA->next_step));
2363 cls.connect_nextsendtime = max(cls.connect_nextsendtime, realtime + 1); // prevent "hammering"
2365 if(!qd0_blind_id_authenticate_with_private_id_verify(CDATA->id, data_in, len_in, msgbuf, &msgbuflen, &status))
2368 return Crypto_ClientError(data_out, len_out, "d0_blind_id_authenticate_with_private_id_verify failed (server authentication error)");
2371 strlcpy(crypto->server_keyfp, pubkeys_fp64[CDATA->s], sizeof(crypto->server_keyfp));
2373 crypto->server_keyfp[0] = 0;
2374 memset(crypto->server_idfp, 0, sizeof(crypto->server_idfp));
2375 fpbuflen = FP64_SIZE;
2376 if(!qd0_blind_id_fingerprint64_public_id(CDATA->id, crypto->server_idfp, &fpbuflen))
2379 return Crypto_ClientError(data_out, len_out, "d0_blind_id_fingerprint64_public_id failed");
2381 if(CDATA->wantserver_idfp[0])
2382 if(memcmp(CDATA->wantserver_idfp, crypto->server_idfp, sizeof(crypto->server_idfp)))
2385 return Crypto_ClientError(data_out, len_out, "Server ID does not match stored host key, refusing to connect");
2387 fpbuflen = DHKEY_SIZE;
2388 if(!qd0_blind_id_sessionkey_public_id(CDATA->id, (char *) crypto->dhkey, &fpbuflen))
2391 return Crypto_ClientError(data_out, len_out, "d0_blind_id_sessionkey_public_id failed");
2394 // cache the server key
2395 Crypto_StoreHostKey(&cls.connect_address, va(vabuf, sizeof(vabuf), "%d %s@%s", crypto->use_aes ? 1 : 0, crypto->server_idfp, pubkeys_fp64[CDATA->s]), false);
2399 // client will auth next
2400 PutWithNul(&data_out_p, len_out, va(vabuf, sizeof(vabuf), "d0pk\\cnt\\4\\id\\%d", CDATA->cdata_id));
2401 if(!qd0_blind_id_copy(CDATA->id, pubkeys[CDATA->c]))
2404 return Crypto_ClientError(data_out, len_out, "d0_blind_id_copy failed");
2406 if(!qd0_blind_id_authenticate_with_private_id_start(CDATA->id, true, false, "XONOTIC", 8, data_out_p, len_out)) // len_out receives used size by this op
2409 return Crypto_ClientError(data_out, len_out, "d0_blind_id_authenticate_with_private_id_start failed");
2411 CDATA->next_step = 5;
2412 data_out_p += *len_out;
2413 *len_out = data_out_p - data_out;
2414 return CRYPTO_DISCARD;
2418 // session key is FINISHED (no server part is to be expected)! By this, all keys are set up
2419 crypto->authenticated = true;
2420 CDATA->next_step = 0;
2421 // assume we got the empty challenge to finish the protocol
2422 PutWithNul(&data_out_p, len_out, "challenge ");
2423 *len_out = data_out_p - data_out;
2424 --*len_out; // remove NUL terminator
2425 return CRYPTO_REPLACE;
2428 else if(!strcmp(cnt, "5"))
2431 unsigned char dhkey[DHKEY_SIZE];
2435 if(CDATA->cdata_id != id)
2436 return Crypto_SoftServerError(data_out, len_out, va(vabuf, sizeof(vabuf), "Got d0pk\\id\\%d when expecting %d", id, CDATA->cdata_id));
2437 if(CDATA->next_step != 5)
2438 return Crypto_SoftClientError(data_out, len_out, va(vabuf, sizeof(vabuf), "Got d0pk\\cnt\\%s when expecting %d", cnt, CDATA->next_step));
2440 cls.connect_nextsendtime = max(cls.connect_nextsendtime, realtime + 1); // prevent "hammering"
2442 if(CDATA->s < 0) // only if server didn't auth
2444 if((s = InfoString_GetValue(string + 4, "aes", infostringvalue, sizeof(infostringvalue))))
2448 if(CDATA->wantserver_idfp[0]) // if we know a host key, honor its encryption setting
2449 if(!aes && CDATA->wantserver_aes)
2452 return Crypto_ClientError(data_out, len_out, "Stored host key requires encryption, but server did not enable encryption");
2454 if(aes && (!d0_rijndael_dll || crypto_aeslevel.integer <= 0))
2457 return Crypto_ClientError(data_out, len_out, "Server insists on encryption too hard");
2459 if(!aes && (d0_rijndael_dll && crypto_aeslevel.integer >= 3))
2462 return Crypto_ClientError(data_out, len_out, "Server insists on plaintext too hard");
2464 crypto->use_aes = aes != 0;
2467 PutWithNul(&data_out_p, len_out, va(vabuf, sizeof(vabuf), "d0pk\\cnt\\6\\id\\%d", CDATA->cdata_id));
2468 if(!qd0_blind_id_authenticate_with_private_id_response(CDATA->id, data_in, len_in, data_out_p, len_out))
2471 return Crypto_ClientError(data_out, len_out, "d0_blind_id_authenticate_with_private_id_response failed");
2473 fpbuflen = DHKEY_SIZE;
2474 if(!qd0_blind_id_sessionkey_public_id(CDATA->id, (char *) dhkey, &fpbuflen))
2477 return Crypto_ClientError(data_out, len_out, "d0_blind_id_sessionkey_public_id failed");
2479 // XOR the two DH keys together to make one
2480 for(i = 0; i < DHKEY_SIZE; ++i)
2481 crypto->dhkey[i] ^= dhkey[i];
2482 // session key is FINISHED! By this, all keys are set up
2483 crypto->authenticated = true;
2484 CDATA->next_step = 0;
2485 data_out_p += *len_out;
2486 *len_out = data_out_p - data_out;
2487 return CRYPTO_DISCARD;
2489 return Crypto_SoftClientError(data_out, len_out, "Got unknown d0_blind_id message from server");
2492 return CRYPTO_NOMATCH;
2495 size_t Crypto_SignData(const void *data, size_t datasize, int keyid, void *signed_data, size_t signed_size)
2497 if(keyid < 0 || keyid >= MAX_PUBKEYS)
2499 if(!pubkeys_havepriv[keyid])
2501 if(qd0_blind_id_sign_with_private_id_sign(pubkeys[keyid], true, false, (const char *)data, datasize, (char *)signed_data, &signed_size))
2506 size_t Crypto_SignDataDetached(const void *data, size_t datasize, int keyid, void *signed_data, size_t signed_size)
2508 if(keyid < 0 || keyid >= MAX_PUBKEYS)
2510 if(!pubkeys_havepriv[keyid])
2512 if(qd0_blind_id_sign_with_private_id_sign_detached(pubkeys[keyid], true, false, (const char *)data, datasize, (char *)signed_data, &signed_size))