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

Refactor/pkcs11 integration #19

Merged
merged 26 commits into from
Dec 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
d844b4b
remove obsolete file
MGTheTrain Dec 14, 2024
e7ea202
refactor by distinguishing settings from inputs and checking non empt…
MGTheTrain Dec 14, 2024
b52ca3f
describe signatures
MGTheTrain Dec 14, 2024
8cf7497
add variadic helper function checking wether file exists
MGTheTrain Dec 14, 2024
6dbae76
add tests for validation functions
MGTheTrain Dec 14, 2024
b72e7c7
modify import of utils package
MGTheTrain Dec 14, 2024
f9299ec
make method privat and fix tests
MGTheTrain Dec 14, 2024
5d0cec9
implement list token and list objects methods
MGTheTrain Dec 14, 2024
a0a01a9
remove obsolete method
MGTheTrain Dec 14, 2024
9ca6f63
rename input parameters in Sign(...) method
MGTheTrain Dec 14, 2024
6118a15
remove obsolete io helper functions
MGTheTrain Dec 14, 2024
c2ca6b3
utilize settings object pointer
MGTheTrain Dec 14, 2024
90a3dde
order input parameters in Verify(...) method
MGTheTrain Dec 14, 2024
dd94728
fix cli tool
MGTheTrain Dec 14, 2024
c1856c8
rename struct
MGTheTrain Dec 14, 2024
0b8a617
consider slot id member attribute when listing tokens
MGTheTrain Dec 14, 2024
ebead2f
rename method
MGTheTrain Dec 14, 2024
323eab6
add list commands; update error handling
MGTheTrain Dec 14, 2024
0f3682c
rename files
MGTheTrain Dec 14, 2024
f8e3fbe
rename files
MGTheTrain Dec 14, 2024
962adc6
remove obsolete comments
MGTheTrain Dec 14, 2024
cf02dc5
consider consistent input argument naming convention
MGTheTrain Dec 14, 2024
d094deb
remove obsolete comments
MGTheTrain Dec 14, 2024
d0e9c73
consider returning json info or error responses
MGTheTrain Dec 14, 2024
72dbd26
fix linter finding
MGTheTrain Dec 15, 2024
05c758e
add action for shutting down containters
MGTheTrain Dec 15, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .github/workflows/dev.yml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ jobs:
run: sudo ./run-test.sh -i
working-directory: ./scripts

- name: Shut down external storage services
run: |
docker compose down -v

# Run static code analysis on source code
# Run vulnerability scanner and generate SBOMs on third part dependencies
# # Create build artifacts, e.g. Build docker image with dev tag for applications and push to container registry
4 changes: 4 additions & 0 deletions .github/workflows/pre-release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ jobs:
run: sudo ./run-test.sh -i
working-directory: ./scripts

- name: Shut down external storage services
run: |
docker compose down -v

# Run static code analysis on source code
# Run vulnerability scanner and generate SBOMs on third part dependencies
# Create build artifacts, e.g. Build docker image with pre-release tag for applications, scan docker image and push to container registry
4 changes: 4 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ jobs:
run: sudo ./run-test.sh -i
working-directory: ./scripts

- name: Shut down external storage services
run: |
docker compose down -v

# Run static code analysis on source code
# Run vulnerability scanner and generate SBOMs on third part dependencies
# Create build artifacts, e.g. Build docker image with release tag for applications, scan docker image and push to container registry
1 change: 1 addition & 0 deletions cmd/crypto-vault-cli/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pkcs11-settings.json
59 changes: 30 additions & 29 deletions cmd/crypto-vault-cli/README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# crypto-vault-cli
# crypto_vault_cli

## Table of Contents

Expand All @@ -15,7 +15,7 @@

## Summary

`crypto-vault-cli` is a command-line tool for file encryption and decryption using AES, RSA and EC algorithms. It provides an easy interface to securely encrypt and decrypt files using symmetric (AES) and asymmetric (RSA, EC) cryptography.
`crypto_vault_cli` is a command-line tool for file encryption and decryption using AES, RSA and EC algorithms. It provides an easy interface to securely encrypt and decrypt files using symmetric (AES) and asymmetric (RSA, EC) cryptography.

## Getting Started

