Debian Codegraph @

lib/Crypto/PublicKey/__init__.py

raw symbols SCIP
1 # -*- coding: utf-8 -*-
2 #
3 # ===================================================================
4 # The contents of this file are dedicated to the public domain. To
5 # the extent that dedication to the public domain is not available,
6 # everyone is granted a worldwide, perpetual, royalty-free,
7 # non-exclusive license to exercise all rights associated with the
8 # contents of this file for any purpose whatsoever.
9 # No rights are reserved.
10 #
11 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
12 # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
13 # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
14 # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
15 # BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
16 # ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
17 # CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
18 # SOFTWARE.
19 # ===================================================================
20
21 from Crypto.Util.asn1 import (DerSequence, DerInteger, DerBitString,
22 DerObjectId, DerNull)
23
24
25 def _expand_subject_public_key_info(encoded):
26 """Parse a SubjectPublicKeyInfo structure.
27
28 It returns a triple with:
29 * OID (string)
30 * encoded public key (bytes)
31 * Algorithm parameters (bytes or None)
32 """
33
34 #
35 # SubjectPublicKeyInfo ::= SEQUENCE {
36 # algorithm AlgorithmIdentifier,
37 # subjectPublicKey BIT STRING
38 # }
39 #
40 # AlgorithmIdentifier ::= SEQUENCE {
41 # algorithm OBJECT IDENTIFIER,
42 # parameters ANY DEFINED BY algorithm OPTIONAL
43 # }
44 #
45
46 spki = DerSequence().decode(encoded, nr_elements=2)
47 algo = DerSequence().decode(spki[0], nr_elements=(1,2))
48 algo_oid = DerObjectId().decode(algo[0])
49 spk = DerBitString().decode(spki[1]).value
50
51 if len(algo) == 1:
52 algo_params = None
53 else:
54 try:
55 DerNull().decode(algo[1])
56 algo_params = None
57 except:
58 algo_params = algo[1]
59
60 return algo_oid.value, spk, algo_params
61
62
63 def _create_subject_public_key_info(algo_oid, public_key, params):
64
65 if params is None:
66 algorithm = DerSequence([DerObjectId(algo_oid)])
67 else:
68 algorithm = DerSequence([DerObjectId(algo_oid), params])
69
70 spki = DerSequence([algorithm,
71 DerBitString(public_key)
72 ])
73 return spki.encode()
74
75
76 def _extract_subject_public_key_info(x509_certificate):
77 """Extract subjectPublicKeyInfo from a DER X.509 certificate."""
78
79 certificate = DerSequence().decode(x509_certificate, nr_elements=3)
80 tbs_certificate = DerSequence().decode(certificate[0],
81 nr_elements=range(6, 11))
82
83 index = 5
84 try:
85 tbs_certificate[0] + 1
86 # Version not present
87 version = 1
88 except TypeError:
89 version = DerInteger(explicit=0).decode(tbs_certificate[0]).value
90 if version not in (2, 3):
91 raise ValueError("Incorrect X.509 certificate version")
92 index = 6
93
94 return tbs_certificate[index]