summaryrefslogtreecommitdiff
path: root/lib/libcrypt
diff options
context:
space:
mode:
authorjhigh <jhigh@NetBSD.org>2019-10-05 18:06:16 +0000
committerjhigh <jhigh@NetBSD.org>2019-10-05 18:06:16 +0000
commit015bbe080d67bbe6ff35b870543edad86ea2774c (patch)
treecf2a39d8babf0af09a6ad69a5de2d24f70527bd1 /lib/libcrypt
parentf3a61d6f6811a57cbecf07998c87f90291ca37b7 (diff)
adding full scheme comparison to libcrypt:crypt and pwhash tests
Diffstat (limited to 'lib/libcrypt')
-rw-r--r--lib/libcrypt/crypt.c72
1 files changed, 65 insertions, 7 deletions
diff --git a/lib/libcrypt/crypt.c b/lib/libcrypt/crypt.c
index 0c17951f044..ca992b2db1e 100644
--- a/lib/libcrypt/crypt.c
+++ b/lib/libcrypt/crypt.c
@@ -1,4 +1,4 @@
-/* $NetBSD: crypt.c,v 1.34 2015/06/17 00:15:26 christos Exp $ */
+/* $NetBSD: crypt.c,v 1.35 2019/10/05 18:06:16 jhigh Exp $ */
/*
* Copyright (c) 1989, 1993
@@ -37,13 +37,14 @@
#if 0
static char sccsid[] = "@(#)crypt.c 8.1.1.1 (Berkeley) 8/18/93";
#else
-__RCSID("$NetBSD: crypt.c,v 1.34 2015/06/17 00:15:26 christos Exp $");
+__RCSID("$NetBSD: crypt.c,v 1.35 2019/10/05 18:06:16 jhigh Exp $");
#endif
#endif /* not lint */
#include <limits.h>
#include <pwd.h>
#include <stdlib.h>
+#include <string.h> /* for strcmp */
#include <unistd.h>
#if defined(DEBUG) || defined(MAIN) || defined(UNIT_TEST)
#include <stdio.h>
@@ -498,6 +499,48 @@ ascii_is_unsafe(char ch)
}
/*
+ * We extract the scheme from setting str to allow for
+ * full scheme name comparison
+ * Updated to reflect alc suggestion(s)
+ *
+ * retuns boolean 0 on failure, 1 on success,
+ */
+static int
+nondes_scheme_substr(const char * setting,char * scheme, unsigned int len)
+{
+ const char * start;
+ const char * sep;
+
+ /* initialize head pointer */
+ start = setting;
+
+ /* clear out scheme buffer regardless of result */
+ memset(scheme, 0, len);
+
+ /* make sure we are working on non-des scheme string */
+ if (*start != _PASSWORD_NONDES) {
+ return 0;
+ }
+
+ /* increment passed initial _PASSWORD_NONDES */
+ start++;
+
+ if ((sep = memchr(start, _PASSWORD_NONDES,len-1)) == NULL) {
+ return 0;
+ }
+
+ /* if empty string, we are done */
+ if (sep == start) {
+ return 1;
+ }
+
+ /* copy scheme substr to buffer */
+ memcpy(scheme, start, (size_t)(sep - start));
+
+ return 1;
+}
+
+/*
* Return a pointer to static data consisting of the "setting"
* followed by an encryption produced by the "key" and "setting".
*/
@@ -505,24 +548,39 @@ static char *
__crypt(const char *key, const char *setting)
{
char *encp;
+ char scheme[12];
int32_t i;
int t;
+ int r;
int32_t salt;
int num_iter, salt_size;
C_block keyblock, rsltblock;
/* Non-DES encryption schemes hook in here. */
if (setting[0] == _PASSWORD_NONDES) {
- switch (setting[1]) {
- case '2':
+ r = nondes_scheme_substr(
+ setting, scheme, sizeof(scheme));
+
+ /* return NULL if we are unable to extract substring */
+ if (!r) {
+ return NULL;
+ }
+
+ /* $2a$ found in bcrypt.c:encode_salt */
+ if (strcmp(scheme, "2a") == 0) {
return (__bcrypt(key, setting));
- case 's':
+ } else if (strcmp(scheme, "sha1") == 0) {
+ /* $sha1$ found in crypt.h:SHA1_MAGIC */
return (__crypt_sha1(key, setting));
- case '1':
- default:
+ } else if (strcmp(scheme, "1") == 0) {
+ /* $1$ found in pw_gensalt.c:__gensalt_md5 */
return (__md5crypt(key, setting));
+ } else {
+ /* invalid scheme, including empty string */
+ return NULL;
}
}
+ /* End non-DES handling */
for (i = 0; i < 8; i++) {
if ((t = 2*(unsigned char)(*key)) != 0)