Expand All @@ -28,9 +28,9 @@
```sh
uuid=$(cat /proc/sys/kernel/random/uuid)
# Encryption
go run crypto-vault-cli.go encrypt-aes --input data/input.txt --output data/${uuid}-output.enc --keySize 16 --keyDir data/
go run crypto_vault_cli.go encrypt-aes --input-file data/input.txt --output-file data/${uuid}-output.enc --key-size 16 --key-dir data/
# Decryption
go run crypto-vault-cli.go decrypt-aes --input data/${uuid}-output.enc --output data/${uuid}-decrypted.txt --symmetricKey <your generated symmetric key from previous encryption operation>
go run crypto_vault_cli.go decrypt-aes --input-file data/${uuid}-output.enc --output-file data/${uuid}-decrypted.txt --symmetric-key <your generated symmetric key from previous encryption operation>
```

#### RSA Example
Expand All @@ -41,10 +41,10 @@ go run crypto-vault-cli.go decrypt-aes --input data/${uuid}-output.enc --output
uuid=$(cat /proc/sys/kernel/random/uuid)

# Encryption
go run crypto-vault-cli.go encrypt-rsa --input data/input.txt --output data/${uuid}-encrypted.txt --keyDir data/
go run crypto_vault_cli.go encrypt-rsa --input-file data/input.txt --output-file data/${uuid}-encrypted.txt --key-dir data/

# Decryption
go run crypto-vault-cli.go decrypt-rsa --input data/${uuid}-encrypted.txt --output data/${uuid}-decrypted.txt --privateKey <your generated private key from previous encryption operation>
go run crypto_vault_cli.go decrypt-rsa --input-file data/${uuid}-encrypted.txt --output-file data/${uuid}-decrypted.txt --private-key <your generated private key from previous encryption operation>
```

#### PKCS#11 encryption and decryption
Expand All @@ -54,10 +54,10 @@ go run crypto-vault-cli.go decrypt-rsa --input data/${uuid}-encrypted.txt --outp
```sh
# RSA-PKCS
# Encryption
go run crypto-vault-cli.go encrypt --module /usr/lib/softhsm/libsofthsm2.so --token-label my-token --object-label my-rsa-key --user-pin 5678 --key-type RSA --input-file data/input.txt --output-file data/encrypted-output.enc
go run crypto_vault_cli.go encrypt --token-label my-token --object-label my-rsa-key --key-type RSA --input-file data/input.txt --output-file data/encrypted-output.enc

# Decryption
go run crypto-vault-cli.go decrypt --module /usr/lib/softhsm/libsofthsm2.so --token-label my-token --object-label my-rsa-key --user-pin 5678 --key-type RSA --input-file data/encrypted-output.enc --output-file data/decrypted-output.txt
go run crypto_vault_cli.go decrypt --token-label my-token --object-label my-rsa-key --key-type RSA --input-file data/encrypted-output.enc --output-file data/decrypted-output.txt
```

---
Expand All @@ -70,10 +70,10 @@ go run crypto-vault-cli.go decrypt --module /usr/lib/softhsm/libsofthsm2.so --to

```sh
# Sign a file with a newly generated ECC key pair (internally generated)
go run crypto-vault-cli.go sign-ecc --input data/input.txt --keyDir data
go run crypto_vault_cli.go sign-ecc --input-file data/input.txt --key-dir data

# Verify the signature using the generated public key
go run crypto-vault-cli.go verify-ecc --input data/input.txt --publicKey <your generated public key from previous signing operation> --signature <your generated signature file from previous signing operation>
go run crypto_vault_cli.go verify-ecc --input-file data/input.txt --public-key <your generated public key from previous signing operation> --signature-file <your generated signature file from previous signing operation>
```

#### PKCS#11 signing and verifying
Expand All @@ -83,43 +83,44 @@ go run crypto-vault-cli.go verify-ecc --input data/input.txt --publicKey <your g
```sh
# RSA-PSS
# Sign data with a PKCS#11 token
go run crypto-vault-cli.go sign --module /usr/lib/softhsm/libsofthsm2.so --token-label my-token --object-label my-rsa-key --user-pin 5678 --key-type RSA --input-file data/input.txt --output-file data/signature.sig
go run crypto_vault_cli.go sign --token-label my-token --object-label my-rsa-key --key-type RSA --data-file data/input.txt --signature-file data/signature.sig

# Verify the signature using the generated public key from the PKCS#11 token
go run crypto-vault-cli.go verify --module /usr/lib/softhsm/libsofthsm2.so --token-label my-token --object-label my-rsa-key --user-pin 5678 --key-type RSA --data-file data/input.txt --signature-file data/signature.sig
go run crypto_vault_cli.go verify --token-label my-token --object-label my-rsa-key --key-type RSA --data-file data/input.txt --signature-file data/signature.sig

