2008-12-21 15:15:53 -06:00
|
|
|
# Authors:
|
|
|
|
# Andrew Wnuk <awnuk@redhat.com>
|
|
|
|
# Jason Gerard DeRose <jderose@redhat.com>
|
|
|
|
#
|
|
|
|
# Copyright (C) 2009 Red Hat
|
|
|
|
# see file 'COPYING' for use and warranty information
|
|
|
|
#
|
|
|
|
# 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; version 2 only
|
|
|
|
#
|
|
|
|
# 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
|
|
|
|
# along with this program; if not, write to the Free Software
|
|
|
|
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
|
|
|
|
|
|
"""
|
2009-02-05 16:30:58 -06:00
|
|
|
Command plugins for IPA-RA certificate operations.
|
2008-12-21 15:15:53 -06:00
|
|
|
"""
|
|
|
|
|
2009-02-12 03:10:12 -06:00
|
|
|
from ipalib import api, SkipPluginModule
|
|
|
|
if api.env.enable_ra is not True:
|
2009-02-12 18:18:54 -06:00
|
|
|
# In this case, abort loading this plugin module...
|
|
|
|
raise SkipPluginModule(reason='env.enable_ra is not True')
|
2009-05-05 14:18:33 -05:00
|
|
|
from ipalib import Command, Str, Int, Bytes, Flag
|
|
|
|
from ipalib import errors
|
2009-07-10 15:40:39 -05:00
|
|
|
from ipalib.plugins.virtual import *
|
2009-10-20 10:59:07 -05:00
|
|
|
from ipalib.plugins.service import split_principal
|
2009-05-05 14:18:33 -05:00
|
|
|
import base64
|
2009-09-10 15:15:14 -05:00
|
|
|
from OpenSSL import crypto
|
2009-10-20 10:59:07 -05:00
|
|
|
from ipalib.request import context
|
|
|
|
from ipapython import dnsclient
|
2009-09-10 15:15:14 -05:00
|
|
|
|
|
|
|
def get_serial(certificate):
|
|
|
|
"""
|
|
|
|
Given a certificate, return the serial number in that cert
|
|
|
|
|
|
|
|
In theory there should be only one cert per object so even if we get
|
|
|
|
passed in a list/tuple only return the first one.
|
|
|
|
"""
|
|
|
|
if type(certificate) in (list, tuple):
|
|
|
|
certificate = certificate[0]
|
|
|
|
try:
|
|
|
|
x509 = crypto.load_certificate(crypto.FILETYPE_ASN1, certificate)
|
|
|
|
serial = str(x509.get_serial_number())
|
|
|
|
except crypto.Error:
|
|
|
|
raise errors.GenericError(format='Unable to decode certificate in entry')
|
|
|
|
|
|
|
|
return serial
|
2009-05-05 14:18:33 -05:00
|
|
|
|
2009-10-20 10:59:07 -05:00
|
|
|
def get_csr_hostname(csr):
|
|
|
|
"""
|
|
|
|
Return the value of CN in the subject of the request
|
|
|
|
"""
|
|
|
|
try:
|
|
|
|
der = base64.b64decode(csr)
|
|
|
|
request = crypto.load_certificate_request(crypto.FILETYPE_ASN1, der)
|
|
|
|
sub = request.get_subject().get_components()
|
|
|
|
for s in sub:
|
|
|
|
if s[0].lower() == "cn":
|
|
|
|
return s[1]
|
|
|
|
except crypto.Error, e:
|
|
|
|
raise errors.GenericError(format='Unable to decode CSR: %s' % str(e))
|
|
|
|
|
|
|
|
return None
|
|
|
|
|
2009-05-05 14:18:33 -05:00
|
|
|
def validate_csr(ugettext, csr):
|
|
|
|
"""
|
|
|
|
For now just verify that it is properly base64-encoded.
|
|
|
|
"""
|
|
|
|
try:
|
|
|
|
base64.b64decode(csr)
|
|
|
|
except Exception, e:
|
|
|
|
raise errors.Base64DecodeError(reason=str(e))
|
2008-12-21 15:15:53 -06:00
|
|
|
|
|
|
|
|
2009-07-10 15:40:39 -05:00
|
|
|
class cert_request(VirtualCommand):
|
2009-02-12 03:10:12 -06:00
|
|
|
"""
|
2009-10-20 10:59:07 -05:00
|
|
|
Submit a certificate signing request.
|
2009-02-12 03:10:12 -06:00
|
|
|
"""
|
2008-12-21 15:15:53 -06:00
|
|
|
|
2009-05-05 14:18:33 -05:00
|
|
|
takes_args = (Str('csr', validate_csr),)
|
2009-07-10 15:40:39 -05:00
|
|
|
operation="request certificate"
|
2008-12-21 15:15:53 -06:00
|
|
|
|
2009-02-12 03:10:12 -06:00
|
|
|
takes_options = (
|
2009-05-05 14:18:33 -05:00
|
|
|
Str('principal',
|
|
|
|
doc="service principal for this certificate (e.g. HTTP/test.example.com)",
|
|
|
|
),
|
|
|
|
Str('request_type',
|
|
|
|
default=u'pkcs10',
|
|
|
|
autofill=True,
|
|
|
|
),
|
|
|
|
Flag('add',
|
|
|
|
doc="automatically add the principal if it doesn't exist",
|
|
|
|
default=False,
|
|
|
|
autofill=True
|
|
|
|
),
|
2009-02-12 03:10:12 -06:00
|
|
|
)
|
2008-12-21 15:15:53 -06:00
|
|
|
|
2009-05-05 14:18:33 -05:00
|
|
|
def execute(self, csr, **kw):
|
2009-11-03 08:35:19 -06:00
|
|
|
ldap = self.api.Backend.ldap2
|
2009-05-05 14:18:33 -05:00
|
|
|
skw = {"all": True}
|
|
|
|
principal = kw.get('principal')
|
|
|
|
add = kw.get('add')
|
|
|
|
del kw['principal']
|
|
|
|
del kw['add']
|
|
|
|
service = None
|
|
|
|
|
2009-10-20 10:59:07 -05:00
|
|
|
# Can this user request certs?
|
|
|
|
self.check_access()
|
|
|
|
|
|
|
|
# FIXME: add support for subject alt name
|
|
|
|
# Is this cert for this principal?
|
|
|
|
subject_host = get_csr_hostname(csr)
|
|
|
|
|
|
|
|
# Ensure that the hostname in the CSR matches the principal
|
|
|
|
(servicename, hostname, realm) = split_principal(principal)
|
|
|
|
if subject_host.lower() != hostname.lower():
|
|
|
|
raise errors.ACIError(info="hostname in subject of request '%s' does not match principal hostname '%s'" % (subject_host, hostname))
|
|
|
|
|
2009-05-05 14:18:33 -05:00
|
|
|
# See if the service exists and punt if it doesn't and we aren't
|
|
|
|
# going to add it
|
|
|
|
try:
|
2009-09-10 15:15:14 -05:00
|
|
|
(dn, service) = api.Command['service_show'](principal, **skw)
|
|
|
|
if 'usercertificate' in service:
|
2009-05-05 14:18:33 -05:00
|
|
|
# FIXME, what to do here? Do we revoke the old cert?
|
2009-09-10 15:15:14 -05:00
|
|
|
raise errors.GenericError(format='entry already has a certificate, serial number %s' % get_serial(service['usercertificate']))
|
2009-05-05 14:18:33 -05:00
|
|
|
except errors.NotFound, e:
|
|
|
|
if not add:
|
2009-09-10 15:15:14 -05:00
|
|
|
raise errors.NotFound(reason="The service principal for this request doesn't exist.")
|
2009-11-03 08:35:19 -06:00
|
|
|
try:
|
|
|
|
(dn, service) = api.Command['service_add'](principal, **{})
|
|
|
|
except errors.ACIError:
|
|
|
|
raise errors.ACIError(info='You need to be a member of the serviceadmin role to add services')
|
|
|
|
|
|
|
|
# We got this far so the service entry exists, can we write it?
|
|
|
|
if not ldap.can_write(dn, "usercertificate"):
|
|
|
|
raise errors.ACIError(info="Insufficient 'write' privilege to the 'userCertificate' attribute of entry '%s'." % dn)
|
2009-05-05 14:18:33 -05:00
|
|
|
|
|
|
|
# Request the certificate
|
|
|
|
result = self.Backend.ra.request_certificate(csr, **kw)
|
|
|
|
|
2009-11-03 08:35:19 -06:00
|
|
|
# Success? Then add it to the service entry.
|
|
|
|
if result.get('status') == 0:
|
2009-05-05 14:18:33 -05:00
|
|
|
skw = {"usercertificate": str(result.get('certificate'))}
|
|
|
|
api.Command['service_mod'](principal, **skw)
|
|
|
|
|
|
|
|
return result
|
|
|
|
|
|
|
|
def output_for_cli(self, textui, result, *args, **kw):
|
2009-02-12 03:10:12 -06:00
|
|
|
if isinstance(result, dict) and len(result) > 0:
|
|
|
|
textui.print_entry(result, 0)
|
|
|
|
else:
|
|
|
|
textui.print_plain('Failed to submit a certificate request.')
|
2008-12-21 15:15:53 -06:00
|
|
|
|
2009-09-10 15:15:14 -05:00
|
|
|
def run(self, *args, **options):
|
|
|
|
"""
|
|
|
|
Dispatch to forward() and execute() to do work locally and on the
|
|
|
|
server.
|
|
|
|
"""
|
|
|
|
if self.env.in_server:
|
|
|
|
return self.execute(*args, **options)
|
|
|
|
|
|
|
|
# Client-side code
|
|
|
|
csr = args[0]
|
|
|
|
if csr[:7] == "file://":
|
|
|
|
file = csr[7:]
|
|
|
|
try:
|
|
|
|
f = open(file, "r")
|
|
|
|
csr = f.readlines()
|
|
|
|
f.close()
|
|
|
|
except IOError, err:
|
|
|
|
raise errors.ValidationError(name='csr', error=err[1])
|
|
|
|
csr = "".join(csr)
|
|
|
|
# We just want the CSR bits, make sure there is nothing else
|
|
|
|
s = csr.find("-----BEGIN NEW CERTIFICATE REQUEST-----")
|
|
|
|
e = csr.find("-----END NEW CERTIFICATE REQUEST-----")
|
|
|
|
if s >= 0:
|
|
|
|
csr = csr[s+40:e]
|
|
|
|
csr = csr.decode('UTF-8')
|
|
|
|
return self.forward(csr, **options)
|
|
|
|
|
2009-02-12 03:10:12 -06:00
|
|
|
api.register(cert_request)
|
2008-12-21 15:15:53 -06:00
|
|
|
|
|
|
|
|
2009-07-10 15:40:39 -05:00
|
|
|
class cert_status(VirtualCommand):
|
2009-02-12 03:10:12 -06:00
|
|
|
"""
|
|
|
|
Check status of a certificate signing request.
|
|
|
|
"""
|
2009-02-05 16:30:58 -06:00
|
|
|
|
2009-05-21 13:31:54 -05:00
|
|
|
takes_args = ('request_id')
|
2009-07-10 15:40:39 -05:00
|
|
|
operation = "certificate status"
|
2008-12-21 15:15:53 -06:00
|
|
|
|
|
|
|
|
2009-05-05 14:18:33 -05:00
|
|
|
def execute(self, request_id, **kw):
|
2009-10-20 10:59:07 -05:00
|
|
|
self.check_access()
|
2009-02-12 03:10:12 -06:00
|
|
|
return self.Backend.ra.check_request_status(request_id)
|
2008-12-21 15:15:53 -06:00
|
|
|
|
2009-05-05 14:18:33 -05:00
|
|
|
def output_for_cli(self, textui, result, *args, **kw):
|
2009-02-12 03:10:12 -06:00
|
|
|
if isinstance(result, dict) and len(result) > 0:
|
|
|
|
textui.print_entry(result, 0)
|
|
|
|
else:
|
|
|
|
textui.print_plain('Failed to retrieve a request status.')
|
2008-12-21 15:15:53 -06:00
|
|
|
|
2009-02-12 03:10:12 -06:00
|
|
|
api.register(cert_status)
|
2008-12-21 15:15:53 -06:00
|
|
|
|
|
|
|
|
2009-07-10 15:40:39 -05:00
|
|
|
class cert_get(VirtualCommand):
|
2009-02-12 03:10:12 -06:00
|
|
|
"""
|
|
|
|
Retrieve an existing certificate.
|
|
|
|
"""
|
2008-12-21 15:15:53 -06:00
|
|
|
|
2009-05-21 13:31:54 -05:00
|
|
|
takes_args = ('serial_number')
|
2009-07-10 15:40:39 -05:00
|
|
|
operation="retrieve certificate"
|
2008-12-21 15:15:53 -06:00
|
|
|
|
2009-02-12 03:10:12 -06:00
|
|
|
def execute(self, serial_number):
|
2009-10-20 10:59:07 -05:00
|
|
|
self.check_access()
|
2009-02-12 03:10:12 -06:00
|
|
|
return self.Backend.ra.get_certificate(serial_number)
|
2008-12-21 15:15:53 -06:00
|
|
|
|
2009-05-05 14:18:33 -05:00
|
|
|
def output_for_cli(self, textui, result, *args, **kw):
|
2009-02-12 03:10:12 -06:00
|
|
|
if isinstance(result, dict) and len(result) > 0:
|
|
|
|
textui.print_entry(result, 0)
|
|
|
|
else:
|
|
|
|
textui.print_plain('Failed to obtain a certificate.')
|
2008-12-21 15:15:53 -06:00
|
|
|
|
2009-02-12 03:10:12 -06:00
|
|
|
api.register(cert_get)
|
2008-12-21 15:15:53 -06:00
|
|
|
|
|
|
|
|
2009-07-10 15:40:39 -05:00
|
|
|
class cert_revoke(VirtualCommand):
|
2009-02-12 03:10:12 -06:00
|
|
|
"""
|
|
|
|
Revoke a certificate.
|
|
|
|
"""
|
2008-12-21 15:15:53 -06:00
|
|
|
|
2009-05-21 13:31:54 -05:00
|
|
|
takes_args = ('serial_number')
|
2009-07-10 15:40:39 -05:00
|
|
|
operation = "revoke certificate"
|
2008-12-21 15:15:53 -06:00
|
|
|
|
2009-02-12 03:10:12 -06:00
|
|
|
# FIXME: The default is 0. Is this really an Int param?
|
2009-05-08 13:10:53 -05:00
|
|
|
takes_options = (
|
|
|
|
Int('revocation_reason?',
|
|
|
|
doc='Reason for revoking the certificate (0-10)',
|
|
|
|
minvalue=0,
|
|
|
|
maxvalue=10,
|
|
|
|
default=0,
|
|
|
|
),
|
|
|
|
)
|
2008-12-21 15:15:53 -06:00
|
|
|
|
|
|
|
|
2009-05-05 14:18:33 -05:00
|
|
|
def execute(self, serial_number, **kw):
|
2009-10-20 10:59:07 -05:00
|
|
|
self.check_access()
|
2009-05-05 14:18:33 -05:00
|
|
|
return self.Backend.ra.revoke_certificate(serial_number, **kw)
|
2008-12-21 15:15:53 -06:00
|
|
|
|
2009-05-05 14:18:33 -05:00
|
|
|
def output_for_cli(self, textui, result, *args, **kw):
|
2009-02-12 03:10:12 -06:00
|
|
|
if isinstance(result, dict) and len(result) > 0:
|
|
|
|
textui.print_entry(result, 0)
|
|
|
|
else:
|
|
|
|
textui.print_plain('Failed to revoke a certificate.')
|
2008-12-21 15:15:53 -06:00
|
|
|
|
2009-02-12 03:10:12 -06:00
|
|
|
api.register(cert_revoke)
|
2008-12-21 15:15:53 -06:00
|
|
|
|
|
|
|
|
2009-07-10 15:40:39 -05:00
|
|
|
class cert_remove_hold(VirtualCommand):
|
2009-02-12 03:10:12 -06:00
|
|
|
"""
|
|
|
|
Take a revoked certificate off hold.
|
|
|
|
"""
|
2008-12-21 15:15:53 -06:00
|
|
|
|
2009-05-21 13:31:54 -05:00
|
|
|
takes_args = ('serial_number')
|
2009-07-10 15:40:39 -05:00
|
|
|
operation = "certificate remove hold"
|
2008-12-21 15:15:53 -06:00
|
|
|
|
2009-05-05 14:18:33 -05:00
|
|
|
def execute(self, serial_number, **kw):
|
2009-10-20 10:59:07 -05:00
|
|
|
self.check_access()
|
2009-02-12 03:10:12 -06:00
|
|
|
return self.Backend.ra.take_certificate_off_hold(serial_number)
|
|
|
|
|
2009-05-05 14:18:33 -05:00
|
|
|
def output_for_cli(self, textui, result, *args, **kw):
|
2009-02-12 03:10:12 -06:00
|
|
|
if isinstance(result, dict) and len(result) > 0:
|
|
|
|
textui.print_entry(result, 0)
|
|
|
|
else:
|
|
|
|
textui.print_plain('Failed to take a revoked certificate off hold.')
|
|
|
|
|
|
|
|
api.register(cert_remove_hold)
|