Skip to content

Commit

Permalink
Merge pull request #747 from ijsf/fix_aes_import
Browse files Browse the repository at this point in the history
Fixes AES secret key import failing on newline characters
  • Loading branch information
jschlyter authored Dec 2, 2024
2 parents a181dae + 90aba38 commit 6d75f15
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 6 deletions.
8 changes: 5 additions & 3 deletions src/bin/util/softhsm2-util-botan.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,15 +68,17 @@ int crypto_import_aes_key
size_t objIDLen
)
{
const size_t cMaxAesKeySize = 1024 + 1; // including null-character
const size_t cMaxAesKeySize = 1024;
char aesKeyValue[cMaxAesKeySize];
size_t aesKeyLength = 0;
FILE* fp = fopen(filePath, "rb");
if (fp == NULL)
{
fprintf(stderr, "ERROR: Could not open the secret key file.\n");
return 1;
}
if (fgets(aesKeyValue, cMaxAesKeySize, fp) == NULL)
aesKeyLength = fread(aesKeyValue, 1, cMaxAesKeySize, fp);
if (aesKeyLength == 0)
{
fprintf(stderr, "ERROR: Could not read the secret key file.\n");
fclose(fp);
Expand All @@ -96,7 +98,7 @@ int crypto_import_aes_key
{ CKA_ENCRYPT, &ckTrue, sizeof(ckTrue) },
{ CKA_DECRYPT, &ckTrue, sizeof(ckTrue) },
{ CKA_SENSITIVE, &ckTrue, sizeof(ckTrue) },
{ CKA_VALUE, &aesKeyValue, strlen(aesKeyValue) }
{ CKA_VALUE, &aesKeyValue, aesKeyLength }
};

CK_OBJECT_HANDLE hKey;
Expand Down
8 changes: 5 additions & 3 deletions src/bin/util/softhsm2-util-ossl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,15 +81,17 @@ int crypto_import_aes_key
size_t objIDLen
)
{
const size_t cMaxAesKeySize = 1024 + 1; // including null-character
const size_t cMaxAesKeySize = 1024;
char aesKeyValue[cMaxAesKeySize];
size_t aesKeyLength = 0;
FILE* fp = fopen(filePath, "rb");
if (fp == NULL)
{
fprintf(stderr, "ERROR: Could not open the secret key file.\n");
return 1;
}
if (fgets(aesKeyValue, cMaxAesKeySize, fp) == NULL)
aesKeyLength = fread(aesKeyValue, 1, cMaxAesKeySize, fp);
if (aesKeyLength == 0)
{
fprintf(stderr, "ERROR: Could not read the secret key file.\n");
fclose(fp);
Expand All @@ -109,7 +111,7 @@ int crypto_import_aes_key
{ CKA_ENCRYPT, &ckTrue, sizeof(ckTrue) },
{ CKA_DECRYPT, &ckTrue, sizeof(ckTrue) },
{ CKA_SENSITIVE, &ckTrue, sizeof(ckTrue) },
{ CKA_VALUE, &aesKeyValue, strlen(aesKeyValue) }
{ CKA_VALUE, &aesKeyValue, aesKeyLength }
};

CK_OBJECT_HANDLE hKey;
Expand Down

0 comments on commit 6d75f15

Please sign in to comment.