|
import base64
|
|
from typing import List
|
|
|
|
import asn1
|
|
from nacl.signing import SigningKey, VerifyKey
|
|
from ndn.app_support.security_v2 import self_sign
|
|
from ndn.encoding import (KeyLocator, NonStrictName, SignatureType, Signer,
|
|
VarBinaryStr)
|
|
|
|
|
|
class Ed25519Signer(Signer):
|
|
key_locator_name: NonStrictName
|
|
key: SigningKey
|
|
|
|
def __init__(self, key_locator_name: NonStrictName, key: SigningKey):
|
|
self.key_locator_name = key_locator_name
|
|
self.key = key
|
|
|
|
def write_signature_info(self, signature_info):
|
|
signature_info.signature_type = 0x05
|
|
signature_info.key_locator = KeyLocator()
|
|
signature_info.key_locator.name = self.key_locator_name
|
|
|
|
def get_signature_value_size(self):
|
|
return 64
|
|
|
|
def write_signature_value(self, wire: VarBinaryStr, contents: List[VarBinaryStr]) -> int:
|
|
signed = self.key.sign(b''.join(contents))
|
|
wire[:] = signed.signature
|
|
return len(signed.signature)
|
|
|
|
|
|
def encode_spki(key: VerifyKey) -> bytes:
|
|
encoder = asn1.Encoder()
|
|
encoder.start()
|
|
encoder.enter(asn1.Numbers.Sequence) # SubjectPublicKeyInfo
|
|
encoder.enter(asn1.Numbers.Sequence) # AlgorithmIdentifier
|
|
encoder.write('1.3.101.112', asn1.Numbers.ObjectIdentifier) # Algorithm
|
|
encoder.leave() # AlgorithmIdentifier
|
|
encoder.write(key.encode(), asn1.Numbers.BitString) # subjectPublicKey
|
|
encoder.leave() # SubjectPublicKeyInfo
|
|
return encoder.output()
|
|
|
|
|
|
signing_key = SigningKey.generate()
|
|
verify_key = signing_key.verify_key
|
|
|
|
signer = Ed25519Signer('/Ed25519-demo/KEY/5a615db7cf0603b5', signing_key)
|
|
|
|
public_key_der = encode_spki(verify_key)
|
|
|
|
cert_name, cert = self_sign(signer.key_locator_name, public_key_der, signer)
|
|
print(base64.b64encode(cert).decode())
|