Projekt

Obecné

Profil

Stáhnout (28 KB) Statistiky
| Větev: | Tag: | Revize:
1
from typing import List
2

    
3
from injector import inject
4

    
5
from src.config.configuration import Configuration
6
from src.constants import ROOT_CA_ID, INTERMEDIATE_CA_ID, CA_ID, CERTIFICATE_ID, CERTIFICATE_STATES, \
7
    CERTIFICATE_REVOCATION_REASONS, SSL_ID, SIGNATURE_ID, AUTHENTICATION_ID, CERTIFICATE_VALID, CERTIFICATE_EXPIRED, \
8
    CERTIFICATE_REVOKED,\
9
    CERTIFICATE_REVOCATION_REASON_HOLD
10
from src.dao.certificate_repository import CertificateRepository
11
from src.exceptions.certificate_not_found_exception import CertificateNotFoundException
12
from src.exceptions.database_exception import DatabaseException
13
from src.exceptions.unknown_exception import UnknownException
14
from src.model.certificate import Certificate
15
from src.model.private_key import PrivateKey
16
from src.model.subject import Subject
17
from src.services.cryptography import CryptographyService
18

    
19
import time
20

    
21
from src.utils.usages_to_extensions import usages_to_extension_lines, ExtensionFieldFlags, CRITICAL, KEY_CERT_SIGN, \
22
    CRL_SIGN, CA, DIGITAL_SIGNATURE, KEY_ENCIPHERMENT, KEY_AGREEMENT, SERVER_AUTH, NON_REPUDIATION, TIME_STAMPING, \
23
    CLIENT_AUTH
24

    
25
from src.utils.logger import Logger
26

    
27
VALID_FROM_TO_DATE_FORMAT = "%d.%m.%Y %H:%M:%S"
28
CA_EXTENSIONS = "basicConstraints=critical,CA:TRUE"
29
CRL_EXTENSION = "crlDistributionPoints=URI:"
30
OCSP_EXTENSION = "authorityInfoAccess=OCSP;URI:"
31
STATUS_REVOKED = "revoked"
32
STATUS_VALID = "valid"
33

    
34
# define which flags are required for various usages
35
REQUIRED_USAGE_EXTENSION_FLAGS = {
36
    CA_ID: ExtensionFieldFlags({CRITICAL, KEY_CERT_SIGN, CRL_SIGN}, {}, {CRITICAL, CA}),
37
    SSL_ID: ExtensionFieldFlags({DIGITAL_SIGNATURE, KEY_ENCIPHERMENT, KEY_AGREEMENT}, {SERVER_AUTH}, {}),
38
    SIGNATURE_ID: ExtensionFieldFlags({DIGITAL_SIGNATURE, NON_REPUDIATION}, {}, {}),
39
    AUTHENTICATION_ID: ExtensionFieldFlags({DIGITAL_SIGNATURE}, {CLIENT_AUTH}, {})}
40

    
41

    
42
class CertificateService:
43

    
44
    @inject
45
    def __init__(self, cryptography_service: CryptographyService,
46
                 certificate_repository: CertificateRepository,
47
                 configuration: Configuration):
48
        self.cryptography_service = cryptography_service
49
        self.certificate_repository = certificate_repository
50
        self.configuration = configuration
51

    
52
    # TODO usages present in method parameters but not in class diagram
53
    def create_root_ca(self, key: PrivateKey, subject: Subject, extensions: str = "", config: str = "",
54
                       usages=None, days=30):
55
        """
56
        Creates a root CA certificate based on the given parameters.
57
        :param key: Private key to be used when generating the certificate
58
        :param subject: Subject to be used put into the certificate
59
        :param config: String containing the configuration to be used
60
        :param extensions: Name of the section in the configuration representing extensions
61
        :param usages: A dictionary containing usages of the certificate to be generated (see constants.py)
62
        :param days: Number of days for which the generated cert. will be considered valid
63
        :return: An instance of Certificate class representing the generated root CA cert
64
        """
