diff options
Diffstat (limited to 'src/widget-otp.c')
| -rw-r--r-- | src/widget-otp.c | 322 |
1 files changed, 322 insertions, 0 deletions
diff --git a/src/widget-otp.c b/src/widget-otp.c new file mode 100644 index 0000000..ae67a86 --- /dev/null +++ b/src/widget-otp.c | |||
| @@ -0,0 +1,322 @@ | |||
| 1 | /* | ||
| 2 | * OTP Utilities | ||
| 3 | * Copyright (C) 2022 David M. Syzdek <david@syzdek.net>. | ||
| 4 | * | ||
| 5 | * Redistribution and use in source and binary forms, with or without | ||
| 6 | * modification, are permitted provided that the following conditions are | ||
| 7 | * met: | ||
| 8 | * | ||
| 9 | * * Redistributions of source code must retain the above copyright | ||
| 10 | * notice, this list of conditions and the following disclaimer. | ||
| 11 | * * Redistributions in binary form must reproduce the above copyright | ||
| 12 | * notice, this list of conditions and the following disclaimer in the | ||
| 13 | * documentation and/or other materials provided with the distribution. | ||
| 14 | * * Neither the name of David M. Syzdek nor the | ||
| 15 | * names of its contributors may be used to endorse or promote products | ||
| 16 | * derived from this software without specific prior written permission. | ||
| 17 | * | ||
| 18 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS | ||
| 19 | * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, | ||
| 20 | * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR | ||
| 21 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL DAVID M SYZDEK BE LIABLE FOR | ||
| 22 | * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
| 23 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR | ||
| 24 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER | ||
| 25 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
| 26 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
| 27 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
| 28 | * SUCH DAMAGE. | ||
| 29 | */ | ||
| 30 | /* | ||
| 31 | * @file src/widget-otp.c | ||
| 32 | */ | ||
| 33 | #define _SRC_WIDGET_OTP_C 1 | ||
| 34 | |||
| 35 | /////////////// | ||
| 36 | // // | ||
| 37 | // Headers // | ||
| 38 | // // | ||
| 39 | /////////////// | ||
| 40 | #pragma mark - Headers | ||
| 41 | |||
| 42 | #include <otputil_compat.h> | ||
| 43 | |||
| 44 | #ifdef HAVE_CONFIG_H | ||
| 45 | # include <config.h> | ||
| 46 | #endif | ||
| 47 | |||
| 48 | #include <errno.h> | ||
| 49 | #include <assert.h> | ||
| 50 | #include <stdarg.h> | ||
| 51 | #include <string.h> | ||
| 52 | #include <strings.h> | ||
| 53 | #include <stdlib.h> | ||
| 54 | #include <getopt.h> | ||
| 55 | #include <stdio.h> | ||
| 56 | |||
| 57 | #include "otputil.h" | ||
| 58 | |||
| 59 | |||
| 60 | ////////////////// | ||
| 61 | // // | ||
| 62 | // Prototypes // | ||
| 63 | // // | ||
| 64 | ////////////////// | ||
| 65 | #pragma mark - Prototypes | ||
| 66 | |||
| 67 | static int | ||
| 68 | otputil_widget_otp_verbose( | ||
| 69 | otputil_config_t * cnf, | ||
| 70 | const otputil_bv_t * otp_data, | ||
| 71 | const otputil_bv_t * otp_verify, | ||
| 72 | const char * status ); | ||
| 73 | |||
| 74 | |||
| 75 | ///////////////// | ||
| 76 | // // | ||
| 77 | // Functions // | ||
| 78 | // // | ||
| 79 | ///////////////// | ||
| 80 | #pragma mark - Functions | ||
| 81 | |||
| 82 | |||
| 83 | //---------------// | ||
| 84 | // main function // | ||
| 85 | //---------------// | ||
| 86 | #pragma mark main function | ||
| 87 | |||
| 88 | int | ||
| 89 | otputil_widget_otp( | ||
| 90 | otputil_config_t * cnf ) | ||
| 91 | { | ||
| 92 | int rc; | ||
| 93 | int hash; | ||
| 94 | int seq; | ||
| 95 | uint64_t meth; | ||
| 96 | char * pass; | ||
| 97 | char * endptr; | ||
| 98 | const char * str; | ||
| 99 | const char * status; | ||
| 100 | static char buff[OTPUTIL_MAX_ENCODE_SIZE]; | ||
| 101 | otputil_bv_t otp_data; | ||
| 102 | otputil_bv_t otp_verify; | ||
| 103 | otputil_bv_t * a; | ||
| 104 | otputil_bv_t * b; | ||
| 105 | uint8_t otp_data_val[512]; | ||
| 106 | uint8_t otp_verify_val[512]; | ||
| 107 | |||
| 108 | assert(cnf != NULL); | ||
| 109 | |||
| 110 | // set OTP method | ||
| 111 | meth = OTPUTIL_METH_OTP; | ||
| 112 | if ((rc = otputil_set_param(NULL, OTPUTIL_OPT_METHOD, &meth)) == -1) | ||
| 113 | { | ||
| 114 | fprintf(stderr, "%s: otputil_set_param(METHOD): %s\n", cnf->prog_name, otputil_err2string(rc)); | ||
| 115 | return(1); | ||
| 116 | }; | ||
| 117 | |||
| 118 | // initial processing of cli arguments | ||
| 119 | if ((rc = otputil_arguments(cnf, cnf->argc, cnf->argv)) != 0) | ||
| 120 | return((rc == -1) ? 0 : 1); | ||
| 121 | if (cnf->argc > optind) | ||
| 122 | { | ||
| 123 | seq = (int)strtoul(cnf->argv[optind], &endptr, 0); | ||
| 124 | if ((endptr == cnf->argv[optind]) || (endptr[0] != '\0')) | ||
| 125 | { | ||
| 126 | fprintf(stderr, "%s: invalid sequence number\n", cnf->prog_name); | ||
| 127 | fprintf(stderr, "Try `%s --help' for more information.\n", cnf->prog_name); | ||
| 128 | return(1); | ||
| 129 | }; | ||
| 130 | if ((rc = otputil_set_param(NULL, OTPUTIL_OPT_OTP_SEQ, &seq)) != OTPUTIL_SUCCESS) | ||
| 131 | { | ||
| 132 | fprintf(stderr, "%s: otputil_set_param(OTP_SEQ): %s\n", cnf->prog_name, otputil_err2string(rc)); | ||
| 133 | return(1); | ||
| 134 | }; | ||
| 135 | }; | ||
| 136 | if (cnf->argc > (optind+1)) | ||
| 137 | { | ||
| 138 | if ((rc = otputil_set_param(NULL, OTPUTIL_OPT_OTP_SEED, cnf->argv[optind+1])) != OTPUTIL_SUCCESS) | ||
| 139 | { | ||
| 140 | fprintf(stderr, "%s: otputil_set_param(OTP_SEED): %s\n", cnf->prog_name, otputil_err2string(rc)); | ||
| 141 | return(1); | ||
| 142 | }; | ||
| 143 | }; | ||
| 144 | |||
| 145 | // verify password and seed were provided | ||
| 146 | otputil_get_param(NULL, OTPUTIL_OPT_OTP_PASS, &pass); | ||
| 147 | if (!(pass)) | ||
| 148 | { | ||
| 149 | str = otputil_getpass("Enter user's password:", NULL, 0); | ||
| 150 | if ((rc = otputil_set_param(NULL, OTPUTIL_OPT_OTP_PASS, str)) != OTPUTIL_SUCCESS) | ||
| 151 | { | ||
| 152 | fprintf(stderr, "%s: otputil_set_param(OTP_PASS): %s\n", cnf->prog_name, otputil_err2string(rc)); | ||
| 153 | return(1); | ||
| 154 | }; | ||
| 155 | }; | ||
| 156 | if ((pass)) | ||
| 157 | free(pass); | ||
| 158 | otputil_get_param(NULL, OTPUTIL_OPT_OTP_SEED, &pass); | ||
| 159 | if (!(pass)) | ||
| 160 | { | ||
| 161 | fprintf(stderr, "%s: must provide OTP seed\n", cnf->prog_name); | ||
| 162 | fprintf(stderr, "Try `%s --help' for more information.\n", cnf->prog_name); | ||
| 163 | return(1); | ||
| 164 | }; | ||
| 165 | free(pass); | ||
| 166 | |||
| 167 | // generate OTP pass phrase | ||
| 168 | if ((str = otputil_str(NULL, buff, sizeof(buff))) == NULL) | ||
| 169 | { | ||
| 170 | fprintf(stderr, "%s: internal error\n", cnf->prog_name); | ||
| 171 | return(1); | ||
| 172 | }; | ||
| 173 | otp_data.bv_val = otp_data_val; | ||
| 174 | otp_data.bv_len = sizeof(otp_data_val); | ||
| 175 | otputil_get_param(NULL, OTPUTIL_OPT_OTP_HASH, &hash); | ||
| 176 | otputil_otp_decode(str, &otp_data, hash); | ||
| 177 | |||
| 178 | // verify existing code | ||
| 179 | if ((cnf->otp_pass)) | ||
| 180 | { | ||
| 181 | otp_verify.bv_val = otp_verify_val; | ||
| 182 | otp_verify.bv_len = sizeof(otp_verify_val); | ||
| 183 | if (otputil_otp_decode(cnf->otp_pass, &otp_verify, hash) == NULL) | ||
| 184 | { | ||
| 185 | fprintf(stderr, "%s: not a valid OTP passphrase to verify\n", cnf->prog_name); | ||
| 186 | return(1); | ||
| 187 | }; | ||
| 188 | a = &otp_data; | ||
| 189 | b = &otp_verify; | ||
| 190 | rc = ((otputil_bvcmp(&a, &b))) ? 2 : 0; | ||
| 191 | status = ((rc)) ? "invalid code" : "valid code"; | ||
| 192 | if ((cnf->verbose)) | ||
| 193 | otputil_widget_otp_verbose(cnf, &otp_data, &otp_verify, status); | ||
| 194 | else | ||
| 195 | printf("%s\n", status); | ||
| 196 | return(rc); | ||
| 197 | }; | ||
| 198 | |||
| 199 | // print results | ||
| 200 | if ( ((cnf->quiet)) || (!(cnf->verbose)) ) | ||
| 201 | { | ||
| 202 | printf("%s\n", str); | ||
| 203 | return(0); | ||
| 204 | }; | ||
| 205 | otputil_widget_otp_verbose(cnf, &otp_data, NULL, NULL); | ||
| 206 | |||
| 207 | return(0); | ||
| 208 | } | ||
| 209 | |||
| 210 | |||
| 211 | int | ||
| 212 | otputil_widget_otp_md4( | ||
| 213 | otputil_config_t * cnf ) | ||
| 214 | { | ||
| 215 | int rc; | ||
| 216 | int hash; | ||
| 217 | assert(cnf != NULL); | ||
| 218 | hash = OTPUTIL_MD_MD4; | ||
| 219 | if ((rc = otputil_set_param(NULL, OTPUTIL_OPT_OTP_HASH, &hash)) == -1) | ||
| 220 | { | ||
| 221 | fprintf(stderr, "%s: otputil_set_param(OTP_HASH): %s\n", cnf->prog_name, otputil_err2string(rc)); | ||
| 222 | return(1); | ||
| 223 | }; | ||
| 224 | return(otputil_widget_otp(cnf)); | ||
| 225 | } | ||
| 226 | |||
| 227 | |||
| 228 | int | ||
| 229 | otputil_widget_otp_md5( | ||
| 230 | otputil_config_t * cnf ) | ||
| 231 | { | ||
| 232 | int rc; | ||
| 233 | int hash; | ||
| 234 | assert(cnf != NULL); | ||
| 235 | hash = OTPUTIL_MD_MD5; | ||
| 236 | if ((rc = otputil_set_param(NULL, OTPUTIL_OPT_OTP_HASH, &hash)) == -1) | ||
| 237 | { | ||
| 238 | fprintf(stderr, "%s: otputil_set_param(OTP_HASH): %s\n", cnf->prog_name, otputil_err2string(rc)); | ||
| 239 | return(1); | ||
| 240 | }; | ||
| 241 | return(otputil_widget_otp(cnf)); | ||
| 242 | } | ||
| 243 | |||
| 244 | |||
| 245 | int | ||
| 246 | otputil_widget_otp_sha1( | ||
| 247 | otputil_config_t * cnf ) | ||
| 248 | { | ||
| 249 | int rc; | ||
| 250 | int hash; | ||
| 251 | assert(cnf != NULL); | ||
| 252 | hash = OTPUTIL_MD_SHA1; | ||
| 253 | if ((rc = otputil_set_param(NULL, OTPUTIL_OPT_OTP_HASH, &hash)) == -1) | ||
| 254 | { | ||
| 255 | fprintf(stderr, "%s: otputil_set_param(OTP_HASH): %s\n", cnf->prog_name, otputil_err2string(rc)); | ||
| 256 | return(1); | ||
| 257 | }; | ||
| 258 | return(otputil_widget_otp(cnf)); | ||
| 259 | } | ||
| 260 | |||
| 261 | |||
| 262 | int | ||
| 263 | otputil_widget_otp_usage( | ||
| 264 | otputil_config_t * cnf ) | ||
| 265 | { | ||
| 266 | assert(cnf != NULL); | ||
| 267 | printf("ALGORITHMS:\n"); | ||
| 268 | printf(" md4 MD4 Message-Digest Algorithm\n"); | ||
| 269 | printf(" md5 MD5 message-digest algorithm\n"); | ||
| 270 | printf(" sha1 SHA-1 (Secure Hash Algorithm 1)\n"); | ||
| 271 | printf(" sha256 SHA-2 (Secure Hash Algorithm 2) with 256 bits\n"); | ||
| 272 | printf(" sha512 SHA-2 (Secure Hash Algorithm 2) with 512 bits\n"); | ||
| 273 | return(0); | ||
| 274 | } | ||
| 275 | |||
| 276 | |||
| 277 | int | ||
| 278 | otputil_widget_otp_verbose( | ||
| 279 | otputil_config_t * cnf, | ||
| 280 | const otputil_bv_t * otp_data, | ||
| 281 | const otputil_bv_t * otp_verify, | ||
| 282 | const char * status ) | ||
| 283 | { | ||
| 284 | int otp_seq; | ||
| 285 | char * util_desc; | ||
| 286 | char * otp_seed; | ||
| 287 | char * otp_pass; | ||
| 288 | int otp_hash; | ||
| 289 | |||
| 290 | otputil_get_param(NULL, OTPUTIL_OPT_OTP_SEQ, &otp_seq); | ||
| 291 | otputil_get_param(NULL, OTPUTIL_OPT_OTP_HASH, &otp_hash); | ||
| 292 | otputil_get_param(NULL, OTPUTIL_OPT_OTP_SEED, &otp_seed); | ||
| 293 | otputil_get_param(NULL, OTPUTIL_OPT_OTP_PASS, &otp_pass); | ||
| 294 | otputil_get_param(NULL, OTPUTIL_OPT_DESC, &util_desc); | ||
| 295 | |||
| 296 | // print secret information | ||
| 297 | printf("S/KEY Secret:\n"); | ||
| 298 | printf(" Description: %s\n", (((util_desc)) ? util_desc : "n/a") ); | ||
| 299 | printf(" Method: OTP (RFC2289)\n"); | ||
| 300 | printf(" Hash: %s\n", otputil_md2str(otp_hash)); | ||
| 301 | if (cnf->verbose > 1) | ||
| 302 | printf(" User Passsword: %s\n", otp_pass); | ||
| 303 | else | ||
| 304 | printf(" User Passsword: ********\n"); | ||
| 305 | printf(" Seed: %s\n", otp_seed); | ||
| 306 | printf(" Sequence: %i\n", otp_seq ); | ||
| 307 | if ((cnf->otp_pass)) | ||
| 308 | printf(" Entered Pass Phrase: %s\n", cnf->otp_pass ); | ||
| 309 | if ((cnf->otp_pass)) | ||
| 310 | printf(" Expected Code: %s\n", otputil_otp_encode(otp_verify, NULL, 0, OTPUTIL_ENC_HEX) ); | ||
| 311 | printf(" OTP Code: %s\n", otputil_otp_encode(otp_data, NULL, 0, OTPUTIL_ENC_HEX) ); | ||
| 312 | if ((cnf->otp_pass)) | ||
| 313 | printf(" Expected Pass Phrase: %s\n", otputil_otp_encode(otp_verify, NULL, 0, OTPUTIL_ENC_SIXWORD) ); | ||
| 314 | printf(" OTP Pass Phrase: %s\n", otputil_otp_encode(otp_data, NULL, 0, OTPUTIL_ENC_SIXWORD) ); | ||
| 315 | if ((status)) | ||
| 316 | printf(" Status: %s\n", status ); | ||
| 317 | printf("\n"); | ||
| 318 | |||
| 319 | return(0); | ||
| 320 | } | ||
| 321 | |||
| 322 | /* end of source file */ | ||
