Projekt

Obecné

Profil

Stáhnout (5.9 KB) Statistiky
| Větev: | Tag: | Revize:
1 4c19a9b1 Stanislav Král
from src.constants import ROOT_CA_ID, INTERMEDIATE_CA_ID, CA_ID, CERTIFICATE_ID
2 4a40b0d2 Stanislav Král
from src.dao.certificate_repository import CertificateRepository
3
from src.model.certificate import Certificate
4 313b647b Stanislav Král
from src.model.private_key import PrivateKey
5 4a40b0d2 Stanislav Král
from src.model.subject import Subject
6
from src.services.cryptography import CryptographyService
7
8 313b647b Stanislav Král
import time
9
10
DATE_FORMAT = "%d.%m.%Y %H:%M:%S"
11 bbcb7c89 Stanislav Král
CA_EXTENSIONS = "basicConstraints=critical,CA:TRUE"
12 313b647b Stanislav Král
13 4a40b0d2 Stanislav Král
14
class CertificateService:
15
16
    def __init__(self, cryptography_service: CryptographyService, certificate_repository: CertificateRepository):
17
        self.cryptography_service = cryptography_service
18
        self.certificate_repository = certificate_repository
19
20 bbcb7c89 Stanislav Král
    # TODO usages present in method parameters but not in class diagram
21 ca3ac7c0 Stanislav Král
    def create_root_ca(self, key: PrivateKey, subject: Subject, extensions: str = "", config: str = "",
22
                       usages=None):
23
        if usages is None:
24
            usages = {}
25
26 313b647b Stanislav Král
        # create a new self signed  certificate
27
        cert_pem = self.cryptography_service.create_sscrt(subject, key.private_key, key_pass=key.password,
28
                                                          extensions=extensions, config=config)
29 ca3ac7c0 Stanislav Král
        # specify CA usage
30
        usages[CA_ID] = True
31
32 4c19a9b1 Stanislav Král
        # wrap into Certificate class
33
        certificate = self.__create_wrapper(cert_pem, key.private_key_id, subject.common_name, usages, 0,
34
                                            ROOT_CA_ID)
35 313b647b Stanislav Král
36
        # store the wrapper into the repository
37
        created_id = self.certificate_repository.create(certificate)
38
39
        # assign the generated ID to the inserted certificate
40
        certificate.certificate_id = created_id
41 4a40b0d2 Stanislav Král
42 313b647b Stanislav Král
        return certificate
43 10fab051 Stanislav Král
44 4c19a9b1 Stanislav Král
    def __create_wrapper(self, cert_pem, private_key_id, common_name, usages, parent_id, cert_type):
45
        # parse the generated pem for subject and notBefore/notAfter fields
46
        subj, not_before, not_after = self.cryptography_service.parse_cert_pem(cert_pem)
47
        # format the parsed date
48
        not_before_formatted = time.strftime(DATE_FORMAT, not_before)
49
        not_after_formatted = time.strftime(DATE_FORMAT, not_after)
50
51
        # create a certificate wrapper
52
        certificate = Certificate(-1, common_name, not_before_formatted, not_after_formatted, cert_pem,
53
                                  private_key_id, cert_type, parent_id, usages)
54
55
        print(f"CW: {parent_id} {certificate.parent_id}")
56
        return certificate
57
58 bbcb7c89 Stanislav Král
    # TODO config parameter present in class diagram but not here (unused)
59
    def create_ca(self, subject_key: PrivateKey, subject: Subject, issuer_cert: Certificate, issuer_key: PrivateKey,
60 ca3ac7c0 Stanislav Král
                  extensions: str = "", days: int = 30, usages=None):
61
        if usages is None:
62
            usages = {}
63
64 bbcb7c89 Stanislav Král
        extensions = extensions + "\n" + CA_EXTENSIONS
65
        # TODO implement AIA URI via extensions
66
        cert_pem = self.cryptography_service.create_crt(subject, subject_key.private_key, issuer_cert.pem_data,
67
                                                        issuer_key.private_key,
68
                                                        subject_key_pass=subject_key.password,
69
                                                        issuer_key_pass=issuer_key.password, extensions=extensions,
70
                                                        days=days)
71
72 4c19a9b1 Stanislav Král
        # specify CA usage
73
        usages[CA_ID] = True
74
75
        # wrap into Certificate class
76
        self.__create_wrapper(cert_pem, subject_key.private_key_id, subject.common_name, usages,
77
                              issuer_cert.certificate_id, INTERMEDIATE_CA_ID)
78
79 bbcb7c89 Stanislav Král
        # parse the generated pem for subject and notBefore/notAfter fields
80
        subj, not_before, not_after = self.cryptography_service.parse_cert_pem(cert_pem)
81
82
        # format the parsed date
83
        not_before_formatted = time.strftime(DATE_FORMAT, not_before)
84
        not_after_formatted = time.strftime(DATE_FORMAT, not_after)
85
86 ca3ac7c0 Stanislav Král
        # specify CA usage
87
        usages[CA_ID] = True
88
89 bbcb7c89 Stanislav Král
        # create a certificate wrapper
90
        certificate = Certificate(-1, subject.common_name, not_before_formatted, not_after_formatted, cert_pem,
91 ca3ac7c0 Stanislav Král
                                  subject_key.private_key_id, INTERMEDIATE_CA_ID, issuer_cert.certificate_id, usages)
92 bbcb7c89 Stanislav Král
93
        # store the wrapper into the repository
94
        created_id = self.certificate_repository.create(certificate)
95
96
        # assign the generated ID to the inserted certificate
97
        certificate.certificate_id = created_id
98
99
        return certificate
100
101 4c19a9b1 Stanislav Král
    def create_end_cert(self, subject_key: PrivateKey, subject: Subject, issuer_cert: Certificate,
102
                        issuer_key: PrivateKey,
103
                        extensions: str = "", days: int = 30, usages=None):
104
        if usages is None:
105
            usages = {}
106
107
        # generate a new certificate
108
        cert_pem = self.cryptography_service.create_crt(subject, subject_key.private_key, issuer_cert.pem_data,
109
                                                        issuer_key.private_key,
110
                                                        subject_key_pass=subject_key.password,
111
                                                        issuer_key_pass=issuer_key.password, extensions=extensions,
112
                                                        days=days)
113
114
        print(f"test cert {issuer_cert.certificate_id}")
115
        # wrap the generated certificate using Certificate class
116
        certificate = self.__create_wrapper(cert_pem, subject_key.private_key_id, subject.common_name, usages,
117
                                            issuer_cert.certificate_id, CERTIFICATE_ID)
118
119
        print(f"parent cert {certificate.parent_id}")
120
121
        created_id = self.certificate_repository.create(certificate)
122
123
        certificate.certificate_id = created_id
124
125
        print(f"parent cert {certificate.parent_id}")
126
127
        return certificate
128
129 10fab051 Stanislav Král
    def get_certificate(self, unique_id: int) -> Certificate:
130
        return self.certificate_repository.read(unique_id)