From 90aba385aea05d591c8f576156714bfe437fa5ea Mon Sep 17 00:00:00 2001 From: Cecill Etheredge Date: Tue, 30 Apr 2024 23:02:46 +0200 Subject: [PATCH] Fixes AES secret key import failing on newline characters --- src/bin/util/softhsm2-util-botan.cpp | 8 +++++--- src/bin/util/softhsm2-util-ossl.cpp | 8 +++++--- 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/src/bin/util/softhsm2-util-botan.cpp b/src/bin/util/softhsm2-util-botan.cpp index 790f5b3da..9e92eb34b 100644 --- a/src/bin/util/softhsm2-util-botan.cpp +++ b/src/bin/util/softhsm2-util-botan.cpp @@ -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); @@ -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; diff --git a/src/bin/util/softhsm2-util-ossl.cpp b/src/bin/util/softhsm2-util-ossl.cpp index 83c6ac1c6..b839d3f0a 100644 --- a/src/bin/util/softhsm2-util-ossl.cpp +++ b/src/bin/util/softhsm2-util-ossl.cpp @@ -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); @@ -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;