1
|
from src.constants import ROOT_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
|
|
12
|
|
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
|
# TODO key passphrase is not present in class diagram
|
20
|
def create_root_ca(self, key: PrivateKey, subject: Subject, extensions: str = "", config: str = ""):
|
21
|
# create a new self signed certificate
|
22
|
cert_pem = self.cryptography_service.create_sscrt(subject, key.private_key, key_pass=key.password,
|
23
|
extensions=extensions, config=config)
|
24
|
|
25
|
# parse the generated pem for subject and notBefore/notAfter fields
|
26
|
subj, not_before, not_after = self.cryptography_service.parse_cert_pem(cert_pem)
|
27
|
|
28
|
# format the parsed date
|
29
|
not_before_formatted = time.strftime(DATE_FORMAT, not_before)
|
30
|
not_after_formatted = time.strftime(DATE_FORMAT, not_after)
|
31
|
|
32
|
# create a certificate wrapper
|
33
|
certificate = Certificate(-1, subject.common_name, not_before_formatted, not_after_formatted, cert_pem,
|
34
|
key.private_key_id, ROOT_CA_ID, 0, {})
|
35
|
|
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
|
|
42
|
return certificate
|