65

    
66
        Logger.debug("Function launched.")
67

    
68
        if usages is None:
69
            usages = {}
70

    
71
        cert_id = self.certificate_repository.get_next_id()
72

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

    
76
        # generate extension configuration lines based on the specified usages
77
        extensions = extensions + "\n" + "\n".join(usages_to_extension_lines(usages, REQUIRED_USAGE_EXTENSION_FLAGS))
78

    
79
        # create a new self signed  certificate
80
        cert_pem = self.cryptography_service.create_sscrt(subject, key.private_key, key_pass=key.password,
81
                                                          extensions=extensions, config=config, days=days, sn=cert_id)
82

    
83
        # wrap into Certificate class
84
        certificate = self.__create_wrapper(cert_pem, key.private_key_id, usages, 0,
85
                                            ROOT_CA_ID)
86

    
87
        # store the wrapper into the repository
88
        created_id = self.certificate_repository.create(certificate)
89

    
90
        # assign the generated ID to the inserted certificate
91
        certificate.certificate_id = created_id
92

    
93
        return certificate
94

    
95
    def __create_wrapper(self, cert_pem, private_key_id, usages, parent_id, cert_type):
96
        """
97
        Wraps the given parameters using the Certificate class. Uses CryptographyService to find out the notBefore and
98
        notAfter fields.
99
        :param cert_pem: PEM of the cert. to be wrapped
100
        :param private_key_id: ID of the private key used to create the given certificate
101
        :param usages: A dictionary containing usages of the generated certificate generated (see constants.py)
102
        :param parent_id: ID of the CA that issued this certificate
103
        :param cert_type: Type of this certificate (see constants.py)
104
        :return: An instance of the Certificate class wrapping the values passed  via method parameters
105
        """
106

    
107
        Logger.debug("Function launched.")
108

    
109
        # parse the generated pem for subject and notBefore/notAfter fields
110
        # TODO this could be improved in the future in such way that calling openssl is not required to parse the dates
111
        subj, not_before, not_after = self.cryptography_service.parse_cert_pem(cert_pem)
112
        # format the parsed date
113
        not_before_formatted = time.strftime(VALID_FROM_TO_DATE_FORMAT, not_before)
114
        not_after_formatted = time.strftime(VALID_FROM_TO_DATE_FORMAT, not_after)
115

    
116
        # create a certificate wrapper
117
        certificate = Certificate(-1, not_before_formatted, not_after_formatted, cert_pem, cert_type, parent_id,
118
                                  private_key_id, usages, subj.common_name, subj.country, subj.locality, subj.state,
119
                                  subj.organization, subj.organization_unit, subj.email_address)
120

    
121
        return certificate
122

    
123
    # TODO config parameter present in class diagram but not here (unused)
124
    def create_ca(self, subject_key: PrivateKey, subject: Subject, issuer_cert: Certificate, issuer_key: PrivateKey,
125
                  extensions: str = "", days: int = 30, usages=None):
126
        """
127
        Creates an intermediate CA certificate issued by the given parent CA.
128
        :param subject_key: Private key to be used when generating the certificate
129
        :param subject: Subject to be used put into the certificate
130
        :param issuer_cert: Issuer certificate that will sign the CSR required to create an intermediate CA
131
        :param issuer_key: PK used to generate the issuer certificate
132
        :param extensions: Extensions to be used when generating the certificate
133
        :param usages: A dictionary containing usages of the certificate to be generated (see constants.py)
134
        :param days: Number of days for which the generated cert. will be considered valid
135
        :return: An instance of Certificate class representing the generated intermediate CA cert
136
        """
137

    
138
        Logger.debug("Function launched.")
139

    
140
        if usages is None:
141
            usages = {}
142

    
143
        # specify CA usage
144
        usages[CA_ID] = True
145

    
146
        # generate extension configuration lines based on the specified usages
147
        extensions = extensions + "\n" + "\n".join(usages_to_extension_lines(usages, REQUIRED_USAGE_EXTENSION_FLAGS))
