Projekt

Obecné

Profil

Stáhnout (1.85 KB) Statistiky
| Větev: | Tag: | Revize:
1 313b647b Stanislav Král
from src.constants import ROOT_CA_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
12 4a40b0d2 Stanislav Král
13
class CertificateService:
14
15
    def __init__(self, cryptography_service: CryptographyService, certificate_repository: CertificateRepository):
16
        self.cryptography_service = cryptography_service
17
        self.certificate_repository = certificate_repository
18
19 313b647b Stanislav Král
    def create_root_ca(self, key: PrivateKey, subject: Subject, extensions: str = "", config: str = ""):
20
        # create a new self signed  certificate
21
        cert_pem = self.cryptography_service.create_sscrt(subject, key.private_key, key_pass=key.password,
22
                                                          extensions=extensions, config=config)
23
24
        # parse the generated pem for subject and notBefore/notAfter fields
25
        subj, not_before, not_after = self.cryptography_service.parse_cert_pem(cert_pem)
26
27
        # format the parsed date
28
        not_before_formatted = time.strftime(DATE_FORMAT, not_before)
29
        not_after_formatted = time.strftime(DATE_FORMAT, not_after)
30
31
        # create a certificate wrapper
32
        certificate = Certificate(-1, subject.common_name, not_before_formatted, not_after_formatted, cert_pem,
33
                                  key.private_key_id, ROOT_CA_ID, 0, {})
34
35
        # store the wrapper into the repository
36
        created_id = self.certificate_repository.create(certificate)
37
38
        # assign the generated ID to the inserted certificate
39
        certificate.certificate_id = created_id
40 4a40b0d2 Stanislav Král
41 313b647b Stanislav Král
        return certificate
42 10fab051 Stanislav Král
43
    def get_certificate(self, unique_id: int) -> Certificate:
44
        return self.certificate_repository.read(unique_id)