File: sign_pythonrsa.py

package info (click to toggle)
python-adb-shell 0.4.4-2
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 760 kB
  • sloc: python: 3,860; makefile: 191; sh: 124
file content (190 lines) | stat: -rw-r--r-- 4,879 bytes parent folder | download
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
# Copyright 2014 Google Inc. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""ADB authentication using the ``rsa`` package.


.. rubric:: Contents

* :class:`_Accum`

    * :meth:`_Accum.digest`
    * :meth:`_Accum.update`

* :func:`_load_rsa_private_key`
* :class:`PythonRSASigner`

    * :meth:`PythonRSASigner.FromRSAKeyPath`
    * :meth:`PythonRSASigner.GetPublicKey`
    * :meth:`PythonRSASigner.Sign`

"""


from pyasn1.codec.der import decoder
from pyasn1.type import univ
import rsa
from rsa import pkcs1


class _Accum(object):
    """A fake hashing algorithm.

    The Python ``rsa`` lib hashes all messages it signs. ADB does it already, we just
    need to slap a signature on top of already hashed message.  Introduce a "fake"
    hashing algo for this.

    Attributes
    ----------
    _buf : bytes
        A buffer for storing data before it is signed

    """
    def __init__(self):
        self._buf = b''

    def update(self, msg):
        """Update this hash object's state with the provided ``msg``.

        Parameters
        ----------
        msg : bytes
            The message to be appended to ``self._buf``

        """
        self._buf += msg

    def digest(self):
        """Return the digest value as a string of binary data.

        Returns
        -------
        self._buf : bytes
            ``self._buf``

        """
        return self._buf


pkcs1.HASH_METHODS['SHA-1-PREHASHED'] = _Accum
pkcs1.HASH_ASN1['SHA-1-PREHASHED'] = pkcs1.HASH_ASN1['SHA-1']


def _load_rsa_private_key(pem):
    """PEM encoded PKCS#8 private key -> ``rsa.PrivateKey``.

    ADB uses private RSA keys in pkcs#8 format. The ``rsa`` library doesn't
    support them natively.  Do some ASN unwrapping to extract naked RSA key
    (in der-encoded form).

    See:

    * https://www.ietf.org/rfc/rfc2313.txt
    * http://superuser.com/a/606266

    Parameters
    ----------
    pem : str
        The private key to be loaded

    Returns
    -------
    rsa.key.PrivateKey
        The loaded private key

    """
    try:
        der = rsa.pem.load_pem(pem, 'PRIVATE KEY')
        keyinfo, _ = decoder.decode(der)

        if keyinfo[1][0] != univ.ObjectIdentifier('1.2.840.113549.1.1.1'):
            raise ValueError('Not a DER-encoded OpenSSL private RSA key')

        private_key_der = keyinfo[2].asOctets()

    except IndexError:
        raise ValueError('Not a DER-encoded OpenSSL private RSA key')

    return rsa.PrivateKey.load_pkcs1(private_key_der, format='DER')


class PythonRSASigner(object):
    """Implements :class:`adb_protocol.AuthSigner` using http://stuvel.eu/rsa.

    Parameters
    ----------
    pub : str, None
        The contents of the public key file
    priv : str, None
        The contents of the private key file

    Attributes
    ----------
    priv_key : rsa.key.PrivateKey
        The loaded private key
    pub_key : str, None
        The contents of the public key file

    """
    def __init__(self, pub=None, priv=None):
        self.priv_key = _load_rsa_private_key(priv)
        self.pub_key = pub

    @classmethod
    def FromRSAKeyPath(cls, rsa_key_path):
        """Create a :class:`PythonRSASigner` instance using the provided private key.

        Parameters
        ----------
        rsa_key_path : str
            The path to the private key; the public key must be ``rsa_key_path + '.pub'``.

        Returns
        -------
        PythonRSASigner
            A :class:`PythonRSASigner` with private key ``rsa_key_path`` and public key ``rsa_key_path + '.pub'``

        """
        with open(rsa_key_path + '.pub') as f:
            pub = f.read()
        with open(rsa_key_path) as f:
            priv = f.read()
        return cls(pub, priv)

    def Sign(self, data):
        """Signs given data using a private key.

        Parameters
        ----------
        data : bytes
            The data to be signed

        Returns
        -------
        bytes
            The signed ``data``

        """
        return rsa.sign(data, self.priv_key, 'SHA-1-PREHASHED')

    def GetPublicKey(self):
        """Returns the public key in PEM format without headers or newlines.

        Returns
        -------
        self.pub_key : str, None
            The contents of the public key file, or ``None`` if a public key was not provided.

        """
        return self.pub_key