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)
365 f = FS_SysOpen(va(vabuf, sizeof(vabuf), "%s%s", fs_userdir, 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 static void Crypto_LoadKeys(void)
776 // note: we are just a CLIENT
778 // PUBLIC KEYS to accept (including modulus)
779 // PRIVATE KEY of user
781 crypto_idstring = NULL;
782 dpsnprintf(crypto_idstring_buf, sizeof(crypto_idstring_buf), "%d", d0_rijndael_dll ? crypto_aeslevel.integer : 0);
783 for(i = 0; i < MAX_PUBKEYS; ++i)
785 memset(pubkeys_fp64[i], 0, sizeof(pubkeys_fp64[i]));
786 memset(pubkeys_priv_fp64[i], 0, sizeof(pubkeys_fp64[i]));
787 pubkeys_havepriv[i] = false;
788 len = Crypto_LoadFile(va(vabuf, sizeof(vabuf), "key_%d.d0pk", i), buf, sizeof(buf));
789 if((pubkeys[i] = Crypto_ReadPublicKey(buf, len)))
792 if(qd0_blind_id_fingerprint64_public_key(pubkeys[i], pubkeys_fp64[i], &len2)) // keeps final NUL
794 Con_Printf("Loaded public key key_%d.d0pk (fingerprint: %s)\n", i, pubkeys_fp64[i]);
795 len = Crypto_LoadFile(va(vabuf, sizeof(vabuf), "key_%d.d0si", i), buf, sizeof(buf));
798 if(Crypto_AddPrivateKey(pubkeys[i], buf, len))
801 if(qd0_blind_id_fingerprint64_public_id(pubkeys[i], pubkeys_priv_fp64[i], &len2)) // keeps final NUL
803 Con_Printf("Loaded private ID key_%d.d0si for key_%d.d0pk (public key fingerprint: %s)\n", i, i, pubkeys_priv_fp64[i]);
804 pubkeys_havepriv[i] = true;
805 strlcat(crypto_idstring_buf, va(vabuf, sizeof(vabuf), " %s@%s", pubkeys_priv_fp64[i], pubkeys_fp64[i]), sizeof(crypto_idstring_buf));
809 // can't really happen
810 // but nothing leaked here
817 // can't really happen
818 qd0_blind_id_free(pubkeys[i]);
823 crypto_idstring = crypto_idstring_buf;
826 Crypto_BuildChallengeAppend();
828 // find a good prefix length for all the keys we know (yes, algorithm is not perfect yet, may yield too long prefix length)
829 crypto_keyfp_recommended_length = 0;
830 memset(buf+256, 0, MAX_PUBKEYS + MAX_PUBKEYS);
831 while(crypto_keyfp_recommended_length < FP64_SIZE)
834 for(i = 0; i < MAX_PUBKEYS; ++i)
838 ++buf[(unsigned char) pubkeys_fp64[i][crypto_keyfp_recommended_length]];
839 if(pubkeys_havepriv[i])
840 if(!buf[256 + MAX_PUBKEYS + i])
841 ++buf[(unsigned char) pubkeys_priv_fp64[i][crypto_keyfp_recommended_length]];
843 for(i = 0; i < MAX_PUBKEYS; ++i)
847 if(buf[(unsigned char) pubkeys_fp64[i][crypto_keyfp_recommended_length]] < 2)
849 if(pubkeys_havepriv[i])
850 if(!buf[256 + MAX_PUBKEYS + i])
851 if(buf[(unsigned char) pubkeys_priv_fp64[i][crypto_keyfp_recommended_length]] < 2)
852 buf[256 + MAX_PUBKEYS + i] = 1;
854 ++crypto_keyfp_recommended_length;
855 for(i = 0; i < MAX_PUBKEYS; ++i)
860 if(pubkeys_havepriv[i])
861 if(!buf[256 + MAX_PUBKEYS + i])
867 if(crypto_keyfp_recommended_length < 7)
868 crypto_keyfp_recommended_length = 7;
871 static void Crypto_UnloadKeys(void)
875 for(i = 0; i < MAX_PUBKEYS; ++i)
878 qd0_blind_id_free(pubkeys[i]);
880 pubkeys_havepriv[i] = false;
881 memset(pubkeys_fp64[i], 0, sizeof(pubkeys_fp64[i]));
882 memset(pubkeys_priv_fp64[i], 0, sizeof(pubkeys_fp64[i]));
883 challenge_append_length = 0;
885 crypto_idstring = NULL;
889 static mempool_t *cryptomempool;
895 static void *d0_malloc(size_t len)
897 return Mem_Alloc(cryptomempool, len);
900 static void d0_free(void *p)
905 static void *d0_createmutex(void)
907 return Thread_CreateMutex();
910 static void d0_destroymutex(void *m)
912 Thread_DestroyMutex(m);
915 static int d0_lockmutex(void *m)
917 return Thread_LockMutex(m);
920 static int d0_unlockmutex(void *m)
922 return Thread_UnlockMutex(m);
928 void Crypto_Shutdown(void)
934 Thread_DestroyMutex(crypto_mutex);
937 Crypto_Rijndael_CloseLibrary();
942 for(i = 0; i < MAX_CRYPTOCONNECTS; ++i)
944 crypto = &cryptoconnects[i].crypto;
947 memset(cryptoconnects, 0, sizeof(cryptoconnects));
948 crypto = &cls.crypto;
953 qd0_blind_id_SHUTDOWN();
955 Crypto_CloseLibrary();
958 Mem_FreePool(&cryptomempool);
961 void Crypto_Init(void)
963 cryptomempool = Mem_AllocPool("crypto", 0, NULL);
965 if(!Crypto_OpenLibrary())
968 qd0_blind_id_setmallocfuncs(d0_malloc, d0_free);
969 if (Thread_HasThreads())
971 crypto_mutex = Thread_CreateMutex();
972 qd0_blind_id_setmutexfuncs(d0_createmutex, d0_destroymutex, d0_lockmutex, d0_unlockmutex);
975 if(!qd0_blind_id_INITIALIZE())
977 Crypto_Rijndael_CloseLibrary();
978 Crypto_CloseLibrary();
979 Con_Printf("libd0_blind_id initialization FAILED, cryptography support has been disabled\n");
983 Crypto_Rijndael_OpenLibrary(); // if this fails, it's uncritical
985 Crypto_InitHostKeys();
990 qboolean Crypto_Available(void)
998 static void Crypto_KeyGen_Finished(int code, size_t length_received, unsigned char *buffer, void *cbdata)
1002 static char buf[8192];
1003 static char buf2[8192];
1004 size_t bufsize, buf2size;
1006 d0_blind_id_t *ctx, *ctx2;
1011 if (crypto_mutex) Thread_LockMutex(crypto_mutex);
1013 if(!d0_blind_id_dll)
1015 Con_Print("libd0_blind_id DLL not found, this command is inactive.\n");
1017 if (crypto_mutex) Thread_UnlockMutex(crypto_mutex);
1021 if(keygen_i >= MAX_PUBKEYS || !pubkeys[keygen_i])
1023 Con_Printf("overflow of keygen_i\n");
1025 if (crypto_mutex) Thread_UnlockMutex(crypto_mutex);
1030 Con_Printf("Unexpected response from keygen server:\n");
1031 Com_HexDumpToConsole(buffer, length_received);
1032 if (crypto_mutex) Thread_UnlockMutex(crypto_mutex);
1035 if(!Crypto_ParsePack((const char *) buffer, length_received, FOURCC_D0IR, p, l, 1))
1037 if(length_received >= 5 && Crypto_LittleLong((const char *) buffer) == FOURCC_D0ER)
1039 Con_Printf("Error response from keygen server: %.*s\n", (int)(length_received - 5), buffer + 5);
1043 Con_Printf("Invalid response from keygen server:\n");
1044 Com_HexDumpToConsole(buffer, length_received);
1047 if (crypto_mutex) Thread_UnlockMutex(crypto_mutex);
1050 if(!qd0_blind_id_finish_private_id_request(pubkeys[keygen_i], p[0], l[0]))
1052 Con_Printf("d0_blind_id_finish_private_id_request failed\n");
1054 if (crypto_mutex) Thread_UnlockMutex(crypto_mutex);
1058 // verify the key we just got (just in case)
1059 ctx = qd0_blind_id_new();
1062 Con_Printf("d0_blind_id_new failed\n");
1064 if (crypto_mutex) Thread_UnlockMutex(crypto_mutex);
1067 ctx2 = qd0_blind_id_new();
1070 Con_Printf("d0_blind_id_new failed\n");
1071 qd0_blind_id_free(ctx);
1073 if (crypto_mutex) Thread_UnlockMutex(crypto_mutex);
1076 if(!qd0_blind_id_copy(ctx, pubkeys[keygen_i]))
1078 Con_Printf("d0_blind_id_copy failed\n");
1079 qd0_blind_id_free(ctx);
1080 qd0_blind_id_free(ctx2);
1082 if (crypto_mutex) Thread_UnlockMutex(crypto_mutex);
1085 if(!qd0_blind_id_copy(ctx2, pubkeys[keygen_i]))
1087 Con_Printf("d0_blind_id_copy failed\n");
1088 qd0_blind_id_free(ctx);
1089 qd0_blind_id_free(ctx2);
1091 if (crypto_mutex) Thread_UnlockMutex(crypto_mutex);
1094 bufsize = sizeof(buf);
1095 if(!qd0_blind_id_authenticate_with_private_id_start(ctx, 1, 1, "hello world", 11, buf, &bufsize))
1097 Con_Printf("d0_blind_id_authenticate_with_private_id_start failed\n");
1098 qd0_blind_id_free(ctx);
1099 qd0_blind_id_free(ctx2);
1101 if (crypto_mutex) Thread_UnlockMutex(crypto_mutex);
1104 buf2size = sizeof(buf2);
1105 if(!qd0_blind_id_authenticate_with_private_id_challenge(ctx2, 1, 1, buf, bufsize, buf2, &buf2size, &status) || !status)
1107 Con_Printf("d0_blind_id_authenticate_with_private_id_challenge failed (server does not have the requested private key)\n");
1108 qd0_blind_id_free(ctx);
1109 qd0_blind_id_free(ctx2);
1111 if (crypto_mutex) Thread_UnlockMutex(crypto_mutex);
1114 bufsize = sizeof(buf);
1115 if(!qd0_blind_id_authenticate_with_private_id_response(ctx, buf2, buf2size, buf, &bufsize))
1117 Con_Printf("d0_blind_id_authenticate_with_private_id_response failed\n");
1118 qd0_blind_id_free(ctx);
1119 qd0_blind_id_free(ctx2);
1121 if (crypto_mutex) Thread_UnlockMutex(crypto_mutex);
1124 buf2size = sizeof(buf2);
1125 if(!qd0_blind_id_authenticate_with_private_id_verify(ctx2, buf, bufsize, buf2, &buf2size, &status) || !status)
1127 Con_Printf("d0_blind_id_authenticate_with_private_id_verify failed (server does not have the requested private key)\n");
1128 qd0_blind_id_free(ctx);
1129 qd0_blind_id_free(ctx2);
1131 if (crypto_mutex) Thread_UnlockMutex(crypto_mutex);
1134 qd0_blind_id_free(ctx);
1135 qd0_blind_id_free(ctx2);
1137 // we have a valid key now!
1138 // make the rest of crypto.c know that
1140 if(qd0_blind_id_fingerprint64_public_id(pubkeys[keygen_i], pubkeys_priv_fp64[keygen_i], &len2)) // keeps final NUL
1142 Con_Printf("Received private ID key_%d.d0pk (public key fingerprint: %s)\n", keygen_i, pubkeys_priv_fp64[keygen_i]);
1143 pubkeys_havepriv[keygen_i] = true;
1144 strlcat(crypto_idstring_buf, va(vabuf, sizeof(vabuf), " %s@%s", pubkeys_priv_fp64[keygen_i], pubkeys_fp64[keygen_i]), sizeof(crypto_idstring_buf));
1145 crypto_idstring = crypto_idstring_buf;
1146 Crypto_BuildChallengeAppend();
1148 // write the key to disk
1151 if(!qd0_blind_id_write_private_id(pubkeys[keygen_i], buf, &l[0]))
1153 Con_Printf("d0_blind_id_write_private_id failed\n");
1155 if (crypto_mutex) Thread_UnlockMutex(crypto_mutex);
1158 if(!(buf2size = Crypto_UnParsePack(buf2, sizeof(buf2), FOURCC_D0SI, p, l, 1)))
1160 Con_Printf("Crypto_UnParsePack failed\n");
1162 if (crypto_mutex) Thread_UnlockMutex(crypto_mutex);
1168 FS_CreatePath(va(vabuf, sizeof(vabuf), "%skey_%d.d0si", fs_userdir, keygen_i));
1169 f = FS_SysOpen(va(vabuf, sizeof(vabuf), "%skey_%d.d0si", fs_userdir, keygen_i), "wb", false);
1173 FS_CreatePath(va(vabuf, sizeof(vabuf), "%skey_%d.d0si", fs_basedir, keygen_i));
1174 f = FS_SysOpen(va(vabuf, sizeof(vabuf), "%skey_%d.d0si", fs_basedir, keygen_i), "wb", false);
1178 Con_Printf("Cannot open key_%d.d0si\n", keygen_i);
1180 if (crypto_mutex) Thread_UnlockMutex(crypto_mutex);
1183 FS_Write(f, buf2, buf2size);
1186 Con_Printf("Saved to key_%d.d0si\n", keygen_i);
1188 if (crypto_mutex) Thread_UnlockMutex(crypto_mutex);
1191 static void Crypto_KeyGen_f(void)
1196 static char buf[8192];
1197 static char buf2[8192];
1198 size_t buf2l, buf2pos;
1199 if(!d0_blind_id_dll)
1201 Con_Print("libd0_blind_id DLL not found, this command is inactive.\n");
1206 Con_Printf("usage:\n%s id url\n", Cmd_Argv(0));
1209 if (crypto_mutex) Thread_LockMutex(crypto_mutex);
1210 i = atoi(Cmd_Argv(1));
1213 Con_Printf("there is no public key %d\n", i);
1214 if (crypto_mutex) Thread_UnlockMutex(crypto_mutex);
1217 if(pubkeys_havepriv[i])
1219 Con_Printf("there is already a private key for %d\n", i);
1220 if (crypto_mutex) Thread_UnlockMutex(crypto_mutex);
1225 Con_Printf("there is already a keygen run on the way\n");
1226 if (crypto_mutex) Thread_UnlockMutex(crypto_mutex);
1230 if(!qd0_blind_id_generate_private_id_start(pubkeys[keygen_i]))
1232 Con_Printf("d0_blind_id_start failed\n");
1234 if (crypto_mutex) Thread_UnlockMutex(crypto_mutex);
1239 if(!qd0_blind_id_generate_private_id_request(pubkeys[keygen_i], buf, &l[0]))
1241 Con_Printf("d0_blind_id_generate_private_id_request failed\n");
1243 if (crypto_mutex) Thread_UnlockMutex(crypto_mutex);
1246 buf2pos = strlen(Cmd_Argv(2));
1247 memcpy(buf2, Cmd_Argv(2), buf2pos);
1248 if(!(buf2l = Crypto_UnParsePack(buf2 + buf2pos, sizeof(buf2) - buf2pos - 1, FOURCC_D0IQ, p, l, 1)))
1250 Con_Printf("Crypto_UnParsePack failed\n");
1252 if (crypto_mutex) Thread_UnlockMutex(crypto_mutex);
1255 if(!(buf2l = base64_encode((unsigned char *) (buf2 + buf2pos), buf2l, sizeof(buf2) - buf2pos - 1)))
1257 Con_Printf("base64_encode failed\n");
1259 if (crypto_mutex) Thread_UnlockMutex(crypto_mutex);
1264 if(!Curl_Begin_ToMemory(buf2, 0, (unsigned char *) keygen_buf, sizeof(keygen_buf), Crypto_KeyGen_Finished, NULL))
1266 Con_Printf("curl failed\n");
1268 if (crypto_mutex) Thread_UnlockMutex(crypto_mutex);
1271 Con_Printf("key generation in progress\n");
1272 if (crypto_mutex) Thread_UnlockMutex(crypto_mutex);
1277 static void Crypto_Reload_f(void)
1279 Crypto_ClearHostKeys();
1280 Crypto_UnloadKeys();
1284 static void Crypto_Keys_f(void)
1287 if(!d0_blind_id_dll)
1289 Con_Print("libd0_blind_id DLL not found, this command is inactive.\n");
1292 for(i = 0; i < MAX_PUBKEYS; ++i)
1296 Con_Printf("%2d: public key key_%d.d0pk (fingerprint: %s)\n", i, i, pubkeys_fp64[i]);
1297 if(pubkeys_havepriv[i])
1298 Con_Printf(" private ID key_%d.d0si (public key fingerprint: %s)\n", i, pubkeys_priv_fp64[i]);
1303 static void Crypto_HostKeys_f(void)
1306 crypto_storedhostkey_t *hk;
1309 if(!d0_blind_id_dll)
1311 Con_Print("libd0_blind_id DLL not found, this command is inactive.\n");
1314 for(i = 0; i < CRYPTO_HOSTKEY_HASHSIZE; ++i)
1316 for(hk = crypto_storedhostkey_hashtable[i]; hk; hk = hk->next)
1318 LHNETADDRESS_ToString(&hk->addr, buf, sizeof(buf), 1);
1319 Con_Printf("%d %s@%.*s %s\n",
1322 crypto_keyfp_recommended_length, pubkeys_fp64[hk->keyid],
1328 static void Crypto_HostKey_Clear_f(void)
1330 lhnetaddress_t addr;
1333 if(!d0_blind_id_dll)
1335 Con_Print("libd0_blind_id DLL not found, this command is inactive.\n");
1339 for(i = 1; i < Cmd_Argc(); ++i)
1341 LHNETADDRESS_FromString(&addr, Cmd_Argv(i), 26000);
1342 if(Crypto_ClearHostKey(&addr))
1344 Con_Printf("cleared host key for %s\n", Cmd_Argv(i));
1349 void Crypto_Init_Commands(void)
1353 Cmd_AddCommand("crypto_reload", Crypto_Reload_f, "reloads cryptographic keys");
1354 Cmd_AddCommand("crypto_keygen", Crypto_KeyGen_f, "generates and saves a cryptographic key");
1355 Cmd_AddCommand("crypto_keys", Crypto_Keys_f, "lists the loaded keys");
1356 Cmd_AddCommand("crypto_hostkeys", Crypto_HostKeys_f, "lists the cached host keys");
1357 Cmd_AddCommand("crypto_hostkey_clear", Crypto_HostKey_Clear_f, "clears a cached host key");
1358 Cvar_RegisterVariable(&crypto_developer);
1360 Cvar_RegisterVariable(&crypto_aeslevel);
1362 crypto_aeslevel.integer = 0; // make sure
1363 Cvar_RegisterVariable(&crypto_servercpupercent);
1364 Cvar_RegisterVariable(&crypto_servercpumaxtime);
1365 Cvar_RegisterVariable(&crypto_servercpudebug);
1371 static void aescpy(unsigned char *key, const unsigned char *iv, unsigned char *dst, const unsigned char *src, size_t len)
1373 const unsigned char *xorpos = iv;
1374 unsigned char xorbuf[16];
1375 unsigned long rk[D0_RIJNDAEL_RKLENGTH(DHKEY_SIZE * 8)];
1377 qd0_rijndael_setup_encrypt(rk, key, DHKEY_SIZE * 8);
1380 for(i = 0; i < 16; ++i)
1381 xorbuf[i] = src[i] ^ xorpos[i];
1382 qd0_rijndael_encrypt(rk, D0_RIJNDAEL_NROUNDS(DHKEY_SIZE * 8), xorbuf, dst);
1390 for(i = 0; i < len; ++i)
1391 xorbuf[i] = src[i] ^ xorpos[i];
1393 xorbuf[i] = xorpos[i];
1394 qd0_rijndael_encrypt(rk, D0_RIJNDAEL_NROUNDS(DHKEY_SIZE * 8), xorbuf, dst);
1397 static void seacpy(unsigned char *key, const unsigned char *iv, unsigned char *dst, const unsigned char *src, size_t len)
1399 const unsigned char *xorpos = iv;
1400 unsigned char xorbuf[16];
1401 unsigned long rk[D0_RIJNDAEL_RKLENGTH(DHKEY_SIZE * 8)];
1403 qd0_rijndael_setup_decrypt(rk, key, DHKEY_SIZE * 8);
1406 qd0_rijndael_decrypt(rk, D0_RIJNDAEL_NROUNDS(DHKEY_SIZE * 8), src, xorbuf);
1407 for(i = 0; i < 16; ++i)
1408 dst[i] = xorbuf[i] ^ xorpos[i];
1416 qd0_rijndael_decrypt(rk, D0_RIJNDAEL_NROUNDS(DHKEY_SIZE * 8), src, xorbuf);
1417 for(i = 0; i < len; ++i)
1418 dst[i] = xorbuf[i] ^ xorpos[i];
1422 // NOTE: we MUST avoid the following begins of the packet:
1423 // 1. 0xFF, 0xFF, 0xFF, 0xFF
1424 // 2. 0x80, 0x00, length/256, length%256
1425 // this luckily does NOT affect AES mode, where the first byte always is in the range from 0x00 to 0x0F
1426 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)
1428 unsigned char h[32];
1430 if(crypto->authenticated)
1434 // AES packet = 1 byte length overhead, 15 bytes from HMAC-SHA-256, data, 0..15 bytes padding
1435 // 15 bytes HMAC-SHA-256 (112bit) suffice as the attacker can't do more than forge a random-looking packet
1436 // HMAC is needed to not leak information about packet content
1437 if(developer_networking.integer)
1439 Con_Print("To be encrypted:\n");
1440 Com_HexDumpToConsole((const unsigned char *) data_src, len_src);
1442 if(len_src + 32 > len || !HMAC_SHA256_32BYTES(h, (const unsigned char *) data_src, len_src, crypto->dhkey, DHKEY_SIZE))
1444 Con_Printf("Crypto_EncryptPacket failed (not enough space: %d bytes in, %d bytes out)\n", (int) len_src, (int) len);
1447 *len_dst = ((len_src + 15) / 16) * 16 + 16; // add 16 for HMAC, then round to 16-size for AES
1448 ((unsigned char *) data_dst)[0] = *len_dst - len_src;
1449 memcpy(((unsigned char *) data_dst)+1, h, 15);
1450 aescpy(crypto->dhkey, (const unsigned char *) data_dst, ((unsigned char *) data_dst) + 16, (const unsigned char *) data_src, len_src);
1455 // HMAC packet = 16 bytes HMAC-SHA-256 (truncated to 128 bits), data
1456 if(len_src + 16 > len || !HMAC_SHA256_32BYTES(h, (const unsigned char *) data_src, len_src, crypto->dhkey, DHKEY_SIZE))
1458 Con_Printf("Crypto_EncryptPacket failed (not enough space: %d bytes in, %d bytes out)\n", (int) len_src, (int) len);
1461 *len_dst = len_src + 16;
1462 memcpy(data_dst, h, 16);
1463 memcpy(((unsigned char *) data_dst) + 16, (unsigned char *) data_src, len_src);
1465 // handle the "avoid" conditions:
1466 i = BuffBigLong((unsigned char *) data_dst);
1468 (i == (int)0xFFFFFFFF) // avoid QW control packet
1470 (i == (int)0x80000000 + (int)*len_dst) // avoid NQ control packet
1472 *(unsigned char *)data_dst ^= 0x80; // this will ALWAYS fix it
1483 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)
1485 unsigned char h[32];
1488 // silently handle non-crypto packets
1489 i = BuffBigLong((unsigned char *) data_src);
1491 (i == (int)0xFFFFFFFF) // avoid QW control packet
1493 (i == (int)0x80000000 + (int)len_src) // avoid NQ control packet
1497 if(crypto->authenticated)
1501 if(len_src < 16 || ((len_src - 16) % 16))
1503 Con_Printf("Crypto_DecryptPacket failed (not enough space: %d bytes in, %d bytes out)\n", (int) len_src, (int) len);
1506 *len_dst = len_src - ((unsigned char *) data_src)[0];
1507 if(len < *len_dst || *len_dst > len_src - 16)
1509 Con_Printf("Crypto_DecryptPacket failed (not enough space: %d bytes in, %d->%d bytes out)\n", (int) len_src, (int) *len_dst, (int) len);
1512 seacpy(crypto->dhkey, (unsigned char *) data_src, (unsigned char *) data_dst, ((const unsigned char *) data_src) + 16, *len_dst);
1514 if(!HMAC_SHA256_32BYTES(h, (const unsigned char *) data_dst, *len_dst, crypto->dhkey, DHKEY_SIZE))
1516 Con_Printf("HMAC fail\n");
1519 if(memcmp(((const unsigned char *) data_src)+1, h, 15)) // ignore first byte, used for length
1521 Con_Printf("HMAC mismatch\n");
1524 if(developer_networking.integer)
1526 Con_Print("Decrypted:\n");
1527 Com_HexDumpToConsole((const unsigned char *) data_dst, *len_dst);
1529 return data_dst; // no need to copy
1535 Con_Printf("Crypto_DecryptPacket failed (not enough space: %d bytes in, %d bytes out)\n", (int) len_src, (int) len);
1538 *len_dst = len_src - 16;
1541 Con_Printf("Crypto_DecryptPacket failed (not enough space: %d bytes in, %d->%d bytes out)\n", (int) len_src, (int) *len_dst, (int) len);
1544 //memcpy(data_dst, data_src + 16, *len_dst);
1545 if(!HMAC_SHA256_32BYTES(h, ((const unsigned char *) data_src) + 16, *len_dst, crypto->dhkey, DHKEY_SIZE))
1547 Con_Printf("HMAC fail\n");
1548 Com_HexDumpToConsole((const unsigned char *) data_src, len_src);
1552 if(memcmp((const unsigned char *) data_src, h, 16)) // ignore first byte, used for length
1554 // undo the "avoid conditions"
1556 (i == (int)0x7FFFFFFF) // avoided QW control packet
1558 (i == (int)0x00000000 + (int)len_src) // avoided NQ control packet
1561 // do the avoidance on the hash too
1563 if(memcmp((const unsigned char *) data_src, h, 16)) // ignore first byte, used for length
1565 Con_Printf("HMAC mismatch\n");
1566 Com_HexDumpToConsole((const unsigned char *) data_src, len_src);
1572 Con_Printf("HMAC mismatch\n");
1573 Com_HexDumpToConsole((const unsigned char *) data_src, len_src);
1577 return ((const unsigned char *) data_src) + 16; // no need to copy, so data_dst is not used
1588 const char *Crypto_GetInfoResponseDataString(void)
1590 crypto_idstring_buf[0] = '0' + crypto_aeslevel.integer;
1591 return crypto_idstring;
1595 qboolean Crypto_ServerAppendToChallenge(const char *data_in, size_t len_in, char *data_out, size_t *len_out, size_t maxlen_out)
1597 // cheap op, all is precomputed
1598 if(!d0_blind_id_dll)
1599 return false; // no support
1601 if(maxlen_out <= *len_out + challenge_append_length)
1603 memcpy(data_out + *len_out, challenge_append, challenge_append_length);
1604 *len_out += challenge_append_length;
1608 static int Crypto_ServerError(char *data_out, size_t *len_out, const char *msg, const char *msg_client)
1612 Con_DPrintf("rejecting client: %s\n", msg);
1614 dpsnprintf(data_out, *len_out, "reject %s", msg_client);
1615 *len_out = strlen(data_out);
1616 return CRYPTO_DISCARD;
1619 static int Crypto_SoftServerError(char *data_out, size_t *len_out, const char *msg)
1622 Con_DPrintf("%s\n", msg);
1623 return CRYPTO_DISCARD;
1626 static int Crypto_ServerParsePacket_Internal(const char *data_in, size_t len_in, char *data_out, size_t *len_out, lhnetaddress_t *peeraddress)
1628 // if "connect": reject if in the middle of crypto handshake
1629 crypto_t *crypto = NULL;
1630 char *data_out_p = data_out;
1631 const char *string = data_in;
1635 char infostringvalue[MAX_INPUTLINE];
1638 if(!d0_blind_id_dll)
1639 return CRYPTO_NOMATCH; // no support
1641 if (len_in > 8 && !memcmp(string, "connect\\", 8) && d0_rijndael_dll && crypto_aeslevel.integer >= 3)
1645 // sorry, we have to verify the challenge here to not reflect network spam
1647 if (!(s = InfoString_GetValue(string + 4, "challenge", infostringvalue, sizeof(infostringvalue))))
1648 return CRYPTO_NOMATCH; // will be later accepted if encryption was set up
1649 // validate the challenge
1650 for (i = 0;i < MAX_CHALLENGES;i++)
1651 if(challenge[i].time > 0)
1652 if (!LHNETADDRESS_Compare(peeraddress, &challenge[i].address) && !strcmp(challenge[i].string, s))
1654 // if the challenge is not recognized, drop the packet
1655 if (i == MAX_CHALLENGES) // challenge mismatch is silent
1656 return CRYPTO_DISCARD; // pre-challenge: rather be silent
1658 crypto = Crypto_ServerFindInstance(peeraddress, false);
1659 if(!crypto || !crypto->authenticated)
1660 return Crypto_ServerError(data_out, len_out, "This server requires authentication and encryption to be supported by your client", NULL);
1662 else if(len_in > 5 && !memcmp(string, "d0pk\\", 5) && ((LHNETADDRESS_GetAddressType(peeraddress) == LHNETADDRESSTYPE_LOOP) || sv_public.integer > -3))
1664 const char *cnt, *s, *p;
1666 int clientid = -1, serverid = -1;
1667 cnt = InfoString_GetValue(string + 4, "id", infostringvalue, sizeof(infostringvalue));
1668 id = (cnt ? atoi(cnt) : -1);
1669 cnt = InfoString_GetValue(string + 4, "cnt", infostringvalue, sizeof(infostringvalue));
1671 return CRYPTO_DISCARD; // pre-challenge: rather be silent
1672 GetUntilNul(&data_in, &len_in);
1674 return CRYPTO_DISCARD; // pre-challenge: rather be silent
1675 if(!strcmp(cnt, "0"))
1678 if (!(s = InfoString_GetValue(string + 4, "challenge", infostringvalue, sizeof(infostringvalue))))
1679 return CRYPTO_DISCARD; // pre-challenge: rather be silent
1680 // validate the challenge
1681 for (i = 0;i < MAX_CHALLENGES;i++)
1682 if(challenge[i].time > 0)
1683 if (!LHNETADDRESS_Compare(peeraddress, &challenge[i].address) && !strcmp(challenge[i].string, s))
1685 // if the challenge is not recognized, drop the packet
1686 if (i == MAX_CHALLENGES) // challenge mismatch is silent
1687 return CRYPTO_DISCARD; // pre-challenge: rather be silent
1689 if (!(s = InfoString_GetValue(string + 4, "aeslevel", infostringvalue, sizeof(infostringvalue))))
1690 aeslevel = 0; // not supported
1692 aeslevel = bound(0, atoi(s), 3);
1693 switch(bound(0, d0_rijndael_dll ? crypto_aeslevel.integer : 0, 3))
1695 default: // dummy, never happens, but to make gcc happy...
1698 return Crypto_ServerError(data_out, len_out, "This server requires encryption to be not required (crypto_aeslevel <= 2)", NULL);
1702 aes = (aeslevel >= 2);
1705 aes = (aeslevel >= 1);
1709 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);
1714 p = GetUntilNul(&data_in, &len_in);
1717 for(i = 0; i < MAX_PUBKEYS; ++i)
1720 if(!strcmp(p, pubkeys_fp64[i]))
1721 if(pubkeys_havepriv[i])
1726 return Crypto_ServerError(data_out, len_out, "Invalid server key", NULL);
1728 p = GetUntilNul(&data_in, &len_in);
1731 for(i = 0; i < MAX_PUBKEYS; ++i)
1734 if(!strcmp(p, pubkeys_fp64[i]))
1739 return Crypto_ServerError(data_out, len_out, "Invalid client key", NULL);
1742 crypto = Crypto_ServerFindInstance(peeraddress, true);
1744 return Crypto_ServerError(data_out, len_out, "Could not create a crypto connect instance", NULL);
1746 CDATA->cdata_id = id;
1747 CDATA->s = serverid;
1748 CDATA->c = clientid;
1749 memset(crypto->dhkey, 0, sizeof(crypto->dhkey));
1750 CDATA->challenge[0] = 0;
1751 crypto->client_keyfp[0] = 0;
1752 crypto->client_idfp[0] = 0;
1753 crypto->server_keyfp[0] = 0;
1754 crypto->server_idfp[0] = 0;
1755 crypto->use_aes = aes != 0;
1759 // I am the server, and my key is ok... so let's set server_keyfp and server_idfp
1760 strlcpy(crypto->server_keyfp, pubkeys_fp64[CDATA->s], sizeof(crypto->server_keyfp));
1761 strlcpy(crypto->server_idfp, pubkeys_priv_fp64[CDATA->s], sizeof(crypto->server_idfp));
1764 CDATA->id = qd0_blind_id_new();
1768 return Crypto_ServerError(data_out, len_out, "d0_blind_id_new failed", "Internal error");
1770 if(!qd0_blind_id_copy(CDATA->id, pubkeys[CDATA->s]))
1773 return Crypto_ServerError(data_out, len_out, "d0_blind_id_copy failed", "Internal error");
1775 PutWithNul(&data_out_p, len_out, va(vabuf, sizeof(vabuf), "d0pk\\cnt\\1\\id\\%d\\aes\\%d", CDATA->cdata_id, crypto->use_aes));
1776 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
1779 return Crypto_ServerError(data_out, len_out, "d0_blind_id_authenticate_with_private_id_start failed", "Internal error");
1781 CDATA->next_step = 2;
1782 data_out_p += *len_out;
1783 *len_out = data_out_p - data_out;
1784 return CRYPTO_DISCARD;
1786 else if(CDATA->c >= 0)
1789 CDATA->id = qd0_blind_id_new();
1793 return Crypto_ServerError(data_out, len_out, "d0_blind_id_new failed", "Internal error");
1795 if(!qd0_blind_id_copy(CDATA->id, pubkeys[CDATA->c]))
1798 return Crypto_ServerError(data_out, len_out, "d0_blind_id_copy failed", "Internal error");
1800 PutWithNul(&data_out_p, len_out, va(vabuf, sizeof(vabuf), "d0pk\\cnt\\5\\id\\%d\\aes\\%d", CDATA->cdata_id, crypto->use_aes));
1801 if(!qd0_blind_id_authenticate_with_private_id_challenge(CDATA->id, true, false, data_in, len_in, data_out_p, len_out, &status))
1804 return Crypto_ServerError(data_out, len_out, "d0_blind_id_authenticate_with_private_id_challenge failed", "Internal error");
1806 CDATA->next_step = 6;
1807 data_out_p += *len_out;
1808 *len_out = data_out_p - data_out;
1809 return CRYPTO_DISCARD;
1814 return Crypto_ServerError(data_out, len_out, "Missing client and server key", NULL);
1817 else if(!strcmp(cnt, "2"))
1820 crypto = Crypto_ServerFindInstance(peeraddress, false);
1822 return CRYPTO_NOMATCH; // pre-challenge, rather be silent
1824 if(CDATA->cdata_id != id)
1825 return Crypto_SoftServerError(data_out, len_out, va(vabuf, sizeof(vabuf), "Got d0pk\\id\\%d when expecting %d", id, CDATA->cdata_id));
1826 if(CDATA->next_step != 2)
1827 return Crypto_SoftServerError(data_out, len_out, va(vabuf, sizeof(vabuf), "Got d0pk\\cnt\\%s when expecting %d", cnt, CDATA->next_step));
1829 PutWithNul(&data_out_p, len_out, va(vabuf, sizeof(vabuf), "d0pk\\cnt\\3\\id\\%d", CDATA->cdata_id));
1830 if(!qd0_blind_id_authenticate_with_private_id_response(CDATA->id, data_in, len_in, data_out_p, len_out))
1833 return Crypto_ServerError(data_out, len_out, "d0_blind_id_authenticate_with_private_id_response failed", "Internal error");
1835 fpbuflen = DHKEY_SIZE;
1836 if(!qd0_blind_id_sessionkey_public_id(CDATA->id, (char *) crypto->dhkey, &fpbuflen))
1839 return Crypto_ServerError(data_out, len_out, "d0_blind_id_sessionkey_public_id failed", "Internal error");
1843 if(!qd0_blind_id_copy(CDATA->id, pubkeys[CDATA->c]))
1846 return Crypto_ServerError(data_out, len_out, "d0_blind_id_copy failed", "Internal error");
1848 CDATA->next_step = 4;
1852 // session key is FINISHED (no server part is to be expected)! By this, all keys are set up
1853 crypto->authenticated = true;
1854 CDATA->next_step = 0;
1856 data_out_p += *len_out;
1857 *len_out = data_out_p - data_out;
1858 return CRYPTO_DISCARD;
1860 else if(!strcmp(cnt, "4"))
1862 crypto = Crypto_ServerFindInstance(peeraddress, false);
1864 return CRYPTO_NOMATCH; // pre-challenge, rather be silent
1866 if(CDATA->cdata_id != id)
1867 return Crypto_SoftServerError(data_out, len_out, va(vabuf, sizeof(vabuf), "Got d0pk\\id\\%d when expecting %d", id, CDATA->cdata_id));
1868 if(CDATA->next_step != 4)
1869 return Crypto_SoftServerError(data_out, len_out, va(vabuf, sizeof(vabuf), "Got d0pk\\cnt\\%s when expecting %d", cnt, CDATA->next_step));
1870 PutWithNul(&data_out_p, len_out, va(vabuf, sizeof(vabuf), "d0pk\\cnt\\5\\id\\%d", CDATA->cdata_id));
1871 if(!qd0_blind_id_authenticate_with_private_id_challenge(CDATA->id, true, false, data_in, len_in, data_out_p, len_out, &status))
1874 return Crypto_ServerError(data_out, len_out, "d0_blind_id_authenticate_with_private_id_challenge failed", "Internal error");
1876 CDATA->next_step = 6;
1877 data_out_p += *len_out;
1878 *len_out = data_out_p - data_out;
1879 return CRYPTO_DISCARD;
1881 else if(!strcmp(cnt, "6"))
1883 static char msgbuf[32];
1884 size_t msgbuflen = sizeof(msgbuf);
1887 unsigned char dhkey[DHKEY_SIZE];
1888 crypto = Crypto_ServerFindInstance(peeraddress, false);
1890 return CRYPTO_NOMATCH; // pre-challenge, rather be silent
1892 if(CDATA->cdata_id != id)
1893 return Crypto_SoftServerError(data_out, len_out, va(vabuf, sizeof(vabuf), "Got d0pk\\id\\%d when expecting %d", id, CDATA->cdata_id));
1894 if(CDATA->next_step != 6)
1895 return Crypto_SoftServerError(data_out, len_out, va(vabuf, sizeof(vabuf), "Got d0pk\\cnt\\%s when expecting %d", cnt, CDATA->next_step));
1897 if(!qd0_blind_id_authenticate_with_private_id_verify(CDATA->id, data_in, len_in, msgbuf, &msgbuflen, &status))
1900 return Crypto_ServerError(data_out, len_out, "d0_blind_id_authenticate_with_private_id_verify failed (authentication error)", "Authentication error");
1903 strlcpy(crypto->client_keyfp, pubkeys_fp64[CDATA->c], sizeof(crypto->client_keyfp));
1905 crypto->client_keyfp[0] = 0;
1906 memset(crypto->client_idfp, 0, sizeof(crypto->client_idfp));
1907 fpbuflen = FP64_SIZE;
1908 if(!qd0_blind_id_fingerprint64_public_id(CDATA->id, crypto->client_idfp, &fpbuflen))
1911 return Crypto_ServerError(data_out, len_out, "d0_blind_id_fingerprint64_public_id failed", "Internal error");
1913 fpbuflen = DHKEY_SIZE;
1914 if(!qd0_blind_id_sessionkey_public_id(CDATA->id, (char *) dhkey, &fpbuflen))
1917 return Crypto_ServerError(data_out, len_out, "d0_blind_id_sessionkey_public_id failed", "Internal error");
1919 // XOR the two DH keys together to make one
1920 for(i = 0; i < DHKEY_SIZE; ++i)
1921 crypto->dhkey[i] ^= dhkey[i];
1923 // session key is FINISHED (no server part is to be expected)! By this, all keys are set up
1924 crypto->authenticated = true;
1925 CDATA->next_step = 0;
1926 // send a challenge-less challenge
1927 PutWithNul(&data_out_p, len_out, "challenge ");
1928 *len_out = data_out_p - data_out;
1929 --*len_out; // remove NUL terminator
1930 return CRYPTO_MATCH;
1932 return CRYPTO_NOMATCH; // pre-challenge, rather be silent
1934 return CRYPTO_NOMATCH;
1937 int Crypto_ServerParsePacket(const char *data_in, size_t len_in, char *data_out, size_t *len_out, lhnetaddress_t *peeraddress)
1941 static double complain_time = 0;
1943 qboolean do_time = false;
1944 qboolean do_reject = false;
1945 char infostringvalue[MAX_INPUTLINE];
1946 if(crypto_servercpupercent.value > 0 || crypto_servercpumaxtime.value > 0)
1947 if(len_in > 5 && !memcmp(data_in, "d0pk\\", 5))
1950 cnt = InfoString_GetValue(data_in + 4, "cnt", infostringvalue, sizeof(infostringvalue));
1952 if(!strcmp(cnt, "0"))
1957 // check if we may perform crypto...
1958 if(crypto_servercpupercent.value > 0)
1960 crypto_servercpu_accumulator += (realtime - crypto_servercpu_lastrealtime) * crypto_servercpupercent.value * 0.01;
1961 if(crypto_servercpumaxtime.value)
1962 if(crypto_servercpu_accumulator > crypto_servercpumaxtime.value)
1963 crypto_servercpu_accumulator = crypto_servercpumaxtime.value;
1967 if(crypto_servercpumaxtime.value > 0)
1968 if(realtime != crypto_servercpu_lastrealtime)
1969 crypto_servercpu_accumulator = crypto_servercpumaxtime.value;
1971 crypto_servercpu_lastrealtime = realtime;
1972 if(do_reject && crypto_servercpu_accumulator < 0)
1974 if(realtime > complain_time + 5)
1975 Con_Printf("crypto: cannot perform requested crypto operations; denial service attack or crypto_servercpupercent/crypto_servercpumaxtime are too low\n");
1977 return CRYPTO_DISCARD;
1979 t = Sys_DirtyTime();
1981 ret = Crypto_ServerParsePacket_Internal(data_in, len_in, data_out, len_out, peeraddress);
1984 t = Sys_DirtyTime() - t;if (t < 0.0) t = 0.0; // dirtytime can step backwards
1985 if(crypto_servercpudebug.integer)
1986 Con_Printf("crypto: accumulator was %.1f ms, used %.1f ms for crypto, ", crypto_servercpu_accumulator * 1000, t * 1000);
1987 crypto_servercpu_accumulator -= t;
1988 if(crypto_servercpudebug.integer)
1989 Con_Printf("is %.1f ms\n", crypto_servercpu_accumulator * 1000);
1994 static int Crypto_ClientError(char *data_out, size_t *len_out, const char *msg)
1996 dpsnprintf(data_out, *len_out, "reject %s", msg);
1997 *len_out = strlen(data_out);
1998 return CRYPTO_REPLACE;
2001 static int Crypto_SoftClientError(char *data_out, size_t *len_out, const char *msg)
2004 Con_Printf("%s\n", msg);
2005 return CRYPTO_DISCARD;
2008 int Crypto_ClientParsePacket(const char *data_in, size_t len_in, char *data_out, size_t *len_out, lhnetaddress_t *peeraddress)
2010 crypto_t *crypto = &cls.crypto;
2011 const char *string = data_in;
2014 char *data_out_p = data_out;
2016 char infostringvalue[MAX_INPUTLINE];
2019 if(!d0_blind_id_dll)
2020 return CRYPTO_NOMATCH; // no support
2022 // if "challenge": verify challenge, and discard message, send next crypto protocol message instead
2023 // otherwise, just handle actual protocol messages
2025 if (len_in == 6 && !memcmp(string, "accept", 6) && cls.connect_trying && d0_rijndael_dll)
2027 int wantserverid = -1;
2028 Crypto_RetrieveHostKey(&cls.connect_address, &wantserverid, NULL, 0, NULL, 0, NULL);
2029 if(!crypto || !crypto->authenticated)
2031 if(wantserverid >= 0)
2032 return Crypto_ClientError(data_out, len_out, "Server tried an unauthenticated connection even though a host key is present");
2033 if(crypto_aeslevel.integer >= 3)
2034 return Crypto_ClientError(data_out, len_out, "This server requires encryption to be not required (crypto_aeslevel <= 2)");
2036 return CRYPTO_NOMATCH;
2038 else if (len_in >= 1 && string[0] == 'j' && cls.connect_trying && d0_rijndael_dll && crypto_aeslevel.integer >= 3)
2040 int wantserverid = -1;
2041 Crypto_RetrieveHostKey(&cls.connect_address, &wantserverid, NULL, 0, NULL, 0, NULL);
2042 if(!crypto || !crypto->authenticated)
2044 if(wantserverid >= 0)
2045 return Crypto_ClientError(data_out, len_out, "Server tried an unauthenticated connection even though a host key is present");
2046 if(crypto_aeslevel.integer >= 3)
2047 return Crypto_ClientError(data_out, len_out, "This server requires encryption to be not required (crypto_aeslevel <= 2)");
2049 return CRYPTO_NOMATCH;
2051 else if (len_in >= 13 && !memcmp(string, "infoResponse\x0A", 13))
2053 s = InfoString_GetValue(string + 13, "d0_blind_id", infostringvalue, sizeof(infostringvalue));
2055 Crypto_StoreHostKey(peeraddress, s, true);
2056 return CRYPTO_NOMATCH;
2058 else if (len_in >= 15 && !memcmp(string, "statusResponse\x0A", 15))
2062 p = strchr(string + 15, '\n');
2066 * (char *) p = 0; // cut off the string there
2068 s = InfoString_GetValue(string + 15, "d0_blind_id", infostringvalue, sizeof(infostringvalue));
2070 Crypto_StoreHostKey(peeraddress, s, true);
2073 * (char *) p = save;
2074 // invoking those nasal demons again (do not run this on the DS9k)
2076 return CRYPTO_NOMATCH;
2078 else if(len_in > 10 && !memcmp(string, "challenge ", 10) && cls.connect_trying)
2080 const char *vlen_blind_id_ptr = NULL;
2081 size_t len_blind_id_ptr = 0;
2083 const char *challenge = data_in + 10;
2086 int clientid = -1, serverid = -1, wantserverid = -1;
2087 qboolean server_can_auth = true;
2088 char wantserver_idfp[FP64_SIZE+1];
2089 int wantserver_aeslevel;
2091 // if we have a stored host key for the server, assume serverid to already be selected!
2092 // (the loop will refuse to overwrite this one then)
2093 wantserver_idfp[0] = 0;
2094 Crypto_RetrieveHostKey(&cls.connect_address, &wantserverid, NULL, 0, wantserver_idfp, sizeof(wantserver_idfp), &wantserver_aeslevel);
2095 // requirement: wantserver_idfp is a full ID if wantserverid set
2097 // if we leave, we have to consider the connection
2098 // unauthenticated; NOTE: this may be faked by a clever
2099 // attacker to force an unauthenticated connection; so we have
2100 // a safeguard check in place when encryption is required too
2101 // in place, or when authentication is required by the server
2102 crypto->authenticated = false;
2104 GetUntilNul(&data_in, &len_in);
2106 return (wantserverid >= 0) ? Crypto_ClientError(data_out, len_out, "Server tried an unauthenticated connection even though a host key is present") :
2107 (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) :
2110 // FTEQW extension protocol
2113 k = Crypto_LittleLong(data_in);
2114 v = Crypto_LittleLong(data_in + 4);
2122 k = Crypto_LittleLong(data_in);
2127 case PROTOCOL_D0_BLIND_ID:
2128 vlen_blind_id_ptr = data_in;
2129 len_blind_id_ptr = v;
2141 if(!vlen_blind_id_ptr)
2142 return (wantserverid >= 0) ? Crypto_ClientError(data_out, len_out, "Server tried an unauthenticated connection even though authentication is required") :
2143 (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) :
2146 data_in = vlen_blind_id_ptr;
2147 len_in = len_blind_id_ptr;
2149 // parse fingerprints
2150 // once we found a fingerprint we can auth to (ANY), select it as clientfp
2151 // once we found a fingerprint in the first list that we know, select it as serverfp
2155 p = GetUntilNul(&data_in, &len_in);
2160 if(!server_can_auth)
2161 break; // other protocol message may follow
2162 server_can_auth = false;
2167 for(i = 0; i < MAX_PUBKEYS; ++i)
2170 if(!strcmp(p, pubkeys_fp64[i]))
2172 if(pubkeys_havepriv[i])
2177 if(wantserverid < 0 || i == wantserverid)
2181 if(clientid >= 0 && serverid >= 0)
2185 // if stored host key is not found:
2186 if(wantserverid >= 0 && serverid < 0)
2187 return Crypto_ClientError(data_out, len_out, "Server CA does not match stored host key, refusing to connect");
2189 if(serverid >= 0 || clientid >= 0)
2191 // TODO at this point, fill clientside crypto struct!
2193 CDATA->cdata_id = ++cdata_id;
2194 CDATA->s = serverid;
2195 CDATA->c = clientid;
2196 memset(crypto->dhkey, 0, sizeof(crypto->dhkey));
2197 strlcpy(CDATA->challenge, challenge, sizeof(CDATA->challenge));
2198 crypto->client_keyfp[0] = 0;
2199 crypto->client_idfp[0] = 0;
2200 crypto->server_keyfp[0] = 0;
2201 crypto->server_idfp[0] = 0;
2202 memcpy(CDATA->wantserver_idfp, wantserver_idfp, sizeof(crypto->server_idfp));
2204 if(CDATA->wantserver_idfp[0]) // if we know a host key, honor its encryption setting
2205 switch(bound(0, d0_rijndael_dll ? crypto_aeslevel.integer : 0, 3))
2207 default: // dummy, never happens, but to make gcc happy...
2209 if(wantserver_aeslevel >= 3)
2210 return Crypto_ServerError(data_out, len_out, "This server requires encryption to be not required (crypto_aeslevel <= 2)", NULL);
2211 CDATA->wantserver_aes = false;
2214 CDATA->wantserver_aes = (wantserver_aeslevel >= 2);
2217 CDATA->wantserver_aes = (wantserver_aeslevel >= 1);
2220 if(wantserver_aeslevel <= 0)
2221 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);
2222 CDATA->wantserver_aes = true;
2226 // build outgoing message
2227 // append regular stuff
2228 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));
2229 PutWithNul(&data_out_p, len_out, serverid >= 0 ? pubkeys_fp64[serverid] : "");
2230 PutWithNul(&data_out_p, len_out, clientid >= 0 ? pubkeys_fp64[clientid] : "");
2234 // I am the client, and my key is ok... so let's set client_keyfp and client_idfp
2235 strlcpy(crypto->client_keyfp, pubkeys_fp64[CDATA->c], sizeof(crypto->client_keyfp));
2236 strlcpy(crypto->client_idfp, pubkeys_priv_fp64[CDATA->c], sizeof(crypto->client_idfp));
2242 CDATA->id = qd0_blind_id_new();
2246 return Crypto_ClientError(data_out, len_out, "d0_blind_id_new failed");
2248 if(!qd0_blind_id_copy(CDATA->id, pubkeys[CDATA->s]))
2251 return Crypto_ClientError(data_out, len_out, "d0_blind_id_copy failed");
2253 CDATA->next_step = 1;
2254 *len_out = data_out_p - data_out;
2256 else if(clientid >= 0)
2258 // skip over server auth, perform client auth only
2260 CDATA->id = qd0_blind_id_new();
2264 return Crypto_ClientError(data_out, len_out, "d0_blind_id_new failed");
2266 if(!qd0_blind_id_copy(CDATA->id, pubkeys[CDATA->c]))
2269 return Crypto_ClientError(data_out, len_out, "d0_blind_id_copy failed");
2271 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
2274 return Crypto_ClientError(data_out, len_out, "d0_blind_id_authenticate_with_private_id_start failed");
2276 CDATA->next_step = 5;
2277 data_out_p += *len_out;
2278 *len_out = data_out_p - data_out;
2281 *len_out = data_out_p - data_out;
2283 return CRYPTO_DISCARD;
2287 if(wantserver_idfp[0]) // if we know a host key, honor its encryption setting
2288 if(wantserver_aeslevel >= 3)
2289 return Crypto_ClientError(data_out, len_out, "Server insists on encryption, but neither can authenticate to the other");
2290 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) :
2294 else if(len_in > 5 && !memcmp(string, "d0pk\\", 5) && cls.connect_trying)
2298 cnt = InfoString_GetValue(string + 4, "id", infostringvalue, sizeof(infostringvalue));
2299 id = (cnt ? atoi(cnt) : -1);
2300 cnt = InfoString_GetValue(string + 4, "cnt", infostringvalue, sizeof(infostringvalue));
2302 return Crypto_ClientError(data_out, len_out, "d0pk\\ message without cnt");
2303 GetUntilNul(&data_in, &len_in);
2305 return Crypto_ClientError(data_out, len_out, "d0pk\\ message without attachment");
2307 if(!strcmp(cnt, "1"))
2310 if(CDATA->cdata_id != id)
2311 return Crypto_SoftServerError(data_out, len_out, va(vabuf, sizeof(vabuf), "Got d0pk\\id\\%d when expecting %d", id, CDATA->cdata_id));
2312 if(CDATA->next_step != 1)
2313 return Crypto_SoftClientError(data_out, len_out, va(vabuf, sizeof(vabuf), "Got d0pk\\cnt\\%s when expecting %d", cnt, CDATA->next_step));
2315 cls.connect_nextsendtime = max(cls.connect_nextsendtime, realtime + 1); // prevent "hammering"
2317 if((s = InfoString_GetValue(string + 4, "aes", infostringvalue, sizeof(infostringvalue))))
2321 // we CANNOT toggle the AES status any more!
2322 // as the server already decided
2323 if(CDATA->wantserver_idfp[0]) // if we know a host key, honor its encryption setting
2324 if(!aes && CDATA->wantserver_aes)
2327 return Crypto_ClientError(data_out, len_out, "Stored host key requires encryption, but server did not enable encryption");
2329 if(aes && (!d0_rijndael_dll || crypto_aeslevel.integer <= 0))
2332 return Crypto_ClientError(data_out, len_out, "Server insists on encryption too hard");
2334 if(!aes && (d0_rijndael_dll && crypto_aeslevel.integer >= 3))
2337 return Crypto_ClientError(data_out, len_out, "Server insists on plaintext too hard");
2339 crypto->use_aes = aes != 0;
2341 PutWithNul(&data_out_p, len_out, va(vabuf, sizeof(vabuf), "d0pk\\cnt\\2\\id\\%d", CDATA->cdata_id));
2342 if(!qd0_blind_id_authenticate_with_private_id_challenge(CDATA->id, true, false, data_in, len_in, data_out_p, len_out, &status))
2345 return Crypto_ClientError(data_out, len_out, "d0_blind_id_authenticate_with_private_id_challenge failed");
2347 CDATA->next_step = 3;
2348 data_out_p += *len_out;
2349 *len_out = data_out_p - data_out;
2350 return CRYPTO_DISCARD;
2352 else if(!strcmp(cnt, "3"))
2354 static char msgbuf[32];
2355 size_t msgbuflen = sizeof(msgbuf);
2359 if(CDATA->cdata_id != id)
2360 return Crypto_SoftServerError(data_out, len_out, va(vabuf, sizeof(vabuf), "Got d0pk\\id\\%d when expecting %d", id, CDATA->cdata_id));
2361 if(CDATA->next_step != 3)
2362 return Crypto_SoftClientError(data_out, len_out, va(vabuf, sizeof(vabuf), "Got d0pk\\cnt\\%s when expecting %d", cnt, CDATA->next_step));
2364 cls.connect_nextsendtime = max(cls.connect_nextsendtime, realtime + 1); // prevent "hammering"
2366 if(!qd0_blind_id_authenticate_with_private_id_verify(CDATA->id, data_in, len_in, msgbuf, &msgbuflen, &status))
2369 return Crypto_ClientError(data_out, len_out, "d0_blind_id_authenticate_with_private_id_verify failed (server authentication error)");
2372 strlcpy(crypto->server_keyfp, pubkeys_fp64[CDATA->s], sizeof(crypto->server_keyfp));
2374 crypto->server_keyfp[0] = 0;
2375 memset(crypto->server_idfp, 0, sizeof(crypto->server_idfp));
2376 fpbuflen = FP64_SIZE;
2377 if(!qd0_blind_id_fingerprint64_public_id(CDATA->id, crypto->server_idfp, &fpbuflen))
2380 return Crypto_ClientError(data_out, len_out, "d0_blind_id_fingerprint64_public_id failed");
2382 if(CDATA->wantserver_idfp[0])
2383 if(memcmp(CDATA->wantserver_idfp, crypto->server_idfp, sizeof(crypto->server_idfp)))
2386 return Crypto_ClientError(data_out, len_out, "Server ID does not match stored host key, refusing to connect");
2388 fpbuflen = DHKEY_SIZE;
2389 if(!qd0_blind_id_sessionkey_public_id(CDATA->id, (char *) crypto->dhkey, &fpbuflen))
2392 return Crypto_ClientError(data_out, len_out, "d0_blind_id_sessionkey_public_id failed");
2395 // cache the server key
2396 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);
2400 // client will auth next
2401 PutWithNul(&data_out_p, len_out, va(vabuf, sizeof(vabuf), "d0pk\\cnt\\4\\id\\%d", CDATA->cdata_id));
2402 if(!qd0_blind_id_copy(CDATA->id, pubkeys[CDATA->c]))
2405 return Crypto_ClientError(data_out, len_out, "d0_blind_id_copy failed");
2407 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
2410 return Crypto_ClientError(data_out, len_out, "d0_blind_id_authenticate_with_private_id_start failed");
2412 CDATA->next_step = 5;
2413 data_out_p += *len_out;
2414 *len_out = data_out_p - data_out;
2415 return CRYPTO_DISCARD;
2419 // session key is FINISHED (no server part is to be expected)! By this, all keys are set up
2420 crypto->authenticated = true;
2421 CDATA->next_step = 0;
2422 // assume we got the empty challenge to finish the protocol
2423 PutWithNul(&data_out_p, len_out, "challenge ");
2424 *len_out = data_out_p - data_out;
2425 --*len_out; // remove NUL terminator
2426 return CRYPTO_REPLACE;
2429 else if(!strcmp(cnt, "5"))
2432 unsigned char dhkey[DHKEY_SIZE];
2436 if(CDATA->cdata_id != id)
2437 return Crypto_SoftServerError(data_out, len_out, va(vabuf, sizeof(vabuf), "Got d0pk\\id\\%d when expecting %d", id, CDATA->cdata_id));
2438 if(CDATA->next_step != 5)
2439 return Crypto_SoftClientError(data_out, len_out, va(vabuf, sizeof(vabuf), "Got d0pk\\cnt\\%s when expecting %d", cnt, CDATA->next_step));
2441 cls.connect_nextsendtime = max(cls.connect_nextsendtime, realtime + 1); // prevent "hammering"
2443 if(CDATA->s < 0) // only if server didn't auth
2445 if((s = InfoString_GetValue(string + 4, "aes", infostringvalue, sizeof(infostringvalue))))
2449 if(CDATA->wantserver_idfp[0]) // if we know a host key, honor its encryption setting
2450 if(!aes && CDATA->wantserver_aes)
2453 return Crypto_ClientError(data_out, len_out, "Stored host key requires encryption, but server did not enable encryption");
2455 if(aes && (!d0_rijndael_dll || crypto_aeslevel.integer <= 0))
2458 return Crypto_ClientError(data_out, len_out, "Server insists on encryption too hard");
2460 if(!aes && (d0_rijndael_dll && crypto_aeslevel.integer >= 3))
2463 return Crypto_ClientError(data_out, len_out, "Server insists on plaintext too hard");
2465 crypto->use_aes = aes != 0;
2468 PutWithNul(&data_out_p, len_out, va(vabuf, sizeof(vabuf), "d0pk\\cnt\\6\\id\\%d", CDATA->cdata_id));
2469 if(!qd0_blind_id_authenticate_with_private_id_response(CDATA->id, data_in, len_in, data_out_p, len_out))
2472 return Crypto_ClientError(data_out, len_out, "d0_blind_id_authenticate_with_private_id_response failed");
2474 fpbuflen = DHKEY_SIZE;
2475 if(!qd0_blind_id_sessionkey_public_id(CDATA->id, (char *) dhkey, &fpbuflen))
2478 return Crypto_ClientError(data_out, len_out, "d0_blind_id_sessionkey_public_id failed");
2480 // XOR the two DH keys together to make one
2481 for(i = 0; i < DHKEY_SIZE; ++i)
2482 crypto->dhkey[i] ^= dhkey[i];
2483 // session key is FINISHED! By this, all keys are set up
2484 crypto->authenticated = true;
2485 CDATA->next_step = 0;
2486 data_out_p += *len_out;
2487 *len_out = data_out_p - data_out;
2488 return CRYPTO_DISCARD;
2490 return Crypto_SoftClientError(data_out, len_out, "Got unknown d0_blind_id message from server");
2493 return CRYPTO_NOMATCH;
2496 size_t Crypto_SignData(const void *data, size_t datasize, int keyid, void *signed_data, size_t signed_size)
2498 if(keyid < 0 || keyid >= MAX_PUBKEYS)
2500 if(!pubkeys_havepriv[keyid])
2502 if(qd0_blind_id_sign_with_private_id_sign(pubkeys[keyid], true, false, (const char *)data, datasize, (char *)signed_data, &signed_size))
2507 size_t Crypto_SignDataDetached(const void *data, size_t datasize, int keyid, void *signed_data, size_t signed_size)
2509 if(keyid < 0 || keyid >= MAX_PUBKEYS)
2511 if(!pubkeys_havepriv[keyid])
2513 if(qd0_blind_id_sign_with_private_id_sign_detached(pubkeys[keyid], true, false, (const char *)data, datasize, (char *)signed_data, &signed_size))