Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use Builder Pattern for HsmKeyParams #155

Closed
wants to merge 9 commits into from
11 changes: 5 additions & 6 deletions src/hsm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -188,8 +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::Builder().setCkaExtractable(false).setCkaSensitive(true).build();
HsmKeyParams hsmKeyParams = HsmKeyParams::Builder{}.setExtractable(false).build();
return generateKey(spec, keyLabel, keyID, hsmKeyParams);
}

Expand Down Expand Up @@ -217,7 +216,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 @@ -234,8 +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::Builder().setCkaExtractable(false).setCkaSensitive(true).build();
HsmKeyParams hsmKeyParams = HsmKeyParams::Builder{}.setExtractable(false).build();
return generateKey(spec, keyLabel, keyID, hsmKeyParams);
}

Expand Down Expand Up @@ -263,8 +261,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
19 changes: 5 additions & 14 deletions src/mococrw/hsm.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,34 +34,25 @@ class HsmKeyParams
public:
class Builder;

bool isExtractable() const { return cka_extractable; }

bool isSensitive() const { return cka_sensitive; }
bool isExtractable() const { return extractable; }

private:
bool cka_extractable;
bool cka_sensitive;
bool extractable;
cps-b marked this conversation as resolved.
Show resolved Hide resolved

/* 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() : 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
Loading