2009-04-22 13:35:43 -05:00
|
|
|
# Authors: Rob Crittenden <rcritten@redhat.com>
|
|
|
|
#
|
|
|
|
# Copyright (C) 2009 Red Hat
|
|
|
|
# see file 'COPYING' for use and warranty information
|
|
|
|
#
|
2010-12-09 06:59:11 -06:00
|
|
|
# This program is free software; you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
2009-04-22 13:35:43 -05:00
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
2010-12-09 06:59:11 -06:00
|
|
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2009-04-22 13:35:43 -05:00
|
|
|
#
|
|
|
|
|
2017-04-27 02:37:38 -05:00
|
|
|
import collections
|
2014-09-18 09:28:59 -05:00
|
|
|
import os
|
2016-12-13 09:32:32 -06:00
|
|
|
import io
|
|
|
|
import pwd
|
|
|
|
import grp
|
2014-09-18 04:19:46 -05:00
|
|
|
import re
|
|
|
|
import tempfile
|
2017-05-03 01:14:27 -05:00
|
|
|
from tempfile import NamedTemporaryFile
|
2014-09-18 04:19:46 -05:00
|
|
|
import shutil
|
2014-09-30 03:13:08 -05:00
|
|
|
import base64
|
2017-01-02 06:53:18 -06:00
|
|
|
|
2016-10-13 02:12:31 -05:00
|
|
|
from cryptography.hazmat.primitives import serialization
|
2017-01-02 06:53:18 -06:00
|
|
|
import cryptography.x509
|
2014-09-18 09:28:59 -05:00
|
|
|
|
2016-10-13 02:12:31 -05:00
|
|
|
from ipapython.dn import DN
|
2014-09-18 04:19:46 -05:00
|
|
|
from ipapython.ipa_log_manager import root_logger
|
2017-05-03 01:14:27 -05:00
|
|
|
from ipapython.kerberos import Principal
|
2014-09-18 09:28:59 -05:00
|
|
|
from ipapython import ipautil
|
2017-02-14 02:58:44 -06:00
|
|
|
from ipalib import x509 # pylint: disable=ipa-forbidden-import
|
2014-09-18 09:28:59 -05:00
|
|
|
|
2016-11-23 07:01:07 -06:00
|
|
|
try:
|
2017-02-14 02:58:44 -06:00
|
|
|
# pylint: disable=import-error,ipa-forbidden-import
|
|
|
|
from ipaplatform.paths import paths
|
|
|
|
# pylint: enable=import-error,ipa-forbidden-import
|
2016-11-23 07:01:07 -06:00
|
|
|
except ImportError:
|
|
|
|
CERTUTIL = '/usr/bin/certutil'
|
|
|
|
PK12UTIL = '/usr/bin/pk12util'
|
|
|
|
OPENSSL = '/usr/bin/openssl'
|
2016-12-01 07:56:49 -06:00
|
|
|
else:
|
|
|
|
CERTUTIL = paths.CERTUTIL
|
|
|
|
PK12UTIL = paths.PK12UTIL
|
|
|
|
OPENSSL = paths.OPENSSL
|
|
|
|
|
2016-11-23 07:01:07 -06:00
|
|
|
|
2010-11-01 12:51:14 -05:00
|
|
|
CA_NICKNAME_FMT = "%s IPA CA"
|
2014-09-18 09:28:59 -05:00
|
|
|
|
2016-12-13 09:32:32 -06:00
|
|
|
NSS_FILES = ("cert8.db", "key3.db", "secmod.db", "pwdfile.txt")
|
|
|
|
|
2017-05-12 03:41:08 -05:00
|
|
|
BAD_USAGE_ERR = 'Certificate key usage inadequate for attempted operation.'
|
|
|
|
|
2017-04-27 02:37:38 -05:00
|
|
|
TrustFlags = collections.namedtuple('TrustFlags', 'has_key trusted ca usages')
|
|
|
|
|
|
|
|
EMPTY_TRUST_FLAGS = TrustFlags(False, None, None, None)
|
|
|
|
|
|
|
|
IPA_CA_TRUST_FLAGS = TrustFlags(
|
|
|
|
False, True, True, frozenset({
|
|
|
|
x509.EKU_SERVER_AUTH,
|
|
|
|
x509.EKU_CLIENT_AUTH,
|
|
|
|
x509.EKU_CODE_SIGNING,
|
|
|
|
x509.EKU_EMAIL_PROTECTION,
|
2017-05-03 01:38:20 -05:00
|
|
|
x509.EKU_PKINIT_CLIENT_AUTH,
|
|
|
|
x509.EKU_PKINIT_KDC,
|
2017-04-27 02:37:38 -05:00
|
|
|
}),
|
|
|
|
)
|
|
|
|
|
|
|
|
EXTERNAL_CA_TRUST_FLAGS = TrustFlags(
|
|
|
|
False, True, True, frozenset({x509.EKU_SERVER_AUTH}),
|
|
|
|
)
|
|
|
|
|
|
|
|
TRUSTED_PEER_TRUST_FLAGS = TrustFlags(
|
|
|
|
False, True, False, frozenset({x509.EKU_SERVER_AUTH}),
|
|
|
|
)
|
2017-04-27 02:33:25 -05:00
|
|
|
|
2014-09-18 09:28:59 -05:00
|
|
|
|
2010-11-01 12:51:14 -05:00
|
|
|
def get_ca_nickname(realm, format=CA_NICKNAME_FMT):
|
|
|
|
return format % realm
|
2014-09-18 09:28:59 -05:00
|
|
|
|
|
|
|
|
2014-09-18 04:19:46 -05:00
|
|
|
def find_cert_from_txt(cert, start=0):
|
|
|
|
"""
|
|
|
|
Given a cert blob (str) which may or may not contian leading and
|
|
|
|
trailing text, pull out just the certificate part. This will return
|
|
|
|
the FIRST cert in a stream of data.
|
|
|
|
|
|
|
|
Returns a tuple (certificate, last position in cert)
|
|
|
|
"""
|
|
|
|
s = cert.find('-----BEGIN CERTIFICATE-----', start)
|
|
|
|
e = cert.find('-----END CERTIFICATE-----', s)
|
|
|
|
if e > 0:
|
|
|
|
e = e + 25
|
|
|
|
|
|
|
|
if s < 0 or e < 0:
|
|
|
|
raise RuntimeError("Unable to find certificate")
|
|
|
|
|
|
|
|
cert = cert[s:e]
|
|
|
|
return (cert, e)
|
|
|
|
|
|
|
|
|
2017-04-27 02:37:38 -05:00
|
|
|
def parse_trust_flags(trust_flags):
|
|
|
|
"""
|
|
|
|
Convert certutil trust flags to TrustFlags object.
|
|
|
|
"""
|
|
|
|
has_key = 'u' in trust_flags
|
|
|
|
|
|
|
|
if 'p' in trust_flags:
|
|
|
|
if 'C' in trust_flags or 'P' in trust_flags or 'T' in trust_flags:
|
|
|
|
raise ValueError("cannot be both trusted and not trusted")
|
|
|
|
return False, None, None
|
|
|
|
elif 'C' in trust_flags or 'T' in trust_flags:
|
|
|
|
if 'P' in trust_flags:
|
|
|
|
raise ValueError("cannot be both CA and not CA")
|
|
|
|
ca = True
|
|
|
|
elif 'P' in trust_flags:
|
|
|
|
ca = False
|
|
|
|
else:
|
|
|
|
return TrustFlags(has_key, None, None, frozenset())
|
|
|
|
|
|
|
|
trust_flags = trust_flags.split(',')
|
|
|
|
ext_key_usage = set()
|
|
|
|
for i, kp in enumerate((x509.EKU_SERVER_AUTH,
|
|
|
|
x509.EKU_EMAIL_PROTECTION,
|
|
|
|
x509.EKU_CODE_SIGNING)):
|
|
|
|
if 'C' in trust_flags[i] or 'P' in trust_flags[i]:
|
|
|
|
ext_key_usage.add(kp)
|
|
|
|
if 'T' in trust_flags[0]:
|
|
|
|
ext_key_usage.add(x509.EKU_CLIENT_AUTH)
|
|
|
|
|
|
|
|
return TrustFlags(has_key, True, ca, frozenset(ext_key_usage))
|
|
|
|
|
|
|
|
|
|
|
|
def unparse_trust_flags(trust_flags):
|
|
|
|
"""
|
|
|
|
Convert TrustFlags object to certutil trust flags.
|
|
|
|
"""
|
|
|
|
has_key, trusted, ca, ext_key_usage = trust_flags
|
|
|
|
|
|
|
|
if trusted is False:
|
|
|
|
if has_key:
|
|
|
|
return 'pu,pu,pu'
|
|
|
|
else:
|
|
|
|
return 'p,p,p'
|
|
|
|
elif trusted is None or ca is None:
|
|
|
|
if has_key:
|
|
|
|
return 'u,u,u'
|
|
|
|
else:
|
|
|
|
return ',,'
|
|
|
|
elif ext_key_usage is None:
|
|
|
|
if ca:
|
|
|
|
if has_key:
|
|
|
|
return 'CTu,Cu,Cu'
|
|
|
|
else:
|
|
|
|
return 'CT,C,C'
|
|
|
|
else:
|
|
|
|
if has_key:
|
|
|
|
return 'Pu,Pu,Pu'
|
|
|
|
else:
|
|
|
|
return 'P,P,P'
|
|
|
|
|
|
|
|
trust_flags = ['', '', '']
|
|
|
|
for i, kp in enumerate((x509.EKU_SERVER_AUTH,
|
|
|
|
x509.EKU_EMAIL_PROTECTION,
|
|
|
|
x509.EKU_CODE_SIGNING)):
|
|
|
|
if kp in ext_key_usage:
|
|
|
|
trust_flags[i] += ('C' if ca else 'P')
|
|
|
|
if ca and x509.EKU_CLIENT_AUTH in ext_key_usage:
|
|
|
|
trust_flags[0] += 'T'
|
|
|
|
if has_key:
|
|
|
|
for i in range(3):
|
|
|
|
trust_flags[i] += 'u'
|
|
|
|
|
|
|
|
trust_flags = ','.join(trust_flags)
|
|
|
|
return trust_flags
|
|
|
|
|
|
|
|
|
2017-05-03 01:14:27 -05:00
|
|
|
def verify_kdc_cert_validity(kdc_cert, ca_certs, realm):
|
|
|
|
pem_kdc_cert = kdc_cert.public_bytes(serialization.Encoding.PEM)
|
|
|
|
pem_ca_certs = '\n'.join(
|
|
|
|
cert.public_bytes(serialization.Encoding.PEM) for cert in ca_certs)
|
|
|
|
|
|
|
|
with NamedTemporaryFile() as kdc_file, NamedTemporaryFile() as ca_file:
|
|
|
|
kdc_file.write(pem_kdc_cert)
|
|
|
|
kdc_file.flush()
|
|
|
|
ca_file.write(pem_ca_certs)
|
|
|
|
ca_file.flush()
|
|
|
|
|
|
|
|
try:
|
|
|
|
ipautil.run(
|
|
|
|
[OPENSSL, 'verify', '-CAfile', ca_file.name, kdc_file.name])
|
|
|
|
eku = kdc_cert.extensions.get_extension_for_class(
|
|
|
|
cryptography.x509.ExtendedKeyUsage)
|
|
|
|
list(eku.value).index(
|
|
|
|
cryptography.x509.ObjectIdentifier(x509.EKU_PKINIT_KDC))
|
|
|
|
except (ipautil.CalledProcessError,
|
|
|
|
cryptography.x509.ExtensionNotFound,
|
|
|
|
ValueError):
|
|
|
|
raise ValueError("invalid for a KDC")
|
|
|
|
|
|
|
|
principal = str(Principal(['krbtgt', realm], realm))
|
|
|
|
gns = x509.process_othernames(x509.get_san_general_names(kdc_cert))
|
|
|
|
for gn in gns:
|
|
|
|
if isinstance(gn, x509.KRB5PrincipalName) and gn.name == principal:
|
|
|
|
break
|
|
|
|
else:
|
|
|
|
raise ValueError("invalid for realm %s" % realm)
|
|
|
|
|
|
|
|
|
2014-09-18 04:19:46 -05:00
|
|
|
class NSSDatabase(object):
|
|
|
|
"""A general-purpose wrapper around a NSS cert database
|
|
|
|
|
|
|
|
For permanent NSS databases, pass the cert DB directory to __init__
|
|
|
|
|
|
|
|
For temporary databases, do not pass nssdir, and call close() when done
|
|
|
|
to remove the DB. Alternatively, a NSSDatabase can be used as a
|
|
|
|
context manager that calls close() automatically.
|
|
|
|
"""
|
|
|
|
# Traditionally, we used CertDB for our NSS DB operations, but that class
|
|
|
|
# got too tied to IPA server details, killing reusability.
|
|
|
|
# BaseCertDB is a class that knows nothing about IPA.
|
|
|
|
# Generic NSS DB code should be moved here.
|
|
|
|
def __init__(self, nssdir=None):
|
|
|
|
if nssdir is None:
|
|
|
|
self.secdir = tempfile.mkdtemp()
|
|
|
|
self._is_temporary = True
|
|
|
|
else:
|
|
|
|
self.secdir = nssdir
|
|
|
|
self._is_temporary = False
|
2016-12-22 13:24:21 -06:00
|
|
|
self.pwd_file = os.path.join(self.secdir, 'pwdfile.txt')
|
2014-09-18 04:19:46 -05:00
|
|
|
|
|
|
|
def close(self):
|
|
|
|
if self._is_temporary:
|
|
|
|
shutil.rmtree(self.secdir)
|
|
|
|
|
|
|
|
def __enter__(self):
|
|
|
|
return self
|
|
|
|
|
|
|
|
def __exit__(self, type, value, tb):
|
|
|
|
self.close()
|
|
|
|
|
2015-11-25 10:17:18 -06:00
|
|
|
def run_certutil(self, args, stdin=None, **kwargs):
|
2016-11-23 07:01:07 -06:00
|
|
|
new_args = [CERTUTIL, "-d", self.secdir]
|
2014-09-18 04:19:46 -05:00
|
|
|
new_args = new_args + args
|
2016-12-06 02:14:54 -06:00
|
|
|
new_args.extend(['-f', self.pwd_file])
|
2015-11-25 10:17:18 -06:00
|
|
|
return ipautil.run(new_args, stdin, **kwargs)
|
2014-09-18 04:19:46 -05:00
|
|
|
|
2016-12-06 02:14:54 -06:00
|
|
|
def create_db(self, user=None, group=None, mode=None, backup=False):
|
2014-09-18 04:19:46 -05:00
|
|
|
"""Create cert DB
|
|
|
|
|
2016-12-13 09:32:32 -06:00
|
|
|
:param user: User owner the secdir
|
|
|
|
:param group: Group owner of the secdir
|
|
|
|
:param mode: Mode of the secdir
|
|
|
|
:param backup: Backup the sedir files
|
2014-09-18 04:19:46 -05:00
|
|
|
"""
|
2016-12-13 09:32:32 -06:00
|
|
|
dirmode = 0o750
|
|
|
|
filemode = 0o640
|
2017-02-16 03:57:14 -06:00
|
|
|
pwdfilemode = 0o640
|
2016-12-13 09:32:32 -06:00
|
|
|
if mode is not None:
|
|
|
|
dirmode = mode
|
|
|
|
filemode = mode & 0o666
|
2017-02-16 03:57:14 -06:00
|
|
|
pwdfilemode = mode & 0o660
|
2016-12-13 09:32:32 -06:00
|
|
|
|
|
|
|
uid = -1
|
|
|
|
gid = -1
|
|
|
|
if user is not None:
|
|
|
|
uid = pwd.getpwnam(user).pw_uid
|
|
|
|
if group is not None:
|
|
|
|
gid = grp.getgrnam(group).gr_gid
|
|
|
|
|
|
|
|
if backup:
|
|
|
|
for filename in NSS_FILES:
|
|
|
|
path = os.path.join(self.secdir, filename)
|
|
|
|
ipautil.backup_file(path)
|
|
|
|
|
|
|
|
if not os.path.exists(self.secdir):
|
|
|
|
os.makedirs(self.secdir, dirmode)
|
|
|
|
|
2016-12-06 02:14:54 -06:00
|
|
|
if not os.path.exists(self.pwd_file):
|
2016-12-13 09:32:32 -06:00
|
|
|
# Create the password file for this db
|
2016-12-06 02:14:54 -06:00
|
|
|
with io.open(os.open(self.pwd_file,
|
2016-12-13 09:32:32 -06:00
|
|
|
os.O_CREAT | os.O_WRONLY,
|
2017-02-16 03:57:14 -06:00
|
|
|
pwdfilemode), 'w', closefd=True) as f:
|
2016-12-06 02:14:54 -06:00
|
|
|
f.write(ipautil.ipa_generate_password())
|
2016-12-13 09:32:32 -06:00
|
|
|
f.flush()
|
|
|
|
|
2016-12-06 02:14:54 -06:00
|
|
|
self.run_certutil(["-N", "-f", self.pwd_file])
|
2014-09-18 04:19:46 -05:00
|
|
|
|
2016-12-13 09:32:32 -06:00
|
|
|
# Finally fix up perms
|
|
|
|
os.chown(self.secdir, uid, gid)
|
|
|
|
os.chmod(self.secdir, dirmode)
|
|
|
|
for filename in NSS_FILES:
|
|
|
|
path = os.path.join(self.secdir, filename)
|
|
|
|
if os.path.exists(path):
|
2017-02-27 08:12:39 -06:00
|
|
|
os.chown(path, uid, gid)
|
2017-02-16 03:57:14 -06:00
|
|
|
if path == self.pwd_file:
|
|
|
|
new_mode = pwdfilemode
|
|
|
|
else:
|
|
|
|
new_mode = filemode
|
|
|
|
os.chmod(path, new_mode)
|
2016-12-13 09:32:32 -06:00
|
|
|
|
2017-03-08 08:24:15 -06:00
|
|
|
def restore(self):
|
|
|
|
for filename in NSS_FILES:
|
|
|
|
path = os.path.join(self.secdir, filename)
|
|
|
|
backup_path = path + '.orig'
|
|
|
|
save_path = path + '.ipasave'
|
|
|
|
try:
|
|
|
|
if os.path.exists(path):
|
|
|
|
os.rename(path, save_path)
|
|
|
|
if os.path.exists(backup_path):
|
|
|
|
os.rename(backup_path, path)
|
|
|
|
except OSError as e:
|
|
|
|
root_logger.debug(e)
|
|
|
|
|
2014-09-18 04:19:46 -05:00
|
|
|
def list_certs(self):
|
|
|
|
"""Return nicknames and cert flags for all certs in the database
|
|
|
|
|
|
|
|
:return: List of (name, trust_flags) tuples
|
|
|
|
"""
|
2015-11-25 10:17:18 -06:00
|
|
|
result = self.run_certutil(["-L"], capture_output=True)
|
|
|
|
certs = result.output.splitlines()
|
2014-09-18 04:19:46 -05:00
|
|
|
|
|
|
|
# FIXME, this relies on NSS never changing the formatting of certutil
|
|
|
|
certlist = []
|
|
|
|
for cert in certs:
|
|
|
|
match = re.match(r'^(.+?)\s+(\w*,\w*,\w*)\s*$', cert)
|
|
|
|
if match:
|
2017-04-27 02:37:38 -05:00
|
|
|
nickname = match.group(1)
|
|
|
|
trust_flags = parse_trust_flags(match.group(2))
|
|
|
|
certlist.append((nickname, trust_flags))
|
2014-09-18 04:19:46 -05:00
|
|
|
|
|
|
|
return tuple(certlist)
|
|
|
|
|
|
|
|
def find_server_certs(self):
|
|
|
|
"""Return nicknames and cert flags for server certs in the database
|
|
|
|
|
|
|
|
Server certs have an "u" character in the trust flags.
|
|
|
|
|
|
|
|
:return: List of (name, trust_flags) tuples
|
|
|
|
"""
|
|
|
|
server_certs = []
|
|
|
|
for name, flags in self.list_certs():
|
2017-04-27 02:37:38 -05:00
|
|
|
if flags.has_key:
|
2014-09-18 04:19:46 -05:00
|
|
|
server_certs.append((name, flags))
|
|
|
|
|
|
|
|
return server_certs
|
|
|
|
|
|
|
|
def get_trust_chain(self, nickname):
|
|
|
|
"""Return names of certs in a given cert's trust chain
|
|
|
|
|
|
|
|
:param nickname: Name of the cert
|
|
|
|
:return: List of certificate names
|
|
|
|
"""
|
|
|
|
root_nicknames = []
|
2015-11-25 10:17:18 -06:00
|
|
|
result = self.run_certutil(["-O", "-n", nickname], capture_output=True)
|
|
|
|
chain = result.output.splitlines()
|
2014-09-18 04:19:46 -05:00
|
|
|
|
|
|
|
for c in chain:
|
|
|
|
m = re.match('\s*"(.*)" \[.*', c)
|
|
|
|
if m:
|
|
|
|
root_nicknames.append(m.groups()[0])
|
|
|
|
|
|
|
|
return root_nicknames
|
|
|
|
|
2016-12-22 13:24:21 -06:00
|
|
|
def export_pkcs12(self, nickname, pkcs12_filename, pkcs12_passwd=None):
|
2016-12-13 09:32:32 -06:00
|
|
|
args = [PK12UTIL, "-d", self.secdir,
|
|
|
|
"-o", pkcs12_filename,
|
|
|
|
"-n", nickname,
|
2016-12-22 13:24:21 -06:00
|
|
|
"-k", self.pwd_file]
|
2016-12-13 09:32:32 -06:00
|
|
|
pkcs12_password_file = None
|
|
|
|
if pkcs12_passwd is not None:
|
|
|
|
pkcs12_password_file = ipautil.write_tmp_file(pkcs12_passwd + '\n')
|
|
|
|
args = args + ["-w", pkcs12_password_file.name]
|
|
|
|
try:
|
|
|
|
ipautil.run(args)
|
|
|
|
except ipautil.CalledProcessError as e:
|
|
|
|
if e.returncode == 17:
|
|
|
|
raise RuntimeError("incorrect password for pkcs#12 file %s" %
|
|
|
|
pkcs12_filename)
|
|
|
|
elif e.returncode == 10:
|
|
|
|
raise RuntimeError("Failed to open %s" % pkcs12_filename)
|
|
|
|
else:
|
|
|
|
raise RuntimeError("unknown error exporting pkcs#12 file %s" %
|
|
|
|
pkcs12_filename)
|
|
|
|
finally:
|
|
|
|
if pkcs12_password_file is not None:
|
|
|
|
pkcs12_password_file.close()
|
|
|
|
|
2016-12-22 13:24:21 -06:00
|
|
|
def import_pkcs12(self, pkcs12_filename, pkcs12_passwd=None):
|
2016-11-23 07:01:07 -06:00
|
|
|
args = [PK12UTIL, "-d", self.secdir,
|
2014-09-18 04:19:46 -05:00
|
|
|
"-i", pkcs12_filename,
|
2016-12-22 13:24:21 -06:00
|
|
|
"-k", self.pwd_file, '-v']
|
2016-11-24 03:07:50 -06:00
|
|
|
pkcs12_password_file = None
|
2014-09-18 04:19:46 -05:00
|
|
|
if pkcs12_passwd is not None:
|
2016-12-08 05:26:06 -06:00
|
|
|
pkcs12_password_file = ipautil.write_tmp_file(pkcs12_passwd + '\n')
|
2016-11-24 03:07:50 -06:00
|
|
|
args = args + ["-w", pkcs12_password_file.name]
|
2014-09-18 04:19:46 -05:00
|
|
|
try:
|
2016-11-24 03:07:50 -06:00
|
|
|
ipautil.run(args)
|
2015-07-30 09:49:29 -05:00
|
|
|
except ipautil.CalledProcessError as e:
|
2014-09-18 04:19:46 -05:00
|
|
|
if e.returncode == 17:
|
|
|
|
raise RuntimeError("incorrect password for pkcs#12 file %s" %
|
|
|
|
pkcs12_filename)
|
|
|
|
elif e.returncode == 10:
|
|
|
|
raise RuntimeError("Failed to open %s" % pkcs12_filename)
|
|
|
|
else:
|
|
|
|
raise RuntimeError("unknown error import pkcs#12 file %s" %
|
|
|
|
pkcs12_filename)
|
2016-11-24 03:07:50 -06:00
|
|
|
finally:
|
|
|
|
if pkcs12_password_file is not None:
|
|
|
|
pkcs12_password_file.close()
|
2014-09-18 04:19:46 -05:00
|
|
|
|
2016-12-22 13:24:21 -06:00
|
|
|
def import_files(self, files, import_keys=False, key_password=None,
|
|
|
|
key_nickname=None):
|
2014-09-18 04:19:46 -05:00
|
|
|
"""
|
|
|
|
Import certificates and a single private key from multiple files
|
|
|
|
|
|
|
|
The files may be in PEM and DER certificate, PKCS#7 certificate chain,
|
|
|
|
PKCS#8 and raw private key and PKCS#12 formats.
|
|
|
|
|
|
|
|
:param files: Names of files to import
|
|
|
|
:param import_keys: Whether to import private keys
|
|
|
|
:param key_password: Password to decrypt private keys
|
|
|
|
:param key_nickname: Nickname of the private key to import from PKCS#12
|
|
|
|
files
|
|
|
|
"""
|
|
|
|
key_file = None
|
|
|
|
extracted_key = None
|
2016-11-17 18:04:24 -06:00
|
|
|
extracted_certs = []
|
2014-09-18 04:19:46 -05:00
|
|
|
|
|
|
|
for filename in files:
|
|
|
|
try:
|
|
|
|
with open(filename, 'rb') as f:
|
|
|
|
data = f.read()
|
|
|
|
except IOError as e:
|
|
|
|
raise RuntimeError(
|
|
|
|
"Failed to open %s: %s" % (filename, e.strerror))
|
|
|
|
|
|
|
|
# Try to parse the file as PEM file
|
|
|
|
matches = list(re.finditer(
|
|
|
|
r'-----BEGIN (.+?)-----(.*?)-----END \1-----', data, re.DOTALL))
|
|
|
|
if matches:
|
|
|
|
loaded = False
|
|
|
|
for match in matches:
|
|
|
|
body = match.group()
|
|
|
|
label = match.group(1)
|
|
|
|
line = len(data[:match.start() + 1].splitlines())
|
|
|
|
|
|
|
|
if label in ('CERTIFICATE', 'X509 CERTIFICATE',
|
|
|
|
'X.509 CERTIFICATE'):
|
|
|
|
try:
|
|
|
|
x509.load_certificate(match.group(2))
|
2016-10-13 02:12:31 -05:00
|
|
|
except ValueError as e:
|
2014-09-18 04:19:46 -05:00
|
|
|
if label != 'CERTIFICATE':
|
|
|
|
root_logger.warning(
|
|
|
|
"Skipping certificate in %s at line %s: %s",
|
|
|
|
filename, line, e)
|
|
|
|
continue
|
|
|
|
else:
|
2016-11-17 18:04:24 -06:00
|
|
|
extracted_certs.append(body)
|
2014-09-18 04:19:46 -05:00
|
|
|
loaded = True
|
|
|
|
continue
|
|
|
|
|
|
|
|
if label in ('PKCS7', 'PKCS #7 SIGNED DATA', 'CERTIFICATE'):
|
|
|
|
try:
|
2016-08-15 22:16:58 -05:00
|
|
|
certs = x509.pkcs7_to_pems(body)
|
2014-09-18 04:19:46 -05:00
|
|
|
except ipautil.CalledProcessError as e:
|
|
|
|
if label == 'CERTIFICATE':
|
|
|
|
root_logger.warning(
|
|
|
|
"Skipping certificate in %s at line %s: %s",
|
|
|
|
filename, line, e)
|
|
|
|
else:
|
|
|
|
root_logger.warning(
|
|
|
|
"Skipping PKCS#7 in %s at line %s: %s",
|
|
|
|
filename, line, e)
|
|
|
|
continue
|
|
|
|
else:
|
2016-11-17 18:04:24 -06:00
|
|
|
extracted_certs.extend(certs)
|
2014-09-18 04:19:46 -05:00
|
|
|
loaded = True
|
|
|
|
continue
|
|
|
|
|
|
|
|
if label in ('PRIVATE KEY', 'ENCRYPTED PRIVATE KEY',
|
|
|
|
'RSA PRIVATE KEY', 'DSA PRIVATE KEY',
|
|
|
|
'EC PRIVATE KEY'):
|
|
|
|
if not import_keys:
|
|
|
|
continue
|
|
|
|
|
|
|
|
if key_file:
|
|
|
|
raise RuntimeError(
|
|
|
|
"Can't load private key from both %s and %s" %
|
|
|
|
(key_file, filename))
|
|
|
|
|
|
|
|
args = [
|
2016-11-23 07:01:07 -06:00
|
|
|
OPENSSL, 'pkcs8',
|
2014-09-18 04:19:46 -05:00
|
|
|
'-topk8',
|
2016-12-22 13:24:21 -06:00
|
|
|
'-passout', 'file:' + self.pwd_file,
|
2014-09-18 04:19:46 -05:00
|
|
|
]
|
|
|
|
if ((label != 'PRIVATE KEY' and key_password) or
|
|
|
|
label == 'ENCRYPTED PRIVATE KEY'):
|
|
|
|
key_pwdfile = ipautil.write_tmp_file(key_password)
|
|
|
|
args += [
|
|
|
|
'-passin', 'file:' + key_pwdfile.name,
|
|
|
|
]
|
|
|
|
try:
|
2015-11-25 10:17:18 -06:00
|
|
|
result = ipautil.run(
|
|
|
|
args, stdin=body, capture_output=True)
|
2014-09-18 04:19:46 -05:00
|
|
|
except ipautil.CalledProcessError as e:
|
|
|
|
root_logger.warning(
|
|
|
|
"Skipping private key in %s at line %s: %s",
|
|
|
|
filename, line, e)
|
|
|
|
continue
|
|
|
|
else:
|
2015-11-25 10:17:18 -06:00
|
|
|
extracted_key = result.output
|
2014-09-18 04:19:46 -05:00
|
|
|
key_file = filename
|
|
|
|
loaded = True
|
|
|
|
continue
|
|
|
|
if loaded:
|
|
|
|
continue
|
|
|
|
raise RuntimeError("Failed to load %s" % filename)
|
|
|
|
|
|
|
|
# Try to load the file as DER certificate
|
|
|
|
try:
|
|
|
|
x509.load_certificate(data, x509.DER)
|
2016-10-13 02:12:31 -05:00
|
|
|
except ValueError:
|
2014-09-18 04:19:46 -05:00
|
|
|
pass
|
|
|
|
else:
|
|
|
|
data = x509.make_pem(base64.b64encode(data))
|
2016-11-17 18:04:24 -06:00
|
|
|
extracted_certs.append(data)
|
2014-09-18 04:19:46 -05:00
|
|
|
continue
|
|
|
|
|
|
|
|
# Try to import the file as PKCS#12 file
|
|
|
|
if import_keys:
|
|
|
|
try:
|
2016-12-22 13:24:21 -06:00
|
|
|
self.import_pkcs12(filename, key_password)
|
2014-09-18 04:19:46 -05:00
|
|
|
except RuntimeError:
|
|
|
|
pass
|
|
|
|
else:
|
|
|
|
if key_file:
|
|
|
|
raise RuntimeError(
|
|
|
|
"Can't load private key from both %s and %s" %
|
|
|
|
(key_file, filename))
|
|
|
|
key_file = filename
|
|
|
|
|
|
|
|
server_certs = self.find_server_certs()
|
|
|
|
if key_nickname:
|
2016-10-07 08:07:49 -05:00
|
|
|
for nickname, _trust_flags in server_certs:
|
2014-09-18 04:19:46 -05:00
|
|
|
if nickname == key_nickname:
|
|
|
|
break
|
|
|
|
else:
|
|
|
|
raise RuntimeError(
|
|
|
|
"Server certificate \"%s\" not found in %s" %
|
|
|
|
(key_nickname, filename))
|
|
|
|
else:
|
|
|
|
if len(server_certs) > 1:
|
|
|
|
raise RuntimeError(
|
|
|
|
"%s server certificates found in %s, "
|
|
|
|
"expecting only one" %
|
|
|
|
(len(server_certs), filename))
|
|
|
|
|
|
|
|
continue
|
|
|
|
|
|
|
|
raise RuntimeError("Failed to load %s" % filename)
|
|
|
|
|
|
|
|
if import_keys and not key_file:
|
|
|
|
raise RuntimeError(
|
|
|
|
"No server certificates found in %s" % (', '.join(files)))
|
|
|
|
|
2016-11-17 18:04:24 -06:00
|
|
|
for cert_pem in extracted_certs:
|
|
|
|
cert = x509.load_certificate(cert_pem)
|
2016-10-13 02:12:31 -05:00
|
|
|
nickname = str(DN(cert.subject))
|
|
|
|
data = cert.public_bytes(serialization.Encoding.DER)
|
2017-04-27 02:33:25 -05:00
|
|
|
self.add_cert(data, nickname, EMPTY_TRUST_FLAGS)
|
2014-09-18 04:19:46 -05:00
|
|
|
|
|
|
|
if extracted_key:
|
2016-11-17 18:04:24 -06:00
|
|
|
in_file = ipautil.write_tmp_file(
|
|
|
|
'\n'.join(extracted_certs) + '\n' + extracted_key)
|
2014-09-18 04:19:46 -05:00
|
|
|
out_file = tempfile.NamedTemporaryFile()
|
|
|
|
out_password = ipautil.ipa_generate_password()
|
|
|
|
out_pwdfile = ipautil.write_tmp_file(out_password)
|
|
|
|
args = [
|
2016-11-23 07:01:07 -06:00
|
|
|
OPENSSL, 'pkcs12',
|
2014-09-18 04:19:46 -05:00
|
|
|
'-export',
|
|
|
|
'-in', in_file.name,
|
|
|
|
'-out', out_file.name,
|
2016-12-22 13:24:21 -06:00
|
|
|
'-passin', 'file:' + self.pwd_file,
|
2014-09-18 04:19:46 -05:00
|
|
|
'-passout', 'file:' + out_pwdfile.name,
|
|
|
|
]
|
|
|
|
try:
|
|
|
|
ipautil.run(args)
|
|
|
|
except ipautil.CalledProcessError as e:
|
|
|
|
raise RuntimeError(
|
|
|
|
"No matching certificate found for private key from %s" %
|
|
|
|
key_file)
|
|
|
|
|
2016-12-22 13:24:21 -06:00
|
|
|
self.import_pkcs12(out_file.name, out_password)
|
2014-09-18 04:19:46 -05:00
|
|
|
|
2017-04-27 02:57:45 -05:00
|
|
|
def trust_root_cert(self, root_nickname, trust_flags):
|
2014-09-18 04:19:46 -05:00
|
|
|
if root_nickname[:7] == "Builtin":
|
|
|
|
root_logger.debug(
|
|
|
|
"No need to add trust for built-in root CAs, skipping %s" %
|
|
|
|
root_nickname)
|
|
|
|
else:
|
2017-04-27 02:37:38 -05:00
|
|
|
trust_flags = unparse_trust_flags(trust_flags)
|
2014-09-18 04:19:46 -05:00
|
|
|
try:
|
|
|
|
self.run_certutil(["-M", "-n", root_nickname,
|
|
|
|
"-t", trust_flags])
|
2016-10-07 08:07:49 -05:00
|
|
|
except ipautil.CalledProcessError:
|
2014-09-18 04:19:46 -05:00
|
|
|
raise RuntimeError(
|
|
|
|
"Setting trust on %s failed" % root_nickname)
|
|
|
|
|
|
|
|
def get_cert(self, nickname, pem=False):
|
2016-03-10 06:16:41 -06:00
|
|
|
args = ['-L', '-n', nickname, '-a']
|
2014-09-18 04:19:46 -05:00
|
|
|
try:
|
2016-03-10 06:16:41 -06:00
|
|
|
result = self.run_certutil(args, capture_output=True)
|
2014-09-18 04:19:46 -05:00
|
|
|
except ipautil.CalledProcessError:
|
|
|
|
raise RuntimeError("Failed to get %s" % nickname)
|
2016-03-10 06:16:41 -06:00
|
|
|
cert = result.output
|
|
|
|
if not pem:
|
2016-10-07 08:07:49 -05:00
|
|
|
cert, _start = find_cert_from_txt(cert, start=0)
|
2016-03-10 06:16:41 -06:00
|
|
|
cert = x509.strip_header(cert)
|
|
|
|
cert = base64.b64decode(cert)
|
|
|
|
return cert
|
2014-09-18 04:19:46 -05:00
|
|
|
|
2014-09-18 04:42:14 -05:00
|
|
|
def has_nickname(self, nickname):
|
|
|
|
try:
|
|
|
|
self.get_cert(nickname)
|
|
|
|
except RuntimeError:
|
|
|
|
# This might be error other than "nickname not found". Beware.
|
|
|
|
return False
|
|
|
|
else:
|
|
|
|
return True
|
|
|
|
|
2014-09-18 04:19:46 -05:00
|
|
|
def export_pem_cert(self, nickname, location):
|
|
|
|
"""Export the given cert to PEM file in the given location"""
|
2015-10-26 14:52:05 -05:00
|
|
|
cert = self.get_cert(nickname, pem=True)
|
2014-09-18 04:19:46 -05:00
|
|
|
with open(location, "w+") as fd:
|
|
|
|
fd.write(cert)
|
2015-07-15 09:38:06 -05:00
|
|
|
os.chmod(location, 0o444)
|
2014-09-18 04:19:46 -05:00
|
|
|
|
|
|
|
def import_pem_cert(self, nickname, flags, location):
|
|
|
|
"""Import a cert form the given PEM file.
|
|
|
|
|
|
|
|
The file must contain exactly one certificate.
|
|
|
|
"""
|
|
|
|
try:
|
|
|
|
with open(location) as fd:
|
|
|
|
certs = fd.read()
|
|
|
|
except IOError as e:
|
|
|
|
raise RuntimeError(
|
|
|
|
"Failed to open %s: %s" % (location, e.strerror)
|
|
|
|
)
|
|
|
|
|
|
|
|
cert, st = find_cert_from_txt(certs)
|
|
|
|
self.add_cert(cert, nickname, flags, pem=True)
|
|
|
|
|
|
|
|
try:
|
|
|
|
find_cert_from_txt(certs, st)
|
|
|
|
except RuntimeError:
|
|
|
|
pass
|
|
|
|
else:
|
|
|
|
raise ValueError('%s contains more than one certificate' %
|
|
|
|
location)
|
|
|
|
|
|
|
|
def add_cert(self, cert, nick, flags, pem=False):
|
2017-04-27 02:37:38 -05:00
|
|
|
flags = unparse_trust_flags(flags)
|
2014-09-18 04:19:46 -05:00
|
|
|
args = ["-A", "-n", nick, "-t", flags]
|
|
|
|
if pem:
|
|
|
|
args.append("-a")
|
|
|
|
self.run_certutil(args, stdin=cert)
|
|
|
|
|
|
|
|
def delete_cert(self, nick):
|
|
|
|
self.run_certutil(["-D", "-n", nick])
|
|
|
|
|
|
|
|
def verify_server_cert_validity(self, nickname, hostname):
|
|
|
|
"""Verify a certificate is valid for a SSL server with given hostname
|
|
|
|
|
|
|
|
Raises a ValueError if the certificate is invalid.
|
|
|
|
"""
|
2017-01-02 06:53:18 -06:00
|
|
|
cert = self.get_cert(nickname)
|
|
|
|
cert = x509.load_certificate(cert, x509.DER)
|
|
|
|
|
2014-09-18 04:19:46 -05:00
|
|
|
try:
|
2017-05-12 03:41:08 -05:00
|
|
|
self.run_certutil(['-V', '-n', nickname, '-u', 'V'],
|
|
|
|
capture_output=True)
|
|
|
|
except ipautil.CalledProcessError as e:
|
|
|
|
# certutil output in case of error is
|
|
|
|
# 'certutil: certificate is invalid: <ERROR_STRING>\n'
|
|
|
|
msg = e.output.split(': ')[2].strip()
|
|
|
|
if msg == BAD_USAGE_ERR:
|
|
|
|
msg = 'invalid for a SSL server.'
|
|
|
|
raise ValueError(msg)
|
2014-09-18 04:19:46 -05:00
|
|
|
|
2017-01-02 06:53:18 -06:00
|
|
|
try:
|
|
|
|
x509.match_hostname(cert, hostname)
|
|
|
|
except ValueError:
|
|
|
|
raise ValueError('invalid for server %s' % hostname)
|
2014-09-18 04:19:46 -05:00
|
|
|
|
|
|
|
def verify_ca_cert_validity(self, nickname):
|
2017-01-02 06:53:18 -06:00
|
|
|
cert = self.get_cert(nickname)
|
|
|
|
cert = x509.load_certificate(cert, x509.DER)
|
|
|
|
|
|
|
|
if not cert.subject:
|
|
|
|
raise ValueError("has empty subject")
|
|
|
|
|
2014-09-18 04:19:46 -05:00
|
|
|
try:
|
2017-01-02 06:53:18 -06:00
|
|
|
bc = cert.extensions.get_extension_for_class(
|
|
|
|
cryptography.x509.BasicConstraints)
|
|
|
|
except cryptography.x509.ExtensionNotFound:
|
|
|
|
raise ValueError("missing basic constraints")
|
|
|
|
|
2017-04-03 05:20:41 -05:00
|
|
|
if not bc.value.ca:
|
2017-01-02 06:53:18 -06:00
|
|
|
raise ValueError("not a CA certificate")
|
|
|
|
|
|
|
|
try:
|
2017-05-12 03:41:08 -05:00
|
|
|
self.run_certutil(['-V', '-n', nickname, '-u', 'L'],
|
|
|
|
capture_output=True)
|
|
|
|
except ipautil.CalledProcessError as e:
|
|
|
|
# certutil output in case of error is
|
|
|
|
# 'certutil: certificate is invalid: <ERROR_STRING>\n'
|
|
|
|
msg = e.output.split(': ')[2].strip()
|
|
|
|
if msg == BAD_USAGE_ERR:
|
|
|
|
msg = 'invalid for a CA.'
|
|
|
|
raise ValueError(msg)
|
2017-05-03 01:14:27 -05:00
|
|
|
|
|
|
|
def verify_kdc_cert_validity(self, nickname, realm):
|
|
|
|
nicknames = self.get_trust_chain(nickname)
|
|
|
|
certs = [self.get_cert(nickname) for nickname in nicknames]
|
|
|
|
certs = [x509.load_certificate(cert, x509.DER) for cert in certs]
|
|
|
|
|
|
|
|
verify_kdc_cert_validity(certs[-1], certs[:-1], realm)
|