148

    
149
        # Add CRL and OCSP distribution point to certificate extensions
150
        cert_id = self.certificate_repository.get_next_id()
151
        extensions = extensions + "\n" + CRL_EXTENSION + " " + self.__get_crl_endpoint(issuer_cert.certificate_id)
152
        extensions = extensions + "\n" + OCSP_EXTENSION + " " + self.__get_ocsp_endpoint(issuer_cert.certificate_id)
153

    
154
        # TODO implement AIA URI via extensions
155
        cert_pem = self.cryptography_service.create_crt(subject, subject_key.private_key, issuer_cert.pem_data,
156
                                                        issuer_key.private_key,
157
                                                        subject_key_pass=subject_key.password,
158
                                                        issuer_key_pass=issuer_key.password, extensions=extensions,
159
                                                        days=days,
160
                                                        sn=cert_id)
161

    
162
        # wrap into Certificate class
163
        certificate = self.__create_wrapper(cert_pem, subject_key.private_key_id, usages,
164
                                            issuer_cert.certificate_id, INTERMEDIATE_CA_ID)
165

    
166
        # store the wrapper into the repository
167
        created_id = self.certificate_repository.create(certificate)
168

    
169
        # assign the generated ID to the inserted certificate
170
        certificate.certificate_id = created_id
171

    
172
        return certificate
173

    
174
    def create_end_cert(self, subject_key: PrivateKey, subject: Subject, issuer_cert: Certificate,
175
                        issuer_key: PrivateKey,
176
                        extensions: str = "", days: int = 30, usages=None):
177
        """
178
        Creates an end certificate issued by the given parent CA.
179
        :param subject_key: Private key to be used when generating the certificate
180
        :param subject: Subject to be used put into the certificate
181
        :param issuer_cert: Issuer certificate that will sign the CSR required to create an intermediate CA
182
        :param issuer_key: PK used to generate the issuer certificate
183
        :param extensions: Extensions to be used when generating the certificate
184
        :param usages: A dictionary containing usages of the certificate to be generated (see constants.py)
185
        :param days: Number of days for which the generated cert. will be considered valid
186
        :return: An instance of Certificate class representing the generated cert
187
        """
188

    
189
        Logger.debug("Function launched.")
190

    
191
        if usages is None:
192
            usages = {}
193

    
194
        # get the next certificate ID in order to be able to specify the serial number
195
        cert_id = self.certificate_repository.get_next_id()
196

    
197
        # generate extension configuration lines based on the specified usages
198
        extensions = extensions + "\n" + "\n".join(usages_to_extension_lines(usages, REQUIRED_USAGE_EXTENSION_FLAGS))
199

    
200
        # Add CRL and OCSP distribution point to certificate extensions
201
        extensions = extensions + "\n" + CRL_EXTENSION + " " + self.__get_crl_endpoint(issuer_cert.certificate_id)
202
        extensions = extensions + "\n" + OCSP_EXTENSION + " " + self.__get_ocsp_endpoint(issuer_cert.certificate_id)
203

    
204
        # generate a new certificate
205
        cert_pem = self.cryptography_service.create_crt(subject, subject_key.private_key, issuer_cert.pem_data,
206
                                                        issuer_key.private_key,
207
                                                        subject_key_pass=subject_key.password,
208
                                                        issuer_key_pass=issuer_key.password, extensions=extensions,
209
                                                        days=days,
210
                                                        sn=cert_id
211
                                                        )
212

    
213
        # wrap the generated certificate using Certificate class
214
        certificate = self.__create_wrapper(cert_pem, subject_key.private_key_id, usages,
215
                                            issuer_cert.certificate_id, CERTIFICATE_ID)
216

    
217
        created_id = self.certificate_repository.create(certificate)
218

    
219
        certificate.certificate_id = created_id
220

    
221
        return certificate
222

    
223
    def get_certificate(self, unique_id: int) -> Certificate:
