-
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: Add Dockerfile to test the plugin. (#10)
* Dockerfile to test the plugin * Use constant for SECP256K1 curve * cleanup docs
- Loading branch information
1 parent
9f8b4d5
commit f74b98d
Showing
14 changed files
with
321 additions
and
28 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
# Copyright 2024, Usman Saleem. | ||
# SPDX-License-Identifier: (Apache-2.0 OR MIT) | ||
|
||
# Exclude everything | ||
* | ||
|
||
# Include specific files and directories needed for the build | ||
!docker/scripts/entrypoint.sh | ||
!Dockerfile | ||
!build/libs/ |
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 |
---|---|---|
|
@@ -8,3 +8,7 @@ | |
build | ||
|
||
.idea | ||
|
||
# Ignore data and tokens in volume directory | ||
docker/volumes/data | ||
docker/volumes/tokens |
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,44 @@ | ||
# syntax=docker/dockerfile:1 | ||
# Copyright 2024, Usman Saleem. | ||
# SPDX-License-Identifier: (Apache-2.0 OR MIT) | ||
|
||
# Start from the latest Hyperledger Besu image | ||
FROM hyperledger/besu:latest | ||
|
||
# Switch to root to install packages | ||
USER 0 | ||
|
||
# Install additional packages for SoftHSM2 and OpenSC | ||
RUN apt-get update && \ | ||
apt-get install -y --no-install-recommends \ | ||
openssl \ | ||
libssl3 \ | ||
softhsm2 \ | ||
opensc \ | ||
gnutls-bin && \ | ||
apt-get clean && \ | ||
rm -rf /var/lib/apt/lists/* | ||
|
||
# Create a directory for SoftHSM2 tokens. This can be overridden using a volume mount to persist. | ||
RUN mkdir -p /var/lib/tokens && chmod 755 /var/lib/tokens && chown besu:besu /var/lib/tokens | ||
|
||
# Switch back to the besu user | ||
USER besu | ||
|
||
# Update workdir to Besu home directory | ||
WORKDIR /opt/besu | ||
|
||
# Set environment variables for SoftHSM2 configuration | ||
ENV SOFTHSM2_CONF=/opt/besu/softhsm2.conf | ||
|
||
# Copy the PKCS11 plugin JAR to the plugins directory | ||
COPY --chown=besu:besu ./build/libs/besu-pkcs11-plugin-*.jar ./plugins/ | ||
|
||
# Copy the initialization script | ||
COPY --chown=besu:besu --chmod=755 ./docker/scripts/entrypoint.sh ./entrypoint.sh | ||
|
||
# Create a custom SoftHSM2 configuration file in besu home directory | ||
RUN echo "directories.tokendir = /var/lib/tokens" > ./softhsm2.conf | ||
|
||
# Set the entrypoint to our new script | ||
ENTRYPOINT ["/opt/besu/entrypoint.sh"] |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
#! /bin/sh | ||
rm -rf ./volumes/data | ||
rm -rf ./volumes/tokens |
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,88 @@ | ||
#!/bin/bash | ||
# Copyright 2024, Usman Saleem. | ||
# SPDX-License-Identifier: (Apache-2.0 OR MIT) | ||
|
||
# Set default values for PIN and SO_PIN | ||
DEFAULT_PIN="test123" | ||
DEFAULT_SO_PIN="sotest123" | ||
|
||
# Path to the PIN file | ||
PIN_FILE="/etc/besu/config/pkcs11-hsm-password.txt" | ||
|
||
# Read PIN from file if it exists, otherwise use environment variable or default value | ||
if [ -f "$PIN_FILE" ]; then | ||
PIN=$(cat "$PIN_FILE") | ||
else | ||
PIN="${PIN:-$DEFAULT_PIN}" | ||
fi | ||
|
||
# Use environment variables if set, otherwise use default values | ||
SO_PIN="${SO_PIN:-$DEFAULT_SO_PIN}" | ||
|
||
# Set up cleanup trap | ||
trap 'rm -f /tmp/ec-secp256k1-*.pem' EXIT | ||
|
||
# Check if SoftHSM module exists | ||
SOFTHSM_MODULE="/usr/lib/softhsm/libsofthsm2.so" | ||
if [ ! -f "$SOFTHSM_MODULE" ]; then | ||
echo "SoftHSM module not found: $SOFTHSM_MODULE" | ||
exit 1 | ||
fi | ||
|
||
# Check if token already exists | ||
if ! softhsm2-util --show-slots | grep -q "testtoken"; then | ||
echo "Initializing SoftHSM token ..." | ||
if ! softhsm2-util --init-token --slot 0 --label "testtoken" --pin "$PIN" --so-pin "$SO_PIN"; then | ||
echo "Failed to initialize token" | ||
exit 1 | ||
fi | ||
|
||
echo "Generating SECP256K1 private key using openssl ..." | ||
# Generating temporary SECP256K1 private key (-noout=not encoded) | ||
if ! openssl ecparam -name secp256k1 -genkey -noout -out /tmp/ec-secp256k1-priv-key.pem; then | ||
echo "Failed to generate private key" | ||
exit 1 | ||
fi | ||
|
||
# Generate public key from private key | ||
if ! openssl ec -in /tmp/ec-secp256k1-priv-key.pem -pubout -out /tmp/ec-secp256k1-pub-key.pem; then | ||
echo "Failed to generate public key" | ||
exit 1 | ||
fi | ||
|
||
# Generate a self-signed certificate | ||
if ! openssl req -new -x509 -key /tmp/ec-secp256k1-priv-key.pem -out /tmp/ec-secp256k1-cert.pem -days 365 -subj '/CN=example.com'; then | ||
echo "Failed to generate self-signed certificate" | ||
exit 1 | ||
fi | ||
|
||
echo "Importing openssl secp256k1 key into softhsm id: 1, label: testkey ..." | ||
# Importing private key and cert in softhsm. Note we have to specify --usage-derive for ECDH key agreement to work | ||
if ! pkcs11-tool --module "$SOFTHSM_MODULE" --login --pin "$PIN" \ | ||
--write-object /tmp/ec-secp256k1-priv-key.pem --type privkey --usage-derive --id 1 --label "testkey" \ | ||
--token-label "testtoken"; then | ||
echo "Failed to import private key" | ||
exit 1 | ||
fi | ||
|
||
if ! pkcs11-tool --module "$SOFTHSM_MODULE" --login --pin "$PIN" \ | ||
--write-object /tmp/ec-secp256k1-pub-key.pem --type pubkey --usage-derive --id 1 --label "testkey" \ | ||
--token-label "testtoken"; then | ||
echo "Failed to import public key" | ||
exit 1 | ||
fi | ||
|
||
if ! pkcs11-tool --module "$SOFTHSM_MODULE" --login --pin "$PIN" \ | ||
--write-object /tmp/ec-secp256k1-cert.pem --type cert --id 1 --label "testkey" \ | ||
--token-label "testtoken"; then | ||
echo "Failed to import certificate" | ||
exit 1 | ||
fi | ||
|
||
echo "Token and keys initialized successfully." | ||
else | ||
echo "Token already exists. Skipping initialization." | ||
fi | ||
|
||
# Launch Besu with the provided arguments | ||
exec besu "$@" |
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,19 @@ | ||
network="dev" | ||
miner-enabled=true | ||
miner-coinbase="0xfe3b557e8fb62b89f4916b721be55ceb828dbd73" | ||
rpc-http-cors-origins=["all"] | ||
host-allowlist=["*"] | ||
rpc-ws-enabled=true | ||
rpc-http-enabled=true | ||
data-path="/var/lib/besu" | ||
|
||
# plugins options | ||
plugin-pkcs11-hsm-config-path="/etc/besu/config/pkcs11-softhsm.cfg" | ||
plugin-pkcs11-hsm-key-alias="testkey" | ||
plugin-pkcs11-hsm-password-path="/etc/besu/config/pkcs11-hsm-password.txt" | ||
|
||
# security module | ||
security-module="pkcs11-hsm" | ||
|
||
# Logging | ||
logging="DEBUG" |
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 @@ | ||
1234 |
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,11 @@ | ||
name = Softhsm-Besu-SM | ||
library = /usr/lib/softhsm/libsofthsm2.so | ||
# Instead of slot = xxx, use slotListIndex | ||
slotListIndex = 0 | ||
showInfo = false | ||
|
||
# In order for ECDHA Key Agreement to work, we need following for derived secrets | ||
attributes(generate,CKO_SECRET_KEY,CKK_GENERIC_SECRET) = { | ||
CKA_SENSITIVE = false | ||
CKA_EXTRACTABLE = true | ||
} |
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
Oops, something went wrong.