|
| 1 | +""" |
| 2 | +<Program Name> |
| 3 | + signer.py |
| 4 | +
|
| 5 | +<Author> |
| 6 | + Martin Vrachev <[email protected]> |
| 7 | +
|
| 8 | +<Started> |
| 9 | + Januart 27, 2021. |
| 10 | +
|
| 11 | +<Copyright> |
| 12 | + See LICENSE for licensing information. |
| 13 | +
|
| 14 | +<Purpose> |
| 15 | + The goal of this module is to provide signing interface supporting multiple |
| 16 | + signing implementations. |
| 17 | +""" |
| 18 | + |
| 19 | +import abc |
| 20 | +import securesystemslib.keys as sslib_keys |
| 21 | + |
| 22 | +class Signature: |
| 23 | + """ |
| 24 | + <Purpose> |
| 25 | + Storage class containing information about a signature and metadata about |
| 26 | + the key used to generate the signature. |
| 27 | + The key metadata that is stored is needed for later verification for the |
| 28 | + signature. |
| 29 | +
|
| 30 | + <Attributes> |
| 31 | + keyid: HEX string used as a unique identifier of the key. |
| 32 | + signature: HEX string representing the signature. |
| 33 | + """ |
| 34 | + def __init__(self, keyid, signature): |
| 35 | + self.keyid = keyid |
| 36 | + self.signature = signature |
| 37 | + |
| 38 | + |
| 39 | + def to_dict(self): |
| 40 | + """ |
| 41 | + <Purpose> |
| 42 | + Returns the JSON-serializable dictionary representation of self. |
| 43 | + """ |
| 44 | + return { |
| 45 | + "keyid": self.keyid, |
| 46 | + "sig": self.signature |
| 47 | + } |
| 48 | + |
| 49 | + |
| 50 | + |
| 51 | +class Signer: |
| 52 | + """ |
| 53 | + <Purpose> |
| 54 | + Signer interface created to support multiple signing implementations. |
| 55 | + """ |
| 56 | + |
| 57 | + __metaclass__ = abc.ABCMeta |
| 58 | + |
| 59 | + @abc.abstractmethod |
| 60 | + def sign(payload): |
| 61 | + """ |
| 62 | + <Purpose> |
| 63 | + Abstract function used for signig a given payload. |
| 64 | +
|
| 65 | + <Arguments> |
| 66 | + payload: bytes to be signed |
| 67 | +
|
| 68 | + <Returns> |
| 69 | + Returns a "Signature" class instance containing the signature and the |
| 70 | + metadata related to the key needed for verification (e.g. keyid). |
| 71 | + """ |
| 72 | + pass |
| 73 | + |
| 74 | + |
| 75 | + |
| 76 | +class SSlibSigner(Signer): |
| 77 | + """ |
| 78 | + <Purpose> |
| 79 | + Securesystemlib default implementation of the "Signer" interface. |
| 80 | + With this implementation the following signature schemas are supported: |
| 81 | +
|
| 82 | + 'RSASSA-PSS' |
| 83 | + RFC3447 - RSASSA-PSS |
| 84 | + http://www.ietf.org/rfc/rfc3447. |
| 85 | +
|
| 86 | + 'ed25519' |
| 87 | + ed25519 - high-speed high security signatures |
| 88 | + http://ed25519.cr.yp.to/ |
| 89 | +
|
| 90 | + <Attributes> |
| 91 | + key_dict: |
| 92 | + A dictionary containing the keys. Both private and public keys are |
| 93 | + needed. An example RSA key dict has the form: |
| 94 | +
|
| 95 | + {'keytype': 'rsa', |
| 96 | + 'scheme': 'rsassa-pss-sha256', |
| 97 | + 'keyid': 'f30a0870d026980100c0573bd557394f8c1bbd6...', |
| 98 | + 'keyval': {'public': '-----BEGIN RSA PUBLIC KEY----- ...', |
| 99 | + 'private': '-----BEGIN RSA PRIVATE KEY----- ...'}} |
| 100 | +
|
| 101 | + The public and private keys are strings in PEM format. |
| 102 | + """ |
| 103 | + def __init__(self, key_dict): |
| 104 | + self.key_dict = key_dict |
| 105 | + |
| 106 | + |
| 107 | + def sign(self, payload): |
| 108 | + """ |
| 109 | + <Purpose> |
| 110 | + Used for signig a given payload. |
| 111 | +
|
| 112 | + <Arguments> |
| 113 | + payload: bytes to be signed |
| 114 | +
|
| 115 | + <Returns> |
| 116 | + Returns a "Signature" class instance containing the signature and the |
| 117 | + metadata related to the key needed for verification (e.g. keyid). |
| 118 | + """ |
| 119 | + signiture_dict = sslib_keys.create_signature(self.key_dict, payload) |
| 120 | + return Signature(signiture_dict['keyid'], signiture_dict['sig']) |
| 121 | + |
| 122 | + |
| 123 | + |
| 124 | +class GPGSigner(Signer): |
| 125 | + """Implement GPG signer""" |
| 126 | + |
| 127 | + def __init__(self, gpg_keyid): |
| 128 | + pass |
| 129 | + |
| 130 | + def sign(self, payload): |
| 131 | + pass |
0 commit comments