Skip to content

Commit 22d5836

Browse files
committed
Skip trailing newlines in pin-source files
Text editors usually append '\n' to the last line when saving a text file. Also 'echo "mypin" > ~/pinfile.txt' appends a newline. It's therefore likely we encounter PIN files where the PIN is delimited with '\n'. Currently, PIN validation would fail in such a case since libp11 passes on the newline to PKCS#11 modules as if it was part of the PIN. We now ignore trailing newlines. There's no specification mandating this, but since PINs are meant for interactive input it seems safe to assume PINs will never be allowed to contain a trailing newline. Further, the pkcs11-provider project is doing the same in their src/util.c:get_pin_file. The change is backwards compatible. PIN files without trailing newline will work as well.
1 parent 8b89cad commit 22d5836

File tree

1 file changed

+13
-1
lines changed

1 file changed

+13
-1
lines changed

src/util_uri.c

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -734,11 +734,23 @@ static int read_from_file(UTIL_CTX *ctx,
734734
return 0;
735735
}
736736
if (BIO_gets(fp, txt, (int)*field_len + 1) > 0) {
737-
memcpy(field, txt, *field_len);
738737
*field_len = strlen(txt);
739738
} else {
740739
*field_len = 0;
740+
goto done;
741741
}
742+
743+
/* files may contain trailing newlines, remove them */
744+
while (*field_len > 0) {
745+
if (txt[*field_len - 1] == '\n' || txt[*field_len - 1] == '\r') {
746+
(*field_len)--;
747+
} else {
748+
break;
749+
}
750+
}
751+
memcpy(field, txt, *field_len);
752+
753+
done:
742754
OPENSSL_free(txt);
743755

744756
BIO_free(fp);

0 commit comments

Comments
 (0)