224
        """
225
        Tries to fetch a certificate from the certificate repository using a given id.
226
        :param unique_id: ID of the certificate to be fetched
227
        :return: Instance of the Certificate class containing a certificate with the given id or `None` if such
228
        certificate is not found
229
        """
230

    
231
        Logger.debug("Function launched.")
232

    
233
        return self.certificate_repository.read(unique_id)
234

    
235
    def get_certificates(self, cert_type=None) -> List[Certificate]:
236
        """
237
        Tries to fetch a list of all certificates from the certificate repository. Using the `cert_type` parameter only
238
        certificates of the given type can be returned.
239
        :param cert_type: Type of certificates to be returned
240
        :return: List of instances of the Certificate class representing all certificates present in the certificate
241
        repository. An empty list is returned when no certificates are found.
242
        """
243

    
244
        Logger.debug("Function launched.")
245

    
246
        return self.certificate_repository.read_all(cert_type)
247

    
248
    def get_certificates_filter(self, target_types, target_usages, target_cn_substring, page, per_page):
249
        """
250
        Tries to fetch a filtered list of all certificates from the certificate repository.
251
        :param target_types: certificate types (filter)
252
        :param target_usages: certificate usages (filter)
253
        :param target_cn_substring: certificate CN substring (filter)
254
        :param page: target page
255
        :param per_page: target page size
256
        :return: List of instances of the Certificate class representing filtered certificates
257
                    present in the certificate repository. An empty list is returned when no certificates are found.
258
        """
259
        return self.certificate_repository.read_all_filter(target_types, target_usages, target_cn_substring,
260
                                                           page, per_page)
261

    
262
    def get_chain_of_trust(self, from_id: int, to_id: int = -1, exclude_root=True) -> List[Certificate]:
263
        """
264
        Traverses the certificate hierarchy tree upwards till a certificate with the `to_id` ID is found or till a
265
        root CA certificate is found. Root certificates are excluded from the chain by default.
266
        :param from_id: ID of the first certificate to be included in the chain of trust
267
        :param to_id: ID of the last certificate to be included in the chain of trust
268
        :param exclude_root: a flag indicating whether root CA certificate should be excluded
269
        :return: a list of certificates representing the chain of trust starting with the certificate given by `from_id`
270
        ID
271
        """
272

    
273
        Logger.debug("Function launched.")
274

    
275
        # read the first certificate of the chain
276
        start_cert = self.certificate_repository.read(from_id)
277

    
278
        # if no cert is found or the current cert is root CA and root CAs should be excluded, then return an empty list
279
        if start_cert is None or (start_cert.type_id == ROOT_CA_ID and exclude_root):
280
            return []
281

    
282
        current_cert = start_cert
283
        chain_of_trust = [current_cert]
284

    
285
        # TODO could possibly be simplified
286
        if start_cert.type_id == ROOT_CA_ID:
287
            # the first cert found is a root ca
288
            return chain_of_trust
289

    
290
        while True:
291
            parent_cert = self.certificate_repository.read(current_cert.parent_id)
292

    
293
            # check whether parent certificate exists
294
            if parent_cert is None:
295
                break
296

    
297
            # check whether the found certificate is a root certificate
298
            if parent_cert.type_id == ROOT_CA_ID:
299
                if not exclude_root:
300
                    # append the found root cert only if root certificates should not be excluded from the CoT
301
                    chain_of_trust.append(parent_cert)
302
                break
303

    
304
            # append the certificate
305
            chain_of_trust.append(parent_cert)
306

    
307
            # stop iterating over certificates if the id of the found certificate matches `to_id` method parameter
308
            if parent_cert.certificate_id == to_id:
309
                break
310

    
311
            current_cert = parent_cert
312

    
313
        return chain_of_trust
314

    
315
    def delete_certificate(self, unique_id):
316
        """
317
        Deletes a certificate. Raises an Exception should any unexpected behavior occur.
318

    
319
        :param unique_id: ID of specific certificate
320
        """
321

    
322
        Logger.debug("Function launched.")
323

    
324
        to_delete = self.certificate_repository.get_all_descendants_of(unique_id)
