Skip to content

Commit

Permalink
fixup! Use Builder Pattern for HsmKeyParams
Browse files Browse the repository at this point in the history
  • Loading branch information
cps-b committed Oct 31, 2023
1 parent c8078e4 commit 12c147b
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 20 deletions.
9 changes: 5 additions & 4 deletions src/hsm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ openssl::SSL_EVP_PKEY_Ptr HsmEngine::generateKey(const RSASpec &spec,
const std::vector<uint8_t> &keyID)
{
HsmKeyParams hsmKeyParams =
HsmKeyParams::Builder().setCkaExtractable(false).setCkaSensitive(true).build();
HsmKeyParams::Builder{}.setExtractable(false).build();
return generateKey(spec, keyLabel, keyID, hsmKeyParams);
}

Expand Down Expand Up @@ -217,7 +217,7 @@ openssl::SSL_EVP_PKEY_Ptr HsmEngine::generateKey(const RSASpec &spec,

PKCS11_params _params;
_params.extractable = static_cast<unsigned char>(params.isExtractable());
_params.sensitive = static_cast<unsigned char>(params.isSensitive());
_params.sensitive = static_cast<unsigned char>(!params.isExtractable());

PKCS11_KGEN_ATTRS pkcs11RSAKeygen;
pkcs11RSAKeygen.type = EVP_PKEY_RSA;
Expand All @@ -235,7 +235,7 @@ openssl::SSL_EVP_PKEY_Ptr HsmEngine::generateKey(const ECCSpec &spec,
const std::vector<uint8_t> &keyID)
{
HsmKeyParams hsmKeyParams =
HsmKeyParams::Builder().setCkaExtractable(false).setCkaSensitive(true).build();
HsmKeyParams::Builder{}.setExtractable(false).build();
return generateKey(spec, keyLabel, keyID, hsmKeyParams);
}

Expand Down Expand Up @@ -263,8 +263,9 @@ openssl::SSL_EVP_PKEY_Ptr HsmEngine::generateKey(const ECCSpec &spec,
pkcs11ECCSpec.curve = curve.c_str();

PKCS11_params _params;
// If the key is extractable it shouldn't be sensitive and vice versa
_params.extractable = static_cast<unsigned char>(params.isExtractable());
_params.sensitive = static_cast<unsigned char>(params.isSensitive());
_params.sensitive = static_cast<unsigned char>(!params.isExtractable());

PKCS11_KGEN_ATTRS pkcs11ECCKeygen;
pkcs11ECCKeygen.type = EVP_PKEY_EC;
Expand Down
17 changes: 4 additions & 13 deletions src/mococrw/hsm.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,32 +36,23 @@ class HsmKeyParams

bool isExtractable() const { return cka_extractable; }

bool isSensitive() const { return cka_sensitive; }

private:
bool cka_extractable;
bool cka_sensitive;
bool extractable;

/* Default is that the key cannot be extracted and is marked as sensitive.
* Check https://docs.oasis-open.org/pkcs11/pkcs11-base/v2.40/os/pkcs11-base-v2.40-os.html
* for more details.
*/
HsmKeyParams() : cka_extractable(false), cka_sensitive(true) {}
HsmKeyParams() : cka_extractable(false) {}
};

class HsmKeyParams::Builder
{
public:
Builder() {}
Builder &setCkaExtractable(bool extractable)
{
params_.cka_extractable = extractable;
return *this;
}

Builder &setCkaSensitive(bool sensitive)
Builder &setExtractable(bool extractable)
{
params_.cka_sensitive = sensitive;
params_.extractable = extractable;
return *this;
}

Expand Down
4 changes: 2 additions & 2 deletions tests/integration/hsm-integration-test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -453,9 +453,9 @@ int main(void)
* Generate extractable and non-extractable keys for ECC and RSA
*/
HsmKeyParams hsmKeyParamsExtract =
HsmKeyParams::Builder().setCkaExtractable(true).setCkaSensitive(false).build();
HsmKeyParams::Builder{}.setExtractable(true).build();

HsmKeyParams hsmKeyParamsDefault = HsmKeyParams::Builder().build();
HsmKeyParams hsmKeyParamsDefault = HsmKeyParams::Builder{}.build();

/* We need a new token otherwise the keys generated before litter the slot */

Expand Down
2 changes: 1 addition & 1 deletion tests/unit/test_hsm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ TEST_F(HSMTest, testHSMKeygenWithParams)
std::string keyLabel{"key-label"};
std::vector<uint8_t> keyId{0x12};
HsmKeyParams params =
HsmKeyParams::Builder().setCkaExtractable(true).setCkaSensitive(false).build();
HsmKeyParams::Builder{}.setExtractable(true).build();
EXPECT_CALL(_mock(),
SSL_ENGINE_ctrl_cmd_string(
engine, StrEq("PIN"), StrEq(pin.c_str()), 0 /*non-optional*/))
Expand Down

0 comments on commit 12c147b

Please sign in to comment.