Projekt

Obecné

Profil

Stáhnout (3.91 KB) Statistiky
| Větev: | Tag: | Revize:
1
from src.constants import ROOT_CA_ID, INTERMEDIATE_CA_ID, CA_ID
2
from src.dao.certificate_repository import CertificateRepository
3
from src.model.certificate import Certificate
4
from src.model.private_key import PrivateKey
5
from src.model.subject import Subject
6
from src.services.cryptography import CryptographyService
7

    
8
import time
9

    
10
DATE_FORMAT = "%d.%m.%Y %H:%M:%S"
11
CA_EXTENSIONS = "basicConstraints=critical,CA:TRUE"
12

    
13

    
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
    # TODO usages present in method parameters but not in class diagram
21
    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
        # 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

    
30
        # parse the generated pem for subject and notBefore/notAfter fields
31
        subj, not_before, not_after = self.cryptography_service.parse_cert_pem(cert_pem)
32

    
33
        # format the parsed date
34
        not_before_formatted = time.strftime(DATE_FORMAT, not_before)
35
        not_after_formatted = time.strftime(DATE_FORMAT, not_after)
36

    
37
        # specify CA usage
38
        usages[CA_ID] = True
39

    
40
        # create a certificate wrapper
41
        certificate = Certificate(-1, subject.common_name, not_before_formatted, not_after_formatted, cert_pem,
42
                                  key.private_key_id, ROOT_CA_ID, 0, usages)
43

    
44
        # store the wrapper into the repository
45
        created_id = self.certificate_repository.create(certificate)
46

    
47
        # assign the generated ID to the inserted certificate
48
        certificate.certificate_id = created_id
49

    
50
        return certificate
51

    
52
    # TODO config parameter present in class diagram but not here (unused)
53
    def create_ca(self, subject_key: PrivateKey, subject: Subject, issuer_cert: Certificate, issuer_key: PrivateKey,
54
                  extensions: str = "", days: int = 30, usages=None):
55
        if usages is None:
56
            usages = {}
57

    
58
        extensions = extensions + "\n" + CA_EXTENSIONS
59
        # TODO implement AIA URI via extensions
60
        cert_pem = self.cryptography_service.create_crt(subject, subject_key.private_key, issuer_cert.pem_data,
61
                                                        issuer_key.private_key,
62
                                                        subject_key_pass=subject_key.password,
63
                                                        issuer_key_pass=issuer_key.password, extensions=extensions,
64
                                                        days=days)
65

    
66
        # parse the generated pem for subject and notBefore/notAfter fields
67
        subj, not_before, not_after = self.cryptography_service.parse_cert_pem(cert_pem)
68

    
69
        # format the parsed date
70
        not_before_formatted = time.strftime(DATE_FORMAT, not_before)
71
        not_after_formatted = time.strftime(DATE_FORMAT, not_after)
72

    
73
        # specify CA usage
74
        usages[CA_ID] = True
75

    
76
        # create a certificate wrapper
77
        certificate = Certificate(-1, subject.common_name, not_before_formatted, not_after_formatted, cert_pem,
78
                                  subject_key.private_key_id, INTERMEDIATE_CA_ID, issuer_cert.certificate_id, usages)
79

    
80
        # store the wrapper into the repository
81
        created_id = self.certificate_repository.create(certificate)
82

    
83
        # assign the generated ID to the inserted certificate
84
        certificate.certificate_id = created_id
85

    
86
        return certificate
87

    
88
    def get_certificate(self, unique_id: int) -> Certificate:
89
        return self.certificate_repository.read(unique_id)
(2-2/4)