| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
#include "atheme.h" |
| 22 |
#include "libathemecore.h" |
| 23 |
|
| 24 |
#if defined HAVE_OPENSSL && defined HAVE_OPENSSL_EC_H |
| 25 |
|
| 26 |
#include <openssl/ec.h> |
| 27 |
#include <openssl/ecdsa.h> |
| 28 |
#include <openssl/evp.h> |
| 29 |
#include <openssl/rand.h> |
| 30 |
#include <openssl/sha.h> |
| 31 |
|
| 32 |
int main(int argc, const char **argv) |
| 33 |
{ |
| 34 |
BIO *in; |
| 35 |
EC_KEY *eckey; |
| 36 |
char challenge[BUFSIZE]; |
| 37 |
const unsigned char *workbuf_p; |
| 38 |
unsigned char *sig_buf, *sig_buf_p; |
| 39 |
size_t len; |
| 40 |
unsigned int buf_len, i; |
| 41 |
|
| 42 |
if (argv[1] == NULL || argv[2] == NULL) |
| 43 |
{ |
| 44 |
fprintf(stderr, "usage: %s [keyfile] [base64challenge]\n", argv[0]); |
| 45 |
return EXIT_FAILURE; |
| 46 |
} |
| 47 |
|
| 48 |
in = BIO_new(BIO_s_file()); |
| 49 |
BIO_read_filename(in, argv[1]); |
| 50 |
eckey = PEM_read_bio_ECPrivateKey(in, NULL, NULL, NULL); |
| 51 |
BIO_free(in); |
| 52 |
|
| 53 |
if (!EC_KEY_check_key(eckey)) |
| 54 |
{ |
| 55 |
fprintf(stderr, "Key data for %s is inconsistent.\n", argv[1]); |
| 56 |
return EXIT_FAILURE; |
| 57 |
} |
| 58 |
|
| 59 |
memset(challenge, '\0', sizeof challenge); |
| 60 |
len = base64_decode(argv[2], challenge, BUFSIZE); |
| 61 |
workbuf_p = (unsigned char *) challenge; |
| 62 |
|
| 63 |
buf_len = ECDSA_size(eckey); |
| 64 |
sig_buf = mowgli_alloc(buf_len); |
| 65 |
sig_buf_p = sig_buf; |
| 66 |
|
| 67 |
if (!ECDSA_sign(0, challenge, len, sig_buf_p, &buf_len, eckey)) |
| 68 |
{ |
| 69 |
fprintf(stderr, "Failed to sign challenge!\n"); |
| 70 |
return EXIT_FAILURE; |
| 71 |
} |
| 72 |
|
| 73 |
base64_encode(sig_buf, buf_len, challenge, BUFSIZE); |
| 74 |
printf("%s\n", challenge); |
| 75 |
|
| 76 |
mowgli_free(sig_buf); |
| 77 |
|
| 78 |
return EXIT_SUCCESS; |
| 79 |
} |
| 80 |
|
| 81 |
#else |
| 82 |
|
| 83 |
int main(int argc, const char **argv) |
| 84 |
{ |
| 85 |
printf("I'm sorry, you didn't compile Atheme with OpenSSL support.\n"); |
| 86 |
return EXIT_SUCCESS; |
| 87 |
} |
| 88 |
|
| 89 |
#endif |