File: rsa.py

package info (click to toggle)
python-securesystemslib 1.3.0-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 3,316 kB
  • sloc: python: 5,319; sh: 38; makefile: 5
file content (203 lines) | stat: -rw-r--r-- 5,904 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
"""
<Module Name>
  rsa.py

<Author>
  Santiago Torres-Arias <santiago@nyu.edu>

<Started>
  Nov 15, 2017

<Copyright>
  See LICENSE for licensing information.

<Purpose>
  RSA-specific handling routines for signature verification and key parsing
"""

import binascii

CRYPTO = True
NO_CRYPTO_MSG = "RSA key support for GPG requires the cryptography library"
try:
    from cryptography.exceptions import InvalidSignature
    from cryptography.hazmat import backends
    from cryptography.hazmat.primitives.asymmetric import padding, rsa, utils
except ImportError:
    CRYPTO = False

# ruff: noqa: E402
from securesystemslib import exceptions
from securesystemslib._gpg import util as gpg_util
from securesystemslib._gpg.exceptions import PacketParsingError


def create_pubkey(pubkey_info):
    """
    <Purpose>
      Create and return an RSAPublicKey object from the passed pubkey_info
      using pyca/cryptography.

    <Arguments>
      pubkey_info:
              An RSA pubkey dict.

    <Exceptions>
      securesystemslib.exceptions.UnsupportedLibraryError if
        the cryptography module is unavailable

    <Returns>
      A cryptography.hazmat.primitives.asymmetric.rsa.RSAPublicKey based on the
      passed pubkey_info.

    """
    if not CRYPTO:  # pragma: no cover
        raise exceptions.UnsupportedLibraryError(NO_CRYPTO_MSG)

    e = int(pubkey_info["keyval"]["public"]["e"], 16)
    n = int(pubkey_info["keyval"]["public"]["n"], 16)
    pubkey = rsa.RSAPublicNumbers(e, n).public_key(backends.default_backend())

    return pubkey


def get_pubkey_params(data):
    """
    <Purpose>
      Parse the public key parameters as multi-precision-integers.

    <Arguments>
      data:
             the RFC4880-encoded public key parameters data buffer as described
             in the fifth paragraph of section 5.5.2.

    <Exceptions>
      securesystemslib._gpg.exceptions.PacketParsingError:
             if the public key parameters are malformed

    <Side Effects>
      None.

    <Returns>
      An RSA public key dict.
    """
    ptr = 0

    modulus_length = gpg_util.get_mpi_length(data[ptr : ptr + 2])
    ptr += 2
    modulus = data[ptr : ptr + modulus_length]
    if len(modulus) != modulus_length:  # pragma: no cover
        raise PacketParsingError("This modulus MPI was truncated!")
    ptr += modulus_length

    exponent_e_length = gpg_util.get_mpi_length(data[ptr : ptr + 2])
    ptr += 2
    exponent_e = data[ptr : ptr + exponent_e_length]
    if len(exponent_e) != exponent_e_length:  # pragma: no cover
        raise PacketParsingError("This e MPI has been truncated!")

    return {
        "e": binascii.hexlify(exponent_e).decode("ascii"),
        "n": binascii.hexlify(modulus).decode("ascii"),
    }


def get_signature_params(data):
    """
    <Purpose>
      Parse the signature parameters as multi-precision-integers.

    <Arguments>
      data:
             the RFC4880-encoded signature data buffer as described
             in the third paragraph of section 5.2.2.

    <Exceptions>
      securesystemslib._gpg.exceptions.PacketParsingError:
             if the public key parameters are malformed

    <Side Effects>
      None.

    <Returns>
      The decoded signature buffer
    """

    ptr = 0
    signature_length = gpg_util.get_mpi_length(data[ptr : ptr + 2])
    ptr += 2
    signature = data[ptr : ptr + signature_length]
    if len(signature) != signature_length:  # pragma: no cover
        raise PacketParsingError("This signature was truncated!")

    return signature


def verify_signature(signature_object, pubkey_info, content, hash_algorithm_id):
    """
    <Purpose>
      Verify the passed signature against the passed content with the passed
      RSA public key using pyca/cryptography.

    <Arguments>
      signature_object:
              A signature dict.

      pubkey_info:
              The RSA public key dict.

      content:
              The signed bytes against which the signature is verified

      hash_algorithm_id:
              one of SHA1, SHA256, SHA512 (see securesystemslib._gpg.constants)
              used to verify the signature
              NOTE: Overrides any hash algorithm specification in "pubkey_info"'s
              "hashes" or "method" fields.

    <Exceptions>
      securesystemslib.exceptions.UnsupportedLibraryError if:
        the cryptography module is unavailable

      ValueError:
        if the passed hash_algorithm_id is not supported (see
        securesystemslib._gpg.util.get_hashing_class)

    <Returns>
      True if signature verification passes and False otherwise

    """
    if not CRYPTO:  # pragma: no cover
        raise exceptions.UnsupportedLibraryError(NO_CRYPTO_MSG)

    hasher = gpg_util.get_hashing_class(hash_algorithm_id)

    pubkey_object = create_pubkey(pubkey_info)

    # zero-pad the signature due to a discrepancy between the openssl backend
    # and the gnupg interpretation of PKCSv1.5. Read more at:
    # https://github.com/in-toto/in-toto/issues/171#issuecomment-440039256
    # we are skipping this if on the tests because well, how would one test this
    # deterministically.
    pubkey_length = len(pubkey_info["keyval"]["public"]["n"])
    signature_length = len(signature_object["signature"])
    if pubkey_length != signature_length:  # pragma: no cover
        zero_pad = "0" * (pubkey_length - signature_length)
        signature_object["signature"] = "{}{}".format(
            zero_pad, signature_object["signature"]
        )

    digest = gpg_util.hash_object(
        binascii.unhexlify(signature_object["other_headers"]), hasher(), content
    )

    try:
        pubkey_object.verify(
            binascii.unhexlify(signature_object["signature"]),
            digest,
            padding.PKCS1v15(),
            utils.Prehashed(hasher()),
        )
        return True
    except InvalidSignature:
        return False