1
- import binascii
2
1
import cbor2
3
2
import cryptography
4
3
import logging
5
4
6
5
from pycose .keys import CoseKey , EC2Key
7
6
from pycose .messages import Sign1Message
8
7
9
- from typing import Optional , Union
8
+ from typing import Optional
10
9
11
10
from . exceptions import UnsupportedMsoDataFormat
12
11
from . settings import COSEKEY_HAZMAT_CRV_MAP , CRV_LEN_MAP
@@ -24,62 +23,60 @@ class MobileSecurityObject:
24
23
only if the authentication signature or MAC is correct.
25
24
"""
26
25
27
- pass
28
-
29
26
30
27
class MsoParser (MobileSecurityObject ):
31
28
"""
32
29
Parameters
33
30
data: CBOR TAG 24
34
-
31
+
35
32
Example:
36
33
MsoParser(mdoc['documents'][0]['issuerSigned']['issuerAuth'])
37
34
38
35
Note
39
36
The signature is contained in an untagged COSE_Sign1
40
37
structure as defined in RFC 8152.
41
38
"""
42
-
39
+
43
40
def __init__ (self , data : cbor2 .CBORTag ):
44
41
self ._data = data
45
-
42
+
46
43
if isinstance (data , bytes ):
47
- self .object :Sign1Message = bytes2CoseSign1 (cbor2 .dumps (cbor2 .CBORTag (18 , value = data )))
44
+ self .object : Sign1Message = bytes2CoseSign1 (
45
+ cbor2 .dumps (cbor2 .CBORTag (18 , value = data )))
48
46
elif isinstance (data , list ):
49
- self .object : Sign1Message = cborlist2CoseSign1 (self ._data )
47
+ self .object : Sign1Message = cborlist2CoseSign1 (self ._data )
50
48
else :
51
49
raise UnsupportedMsoDataFormat (
52
50
f"MsoParser only supports raw bytes and list, a { type (data )} was provided"
53
51
)
54
-
55
-
56
- self .object .key :Optional [CoseKey , None ] = None
57
- self .public_key :cryptography .hazmat .backends .openssl .ec ._EllipticCurvePublicKey = None
58
- self .x509_certificates :list = []
59
-
52
+
53
+ self .object .key : Optional [CoseKey , None ] = None
54
+ self .public_key : cryptography .hazmat .backends .openssl .ec ._EllipticCurvePublicKey = None
55
+ self .x509_certificates : list = []
56
+
60
57
@property
61
58
def payload_as_cbor (self ):
62
59
"""
63
60
return the decoded payload
64
61
"""
65
62
return cbor2 .loads (self .object .payload )
66
-
63
+
67
64
@property
68
65
def payload_as_raw (self ):
69
66
return self .object .payload
70
-
67
+
71
68
@property
72
69
def payload_as_dict (self ):
73
70
return cbor2 .loads (
74
71
cbor2 .loads (self .object .payload ).value
75
72
)
76
-
73
+
77
74
@property
78
75
def raw_public_keys (self ) -> bytes :
79
76
return list (self .object .uhdr .values ())
80
-
77
+
81
78
def load_public_key (self ):
82
-
79
+
83
80
logger .warning (
84
81
"TODO: in next releases. "
85
82
"The certificate is to be considered as untrusted, this release "
@@ -90,11 +87,11 @@ def load_public_key(self):
90
87
self .x509_certificates .append (
91
88
cryptography .x509 .load_der_x509_certificate (i )
92
89
)
93
-
90
+
94
91
self .public_key = self .x509_certificates [0 ].public_key ()
95
-
92
+
96
93
key = EC2Key (
97
- crv = COSEKEY_HAZMAT_CRV_MAP [self .public_key .curve .name ],
94
+ crv = COSEKEY_HAZMAT_CRV_MAP [self .public_key .curve .name ],
98
95
x = self .public_key .public_numbers ().x .to_bytes (
99
96
CRV_LEN_MAP [self .public_key .curve .name ], 'big'
100
97
)
@@ -105,12 +102,11 @@ def verify_signature(self) -> bool:
105
102
106
103
if not self .object .key :
107
104
self .load_public_key ()
108
-
105
+
109
106
return self .object .verify_signature ()
110
107
111
108
112
109
class MsoWriter (MobileSecurityObject ):
113
110
"""
114
-
111
+
115
112
"""
116
- pass
0 commit comments