-
Notifications
You must be signed in to change notification settings - Fork 25
/
certsrv.py
504 lines (404 loc) · 18.1 KB
/
certsrv.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
"""
A Python client for the Microsoft AD Certificate Services web page.
https://github.com/magnuswatn/certsrv
"""
import os
import re
import base64
import logging
import warnings
import requests
__version__ = "2.1.1"
logger = logging.getLogger(__name__)
TIMEOUT = 30
class RequestDeniedException(Exception):
"""Signifies that the request was denied by the ADCS server."""
def __init__(self, message, response):
Exception.__init__(self, message)
self.response = response
class CouldNotRetrieveCertificateException(Exception):
"""Signifies that the certificate could not be retrieved."""
def __init__(self, message, response):
Exception.__init__(self, message)
self.response = response
class CertificatePendingException(Exception):
"""Signifies that the request needs to be approved by a CA admin."""
def __init__(self, req_id):
Exception.__init__(
self,
"Your certificate request has been received. "
"However, you must wait for an administrator to issue the "
"certificate you requested. Your Request Id is {0}.".format(req_id),
)
self.req_id = req_id
class Certsrv(object):
"""
Represents a Microsoft AD Certificate Services web server.
Args:
server: The FQDN to a server running the Certification Authority
Web Enrollment role (must be listening on https).
username: The username for authentication.
password: The password for authentication.
auth_method: The chosen authentication method. Either 'basic' (the default),
'ntlm' or 'cert' (SSL client certificate).
cafile: A PEM file containing the CA certificates that should be trusted.
timeout: The timeout to use against the CA server, in seconds.
The default is 30.
Note:
If you use a client certificate for authentication (auth_method=cert),
the username parameter should be the path to a certificate, and
the password parameter the path to a (unencrypted) private key.
"""
def __init__(self, server, username, password, auth_method="basic",
cafile=None, timeout=TIMEOUT):
self.server = server
self.timeout = timeout
self.auth_method = auth_method
self.session = requests.Session()
if cafile:
self.session.verify = cafile
else:
# requests uses it's own CA bundle by default
# but ADCS servers often have certificates
# from private CAs that are locally trusted,
# so we try to find, and use, the system bundle
# instead. Fallback to requests own.
self.session.verify = _get_ca_bundle()
self._set_credentials(username, password)
# We need certsrv to think we are a browser,
# or otherwise the Content-Type of the retrieved
# certificate will be wrong (for some reason).
self.session.headers = {
"User-agent": "Mozilla/5.0 certsrv (https://github.com/magnuswatn/certsrv)"
}
def _set_credentials(self, username, password):
if self.auth_method == "ntlm":
from requests_ntlm import HttpNtlmAuth
self.session.auth = HttpNtlmAuth(username, password)
elif self.auth_method == "cert":
self.session.cert = (username, password)
else:
self.session.auth = (username, password)
def _post(self, url, **kwargs):
response = self.session.post(url, timeout=self.timeout, **kwargs)
return self._handle_response(response)
def _get(self, url, **kwargs):
response = self.session.get(url, timeout=self.timeout, **kwargs)
return self._handle_response(response)
@staticmethod
def _handle_response(response):
logger.debug(
"Sent %s request to %s, with headers:\n%s\n\nand body:\n%s",
response.request.method,
response.request.url,
"\n".join(
["{0}: {1}".format(k, v) for k, v in response.request.headers.items()]
),
response.request.body,
)
try:
debug_content = response.content.decode()
except UnicodeDecodeError:
debug_content = base64.b64encode(response.content)
logger.debug(
"Recieved response:\nHTTP %s\n%s\n\n%s",
response.status_code,
"\n".join(["{0}: {1}".format(k, v) for k, v in response.headers.items()]),
debug_content,
)
response.raise_for_status()
return response
def get_cert(self, csr, template, encoding="b64", attributes=None):
"""
Gets a certificate from the ADCS server.
Args:
csr: The certificate request to submit.
template: The certificate template the cert should be issued from.
encoding: The desired encoding for the returned certificate.
Possible values are 'bin' for binary and 'b64' for Base64 (PEM).
attributes: Additional Attributes (request attibutes) to be sent along with
the request.
Returns:
The issued certificate.
Raises:
RequestDeniedException: If the request was denied by the ADCS server.
CertificatePendingException: If the request needs to be approved
by a CA admin.
CouldNotRetrieveCertificateException: If something went wrong while
fetching the cert.
"""
cert_attrib = "CertificateTemplate:{0}\r\n".format(template)
if attributes:
cert_attrib += attributes
data = {
"Mode": "newreq",
"CertRequest": csr,
"CertAttrib": cert_attrib,
"FriendlyType": "Saved-Request Certificate",
"TargetStoreFlags": "0",
"SaveCert": "yes",
}
url = "https://{0}/certsrv/certfnsh.asp".format(self.server)
response = self._post(url, data=data)
# We need to parse the Request ID from the returning HTML page
try:
req_id = re.search(r"certnew.cer\?ReqID=(\d+)&", response.text).group(1)
except AttributeError:
# We didn't find any request ID in the response. It may need approval.
if re.search(r"Certificate Pending", response.text):
req_id = re.search(r"Your Request Id is (\d+).", response.text).group(1)
raise CertificatePendingException(req_id)
else:
# Must have failed. Lets find the error message
# and raise a RequestDeniedException.
try:
error = re.search(
r'The disposition message is "([^"]+)', response.text
).group(1)
except AttributeError:
error = "An unknown error occured"
raise RequestDeniedException(error, response.text)
return self.get_existing_cert(req_id, encoding)
def get_existing_cert(self, req_id, encoding="b64"):
"""
Gets a certificate that has already been created from the ADCS server.
Args:
req_id: The request ID to retrieve.
encoding: The desired encoding for the returned certificate.
Possible values are 'bin' for binary and 'b64' for Base64 (PEM).
Returns:
The issued certificate.
Raises:
CouldNotRetrieveCertificateException: If something went wrong
while fetching the cert.
"""
cert_url = "https://{0}/certsrv/certnew.cer".format(self.server)
params = {"ReqID": req_id, "Enc": encoding}
response = self._get(cert_url, params=params)
if response.headers["Content-Type"] != "application/pkix-cert":
# The response was not a cert. Something must have gone wrong
try:
error = re.search(
"Disposition message:[^\t]+\t\t([^\r\n]+)", response.text
).group(1)
except AttributeError:
error = "An unknown error occured"
raise CouldNotRetrieveCertificateException(error, response.text)
else:
return response.content
def get_ca_cert(self, encoding="b64"):
"""
Gets the (newest) CA certificate from the ADCS server.
Args:
encoding: The desired encoding for the returned certificate.
Possible values are 'bin' for binary and 'b64' for Base64 (PEM).
Returns:
The newest CA certificate from the server.
"""
url = "https://{0}/certsrv/certcarc.asp".format(self.server)
response = self._get(url)
# We have to check how many renewals this server has had,
# so that we get the newest CA cert.
renewals = re.search(r"var nRenewals=(\d+);", response.text).group(1)
cert_url = "https://{0}/certsrv/certnew.cer".format(self.server)
params = {"ReqID": "CACert", "Enc": encoding, "Renewal": renewals}
response = self._get(cert_url, params=params)
if response.headers["Content-Type"] != "application/pkix-cert":
raise CouldNotRetrieveCertificateException(
"An unknown error occured", response.content
)
return response.content
def get_chain(self, encoding="bin"):
"""
Gets the CA chain from the ADCS server.
Args:
encoding: The desired encoding for the returned certificates.
Possible values are 'bin' for binary and 'b64' for Base64 (PEM).
Returns:
The CA chain from the server, in PKCS#7 format.
"""
url = "https://{0}/certsrv/certcarc.asp".format(self.server)
response = self._get(url)
# We have to check how many renewals this server has had, so that we get the newest chain
renewals = re.search(r"var nRenewals=(\d+);", response.text).group(1)
chain_url = "https://{0}/certsrv/certnew.p7b".format(self.server)
params = {"ReqID": "CACert", "Renewal": renewals, "Enc": encoding}
chain_response = self._get(chain_url, params=params)
if chain_response.headers["Content-Type"] != "application/x-pkcs7-certificates":
raise CouldNotRetrieveCertificateException(
"An unknown error occured", chain_response.content
)
return chain_response.content
def check_credentials(self):
"""
Checks the specified credentials against the ADCS server.
Returns:
True if authentication succeeded, False if it failed.
"""
url = "https://{0}/certsrv/".format(self.server)
try:
self._get(url)
except requests.exceptions.HTTPError as error:
if error.response.status_code == 401:
return False
else:
raise
return True
def update_credentials(self, username, password):
"""
Updates the credentials used against the ADCS server.
Args:
username: The username for authentication.
password: The password for authentication.
"""
if self.auth_method in ("ntlm", "cert"):
# NTLM and SSL is connection based,
# so we need to close the connection
# to be able to re-authenticate
self.session.close()
self._set_credentials(username, password)
def _get_ca_bundle():
"""Tries to find the platform ca bundle for the system (on linux systems)"""
ca_bundles = [
# list taken from https://golang.org/src/crypto/x509/root_linux.go
"/etc/ssl/certs/ca-certificates.crt", # Debian/Ubuntu/Gentoo etc.
"/etc/pki/tls/certs/ca-bundle.crt", # Fedora/RHEL 6
"/etc/ssl/ca-bundle.pem", # OpenSUSE
"/etc/pki/tls/cacert.pem", # OpenELEC
"/etc/pki/ca-trust/extracted/pem/tls-ca-bundle.pem", # CentOS/RHEL 7
]
for ca_bundle in ca_bundles:
if os.path.isfile(ca_bundle):
return ca_bundle
# if the bundle was not found, we revert back to requests own
return True
def get_cert(server, csr, template, username, password, encoding="b64", **kwargs):
"""
Gets a certificate from a Microsoft AD Certificate Services web page.
Args:
server: The FQDN to a server running the Certification Authority
Web Enrollment role (must be listening on https).
csr: The certificate request to submit.
template: The certificate template the cert should be issued from.
username: The username for authentication.
pasword: The password for authentication.
encoding: The desired encoding for the returned certificate.
Possible values are 'bin' for binary and 'b64' for Base64 (PEM).
auth_method: The chosen authentication method. Either 'basic' (the default),
'ntlm' or 'cert' (ssl client certificate).
cafile: A PEM file containing the CA certificates that should be trusted.
Returns:
The issued certificate.
Raises:
RequestDeniedException: If the request was denied by the ADCS server.
CertificatePendingException: If the request needs to be approved by a CA admin.
CouldNotRetrieveCertificateException: If something went wrong while
fetching the cert.
Note:
This method is deprecated.
"""
warnings.warn(
"This function is deprecated. Use the method on the Certsrv class instead",
DeprecationWarning,
)
certsrv = Certsrv(server, username, password, **kwargs)
return certsrv.get_cert(csr, template, encoding)
def get_existing_cert(server, req_id, username, password, encoding="b64", **kwargs):
"""
Gets a certificate that has already been created from a
Microsoft AD Certificate Services web page.
Args:
server: The FQDN to a server running the Certification Authority
Web Enrollment role (must be listening on https).
req_id: The request ID to retrieve.
username: The username for authentication.
pasword: The password for authentication.
encoding: The desired encoding for the returned certificate.
Possible values are 'bin' for binary and 'b64' for Base64 (PEM).
auth_method: The chosen authentication method. Either 'basic' (the default),
'ntlm' or 'cert' (ssl client certificate).
cafile: A PEM file containing the CA certificates that should be trusted.
Returns:
The issued certificate.
Raises:
CouldNotRetrieveCertificateException: If something went wrong while
fetching the cert.
Note:
This method is deprecated.
"""
warnings.warn(
"This function is deprecated. Use the method on the Certsrv class instead",
DeprecationWarning,
)
certsrv = Certsrv(server, username, password, **kwargs)
return certsrv.get_existing_cert(req_id, encoding)
def get_ca_cert(server, username, password, encoding="b64", **kwargs):
"""
Gets the (newest) CA certificate from a Microsoft AD Certificate Services web page.
Args:
server: The FQDN to a server running the Certification Authority
Web Enrollment role (must be listening on https).
username: The username for authentication.
pasword: The password for authentication.
encoding: The desired encoding for the returned certificate.
Possible values are 'bin' for binary and 'b64' for Base64 (PEM).
auth_method: The chosen authentication method. Either 'basic' (the default),
'ntlm' or 'cert' (ssl client certificate).
cafile: A PEM file containing the CA certificates that should be trusted.
Returns:
The newest CA certificate from the server.
Note:
This method is deprecated.
"""
warnings.warn(
"This function is deprecated. Use the method on the Certsrv class instead",
DeprecationWarning,
)
certsrv = Certsrv(server, username, password, **kwargs)
return certsrv.get_ca_cert(encoding)
def get_chain(server, username, password, encoding="bin", **kwargs):
"""
Gets the chain from a Microsoft AD Certificate Services web page.
Args:
server: The FQDN to a server running the Certification Authority
Web Enrollment role (must be listening on https).
username: The username for authentication.
pasword: The password for authentication.
encoding: The desired encoding for the returned certificates.
Possible values are 'bin' for binary and 'b64' for Base64 (PEM).
auth_method: The chosen authentication method. Either 'basic' (the default),
'ntlm' or 'cert' (ssl client certificate).
cafile: A PEM file containing the CA certificates that should be trusted.
Returns:
The CA chain from the server, in PKCS#7 format.
Note:
This method is deprecated.
"""
warnings.warn(
"This function is deprecated. Use the method on the Certsrv class instead",
DeprecationWarning,
)
certsrv = Certsrv(server, username, password, **kwargs)
return certsrv.get_chain(encoding)
def check_credentials(server, username, password, **kwargs):
"""
Checks the specified credentials against the specified ADCS server.
Args:
ca: The FQDN to a server running the Certification Authority
Web Enrollment role (must be listening on https).
username: The username for authentication.
pasword: The password for authentication.
auth_method: The chosen authentication method. Either 'basic' (the default),
'ntlm' or 'cert' (ssl client certificate).
cafile: A PEM file containing the CA certificates that should be trusted.
Returns:
True if authentication succeeded, False if it failed.
Note:
This method is deprecated.
"""
warnings.warn(
"This function is deprecated. Use the method on the Certsrv class instead",
DeprecationWarning,
)
certsrv = Certsrv(server, username, password, **kwargs)
return certsrv.check_credentials()