X-Git-Url: http://de.git.xonotic.org/?p=xonotic%2Fdarkplaces.git;a=blobdiff_plain;f=crypto-keygen-standalone.c;h=d9206915a775453043d36bc3817e9f0fccd362da;hp=95268400c0acd508db88f9d51cb2caeb0b29c143;hb=19487ef2ebada39fb6a6a372b30ca4f66bb78ada;hpb=1dfae27462dba735f317cac38f3b7deb415ed154 diff --git a/crypto-keygen-standalone.c b/crypto-keygen-standalone.c index 95268400..d9206915 100644 --- a/crypto-keygen-standalone.c +++ b/crypto-keygen-standalone.c @@ -97,7 +97,10 @@ void file2buf(const char *fn, char **data, size_t *datasize) *data = NULL; *datasize = 0; size_t n = 0, dn = 0; - f = fopen(fn, "rb"); + if(!strncmp(fn, "/dev/fd/", 8)) + f = fdopen(atoi(fn + 8), "rb"); + else + f = fopen(fn, "rb"); if(!f) { return; @@ -108,6 +111,7 @@ void file2buf(const char *fn, char **data, size_t *datasize) if(!*data) { *datasize = 0; + fclose(f); return; } dn = fread(*data + n, 1, *datasize - n, f); @@ -122,7 +126,10 @@ void file2buf(const char *fn, char **data, size_t *datasize) int buf2file(const char *fn, const char *data, size_t n) { FILE *f; - f = fopen(fn, "wb"); + if(!strncmp(fn, "/dev/fd/", 8)) + f = fdopen(atoi(fn + 8), "wb"); + else + f = fopen(fn, "wb"); if(!f) return 0; n = fwrite(data, n, 1, f); @@ -188,10 +195,11 @@ void USAGE(const char *me) "%s -p public.d0pk -I idkey.d0si\n" "%s -0 -p public.d0pk -I idkey.d0si\n" "%s -0 -p public.d0pk\n" - "%s -p public.d0pk -I idkey.d0si -f file-to-sign.dat -o file-signed.dat\n" - "%s -p public.d0pk -f file-signed.dat -o file-content.dat\n" - "%s -p public.d0pk -f file-signed.dat -o file-content.dat -O idkey.d0pi\n", - me, me, me, me, me, me, me, me, me, me, me, me, me, me, me, me, me + "%s -p public.d0pk -I idkey.d0si -d file-to-sign.dat -o file-signed.dat\n" + "%s -p public.d0pk -s file-signed.dat -o file-content.dat [-O id.d0pi]\n" + "%s -p public.d0pk -I idkey.d0si -d file-to-sign.dat -O signature.dat\n" + "%s -p public.d0pk -d file-to-sign.dat -s signature.dat [-O id.d0pi]\n", + me, me, me, me, me, me, me, me, me, me, me, me, me, me, me, me, me, me ); } @@ -271,9 +279,10 @@ int main(int argc, char **argv) const char *lumps[2]; char *databuf_in; size_t databufsize_in; char *databuf_out; size_t databufsize_out; + char *databuf_sig; size_t databufsize_sig; char lumps_w0[65536]; char lumps_w1[65536]; - const char *pubkeyfile = NULL, *privkeyfile = NULL, *pubidfile = NULL, *prividfile = NULL, *idreqfile = NULL, *idresfile = NULL, *outfile = NULL, *outfile2 = NULL, *camouflagefile = NULL, *datafile = NULL; + const char *pubkeyfile = NULL, *privkeyfile = NULL, *pubidfile = NULL, *prividfile = NULL, *idreqfile = NULL, *idresfile = NULL, *outfile = NULL, *outfile2 = NULL, *camouflagefile = NULL, *datafile = NULL, *sigfile = NULL; char fp64[513]; size_t fp64size = 512; int mask = 0; int bits = 1024; @@ -289,7 +298,7 @@ int main(int argc, char **argv) umask_save = umask(0022); ctx = d0_blind_id_new(); - while((opt = getopt(argc, argv, "f:p:P:i:I:j:J:o:O:c:b:x:X:y:Fn:C0")) != -1) + while((opt = getopt(argc, argv, "d:s:p:P:i:I:j:J:o:O:c:b:x:X:y:Fn:C0")) != -1) { switch(opt) { @@ -346,10 +355,14 @@ int main(int argc, char **argv) // test mode mask |= 0x200; break; - case 'f': + case 'd': datafile = optarg; mask |= 0x400; break; + case 's': + sigfile = optarg; + mask |= 0x800; + break; case 'X': infix = optarg; break; @@ -470,7 +483,17 @@ int main(int argc, char **argv) file2buf(datafile, &databuf_in, &databufsize_in); if(!databuf_in) { - fprintf(stderr, "could not decode private ID\n"); + fprintf(stderr, "could not decode data\n"); + exit(1); + } + } + + if(mask & 0x800) + { + file2buf(sigfile, &databuf_sig, &databufsize_sig); + if(!databuf_sig) + { + fprintf(stderr, "could not decode signature\n"); exit(1); } } @@ -610,14 +633,21 @@ int main(int argc, char **argv) CHECK(d0_blind_id_sign_with_private_id_sign(ctx, 1, 0, databuf_in, databufsize_in, databuf_out, &databufsize_out)); buf2file(outfile, databuf_out, databufsize_out); break; - case 0x441: - case 0x4C1: - // public key, data -> signed data, optional public ID + case 0x489: + // public key, private ID, data -> signature + databufsize_out = databufsize_in + 8192; + databuf_out = malloc(databufsize_out); + CHECK(d0_blind_id_sign_with_private_id_sign_detached(ctx, 1, 0, databuf_in, databufsize_in, databuf_out, &databufsize_out)); + buf2file(outfile2, databuf_out, databufsize_out); + break; + case 0x841: + case 0x8C1: + // public key, signed data -> data, optional public ID { D0_BOOL status; - databufsize_out = databufsize_in; + databufsize_out = databufsize_sig; databuf_out = malloc(databufsize_out); - CHECK(d0_blind_id_sign_with_private_id_verify(ctx, 1, 0, databuf_in, databufsize_in, databuf_out, &databufsize_out, &status)); + CHECK(d0_blind_id_sign_with_private_id_verify(ctx, 1, 0, databuf_sig, databufsize_sig, databuf_out, &databufsize_out, &status)); CHECK(d0_blind_id_fingerprint64_public_id(ctx, fp64, &fp64size)); printf("%d\n", (int)status); printf("%.*s\n", (int)fp64size, fp64); @@ -635,6 +665,28 @@ int main(int argc, char **argv) } } break; + case 0xC01: + case 0xC81: + // public key, signature, signed data -> optional public ID + { + D0_BOOL status; + CHECK(d0_blind_id_sign_with_private_id_verify_detached(ctx, 1, 0, databuf_sig, databufsize_sig, databuf_in, databufsize_in, &status)); + CHECK(d0_blind_id_fingerprint64_public_id(ctx, fp64, &fp64size)); + printf("%d\n", (int)status); + printf("%.*s\n", (int)fp64size, fp64); + + if(outfile2) + { + lumps[0] = lumps_w0; + lumpsize[0] = sizeof(lumps_w0); + lumps[1] = lumps_w1; + lumpsize[1] = sizeof(lumps_w1); + CHECK(d0_blind_id_write_public_key(ctx, lumps_w0, &lumpsize[0])); + CHECK(d0_blind_id_write_private_id_modulus(ctx, lumps_w1, &lumpsize[1])); + lumps2file(outfile2, FOURCC_D0PK, lumps, lumpsize, 2, 0); + } + } + break; /* case 0x09: // public key, private ID file -> test whether key is properly signed