Add kx509 client and revamp kx509 service

This commit adds support for kx509 in libkrb5, and revamps the KDC's
kx509 service (fixing bugs, adding features).

Of note is that kx509 is attempted optimistically by the client, with
the certificate and private key stored in the ccache, and optionally in
an external PEM or DER file.

NOTE: We do not optimistically use kx509 in krb5_cc_store_cred() if the
      ccache is a MEMORY ccache so we don't generate a key when
      accepting a GSS context with a delegated credential.

kx509 protocol issues to be fixed in an upcoming commit:

 - no proof of possession (this is mostly not too bad, but we'll want to
   fix it by using CSRs)
 - no algorithm agility (only plain RSA is supported)
 - very limited (no way to request any options in regards to the
   requested cert)
 - error codes are not very useful

Things we're adding in this commit:

 - libkrb5 kx509 client
 - automatic kx509 usage hooked in via krb5_cc_store_cred() of start TGT
 - per-realm templates on the KDC side
 - per-realm issuer certificates
 - send error messages on the KDC side
   (this is essential to avoid client-side timeouts on error)
 - authenticate as many error messages
 - add a protocol probe feature so we can avoid generating a
   keypair if the service is not enabled
   (once we add support for ECC algorithms we won't need this
    anymore; the issue is that RSA keygen is slow)
 - support for different types of client principals, not just username:

    - host-based service and domain-based service, each with its own
      template set per-{realm, service} or per-service

   (the idea is to support issuance of server certificates too, not
    just client/user certs)
 - more complete support for SAN types
 - tests (including that PKINIT->kx509->PKINIT works, which makes it
   possible to have "delegation" of PKIX credentials by just delegating
   Kerberos credentials)
 - document the protocol in lib/krb5/kx509.c

Future work:

 - add option for longer-ticket-lifetime service certs
 - add support for ECDSA, and some day for ed25519 and ed448
 - reuse private key when running kinit
   (this will require rethinking how we trigger optimistic kx509
    usage)
 - HDB lookup for:
    - optional revocation check (not strictly necessary)
    - adding to certificates those SANs listed in HDB
       - hostname aliases (dNSName SANs)
       - rfc822Name (email)
       - XMPP SANs
       - id-pkinit-san (a user could have aliases too)
 - support username wild-card A RRs, ala OSKT/krb5_admin
    i.e., if a host/f.q.d.n principal asks for a certificate for
    some service at some-label.f.q.d.n, then issue it
   (this is not needed at OSKT sites because OSKT already
    supports keying such service principals, which means kx509
    will issue certificates for them, however, it would be nice
    to be able to have this independent of OSKT)
   (a better way to do this would be to integrate more of OSKT
    into Heimdal proper)
 - a kx509 command, or heimtools kx509 subcommand for explicitly
   attempting use of the kx509 protocol (as opposed to implicit, as is
   done in kinit via krb5_cc_store_cred() magic right now)

Issues:

 - optimistically trying kx509 on start realm TGT store -> timeout issues!
    - newer KDCs will return errors because of this commit; older ones
      will not, which causes timouts
    - need a separate timeout setting for kx509 for optimistic case
    - need a [realm] config item and DNS SRV RR lookup for whether a
      realm is expected to support kx509 service
This commit is contained in:
Nicolas Williams
2019-06-17 19:34:21 -05:00
parent 78cb995e6e
commit 6a7e7eace6
23 changed files with 2729 additions and 263 deletions

View File

@@ -1,5 +1,7 @@
-- $Id$
-- The kx509 protocol is documented in RFC6717.
KX509 DEFINITIONS ::=
BEGIN
@@ -12,20 +14,134 @@ KX509-ERROR-CODE ::= INTEGER {
KX509-STATUS-SERVER-TEMP(5),
-- 6 is used internally in the umich client, avoid that
KX509-STATUS-SERVER-KEY(7)
-- Let us reserve 1000+ for Kebreros protocol wire error codes -Nico
}
-- Version 2, which has no proof of possession
Kx509Request ::= SEQUENCE {
authenticator OCTET STRING,
pk-hash OCTET STRING,
pk-key OCTET STRING
pk-hash OCTET STRING, -- HMAC(ticket_session_key, pk-key)
pk-key OCTET STRING -- the public key, DER-encoded (RSA, basically)
}
-- Kx509ErrorCode is a Heimdal-specific enhancement with no change on the wire,
-- and really only just so the error-code field below can fit on one line.
Kx509ErrorCode ::= INTEGER (-2147483648..2147483647)
Kx509Response ::= SEQUENCE {
error-code[0] INTEGER (-2147483648..2147483647)
OPTIONAL -- DEFAULT 0 --,
hash[1] OCTET STRING OPTIONAL,
certificate[2] OCTET STRING OPTIONAL,
error-code[0] Kx509ErrorCode DEFAULT 0,
hash[1] OCTET STRING OPTIONAL, -- HMAC(session_key, ...)
certificate[2] OCTET STRING OPTIONAL, -- Certificates (plural)
-- if client used a
-- Kx509CSRPlus
e-text[3] VisibleString OPTIONAL
}
-- Offset for Kerberos protocol errors when error-code set to one:
kx509-krb5-error-base INTEGER ::= 1000
-- RFC6717 says this about error codes:
--
-- +------------+-----------------------------+------------------------+
-- | error-code | Condition | Example |
-- +------------+-----------------------------+------------------------+
-- | 1 | Permanent problem with | Incompatible version |
-- | | client request | |
-- | 2 | Solvable problem with | Expired Kerberos |
-- | | client request | credentials |
-- | 3 | Temporary problem with | Packet loss |
-- | | client request | |
-- | 4 | Permanent problem with the | Internal |
-- | | server | misconfiguration |
-- | 5 | Temporary problem with the | Server overloaded |
-- | | server | |
-- +------------+-----------------------------+------------------------+
--
-- Looking at UMich CITI's kca (server-side of kx509) implementation, it always
-- sends 0 as the status code, and the UMich CITI kx509 client never checks it.
-- All of these error codes are local only in the UMich CITI implementation.
--
-- Meanwhile, Heimdal used to never send error responses at all.
--
-- As a result we can use whatever error codes we want. We'll send Kerberos
-- protocol errors + 1000. And we'll never use RFC6717 error codes at all.
--
-- Looking at umich source...
--
-- #define KX509_STATUS_GOOD 0 /* No problems handling client request */
-- #define KX509_STATUS_CLNT_BAD 1 /* Client-side permanent problem */
-- /* ex. version incompatible */
-- #define KX509_STATUS_CLNT_FIX 2 /* Client-side solvable problem */
-- /* ex. re-authenticate */
-- #define KX509_STATUS_CLNT_TMP 3 /* Client-side temporary problem */
-- /* ex. packet loss */
-- #define KX509_STATUS_SRVR_BAD 4 /* Server-side permanent problem */
-- /* ex. server broken */
-- #define KX509_STATUS_SRVR_TMP 5 /* Server-side temporary problem */
-- /* ex. server overloaded */
-- #define KX509_STATUS_SRVR_CANT_CLNT_VERS 6 /* Server-side doesn't handle */
-- /* existence of client_version */
-- /* field in KX509_REQUEST */
--
-- The umich server uses these errors in these situations:
--
-- - KX509_STATUS_SRVR_TMP is for:
-- - request decode errors
-- - krb5_is_ap_req() errors
-- - wrong Kerberos protocol vno in AP-REQ
-- - some ENOMEMs
-- - UDP read errors (??)
-- - LDAP issues (they use LDAP to map realm-chopped user princ names to
-- full names)
-- - pk decode errors
-- - KX509_STATUS_CLNT_TMP is for:
-- - HMAC mismatch
-- - some ENOMEMs
-- - failure to accept AP-REQ
-- - failure to unparse princ names from AP-REQ's Ticket
-- - KX509_STATUS_SRVR_BAD is for:
-- - configuration issues (missing issuer creds)
-- - serial number transaction issues (we should randomize)
-- - subjectName construction issues
-- - certificate construction issues (ENOMEM, say)
-- - failure to authenticate (never happens, since KX509_STATUS_CLNT_TMP is
-- used earlier when krb5_rd_req() fails)
-- - KX509_STATUS_CLNT_FIX is for:
-- - more than one component client principals
-- - client princ name component zero string length shorter than 3 or
-- longer than 8 (WTF)
-- - other policy issues
-- - KX509_STATUS_CLNT_BAD
-- - wrong protocol version number (version_2_0)
-- Possible new version designs:
--
-- - keep the protocol the same but use a CSR instead of a raw RSA public key
-- - on the server try decoding first a CSR, then a raw RSA public key
--
-- - keep the protocol the same but use either a CSR or a self-signed cert
-- - on the server try decoding first a Certificate, then a CSR, then a raw
-- RSA public key
--
-- CSRs are a pain to deal with. Self-signed certificates can act as a
-- CSR of a sort. Use notBefore == 1970-01-01T00:00:00Z and an EKU
-- denoting "this certificate is really a much-easier-to-work-with CSR
-- alternative".
--
-- - keep the protocol similar, but use the checksum field of the
-- Authenticator to authenticate the request data; use a KRB-PRIV for the
-- reply
--
-- - extend the KDC/AS/TGS protocols to support certificate issuance, either
-- at the same time as ticket acquisition, or as an alternative
-- - send a CSR as a authz-data element
-- - expect an EncryptedData with the issued Certificate inside as the
-- Ticket in the result (again, ugly hack)
-- - or maybe just add new messages, but, the thing is that the existing
-- "AP-REP + stuff" kx509 protocol is a fine design pattern, there's no
-- need to radically change it, just slightly.
--
-- The main benefit of using an extension to the KDC/AS/TGS protocols is that
-- we could then use FAST for confidentiality protection.
END

