Skip to content

Commit

Permalink
Use Builder Pattern for HsmKeyParams (#156)
Browse files Browse the repository at this point in the history
Use Builder Pattern for HsmKeyParams
  • Loading branch information
cps-b authored Nov 13, 2023
1 parent 8e2036c commit 0169669
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 14 deletions.
13 changes: 7 additions & 6 deletions src/hsm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ openssl::SSL_EVP_PKEY_Ptr HsmEngine::generateKey(const RSASpec &spec,
const std::string &keyLabel,
const std::vector<uint8_t> &keyID)
{
HsmKeyParams hsmKeyParams;
HsmKeyParams hsmKeyParams = HsmKeyParams::Builder{}.setExtractable(false).build();
return generateKey(spec, keyLabel, keyID, hsmKeyParams);
}

Expand All @@ -215,8 +215,8 @@ openssl::SSL_EVP_PKEY_Ptr HsmEngine::generateKey(const RSASpec &spec,
pkcs11RSASpec.bits = spec.numberOfBits();

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

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

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

PKCS11_params _params;
_params.extractable = static_cast<unsigned char>(params.cka_extractable);
_params.sensitive = static_cast<unsigned char>(params.cka_sensitive);
// 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.isExtractable());

PKCS11_KGEN_ATTRS pkcs11ECCKeygen;
pkcs11ECCKeygen.type = EVP_PKEY_EC;
Expand Down
31 changes: 27 additions & 4 deletions src/mococrw/hsm.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,40 @@ class ECCSpec;
class RSASpec;

/**
* This struct currently contains PKCS#11 attributes which are changeable on key creation.
* This class currently contains PKCS#11 attributes which are changeable on key creation.
* In the future also parameters for other keystorage interfaces can be added.
*/
struct HsmKeyParams
class HsmKeyParams
{
public:
class Builder;

bool isExtractable() const { return _extractable; }

private:
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.
*/
bool cka_extractable = false;
bool cka_sensitive = true;
HsmKeyParams() : _extractable(false) {}
};

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

HsmKeyParams build() { return params_; }

private:
HsmKeyParams params_;
};

/**
Expand Down
6 changes: 3 additions & 3 deletions tests/integration/hsm-integration-test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -452,9 +452,9 @@ int main(void)
/**
* Generate extractable and non-extractable keys for ECC and RSA
*/
HsmKeyParams hsmKeyParamsExtract = {/*.cka_extractable =*/true,
/* .cka_sensitive = */ false};
HsmKeyParams hsmKeyParamsDefault;
HsmKeyParams hsmKeyParamsExtract = HsmKeyParams::Builder{}.setExtractable(true).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 @@ -153,7 +153,7 @@ TEST_F(HSMTest, testHSMKeygenWithParams)
auto hsm = initialiseEngine();
std::string keyLabel{"key-label"};
std::vector<uint8_t> keyId{0x12};
HsmKeyParams params{true, false};
HsmKeyParams params = 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 0169669

Please sign in to comment.