# ECDSA
# Sign data with a PKCS#11 token
go run crypto-vault-cli.go sign --module /usr/lib/softhsm/libsofthsm2.so --token-label my-token --object-label my-ecdsa-key --user-pin 5678 --key-type ECDSA --input-file data/input.txt --output-file data/signature.sig
go run crypto_vault_cli.go sign --token-label my-token --object-label my-ecdsa-key --key-type ECDSA --data-file data/input.txt --signature-file data/signature.sig

# Verify the signature using the generated public key from the PKCS#11 token
go run crypto-vault-cli.go verify --module /usr/lib/softhsm/libsofthsm2.so --token-label my-token --object-label my-ecdsa-key --user-pin 5678 --key-type ECDSA --data-file data/input.txt --signature-file data/signature.sig
go run crypto_vault_cli.go verify --token-label my-token --object-label my-ecdsa-key --key-type ECDSA --data-file data/input.txt --signature-file data/signature.sig
```

---

### PKCS#11 key management operations

```sh
# Check available slots
pkcs11-tool --module /usr/lib/softhsm/libsofthsm2.so -L
# Initialize a PKCS#11 token
go run crypto-vault-cli.go initialize-token --module /usr/lib/softhsm/libsofthsm2.so --token-label my-token --so-pin 1234 --user-pin 5678 --slot "0x0"
# Configure settings
go run crypto_vault_cli.go store-pkcs11-settings --module /usr/lib/softhsm/libsofthsm2.so --so-pin 1234 --user-pin 5678 --slot-id "0x0"

# List token slots
go run crypto_vault_cli.go list-slots

# Check if PKCS#11 token is set
go run crypto-vault-cli.go is-token-set --module /usr/lib/softhsm/libsofthsm2.so --token-label my-token
# Initialize a PKCS#11 token
go run crypto_vault_cli.go initialize-token --token-label my-token

# Check if an object (e.g., key) exists in the PKCS#11 token
go run crypto-vault-cli.go is-object-set --module /usr/lib/softhsm/libsofthsm2.so --token-label my-token --object-label my-rsa-key --user-pin 5678
# Check all keys of a token
pkcs11-tool --module /usr/lib/softhsm/libsofthsm2.so -O --token-label "my-token" --pin 5678

# Adding keys to tokens
# Add an RSA or ECDSA key pair (private and public key) to a PKCS#11 token
go run crypto-vault-cli.go add-key --module /usr/lib/softhsm/libsofthsm2.so --token-label my-token --object-label my-rsa-key --key-type RSA --key-size 2048 --user-pin 5678
# Add an RSA or EC key pair (private and public key) to a PKCS#11 token
go run crypto_vault_cli.go add-key --token-label my-token --object-label my-rsa-key --key-type RSA --key-size 2048
go run crypto_vault_cli.go add-key --token-label my-token --object-label my-ecdsa-key --key-type ECDSA --key-size 256

# List token objects
go run crypto_vault_cli.go list-objects --token-label "my-token"

# Deleting keys from tokens
# Delete an object (e.g., RSA or ECDSA key) from the PKCS#11 token
go run crypto-vault-cli.go delete-object --module /usr/lib/softhsm/libsofthsm2.so --token-label my-token --object-label my-rsa-key --object-type pubkey --user-pin 5678
go run crypto-vault-cli.go delete-object --module /usr/lib/softhsm/libsofthsm2.so --token-label my-token --object-label my-rsa-key --object-type privkey --user-pin 5678
# Delete an object (e.g., RSA or EC key) from the PKCS#11 token
go run crypto_vault_cli.go delete-object --token-label my-token --object-label my-rsa-key --object-type pubkey
go run crypto_vault_cli.go delete-object --token-label my-token --object-label my-rsa-key --object-type privkey
```
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,14 @@ import (
func main() {
var rootCmd = &cobra.Command{Use: "crypto-vault-cli"}

// AES Commands
commands.InitAESCommands(rootCmd)

// RSA Commands
commands.InitRSACommands(rootCmd)

// ECDSA Commands
commands.InitECDSACommands(rootCmd)

// PKCS11 Token Commands
commands.InitPKCS11Commands(rootCmd)

// Execute the root command
if err := rootCmd.Execute(); err != nil {
fmt.Println(err)
os.Exit(1)
Expand Down
125 changes: 0 additions & 125 deletions cmd/crypto-vault-cli/internal/commands/aes-commands.go

This file was deleted.

Loading
Loading