Projekt

Obecné

Profil

Stáhnout (5.8 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
        return certificate
56
57 bbcb7c89 Stanislav Král
    # TODO config parameter present in class diagram but not here (unused)
58
    def create_ca(self, subject_key: PrivateKey, subject: Subject, issuer_cert: Certificate, issuer_key: PrivateKey,
59 ca3ac7c0 Stanislav Král
                  extensions: str = "", days: int = 30, usages=None):
60
        if usages is None:
61
            usages = {}
62
63 bbcb7c89 Stanislav Král
        extensions = extensions + "\n" + CA_EXTENSIONS
64
        # TODO implement AIA URI via extensions
65
        cert_pem = self.cryptography_service.create_crt(subject, subject_key.private_key, issuer_cert.pem_data,
66
                                                        issuer_key.private_key,
67
                                                        subject_key_pass=subject_key.password,
68
                                                        issuer_key_pass=issuer_key.password, extensions=extensions,
69
                                                        days=days)
70
71 4c19a9b1 Stanislav Král
        # specify CA usage
72
        usages[CA_ID] = True
73
74
        # wrap into Certificate class
75
        self.__create_wrapper(cert_pem, subject_key.private_key_id, subject.common_name, usages,
76
                              issuer_cert.certificate_id, INTERMEDIATE_CA_ID)
77
78 bbcb7c89 Stanislav Král
        # parse the generated pem for subject and notBefore/notAfter fields
79
        subj, not_before, not_after = self.cryptography_service.parse_cert_pem(cert_pem)
80
81
        # format the parsed date
82
        not_before_formatted = time.strftime(DATE_FORMAT, not_before)
83
        not_after_formatted = time.strftime(DATE_FORMAT, not_after)
84
85 ca3ac7c0 Stanislav Král
        # specify CA usage
86
        usages[CA_ID] = True
87
88 bbcb7c89 Stanislav Král
        # create a certificate wrapper
89
        certificate = Certificate(-1, subject.common_name, not_before_formatted, not_after_formatted, cert_pem,
90 ca3ac7c0 Stanislav Král
                                  subject_key.private_key_id, INTERMEDIATE_CA_ID, issuer_cert.certificate_id, usages)
91 bbcb7c89 Stanislav Král
92
        # store the wrapper into the repository
93
        created_id = self.certificate_repository.create(certificate)
94
95
        # assign the generated ID to the inserted certificate
96
        certificate.certificate_id = created_id
97
98
        return certificate
99
100 4c19a9b1 Stanislav Král
    def create_end_cert(self, subject_key: PrivateKey, subject: Subject, issuer_cert: Certificate,
101
                        issuer_key: PrivateKey,
102
                        extensions: str = "", days: int = 30, usages=None):
103
        if usages is None:
104
            usages = {}
105
106
        # generate a new certificate
107
        cert_pem = self.cryptography_service.create_crt(subject, subject_key.private_key, issuer_cert.pem_data,
108
                                                        issuer_key.private_key,
109
                                                        subject_key_pass=subject_key.password,
110
                                                        issuer_key_pass=issuer_key.password, extensions=extensions,
111
                                                        days=days)
112
113
        # wrap the generated certificate using Certificate class
114
        certificate = self.__create_wrapper(cert_pem, subject_key.private_key_id, subject.common_name, usages,
115
                                            issuer_cert.certificate_id, CERTIFICATE_ID)
116
117
        created_id = self.certificate_repository.create(certificate)
118
119
        certificate.certificate_id = created_id
120
121
        return certificate
122
123 10fab051 Stanislav Král
    def get_certificate(self, unique_id: int) -> Certificate:
124
        return self.certificate_repository.read(unique_id)
125 2a90f4fd Stanislav Král
126
    def get_certificates(self, cert_type=None) -> Certificate:
127
        return self.certificate_repository.read_all(cert_type)