Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

crypt_prog: add -verify_string command (to verify a string signature) #5309

Merged
merged 1 commit into from
Jul 17, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 48 additions & 22 deletions lib/crypt_prog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@
// create a signature for a given string
// write it in hex notation
// -verify file signature_file public_keyfile
// verify a signature
// verify a file signature
// -verify_string string signature_file public_keyfile
// verify a string signature
// -test_crypt private_keyfile public_keyfile
// test encrypt/decrypt
// -convkey o2b/b2o priv/pub input_file output_file
Expand Down Expand Up @@ -62,23 +64,25 @@

void usage() {
fprintf(stderr,
"Usage: crypt_prog options\n\n"
"Options:\n\n"
"-genkey n private_keyfile public_keyfile\n"
" create an n-bit key pair\n"
"-sign file private_keyfile\n"
" create a signature for a given file, write to stdout\n"
"-sign_string string private_keyfile\n"
" create a signature for a given string\n"
"-verify file signature_file public_keyfile\n"
" verify a signature\n"
"-test_crypt private_keyfile public_keyfile\n"
" test encrypt/decrypt functions\n"
"-convkey o2b/b2o priv/pub input_file output_file\n"
" convert keys between BOINC and OpenSSL format\n"
"-cert_verify file signature certificate_dir\n"
" verify a signature using a directory of certificates\n"
);
"Usage: crypt_prog options\n\n"
"Options:\n\n"
"-genkey n private_keyfile public_keyfile\n"
" create an n-bit key pair\n"
"-sign file private_keyfile\n"
" create a signature for a given file, write to stdout\n"
"-sign_string string private_keyfile\n"
" create a signature for a given string\n"
"-verify file signature_file public_keyfile\n"
" verify a file signature\n"
"-verify_string string signature_file public_keyfile\n"
" verify a string signature\n"
"-test_crypt private_keyfile public_keyfile\n"
" test encrypt/decrypt functions\n"
"-convkey o2b/b2o priv/pub input_file output_file\n"
" convert keys between BOINC and OpenSSL format\n"
"-cert_verify file signature certificate_dir\n"
" verify a signature using a directory of certificates\n"
);
}

unsigned int random_int() {
Expand Down Expand Up @@ -214,13 +218,35 @@
retval = md5_file(argv[2], md5_buf, size);
if (retval) die("md5_file");
retval = check_file_signature(
md5_buf, public_key, signature, is_valid
);
md5_buf, public_key, signature, is_valid
);
if (retval) die("check_file_signature");
if (is_valid) {
printf("file is valid\n");
printf("signature is valid\n");

Check warning on line 225 in lib/crypt_prog.cpp

View check run for this annotation

Codecov / codecov/patch

lib/crypt_prog.cpp#L225

Added line #L225 was not covered by tests
} else {
printf("file is invalid\n");
printf("signature is invalid\n");
return 1;

Check warning on line 228 in lib/crypt_prog.cpp

View check run for this annotation

Codecov / codecov/patch

lib/crypt_prog.cpp#L227-L228

Added lines #L227 - L228 were not covered by tests
}
} else if (!strcmp(argv[1], "-verify_string")) {
if (argc < 5) {
usage();
exit(1);

Check warning on line 233 in lib/crypt_prog.cpp

View check run for this annotation

Codecov / codecov/patch

lib/crypt_prog.cpp#L232-L233

Added lines #L232 - L233 were not covered by tests
}
fpub = fopen(argv[4], "r");

Check warning on line 235 in lib/crypt_prog.cpp

View check run for this annotation

Codecov / codecov/patch

lib/crypt_prog.cpp#L235

Added line #L235 was not covered by tests
if (!fpub) die("fopen");
retval = scan_key_hex(fpub, (KEY*)&public_key, sizeof(public_key));

Check warning on line 237 in lib/crypt_prog.cpp

View check run for this annotation

Codecov / codecov/patch

lib/crypt_prog.cpp#L237

Added line #L237 was not covered by tests
if (retval) die("read_public_key");
f = fopen(argv[3], "r");

Check warning on line 239 in lib/crypt_prog.cpp

View check run for this annotation

Codecov / codecov/patch

lib/crypt_prog.cpp#L239

Added line #L239 was not covered by tests
if (!f) die("fopen");
int n = fread(cbuf, 1, 256, f);
cbuf[n] = 0;

Check warning on line 242 in lib/crypt_prog.cpp

View check run for this annotation

Codecov / codecov/patch

lib/crypt_prog.cpp#L241-L242

Added lines #L241 - L242 were not covered by tests

retval = check_string_signature(argv[2], cbuf, public_key, is_valid);

Check warning on line 244 in lib/crypt_prog.cpp

View check run for this annotation

Codecov / codecov/patch

lib/crypt_prog.cpp#L244

Added line #L244 was not covered by tests
if (retval) die("check_string_signature");
if (is_valid) {
printf("signature is valid\n");

Check warning on line 247 in lib/crypt_prog.cpp

View check run for this annotation

Codecov / codecov/patch

lib/crypt_prog.cpp#L247

Added line #L247 was not covered by tests
} else {
printf("signature is invalid\n");

Check warning on line 249 in lib/crypt_prog.cpp

View check run for this annotation

Codecov / codecov/patch

lib/crypt_prog.cpp#L249

Added line #L249 was not covered by tests
return 1;
}
} else if (!strcmp(argv[1], "-test_crypt")) {
Expand Down
Loading