Skip to content

Commit f10acff

Browse files
committed
Include type information in documentation
This also includes a few other cleanups moving some documentation to docstrings.
1 parent 02c35e7 commit f10acff

15 files changed

Lines changed: 252 additions & 270 deletions

docs/encryption.rst

Lines changed: 11 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -46,11 +46,12 @@ The Encryption class
4646

4747
.. class:: Encryption
4848

49-
.. attribute:: id
49+
.. autoattribute:: id
5050

5151
Optional identifier of the encryption key.
5252

5353
.. attribute:: algorithm
54+
:type: str | None
5455

5556
A URI of the encryption algorithm used. See the section
5657
:ref:`encryption-algorithms` below for a list of algorithms URIs.
@@ -62,22 +63,24 @@ The Encryption class
6263

6364

6465
.. attribute:: is_encrypted
66+
:type: bool
6567

6668
An indicator of whether the PSKC file requires an additional pre-shared
6769
key or passphrase to decrypt the contents of the file. Will be ``True``
6870
if a key or passphrase is needed, ``False`` otherwise.
6971

70-
.. attribute:: key_names
72+
.. autoattribute:: key_names
7173

7274
List of names provided for the encryption key.
7375

7476
.. attribute:: key_name
77+
:type: str | None
7578

7679
Since usually only one name is defined for a key but the schema allows
7780
for multiple names, this is a shortcut for accessing the first value of
7881
:attr:`key_names`. It will return ``None`` if no name is available.
7982

80-
.. attribute:: key
83+
.. autoattribute:: key
8184

8285
The binary value of the encryption key. In the case of pre-shared keys
8386
this value should be set before trying to access encrypted information
@@ -86,75 +89,20 @@ The Encryption class
8689
When using key derivation the secret key is available in this attribute
8790
after calling :func:`derive_key`.
8891

89-
.. function:: derive_key(password)
92+
.. automethod:: derive_key
9093

91-
Derive a key from the supplied password and information in the PSKC
92-
file (generally algorithm, salt, etc.).
93-
94-
This function may raise a :exc:`~pskc.exceptions.KeyDerivationError`
95-
exception if key derivation fails for some reason.
96-
97-
.. attribute:: fields
94+
.. autoattribute:: fields
9895

9996
A list of :class:`~pskc.key.Key` instance field names that will be
10097
encrypted when the PSKC file is written. List values can contain
10198
``secret``, ``counter``, ``time_offset``, ``time_interval`` and
10299
``time_drift``.
103100

104-
.. function:: setup_preshared_key(...)
105-
106-
Configure pre-shared key encryption when writing the file.
107-
108-
:param bytes key: the encryption key to use
109-
:param str id: encryption key identifier
110-
:param str algorithm: encryption algorithm
111-
:param int key_length: encryption key length in bytes
112-
:param str key_name: a name for the key
113-
:param list key_names: a number of names for the key
114-
:param list fields: a list of fields to encrypt
115-
116-
This is a utility function to easily set up encryption. Encryption can
117-
also be set up by manually by setting the
118-
:class:`~pskc.encryption.Encryption` properties.
119-
120-
This method will generate a key if required and set the passed values.
121-
By default AES128-CBC encryption will be configured and unless a key is
122-
specified one of the correct length will be generated. If the algorithm
123-
does not provide integrity checks (e.g. CBC-mode algorithms) integrity
124-
checking in the PSKC file will be set up using
125-
:func:`~pskc.mac.MAC.setup()`.
126-
127-
By default only the :attr:`~pskc.key.Key.secret` property will be
128-
encrypted when writing the file.
129-
130-
.. function:: setup_pbkdf2(...)
131-
132-
Configure password-based PSKC encryption when writing the file.
133-
134-
:param str password: the password to use (required)
135-
:param str id: encryption key identifier
136-
:param str algorithm: encryption algorithm
137-
:param int key_length: encryption key length in bytes
138-
:param str key_name: a name for the key
139-
:param list key_names: a number of names for the key
140-
:param list fields: a list of fields to encrypt
141-
:param bytes salt: PBKDF2 salt
142-
:param int salt_length: used when generating random salt
143-
:param int iterations: number of PBKDF2 iterations
144-
:param function prf: PBKDF2 pseudorandom function
145-
146-
Defaults for the above parameters are similar to those for
147-
:func:`setup_preshared_key()` but the password parameter is required.
148-
149-
By default 12000 iterations will be used and a random salt with the
150-
length of the to-be-generated encryption key will be used.
101+
.. automethod:: setup_preshared_key
151102

152-
.. function:: remove_encryption()
103+
.. automethod:: setup_pbkdf2
153104

154-
Decrypt all data stored in the PSKC file and remove the encryption
155-
configuration. This can be used to read and encrypted PSKC file,
156-
decrypt the file, remove the encryption and output an unencrypted PSKC
157-
file or to replace the encryption algorithm.
105+
.. automethod:: remove_encryption
158106

159107

160108
.. _encryption-algorithms:

docs/exceptions.rst

Lines changed: 5 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -6,36 +6,12 @@ only raise exceptions on wildly invalid PSKC files.
66

