Revision 0602cef3 lib/utils/x509.py
b/lib/utils/x509.py | ||
---|---|---|
27 | 27 |
import re |
28 | 28 |
import datetime |
29 | 29 |
import calendar |
30 |
import errno |
|
31 |
import logging |
|
30 | 32 |
|
31 | 33 |
from ganeti import errors |
32 | 34 |
from ganeti import constants |
35 |
from ganeti import pathutils |
|
33 | 36 |
|
34 | 37 |
from ganeti.utils import text as utils_text |
35 | 38 |
from ganeti.utils import io as utils_io |
... | ... | |
338 | 341 |
ctx.use_certificate(cert) |
339 | 342 |
|
340 | 343 |
return ctx.check_privatekey |
344 |
|
|
345 |
|
|
346 |
def CheckNodeCertificate(cert, _noded_cert_file=pathutils.NODED_CERT_FILE): |
|
347 |
"""Checks the local node daemon certificate against given certificate. |
|
348 |
|
|
349 |
Both certificates must be signed with the same key (as stored in the local |
|
350 |
L{pathutils.NODED_CERT_FILE} file). No error is raised if no local |
|
351 |
certificate can be found. |
|
352 |
|
|
353 |
@type cert: OpenSSL.crypto.X509 |
|
354 |
@param cert: X509 certificate object |
|
355 |
@raise errors.X509CertError: When an error related to X509 occurred |
|
356 |
@raise errors.GenericError: When the verification failed |
|
357 |
|
|
358 |
""" |
|
359 |
try: |
|
360 |
noded_pem = utils_io.ReadFile(_noded_cert_file) |
|
361 |
except EnvironmentError, err: |
|
362 |
if err.errno != errno.ENOENT: |
|
363 |
raise |
|
364 |
|
|
365 |
logging.debug("Node certificate file '%s' was not found", _noded_cert_file) |
|
366 |
return |
|
367 |
|
|
368 |
try: |
|
369 |
noded_cert = \ |
|
370 |
OpenSSL.crypto.load_certificate(OpenSSL.crypto.FILETYPE_PEM, noded_pem) |
|
371 |
except Exception, err: |
|
372 |
raise errors.X509CertError(_noded_cert_file, |
|
373 |
"Unable to load certificate: %s" % err) |
|
374 |
|
|
375 |
try: |
|
376 |
noded_key = \ |
|
377 |
OpenSSL.crypto.load_privatekey(OpenSSL.crypto.FILETYPE_PEM, noded_pem) |
|
378 |
except Exception, err: |
|
379 |
raise errors.X509CertError(_noded_cert_file, |
|
380 |
"Unable to load private key: %s" % err) |
|
381 |
|
|
382 |
# Check consistency of server.pem file |
|
383 |
check_fn = PrepareX509CertKeyCheck(noded_cert, noded_key) |
|
384 |
try: |
|
385 |
check_fn() |
|
386 |
except OpenSSL.SSL.Error: |
|
387 |
# This should never happen as it would mean the certificate in server.pem |
|
388 |
# is out of sync with the private key stored in the same file |
|
389 |
raise errors.X509CertError(_noded_cert_file, |
|
390 |
"Certificate does not match with private key") |
|
391 |
|
|
392 |
# Check with supplied certificate with local key |
|
393 |
check_fn = PrepareX509CertKeyCheck(cert, noded_key) |
|
394 |
try: |
|
395 |
check_fn() |
|
396 |
except OpenSSL.SSL.Error: |
|
397 |
raise errors.GenericError("Given cluster certificate does not match" |
|
398 |
" local key") |
Also available in: Unified diff