|
2 | 2 | # Licensed under the MIT License.
|
3 | 3 | import pathlib
|
4 | 4 | import argparse
|
5 |
| -from typing import Optional |
| 5 | +from typing import Union, Optional |
6 | 6 |
|
7 | 7 | import cwt
|
8 | 8 | import pycose
|
9 | 9 | import pycose.headers
|
10 | 10 | import pycose.messages
|
11 | 11 | import pycose.keys.ec2
|
| 12 | +from cryptography.hazmat.primitives.serialization import Encoding, PublicFormat |
| 13 | +from cryptography.hazmat.primitives.serialization import load_pem_private_key |
| 14 | + |
| 15 | +# NOTE These are unmaintained but the |
| 16 | +# https://github.com/hashberg-io/multiformats stuff and base58 modules don't |
| 17 | +# produce the same results: |
| 18 | +# https://grotto-networking.com/blog/posts/DID_Key.html#bug-in-multibase-library |
| 19 | +import multibase |
| 20 | +import multicodec |
12 | 21 |
|
13 | 22 | # TODO jwcrypto is LGPLv3, is there another option with a permissive licence?
|
14 | 23 | import jwcrypto.jwk
|
15 | 24 |
|
| 25 | +from scitt_emulator.did_helpers import DID_KEY_METHOD, MULTICODEC_HEX_P384_PUBLIC_KEY |
| 26 | + |
16 | 27 |
|
17 | 28 | @pycose.headers.CoseHeaderAttribute.register_attribute()
|
18 | 29 | class CWTClaims(pycose.headers.CoseHeaderAttribute):
|
@@ -40,7 +51,7 @@ class TBD(pycose.headers.CoseHeaderAttribute):
|
40 | 51 |
|
41 | 52 | def create_claim(
|
42 | 53 | claim_path: pathlib.Path,
|
43 |
| - issuer: str, |
| 54 | + issuer: Union[str, None], |
44 | 55 | subject: str,
|
45 | 56 | content_type: str,
|
46 | 57 | payload: str,
|
@@ -91,6 +102,23 @@ def create_claim(
|
91 | 102 | cwt_cose_key_to_cose_key = cwt_cose_key.to_dict()
|
92 | 103 | sign1_message_key = pycose.keys.ec2.EC2Key.from_dict(cwt_cose_key_to_cose_key)
|
93 | 104 |
|
| 105 | + # If issuer was not given used did:key of public key |
| 106 | + if issuer is None: |
| 107 | + multicodec_prefix_p_384 = "p384-pub" |
| 108 | + multicodec.constants.NAME_TABLE[multicodec_prefix_p_384] = MULTICODEC_HEX_P384_PUBLIC_KEY |
| 109 | + issuer = ( |
| 110 | + DID_KEY_METHOD |
| 111 | + + multibase.encode( |
| 112 | + "base58btc", |
| 113 | + multicodec.add_prefix( |
| 114 | + multicodec_prefix_p_384, |
| 115 | + load_pem_private_key(key_as_pem_bytes, password=None) |
| 116 | + .public_key() |
| 117 | + .public_bytes(Encoding.X962, PublicFormat.CompressedPoint), |
| 118 | + ), |
| 119 | + ).decode() |
| 120 | + ) |
| 121 | + |
94 | 122 | # CWT_Claims (label: 14 pending [CWT_CLAIM_COSE]): A CWT representing
|
95 | 123 | # the Issuer (iss) making the statement, and the Subject (sub) to
|
96 | 124 | # correlate a collection of statements about an Artifact. Additional
|
@@ -163,7 +191,7 @@ def create_claim(
|
163 | 191 | def cli(fn):
|
164 | 192 | p = fn("create-claim", description="Create a fake SCITT claim")
|
165 | 193 | p.add_argument("--out", required=True, type=pathlib.Path)
|
166 |
| - p.add_argument("--issuer", required=True, type=str) |
| 194 | + p.add_argument("--issuer", required=False, type=str, default=None) |
167 | 195 | p.add_argument("--subject", required=True, type=str)
|
168 | 196 | p.add_argument("--content-type", required=True, type=str)
|
169 | 197 | p.add_argument("--payload", required=True, type=str)
|
|
0 commit comments