aboutsummaryrefslogtreecommitdiffstatshomepage
diff options
context:
space:
mode:
authorDavid M. Syzdek <david@syzdek.net>2023-01-12 01:12:45 -0900
committerDavid M. Syzdek <david@syzdek.net>2023-01-12 01:13:56 -0900
commit9fcee0c6a6a8641d19c3d0a1124d91cf6b620f9d (patch)
tree274d7e3ab615151f89d8a2417f7cc3ffb947dbb4
parent08e9ad179bad21d46d2389629f35e0bccf296ab3 (diff)
downloadotputils-9fcee0c6a6a8641d19c3d0a1124d91cf6b620f9d.tar.gz
otputils-9fcee0c6a6a8641d19c3d0a1124d91cf6b620f9d.tar.bz2
otputils-9fcee0c6a6a8641d19c3d0a1124d91cf6b620f9d.tar.xz
otputils-9fcee0c6a6a8641d19c3d0a1124d91cf6b620f9d.zip
libotputil: fixing checks in otputil_otp_code()
-rw-r--r--include/otputil.h5
-rw-r--r--lib/libotputil/lrfc2289-otp.c74
2 files changed, 50 insertions, 29 deletions
diff --git a/include/otputil.h b/include/otputil.h
index f549eb2..13420fc 100644
--- a/include/otputil.h
+++ b/include/otputil.h
@@ -348,12 +348,13 @@ otputil_hotp_str(
348//---------------// 348//---------------//
349#pragma mark OTP functions (RFC 2289) 349#pragma mark OTP functions (RFC 2289)
350 350
351_OTPUTIL_F uint64_t 351_OTPUTIL_F int
352otputil_otp_code( 352otputil_otp_code(
353 const char * otp_pass, 353 const char * otp_pass,
354 const char * otp_seed, 354 const char * otp_seed,
355 int otp_seq, 355 int otp_seq,
356 int otp_hash ); 356 int otp_hash,
357 uint64_t * otp_resultp );
357 358
358 359
359_OTPUTIL_F otputil_bv_t * 360_OTPUTIL_F otputil_bv_t *
diff --git a/lib/libotputil/lrfc2289-otp.c b/lib/libotputil/lrfc2289-otp.c
index a9d55e4..e3512f3 100644
--- a/lib/libotputil/lrfc2289-otp.c
+++ b/lib/libotputil/lrfc2289-otp.c
@@ -67,12 +67,13 @@
67///////////////// 67/////////////////
68#pragma mark - Functions 68#pragma mark - Functions
69 69
70uint64_t 70int
71otputil_otp_code( 71otputil_otp_code(
72 const char * otp_pass, 72 const char * otp_pass,
73 const char * otp_seed, 73 const char * otp_seed,
74 int otp_seq, 74 int otp_seq,
75 int otp_hash ) 75 int otp_hash,
76 uint64_t * otp_resultp )
76{ 77{
77 char secret[OTPUTIL_OTP_PASS_MAX_LEN+OTPUTIL_OTP_SEED_MAX_LEN+1]; 78 char secret[OTPUTIL_OTP_PASS_MAX_LEN+OTPUTIL_OTP_SEED_MAX_LEN+1];
78 size_t len; 79 size_t len;
@@ -86,41 +87,59 @@ otputil_otp_code(
86 assert(otp_pass != NULL); 87 assert(otp_pass != NULL);
87 assert(otp_seed != NULL); 88 assert(otp_seed != NULL);
88 89
89 // check input parameters 90 // check otp_pass
90 if (otp_seq == 0) 91 len = strlen(otp_pass);
91 return(0); 92 if ((len > OTPUTIL_OTP_PASS_MAX_LEN) || (len < OTPUTIL_OTP_PASS_MIN_LEN))
92 if ((len = strlen(otp_pass)) > OTPUTIL_OTP_PASS_MAX_LEN) 93 return(-1);
93 return(0); 94
94 if (len < OTPUTIL_OTP_PASS_MIN_LEN) 95 // check otp_seed
95 return(0); 96 for(len = 0; ((otp_seed[len])); len++)
96 if ((len = strlen(otp_seed)) > OTPUTIL_OTP_SEED_MAX_LEN) 97 if (!(isalnum(otp_seed[len])))
97 return(0); 98 return(-1);
98 if (len < OTPUTIL_OTP_SEED_MIN_LEN) 99 if ((len > OTPUTIL_OTP_SEED_MAX_LEN) || (len < OTPUTIL_OTP_SEED_MIN_LEN))
99 return(0); 100 return(-1);
101
102 // check otp_hash
100 if ((evp_md = otputil_evp_md(otp_hash)) == NULL) 103 if ((evp_md = otputil_evp_md(otp_hash)) == NULL)
101 return(0); 104 return(-1);
102 105
103 // generate secret 106 // generate secret
104 strncpy(secret, otp_pass, sizeof(secret)); 107 bindle_strlcpy(secret, otp_seed, sizeof(secret));
105 strncat(secret, otp_seed, sizeof(secret)-1); 108 bindle_strlcat(secret, otp_pass, sizeof(secret));
106 secret_len = (unsigned)strlen(secret); 109 secret_len = (unsigned)strlen(secret);
107 110
108 // generate hashes 111 // generate hashes
109 for(pos = 0; (pos < otp_seq); pos++) 112 for(pos = 0; (pos <= otp_seq); pos++)
110 { 113 {
111 md_len = sizeof(md); 114 md_len = sizeof(md);
112 if (!(EVP_Digest(secret, secret_len, md, &md_len, evp_md, NULL))) 115 if (!(EVP_Digest(secret, secret_len, md, &md_len, evp_md, NULL)))
113 return(0); 116 return(-1);
114 for(u = 8; (u < md_len); u += 8) 117 switch(otp_hash)
115 md[u%8] ^= md[u]; 118 {
116 memcpy(secret, md, 8); 119 case OTPUTIL_MD_MD4:
117 secret_len = 8; 120 for(u = 0; (u < 8); u++)
121 md[u] ^= md[u+8];
122 memcpy(secret, md, 8);
123 secret_len = 8;
124 break;
125
126 case OTPUTIL_MD_MD5:
127 break;
128
129 case OTPUTIL_MD_SHA1:
130 break;
131
132 default:
133 return(-1);
134 };
118 }; 135 };
119 136
120 return( ((uint64_t)md[0] << 56) | ((uint64_t)md[1] << 48) 137 *otp_resultp = ((uint64_t)md[0] << 56) | ((uint64_t)md[1] << 48)
121 | ((uint64_t)md[2] << 40) | ((uint64_t)md[3] << 32) 138 | ((uint64_t)md[2] << 40) | ((uint64_t)md[3] << 32)
122 | ((uint64_t)md[4] << 24) | ((uint64_t)md[5] << 16) 139 | ((uint64_t)md[4] << 24) | ((uint64_t)md[5] << 16)
123 | ((uint64_t)md[6] << 8) | ((uint64_t)md[7] << 0) ); 140 | ((uint64_t)md[6] << 8) | ((uint64_t)md[7] << 0);
141
142 return(0);
124} 143}
125 144
126 145
@@ -143,7 +162,8 @@ otputil_otp_str(
143 res.bv_val = bv_val; 162 res.bv_val = bv_val;
144 res.bv_len = sizeof(bv_val); 163 res.bv_len = sizeof(bv_val);
145 164
146 val = otputil_otp_code(otp_pass, otp_seed, otp_seq, otp_hash); 165 if (otputil_otp_code(otp_pass, otp_seed, otp_seq, otp_hash, &val) == -1)
166 return(NULL);
147 167
148 bv_val[0] = (val >> 56) & 0xff; 168 bv_val[0] = (val >> 56) & 0xff;
149 bv_val[1] = (val >> 48) & 0xff; 169 bv_val[1] = (val >> 48) & 0xff;