+ if(pubkeys_havesig[keygen_i])
+ {
+ Con_Printf("there is already a signed private key for %d\n", i);
+ keygen_i = -1;
+ SV_UnlockThreadMutex();
+ return;
+ }
+ // if we get here, we only need a signature, no new keygen run needed
+ Con_Printf("Only need a signature for an existing key...\n");
+ }
+ else
+ {
+ // we also need a new ID itself
+ if(!qd0_blind_id_generate_private_id_start(pubkeys[keygen_i]))
+ {
+ Con_Printf("d0_blind_id_start failed\n");
+ keygen_i = -1;
+ SV_UnlockThreadMutex();
+ return;
+ }
+ // verify the key we just got (just in case)
+ if(!qd0_blind_id_verify_private_id(pubkeys[keygen_i]))
+ {
+ Con_Printf("d0_blind_id_verify_private_id failed\n");
+ keygen_i = -1;
+ SV_UnlockThreadMutex();
+ return;
+ }
+ // we have a valid key now!
+ // make the rest of crypto.c know that
+ len2 = FP64_SIZE;
+ if(qd0_blind_id_fingerprint64_public_id(pubkeys[keygen_i], pubkeys_priv_fp64[keygen_i], &len2)) // keeps final NUL
+ {
+ Con_Printf("Generated private ID key_%d.d0pk (public key fingerprint: %s)\n", keygen_i, pubkeys_priv_fp64[keygen_i]);
+ pubkeys_havepriv[keygen_i] = true;
+ strlcat(crypto_idstring_buf, va(vabuf, sizeof(vabuf), " %s@%s", pubkeys_priv_fp64[keygen_i], pubkeys_fp64[keygen_i]), sizeof(crypto_idstring_buf));
+ crypto_idstring = crypto_idstring_buf;
+ Crypto_BuildChallengeAppend();
+ }
+ // write the key to disk
+ p[0] = buf;
+ l[0] = sizeof(buf);
+ if(!qd0_blind_id_write_private_id(pubkeys[keygen_i], buf, &l[0]))
+ {
+ Con_Printf("d0_blind_id_write_private_id failed\n");
+ keygen_i = -1;
+ SV_UnlockThreadMutex();
+ return;
+ }
+ if(!(buf2size = Crypto_UnParsePack(buf2, sizeof(buf2), FOURCC_D0SI, p, l, 1)))
+ {
+ Con_Printf("Crypto_UnParsePack failed\n");
+ keygen_i = -1;
+ SV_UnlockThreadMutex();
+ return;
+ }
+
+ FS_CreatePath(va(vabuf, sizeof(vabuf), "%skey_%d.d0si%s", *fs_userdir ? fs_userdir : fs_basedir, keygen_i, sessionid.string));
+ f = FS_SysOpen(va(vabuf, sizeof(vabuf), "%skey_%d.d0si%s", *fs_userdir ? fs_userdir : fs_basedir, keygen_i, sessionid.string), "wb", false);
+ if(!f)
+ {
+ Con_Printf("Cannot open key_%d.d0si%s\n", keygen_i, sessionid.string);
+ keygen_i = -1;
+ SV_UnlockThreadMutex();
+ return;
+ }
+ FS_Write(f, buf2, buf2size);
+ FS_Close(f);
+
+ Crypto_SavePubKeyTextFile(keygen_i);
+
+ Con_Printf("Saved unsigned key to key_%d.d0si%s\n", keygen_i, sessionid.string);