1
|
from typing import List, Dict
|
2
|
|
3
|
from ..db_objects.certificate import Certificate
|
4
|
from ..dao.repository import IRepository
|
5
|
from db_manager import DBManager
|
6
|
from ..constants import *
|
7
|
|
8
|
|
9
|
class CertificateRepositoryImpl(IRepository):
|
10
|
|
11
|
def create(self, common_name: str, valid_from: str, valid_to: str, pem_data: str,
|
12
|
private_key_id: int, type_id: int, parent_id: int, usages: Dict[int, bool]) -> bool:
|
13
|
"""
|
14
|
Creates a certificate
|
15
|
|
16
|
:param common_name: private key of the certificate
|
17
|
:param valid_from: string of date (time) of validation after (from)
|
18
|
:param valid_to: string of date (time) of validation before (to)
|
19
|
:param pem_data: data of certificate in a PEM format
|
20
|
:param private_key_id: ID from PrivateKeys table for specific private key
|
21
|
:param type_id: ID from CertificateTypes table (ROOT_CA_ID=1, INTERMEDIATE_CA_ID=2, CERTIFICATE_ID=3)
|
22
|
:param parent_id: ID from Certificates table for specific parent certificate
|
23
|
for root CA use e.g. -1 (ID will be changed after created certificate)
|
24
|
:param usages: dictionary of usages IDs which certificate use
|
25
|
Dictionary[Integer, Boolean]
|
26
|
- Key = ID (CA_ID=1, SSL_ID=2, SIGNATURE_ID=3, AUTHENTICATION_ID=4)
|
27
|
- Value = used=True/unused=False
|
28
|
|
29
|
:return: the result of whether the creation was successful
|
30
|
"""
|
31
|
|
32
|
sql = (f"INSERT INTO {TAB_CERTIFICATES} ({COL_COMMON_NAME},{COL_VALID_FROM},{COL_VALID_TO},{COL_PEM_DATA},"
|
33
|
f"{COL_PRIVATE_KEY_ID},{COL_TYPE_ID},{COL_PARENT_ID})"
|
34
|
f"VALUES(?,?,?,?,?,?,?)")
|
35
|
result = DBManager.create(sql, [common_name, valid_from, valid_to, pem_data,
|
36
|
private_key_id, type_id, parent_id], usages)
|
37
|
return result
|
38
|
|
39
|
def read(self, certificate_id: int = None):
|
40
|
"""
|
41
|
Reads (selects) a certificate or certificates
|
42
|
|
43
|
:param certificate_id: ID of specific certificate
|
44
|
|
45
|
:return: instance of Certificate class if ID was used otherwise list of this instances
|
46
|
"""
|
47
|
|
48
|
sql_extend = ""
|
49
|
|
50
|
if certificate_id is not None:
|
51
|
sql_extend = f" WHERE {COL_ID} = ?"
|
52
|
|
53
|
sql = f"SELECT * FROM {TAB_CERTIFICATES}{sql_extend}"
|
54
|
certificate_rows = DBManager.read(sql, certificate_id)
|
55
|
|
56
|
certificates: list = []
|
57
|
|
58
|
for certificate_row in certificate_rows:
|
59
|
sql = f"SELECT * FROM {TAB_CERTIFICATE_USAGES} WHERE {COL_CERTIFICATE_ID} = ?"
|
60
|
usage_rows = DBManager.read(sql, certificate_row[0])
|
61
|
|
62
|
usage_dict: Dict[int, bool] = {}
|
63
|
for usage_row in usage_rows:
|
64
|
usage_dict[usage_row[1]] = True
|
65
|
|
66
|
certificates.append(Certificate(certificate_row[0],
|
67
|
certificate_row[1],
|
68
|
certificate_row[2],
|
69
|
certificate_row[3],
|
70
|
certificate_row[4],
|
71
|
certificate_row[5],
|
72
|
certificate_row[6],
|
73
|
certificate_row[7],
|
74
|
usage_dict))
|
75
|
|
76
|
if certificate_id is not None:
|
77
|
return certificates[0]
|
78
|
|
79
|
return certificates
|
80
|
|
81
|
def update(self, certificate_id: int, common_name: str = None, valid_from: str = None, valid_to: str = None,
|
82
|
pem_data: str = None, private_key_id: int = None, type_id: int = None, parent_id: int = None,
|
83
|
usages: Dict[int, bool] = None) -> bool:
|
84
|
"""
|
85
|
Updates a certificate
|
86
|
|
87
|
:param certificate_id: ID of specific certificate
|
88
|
:param common_name: private key of the certificate
|
89
|
:param valid_from: string of date (time) of validation after (from)
|
90
|
:param valid_to: string of date (time) of validation before (to)
|
91
|
:param pem_data: data of certificate in a PEM format
|
92
|
:param private_key_id: ID from PrivateKeys table for specific private key
|
93
|
:param type_id: ID from CertificateTypes table (ROOT_CA_ID=1, INTERMEDIATE_CA_ID=2, CERTIFICATE_ID=3)
|
94
|
:param parent_id: ID from Certificates table for specific parent certificate
|
95
|
for root CA use e.g. -1 (ID will be changed after created certificate)
|
96
|
:param usages: dictionary of usages IDs which certificate use
|
97
|
Dictionary[Integer, Boolean]
|
98
|
- Key = ID (CA_ID=1, SSL_ID=2, SIGNATURE_ID=3, AUTHENTICATION_ID=4)
|
99
|
- Value = used=True/unused=False
|
100
|
|
101
|
:return: the result of whether the updation was successful
|
102
|
"""
|
103
|
|
104
|
updated_list = []
|
105
|
values = []
|
106
|
if common_name is not None:
|
107
|
updated_list.append(f"{COL_COMMON_NAME} = ?")
|
108
|
values.append(common_name)
|
109
|
if valid_from is not None:
|
110
|
updated_list.append(f"{COL_VALID_FROM} = ?")
|
111
|
values.append(valid_from)
|
112
|
if valid_to is not None:
|
113
|
updated_list.append(f"{COL_VALID_TO} = ?")
|
114
|
values.append(valid_to)
|
115
|
if pem_data is not None:
|
116
|
updated_list.append(f"{COL_PEM_DATA} = ?")
|
117
|
values.append(pem_data)
|
118
|
if private_key_id is not None:
|
119
|
updated_list.append(f"{COL_PRIVATE_KEY_ID} = ?")
|
120
|
values.append(private_key_id)
|
121
|
if type_id is not None:
|
122
|
updated_list.append(f"{COL_TYPE_ID} = ?")
|
123
|
values.append(type_id)
|
124
|
if parent_id is not None:
|
125
|
updated_list.append(f"{COL_PARENT_ID} = ?")
|
126
|
values.append(parent_id)
|
127
|
|
128
|
updated_str = ", ".join(updated_list)
|
129
|
sql = f"UPDATE {TAB_CERTIFICATES} SET {updated_str} WHERE {COL_ID} = ?"
|
130
|
result = DBManager.update(sql, certificate_id, values, usages)
|
131
|
return result
|
132
|
|
133
|
def delete(self, certificate_id: int) -> bool:
|
134
|
"""
|
135
|
Deletes a certificate
|
136
|
|
137
|
:param certificate_id: ID of specific certificate
|
138
|
|
139
|
:return: the result of whether the deletion was successful
|
140
|
"""
|
141
|
|
142
|
sql = f"DELETE FROM {TAB_CERTIFICATES} WHERE {COL_ID} = ?"
|
143
|
result = DBManager.delete(sql, certificate_id, True)
|
144
|
return result
|