325
        if to_delete is None:
326
            Logger.error(f"No such certificate found 'ID = {unique_id}'.")
327
            raise CertificateNotFoundException(unique_id)
328

    
329
        for cert in to_delete:
330
            try:
331
                self.set_certificate_revocation_status(cert.certificate_id, STATUS_REVOKED)
332
            except CertificateAlreadyRevokedException:
333
                Logger.info(f"Certificate already revoked 'ID = {unique_id}'.")
334
                # TODO log as an info/debug, not warning or above <-- perfectly legal
335
                pass
336

    
337
            if not self.certificate_repository.delete(cert.certificate_id):
338
                Logger.error(f"The certificate has not been deleted 'ID = {cert.certificate_id}'.")
339

    
340
    def get_certificates_issued_by(self, issuer_id):
341
        """
342
        Returns a list of all children of a certificate identified by an unique_id.
343
        Raises a DatabaseException should any unexpected behavior occur.
344
        :param issuer_id: target certificate ID
345
        :return: children of unique_id
346
        """
347

    
348
        Logger.debug("Function launched.")
349

    
350
        try:
351
            if self.certificate_repository.read(issuer_id) is None:
352
                Logger.error(f"No such certificate found 'ID = {issuer_id}'.")
353
                raise CertificateNotFoundException(issuer_id)
354
        except DatabaseException:
355
            Logger.error(f"No such certificate found 'ID = {issuer_id}'.")
356
            raise CertificateNotFoundException(issuer_id)
357

    
358
        return self.certificate_repository.get_all_issued_by(issuer_id)
359

    
360
    def get_certificates_issued_by_filter(self, issuer_id, target_types, target_usages, target_cn_substring, page,
361
                                          per_page):
362
        """
363
        Returns a list of all children of a certificate identified by an unique_id.
364
        Filters the results according to target types, usages, cn substring and pagination.
365
        Raises a DatabaseException should any unexpected behavior occur.
366
        :param issuer_id: target certificate ID
367
        :param target_types: filter of types
368
        :param target_usages: filter of usages
369
        :param target_cn_substring: CN substring
370
        :param page: target page or None
371
        :param per_page: certs per page or None
372
        :return: list of certificates (a page if specified)
373
        """
374

    
375
        Logger.debug("Function launched.")
376

    
377
        try:
378
            if self.certificate_repository.read(issuer_id) is None:
379
                Logger.error(f"No such certificate found 'ID = {issuer_id}'.")
380
                raise CertificateNotFoundException(issuer_id)
381
        except DatabaseException:
382
            Logger.error(f"No such certificate found 'ID = {issuer_id}'.")
383
            raise CertificateNotFoundException(issuer_id)
384

    
385
        return self.certificate_repository.get_all_issued_by_filter(issuer_id, target_types, target_usages,
386
                                                                    target_cn_substring, page, per_page)
387

    
388
    def set_certificate_revocation_status(self, id, status, reason="unspecified"):
389
        """
390
        Set certificate status to 'valid' or 'revoked'.
391
        If the new status is revoked a reason can be provided -> default is unspecified
392
        :param reason: reason for revocation
393
        :param id: identifier of the certificate whose status is to be changed
394
        :param status: new status of the certificate
395
        :raises CertificateStatusInvalidException: if status is not valid
396
        :raises RevocationReasonInvalidException: if reason is not valid
397
        :raises CertificateNotFoundException: if certificate with given id cannot be found
398
        :raises CertificateCannotBeSetToValid: if certificate was already revoked and not on hold,
399
                it cannot be set revalidated
400
        :raises CertificateAlreadyRevokedException: if caller tries to revoke a certificate that is already revoked
401
        :raises UnknownException: if the database is corrupted
402
        """
403

    
404
        Logger.debug("Function launched.")
405

    
406
        if status not in CERTIFICATE_STATES:
407
            Logger.error(f"Wrong parameter, invalid status '{status}'.")
408
            raise CertificateStatusInvalidException(status)
