-
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.
- Loading branch information
Torsten Egenolf
committed
Jun 3, 2024
1 parent
3b8a5cb
commit d2ca906
Showing
5 changed files
with
145 additions
and
29 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,64 @@ | ||
# How to setup signing material for DID Signing | ||
|
||
KDS is able to provide a DID-Document holding the downloaded keys. The DID-Document will be signed by a private key provided in a KeyStore. | ||
|
||
Generate Private Key (Choose another Curve depending your needs) | ||
|
||
``` | ||
openssl ecparam -name prime256v1 -genkey -noout -out did-signer.pem | ||
``` | ||
|
||
Convert PEM-File to KeyStore | ||
|
||
``` | ||
openssl pkcs12 -export -out did-signer.p12 -inkey did-signer.pem -nocerts -passout pass:secure-password -name did-signer | ||
``` | ||
|
||
This will result in a KeyStore (P12) containing the previously generated private key stored with alias "did-signer" and secured with password "secure-password" | ||
|
||
```yaml | ||
dgc: | ||
did: | ||
didSigningProvider: local-keystore | ||
localKeyStore: | ||
alias: did-signer | ||
password: secure-password | ||
path: ./certs/did-signer.p12 | ||
``` | ||
## How to publish corresponding public key for verification of DID signature | ||
Generate the public key of the did singer | ||
``` | ||
openssl ec -in did-signer.pem -pubout -out did-signer-public-key.pem | ||
``` | ||
|
||
Adapt the following environment variables to your needs and generate a did document for your public key. | ||
|
||
| Environment Variable | Description | | ||
| --- | --- | | ||
| `PUBLIC_KEY_FILE` | Path to the public key file (e.g., "./did-signer-public-key.pem") | | ||
| `DID_ID` | Identifier for the DID (e.g., "did:web:raw.githubusercontent.com:WorldHealthOrganization:tng-participants-dev:main:WHO:signing:DID") | | ||
| `DID_CONTROLLER` | Controller for the DID (e.g., "did:web:raw.githubusercontent.com:WorldHealthOrganization:tng-participants-dev:main:WHO:signing:DID") | | ||
|
||
``` | ||
export PUBLIC_KEY_FILE="./did-signer-public-key.pem" | ||
export DID_ID="did:web:raw.githubusercontent.com:WorldHealthOrganization:tng-participants-dev:main:WHO:signing:DID" | ||
export DID_CONTROLLER="did:web:raw.githubusercontent.com:WorldHealthOrganization:tng-participants-dev:main:WHO:signing:DID" | ||
python generate_did_document.py | ||
``` | ||
|
||
Place the generated DID to it's intended location on a host corresponding to the DID ID as defined by [did:web method specification](https://w3c-ccg.github.io/did-method-web/). | ||
|
||
## How to update the did-signer in the environment | ||
|
||
``` | ||
kubectl create secret generic did-signer-secret --dry-run=client --namespace=kds -o yaml --from-file=did-signer.p12 > did-signer-secret.yaml | ||
``` | ||
|
||
Connected to the correct kubernetes context deploy the generated secret | ||
|
||
```(shell) | ||
kubectl apply -f did-signer-secret.yaml | ||
``` |
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,64 @@ | ||
import json | ||
import base64 | ||
import os | ||
from cryptography.hazmat.primitives import serialization | ||
from cryptography.hazmat.primitives.asymmetric import ec | ||
from cryptography.hazmat.backends import default_backend | ||
|
||
# Get the path to the public key file, did-id and did-controller from the environment variables | ||
public_key_file = os.getenv('PUBLIC_KEY_FILE') | ||
did_id = os.getenv('DID_ID') | ||
did_controller = os.getenv('DID_CONTROLLER') | ||
|
||
# Read the public key from the file | ||
with open(public_key_file, 'rb') as f: | ||
public_key_pem = f.read() | ||
|
||
# Load the public key | ||
public_key = serialization.load_pem_public_key(public_key_pem, backend=default_backend()) | ||
|
||
# Check if the public key is an elliptic curve public key | ||
if isinstance(public_key, ec.EllipticCurvePublicKey): | ||
# Get the x and y coordinates of the public key | ||
x = public_key.public_numbers().x | ||
y = public_key.public_numbers().y | ||
|
||
# Convert the x and y coordinates to base64url format | ||
x = base64.urlsafe_b64encode(x.to_bytes((x.bit_length() + 7) // 8, 'big')).decode().rstrip('=') | ||
y = base64.urlsafe_b64encode(y.to_bytes((y.bit_length() + 7) // 8, 'big')).decode().rstrip('=') | ||
|
||
# Convert the public key to PEM format and encode it in base64 | ||
public_key_pem = public_key.public_bytes( | ||
encoding=serialization.Encoding.PEM, | ||
format=serialization.PublicFormat.SubjectPublicKeyInfo | ||
) | ||
public_key_pem_b64 = base64.b64encode(public_key_pem).decode() | ||
|
||
did_document = { | ||
"@context": [ | ||
"https://www.w3.org/ns/did/v1", | ||
"https://w3id.org/security/suites/jws-2020/v1" | ||
], | ||
"id": did_id, | ||
"controller": did_controller, | ||
"verificationMethod": [ | ||
{ | ||
"id": did_id + "#%2FrcyDdJNU%2FA%3D", | ||
"type": "JsonWebKey2020", | ||
"controller": did_controller, | ||
"publicKeyJwk": { | ||
"kty": "EC", | ||
"x5c": [ | ||
public_key_pem_b64 | ||
], | ||
"crv": "P-256", | ||
"x": x, | ||
"y": y | ||
} | ||
} | ||
] | ||
} | ||
|
||
# Write the DID document to a file | ||
with open('did.json', 'w') as f: | ||
json.dump(did_document, f, indent=4) |
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