1 |
ca3ac7c0
|
Stanislav Král
|
from src.constants import ROOT_CA_ID, INTERMEDIATE_CA_ID, 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 |
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 |
|
|
|
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 |
ca3ac7c0
|
Stanislav Král
|
# specify CA usage
|
38 |
|
|
usages[CA_ID] = True
|
39 |
|
|
|
40 |
313b647b
|
Stanislav Král
|
# create a certificate wrapper
|
41 |
|
|
certificate = Certificate(-1, subject.common_name, not_before_formatted, not_after_formatted, cert_pem,
|
42 |
ca3ac7c0
|
Stanislav Král
|
key.private_key_id, ROOT_CA_ID, 0, usages)
|
43 |
313b647b
|
Stanislav Král
|
|
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 |
4a40b0d2
|
Stanislav Král
|
|
50 |
313b647b
|
Stanislav Král
|
return certificate
|
51 |
10fab051
|
Stanislav Král
|
|
52 |
bbcb7c89
|
Stanislav Král
|
# 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 |
ca3ac7c0
|
Stanislav Král
|
extensions: str = "", days: int = 30, usages=None):
|
55 |
|
|
if usages is None:
|
56 |
|
|
usages = {}
|
57 |
|
|
|
58 |
bbcb7c89
|
Stanislav Král
|
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 |
ca3ac7c0
|
Stanislav Král
|
# specify CA usage
|
74 |
|
|
usages[CA_ID] = True
|
75 |
|
|
|
76 |
bbcb7c89
|
Stanislav Král
|
# create a certificate wrapper
|
77 |
|
|
certificate = Certificate(-1, subject.common_name, not_before_formatted, not_after_formatted, cert_pem,
|
78 |
ca3ac7c0
|
Stanislav Král
|
subject_key.private_key_id, INTERMEDIATE_CA_ID, issuer_cert.certificate_id, usages)
|
79 |
bbcb7c89
|
Stanislav Král
|
|
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 |
10fab051
|
Stanislav Král
|
def get_certificate(self, unique_id: int) -> Certificate:
|
89 |
|
|
return self.certificate_repository.read(unique_id)
|