View File

@@ -71,7 +71,7 @@ libkrb5_la_LIBADD = \
$(top_builddir)/lib/ipc/libheim-ipcc.la \
$(top_builddir)/lib/wind/libwind.la \
$(top_builddir)/lib/base/libheimbase.la \
$(LIB_pkinit) \
$(top_builddir)/lib/hx509/libhx509.la \
$(LIB_openssl_crypto) \
$(use_sqlite) \
$(LIB_com_err) \
@@ -100,7 +100,7 @@ librfc3961_la_LIBADD = \
lib_LTLIBRARIES = libkrb5.la
ERR_FILES = krb5_err.c krb_err.c heim_err.c k524_err.c k5e1_err.c
ERR_FILES = krb5_err.c krb_err.c heim_err.c k524_err.c k5e1_err.c kx509_err.c
libkrb5_la_CPPFLAGS = \
-DBUILD_KRB5_LIB \
@@ -191,6 +191,7 @@ dist_libkrb5_la_SOURCES = \
krbhst.c \
kuserok.c \
kuserok_plugin.h \
kx509.c \
log.c \
mcache.c \
misc.c \
@@ -281,7 +282,8 @@ ALL_OBJECTS += $(test_renew_OBJECTS)
ALL_OBJECTS += $(test_rfc3961_OBJECTS)
$(ALL_OBJECTS): $(srcdir)/krb5-protos.h $(srcdir)/krb5-private.h
$(ALL_OBJECTS): krb5_err.h heim_err.h k524_err.h k5e1_err.h krb_err.h k524_err.h
$(ALL_OBJECTS): krb5_err.h heim_err.h k524_err.h k5e1_err.h \
krb_err.h k524_err.h kx509_err.h
librfc3961_la_SOURCES = \
crc.c \
@@ -385,7 +387,7 @@ dist_include_HEADERS = \
noinst_HEADERS = $(srcdir)/krb5-private.h
nodist_include_HEADERS = krb5_err.h heim_err.h k524_err.h k5e1_err.h
nodist_include_HEADERS = krb5_err.h heim_err.h k524_err.h k5e1_err.h kx509_err.h
# XXX use nobase_include_HEADERS = krb5/locate_plugin.h
krb5dir = $(includedir)/krb5
@@ -409,9 +411,10 @@ CLEANFILES = \
krb_err.c krb_err.h \
heim_err.c heim_err.h \
k524_err.c k524_err.h \
k5e1_err.c k5e1_err.h
k5e1_err.c k5e1_err.h \
kx509_err.c kx509_err.h
$(libkrb5_la_OBJECTS): krb5_err.h krb_err.h heim_err.h k524_err.h k5e1_err.h
$(libkrb5_la_OBJECTS): krb5_err.h krb_err.h heim_err.h k524_err.h k5e1_err.h kx509_err.h
test_config_strings.out: test_config_strings.cfg
$(CP) $(srcdir)/test_config_strings.cfg test_config_strings.out
@@ -427,6 +430,7 @@ EXTRA_DIST = \
heim_err.et \
k524_err.et \
k5e1_err.et \
kx509_err.et \
$(man_MANS) \
version-script.map \
test_config_strings.cfg \
@@ -445,3 +449,5 @@ heim_err.h: heim_err.et
k524_err.h: k524_err.et
k5e1_err.h: k5e1_err.et
kx509_err.h: kx509_err.et

View File

