diff options
author | michael <michael@82007160-df01-0410-b94d-b575c5fd34c7> | 2013-06-09 16:16:58 +0000 |
---|---|---|
committer | michael <michael@82007160-df01-0410-b94d-b575c5fd34c7> | 2013-06-09 16:16:58 +0000 |
commit | efb0bc5730df991cc3a1625f8f31e469a4f4c98e (patch) | |
tree | 14a0135bae8a8e9313a0f321069224988f0bec0a /tools | |
parent | 7acc319c54f0d059a6abf6178f1da228594f98da (diff) |
- respond.c: white-space changes/style corrections
git-svn-id: svn://svn.ircd-hybrid.org/svnroot/ircd-hybrid/branches/8.1.x@2221 82007160-df01-0410-b94d-b575c5fd34c7
Diffstat (limited to 'tools')
-rw-r--r-- | tools/respond.c | 255 |
1 files changed, 137 insertions, 118 deletions
diff --git a/tools/respond.c b/tools/respond.c index 9a2169c..8884ea5 100644 --- a/tools/respond.c +++ b/tools/respond.c @@ -19,6 +19,7 @@ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * $Id: respond.c 33 2005-10-02 20:50:00Z knight $ */ + #include <stdio.h> #include <string.h> #include <openssl/err.h> @@ -30,137 +31,155 @@ static int insecure_mode = 0; static char *pass_param = NULL; -static int pass_cb(char *buf, int size, int rwflag, void *u) +static int +pass_cb(char *buf, int size, int rwflag, void *u) { - int len; - char *tmp; - - if (insecure_mode != 0) - { - if (pass_param == NULL) - return 0; - len = strlen(pass_param); - if (len <= 0) /* This SHOULDN'T happen */ - return 0; - if (len > size) - len = size; - memcpy(buf, pass_param, len); - return len; - } - - tmp = getpass("Enter passphrase for challenge: "); - if (!tmp) - { - puts("Couldn't read passphrase from stdin!"); - exit(-1); - } - len = strlen(tmp); - if (len <= 0) - return 0; - if (len > size) - len = size; - memcpy(buf, tmp, len); - return len; + int len = 0; + char *tmp = NULL; + + if (insecure_mode != 0) + { + if (pass_param == NULL) + return 0; + len = strlen(pass_param); + + if (len <= 0) /* This SHOULDN'T happen */ + return 0; + if (len > size) + len = size; + + memcpy(buf, pass_param, len); + return len; + } + + tmp = getpass("Enter passphrase for challenge: "); + + if (!tmp) + { + puts("Couldn't read passphrase from stdin!"); + exit(-1); + } + + len = strlen(tmp); + + if (len <= 0) + return 0; + if (len > size) + len = size; + + memcpy(buf, tmp, len); + return len; } static void -binary_to_hex( unsigned char * bin, char * hex, int length ) +binary_to_hex(unsigned char *bin, char *hex, int length) { - static const char trans[] = "0123456789ABCDEF"; - int i; - - for( i = 0; i < length; i++ ) - { - hex[i<<1] = trans[bin[i] >> 4]; - hex[(i<<1)+1] = trans[bin[i] & 0xf]; - } - hex[i<<1] = '\0'; + static const char trans[] = "0123456789ABCDEF"; + int i; + + for (i = 0; i < length; ++i) + { + hex[(i << 1) ] = trans[bin[i] >> 4]; + hex[(i << 1) + 1] = trans[bin[i] & 0xf]; + } + + hex[i << 1] = '\0'; } static int hex_to_binary(const char *from, char *to, int len) { - char a, b=1; - int p=0; - const char *ptr = from; - while (-1) - { - a = *ptr++; - if (!a) - break; - b = *ptr++; - - /* If this happens, we got bad input. */ - if (!b) - break; - if (p >= len) - break; - if (!((a >= '0' && a <= '9') || (a >= 'A' && a <= 'F'))) - break; - if (!((b >= '0' && b <= '9') || (b >= 'A' && b <= 'F'))) - break; - to[p++] = ((a <= '9') ? (a - '0') : (a - 'A' + 0xA))<<4 | - ((b <= '9') ? (b - '0') : (b - 'A' + 0xA)); - } - return p; + char a, b = 1; + int p = 0; + const char *ptr = from; + + while (-1) + { + a = *ptr++; + + if (!a) + break; + + b = *ptr++; + + /* If this happens, we got bad input. */ + if (!b) + break; + if (p >= len) + break; + if (!((a >= '0' && a <= '9') || (a >= 'A' && a <= 'F'))) + break; + if (!((b >= '0' && b <= '9') || (b >= 'A' && b <= 'F'))) + break; + + to[p++] = ((a <= '9') ? (a - '0') : (a - 'A' + 0xA)) << 4 | + ((b <= '9') ? (b - '0') : (b - 'A' + 0xA)); + } + + return p; } int main(int argc, char **argv) { - FILE *kfile; - RSA *rsa = NULL; - char ndata[257], ddata[257]; - /* respond privatefile challenge */ - if (argc < 3) - { - puts("Usage: respond privatefile challenge [passphrase]"); - return 0; - } - - if (argc == 4) - { - /* This is TOTALLY insecure and not recommended, but for - ** interfacing with irc client scripts, it's either this - ** or don't use a passphrase. - ** - ** The likelihood of a passphrase leaking isn't TOO great, - ** only ps auxww will show it, and even then, only at the - ** precise moment this is called. - */ - insecure_mode = 1; - pass_param = argv[3]; - } - - if (!(kfile = fopen(argv[1], "r"))) - { - puts("Could not open the private keyfile."); - return 0; - } - - SSLeay_add_all_ciphers(); - rsa = PEM_read_RSAPrivateKey(kfile, NULL,pass_cb, NULL); - - if(!rsa) - { - puts("Unable to read your private key, is the passphrase wrong?"); - return 0; - } - - fclose(kfile); - if (hex_to_binary(argv[2], ndata, 128) != 128) - { - puts("Bad challenge."); - return -1; - } - - if (RSA_private_decrypt(128, (unsigned char*)ndata, - (unsigned char*)ddata, rsa, RSA_PKCS1_PADDING) == -1) - { - puts("Decryption error."); - return -1; - } - binary_to_hex((unsigned char*)ddata, ndata, 32); - puts(ndata); - return 0; + FILE *kfile = NULL; + RSA *rsa = NULL; + char ndata[257], ddata[257]; + + /* respond privatefile challenge */ + if (argc < 3) + { + puts("Usage: respond privatefile challenge [passphrase]"); + return 0; + } + + if (argc == 4) + { + /* + * This is TOTALLY insecure and not recommended, but for + * interfacing with irc client scripts, it's either this + * or don't use a passphrase. + * + * The likelihood of a passphrase leaking isn't TOO great, + * only ps auxww will show it, and even then, only at the + * precise moment this is called. + */ + insecure_mode = 1; + pass_param = argv[3]; + } + + if (!(kfile = fopen(argv[1], "r"))) + { + puts("Could not open the private keyfile."); + return 0; + } + + SSLeay_add_all_ciphers(); + rsa = PEM_read_RSAPrivateKey(kfile, NULL,pass_cb, NULL); + + if (!rsa) + { + puts("Unable to read your private key, is the passphrase wrong?"); + return 0; + } + + fclose(kfile); + + if (hex_to_binary(argv[2], ndata, 128) != 128) + { + puts("Bad challenge."); + return -1; + } + + if (RSA_private_decrypt(128, (unsigned char *)ndata, + (unsigned char *)ddata, rsa, RSA_PKCS1_PADDING) == -1) + { + puts("Decryption error."); + return -1; + } + + binary_to_hex((unsigned char *)ddata, ndata, 32); + puts(ndata); + + return 0; } |