77
.. module:: pskc.exceptions
88

9-
.. exception:: PSKCError
9+
.. autoexception:: PSKCError
1010

11-
The base class for all exceptions that the module will raise. In some
12-
cases third-party code may raise additional exceptions.
11+
.. autoexception:: ParseError
1312

14-
.. exception:: ParseError
13+
.. autoexception:: EncryptionError
1514

16-
Raised when the PSKC file cannot be correctly read due to invalid XML or
17-
some required element or attribute is missing. This exception should only
18-
be raised when parsing the file (i.e. when the :class:`~pskc.PSKC` class is
19-
instantiated).
15+
.. autoexception:: DecryptionError
2016

21-
.. .. exception:: EncryptionError
22-
23-
Raised when encrypting a value is not possible due to key length issues,
24-
missing or wrong length plain text, or other issues.
25-
26-
.. exception:: DecryptionError
27-
28-
Raised when decrypting a value fails due to missing or incorrect key,
29-
unsupported decryption or MAC algorithm, failed message authentication
30-
check or other error.
31-
32-
This exception is generally raised when accessing encrypted information
33-
(i.e. the :attr:`~pskc.key.Key.secret`, :attr:`~pskc.key.Key.counter`,
34-
:attr:`~pskc.key.Key.time_offset`, :attr:`~pskc.key.Key.time_interval` or
35-
:attr:`~pskc.key.Key.time_drift` attributes of the :class:`~pskc.key.Key`
36-
class).
37-
38-
.. exception:: KeyDerivationError
39-
40-
Raised when key derivation fails due to an unsupported algorithm or
41-
missing information in the PSKC file.
17+
.. autoexception:: KeyDerivationError

docs/mac.rst

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ The MAC class
2727
.. class:: MAC
2828

2929
.. attribute:: algorithm
30+
:type: str | None
3031

3132
A URI of the MAC algorithm used for message authentication. See the
3233
section :ref:`mac-algorithms` below for a list of algorithm URIs.
@@ -37,25 +38,14 @@ The MAC class
3738
``http://www.w3.org/2001/04/xmldsig-more#hmac-sha256``.
3839

3940
.. attribute:: key
41+
:type: bytes | None
4042

4143
For HMAC checking, this contains the binary value of the MAC key. The
4244
MAC key is generated specifically for each PSKC file and encrypted with
4345
the PSKC encryption key, so the PSKC file should be decrypted first
4446
(see :doc:`encryption`).
4547

46-
.. function:: setup(...)
47-
48-
Configure an encrypted MAC key for creating a new PSKC file.
49-
50-
:param str algorithm: encryption algorithm
51-
:param bytes key: the encryption key to use
52-
53-
None of the arguments are required. By default HMAC-SHA1 will be used
54-
as a MAC algorithm. If no key is configured a random key will be
55-
generated with the length of the output of the configured hash.
56-
57-
This function will automatically be called when the configured
58-
encryption algorithm requires a message authentication code.
48+
.. automethod:: setup
5949

6050

6151
.. _mac-algorithms:

docs/policy.rst

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -19,84 +19,83 @@ The Policy class
1919

2020
.. class:: Policy
2121

22-
.. attribute:: start_date
22+
.. autoattribute:: start_date
2323

2424
:class:`datetime.datetime` value that indicates that the key must not
2525
be used before this date.
2626

27-
.. attribute:: expiry_date
27+
.. autoattribute:: expiry_date
2828

2929
:class:`datetime.datetime` value that indicates that the key must not
3030
be used after this date. Systems should not rely upon the device to
3131
enforce key usage date restrictions, as some devices do not have an
3232
internal clock.
3333

34-
.. attribute:: number_of_transactions
34+
.. autoattribute:: number_of_transactions
3535

3636
The value indicates the maximum number of times a key carried within
3737
the PSKC document may be used by an application after having received
3838
it.
3939

40-
.. attribute:: key_usage
40+
.. autoattribute:: key_usage
4141

4242
A list of `valid usage scenarios
4343
<https://www.iana.org/assignments/pskc/#key-usage>`__ for the
4444
key that the recipient should check against the intended usage of the
4545
key. Also see :func:`may_use` and :ref:`key-use-constants` below.
4646

47-
.. attribute:: pin_key_id
47+
.. autoattribute:: pin_key_id
4848

4949
The unique `id` of the key within the PSKC file that contains the value
5050
of the PIN that protects this key.
5151

5252
.. attribute:: pin_key
53+
:type: Key | None
5354

5455
Instance of the :class:`~pskc.key.Key` (if any) that contains the value
5556
of the PIN referenced by :attr:`pin_key_id`.
5657

5758
.. attribute:: pin
59+
:type: str | None
5860

5961
PIN value referenced by :attr:`pin_key_id` (if any). The value is
6062
transparently decrypted if possible.
6163

62-
.. attribute:: pin_usage
64+
.. autoattribute:: pin_usage
6365

6466
Describe how the PIN is used during the usage of the key. See
6567
:ref:`pin-use-constants` below.
6668

67-
.. attribute:: pin_max_failed_attempts
69+
.. autoattribute:: pin_max_failed_attempts
6870

