A comprehensive Java Spring Boot application demonstrating enterprise-grade integration with Hyperledger Fabric blockchain network using Hardware Security Module (HSM) for enhanced security and key protection. This project serves as a production-ready reference implementation for organizations looking to build secure blockchain applications with cryptographic key protection.
This project demonstrates how to build a secure, enterprise-grade application that interacts with Hyperledger Fabric while protecting cryptographic keys using Hardware Security Module (HSM) technology. It addresses several critical concerns for organizations deploying blockchain solutions:
-
Security & Compliance: Demonstrates PKCS#11 integration with HSM for secure key storage, meeting regulatory requirements for financial and sensitive operations.
-
Identity Management: Implements comprehensive user and admin identity lifecycle management through Fabric CA, including registration, enrollment, and certificate revocation.
-
Enterprise Integration: Provides a production-ready RESTful API that can be integrated into existing enterprise applications and systems.
-
Extensibility: Built with a modular architecture that can be extended to support additional blockchain use cases and custom business logic.
-
Operational Model: Includes configuration externalization through application.yml, allowing for deployment across different environments without code changes.
- HSM Integration: Secure key management using Hardware Security Module (HSM) through PKCS#11 standard
- Certificate-Based Identity: Admin and user registration with certificate-based authentication and authorization
- RESTful API: Comprehensive API for interacting with Fabric chaincode and network operations
- CA Integration: Complete Certificate Authority (CA) operations for identity management
- Transaction Handling: Secure transaction submission, endorsement, and query capabilities
- Configurable Settings: Externalized configuration for all connection parameters, cryptographic settings, and network elements
- Java 11 or later
- Gradle 6.5+
- Docker and Docker Compose
- Hyperledger Fabric v2.2+ network setup
- SoftHSM or a hardware HSM device
hyperledger-fabric-hsm-springboot/
├── src/ # Source code
│ ├── main/java # Java source files
│ │ └── com/example/blockchain
│ │ ├── api/ # REST API controllers and DTOs
│ │ │ ├── controller/ # REST endpoint controllers
│ │ │ └── dto/ # Data transfer objects and responses
│ │ ├── config/ # Application configuration
│ │ │ ├── FabricConfig.java # Fabric network connectivity
│ │ │ ├── HsmConfig.java # HSM integration setup
│ │ │ └── SecurityConfig.java # API security settings
│ │ ├── exception/ # Custom exceptions and error handling
│ │ ├── service/ # Business logic implementation
│ │ │ ├── AdminService.java # Admin identity operations
│ │ │ ├── UserService.java # User management
│ │ │ └── TransactionService.java # Blockchain transactions
│ │ └── util/ # Utility classes for cryptography and helpers
│ └── resources/ # Application resources
│ └── application.yml # Externalized configuration
├── creds/ # Credentials storage (gitignored)
│ ├── certs/ # Certificate storage
│ │ └── *.pem # PEM-encoded certificates
│ ├── keys/ # Key storage (HSM-backed)
│ │ └── *.pem # PEM-encoded public keys
│ └── csrs/ # Certificate signing requests
├── org1.example.com/ # Fabric organization files and connection profiles
├── build/ # Build output directory
├── gradle/ # Gradle wrapper
├── Dockerfile # Docker build definition
├── build.gradle # Gradle build configuration
├── API-TESTING-GUIDE.md # Detailed API documentation
├── Fabric-Blockchain-API.postman_collection.json # Postman API collection
└── run-with-hsm.sh # Script to run with HSM
This application follows a multi-layered architecture:
- API Layer: RESTful controllers exposing blockchain operations to external systems
- Service Layer: Core business logic implementing blockchain interactions
- Configuration Layer: HSM and Fabric network connectivity setup
- Utility Layer: Cryptographic operations and helper functions
- HSM Integration: Uses the PKCS#11 standard through SunPKCS11 provider
- Identity Model: X.509 certificates with proper chain of trust through Fabric CA
- Transaction Security: Implements Fabric Gateway API with HSM-backed signatures for proposals, transactions and commits
- Signature Verification: Enforces canonical signature format to prevent signature malleability attacks
Ensure you have the necessary Fabric certificates and connection profiles in the org1.example.com directory. The application expects a standard Fabric network setup with at least:
- One organization (Org1) with properly configured MSP
- At least one peer with endorsing capability
- A Fabric CA server for identity management
- A deployed chaincode (default name: "basic")
Set up your Hardware Security Module:
# For SoftHSM (development/testing)
softhsm2-util --init-token --slot 0 --label "ForFabric" --pin 98765432
# For production HSM, follow your vendor's instructions and update the
# HSM configuration in application.yml accordinglyReview and update application.yml to match your environment:
# Hyperledger Fabric Network Configuration
fabric:
msp-id: Org1MSP
channel-name: mychannel
chaincode-name: basic
peer-endpoint: localhost:7051
ca-server-endpoint: https://localhost:7054
# HSM Configuration
hsm:
name: ForFabric
library: /usr/lib/x86_64-linux-gnu/softhsm/libsofthsm2.so
slot: 1681257824
pin: 98765432./gradlew clean build# Using HSM (recommended for production-like testing)
./run-with-hsm.sh
# Without HSM (for development only, not secure)
./gradlew bootRunThe application exposes a comprehensive REST API for blockchain operations. Refer to the API-TESTING-GUIDE.md or import the Postman collection Fabric-Blockchain-API.postman_collection.json for detailed API documentation.
- POST /blockchain-api/api/v1/blockchain/admin/register - Register a new admin with HSM-protected keys
- POST /blockchain-api/api/v1/blockchain/users/register - Register a new regular user with HSM-protected keys
- POST /blockchain-api/api/v1/blockchain/transactions - Submit a transaction to the blockchain
- POST /blockchain-api/api/v1/blockchain/query - Query the blockchain ledger
Register a user:
POST /blockchain-api/api/v1/blockchain/users/register
{
"userId": "6c46e42e-c5df-46b9-b26c-38d9cda72ca2"
}Submit a transaction:
POST /blockchain-api/api/v1/blockchain/transactions
Header: User-ID: 6c46e42e-c5df-46b9-b26c-38d9cda72ca2
{
"functionName": "CreateAsset",
"arguments": ["asset1", "blue", "5", "Tom", "100"]
}- HSM Key Protection: Private keys are generated and stored exclusively in the HSM, never exposed in memory
- Certificate-Based Identity: Strong X.509 certificate-based authentication for all blockchain operations
- Secure Credential Management: Double-checked locking pattern for thread-safe admin credential management
- CSR-based Enrollment: Secure enrollment using Certificate Signing Requests (CSR) model
- Signature Normalization: Implementation of low-S value signature normalization to prevent malleability attacks
- Configuration Externalization: All sensitive configuration stored in application.yml, not hardcoded
To interact with your custom chaincode, modify the chaincode name in application.yml and create appropriate transaction request DTOs reflecting your chaincode functions.
For production HSMs, replace the SoftHSM library path with your vendor-specific PKCS#11 library and update the slot configuration and credentials accordingly.
The application can connect to any properly configured Fabric network by updating the connection profiles and certificates in the org1.example.com directory.
Run the automated tests:
./gradlew testFor production deployments:
- Replace SoftHSM with a certified hardware HSM solution
- Implement proper user authentication and authorization for API access
- Configure TLS for the API endpoints
- Set up proper logging and monitoring solutions
- Consider implementing key rotation policies
Contributions are welcome! Please feel free to submit a Pull Request to the hyperledger-fabric-hsm-springboot repository.
This project is licensed under the Apache License 2.0 - see the LICENSE file for details.
Or use the clean tests script:
./clean-tests.shBuild and run as a Docker container:
docker build -t fabric-blockchain-sample .
docker run -p 8080:8080 --name blockchain-api fabric-blockchain-sample- Fork the repository
- Create a feature branch:
git checkout -b feature-name - Commit your changes:
git commit -am 'Add feature' - Push to the branch:
git push origin feature-name - Submit a pull request
This project is licensed under the MIT License.