Revize 932420d8
Přidáno uživatelem Michal Seják před téměř 4 roky(ů)
src/services/key_service.py | ||
---|---|---|
25 | 25 |
# generate a new private key |
26 | 26 |
private_key_pem = self.cryptography_service.create_private_key(passphrase) |
27 | 27 |
|
28 |
# store the private key |
|
29 |
private_key = self.wrap_and_store_key(passphrase, private_key_pem) |
|
30 |
|
|
31 |
return private_key |
|
32 |
|
|
33 |
def wrap_and_store_key(self, passphrase, private_key_pem): |
|
34 |
""" |
|
35 |
Stores a previously unknown PK PEM/passphrase tuple in a PK repository while wrapping the tuple in a PrivateKey |
|
36 |
wrapper. |
|
37 |
:param passphrase: PK passphrase |
|
38 |
:param private_key_pem: PK PEM |
|
39 |
:return: a new PrivateKey wrapper |
|
40 |
""" |
|
41 |
Logger.debug("Function launched.") |
|
42 |
|
|
28 | 43 |
# store generated PK and the passphrase in a wrapper |
29 | 44 |
private_key = PrivateKey(-1, private_key_pem, passphrase) |
30 |
|
|
31 | 45 |
# store the wrapper in the PK repository |
32 | 46 |
private_key_id = self.private_key_repository.create(private_key) |
33 |
|
|
34 | 47 |
# assign the generated ID to the wrapper |
35 | 48 |
private_key.private_key_id = private_key_id |
49 |
return private_key |
|
50 |
|
|
51 |
def wrap_custom_key(self, key_pem, passphrase): |
|
52 |
""" |
|
53 |
Checks whether a supplied PEM/passphrase tuple is already known and tries to fetch it from a PK repository, |
|
54 |
if not, stores it in a PK repository. Returns a wrapped PEM/passphrase tuple in a PrivateKey instance together |
|
55 |
with its associated ID. |
|
56 |
:param key_pem: target PEM |
|
57 |
:param passphrase: corresponding passphrase |
|
58 |
:return: PrivateKey instance |
|
59 |
""" |
|
60 |
Logger.debug("Function launched.") |
|
61 |
|
|
62 |
# check if the supplied key is already known |
|
63 |
private_key = self.private_key_repository.find_pk(key_pem) |
|
64 |
|
|
65 |
# if not |
|
66 |
if private_key is None: |
|
67 |
# store as new |
|
68 |
private_key = self.wrap_and_store_key(passphrase, key_pem) |
|
36 | 69 |
|
37 | 70 |
return private_key |
38 | 71 |
|
... | ... | |
86 | 119 |
|
87 | 120 |
return self.cryptography_service.extract_public_key_from_private_key(private_key.private_key, |
88 | 121 |
private_key.password) |
122 |
|
|
123 |
def verify_key(self, key, passphrase): |
|
124 |
""" |
|
125 |
Verifies whether the provided key is encrypted by the provided passphrase. If passphrase is none, verifies |
|
126 |
that the provided key is unencrypted. |
|
127 |
Simply calls the namesake CryptographyService method. |
|
128 |
:param key: target key |
|
129 |
:param passphrase: target passphrase or None |
|
130 |
:return: True if the condition is fulfilled, else False |
|
131 |
""" |
|
132 |
return self.cryptography_service.verify_key(key, passphrase) |
Také k dispozici: Unified diff
Re #8705 - Added wrapping methods and the `verify_key` method to KeyService.