409
        if reason not in CERTIFICATE_REVOCATION_REASONS:
410
            Logger.error(f"Wrong parameter, invalid reason '{reason}'.")
411
            raise RevocationReasonInvalidException(reason)
412

    
413
        # check whether the certificate exists
414
        certificate = self.certificate_repository.read(id)
415
        if certificate is None:
416
            Logger.error(f"No such certificate found 'ID = {id}'.")
417
            raise CertificateNotFoundException(id)
418

    
419
        updated = False
420
        if status == STATUS_VALID:
421
            # if the certificate is revoked but the reason is not certificateHold, it cannot be re-validated
422
            #    -> throw an exception
423
            if certificate.revocation_reason != "" and \
424
               certificate.revocation_reason != CERTIFICATE_REVOCATION_REASON_HOLD:
425
                raise CertificateCannotBeSetToValid(certificate.revocation_reason)
426
            updated = self.certificate_repository.clear_certificate_revocation(id)
427
        elif status == STATUS_REVOKED:
428
            # check if the certificate is not revoked already
429
            revoked = self.certificate_repository.get_all_revoked_by(certificate.parent_id)
430
            if certificate.certificate_id in [x.certificate_id for x in revoked]:
431
                Logger.error(f"Certificate already revoked 'ID = {id}'.")
432
                raise CertificateAlreadyRevokedException(id)
433

    
434
            revocation_timestamp = int(time.time())
435
            updated = self.certificate_repository.set_certificate_revoked(id, str(revocation_timestamp), reason)
436

    
437
        if not updated:
438
            Logger.error(f"Repository returned 'false' from clear_certificate_revocation() "
439
                         f"or set_certificate_revoked().")
440
            raise UnknownException("Repository returned 'false' from clear_certificate_revocation() "
441
                                   "or set_certificate_revoked().")
442

    
443
    def get_subject_from_certificate(self, certificate: Certificate) -> Subject:
444
        """
445
        Get Subject distinguished name from a Certificate
446
        :param certificate: certificate instance whose Subject shall be parsed
447
        :return: instance of Subject class containing resulting distinguished name
448
        """
449

    
450
        Logger.debug("Function launched.")
451

    
452
        subject = Subject(certificate.common_name,
453
                          certificate.country_code,
454
                          certificate.locality,
455
                          certificate.province,
456
                          certificate.organization,
457
                          certificate.organizational_unit,
458
                          certificate.email_address)
459
        return subject
460

    
461
    def get_public_key_from_certificate(self, certificate: Certificate):
462
        """
463
        Extracts a public key from the given certificate
464
        :param certificate: an instance of the Certificate class containing the certificate from which a public key
465
        should be extracted.
466
        :return: a string containing the extracted public key in PEM format
467
        """
468

    
469
        Logger.debug("Function launched.")
470

    
471
        return self.cryptography_service.extract_public_key_from_certificate(certificate.pem_data)
472

    
473
    def get_certificate_state(self, id: int) -> str:
474
        """
475
        Check whether the certificate is expired, valid or revoked.
476
            - in case it's revoked and expired, revoked is returned
477
        :param id: identifier of the certificate
478
        :return: certificates state from {valid, revoked, expired}
479
        :raises CertificateNotFoundException: in case id of non-existing certificate is entered
480
        """
481
        Logger.debug("Function launched.")
482
        status = CERTIFICATE_VALID
483

    
484
        # Read the selected certificate from the repository
485
        certificate = self.certificate_repository.read(id)
486
        if certificate is None:
487
            Logger.error("Certificate whose details were requested does not exist.")
488
            raise CertificateNotFoundException(id)
489

    
490
        # check the expiration date using OpenSSL
491
        if not self.cryptography_service.verify_cert(certificate.pem_data):
492
            status = CERTIFICATE_EXPIRED
493

    
494
        # check certificate revocation
495
        all_revoked_by_parent = self.certificate_repository.get_all_revoked_by(certificate.parent_id)
496
        all_revoked_by_parent_ids = [cert.certificate_id for cert in all_revoked_by_parent]