@@ -103,6 +103,7 @@ libkrb5_OBJS = \
$(OBJ)\keytab_memory.obj \
$(OBJ)\krbhst.obj \
$(OBJ)\kuserok.obj \
$(OBJ)\kx509.obj \
$(OBJ)\log.obj \
$(OBJ)\mcache.obj \
$(OBJ)\misc.obj \
@@ -173,6 +174,7 @@ INCFILES= \
$(INCDIR)\heim_err.h \
$(INCDIR)\k524_err.h \
$(INCDIR)\k5e1_err.h \
$(INCDIR)\kx509_err.h \
$(INCDIR)\kcm.h \
$(INCDIR)\krb_err.h \
$(INCDIR)\krb5.h \
@@ -268,6 +270,7 @@ dist_libkrb5_la_SOURCES = \
krb5-v4compat.h \
krbhst.c \
kuserok.c \
kx509.c \
log.c \
mcache.c \
misc.c \
@@ -360,6 +363,11 @@ $(OBJ)\k5e1_err.c $(OBJ)\k5e1_err.h: k5e1_err.et
$(BINDIR)\compile_et.exe $(SRCDIR)\k5e1_err.et
cd $(SRCDIR)
$(OBJ)\kx509_err.c $(OBJ)\kx509_err.h: kx509_err.et
cd $(OBJ)
$(BINDIR)\compile_et.exe $(SRCDIR)\kx509_err.et
cd $(SRCDIR)
#----------------------------------------------------------------------
# libkrb5

View File

