| 1 |
# Copyright New York University and the in-toto contributors |
| 2 |
# SPDX-License-Identifier: Apache-2.0 |
| 3 |
|
| 4 |
""" |
| 5 |
<Program Name> |
| 6 |
_signer.py |
| 7 |
|
| 8 |
<Author> |
| 9 |
Pradyumna Krishna <[email protected]> |
| 10 |
|
| 11 |
<Started> |
| 12 |
Jan 26, 2023 |
| 13 |
|
| 14 |
<Copyright> |
| 15 |
See LICENSE for licensing information. |
| 16 |
|
| 17 |
<Purpose> |
| 18 |
Provides in-toto flavored GPGSigner, GPGSignature and GPGKey. |
| 19 |
|
| 20 |
""" |
| 21 |
|
| 22 |
from dataclasses import dataclass |
| 23 |
from typing import Any, Dict, List, Optional |
| 24 |
|
| 25 |
import securesystemslib.gpg.exceptions as gpg_exceptions |
| 26 |
import securesystemslib.gpg.functions as gpg |
| 27 |
from securesystemslib import exceptions |
| 28 |
from securesystemslib.signer import Key, SecretsHandler, Signature, Signer |
| 29 |
|
| 30 |
|
| 31 |
class GPGSignature(Signature): |
| 32 |
"""A container class containing information about a gpg signature. |
| 33 |
Besides the signature, it also contains other meta information |
| 34 |
needed to uniquely identify the key used to generate the signature. |
| 35 |
|
| 36 |
Attributes: |
| 37 |
keyid: HEX string used as a unique identifier of the key. |
| 38 |
signature: HEX string representing the signature. |
| 39 |
other_headers: HEX representation of additional GPG headers. |
| 40 |
""" |
| 41 |
|
| 42 |
def __init__( |
| 43 |
self, |
| 44 |
keyid: str, |
| 45 |
signature: str, |
| 46 |
other_headers: str, |
| 47 |
): |
| 48 |
super().__init__(keyid, signature) |
| 49 |
self.other_headers = other_headers |
| 50 |
|
| 51 |
@classmethod |
| 52 |
def from_dict(cls, signature_dict: Dict) -> "GPGSignature": |
| 53 |
"""Creates a ``GPGSignature`` object from its JSON/dict |
| 54 |
representation. |
| 55 |
|
| 56 |
Arguments: |
| 57 |
signature_dict: Dict containing valid "keyid", "signature" and |
| 58 |
"other_fields" fields. |
| 59 |
Raises: |
| 60 |
KeyError: If any of the "keyid", "sig" or "other_headers" fields |
| 61 |
are missing from the signature_dict. |
| 62 |
Returns: |
| 63 |
``GPGSignature`` instance. |
| 64 |
""" |
| 65 |
|
| 66 |
return cls( |
| 67 |
signature_dict["keyid"], |
| 68 |
signature_dict["signature"], |
| 69 |
signature_dict["other_headers"], |
| 70 |
) |
| 71 |
|
| 72 |
def to_dict(self) -> Dict: |
| 73 |
"""Returns the JSON-serializable dictionary representation of self.""" |
| 74 |
return { |
| 75 |
"keyid": self.keyid, |
| 76 |
"signature": self.signature, |
| 77 |
"other_headers": self.other_headers, |
| 78 |
} |
| 79 |
|
| 80 |
|
| 81 |
class GPGSigner(Signer): |
| 82 |
"""A in-toto gpg implementation of the ``Signer`` interface. |
| 83 |
Provides a sign method to generate a cryptographic signature with gpg, using |
| 84 |
an RSA, DSA or EdDSA private key identified by the keyid on the instance. |
| 85 |
|
| 86 |
Arguments: |
| 87 |
keyid: The keyid of the gpg signing keyid. If not passed the default |
| 88 |
key in the keyring is used. |
| 89 |
homedir: Path to the gpg keyring. If not passed the default keyring |
| 90 |
is used. |
| 91 |
""" |
| 92 |
|
| 93 |
def __init__( |
| 94 |
self, keyid: Optional[str] = None, homedir: Optional[str] = None |
| 95 |
): |
| 96 |
self.keyid = keyid |
| 97 |
self.homedir = homedir |
| 98 |
|
| 99 |
@classmethod |
| 100 |
def from_priv_key_uri( |
| 101 |
cls, |
| 102 |
priv_key_uri: str, |
| 103 |
public_key: Key, |
| 104 |
secrets_handler: Optional[SecretsHandler] = None, |
| 105 |
) -> "GPGSigner": |
| 106 |
raise NotImplementedError( |
| 107 |
"Incompatible with private key URIs" |
| 108 |
) # pragma: no cover |
| 109 |
|
| 110 |
def sign(self, payload: bytes) -> GPGSignature: |
| 111 |
"""Signs a given payload by the key assigned to the ``GPGSigner`` |
| 112 |
instance. Calls the gpg command line utility to sign the passed content |
| 113 |
with the key identified by the passed keyid from the gpg keyring at the |
| 114 |
passed homedir. |
| 115 |
|
| 116 |
Arguments: |
| 117 |
payload: The bytes to be signed. |
| 118 |
Raises: |
| 119 |
securesystemslib.exceptions.FormatError: |
| 120 |
If the keyid was passed and does not match |
| 121 |
securesystemslib.formats.KEYID_SCHEMA. |
| 122 |
ValueError: the gpg command failed to create a valid signature. |
| 123 |
OSError: the gpg command is not present or non-executable. |
| 124 |
securesystemslib.exceptions.UnsupportedLibraryError: the gpg command is |
| 125 |
not available, or the cryptography library is not installed. |
| 126 |
securesystemslib.gpg.exceptions.CommandError: the gpg command returned a |
| 127 |
non-zero exit code. |
| 128 |
securesystemslib.gpg.exceptions.KeyNotFoundError: the used gpg version is |
| 129 |
not fully supported and no public key can be found for short keyid. |
| 130 |
Returns: |
| 131 |
Returns a ``GPGSignature`` class instance. |
| 132 |
""" |
| 133 |
|
| 134 |
sig_dict = gpg.create_signature(payload, self.keyid, self.homedir) |
| 135 |
return GPGSignature(**sig_dict) |
| 136 |
|
| 137 |
|
| 138 |
@dataclass |
| 139 |
class GPGKey(Key): |
| 140 |
"""A container class representing public key portion of a GPG key. |
| 141 |
Provides a verify method to verify a cryptographic signature with a |
| 142 |
gpg-style rsa, dsa or ecdsa public key on the instance. |
| 143 |
|
| 144 |
Attributes: |
| 145 |
type: Key type, e.g. "rsa", "dsa" or "ecdsa". |
| 146 |
method: GPG Key Scheme, For example: |
| 147 |
"pgp+rsa-pkcsv1.5", "pgp+dsa-fips-180-2", and "pgp+eddsa-ed25519". |
| 148 |
hashes: list of GPG Hash Algorithms, e.g. "pgp+SHA2". |
| 149 |
keyval: Opaque key content. |
| 150 |
keyid: Key identifier that is unique within the metadata it is used in. |
| 151 |
Keyid is not verified to be the hash of a specific representation |
| 152 |
of the key. |
| 153 |
creation_time: Unix timestamp when GPG key was created. |
| 154 |
validity_period: Validity of the GPG Keys in days. |
| 155 |
subkeys: A dictionary containing keyid and GPG subkey. |
| 156 |
""" |
| 157 |
|
| 158 |
type: str |
| 159 |
method: str |
| 160 |
hashes: List[str] |
| 161 |
keyval: Dict[str, str] |
| 162 |
keyid: str |
| 163 |
creation_time: Optional[int] = None |
| 164 |
validity_period: Optional[int] = None |
| 165 |
subkeys: Optional[Dict[str, "GPGKey"]] = None |
| 166 |
|
| 167 |
@classmethod |
| 168 |
def from_dict(cls, keyid: str, key_dict: Dict[str, Any]): |
| 169 |
"""Creates ``GPGKey`` object from its json/dict representation. |
| 170 |
Raises: |
| 171 |
KeyError, TypeError: Invalid arguments. |
| 172 |
""" |
| 173 |
subkeys_dict = key_dict.get("subkeys") |
| 174 |
|
| 175 |
gpg_subkeys = None |
| 176 |
if subkeys_dict: |
| 177 |
gpg_subkeys = { |
| 178 |
_keyid: GPGKey.from_dict(_keyid, subkey_dict) |
| 179 |
for (_keyid, subkey_dict) in subkeys_dict.items() |
| 180 |
} |
| 181 |
|
| 182 |
return cls( |
| 183 |
key_dict["type"], |
| 184 |
key_dict["method"], |
| 185 |
key_dict["hashes"], |
| 186 |
key_dict["keyval"], |
| 187 |
keyid, |
| 188 |
key_dict.get("creation_time"), |
| 189 |
key_dict.get("validity_period"), |
| 190 |
gpg_subkeys, |
| 191 |
) |
| 192 |
|
| 193 |
@classmethod |
| 194 |
def from_legacy_dict(cls, key_dict: Dict[str, Any]): |
| 195 |
"""Create GPGKey from legacy dictionary representation.""" |
| 196 |
|
| 197 |
keyid = key_dict["keyid"] |
| 198 |
return cls.from_dict(keyid, key_dict) |
| 199 |
|
| 200 |
def to_dict(self): |
| 201 |
"""Returns the dictionary representation of self.""" |
| 202 |
|
| 203 |
key_dict = { |
| 204 |
"method": self.method, |
| 205 |
"type": self.type, |
| 206 |
"hashes": self.hashes, |
| 207 |
"keyid": self.keyid, |
| 208 |
"keyval": self.keyval, |
| 209 |
} |
| 210 |
|
| 211 |
if self.creation_time: |
| 212 |
key_dict["creation_time"] = self.creation_time |
| 213 |
if self.validity_period: |
| 214 |
key_dict["validity_period"] = self.validity_period |
| 215 |
if self.subkeys: |
| 216 |
subkeys_dict = { |
| 217 |
keyid: subkey.to_dict() |
| 218 |
for (keyid, subkey) in self.subkeys.items() |
| 219 |
} |
| 220 |
key_dict["subkeys"] = subkeys_dict |
| 221 |
|
| 222 |
return key_dict |
| 223 |
|
| 224 |
@classmethod |
| 225 |
def from_keyring(cls, keyid, homedir=None): |
| 226 |
"""Creates ``GPGKey`` object from GnuPG Keyring.""" |
| 227 |
|
| 228 |
pubkey_dict = gpg.export_pubkey(keyid, homedir) |
| 229 |
return cls.from_dict(keyid, pubkey_dict) |
| 230 |
|
| 231 |
def verify_signature(self, signature: GPGSignature, data: bytes) -> None: |
| 232 |
"""Verifies a given payload by the key assigned to the GPGKey |
| 233 |
instance. |
| 234 |
|
| 235 |
Arguments: |
| 236 |
signature: A ``GPGSignature`` class instance. |
| 237 |
data: The bytes to be verified. |
| 238 |
""" |
| 239 |
|
| 240 |
try: |
| 241 |
if not gpg.verify_signature( |
| 242 |
signature.to_dict(), self.to_dict(), data |
| 243 |
): |
| 244 |
raise exceptions.UnverifiedSignatureError( |
| 245 |
f"Failed to verify signature by {self.keyid}" |
| 246 |
) |
| 247 |
except ( |
| 248 |
exceptions.FormatError, |
| 249 |
exceptions.UnsupportedLibraryError, |
| 250 |
gpg_exceptions.KeyExpirationError, |
| 251 |
) as e: |
| 252 |
raise exceptions.VerificationError( |
| 253 |
f"Unknown failure to verify signature by {self.keyid}" |
| 254 |
) from e |