X-Git-Url: http://git.xonotic.org/?p=xonotic%2Fdarkplaces.git;a=blobdiff_plain;f=crypto-keygen-standalone.c;h=d9206915a775453043d36bc3817e9f0fccd362da;hp=7ae6d66e37523797337d6fa0f4c1a1a85139b011;hb=8d2492ac4364e1a51e7f84c6ea1c34429f060939;hpb=018f5c7c845311a157da255d819683770c6543c4 diff --git a/crypto-keygen-standalone.c b/crypto-keygen-standalone.c index 7ae6d66e..d9206915 100644 --- a/crypto-keygen-standalone.c +++ b/crypto-keygen-standalone.c @@ -91,49 +91,87 @@ static size_t Crypto_UnParsePack(char *buf, size_t len, unsigned long header, co return pos; } -void file2lumps(const char *fn, unsigned long header, const char **lumps, size_t *lumpsize, size_t nlumps) +void file2buf(const char *fn, char **data, size_t *datasize) { FILE *f; - char buf[65536]; - size_t n; - f = fopen(fn, "rb"); + *data = NULL; + *datasize = 0; + size_t n = 0, dn = 0; + if(!strncmp(fn, "/dev/fd/", 8)) + f = fdopen(atoi(fn + 8), "rb"); + else + f = fopen(fn, "rb"); if(!f) + { + return; + } + for(;;) + { + *data = realloc(*data, *datasize += 8192); + if(!*data) + { + *datasize = 0; + fclose(f); + return; + } + dn = fread(*data + n, 1, *datasize - n, f); + if(!dn) + break; + n += dn; + } + fclose(f); + *datasize = n; +} + +int buf2file(const char *fn, const char *data, size_t n) +{ + FILE *f; + 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); + if(fclose(f) || !n) + return 0; + return 1; +} + +void file2lumps(const char *fn, unsigned long header, const char **lumps, size_t *lumpsize, size_t nlumps) +{ + char *buf; + size_t n; + file2buf(fn, &buf, &n); + if(!buf) { fprintf(stderr, "could not open %s\n", fn); exit(1); } - n = fread(buf, 1, sizeof(buf), f); - fclose(f); if(!Crypto_ParsePack(buf, n, header, lumps, lumpsize, nlumps)) { fprintf(stderr, "could not parse %s as %c%c%c%c (%d lumps expected)\n", fn, (int) header & 0xFF, (int) (header >> 8) & 0xFF, (int) (header >> 16) & 0xFF, (int) (header >> 24) & 0xFF, (int) nlumps); + free(buf); exit(1); } + free(buf); } mode_t umask_save; void lumps2file(const char *fn, unsigned long header, const char *const *lumps, size_t *lumpsize, size_t nlumps, D0_BOOL private) { - FILE *f; char buf[65536]; size_t n; if(private) umask(umask_save | 0077); else umask(umask_save); - f = fopen(fn, "wb"); - if(!f) - { - fprintf(stderr, "could not open %s\n", fn); - exit(1); - } if(!(n = Crypto_UnParsePack(buf, sizeof(buf), header, lumps, lumpsize, nlumps))) { fprintf(stderr, "could not unparse for %s\n", fn); exit(1); } - n = fwrite(buf, n, 1, f); - if(fclose(f) || !n) + if(!buf2file(fn, buf, n)) { fprintf(stderr, "could not write %s\n", fn); exit(1); @@ -156,8 +194,12 @@ void USAGE(const char *me) "%s -p public.d0pk -i id.d0pi\n" "%s -p public.d0pk -I idkey.d0si\n" "%s -0 -p public.d0pk -I idkey.d0si\n" - "%s -0 -p public.d0pk\n", - me, me, me, me, me, me, me, me, me, me, me, me, me, me + "%s -0 -p public.d0pk\n" + "%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 ); } @@ -235,9 +277,12 @@ int main(int argc, char **argv) int opt; size_t lumpsize[2]; 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; + 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; @@ -253,7 +298,7 @@ int main(int argc, char **argv) umask_save = umask(0022); ctx = d0_blind_id_new(); - while((opt = getopt(argc, argv, "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) { @@ -310,6 +355,14 @@ int main(int argc, char **argv) // test mode mask |= 0x200; break; + case 'd': + datafile = optarg; + mask |= 0x400; + break; + case 's': + sigfile = optarg; + mask |= 0x800; + break; case 'X': infix = optarg; break; @@ -425,6 +478,26 @@ int main(int argc, char **argv) } } + if(mask & 0x400) + { + file2buf(datafile, &databuf_in, &databufsize_in); + if(!databuf_in) + { + 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); + } + } + switch(mask) { // modes of operation: @@ -553,6 +626,67 @@ int main(int argc, char **argv) CHECK(d0_blind_id_fingerprint64_public_id(ctx, fp64, &fp64size)); printf("%.*s\n", (int)fp64size, fp64); break; + case 0x449: + // public key, private ID, data -> signed data + databufsize_out = databufsize_in + 8192; + databuf_out = malloc(databufsize_out); + 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 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_sig; + databuf_out = malloc(databufsize_out); + 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); + buf2file(outfile, databuf_out, databufsize_out); + + 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 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