diff --git a/src/bin/util/softhsm2-util-botan.cpp b/src/bin/util/softhsm2-util-botan.cpp index 790f5b3d..9e92eb34 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 83c6ac1c..b839d3f0 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;