Test Failed
Push — main ( 235d86...c15fe1 )
by Sat CFDI
04:43
created

satcfdi.certifica.pkcs7   A

Complexity

Total Complexity 22

Size/Duplication

Total Lines 83
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 70
dl 0
loc 83
rs 10
c 0
b 0
f 0
wmc 22

1 Function

Rating   Name   Duplication   Size   Complexity  
F create_pkcs7() 0 70 22
1
import os
2
from datetime import datetime
3
4
from ..ans1e import Ans1Encoder, Numbers, Classes, to_utc_time
5
from ..models import Signer
6
7
current_dir = os.path.dirname(__file__)
8
9
from cryptography.hazmat.primitives import hashes
10
from cryptography.hazmat.primitives.asymmetric import padding
11
12
13
def create_pkcs7(data, signer: Signer, hash_algorithm):
14
    cert_bytes = signer.certificate_bytes()
15
    issuer_der = signer.certificate.get_issuer().der()
16
    serial = signer.certificate.get_serial_number()
17
18
    hash_object = hashes.Hash(hash_algorithm)
19
    hash_object.update(data)
20
    digest = hash_object.finalize()
21
22
    utctime = to_utc_time(datetime.utcnow())
23
24
    e = Ans1Encoder()
25
    with e.seq():
26
        e.oid("1.2.840.113549.1.9.3")
27
        with e.set():
28
            e.oid("1.2.840.113549.1.7.1")
29
    with e.seq():
30
        e.oid("1.2.840.113549.1.9.5")
31
        with e.set():
32
            e(utctime, nr=Numbers.UTCTime)
33
    with e.seq():
34
        e.oid("1.2.840.113549.1.9.4")
35
        with e.set():
36
            e(digest, nr=Numbers.OctetString)
37
    signed_attributes = e.output()
38
39
    e = Ans1Encoder()
40
    with e.set():
41
        e.write(signed_attributes)
42
    signing_data = e.output()
43
44
    signature = signer.key.sign(
45
        data=signing_data,
46
        padding=padding.PKCS1v15(),
47
        algorithm=hash_algorithm
48
    )
49
50
    e = Ans1Encoder()
51
    with e.seq():
52
        e.oid('1.2.840.113549.1.7.2')
53
        with e.enter(nr=0, cls=Classes.Context):
54
            with e.seq():
55
                e(1, nr=Numbers.Integer)
56
                with e.set():
57
                    with e.seq():
58
                        e.oid('1.3.14.3.2.26')
59
                        e(nr=Numbers.Null)
60
                with e.seq():
61
                    e.oid("1.2.840.113549.1.7.1")
62
                    with e.enter(nr=0, cls=Classes.Context):
63
                        e(data, nr=Numbers.OctetString)
64
                with e.enter(nr=0, cls=Classes.Context):
65
                    e.write(cert_bytes)
66
                with e.set():
67
                    with e.seq():
68
                        e(1, nr=Numbers.Integer)
69
                        with e.seq():
70
                            e.write(issuer_der)
71
                            e(serial, nr=Numbers.Integer)
72
                        with e.seq():
73
                            e.oid('1.3.14.3.2.26')
74
                            e(nr=Numbers.Null)
75
                        with e.enter(nr=0, cls=Classes.Context):
76
                            e.write(signed_attributes)
77
                        with e.seq():
78
                            e.oid('1.2.840.113549.1.1.1')
79
                            e(nr=Numbers.Null)
80
                        e(signature, nr=Numbers.OctetString)
81
82
    return e.output()
83