6971
The maximum number of times the PIN may be entered wrongly before it
7072
MUST NOT be possible to use the key any more.
7173

72-
.. attribute:: pin_min_length
74+
.. autoattribute:: pin_min_length
7375

7476
The minimum length of a PIN that can be set to protect the associated
7577
key.
7678

77-
.. attribute:: pin_max_length
79+
.. autoattribute:: pin_max_length
7880

7981
The maximum length of a PIN that can be set to protect this key.
8082

81-
.. attribute:: pin_encoding
83+
.. autoattribute:: pin_encoding
8284

8385
The encoding of the PIN which is one of ``DECIMAL``, ``HEXADECIMAL``,
8486
``ALPHANUMERIC``, ``BASE64``, or ``BINARY`` (see
8587
:attr:`~pskc.key.Key.challenge_encoding`).
8688

8789
.. attribute:: unknown_policy_elements
90+
:type: bool
8891

8992
Boolean that is set to ``True`` if the PSKC policy information contains
9093
unknown or unsupported definitions or values. A conforming
9194
implementation must assume that key usage is not permitted if this
9295
value is ``True`` to ensure that the lack of understanding of certain
9396
extensions does not lead to unintended key usage.
9497

95-
.. function:: may_use(usage=None, now=None)
96-
97-
Check whether the key may be used for the provided purpose. The key
98-
:attr:`start_date` and :attr:`expiry_date` are also checked. The `now`
99-
argument can be used to specify another point in time to check against.
98+
.. automethod:: may_use
10099

101100
.. _key-use-constants:
102101

docs/signatures.rst

Lines changed: 11 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -31,84 +31,54 @@ The Signature class
3131
.. class:: Signature
3232

3333
.. attribute:: is_signed
34+
:type: bool
3435

3536
A boolan value that indicates whether an XML signature is present in
3637
the PSKC file. This property does not indicate whether the signature
3738
is validated.
3839

3940
.. attribute:: algorithm
41+
:type: str | None
4042

4143
A URI of the signing algorithm used.
4244
Assigned values to this attribute will be converted to the canonical
4345
URI for the algorithm if it is known.
4446

45-
.. attribute:: canonicalization_method
47+
.. autoattribute:: canonicalization_method
4648

4749
A URI that is used to identify the XML canonicalization method used.
4850

49-
.. attribute:: digest_algorithm
51+
.. autoattribute:: digest_algorithm
5052

5153
A URI that identifies that hashing algorithm that is used to construct
5254
the signature.
5355

54-
.. attribute:: issuer
56+
.. autoattribute:: issuer
5557

5658
A distinguished name of the issuer of the certificate that belongs to
5759
the key that is used for the signature.
5860

59-
.. attribute:: serial
61+
.. autoattribute:: serial
6062

6163
A serial number of the certificate that belongs to the key that is used
6264
for the signature.
6365

64-
.. attribute:: key
66+
.. autoattribute:: key
6567

6668
A PEM encoded key that will be used to create the signed PSKC file.
6769

68-
.. attribute:: certificate
70+
.. autoattribute:: certificate
6971

7072
A PEM encoded certificate that is embedded inside the signature that
7173
can be used to validate the signature.
7274

7375
.. attribute:: signed_pskc
76+
:type: PSKC
7477

7578
A :class:`~pskc.PSKC` instance that contains the signed contents of the
7679
PSKC file. It is usually required to call :func:`verify` before
7780
accessing this attribute without raising an exception.
7881

79-
.. function:: verify(certificate=None, ca_pem_file=None)
82+
.. automethod:: verify
8083

81-
Verify the validity of the embedded XML signature. This function will
82-
raise an exception when the validation fails.
83-
84-
:param bytes certificate: a PEM encoded certificate that is used for verification
85-
:param str ca_pem_file: the name of a file that contains a CA certificate
86-
87-
The signature can be verified in three ways:
88-
89-
* The signature was made with a key that has a certificate that is
90-
signed by a CA that is configured in the system CA store. In this
91-
case neither `certificate` or `ca_pem_file` need to be specified (but
92-
a certificate needs to be embedded inside the PSKC file).
93-
* The signature was made with a key and a certificate for the key was
94-
transmitted out-of-band. In this case the `certificate` argument
95-
needs to be present.
96-
* The signature was made with a key and has a certificate that is
97-
signed by a specific CA who's certificate was transmitted
98-
out-of-band. In this case the `ca_pem_file` is used to point to a CA
99-
certificate file (but a certificate needs to be embedded inside the
100-
PSKC file).
101-
102-
After calling this function a verified version of the PSKC file will
103-
be present in the :attr:`signed_pskc` attribute.
104-
105-
.. function:: sign(key, certificate=None)
106-
107-
Set up a key and optionally a certificate that will be used to create an
108-
embedded XML signature when writing the file.
109-
110-
:param bytes key: PEM encoded key used for signing
111-
:param bytes certificate: PEM encoded certificate that will be embedded
112-
113-
This is a utility function that is used to configure the properties
114-
needed to create a signed PSKC file.
84+
.. automethod:: sign

0 commit comments

Comments
 (0)