@@ -716,8 +716,12 @@ krb5_cc_initialize(krb5_context context,
krb5_error_code ret;
ret = (*id->ops->init)(context, id, primary_principal);
if (ret == 0)
id->initialized = 1;
if (ret == 0) {
id->cc_kx509_done = 0;
id->cc_initialized = 1;
id->cc_need_start_realm = 1;
id->cc_start_tgt_stored = 0;
}
return ret;
}
@@ -735,11 +739,32 @@ KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
krb5_cc_destroy(krb5_context context,
krb5_ccache id)
{
krb5_error_code ret2 = 0;
krb5_error_code ret;
krb5_data d;
/*
* Destroy associated hx509 PKIX credential store created by krb5_kx509*().
*/
if ((ret = krb5_cc_get_config(context, id, NULL, "kx509store", &d)) == 0) {
char *name;
if ((name = strndup(d.data, d.length)) == NULL) {
ret2 = krb5_enomem(context);
} else {
hx509_certs certs;
ret = hx509_certs_init(context->hx509ctx, name, 0, NULL, &certs);
if (ret == 0)
ret2 = hx509_certs_destroy(context->hx509ctx, &certs);
else
hx509_certs_free(&certs);
free(name);
}
}
ret = (*id->ops->destroy)(context, id);
krb5_cc_close (context, id);
return ret;
(void) krb5_cc_close(context, id);
return ret ? ret : ret2;
}
/**
@@ -756,6 +781,44 @@ krb5_cc_close(krb5_context context,
krb5_ccache id)
{
krb5_error_code ret;
/*
* We want to automatically acquire a PKIX credential using kx509.
*
* This can be slow if we're generating an RSA key. Plus it means talking
* to the KDC.
*
* We only want to do this when:
*
* - krb5_cc_initialize() was called on this ccache handle,
* - a start TGT was stored (actually, a cross-realm TGT would do),
*
* and
*
* - we aren't creating a gss_cred_id_t for a delegated credential.
*
* We only have a heuristic for the last condition: that `id' is not a
* MEMORY ccache, which is what's used for delegated credentials.
*
* We really only want to do this when storing a credential in a user's
* default ccache, but we leave it to krb5_kx509() to do that check.
*
* XXX Perhaps we should do what krb5_kx509() does here, and just call
* krb5_kx509_ext() (renamed to krb5_kx509()). Then we wouldn't need
* the delegated cred handle heuristic.
*/
if (id->cc_initialized && id->cc_start_tgt_stored && !id->cc_kx509_done &&
strcmp("MEMORY", krb5_cc_get_type(context, id)) != 0) {
_krb5_debug(context, 2, "attempting to fetch a certificate using "
"kx509");
ret = krb5_kx509(context, id, NULL);
if (ret)
_krb5_debug(context, 2, "failed to fetch a certificate");
else
_krb5_debug(context, 2, "fetched a certificate");
ret = 0;
}
ret = (*id->ops->close)(context, id);
free(id);
return ret;
@@ -777,31 +840,49 @@ krb5_cc_store_cred(krb5_context context,
{
krb5_error_code ret;
krb5_data realm;
const char *cfg = "";
ret = (*id->ops->store)(context, id, creds);
if (ret)
return ret;
/* Look for and mark the first root TGT's realm as the start realm */
if (ret == 0 && id->initialized &&
/* Automatic cc_config-setting and other actions */
if (krb5_principal_get_num_comp(context, creds->server) > 1 &&
krb5_is_config_principal(context, creds->server))
cfg = krb5_principal_get_comp_string(context, creds->server, 1);
if (id->cc_initialized && !id->cc_start_tgt_stored &&
krb5_principal_is_root_krbtgt(context, creds->server)) {
id->initialized = 0;
/* Mark the first root TGT's realm as the start realm */
id->cc_start_tgt_stored = 1;
id->cc_need_start_realm = 0;
realm.length = strlen(creds->server->realm);
realm.data = creds->server->realm;
(void) krb5_cc_set_config(context, id, NULL, "start_realm", &realm);
} else if (ret == 0 && id->initialized &&
krb5_is_config_principal(context, creds->server) &&
strcmp(creds->server->name.name_string.val[1], "start_realm") == 0) {
} else if (id->cc_initialized && id->cc_start_tgt_stored &&
!id->cc_kx509_done && strcmp(cfg, "kx509cert") == 0) {
/*
* But if the caller is storing a start_realm ccconfig, then
* stop looking for root TGTs to mark as the start_realm.
*
* By honoring any start_realm cc config stored, we interop
* both, with ccache implementations that don't preserve
* insertion order, and Kerberos implementations that store this
* cc config before the TGT.
* Do not attempt kx509 at cc close time -- we're copying a ccache and
* we've already got a cert (and private key).
*/
id->initialized = 0;
id->cc_kx509_done = 1;
} else if (id->cc_initialized && id->cc_start_tgt_stored &&
!id->cc_kx509_done && strcmp(cfg, "kx509_service_status") == 0) {
/*
* Do not attempt kx509 at cc close time -- we're copying a ccache and
* we know the kx509 service is not available.
*/
id->cc_kx509_done = 1;
} else if (id->cc_initialized && strcmp(cfg, "start_realm") == 0) {
/*
* If the caller is storing a start_realm ccconfig, then stop looking
* for root TGTs to mark as the start_realm.
*
* By honoring any start_realm cc config stored, we interop both, with
* ccache implementations that don't preserve insertion order, and
* Kerberos implementations that store this cc config before the TGT.
*/
id->cc_need_start_realm = 0;
}
return ret;
}

View File

@@ -342,6 +342,28 @@ be allowed to run.
.It Li fcache_strict_checking
strict checking in FILE credential caches that owner, no symlink and
permissions is correct.
.It Li enable-kx509 = Va boolean
Enable use of kx509 so that every TGT that can has a corresponding
PKIX certificate. Default: false.
.It Li kx509_gen_key_type = Va public-key-type
Type of public key for kx509 private key generation. Defaults to
.Va rsa
and currently only
.Va rsa
is supported.
.It Li kx509_gen_rsa_key_size = Va number-of-bits
RSA key size for kx509. Defaults to 2048.
.It Li kx509_store = path
A file path into which to write a certificate obtained with
kx509, and its private key, when attempting kx509 optimistically
using credentials from a default ccache. Tokens will be
expanded.
.It Li kx509_hostname = Va hostname
If set, then the kx509 client will use this hostname for the
kx509 service. This can also be set in the
.Li [realm]
section on a per-realm basis. If not set then a TGS name will be
used.
.It Li name_canon_rules = Va rules
One or more service principal name canonicalization rules. Each rule
consists of one or more tokens separated by colon (':'). Currently
@@ -706,6 +728,8 @@ Should the kdc answer digest requests. The default is FALSE.
.It Li digests_allowed = Va list of digests
Specifies the digests the kdc will reply to. The default is
.Li ntlm-v2 .
.It Li enable-kx509 = Va boolean
Enables kx509 service.
.It Li kx509_ca = Va file
Specifies the PEM credentials for the kx509 certification authority.
.It Li require_initial_kca_tickets = Va boolean
@@ -718,13 +742,31 @@ Defaults to true for the global setting.
If true then the kx509 client principal's name and realm will be
included in an
.Li id-pkinit-san
certificate extension.
subject alternative name certificate extension.
This can be set on a per-realm basis as well as globally.
Defaults to true for the global setting.
.It Li kx509_include_email_san = Va boolean
If true then the kx509 client user principal's name and realm will be
included in an
.Li rfc822Name
subject alternative name certificate extension, with the downcased
realm as the domainname.
This can be set on a per-realm basis as well as globally.
Defaults to false for the global setting.
.It Li kx509_include_dnsname_san = Va boolean
If true then the kx509 host-based or domain-based client principal's
hostname will be included in an
.Li dNSName
subject alternative name certificate extension, with the
downcased realm as the domainname. This can be set on a
per-realm basis as well as
globally. Defaults to false for the global setting.
.It Li kx509_template = Va file
Specifies the PEM file with a template for the certificates to be
issued.
The following variables can be interpolated in the subject name using
issued to kx509 clients whose principal names have one component
(i.e., are user principals). A template is a certificate with
variables to be interpolated in the subjectName. The following
variables can be interpolated in the subject name using
${variable} syntax:
.Bl -tag -width "xxx" -offset indent
.It principal-name
@@ -734,6 +776,53 @@ The full name of the kx509 client principal, excluding the realm name.
.It principal-name-realm
The name of the client principal's realm.
.El
.It Li kx509_templates = {
.Bl -tag -width "xxx" -offset indent
.It Li two_component_user = {
.Bl -tag -width "xxx" -offset indent
.It Va first-component-of-principal-name = Va file
.It ...
.It Li }
.El
.It Li hostbased = {
.Bl -tag -width "xxx" -offset indent
.It Va service = Va file
.It ...
.It Li }
.El
.It Li domainbased = {
.Bl -tag -width "xxx" -offset indent
.It Va service = Va file
.It ...
.It Li }
.El
.It Li }
.El
Specifies the PEM files with templates for the certificates to be
issued to clients with principal names with two or three name
components. This is useful for issuing server certificates to
host-based principals. The following variables can be
interpolated in the subject name using
.Va ${variable}
syntax:
.Bl -tag -width "xxx" -offset indent
.It principal-name
The full name of the kx509 client principal.
.It principal-name-without-realm
The full name of the kx509 client principal, excluding the realm name.
.It principal-name-realm
The name of the client principal's realm.
.It principal-component0
The first component of the client principal.
.It principal-component1
The second component of the client principal.
.It principal-component2
The third component of the client principal.
.It principal-service-name
The name of the service.
.It principal-host-name
The name of the host.
.El
.El
The
.Li kx509 ,

View File

@@ -386,7 +386,10 @@ typedef struct krb5_cccol_cursor_data *krb5_cccol_cursor;
typedef struct krb5_ccache_data {
const struct krb5_cc_ops *ops;
krb5_data data;
int initialized; /* if non-zero: krb5_cc_initialize() called, now empty */
unsigned int cc_initialized:1; /* if 1: krb5_cc_initialize() called */
unsigned int cc_need_start_realm:1;
unsigned int cc_start_tgt_stored:1;
unsigned int cc_kx509_done:1;
}krb5_ccache_data;
typedef struct krb5_ccache_data *krb5_ccache;

796
lib/krb5/kx509.c Normal file
View File

@@ -0,0 +1,796 @@
/*
* Copyright (c) 2019 Kungliga Tekniska Högskolan
* (Royal Institute of Technology, Stockholm, Sweden).
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* 3. Neither the name of the Institute nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#include "krb5_locl.h"
#include <kx509_asn1.h>
#include <kx509_err.h>
#include "../hx509/hx_locl.h" /* XXX find a better way */
#include "hx509-private.h"
/*
* This file implements a client for the kx509 protocol -- a Kerberized online
* CA that can issue a Certificate to a client that authenticates using
* Kerberos.
*
* The kx509 protocol is the inverse of PKINIT. Whereas PKINIT allows users
* with PKIX credentials to acquire Kerberos credentials, the kx509 protocol
* allows users with Kerberos credentials to acquire PKIX credentials.
*
* I.e., kx509 is a bridge, just like PKINIT.
*
* The kx509 protocol is very simple, and very limited.
*
* A request consists of a DER-encoded Kx509Request message prefixed with four
* bytes identifying the protocol (see `version_2_0' below).
*
* A Kx509Request message contains an AP-REQ, a public key, and an HMAC of the
* public key made with the session key of the AP-REQ's ticket.
*
* The service principal can be either kca_service/hostname.fqdn or
* krbtgt/REALM (a Heimdal innovation).
*
* If a request is missing a public key, then the request is a probe intended
* to discover whether the service is enabled, thus helping the client avoid
* a possibly-slow private key generation operation.
*
* The response is a DER-encoded Kx509Response also prefixed with
* `version_2_0', and contains: an optional error code and error text, an
* optional certificate (for the success case), and an optional HMAC of those
* fields that is present when the service was able to verify the AP-REQ.
*
* Limitations:
*
* - no proof of possession for the public key
* - only RSA keys are supported
* - no way to express options (e.g., what KUs, EKUs, or SANs are desired)
* - no sub-session key usage
* - no reflection protection other than the HMAC's forgery protection and the
* fact that the client could tell that a reflected attack isn't success
*
* Future directions:
*
* - Since the public key field of the request is an OCTET STRING, we could
* send a CSR, or even an expired certificate (possibly self-signed,
* possibly one issued earlier) that can serve as a template.
*
* This solves the first three limitations, as it allows the client to
* demonstrate proof of possession, allows arbitrary public key types, and
* allows the client to express desires about the to-be-issued certificate.
*
* - Use the AP-REQ's Authenticator's sub-session key for the HMAC, and derive
* per-direction sub-sub-keys.
*
* - We might design a new protocol that better fits the RFC4120 KDC message
* framework.
*/
static const unsigned char version_2_0[4] = {0 , 0, 2, 0};
struct kx509_ctx_data {
char *send_to_realm; /* realm to which to send request */
krb5_keyblock *hmac_key; /* For HMAC validation */
hx509_private_key *keys;
hx509_private_key priv_key;
};
static krb5_error_code
load_priv_key(krb5_context context,
struct kx509_ctx_data *kx509_ctx,
const char *fn)
{
hx509_private_key *keys = NULL;
hx509_certs certs = NULL;
krb5_error_code ret;
ret = hx509_certs_init(context->hx509ctx, fn, 0, NULL, &certs);
if (ret == ENOENT)
return 0;
if (ret == 0)
ret = _hx509_certs_keys_get(context->hx509ctx, certs, &keys);
if (ret == 0 && keys[0] == NULL)
ret = ENOENT;
if (ret == 0)
kx509_ctx->priv_key = _hx509_private_key_ref(keys[0]);
if (ret)
krb5_set_error_message(context, ret, "Could not load private key "
"from %s for kx509: %s", fn,
hx509_get_error_string(context->hx509ctx, ret));
hx509_certs_free(&certs);
return ret;
}
static krb5_error_code
gen_priv_key(krb5_context context,
const char *gen_type,
unsigned long gen_bits,
hx509_private_key *key)
{
struct hx509_generate_private_context *key_gen_ctx = NULL;
krb5_error_code ret;
_krb5_debug(context, 1, "kx509: gen priv key");
if (strcmp(gen_type, "rsa")) {
krb5_set_error_message(context, ENOTSUP, "Key type %s is not "
"supported for kx509; only \"rsa\" is "
"supported for kx509 at this time",
gen_type);
return ENOTSUP;
}
ret = _hx509_generate_private_key_init(context->hx509ctx,
ASN1_OID_ID_PKCS1_RSAENCRYPTION,
&key_gen_ctx);
if (ret == 0)
ret = _hx509_generate_private_key_bits(context->hx509ctx, key_gen_ctx, gen_bits);
if (ret == 0)
ret = _hx509_generate_private_key(context->hx509ctx, key_gen_ctx, key);
_hx509_generate_private_key_free(&key_gen_ctx);
if (ret)
krb5_set_error_message(context, ret,
"Could not generate a private key: %s",
hx509_get_error_string(context->hx509ctx, ret));
return ret;
}
/* Set a cc config entry indicating that the kx509 service is not available */
static void
store_kx509_disabled(krb5_context context, const char *realm, krb5_ccache cc)
{
krb5_data data;
if (!cc)
return;
data.data = (void *)(uintptr_t)realm;
data.length = strlen(realm);
krb5_cc_set_config(context, cc, NULL, "kx509_service_realm", &data);
data.data = "disabled";
data.length = strlen(data.data);
krb5_cc_set_config(context, cc, NULL, "kx509_service_status", &data);
}
/* Store the private key and certificate where requested */
static krb5_error_code
store(krb5_context context,
const char *hx509_store,
const char *realm,
krb5_ccache cc,
hx509_private_key key,
hx509_cert cert)
{
heim_octet_string hdata;
krb5_error_code ret = 0;
krb5_data data;
krb5_clear_error_message(context);
if (cc) {
/* Record the realm we used */
data.data = (void *)(uintptr_t)realm;
data.length = strlen(realm);
krb5_cc_set_config(context, cc, NULL, "kx509_service_realm", &data);
/* Serialize and store the certificate in the ccache */
ret = hx509_cert_binary(context->hx509ctx, cert, &hdata);
data.data = hdata.data;
data.length = hdata.length;
if (ret == 0)
ret = krb5_cc_set_config(context, cc, NULL, "kx509cert", &data);
free(hdata.data);
/*
* Serialized and store the key in the ccache. Use PKCS#8 so that we
* store the algorithm OID too, which is needed in order to be able to
* read the private key back.
*/
if (ret == 0)
ret = _hx509_private_key_export(context->hx509ctx, key,
HX509_KEY_FORMAT_PKCS8, &hdata);
data.data = hdata.data;
data.length = hdata.length;
if (ret == 0)
ret = krb5_cc_set_config(context, cc, NULL, "kx509key", &data);
free(hdata.data);
if (ret)
krb5_set_error_message(context, ret, "Could not store kx509 "
"private key and certificate in ccache %s",
krb5_cc_get_name(context, cc));
}
/* Store the private key and cert in an hx509 store */
if (hx509_store != NULL) {
hx509_certs certs;
_hx509_cert_assign_key(cert, key); /* store both in the same store */
ret = hx509_certs_init(context->hx509ctx, hx509_store,
HX509_CERTS_CREATE, NULL, &certs);
if (ret == 0)
ret = hx509_certs_add(context->hx509ctx, certs, cert);
if (ret == 0)
ret = hx509_certs_store(context->hx509ctx, certs, 0, NULL);
hx509_certs_free(&certs);
if (ret)
krb5_prepend_error_message(context, ret, "Could not store kx509 "
"private key and certificate in key "
"store %s", hx509_store);
}
/* Store the name of the hx509 store in the ccache too */
if (cc && hx509_store) {
data.data = (void *)(uintptr_t)hx509_store;
data.length = strlen(hx509_store);
(void) krb5_cc_set_config(context, cc, NULL, "kx509store", &data);
}
return ret;
}
static void
init_kx509_ctx(struct kx509_ctx_data *ctx)
{
memset(ctx, 0, sizeof(*ctx));
ctx->send_to_realm = NULL;
ctx->hmac_key = NULL;
ctx->keys = NULL;
ctx->priv_key = NULL;
}
static void
free_kx509_ctx(krb5_context context, struct kx509_ctx_data *ctx)
{
krb5_free_keyblock(context, ctx->hmac_key);
free(ctx->send_to_realm);
hx509_private_key_free(&ctx->priv_key);
if (ctx->keys)
_hx509_certs_keys_free(context->hx509ctx, ctx->keys);
init_kx509_ctx(ctx);
}
/*
* Make a request, which is a DER-encoded Kx509Request with version_2_0
* prefixed to it.
*
* If no private key is given, then a probe request will be made.
*/
static krb5_error_code
mk_kx509_req(krb5_context context,
struct kx509_ctx_data *kx509_ctx,
krb5_ccache incc,
const char *realm,
hx509_private_key private_key,
krb5_data *req)
{
unsigned char digest[SHA_DIGEST_LENGTH];
SubjectPublicKeyInfo spki;
struct Kx509Request kx509_req;
krb5_data pre_req;
krb5_auth_context ac = NULL;
krb5_error_code ret = 0;
krb5_creds this_cred;
krb5_creds *cred = NULL;
HMAC_CTX ctx;
const char *hostname;
size_t len;
krb5_data_zero(&pre_req);
memset(&spki, 0, sizeof(spki));
memset(&this_cred, 0, sizeof(this_cred));
memset(&kx509_req, 0, sizeof(kx509_req));
kx509_req.pk_hash.data = digest;
kx509_req.pk_hash.length = SHA_DIGEST_LENGTH;
if (private_key) {
/* Encode the public key for use in the request */
ret = hx509_private_key2SPKI(context->hx509ctx, private_key, &spki);
kx509_req.pk_key.data = spki.subjectPublicKey.data;
kx509_req.pk_key.length = spki.subjectPublicKey.length >> 3;
} else {
/* Probe */
kx509_req.pk_key.data = NULL;
kx509_req.pk_key.length = 0;
}
if (ret == 0)
ret = krb5_auth_con_init(context, &ac);
if (ret == 0)
ret = krb5_cc_get_principal(context, incc, &this_cred.client);
if (ret == 0) {
/*
* The kx509 protocol as deployed uses kca_service/kdc_hostname, but
* this is inconvenient in libkrb5: we want to be able to use the
* send_to_kdc machinery, and since the Heimdal KDC is also the kx509
* service, we want not to have to specify kx509 hosts separately from
* KDCs.
*
* We'd much rather use krbtgt/CLIENT_REALM@REQUESTED_REALM. What
* we do is assume all KDCs for `realm' support the kx509 service and
* then sendto the KDCs for that realm while using a hostbased service
* if still desired.
*
* Note that upstairs we try to get the start_realm cc config, so if
* realm wasn't given to krb5_kx509_ext(), then it should be set to
* that already unless there's no start_realm cc config, in which case
* we'll use the ccache's default client principal's realm.
*/
realm = realm ? realm : this_cred.client->realm;
hostname = krb5_config_get_string(context, NULL, "realm", realm,
"kx509_hostname", NULL);
if (hostname == NULL)
hostname = krb5_config_get_string(context, NULL, "libdefaults",
"kx509_hostname", NULL);
if (hostname) {
ret = krb5_sname_to_principal(context, hostname, "kca_service",
KRB5_NT_SRV_HST, &this_cred.server);
if (ret == 0)
ret = krb5_principal_set_realm(context, this_cred.server,
realm);
} else {
ret = krb5_make_principal(context, &this_cred.server, realm,
KRB5_TGS_NAME, this_cred.client->realm,
NULL);
}
}
/* Make the AP-REQ and extract the HMAC key */
if (ret == 0)
ret = krb5_get_credentials(context, 0, incc, &this_cred, &cred);
if (ret == 0)
ret = krb5_mk_req_extended(context, &ac, AP_OPTS_USE_SUBKEY, NULL, cred,
&kx509_req.authenticator);
krb5_free_keyblock(context, kx509_ctx->hmac_key);
kx509_ctx->hmac_key = NULL;
if (ret == 0)
ret = krb5_auth_con_getkey(context, ac, &kx509_ctx->hmac_key);
/* Save the realm to send to */
free(kx509_ctx->send_to_realm);
kx509_ctx->send_to_realm = NULL;
if (ret == 0 &&
(kx509_ctx->send_to_realm =
strdup(krb5_principal_get_realm(context, cred->server))) == NULL)
ret = krb5_enomem(context);
if (ret)
goto out;
/* Add the the key and HMAC to the message */
HMAC_CTX_init(&ctx);
HMAC_Init_ex(&ctx, kx509_ctx->hmac_key->keyvalue.data,
kx509_ctx->hmac_key->keyvalue.length, EVP_sha1(), NULL);
HMAC_Update(&ctx, version_2_0, sizeof(version_2_0));
if (private_key) {
HMAC_Update(&ctx, kx509_req.pk_key.data, kx509_req.pk_key.length);
} else {
/* Probe */
HMAC_Update(&ctx, kx509_req.authenticator.data, kx509_req.authenticator.length);
}
HMAC_Final(&ctx, kx509_req.pk_hash.data, 0);
HMAC_CTX_cleanup(&ctx);
/* Encode the message, prefix `version_2_0', output the result */
ASN1_MALLOC_ENCODE(Kx509Request, pre_req.data, pre_req.length, &kx509_req, &len, ret);
ret = krb5_data_alloc(req, pre_req.length + sizeof(version_2_0));
if (ret == 0) {
memcpy(req->data, version_2_0, sizeof(version_2_0));
memcpy(((unsigned char *)req->data) + sizeof(version_2_0),
pre_req.data, pre_req.length);
}
out:
free(pre_req.data);
krb5_free_creds(context, cred);
krb5_xfree(kx509_req.authenticator.data);
free_SubjectPublicKeyInfo(&spki);
krb5_free_cred_contents(context, &this_cred);
krb5_auth_con_free(context, ac);
if (ret == 0 && req->length != len + sizeof(version_2_0)) {
krb5_data_free(req);
krb5_set_error_message(context, ret = ERANGE,
"Could not make a kx509 request");
}
return ret;
}
/* Parse and validate a kx509 reply */
static krb5_error_code
rd_kx509_resp(krb5_context context,
struct kx509_ctx_data *kx509_ctx,
krb5_data *rep,
hx509_cert *cert)
{
unsigned char digest[SHA_DIGEST_LENGTH];
Kx509Response r;
krb5_error_code code = 0;
krb5_error_code ret = 0;
heim_string_t hestr;
heim_error_t herr = NULL;
const char *estr;
HMAC_CTX ctx;
size_t hdr_len = sizeof(version_2_0);
size_t len;
*cert = NULL;
/* Strip `version_2_0' prefix */
if (rep->length < hdr_len || memcmp(rep->data, version_2_0, hdr_len)) {
krb5_set_error_message(context, ENOTSUP,
"KDC does not support kx509 protocol");
return ENOTSUP; /* XXX */
}
/* Decode */
ret = decode_Kx509Response(((unsigned char *)rep->data) + 4,
rep->length - 4, &r, &len);
if (ret == 0 && len + hdr_len != rep->length)
ret = EINVAL; /* XXX */
if (ret) {
krb5_set_error_message(context, ret, "kx509 response is not valid");
return ret;
}
HMAC_CTX_init(&ctx);
HMAC_Init_ex(&ctx, kx509_ctx->hmac_key->keyvalue.data,
kx509_ctx->hmac_key->keyvalue.length, EVP_sha1(), NULL);
HMAC_Update(&ctx, version_2_0, sizeof(version_2_0));
{
int32_t t = r.error_code;
unsigned char encint[sizeof(t) + 1];
size_t k;
/*
* RFC6717 says this about how the error-code is included in the HMAC:
*
* o DER representation of the error-code exclusive of the tag and
* length, if it is present.
*
* So we use der_put_integer(), which encodes from the right.
*
* RFC6717 does not constrain the error-code's range. We assume it to
* be a 32-bit, signed integer, for which we'll need no more than 5
* bytes.
*/
ret = der_put_integer(&encint[sizeof(encint) - 1],
sizeof(encint), &t, &k);
if (ret == 0)
HMAC_Update(&ctx, &encint[sizeof(encint)] - k, k);
/* Normalize error code */
if (r.error_code == 0) {
code = 0; /* No error */
} else if (r.error_code < 0) {
code = KRB5KRB_ERR_GENERIC; /* ??? */
} else if (r.error_code <= KX509_ERR_SRV_OVERLOADED) {
/*
* RFC6717 (kx509) error code. These are actually not used on the
* wire in any existing implementations that we are aware of. Just
* in case, however, we'll map these.
*/
code = KX509_ERR_CLNT_FATAL + r.error_code;
} else if (r.error_code < kx509_krb5_error_base) {
/* Unknown error codes */
code = KRB5KRB_ERR_GENERIC;
} else {
/*
* Heimdal-specific enhancement to RFC6171: Kerberos wire protocol
* error codes.
*/
code = KRB5KDC_ERR_NONE + r.error_code - kx509_krb5_error_base;
if (code >= KRB5_ERR_RCSID)
code = KRB5KRB_ERR_GENERIC;
if (code == KRB5KDC_ERR_NONE)
code = 0;
}
}
if (r.certificate)
HMAC_Update(&ctx, r.certificate->data, r.certificate->length);
if (r.e_text)
HMAC_Update(&ctx, *r.e_text, strlen(*r.e_text));
HMAC_Final(&ctx, &digest, 0);
HMAC_CTX_cleanup(&ctx);
if (r.hash == NULL) {
/*
* No HMAC -> unauthenticated [error] response.
*
* Do not output any certificate.
*/
free_Kx509Response(&r);
return code;
}
/*
* WARNING: We do not validate that `r.certificate' is a DER-encoded
* Certificate, not here, and we don't use a different HMAC key
* for the response than for the request.
*
* If ever we start sending a Certificate as the Kx509Request
* pk-key field, then we'll have a reflection attack. As the
* Certificate we'd send in that case will be expired, the
* reflection attack would be just a DoS.
*/
if (r.hash->length != sizeof(digest) ||
ct_memcmp(r.hash->data, digest, sizeof(digest)) != 0) {
krb5_set_error_message(context, KRB5KDC_ERR_PREAUTH_FAILED,
"kx509 response MAC mismatch");
free_Kx509Response(&r);
return KRB5KRB_AP_ERR_BAD_INTEGRITY;
}
if (r.certificate == NULL) {
/* Authenticated response, either an error or probe success */
free_Kx509Response(&r);
if (code != KRB5KDC_ERR_POLICY && kx509_ctx->priv_key == NULL)
return 0; /* Probe success */
return code;
}
/* Import the certificate payload */
*cert = hx509_cert_init_data(context->hx509ctx, r.certificate->data,
r.certificate->length, &herr);
free_Kx509Response(&r);
if (cert) {
heim_release(herr);
return 0;
}
hestr = herr ? heim_error_copy_string(herr) : NULL;
estr = hestr ? heim_string_get_utf8(hestr) : "(no error message)";
krb5_set_error_message(context, ret, "Could not parse certificate "
"produced by kx509 KDC: %s (%ld)",
estr,
herr ? (long)heim_error_get_code(herr) : 0L);
heim_release(hestr);
heim_release(herr);
return HEIM_PKINIT_CERTIFICATE_INVALID; /* XXX */
}
/*
* Make a request, send it, get the response, parse it, and store the
* private key and certificate.
*/
static krb5_error_code
kx509_core(krb5_context context,
struct kx509_ctx_data *kx509_ctx,
krb5_ccache incc,
const char *realm,
const char *hx509_store,
krb5_ccache outcc)
{
krb5_error_code ret;
hx509_cert cert = NULL;
krb5_data req, resp;
krb5_data_zero(&req);
krb5_data_zero(&resp);
/* Make the kx509 request */
ret = mk_kx509_req(context, kx509_ctx, incc, realm, kx509_ctx->priv_key,
&req);
/* Send the kx509 request and get the response */
if (ret == 0)
ret = krb5_sendto_context(context, NULL, &req,
kx509_ctx->send_to_realm, &resp);
if (ret == 0)
ret = rd_kx509_resp(context, kx509_ctx, &resp, &cert);
/* Store the key and cert! */
if (ret == 0 && kx509_ctx->priv_key)
ret = store(context, hx509_store, kx509_ctx->send_to_realm, outcc,
kx509_ctx->priv_key, cert);
else if (ret == KRB5KDC_ERR_POLICY || ret == KRB5_KDC_UNREACH)
/* Probe failed -> Record that the realm does not support kx509 */
store_kx509_disabled(context, kx509_ctx->send_to_realm, outcc);
hx509_cert_free(cert);
krb5_data_free(&resp);
krb5_data_free(&req);
return ret;
}
/**
* Use the kx509 v2 protocol to get a certificate for the client principal.
*
* Given a private key this function will get a certificate. If no private key
* is given, one will be generated.
*
* The private key and certificate will be stored in the given hx509 store
* (e.g, "PEM-FILE:/path/to/file.pem") and/or given output ccache. When stored
* in a ccache, the DER-encoded Certificate will be stored as the data payload
* of a "cc config" named "kx509cert", while the key will be stored as a
* DER-encoded PKCS#8 PrivateKeyInfo in a cc config named "kx509key".
*
* @param context The Kerberos library context
* @param incc A credential cache
* @param realm A realm from which to get the certificate (uses the client
* principal's realm if NULL)
* @param use_priv_key_store An hx509 store containing a private key to certify
* (if NULL, a key will be generated)
* @param gen_type The public key algorithm for which to generate a private key
* @param gen_bits The size of the public key to generate, in bits
* @param hx509_store An hx509 store into which to store the private key and
* certificate (e.g, "PEM-FILE:/path/to/file.pem")
* @param outcc A ccache into which to store the private key and certificate
*
* @return A krb5 error code.
*/
KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
krb5_kx509_ext(krb5_context context,
krb5_ccache incc,
const char *realm,
const char *use_priv_key_store,
const char *gen_type,
int gen_bits,
const char *hx509_store,
krb5_ccache outcc)
{
struct kx509_ctx_data kx509_ctx;
krb5_error_code ret;
char *freeme = NULL;
/* TODO: Eventually switch to ECDSA, and eventually to ed25519 or ed448 */
if (gen_type == NULL) {
gen_type = krb5_config_get_string_default(context, NULL, "rsa",
"libdefaults",
"kx509_gen_key_type", NULL);
}
if (gen_bits == 0) {
/*
* The key size is really only for non-ECC, of which we'll only support
* RSA. For ECC key sizes will either be implied by the `key_type' or
* will have to be a magic value that allows us to pick from some small
* set of curves (e.g., 255 == Curve25519).
*/
gen_bits = krb5_config_get_int_default(context, NULL, 2048,
"libdefaults",
"kx509_gen_rsa_key_size", NULL);
}
init_kx509_ctx(&kx509_ctx);
if (realm == NULL) {
krb5_data data;
ret = krb5_cc_get_config(context, incc, NULL, "start_realm", &data);
if (ret == 0) {
if ((freeme = strndup(data.data, data.length)) == NULL)
return krb5_enomem(context);
realm = freeme;
}
}
if (use_priv_key_store) {
/* Get the given private key if it exists, and use it */
ret = load_priv_key(context, &kx509_ctx, use_priv_key_store);
if (ret == 0) {
ret = kx509_core(context, &kx509_ctx, incc, realm, hx509_store,
outcc);
free_kx509_ctx(context, &kx509_ctx);
free(freeme);
return ret;
}
if (ret != ENOENT) {
free_kx509_ctx(context, &kx509_ctx);
free(freeme);
return ret;
}
/* Key store doesn't exist or has no keys, fall through */
}
/*
* No private key given, so we generate one.
*
* However, before taking the hit for generating a keypair we probe to see
* if we're likely to succeeed.
*/
/* Probe == call kx509_core() w/o a private key */
ret = kx509_core(context, &kx509_ctx, incc, realm, NULL, outcc);
if (ret == 0)
ret = gen_priv_key(context, gen_type, gen_bits, &kx509_ctx.priv_key);
if (ret == 0)
ret = kx509_core(context, &kx509_ctx, incc, realm, hx509_store, outcc);
free_kx509_ctx(context, &kx509_ctx);
free(freeme);
return ret;
}
/**
* Generates a public key and uses the kx509 v2 protocol to get a certificate
* for that key and the client principal's subject name.
*
* The private key and certificate will be stored in the given ccache, and also
* in a corresponding hx509 store if one is configured via [libdefaults]
* kx509_store.
*
* XXX NOTE: Dicey feature here... Review carefully!
*
* @param context The Kerberos library context
* @param cc A credential cache
* @param realm A realm from which to get the certificate (uses the client
* principal's realm if NULL)
*
* @return A krb5 error code.
*/
KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
krb5_kx509(krb5_context context, krb5_ccache cc, const char *realm)
{
krb5_error_code ret = 0;
const char *defcc;
char *ccache_full_name = NULL;
char *store_exp = NULL;
/*
* The idea is that IF we are asked to do kx509 w/ creds from a default
* ccache THEN we should store the kx509 certificate (if we get one) and
* private key in the default hx509 store for kx509.
*
* Ideally we could have HTTP user-agents and/or TLS libraries look for
* client certificates and private keys in that default hx509 store.
*
* Of course, those user-agents / libraries should be configured to use
* those credentials with specific hostnames/domainnames, not the entire
* Internet, as the latter leaks the user's identity to the world.
*
* So we check if the full name for `cc' is the same as that of the default
* ccache name, and if so we get the [libdefaults] kx509_store string and
* expand it, then use it.
*/
if ((defcc = krb5_cc_configured_default_name(context)) &&
krb5_cc_get_full_name(context, cc, &ccache_full_name) == 0 &&
strcmp(defcc, ccache_full_name) == 0) {
/* Find an hx509 store */
const char *store = krb5_config_get_string(context, NULL,
"libdefaults",
"kx509_store", NULL);
if (store)
ret = _krb5_expand_path_tokens(context, store, 1, &store_exp);
}
/*
* If we did settle on a default hx509 store, we'll use it for reading the
* private key from (if it exists) as well as for storing the certificate
* (and private key) into, which may save us some key generation cycles.
*/
ret = krb5_kx509_ext(context, cc, realm, store_exp, NULL, 0,
store_exp, cc);
free(ccache_full_name);
free(store_exp);
return ret;
}

39
lib/krb5/kx509_err.et Normal file
View File

@@ -0,0 +1,39 @@
#
# Error messages for the krb5 library
#
# This might look like a com_err file, but is not
#
# RFC6171 says:
#
# +------------+-----------------------------+------------------------+
# | error-code | Condition | Example |
# +------------+-----------------------------+------------------------+
# | 1 | Permanent problem with | Incompatible version |
# | | client request | |
# | 2 | Solvable problem with | Expired Kerberos |
# | | client request | credentials |
# | 3 | Temporary problem with | Packet loss |
# | | client request | |
# | 4 | Permanent problem with the | Internal |
# | | server | misconfiguration |
# | 5 | Temporary problem with the | Server overloaded |
# | | server | |
# +------------+-----------------------------+------------------------+
#
# Error 3 makes no sense on the wire, and in the library it only makes sense as
# a timeout, so we'll name it KX509_ERR_TIMEOUT.
# Error table names must be no more than four characters...
error_table kx59
prefix KX509_ERR
error_code NONE, "Kx509 success"
error_code CLNT_FATAL, "Kx509 request error, possibly unsupported version"
error_code CLNT_SOLVABLE, "Kx509 request error such as expired credentials"
error_code TIMEOUT, "Kx509 request timed out"
error_code SRV_FATAL, "Permanent server problem"
error_code SRV_OVERLOADED, "Kx509 server is overloaded"
end

View File

@@ -438,6 +438,8 @@ EXPORTS
krb5_kt_resolve
krb5_kt_start_seq_get
krb5_kuserok
krb5_kx509
krb5_kx509_ext
krb5_log
krb5_log_msg
krb5_make_addrport

View File

@@ -431,6 +431,8 @@ HEIMDAL_KRB5_2.0 {
krb5_kt_resolve;
krb5_kt_start_seq_get;
krb5_kuserok;
krb5_kx509;
krb5_kx509_ext;
krb5_log;
krb5_log_msg;
krb5_make_addrport;