497

    
498
        if id in all_revoked_by_parent_ids:
499
            status = CERTIFICATE_REVOKED
500

    
501
        return status
502

    
503
    def __get_crl_endpoint(self, ca_identifier: int) -> str:
504
        """
505
        Get URL address of CRL distribution endpoint based on
506
        issuer's ID
507

    
508
        :param ca_identifier: ID of issuing authority
509
        :return: CRL endpoint for the given CA
510
        """
511

    
512
        Logger.debug("Function launched.")
513

    
514
        return self.configuration.base_server_url + "/api/crl/" + str(ca_identifier)
515

    
516
    def __get_ocsp_endpoint(self, ca_identifier: int) -> str:
517
        """
518
        Get URL address of OCSP distribution endpoint based on
519
        issuer's ID
520

    
521
        :param ca_identifier: ID of issuing authority
522
        :return: OCSP endpoint for the given CA
523
        """
524

    
525
        Logger.debug("Function launched.")
526

    
527
        return self.configuration.base_server_url + "/api/ocsp/" + str(ca_identifier)
528

    
529
    def generate_pkcs_identity(self, certificate: Certificate, cert_key: PrivateKey, identity_name: str, identity_passphrase: str):
530
        """
531
        Generates a PKCS identity of the certificate given by the specified ID while using the private key passed.
532
        A name of the identity to be used and certificate's passphrase have to be specified as well as the passphrase
533
        of certificate's private key (if encrypted).
534
        :param certificate: certificate to be put into the PKCS identity store
535
        :param cert_key: key used to sign the given certificate
536
        :param identity_name: name to be given to the identity to be created
537
        :param identity_passphrase: passphrase to be used to encrypt the identity
538
        :return: byte array containing the generated identity (PKCS12 store)
539
        """
540
        Logger.debug("Function launched.")
541

    
542
        # get the chain of trust of the certificate whose identity should be generated and exclude the certificate
543
        # whose chain of trust we are querying
544
        cot_pem_list = [cert.pem_data for cert in self.get_chain_of_trust(certificate.certificate_id, exclude_root=False)[1:]]
545

    
546
        return self.cryptography_service.generate_pkcs_identity(certificate.pem_data, cert_key.private_key,
547
                                                                identity_name,
548
                                                                identity_passphrase, cot_pem_list, cert_key.password)
549

    
550

    
551
class RevocationReasonInvalidException(Exception):
552
    """
553
    Exception that denotes that the caller was trying to revoke
554
    a certificate using an invalid revocation reason
555
    """
556

    
557
    def __init__(self, reason):
558
        self.reason = reason
559

    
560
    def __str__(self):
561
        return f"Revocation reason '{self.reason}' is not valid."
562

    
563

    
564
class CertificateStatusInvalidException(Exception):
565
    """
566
    Exception that denotes that the caller was trying to set
567
    a certificate to an invalid state
568
    """
569

    
570
    def __init__(self, status):
571
        self.status = status
572

    
573
    def __str__(self):
574
        return f"Certificate status '{self.status}' is not valid."
575

    
576

    
577
class CertificateAlreadyRevokedException(Exception):
578
    """
579
    Exception that denotes that the caller was trying to revoke
580
    a certificate that is already revoked
581
    """
582

    
583
    def __init__(self, id):
584
        self.id = id
585

    
586
    def __str__(self):
587
        return f"Certificate id '{self.id}' is already revoked."
588

    
589

    
590
class CertificateCannotBeSetToValid(Exception):
591
    """
592
    Exception that denotes that the caller was trying to
593
    set certificate to valid if the certificate was already
594
    revoked but not certificateHold.
595
    """
596

    
597
    def __init__(self, old_reason):
598
        self.old_state = old_reason
599

    
600
    def __str__(self):
601
        return "Cannot set revoked certificate back to valid when the certificate revocation reason is not " \
602
               "certificateHold. " \
603
               f"The revocation reason of the certificate is {self.old_state}"
(2-2/4)