-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Besu plugin that provides a PKCS11 based security module (#9)
* build: Add hyperledger jfrog maven repository for Besu * doc: Update README with build instructions * build: Add besu plugin dependency * feat: Implement BesuPlugin interface * feat: Add cli option and security module * feat: Pkcs11 Security Module Service implementation * chore: Throw SecurityModuleException
- Loading branch information
1 parent
4184b33
commit 9f8b4d5
Showing
11 changed files
with
472 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
5 changes: 0 additions & 5 deletions
5
src/main/java/info/usmans/besu/plugin/softhsm/BesuHSMPlugin.java
This file was deleted.
Oops, something went wrong.
67 changes: 67 additions & 0 deletions
67
src/main/java/info/usmans/besu/plugin/softhsm/Pkcs11HsmPlugin.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
// Copyright 2024, Usman Saleem. | ||
// SPDX-License-Identifier: (Apache-2.0 OR MIT) | ||
package info.usmans.besu.plugin.softhsm; | ||
|
||
import com.google.auto.service.AutoService; | ||
import org.hyperledger.besu.plugin.BesuContext; | ||
import org.hyperledger.besu.plugin.BesuPlugin; | ||
import org.hyperledger.besu.plugin.services.PicoCLIOptions; | ||
import org.hyperledger.besu.plugin.services.SecurityModuleService; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
/** | ||
* A Besu plugin that provides a custom security module to load the node key from an HSM using | ||
* PKCS11 libraries. | ||
*/ | ||
@AutoService(BesuPlugin.class) | ||
public class Pkcs11HsmPlugin implements BesuPlugin { | ||
static final String SECURITY_MODULE_NAME = "pkcs11-hsm"; | ||
private static final Logger LOG = LoggerFactory.getLogger(Pkcs11HsmPlugin.class); | ||
private final Pkcs11PluginCliOptions cliParams = new Pkcs11PluginCliOptions(); | ||
|
||
@Override | ||
public void register(final BesuContext besuContext) { | ||
LOG.info("Registering plugin ..."); | ||
registerCliOptions(besuContext); | ||
registerSecurityModule(besuContext); | ||
} | ||
|
||
/** | ||
* Registers {@code Pkcs11PluginCliOptions} with {@code PicoCLIOptions} service provided by {@code | ||
* BesuContext}. | ||
* | ||
* @param besuContext An instance of {@code BesuContext} | ||
*/ | ||
private void registerCliOptions(final BesuContext besuContext) { | ||
besuContext | ||
.getService(PicoCLIOptions.class) | ||
.orElseThrow(() -> new IllegalStateException("Expecting PicoCLIOptions to be present")) | ||
.addPicoCLIOptions(SECURITY_MODULE_NAME, cliParams); | ||
} | ||
|
||
/** | ||
* Registers {@code Pkcs11SecurityModule} with the {@code SecurityModuleService} service provided | ||
* by {@code BesuContext}. | ||
* | ||
* @param besuContext An instance of {@code BesuContext} | ||
*/ | ||
private void registerSecurityModule(final BesuContext besuContext) { | ||
// lazy-init our security module implementation during register phase | ||
besuContext | ||
.getService(SecurityModuleService.class) | ||
.orElseThrow( | ||
() -> new IllegalStateException("Expecting SecurityModuleService to be present")) | ||
.register(SECURITY_MODULE_NAME, () -> new Pkcs11SecurityModuleService(cliParams)); | ||
} | ||
|
||
@Override | ||
public void start() { | ||
LOG.debug("Starting plugin ..."); | ||
} | ||
|
||
@Override | ||
public void stop() { | ||
LOG.debug("Stopping plugin ..."); | ||
} | ||
} |
71 changes: 71 additions & 0 deletions
71
src/main/java/info/usmans/besu/plugin/softhsm/Pkcs11PluginCliOptions.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
// Copyright 2024, Usman Saleem. | ||
// SPDX-License-Identifier: (Apache-2.0 OR MIT) | ||
package info.usmans.besu.plugin.softhsm; | ||
|
||
import static info.usmans.besu.plugin.softhsm.Pkcs11HsmPlugin.SECURITY_MODULE_NAME; | ||
|
||
import java.nio.file.Path; | ||
import picocli.CommandLine.Option; | ||
|
||
/** Represents cli options that are required by the Besu PKCS11-SoftHSM plugin. */ | ||
public class Pkcs11PluginCliOptions { | ||
@Option( | ||
names = "--plugin-" + SECURITY_MODULE_NAME + "-config-path", | ||
description = "Path to the PKCS11 configuration file", | ||
required = true, | ||
paramLabel = "<path>") | ||
private Path pkcs11ConfigPath; | ||
|
||
@Option( | ||
names = "--plugin-" + SECURITY_MODULE_NAME + "-password-path", | ||
description = "Path to the file that contains password or PIN to access PKCS11 token", | ||
required = true, | ||
paramLabel = "<path>") | ||
private Path pkcs11PasswordPath; | ||
|
||
@Option( | ||
names = "--plugin-" + SECURITY_MODULE_NAME + "-key-alias", | ||
description = "Alias or label of the private key that is stored in the HSM", | ||
required = true, | ||
paramLabel = "<path>") | ||
private String privateKeyAlias; | ||
|
||
/** Default constructor. Performs no initialization. */ | ||
public Pkcs11PluginCliOptions() {} | ||
|
||
/** | ||
* Constructor that initializes the PKCS11 configuration file path. | ||
* | ||
* @param pkcs11ConfigPath the path to the PKCS11 configuration file | ||
*/ | ||
public Pkcs11PluginCliOptions(final Path pkcs11ConfigPath) { | ||
this.pkcs11ConfigPath = pkcs11ConfigPath; | ||
} | ||
|
||
/** | ||
* Returns the path to the PKCS11 configuration file. | ||
* | ||
* @return the path to the PKCS11 configuration file | ||
*/ | ||
public Path getPkcs11ConfigPath() { | ||
return pkcs11ConfigPath; | ||
} | ||
|
||
/** | ||
* Returns the path to the file that contains the password or PIN to access the PKCS11 token. | ||
* | ||
* @return the path to the file that contains the password or PIN to access the PKCS11 token | ||
*/ | ||
public Path getPkcs11PasswordPath() { | ||
return pkcs11PasswordPath; | ||
} | ||
|
||
/** | ||
* Returns the alias or label of the private key that is stored in the HSM. | ||
* | ||
* @return the alias or label of the private key that is stored in the HSM | ||
*/ | ||
public String getPrivateKeyAlias() { | ||
return privateKeyAlias; | ||
} | ||
} |
Oops, something went wrong.