1
|
|
|
# -*- coding: utf-8 -*- |
2
|
|
|
# Copyright (C) 2021 Greenbone Networks GmbH |
3
|
|
|
# |
4
|
|
|
# SPDX-License-Identifier: GPL-3.0-or-later |
5
|
|
|
# |
6
|
|
|
# This program is free software: you can redistribute it and/or modify |
7
|
|
|
# it under the terms of the GNU General Public License as published by |
8
|
|
|
# the Free Software Foundation, either version 3 of the License, or |
9
|
|
|
# (at your option) any later version. |
10
|
|
|
# |
11
|
|
|
# This program is distributed in the hope that it will be useful, |
12
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
13
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14
|
|
|
# GNU General Public License for more details. |
15
|
|
|
# |
16
|
|
|
# You should have received a copy of the GNU General Public License |
17
|
|
|
# along with this program. If not, see <http://www.gnu.org/licenses/>. |
18
|
|
|
|
19
|
|
|
# pylint: disable=redefined-builtin |
20
|
|
|
|
21
|
|
|
from typing import Any, Optional |
22
|
|
|
|
23
|
|
|
from gvm.errors import RequiredArgument |
24
|
|
|
from gvm.utils import add_filter, to_bool |
25
|
|
|
from gvm.xml import XmlCommand |
26
|
|
|
|
27
|
|
|
|
28
|
|
|
class TLSCertificateMixin: |
29
|
|
|
def clone_tls_certificate(self, tls_certificate_id: str) -> Any: |
30
|
|
|
"""Modifies an existing TLS certificate. |
31
|
|
|
|
32
|
|
|
Arguments: |
33
|
|
|
tls_certificate_id: The UUID of an existing TLS certificate |
34
|
|
|
|
35
|
|
|
Returns: |
36
|
|
|
The response. See :py:meth:`send_command` for details. |
37
|
|
|
""" |
38
|
|
|
if not tls_certificate_id: |
39
|
|
|
raise RequiredArgument( |
40
|
|
|
function=self.clone_tls_certificate.__name__, |
41
|
|
|
argument='tls_certificate_id', |
42
|
|
|
) |
43
|
|
|
|
44
|
|
|
cmd = XmlCommand("create_tls_certificate") |
45
|
|
|
|
46
|
|
|
cmd.add_element("copy", tls_certificate_id) |
47
|
|
|
|
48
|
|
|
return self._send_xml_command(cmd) |
49
|
|
|
|
50
|
|
View Code Duplication |
def create_tls_certificate( |
|
|
|
|
51
|
|
|
self, |
52
|
|
|
name: str, |
53
|
|
|
certificate: str, |
54
|
|
|
*, |
55
|
|
|
comment: Optional[str] = None, |
56
|
|
|
trust: Optional[bool] = None, |
57
|
|
|
) -> Any: |
58
|
|
|
"""Create a new TLS certificate |
59
|
|
|
|
60
|
|
|
Arguments: |
61
|
|
|
name: Name of the TLS certificate, defaulting to the MD5 |
62
|
|
|
fingerprint. |
63
|
|
|
certificate: The Base64 encoded certificate data (x.509 DER or PEM). |
64
|
|
|
comment: Comment for the TLS certificate. |
65
|
|
|
trust: Whether the certificate is trusted. |
66
|
|
|
|
67
|
|
|
Returns: |
68
|
|
|
The response. See :py:meth:`send_command` for details. |
69
|
|
|
""" |
70
|
|
|
if not name: |
71
|
|
|
raise RequiredArgument( |
72
|
|
|
function=self.create_tls_certificate.__name__, argument='name' |
73
|
|
|
) |
74
|
|
|
if not certificate: |
75
|
|
|
raise RequiredArgument( |
76
|
|
|
function=self.create_tls_certificate.__name__, |
77
|
|
|
argument='certificate', |
78
|
|
|
) |
79
|
|
|
|
80
|
|
|
cmd = XmlCommand("create_tls_certificate") |
81
|
|
|
|
82
|
|
|
if comment: |
83
|
|
|
cmd.add_element("comment", comment) |
84
|
|
|
|
85
|
|
|
cmd.add_element("name", name) |
86
|
|
|
cmd.add_element("certificate", certificate) |
87
|
|
|
|
88
|
|
|
if trust: |
89
|
|
|
cmd.add_element("trust", to_bool(trust)) |
90
|
|
|
|
91
|
|
|
return self._send_xml_command(cmd) |
92
|
|
|
|
93
|
|
|
def delete_tls_certificate(self, tls_certificate_id: str) -> Any: |
94
|
|
|
"""Deletes an existing tls certificate |
95
|
|
|
|
96
|
|
|
Arguments: |
97
|
|
|
tls_certificate_id: UUID of the tls certificate to be deleted. |
98
|
|
|
""" |
99
|
|
|
if not tls_certificate_id: |
100
|
|
|
raise RequiredArgument( |
101
|
|
|
function=self.delete_tls_certificate.__name__, |
102
|
|
|
argument='tls_certificate_id', |
103
|
|
|
) |
104
|
|
|
|
105
|
|
|
cmd = XmlCommand("delete_tls_certificate") |
106
|
|
|
cmd.set_attribute("tls_certificate_id", tls_certificate_id) |
107
|
|
|
|
108
|
|
|
return self._send_xml_command(cmd) |
109
|
|
|
|
110
|
|
|
def get_tls_certificates( |
111
|
|
|
self, |
112
|
|
|
*, |
113
|
|
|
filter: Optional[str] = None, |
114
|
|
|
filter_id: Optional[str] = None, |
115
|
|
|
include_certificate_data: Optional[bool] = None, |
116
|
|
|
details: Optional[bool] = None, |
117
|
|
|
) -> Any: |
118
|
|
|
"""Request a list of TLS certificates |
119
|
|
|
|
120
|
|
|
Arguments: |
121
|
|
|
filter: Filter term to use for the query |
122
|
|
|
filter_id: UUID of an existing filter to use for the query |
123
|
|
|
include_certificate_data: Whether to include the certificate data in |
124
|
|
|
the response |
125
|
|
|
details: Whether to include additional details of the |
126
|
|
|
tls certificates |
127
|
|
|
|
128
|
|
|
Returns: |
129
|
|
|
The response. See :py:meth:`send_command` for details. |
130
|
|
|
""" |
131
|
|
|
|
132
|
|
|
cmd = XmlCommand("get_tls_certificates") |
133
|
|
|
|
134
|
|
|
add_filter(cmd, filter, filter_id) |
135
|
|
|
|
136
|
|
|
if details is not None: |
137
|
|
|
cmd.set_attribute("details", to_bool(details)) |
138
|
|
|
|
139
|
|
|
if include_certificate_data is not None: |
140
|
|
|
cmd.set_attribute( |
141
|
|
|
"include_certificate_data", to_bool(include_certificate_data) |
142
|
|
|
) |
143
|
|
|
|
144
|
|
|
return self._send_xml_command(cmd) |
145
|
|
|
|
146
|
|
|
def get_tls_certificate(self, tls_certificate_id: str) -> Any: |
147
|
|
|
"""Request a single TLS certificate |
148
|
|
|
|
149
|
|
|
Arguments: |
150
|
|
|
tls_certificate_id: UUID of an existing TLS certificate |
151
|
|
|
|
152
|
|
|
Returns: |
153
|
|
|
The response. See :py:meth:`send_command` for details. |
154
|
|
|
""" |
155
|
|
|
cmd = XmlCommand("get_tls_certificates") |
156
|
|
|
|
157
|
|
|
if not tls_certificate_id: |
158
|
|
|
raise RequiredArgument( |
159
|
|
|
function=self.get_tls_certificate.__name__, |
160
|
|
|
argument='tls_certificate_id', |
161
|
|
|
) |
162
|
|
|
|
163
|
|
|
cmd.set_attribute("tls_certificate_id", tls_certificate_id) |
164
|
|
|
|
165
|
|
|
# for single tls certificate always request cert data |
166
|
|
|
cmd.set_attribute("include_certificate_data", "1") |
167
|
|
|
|
168
|
|
|
# for single entity always request all details |
169
|
|
|
cmd.set_attribute("details", "1") |
170
|
|
|
|
171
|
|
|
return self._send_xml_command(cmd) |
172
|
|
|
|
173
|
|
View Code Duplication |
def modify_tls_certificate( |
|
|
|
|
174
|
|
|
self, |
175
|
|
|
tls_certificate_id: str, |
176
|
|
|
*, |
177
|
|
|
name: Optional[str] = None, |
178
|
|
|
comment: Optional[str] = None, |
179
|
|
|
trust: Optional[bool] = None, |
180
|
|
|
) -> Any: |
181
|
|
|
"""Modifies an existing TLS certificate. |
182
|
|
|
|
183
|
|
|
Arguments: |
184
|
|
|
tls_certificate_id: UUID of the TLS certificate to be modified. |
185
|
|
|
name: Name of the TLS certificate, defaulting to the MD5 fingerprint |
186
|
|
|
comment: Comment for the TLS certificate. |
187
|
|
|
trust: Whether the certificate is trusted. |
188
|
|
|
|
189
|
|
|
Returns: |
190
|
|
|
The response. See :py:meth:`send_command` for details. |
191
|
|
|
""" |
192
|
|
|
if not tls_certificate_id: |
193
|
|
|
raise RequiredArgument( |
194
|
|
|
function=self.modify_tls_certificate.__name__, |
195
|
|
|
argument='tls_certificate_id', |
196
|
|
|
) |
197
|
|
|
|
198
|
|
|
cmd = XmlCommand("modify_tls_certificate") |
199
|
|
|
cmd.set_attribute("tls_certificate_id", str(tls_certificate_id)) |
200
|
|
|
|
201
|
|
|
if comment: |
202
|
|
|
cmd.add_element("comment", comment) |
203
|
|
|
|
204
|
|
|
if name: |
205
|
|
|
cmd.add_element("name", name) |
206
|
|
|
|
207
|
|
|
if trust: |
208
|
|
|
cmd.add_element("trust", to_bool(trust)) |
209
|
|
|
|
210
|
|
|
return self._send_xml_command(cmd) |
211
|
|
|
|