Projekt

Obecné

Profil

Stáhnout (1.96 KB) Statistiky
| Větev: | Tag: | Revize:
1
from datetime import datetime
2

    
3
from injector import inject
4

    
5
from src.dao.certificate_repository import CertificateRepository
6
from src.services.crl.ca_index_file_line_generator import create_index_file_revoked_line
7
from src.services.cryptography import CryptographyService
8

    
9

    
10
class CrlService:
11
    @inject
12
    def __init__(self,
13
                 certificate_repository: CertificateRepository,
14
                 cryptography_service: CryptographyService
15
                 ):
16
        self.certificate_repository = certificate_repository
17
        self.cryptography_service = cryptography_service
18

    
19
    def create_revoked_index(self, ca_id) -> str:
20
        """
21
        Queries the certificate repository and looks for all certificates revoked by the certificate authority given
22
        by the passed ID. Found certificates are then put into a string representing the CA's database index file.
23
        
24
        :param ca_id: ID of the CA whose revoked certificates should be put into the index file
25
        :return: a str representing the content of a CA index file
26
        """
27

    
28
        index_lines = []
29
        # iterate over revoked certificates of the CA given by an ID
30
        for certificate in self.certificate_repository.get_all_revoked_by(ca_id):
31
            # extract the complete subject information and not_after date field
32
            subject, _, not_after = self.cryptography_service.parse_cert_pem(certificate.pem_data)
33

    
34
            line = create_index_file_revoked_line(certificate,
35
                                                  subject,
36
                                                  # parse revocation date from unix timestamp to struct_time
37
                                                  datetime.utcfromtimestamp(int(certificate.revocation_date)).timetuple(),
38
                                                  not_after)
39

    
40
            # append it to the list of lines
41
            index_lines.append(line)
42

    
43
        # join all lines with a new line
44
        return "\n".join(index_lines)
(3-3/3)