Skip to content

Commit 9255a6d

Browse files
committed
Fixes AES secret key import failing on newline characters
1 parent 913e7bf commit 9255a6d

File tree

2 files changed

+10
-6
lines changed

2 files changed

+10
-6
lines changed

src/bin/util/softhsm2-util-botan.cpp

+5-3
Original file line numberDiff line numberDiff line change
@@ -68,15 +68,17 @@ int crypto_import_aes_key
6868
size_t objIDLen
6969
)
7070
{
71-
const size_t cMaxAesKeySize = 1024 + 1; // including null-character
71+
const size_t cMaxAesKeySize = 1024;
7272
char aesKeyValue[cMaxAesKeySize];
73+
size_t aesKeyLength = 0;
7374
FILE* fp = fopen(filePath, "rb");
7475
if (fp == NULL)
7576
{
7677
fprintf(stderr, "ERROR: Could not open the secret key file.\n");
7778
return 1;
7879
}
79-
if (fgets(aesKeyValue, cMaxAesKeySize, fp) == NULL)
80+
aesKeyLength = fread(aesKeyValue, 1, cMaxAesKeySize, fp);
81+
if (aesKeyLength == 0)
8082
{
8183
fprintf(stderr, "ERROR: Could not read the secret key file.\n");
8284
fclose(fp);
@@ -96,7 +98,7 @@ int crypto_import_aes_key
9698
{ CKA_ENCRYPT, &ckTrue, sizeof(ckTrue) },
9799
{ CKA_DECRYPT, &ckTrue, sizeof(ckTrue) },
98100
{ CKA_SENSITIVE, &ckTrue, sizeof(ckTrue) },
99-
{ CKA_VALUE, &aesKeyValue, strlen(aesKeyValue) }
101+
{ CKA_VALUE, &aesKeyValue, aesKeyLength }
100102
};
101103

102104
CK_OBJECT_HANDLE hKey;

src/bin/util/softhsm2-util-ossl.cpp

+5-3
Original file line numberDiff line numberDiff line change
@@ -81,15 +81,17 @@ int crypto_import_aes_key
8181
size_t objIDLen
8282
)
8383
{
84-
const size_t cMaxAesKeySize = 1024 + 1; // including null-character
84+
const size_t cMaxAesKeySize = 1024;
8585
char aesKeyValue[cMaxAesKeySize];
86+
size_t aesKeyLength = 0;
8687
FILE* fp = fopen(filePath, "rb");
8788
if (fp == NULL)
8889
{
8990
fprintf(stderr, "ERROR: Could not open the secret key file.\n");
9091
return 1;
9192
}
92-
if (fgets(aesKeyValue, cMaxAesKeySize, fp) == NULL)
93+
aesKeyLength = fread(aesKeyValue, 1, cMaxAesKeySize, fp);
94+
if (aesKeyLength == 0)
9395
{
9496
fprintf(stderr, "ERROR: Could not read the secret key file.\n");
9597
fclose(fp);
@@ -109,7 +111,7 @@ int crypto_import_aes_key
109111
{ CKA_ENCRYPT, &ckTrue, sizeof(ckTrue) },
110112
{ CKA_DECRYPT, &ckTrue, sizeof(ckTrue) },
111113
{ CKA_SENSITIVE, &ckTrue, sizeof(ckTrue) },
112-
{ CKA_VALUE, &aesKeyValue, strlen(aesKeyValue) }
114+
{ CKA_VALUE, &aesKeyValue, aesKeyLength }
113115
};
114116

115117
CK_OBJECT_HANDLE hKey;

0 commit comments

Comments
 (0)