#include <libtasn1.h>
#include <krb5.h>
+/* Plus, from k5-int.h: */
+krb5_error_code KRB5_CALLCONV krb5_decrypt_tkt_part(krb5_context,
+ const krb5_keyblock *,
+ krb5_ticket * );
+
#include <quick-der/api.h>
#include <quick-der/rfc4120.h>
typedef DER_OVLY_rfc4120_Ticket ticket_t;
typedef DER_OVLY_rfc4120_Authenticator authenticator_t;
+typedef DER_OVLY_rfc4120_EncryptedData encrypted_data_t;
#include <tlspool/internal.h>
static krb5_context krbctx_cli, krbctx_srv;
static krb5_keytab krb_kt_cli, krb_kt_srv;
static bool got_cc_cli, got_cc_srv;
-static int have_key_tgt (
+static int have_key_tgt_cc (
struct command *cmd, // in, session context
krb5_context ctx, // in, kerberos context
bool use_cc, // in, whether to use cc
char *p11uri, // in/opt, PKCS #11 pwd URI
krb5_keytab kt, // in/opt, keytab
krb5_keyblock *key, // opt/opt session key
- krb5_creds *tgt); // out/opt, tkt granting tkt
+ krb5_creds **tgt, // out/opt, tkt granting tkt
+ krb5_ccache *cc); // out/opt, cred cache
static int have_service_ticket (
struct command *cmd, // in, session context
krb5_context ctx, // in, kerberos context
krb5_ccache cc_opt, // in/opt, credcache
krb5_principal cli, // in, client principal
- krb5_creds *ticket); // out/opt, tkt granting tkt
+ krb5_creds **ticket);// out/opt, tkt granting tkt
#endif
setup_starttls_credentials ());
//
// Parse the default priority string
- E_g2e ("Failed to setup NORMAL priority cache",
- gnutls_priority_init (&priority_normal, "NONE:+VERS-TLS-ALL:+VERS-DTLS-ALL:+COMP-NULL:"
#ifdef HAVE_TLS_KDH
- "%%ASYM_CERT_TYPES:"
+ E_g2e ("Failed to setup NORMAL priority cache",
+ gnutls_priority_init (&priority_normal,
+ "NONE:"
+ "%ASYM_CERT_TYPES:"
+ "+VERS-TLS-ALL:+VERS-DTLS-ALL:"
+ "+COMP-NULL:"
+ "+CIPHER-ALL:+CURVE-ALL:+SIGN-ALL:+MAC-ALL:"
+ "+ANON-ECDH:"
+ "+ECDHE-KRB:" // +ECDHE-KRB-RSA:+ECDHE-KRB-ECDSA:"
+ "+ECDHE-RSA:+DHE-RSA:+ECDHE-ECDSA:+DHE-DSS:+RSA:"
+ "+CTYPE-SRV-KRB:+CTYPE-SRV-X.509:+CTYPE-SRV-OPENPGP:"
+ "+CTYPE-CLI-KRB:+CTYPE-CLI-X.509:+CTYPE-CLI-OPENPGP:"
+ "+SRP:+SRP-RSA:+SRP-DSS",
+ NULL));
+#else
+ E_g2e ("Failed to setup NORMAL priority cache",
+ gnutls_priority_init (&priority_normal,
+ "NONE:"
+ "+VERS-TLS-ALL:+VERS-DTLS-ALL:"
+ "+COMP-NULL:+CIPHER-ALL:+CURVE-ALL:+SIGN-ALL:+MAC-ALL:"
+ "+ANON-ECDH:"
+ "+ECDHE-RSA:+DHE-RSA:+ECDHE-ECDSA:+DHE-DSS:+RSA:"
+ "+CTYPE-X.509:+CTYPE-OPENPGP:"
+ "+SRP:+SRP-RSA:+SRP-DSS",
+ NULL));
#endif
- "+CIPHER-ALL:+CURVE-ALL:+SIGN-ALL:+MAC-ALL:+ANON-ECDH:+ECDHE-RSA:+DHE-RSA:+ECDHE-ECDSA:+DHE-DSS:+RSA:+CTYPE-X.509:+CTYPE-OPENPGP:+SRP:+SRP-RSA:+SRP-DSS", NULL));
- // gnutls_priority_init (&priority_normal, "NORMAL:-RSA:+ANON-ECDH:+RSA:+CTYPE-X.509:+CTYPE-OPENPGP:+SRP:+SRP-RSA:+SRP-DSS", NULL));
//
// Try to setup on-the-fly signing key / certificate and gen a certkey
otfsigcrt = cfg_tls_onthefly_signcert ();
}
-/* The callback function that retrieves a TLS-KDH "signature", which is kept
- * outside of GnuTLS. The callback computes an authenticator encrypted to
- * the session's Kerberos key.
- */
-#ifdef HAVE_TLS_KDH
-gtls_error cli_kdhsig_encode (gnutls_session_t session,
- gnutls_datum_t *enc_authenticator,
- gnutls_datum_t *dec_authenticator,
- const gnutls_datum_t *hash,
- int32_t checksum_type) {
- gnutls_certificate_type_t peercert;
- "...SETUP SECURE HASH IN AUTHENTICATOR...";
- peercert = gnutls_certificate_type_get_peers (session);
- if (peercert == GNUTLS_CRT_KRB) {
- // This is KDH-Only, and so we MUST create a new key
- "...GENERATE RANDOM...AND SETUP AS SUBKEY...";
- }
- "...SETUP MICROSECOND TIMER...";
- "...SETUP KRB VERSION NUMBER...";
- "...SETUP CLIENT REALM AND PRINCIPALNAME...";
- return GNUTLS_E_UNSUPPORTED_SIGNATURE_ALGORITHM;
-}
-#endif
-
-
-/* The callback function that verifies a TLS-KDH "signature", which is kept
- * outside of GnuTLS. The callback verifies the authenticator against the
- * provided session hash and returns the decrypted authenticator.
- */
-#ifdef HAVE_TLS_KDH
-int srv_kdhsig_decode (gnutls_session_t session,
- const gnutls_datum_t *enc_authenticator,
- gnutls_datum_t *dec_authenticator,
- gnutls_datum_t *hash,
- int32_t *checksum_type) {
- return GNUTLS_E_UNSUPPORTED_SIGNATURE_ALGORITHM;
-}
-#endif
-
-
/* The callback function that retrieves certification information from either
* the client or the server in the course of the handshake procedure.
*/
//
// Setup a number of common references and structures
+ errno = 0;
*pcert = NULL;
cmd = (struct command *) gnutls_session_get_ptr (session);
if (cmd == NULL) {
#endif
} else {
// GNUTLS_CRT_RAW, GNUTLS_CRT_UNKNOWN, or other
- tlog (TLOG_TLS, LOG_ERR, "Funny sort of certificate retrieval attempted as a %s", rolestr);
+ tlog (TLOG_TLS, LOG_ERR, "Funny sort of certificate %d retrieval attempted as a %s", certtp, rolestr);
E_g2e ("Requested certtype is neither X.509 nor OpenPGP",
GNUTLS_E_CERTIFICATE_ERROR);
return gtls_errno;
//
// First, try to obtain a TGT and key, in various ways
krb5_keyblock key;
- krb5_creds tgt;
- krb5_creds tkt;
- int status;
- memset (&key, 0, sizeof (key));
- memset (&tgt, 0, sizeof (tgt));
- memset (&tkt, 0, sizeof (tkt));
- status = have_key_tgt (
+ krb5_creds *tgt = NULL;
+ krb5_creds *ticket = NULL;
+ krb5_ccache cc = NULL;
+ int status = 0;
+ memset (&key, 0, sizeof (key ));
+ status = have_key_tgt_cc (
cmd, krbctx_cli,
1, 0, 0,
p11priv,
krb_kt_cli,
- &key, &tgt);
+ &key, &tgt, &cc);
if (status >= 1) {
// We never use this key ourselves
krb5_free_keyblock_contents (krbctx_cli, &key);
break;
}
//
- // Now find a service ticket to talk to
+ // Store client identity in session object
+ if (0 != krb5_copy_principal (
+ krbctx_cli,
+ tgt->client,
+ &cmd->krbid_cli)) {
+ krb5_free_creds (krbctx_cli, tgt);
+ tgt = NULL;
+ if (cc != NULL) {
+ krb5_cc_close (krbctx_cli, cc);
+ cc = NULL;
+ }
+ gtls_errno = GNUTLS_E_NO_CERTIFICATE_FOUND;
+ break;
+ }
+ //
+ // Now find a service ticket to talk to, and its key
//TODO// Pass credcache instead?
status = have_service_ticket (
cmd, krbctx_cli,
- NULL, //TODO// Should get cc from have_key_tgt()
- tgt.client,
- &tkt);
- krb5_free_cred_contents (krbctx_cli, &tgt);
+ cc,
+ cmd->krbid_cli,
+ &ticket);
+ if (cc != NULL) {
+ // We don't need cc anymore below
+ krb5_cc_close (krbctx_cli, cc);
+ }
if (status < 1) {
// Stop processing when no ticket was found
+ krb5_free_creds (krbctx_cli, tgt);
+ tgt = NULL;
+ gtls_errno = GNUTLS_E_NO_CERTIFICATE_FOUND;
+ break;
+ }
+ //
+ // Only for KDH-Only mode can the client rely on a
+ // server principal taken from the ticket;
+ // So only store krbid_srv for KDH-Only mode.
+ if ((gnutls_certificate_type_get_peers (cmd->session)
+ == GNUTLS_CRT_KRB) &&
+ (0 != krb5_copy_principal (
+ krbctx_cli,
+ tgt->server,
+ &cmd->krbid_srv))) {
+ krb5_free_creds (krbctx_cli, ticket);
gtls_errno = GNUTLS_E_NO_CERTIFICATE_FOUND;
break;
}
- certdatum.data = tkt.ticket.data;
- certdatum.size = tkt.ticket.length;
+ krb5_free_creds (krbctx_cli, tgt);
+ tgt = NULL;
+ if (0 != krb5_copy_keyblock_contents (
+ krbctx_cli,
+ &ticket->keyblock,
+ &cmd->krb_key)) {
+ gtls_errno = GNUTLS_E_NO_CERTIFICATE_FOUND;
+ // continue, with E_g2e() skipping import
+ }
+ certdatum.data = ticket->ticket.data;
+ certdatum.size = ticket->ticket.length;
E_g2e ("MOVED: Failed to import Kerberos ticket",
gnutls_pcert_import_krb_raw (
*pcert,
&certdatum,
0));
- krb5_free_cred_contents (krbctx_cli, &tkt);
+ krb5_free_creds (krbctx_cli, ticket);
} else {
//
// For KDH-Only, the server supplies one of:
- // - an empty ticket (0 bytes long)
- // - a TGT for user-to-user mode (where considered useful)
+ // - a TGT for user-to-user mode (for p2p exchanges)
+ // - an DER NULL to waive u2u mode
//TODO// E_g2e ("MOVED: Failed to import Kerberos ticket",
//TODO// gnutls_pcert_import_krb_raw (
//TODO// *pcert,
//TODO// &certdatum, //TODO:WHATSFOUND//
//TODO// 0));
int u2u = 0;
- krb5_creds tgt;
- int status;
+ int status = 0;
+ krb5_creds *tgt = NULL;
//
// Determine whether we want to run in user-to-user mode
// for which we should supply a TGT to the TLS client
// u2u = u2u || "shaken hands on TLS symmetry extension"
u2u = u2u && got_cc_srv; // We may simply not be able!
//
- // When not in user-to-user mode, deliver 0 bytes
+ // When not in user-to-user mode, deliver DER NULL
if (!u2u) {
- certdatum.data = "";
- certdatum.size = 0;
+ certdatum.data = "\x05\x00";
+ certdatum.size = 2;
E_g2e ("Failed to withhold Kerberos server ticket",
gnutls_pcert_import_krb_raw (
*pcert,
}
//
// Continue specifically for user-to-user mode.
+ //TODO// Setup server principal identity
//
// Fetch the service's key
- memset (&tgt, 0, sizeof (tgt));
- status = have_key_tgt (
+ status = have_key_tgt_cc (
cmd, krbctx_srv,
1, 0, 0, // Hmm... later we know kvno/etype
p11priv,
krb_kt_srv,
- &cmd->krb_key, &tgt);
+ &cmd->krb_key, &tgt, NULL);
if (status == 1) {
// There's no use in having just the key
krb5_free_keyblock_contents (krbctx_srv, &cmd->krb_key);
+ memset (&cmd->krb_key, 0, sizeof (cmd->krb_key));
}
if (status < 2) {
gtls_errno = GNUTLS_E_NO_CERTIFICATE_FOUND;
- break;
+ } else if (0 != krb5_copy_principal (
+ krbctx_srv,
+ tgt->server,
+ &cmd->krbid_srv)) {
+ gtls_errno = GNUTLS_E_NO_CERTIFICATE_FOUND;
}
- certdatum.data = tgt.ticket.data;
- certdatum.size = tgt.ticket.length;
+ certdatum.data = tgt->ticket.data;
+ certdatum.size = tgt->ticket.length;
E_g2e ("Failed to withhold Kerberos server ticket",
gnutls_pcert_import_krb_raw (
*pcert,
&certdatum,
0));
- krb5_free_cred_contents (krbctx_srv, &tgt);
+ krb5_free_creds (krbctx_cli, tgt);
+ tgt = NULL;
}
break;
#endif
//TODO// Moved out (end)
+#ifdef ANCIENT_CODE_WHEN_DBERRNO_RAN_IN_PARALLEL
//
// Lap up any overseen POSIX error codes in errno
if (errno) {
cmd->session_errno = errno;
gtls_errno = GNUTLS_E_NO_CIPHER_SUITES; /* Vaguely matching */
}
+#endif
//
// Return the overral error code, hopefully GNUTLS_E_SUCCESS
NULL, /* use master key */
0));
break;
+ case LID_TYPE_KRB5:
+ /* Binary information is currently moot, so do not load it */
+ break;
default:
/* Should not happen */
break;
}
+
+/********** KERBEROS SUPPORT FUNCTIONS FOR TLS-KDH **********/
+
+
+
/* Prepare the Kerberos resources for use by clients and/or servers.
*/
#ifdef HAVE_TLS_KDH
k5err = krb5_kt_resolve (krbctx_srv, cfg, &krb_kt_srv);
}
cfg = cfg_krb_client_credcache ();
+#if 0 /* Temporary bypass of cctype checks */
if ((k5err == 0) && (cfg != NULL)) {
k5err = krb5_cc_set_default_name (krbctx_cli, cfg);
if (k5err == 0) {
krb5_cc_close (krbctx_cli, krb_cc_tmp);
}
}
+#endif
cfg = cfg_krb_server_credcache ();
+#if 0 /* Temporary bypass of cctype checks */
if ((k5err == 0) && (cfg != NULL)) {
k5err = krb5_cc_set_default_name (krbctx_srv, cfg);
if (k5err == 0) {
krb5_cc_close (krbctx_srv, krb_cc_tmp);
}
}
+#endif
//
// Check for consistency and log helpful messages for the sysop
if (k5err != 0) {
tlog (TLOG_DAEMON | TLOG_KERBEROS, LOG_ERR, "No kerberos_server_keytab configured, so Kerberos cannot work at all");
retval = GNUTLS_E_UNWANTED_ALGORITHM;
/* TODO: Only for MIT krb5 1.11 and up
- } else if (0 == krb5_kt_have_content (krb_ctx, krb_kt_cli)) {
- tlog (TLOG_DAEMON | TLOG_KERBEROS, LOG_ERR, "Keytab in kerberos_client_keytab is absent or empty");
+ } else if (0 == krb5_kt_have_content (krb_ctx, krb_kt_srv)) {
+ tlog (TLOG_DAEMON | TLOG_KERBEROS, LOG_ERR, "Keytab in kerberos_server_keytab is absent or empty");
retval = GNUTLS_E_UNWANTED_ALGORITHM;
*/
}
if (krbctx_cli == NULL) {
tlog (TLOG_DAEMON | TLOG_KERBEROS, LOG_ERR, "No kerberos_client_credcache configured, so Kerberos cannot work at all");
retval = GNUTLS_E_UNWANTED_ALGORITHM;
+#if 0 /* Temporary bypass of cctype checks */
} else if (!krb5_cc_support_switch (
krbctx_cli, cctype_cli)) {
tlog (TLOG_DAEMON | TLOG_KERBEROS, LOG_ERR, "Your kerberos_client_credcache does not support multilpe identities");
retval = GNUTLS_E_UNWANTED_ALGORITHM;
+#endif
}
if (krbctx_srv == NULL) {
tlog (TLOG_DAEMON | TLOG_KERBEROS, LOG_WARNING, "No kerberos_server_credcache configured, so user-to-user Kerberos will not work");
+#if 0 /* Temporary bypass of cctype checks */
} else if (!krb5_cc_support_switch (
krbctx_srv, cctype_srv)) {
tlog (TLOG_DAEMON | TLOG_KERBEROS, LOG_ERR, "Your kerberos_server_credcache does not support multilpe identities");
retval = GNUTLS_E_UNWANTED_ALGORITHM;
+#endif
}
if (retval != GNUTLS_E_SUCCESS) {
cleanup_starttls_kerberos ();
}
#endif
+
/* Cleanup Kerberos resources. This must be an idempotent function, because
* it is called when Kerberos panics as well as when
*/
struct command *cmd = (struct command *) vcmd;
int i;
krb5_prompt_type *codes = krb5_get_prompt_types (ctx);
- char pin [129];
int attempt = 0;
static const char *token_url = "pkcs11:manufacturer=Kerberos+infrastructure;model=TLS+Pool;serial=%28none%29";
static const char *token_label = "Kerberos infrastructure";
//TODO// Do we need to cycle passwords to cover retry?
//TODO// Delete any failed passwords?
//TODO:FIXED//
- memset (pin, 0, sizeof (pin));
if (attempt >= MAX_P11ITER_ATTEMPTS) {
return KRB5_LIBOS_CANTREADPWD;
}
// Nothing in PKCS #11 --> so fallback on manual entry
if (!pin_callback (attempt,
token_url, "Enter Kerberos password:",
- pin, sizeof (pin)-1)) {
- memset (pin, 0, sizeof (pin));
+ prompts [i].reply->data,
+ prompts [i].reply->length)) {
+ memset (prompts [i].reply->data, 0, prompts [i].reply->length);
return KRB5_LIBOS_CANTREADPWD;
}
//TODO// Manage data structure
- prompts [i].reply->data = strdup (pin);
- prompts [i].reply->length = strlen (pin);
+ prompts [i].reply->length = strlen (prompts [i].reply->data);
return 0;
case KRB5_PROMPT_TYPE_NEW_PASSWORD:
case KRB5_PROMPT_TYPE_NEW_PASSWORD_AGAIN:
* so 0 means error, 1 means key only and 2 means key and tgt.
*/
#ifdef HAVE_TLS_KDH
-static int have_key_tgt (struct command *cmd, // in, session context
+static int have_key_tgt_cc (struct command *cmd, // in, session context
krb5_context ctx, // in, kerberos context
bool use_cc, // in, whether to use cc
krb5_kvno kvno, // in, kvno (0 for highest)
char *p11uri, // in/opt, PKCS #11 pwd URI
krb5_keytab kt, // in/opt, keytab
krb5_keyblock *key, // opt/opt session key
- krb5_creds *tgt) { // out/opt, tkt granting tkt
+ krb5_creds **tgt, // out/opt, tkt granting tkt
+ krb5_ccache *cc) { // out/opt, cred cache
int k5err = 0;
- krb5_ccache cc = NULL;
+ krb5_ccache newcc = NULL;
krb5_principal sought = NULL;
+ krb5_principal sought1 = NULL;
krb5_principal tgtname = NULL;
krb5_keytab_entry ktentry;
const char *svc = cmd->cmd.pio_data.pioc_starttls.service;
time_t now = 0;
//
// Assertions, and initialise variables
- assert (cmd != NULL);
- assert (ctx != NULL);
- assert (key != NULL);
- assert (tgt != NULL);
+ assert ( cmd != NULL);
+ assert ( ctx != NULL);
+ assert ( key != NULL);
+ assert (*tgt == NULL);
krb5_free_keyblock_contents (ctx, key);
- krb5_free_cred_contents (ctx, tgt);
+ if (cc != NULL) {
+ *cc = NULL;
+ }
//
// Construct the realm name
liddom = strrchr (lid, '@');
lid1len = strnlen (lid, 128);
}
k5err = krb5_get_host_realm (ctx, liddom, &realms);
- if ((k5err == 0) && (realms [0] != NULL) && (**realms != '\0')) {
+ if ((k5err == 0) && (realms [0] != NULL) && (*realms [0] != '\0')) {
strncpy (realm, realms [0], sizeof (realm));
realm [sizeof (realm)-1] = '\0';
} else {
strlen (realm), realm,
strnlen (lid, 128), lid,
0);
+ break;
case KRB5_NT_SRV_HST:
+ if (strcmp (svc, "http") == 0) {
+ svc = "HTTP";
+ }
k5err = krb5_build_principal_ext (ctx, &sought,
strlen (realm), realm,
strlen (svc), svc,
} else {
sought = NULL;
}
- k5err = krb5_cc_cache_match (ctx, sought, &cc);
- if (sought != NULL) {
- krb5_free_principal (ctx, sought);
- sought = NULL;
- }
+ k5err = krb5_cc_cache_match (ctx, sought, &newcc);
if (k5err != 0) {
- if (nametype_alt != nametype) {
+ if ((nametype_alt != nametype) && (sought1 == NULL)) {
nametype = nametype_alt;
+ sought1 = sought;
+ sought = NULL;
goto retry;
}
- // We utterly failed
- goto cleanup;
+ //
+ // We failed to find an *existing* credentials cache
+ // for the local identity.
+ //
+ // Our new hope is to create a fresh credential, and add
+ // it to the current credcache. To that end, we now try
+ // to overrule k5err by getting hold of our default cc.
+ goto from_scratch;
}
//
// Construct the TGT name
- k5err = krb5_build_principal (ctx, &tgtname,
+ k5err = krb5_build_principal_ext (ctx, &tgtname,
strlen (realm), realm,
6, "krbtgt",
strlen (realm), realm,
//
// Try to get the service ticket for the TGT name from the cache
krb5_creds credreq;
+ memset (&credreq, 0, sizeof (credreq));
+ credreq.client = sought;
+ credreq.server = tgtname;
k5err = krb5_get_credentials (ctx,
/* KRB5_GC_USER_USER ?|? */
( use_cc ? 0 : KRB5_GC_CACHED ),
- cc,
- &credreq, &tgt);
+ newcc,
+ &credreq, tgt);
time (&now);
if ((k5err == 0)
- && (now + 300 > tgt->times.endtime)
- && (now + 300 < tgt->times.renew_till)) {
- krb5_free_creds (ctx, tgt);
+ && (now + 300 > (*tgt)->times.endtime)
+ && (now + 300 < (*tgt)->times.renew_till)) {
+ //TODO:NOTHERE// krb5_free_creds (ctx, *tgt);
+ //TODO:NOTHERE// *tgt = NULL;
// Try to renew the ticket
k5err = krb5_get_renewed_creds (ctx,
- tgt,
+ *tgt,
sought,
- cc,
+ newcc,
NULL); /* krbtgt/REALM@REALM */
}
if ((k5err == 0)
- && (now + 300 > tgt->times.endtime)) {
+ && (now + 300 > (*tgt)->times.endtime)) {
// Thanks, but no thanks!
- krb5_free_creds (ctx, tgt);
+ krb5_free_creds (ctx, *tgt);
+ *tgt = NULL;
k5err = 666;
}
if (k5err == 0) {
// First case worked -- return <key,tgt> from credout
k5err = krb5_copy_keyblock_contents (ctx,
- &tgt->keyblock,
+ &(*tgt)->keyblock,
key);
// On failure, key shows failure
+ if (cc != NULL) {
+ *cc = newcc;
+ newcc = NULL;
+ }
goto cleanup;
}
+from_scratch:
//
- // Prior attempts failed. Instead, look for keytab presence.
+ // Prior attempts failed. Instead, look for keytab or p11uri presence.
// This is skipped when the use_cc option below welcomes krb5_creds.
- if ((key->contents == NULL) && (p11uri == NULL)) {
+ if ((key->contents == NULL) && (p11uri == NULL) && (kt == NULL)) {
// We cannot obtain a new krbtgt
// We simply return what we've got (which may be nothing)
goto cleanup;
}
- if (!use_cc) {
+ if ((kt == NULL) && (!use_cc)) {
// We have nowhere to store a new krbtgt if we got it
// We simply return what we've got (which is at least a key)
goto cleanup;
// Either we have a keytab key, or we have a p11uri,
// so we can attempt to create a new credcache with a new krbtgt
if (use_cc) {
- if (cc == NULL) {
- k5err = krb5_cc_default (ctx, &cc);
+ if (newcc == NULL) {
+ k5err = krb5_cc_default (ctx, &newcc);
if (k5err != 0) {
// Utter failure to do even the simplest thing
goto cleanup;
}
}
- if (p11uri == NULL) {
- k5err = krb5_get_init_creds_keytab (
- ctx,
- tgt,
- sought, //TODO:KEEP
- kt,
- 0, /* start now please */
- NULL, /* get a TGT please */
- NULL); //TODO// opts needed?
- } else {
- //TODO// Prepare PKCS #11 access
- k5err = krb5_get_init_creds_password (
- ctx,
- tgt,
- sought, //TODO:KEEP
- NULL, // Use callbacks for password
- clisrv_p11krb_callback,
- cmd, /* callback data pointer */
- 0, /* start now please */
- NULL, /* get a TGT please */
- NULL); //TODO// opts needed?
- //TODO// End PKCS #11 access
+ *tgt = malloc (sizeof (**tgt));
+ if (*tgt == NULL) {
+ // Memory error
+ goto cleanup;
}
- if (k5err == 0) {
- krb5_copy_keyblock_contents (ctx,
- &ktentry.key,
- key);
- // Failure will show up in key
+ memset (*tgt, 0, sizeof (**tgt));
+ if ((sought != NULL) && (sought1 == NULL)) {
+ // We only tried one name
+ sought1 = sought;
+ sought = NULL;
}
+ do {
+ if (sought1 == NULL) {
+ break;
+ }
+ if (p11uri == NULL) {
+ k5err = krb5_get_init_creds_keytab (
+ ctx,
+ *tgt,
+ sought1,
+ kt,
+ 0, /* start now please */
+ NULL, /* get a TGT please */
+ NULL); //TODO// opts needed?
+ } else {
+ //TODO// Prepare PKCS #11 access
+ k5err = krb5_get_init_creds_password (
+ ctx,
+ *tgt,
+ sought1,
+#ifdef TOM_IS_WEG
+ NULL, // Use callbacks for password
+ clisrv_p11krb_callback,
+#else
+ "1234",
+ NULL,
+#endif
+ cmd, /* callback data pointer */
+ 0, /* start now please */
+ NULL, /* get a TGT please */
+ NULL); //TODO// opts needed?
+ //TODO// End PKCS #11 access
+ }
+ krb5_free_principal (ctx, sought1);
+ sought1 = sought;
+ sought = NULL;
+ } while (k5err != 0);
if (k5err != 0) {
// Failed to initiate new credentials
+ krb5_free_creds (ctx, *tgt);
+ *tgt = NULL;
goto cleanup;
}
+ // Try to store the credential, if it was found
+ if (sought1 != NULL) {
+ k5err = krb5_cc_initialize (ctx, newcc, sought1);
+ if (k5err == 0) {
+ k5err = krb5_cc_store_cred (ctx, newcc, *tgt);
+ }
+ }
+ // Copy the keyblock; any failure will show up in key
+ krb5_copy_keyblock_contents (ctx,
+ &(*tgt)->keyblock, //TODO:UNINIT// &ktentry.key,
+ key);
//
// We succeeded in setting up a new Ticket Granting Ticket!
+ if (cc != NULL) {
+ *cc = newcc;
+ newcc = NULL;
+ }
goto cleanup;
}
//
key);
krb5_free_keytab_entry_contents (ctx, &ktentry);
// On failure, key shows failure.
+ if (cc != NULL) {
+ *cc = newcc;
+ newcc = NULL;
+ }
goto cleanup;
}
}
cleanup:
//
// Cleanup and return the <key,tgt> values as they were delivered
+ if (sought1 != NULL) {
+ krb5_free_principal (ctx, sought1);
+ sought1 = NULL;
+ }
+ if (sought != NULL) {
+ krb5_free_principal (ctx, sought);
+ sought = NULL;
+ }
if (tgtname != NULL) {
krb5_free_principal (ctx, tgtname);
tgtname = NULL;
}
- if (cc != NULL) {
- krb5_cc_close (ctx, cc);
+ if (newcc != NULL) {
+ krb5_cc_close (ctx, newcc);
+ newcc = NULL;
}
if (key->contents == NULL) {
+ if (k5err != 0) {
+ const char *errmsg = krb5_get_error_message (ctx, k5err);
+ tlog (TLOG_DAEMON, LOG_ERR, "Kerberos error in have_key_tgt_cc: %s", errmsg);
+ krb5_free_error_message (ctx, errmsg);
+ }
+ if (*tgt != NULL) {
+ krb5_free_creds (ctx, *tgt);
+ *tgt = NULL;
+ }
+ if ((cc != NULL) && (*cc != NULL)) {
+ krb5_cc_close (ctx, *cc);
+ *cc = NULL;
+ }
return 0;
- } else if (tgt->ticket.data == NULL) {
+ } else if (tgt == NULL) {
+ if ((cc != NULL) && (*cc != NULL)) {
+ krb5_cc_close (ctx, *cc);
+ *cc = NULL;
+ }
return 1;
- } else {
+ } else if ((cc == NULL) || (*cc == NULL)) {
return 2;
+ } else {
+ return 3;
}
}
#endif
* missing backend services; this is not something the client should decide
* on, and certainly not after being requested by the service. The error
* and recovery could be implemented here (if we can get the error info out
- * of the libkrb5 API).
+ * of the libkrb5 API). Alternatively, we might consider passing the
+ * authorization data in the authenticator since we get to control it.
+ * What will the specification say?
*
* The return value indicates how many of the requested output values have
* been provided, counting from the first. So, 0 means a total failure and
krb5_context ctx, // in, kerberos context
krb5_ccache cc_opt, // in/opt, credcache
krb5_principal cli, // in, client principal
- krb5_creds *ticket) { // out, tkt granting tkt
+ krb5_creds **ticket) {// out, tkt granting tkt
int k5err = 0;
krb5_ccache cc = cc_opt;
krb5_flags u2u = 0;
// Sanity checks and initialisation
memset (&tkt_srv, 0, sizeof (tkt_srv));
memset (&credreq, 0, sizeof (credreq));
+ *ticket = NULL;
//
// Determine the optional cc parameter if it was not provided
- //TODO// This can go if we always get it passed from have_key_tgt()
+ //TODO// This can go if we always get it passed from have_key_tgt_cc()
if (cc == NULL) {
k5err = krb5_cc_cache_match (ctx, cli, &cc);
if (k5err != 0) {
riddom = rid; // localid is a host
}
k5err = krb5_get_host_realm (ctx, riddom, &realms);
- if ((k5err == 0) && (realms [0] != NULL) && (**realms != '\0')) {
+ if ((k5err == 0) && (realms [0] != NULL) && (*realms [0] != '\0')) {
strncpy (realm, realms [0], sizeof (realm));
realm [sizeof (realm)-1] = '\0';
} else {
} else {
k5err = 0;
}
+ if (strcmp (svc, "http") == 0) {
+ svc = "HTTP";
+ }
k5err = krb5_build_principal_ext (ctx, &srv,
strlen (realm), realm,
strlen (svc), svc,
// - we are sure of GNUTLS_CRD_CERTIFICATE because we implement it now
// - we must ensure that this is KDH-Only (remote GNUTLS_CRT_KRB)
// - we must ensure that the remote provided a non-empty ticket
- if (gnutls_certificate_type_get (cmd->session) == GNUTLS_CRT_KRB) {
- // This is KDH-Only
- const gnutls_datum_t *peer_tkt;
- unsigned int peer_tkt_count;
- peer_tkt = gnutls_certificate_get_peers (cmd->session, &peer_tkt_count);
- if ((peer_tkt != NULL) && (peer_tkt_count >= 1) && (peer_tkt [0].size > 5)) {
+ if (gnutls_certificate_type_get_peers (cmd->session) == GNUTLS_CRT_KRB) {
+ // This is KDH-Only -- and the server may present a TGT
+ const gnutls_datum_t *opt_srv_tkt;
+ unsigned int srv_tkt_count;
+ opt_srv_tkt = gnutls_certificate_get_peers (cmd->session, &srv_tkt_count);
+ if ((opt_srv_tkt != NULL) && (srv_tkt_count >= 1) && (opt_srv_tkt [0].size > 5)) {
// Looks good, we'll use only the first (normally only) one
- credreq.second_ticket.data = peer_tkt [0].data;
- credreq.second_ticket.length = peer_tkt [0].size;
+ credreq.second_ticket.data = opt_srv_tkt [0].data;
+ credreq.second_ticket.length = opt_srv_tkt [0].size;
u2u = KRB5_GC_USER_USER;
}
}
//
// Fetch the ticket for the service
- k5err = krb5_get_credentials (ctx, u2u, cc, &credreq, &ticket);
+ k5err = krb5_get_credentials (ctx, u2u, cc, &credreq, ticket);
//
// Cleanup and return; the return value depends on k5err
cleanup:
if ((cc != NULL) && (cc_opt == NULL)) {
- //TODO// This can go if we always get it passed from have_key_tgt()
+ //TODO// This can go if we always get it passed from have_key_tgt_cc()
krb5_cc_close (ctx, cc);
cc = NULL;
}
#endif
+/* DER utility: This should probably appear in Quick DER sometime soon.
+ *
+ * Pack an Int32 or UInt32 and return the number of bytes. Do not pack a header
+ * around it. The function returns the number of bytes taken, even 0 is valid.
+ */
+typedef uint8_t QDERBUF_INT32_T [4];
+dercursor qder2b_pack_int32 (uint8_t *target_4b, int32_t value) {
+ dercursor retval;
+ int shift = 24;
+ retval.derptr = target_4b;
+ retval.derlen = 0;
+ while (shift >= 0) {
+ if ((retval.derlen == 0) && (shift > 0)) {
+ // Skip sign-extending initial bytes
+ uint32_t neutro = (value >> (shift - 1) ) & 0x000001ff;
+ if ((neutro == 0x000001ff) || (neutro == 0x00000000)) {
+ shift -= 8;
+ continue;
+ }
+ }
+ target_4b [retval.derlen] = (value >> shift) & 0xff;
+ retval.derlen++;
+ shift -= 8;
+ }
+ return retval;
+}
+typedef uint8_t QDERBUF_UINT32_T [5];
+dercursor qder2b_pack_uint32 (uint8_t *target_5b, uint32_t value) {
+ dercursor retval;
+ int ofs = 0;
+ if (value & 0x80000000) {
+ *target_5b = 0x00;
+ ofs = 1;
+ }
+ retval = qder2b_pack_int32 (target_5b + ofs, (int32_t) value);
+ retval.derptr -= ofs;
+ retval.derlen += ofs;
+ return retval;
+}
+
+
+/* DER utility: This should probably appear in Quick DER sometime soon.
+ *
+ * Unpack an Int32 or UInt32 from a given number of bytes. Do not assume a header
+ * around it. The function returns the value found.
+ *
+ * Out of range values are returned as 0. This value only indicates invalid
+ * return when len > 1, so check for that.
+ */
+int32_t qder2b_unpack_int32 (dercursor data4) {
+ int32_t retval = 0;
+ int idx;
+ if (data4.derlen > 4) {
+ goto done;
+ }
+ if ((data4.derlen > 0) && (0x80 & *data4.derptr)) {
+ retval = -1;
+ }
+ for (idx=0; idx<data4.derlen; idx++) {
+ retval <<= 8;
+ retval += data4.derptr [idx];
+ }
+done:
+ return retval;
+}
+uint32_t qder2b_unpack_uint32 (dercursor data5) {
+ uint32_t retval = 0;
+ int ofs = 0;
+ if (data5.derlen > 5) {
+ goto done;
+ }
+ if (data5.derlen == 5) {
+ if (*data5.derptr != 0x00) {
+ goto done;
+ }
+ // Modify the local copy on our stack
+ data5.derlen--;
+ data5.derptr++;
+ }
+ retval = (uint32_t) qder2b_unpack_int32 (data5);
+done:
+ return retval;
+}
+
+
+#ifdef HAVE_TLS_KDH
+/* TODO: Debugging function for printing (descr,ptr,len) ranges */
+static inline void prange (char *descr, uint8_t *ptr, int len) {
+ fprintf (stderr, "%s #%04d: %02x %02x %02x %02x %02x %02x %02x %02x...%02x %02x %02x %02x\n",
+ descr, len,
+ ptr [0], ptr [1], ptr [2], ptr [3],
+ ptr [4], ptr [5], ptr [6], ptr [7],
+ ptr [len-4], ptr [len-3], ptr [len-2], ptr [len-1]);
+}
+static inline void prangefull (char *descr, uint8_t *ptr, int len) {
+ fprintf (stderr, "%s #%04d:", descr, len);
+ while (len-- > 0) {
+ fprintf (stderr, " %02x", *ptr++);
+ }
+ fprintf (stderr, "\n");
+}
+#endif
+
+
+/* The callback function that retrieves a TLS-KDH "signature", which is kept
+ * outside of GnuTLS. The callback computes an authenticator encrypted to
+ * the session's Kerberos key.
+ */
+#ifdef HAVE_TLS_KDH
+static gtls_error cli_kdhsig_encode (gnutls_session_t session,
+ gnutls_datum_t *enc_authenticator,
+ gnutls_datum_t *dec_authenticator,
+ const gnutls_datum_t *hash,
+ int32_t checksum_type) {
+ //
+ // Variables, sanity checking, initialisation
+ struct command *cmd;
+ int k5err = 0;
+ authenticator_t auth;
+ QDERBUF_INT32_T derv5;
+ QDERBUF_INT32_T dernametype;
+ QDERBUF_INT32_T dercksumtype;
+ krb5_keyblock subkey;
+ gnutls_certificate_type_t peercert;
+ QDERBUF_INT32_T dersubkey;
+ krb5_timestamp now_s;
+ char derctime [100];
+ krb5_int32 now_us;
+ QDERBUF_INT32_T dercusec;
+ cmd = (struct command *) gnutls_session_get_ptr (session);
+ memset (&auth, 0, sizeof (auth));
+ memset (&subkey, 0, sizeof (subkey));
+ assert (cmd->krbid_cli != NULL);
+ assert (cmd->krb_key.contents != NULL);
+ static const uint8_t auth_packer [] = {
+ DER_PACK_rfc4120_Authenticator, DER_PACK_END };
+ static const uint8_t encdata_packer [] = {
+ DER_PACK_rfc4120_EncryptedData, DER_PACK_END };
+ //
+ // Setup secure hash in authenticator (never optional for TLS-KDH)
+ auth.cksum.cksumtype = qder2b_pack_int32 (dercksumtype, checksum_type);
+ auth.cksum.checksum.derptr = hash->data;
+ auth.cksum.checksum.derlen = hash->size;
+ //
+ // Optionally include a subkey (namely, for KDH-Only)
+ peercert = gnutls_certificate_type_get_peers (session);
+ if (peercert == GNUTLS_CRT_KRB) {
+ // This is KDH-Only, for which we MUST create a random subkey
+ k5err = krb5_c_make_random_key (
+ krbctx_cli,
+ ENCTYPE_AES256_CTS_HMAC_SHA1_96,
+ &subkey);
+ if (k5err != 0) {
+ return GNUTLS_E_ENCRYPTION_FAILED;
+ }
+ auth.subkey.keytype = qder2b_pack_int32 (dersubkey, subkey.enctype);
+ auth.subkey.keyvalue.derptr = subkey.contents;
+ auth.subkey.keyvalue.derlen = subkey.length;
+prange ("cli_K", subkey.contents, subkey.length);
+ }
+ //
+ // Setup the client realm and principal name
+ auth.crealm.derptr = cmd->krbid_cli->realm.data;
+ auth.crealm.derlen = cmd->krbid_cli->realm.length;
+ auth.cname.name_type = qder2b_pack_int32 (dernametype, cmd->krbid_cli->type);
+ // The SEQUENCE OF with just one component is trivial to prepack
+ auth.cname.name_string.derptr = cmd->krbid_cli->data [0].data;
+ auth.cname.name_string.derlen = cmd->krbid_cli->data [0].length;
+ //
+ // Setup the Kerberos version number (5)
+ auth.authenticator_vno = qder2b_pack_int32 (derv5, 5);
+ //
+ // Setup the obliged microsecond timer values (ignore error returns)
+ krb5_us_timeofday (krbctx_cli, &now_s, &now_us);
+ krb5_timestamp_to_string (now_s, derctime, sizeof (derctime));
+ derctime [sizeof (derctime)-1] = '\0';
+ auth.ctime.derptr = derctime;
+ auth.ctime.derlen = strlen (derctime);
+ auth.cusec = qder2b_pack_int32 (dercusec, now_us);
+ //
+ // Pack the decoded result into dec_authenticator
+ size_t declen = der_pack ( auth_packer,
+ (const dercursor *) &auth,
+ NULL // Measure length, no output yet
+ );
+ uint8_t *decptr = gnutls_malloc (declen);
+ if (decptr == NULL) {
+ return GNUTLS_E_MEMORY_ERROR;
+ }
+ der_pack ( auth_packer,
+ (const dercursor *) &auth,
+ decptr + declen);
+ krb5_free_keyblock_contents (krbctx_cli, &subkey);
+prangefull ("cli_A", decptr, declen);
+ size_t rawlen;
+ if (0 != krb5_c_encrypt_length (krbctx_cli,
+ cmd->krb_key.enctype,
+ declen,
+ &rawlen)) {
+ gnutls_free (decptr);
+ return GNUTLS_E_ENCRYPTION_FAILED;
+ }
+ uint8_t *rawptr = gnutls_malloc (rawlen);
+ if (rawptr == NULL) {
+ gnutls_free (decptr);
+ return GNUTLS_E_MEMORY_ERROR;
+ }
+ krb5_data decdata;
+ krb5_enc_data rawdata;
+ memset (&decdata, 0, sizeof (decdata));
+ memset (&rawdata, 0, sizeof (rawdata));
+ decdata.data = decptr;
+ decdata.length = declen;
+ rawdata.ciphertext.data = rawptr;
+ rawdata.ciphertext.length = rawlen;
+ if (0 != krb5_c_encrypt ( krbctx_cli,
+ &cmd->krb_key,
+ 11 /* stealing key usage from AP-REQ */,
+ NULL,
+ &decdata,
+ &rawdata)) {
+ gnutls_free (rawptr);
+ gnutls_free (decptr);
+ return GNUTLS_E_ENCRYPTION_FAILED;
+ }
+ //
+ // Prepare the header information
+ QDERBUF_INT32_T deretype;
+ QDERBUF_UINT32_T derkvno;
+ encrypted_data_t encdata;
+ memset (&encdata, 0, sizeof (encdata));
+ encdata.etype = qder2b_pack_int32 (deretype, cmd->krb_key.enctype);
+ //NOT// encdata.kvno = qder2b_pack_int32 (derkvno, cmd->krb_key.kvno);
+ encdata.cipher.derptr = rawdata.ciphertext.data;
+ encdata.cipher.derlen = rawdata.ciphertext.length;
+ //
+ // Prepare for packing the header and rawdata as EncryptedData
+ size_t enclen = der_pack ( encdata_packer,
+ (const dercursor *) &encdata,
+ NULL // Measure length, no output yet
+ );
+ uint8_t *encptr = gnutls_malloc (enclen);
+ if (encptr == NULL) {
+ gnutls_free (rawptr);
+ gnutls_free (decptr);
+ return GNUTLS_E_MEMORY_ERROR;
+ }
+ der_pack ( encdata_packer,
+ (const dercursor *) &encdata,
+ encptr + enclen);
+ gnutls_free (rawptr);
+ //
+ // Return our final verdict on the generation of the Authenticator
+ dec_authenticator->data = decptr;
+ dec_authenticator->size = declen;
+ enc_authenticator->data = encptr;
+ enc_authenticator->size = enclen;
+prange ("cli_D", decptr, declen);
+prange ("cli_E", encptr, enclen);
+ return 0;
+}
+#endif
+
+
+/* The callback function that verifies a TLS-KDH "signature", which is kept
+ * outside of GnuTLS. The callback verifies the authenticator against the
+ * provided session hash and returns the decrypted authenticator.
+ */
+#ifdef HAVE_TLS_KDH
+static int srv_kdhsig_decode (gnutls_session_t session,
+ const gnutls_datum_t *enc_authenticator,
+ gnutls_datum_t *dec_authenticator,
+ gnutls_datum_t *hash,
+ int32_t *checksum_type) {
+ //
+ // Variables, sanity checks and initialisation
+ int k5err = 0;
+ struct command *cmd;
+ static const uint8_t encdata_packer [] = {
+ DER_PACK_rfc4120_EncryptedData, DER_PACK_END };
+ static const uint8_t auth_packer [] = {
+ DER_PACK_rfc4120_Authenticator, DER_PACK_END };
+ encrypted_data_t encdata;
+ cmd = (struct command *) gnutls_session_get_ptr (session);
+prange ("srv_E", enc_authenticator->data, enc_authenticator->size);
+ //
+ // Retrieve the session key and store it in cmd->krb_key.
+ //
+ // Prior setting of cmd->krb_key would be due to user-to-user mode
+ // having been setup with this as the server-supplied TGT key, in
+ // which case cmd->krb_key would need to be overwritten by the
+ // session key.
+ //
+ // When no prior cmd->krb_key is available, use the keytab to
+ // decode the client's ticket.
+ assert (gnutls_certificate_type_get_peers (session) == GNUTLS_CRT_KRB);
+ const gnutls_datum_t *certs;
+ unsigned int num_certs;
+ certs = gnutls_certificate_get_peers (cmd->session, &num_certs);
+ if (num_certs != 1) {
+ return GNUTLS_E_NO_CERTIFICATE_FOUND;
+ }
+ krb5_data krbcert;
+ krb5_ticket *tkt;
+ krbcert.data = certs [0].data;
+ krbcert.length = certs [0].size;
+prange ("srv_C", certs [0].data, certs [0].size);
+ if (0 != krb5_decode_ticket (&krbcert, &tkt)) {
+ return GNUTLS_E_NO_CERTIFICATE_FOUND;
+ }
+ if (cmd->krb_key.contents != NULL) {
+ // user-to-user mode
+ k5err = krb5_decrypt_tkt_part (
+ krbctx_srv,
+ &cmd->krb_key,
+ tkt);
+ krb5_free_keyblock_contents (
+ krbctx_srv,
+ &cmd->krb_key);
+ } else {
+ // client-to-server mode
+ k5err = krb5_server_decrypt_ticket_keytab (
+ krbctx_srv,
+ krb_kt_srv,
+ tkt);
+ }
+ if (k5err == 0) {
+ k5err = krb5_copy_keyblock_contents (
+ krbctx_srv,
+ tkt->enc_part2->session,
+ &cmd->krb_key);
+ }
+ if (k5err == 0) {
+ k5err = krb5_copy_principal (
+ krbctx_srv,
+ tkt->enc_part2->client,
+ &cmd->krbid_cli);
+ }
+ if (k5err == 0) {
+ if (cmd->krbid_srv != NULL) {
+ k5err = krb5_principal_compare (
+ krbctx_srv,
+ tkt->server,
+ cmd->krbid_srv);
+ // Server name changed since u2u setup => k5err
+ } else {
+ k5err = krb5_copy_principal (
+ krbctx_srv,
+ tkt->server,
+ &cmd->krbid_srv);
+ }
+ }
+ krb5_free_ticket (krbctx_srv, tkt);
+ if (k5err != 0) {
+ const char *errmsg = krb5_get_error_message (krbctx_srv, k5err);
+ tlog (TLOG_DAEMON, LOG_ERR, "Kerberos error in srv_kdhsig_decode: %s", errmsg);
+ krb5_free_error_message (krbctx_srv, errmsg);
+ return GNUTLS_E_NO_CERTIFICATE_FOUND;
+ }
+ //
+ // Harvest the EncryptedData fields from the enc_authenticator
+ dercursor enctransport;
+ enctransport.derptr = enc_authenticator->data;
+ enctransport.derlen = enc_authenticator->size;
+prangefull ("EncData2unpack", enctransport.derptr, enctransport.derlen);
+ memset (&encdata, 0, sizeof (encdata));
+ if (0 != der_unpack ( &enctransport,
+ encdata_packer,
+ (dercursor *) &encdata,
+ 1)) {
+ tlog (TLOG_DAEMON, LOG_ERR, "Failed to der_unpack(EncryptedData) in server: %s", strerror (errno));
+ return GNUTLS_E_DECRYPTION_FAILED;
+ }
+ if (encdata.kvno.derptr != NULL) {
+ //TODO//NOTYET//ANDWHY// return GNUTLS_E_DECRYPTION_FAILED;
+ }
+ int32_t etype = qder2b_unpack_int32 (encdata.etype);
+ //
+ // Decrypt the EncryptedData fields into the dec_authenticator
+ krb5_enc_data rawdata;
+ krb5_data decdata;
+ memset (&rawdata, 0, sizeof (rawdata));
+ memset (&decdata, 0, sizeof (decdata));
+ rawdata.enctype = etype;
+ rawdata.ciphertext.data = encdata.cipher.derptr;
+ rawdata.ciphertext.length = encdata.cipher.derlen;
+prange ("srv_R", encdata.cipher.derptr, encdata.cipher.derlen);
+ decdata.data = dec_authenticator->data;
+ decdata.length = dec_authenticator->size;
+ if (0 != krb5_c_decrypt ( krbctx_srv,
+ &cmd->krb_key,
+ 11 /* stealing key usage from AP-REQ */,
+ NULL,
+ &rawdata,
+ &decdata)) {
+ return GNUTLS_E_DECRYPTION_FAILED;
+ }
+ dec_authenticator->size = decdata.length;
+prange ("srv_D", decdata.data, decdata.length);
+ //
+ // Unpack the decrypted Authenticator
+ dercursor decsyntax;
+ decsyntax.derptr = decdata.data;
+ decsyntax.derlen = decdata.length;
+prangefull ("srv_A", decdata.data, decdata.length);
+ authenticator_t auth;
+ memset (&auth, 0, sizeof (auth));
+ if (0 != der_unpack ( &decsyntax,
+ auth_packer,
+ (dercursor *) &auth,
+ 1)) {
+ tlog (TLOG_DAEMON, LOG_ERR, "Failed to der_unpack(Authenticator) in server: %s", strerror (errno));
+ return GNUTLS_E_DECRYPTION_FAILED;
+ }
+ //
+ // Validate the contents of the Authenticator
+ if (qder2b_unpack_int32 (auth.authenticator_vno) != 5) {
+ return GNUTLS_E_DECRYPTION_FAILED;
+ }
+ if (auth.cksum.checksum.derptr == NULL) {
+ return GNUTLS_E_DECRYPTION_FAILED;
+ }
+ if (auth.cksum.checksum.derlen < 16) {
+ return GNUTLS_E_DECRYPTION_FAILED;
+ }
+ //TODO// Optionally, for KDH-Only, ensure presence and size of a subkey
+ //
+ // Produce the requested content from the Authenticator and return
+ *checksum_type = qder2b_unpack_int32 (auth.cksum.cksumtype);
+ hash->data = auth.cksum.checksum.derptr;
+ hash->size = auth.cksum.checksum.derlen;
+ return 0;
+}
+#endif
+
+
+
/********** VALIDATION EXPRESSION LINKUP TO GNUTLS **********/
+
/*
* The following functions implement the various validation expression
* components in terms of the GnuTLS sessions of this code file.
valflag = o2vf (online_globaldir_x509 (
rid,
raw->data, raw->size));
-#ifdef GNUTLS_CRT_KRB
+#ifdef HAVE_TLS_KDH
} else if (cmd->remote_cert_type == GNUTLS_CRT_KRB) {
- // Kerberos is sufficiently "live" to always pass O
+ // Kerberos is sufficiently "live" to be pass O
valflag = 1;
goto setvalflag;
#endif
}
//
// Continue loading the certificate information: X.509, PGP, ...
- cmd->remote_cert_type = gnutls_certificate_type_get (cmd->session);
+#ifdef HAVE_TLS_KDH
+ cmd->remote_cert_type = gnutls_certificate_type_get_peers (cmd->session);
certs = gnutls_certificate_get_peers (cmd->session, &num_certs);
+ // Note: server's certs _may_ be DER NULL due to mutual auth in Kerberos
+#else
+ cmd->remote_cert_type = gnutls_certificate_type_get (cmd->session);
+ certs = gnutls_certificate_get (cmd->session, &num_certs);
+#endif
if (certs == NULL) {
num_certs = 0;
}
#endif
tlog (TLOG_DB, LOG_DEBUG, "BDB entry has flags=0x%08x, so we (%04x/%04x) %s it", flags, lidrole, LID_ROLE_MASK, ok? "store": "skip ");
if (ok) {
+ if (cmd->lids [lidtype - LID_TYPE_MIN].data != NULL) {
+ free (cmd->lids [lidtype - LID_TYPE_MIN].data);
+ }
// Move the credential into the command structure
dbt_store (&creddata,
&cmd->lids [lidtype - LID_TYPE_MIN]);
+fprintf (stderr, "DEBUG: Storing cmd->lids[%d].data 0x%016x\n", lidtype-LID_TYPE_MIN, cmd->lids [lidtype-LID_TYPE_MIN].data);
found = 1;
} else {
// Skip the credential by freeing its data structure
* Return 1 for yes or 0 for no; this is used in priority strings.
*/
static inline int lidtpsup (struct command *cmd, int lidtp) {
- return 1; //TODO// Can we decide if we needn't authenticate?
return cmd->lids [lidtp - LID_TYPE_MIN].data != NULL;
}
// - Configured security parameters (database? variable?)
// - CTYPEs, SRP, ANON-or-not --> fill in as + or - characters
if (gtls_errno == GNUTLS_E_SUCCESS) {
- char priostr [256];
+ char priostr [512];
+#ifdef HAVE_TLS_KDH
snprintf (priostr, sizeof (priostr)-1,
// "NORMAL:-RSA:" -- also ECDH-RSA, ECDHE-RSA, ...DSA...
"NONE:"
-#ifdef HAVE_TLS_KDH
"%%ASYM_CERT_TYPES:"
-#endif
"+VERS-TLS-ALL:+VERS-DTLS-ALL:"
"+COMP-NULL:"
"+CIPHER-ALL:+CURVE-ALL:+SIGN-ALL:+MAC-ALL:"
"%cANON-ECDH:"
+ "+ECDHE-KRB:" // +ECDHE-KRB-RSA:+ECDHE-KRB-ECDHE:" // opt?
"+ECDHE-RSA:+DHE-RSA:+ECDHE-ECDSA:+DHE-DSS:+RSA:" //TODO//
- "%cCTYPE-X.509:"
- "%cCTYPE-OPENPGP:"
-#ifdef HAVE_TLS_KDH
- "%cCTYPE-CLI-KRB:%cCTYPE-SRV-X.509:"
-#endif
+ "+CTYPE-SRV-KRB:+CTYPE-SRV-X.509:+CTYPE-SRV-OPENPGP:"
+ "%cCTYPE-CLI-KRB:"
+ "%cCTYPE-CLI-X.509:"
+ "%cCTYPE-CLI-OPENPGP:"
"%cSRP:%cSRP-RSA:%cSRP-DSS",
anonpre_ok ?'+':'-',
- lidtpsup (cmd, LID_TYPE_X509) ?'+':'-',
- lidtpsup (cmd, LID_TYPE_PGP) ?'+':'-',
-#ifdef HAVE_TLS_KDH
- lidtpsup (cmd, LID_TYPE_KRB5) ?'+':'-',
- lidtpsup (cmd, LID_TYPE_KRB5) ?'+':'-',
-#endif
+ 1 /* lidtpsup (cmd, LID_TYPE_KRB5)*/ ?'+':'-',
+ 1 /*lidtpsup (cmd, LID_TYPE_X509)*/ ?'+':'-',
+ 1 /*lidtpsup (cmd, LID_TYPE_PGP)*/ ?'+':'-',
//TODO// Temporarily patched out SRP
lidtpsup (cmd, LID_TYPE_SRP) ?'+':'-',
lidtpsup (cmd, LID_TYPE_SRP) ?'+':'-',
lidtpsup (cmd, LID_TYPE_SRP) ?'+':'-');
-// strcpy (priostr, "NONE:+VERS-TLS-ALL:+MAC-ALL:+RSA:+AES-128-CBC:+SIGN-ALL:+COMP-NULL"); //TODO:TEST//
-// strcpy (priostr, "NONE:+VERS-TLS-ALL:+VERS-DTLS-ALL:+MAC-ALL:+RSA:+AES-128-CBC:+SIGN-ALL:+COMP-NULL"); //TODO:TEST//
+#else
+ // It's not possible to make good decisions on certificate type
+ // for both sides based on knowledge of local authentication
+ // abilities. So we permit all (but would like to be subtler).
+ snprintf (priostr, sizeof (priostr)-1,
+ // "NORMAL:-RSA:" -- also ECDH-RSA, ECDHE-RSA, ...DSA...
+ "NONE:"
+ "+VERS-TLS-ALL:+VERS-DTLS-ALL:"
+ "+COMP-NULL:"
+ "+CIPHER-ALL:+CURVE-ALL:+SIGN-ALL:+MAC-ALL:"
+ "%cANON-ECDH:"
+ "+ECDHE-RSA:+DHE-RSA:+ECDHE-ECDSA:+DHE-DSS:+RSA:" //TODO//
+ "%cCTYPE-X.509:"
+ "%cCTYPE-OPENPGP:"
+ "%cSRP:%cSRP-RSA:%cSRP-DSS",
+ anonpre_ok ?'+':'-',
+ 1 ?'+':'-',
+ 1 ?'+':'-',
+ //TODO// Temporarily patched out SRP
+ 1 ?'+':'-',
+ 1 ?'+':'-',
+ 1 ?'+':'-');
+#endif
tlog (TLOG_TLS, LOG_DEBUG, "Constructed priority string %s for local ID %s",
priostr, cmd->cmd.pio_data.pioc_starttls.localid);
E_g2e ("Failed to set GnuTLS priority string",
* - TLS hints -- Server Name Indication
* - User hints -- local and remote identities provided
*/
-int srv_clienthello (gnutls_session_t session) {
+static int srv_clienthello (gnutls_session_t session, unsigned int htype, unsigned int post, unsigned int incoming, const gnutls_datum_t *msg) {
struct command *cmd;
+ int gtls_errno = GNUTLS_E_SUCCESS;
char sni [sizeof (cmd->cmd.pio_data.pioc_starttls.remoteid)]; // static
size_t snilen = sizeof (sni);
int snitype;
- int gtls_errno = GNUTLS_E_SUCCESS;
char *lid;
+tlog (LOG_DAEMON, LOG_INFO, "Invoked %sprocessor for Client Hello, htype=%d, incoming=%d\n",
+ post ? "post" : "pre",
+ htype,
+ incoming);
+
fprintf (stderr, "DEBUG: Got errno = %d / %s at %d\n", errno, strerror (errno), __LINE__);
errno = 0;
fprintf (stderr, "DEBUG: Got errno = %d / %s at %d\n", errno, strerror (errno), __LINE__);
+
+if (!post) {
//
// Setup a number of common references
cmd = (struct command *) gnutls_session_get_ptr (session);
if (cmd == NULL) {
return GNUTLS_E_INVALID_SESSION;
}
- lid = cmd->cmd.pio_data.pioc_starttls.localid;
//
// Setup server-specific credentials and priority string
cmd->anonpre & ANONPRE_SERVER));
fprintf (stderr, "DEBUG: Got gtls_errno = %d at %d\n", gtls_errno, __LINE__);
+} else {
+
+ //
+ // Setup a number of common references
+ cmd = (struct command *) gnutls_session_get_ptr (session);
+ if (cmd == NULL) {
+ return GNUTLS_E_INVALID_SESSION;
+ }
+ lid = cmd->cmd.pio_data.pioc_starttls.localid;
+
//
// Setup to ignore/request/require remote identity (from client)
fprintf (stderr, "DEBUG: Got errno = %d / %s at %d\n", errno, strerror (errno), __LINE__);
sni [sizeof (sni) - 1] = '\0';
}
fprintf (stderr, "DEBUG: Got gtls_errno = %d at %d\n", gtls_errno, __LINE__);
+}
//
// Lap up any unnoticed POSIX error messages
fprintf (stderr, "DEBUG: Configuring for server credentials callback if %d==0\n", gtls_errno);
if (!renegotiating) { //TODO:TEST//
if (gtls_errno == GNUTLS_E_SUCCESS) {
- gnutls_handshake_set_post_client_hello_function (
+ gnutls_handshake_set_hook_function (
session,
+ GNUTLS_HANDSHAKE_CLIENT_HELLO,
+ GNUTLS_HOOK_BOTH,
srv_clienthello);
}
} //TODO:TEST//
// Cleanup any prefetched identities
for (i=LID_TYPE_MIN; i<=LID_TYPE_MAX; i++) {
if (cmd->lids [i - LID_TYPE_MIN].data != NULL) {
+fprintf (stderr, "DEBUG: Freeing cmd->lids[%d].data 0x%016x\n", i-LID_TYPE_MIN, cmd->lids [i-LID_TYPE_MIN].data);
free (cmd->lids [i - LID_TYPE_MIN].data);
}
}
if (cmd->krb_key.contents != NULL) {
// RATHER BLUNT: It shouldn't matter which krbctx_ is used...
krb5_free_keyblock_contents (krbctx_srv, &cmd->krb_key);
+ memset (&cmd->krb_key, 0, sizeof (cmd->krb_key));
+ }
+ if (cmd->krbid_srv != NULL) {
+ // RATHER BLUNT: It shouldn't matter which krbctx_ is used...
+ krb5_free_principal (krbctx_srv, cmd->krbid_srv);
+ cmd->krbid_srv = NULL;
+ }
+ if (cmd->krbid_cli != NULL) {
+ // RATHER BLUNT: It shouldn't matter which krbctx_ is used...
+ krb5_free_principal (krbctx_srv, cmd->krbid_cli);
+ cmd->krbid_cli = NULL;
}
#if 0