Commit Graph

2280 Commits

Author SHA1 Message Date
Luke Howard
8fc67658a6 various: squash MSVC uninitialized variable warnings (C4701)
Initialize some variables to silence some false positive MSVC warnings.
2021-09-21 18:59:38 +10:00
Robert Crowston
9b46d6436f Only #include <malloc.h> if it is available.
e.g., OpenBSD does not provide this header.
2021-09-07 12:49:19 +10:00
Luke Howard
fc9f9b322a gss: implement gss_krb5_ccache_name()
Correctly implement gss_krb5_ccache_name() in terms of
gss_set_sec_context_option(GSS_KRB5_CCACHE_NAME_X). The previous implementation
was a NOOP.

Note: global ccache name should really be thread-specific rather than global.

Closes #803.
2021-09-06 13:26:55 +10:00
Luke Howard
774f50b28b gss: move GSS pre-auth helpers to convenience lib
GSS pre-auth helpers do not belong in libgssapi, so move them to a separate
convenience library.
2021-08-27 15:20:07 +10:00
Nicolas Williams
a5378daa6c Revert "gss: Fix leak of output_token in GSS PA"
This reverts commit 041907d517.
2021-08-26 01:06:33 -05:00
Nicolas Williams
041907d517 gss: Fix leak of output_token in GSS PA 2021-08-25 22:53:52 -05:00
Andrew Bartlett
6f21611755 Avoid -Werror=address by skipping pointless _mg_buffer_zero()
We do not need to zero out the local variable output_token
if we do not later call gss_release_buffer() on it.

This aovids a -Werror=address compile failure under the
strict compiler options Samba uses when compiled on Ubuntu
20.04 with gcc version 9.3.0 (Ubuntu 9.3.0-17ubuntu1~20.04)

In file included from ../../source4/heimdal/lib/gssapi/preauth/pa_client.c:34:
../../source4/heimdal/lib/gssapi/preauth/pa_client.c:148:21: error: the address of ‘output_token’ will always evaluate as ‘true’ [-Werror=address]
  148 |     _mg_buffer_zero(&output_token);
      |                     ^
../../source4/heimdal/lib/gssapi/mech/mech_locl.h:72:7: note: in definition of macro ‘_mg_buffer_zero’
   72 |   if (buffer) {   \
      |       ^~~~~~

Signed-off-by: Andrew Bartlett <abartlet@samba.org>
2021-08-26 09:31:00 +10:00
Nicolas Williams
75449e1071 gss: Fix build for GSS preauth helpers 2021-08-25 15:41:35 -05:00
Luke Howard
b3511c145c gss: check for NULL before calling dlclose()
Calling dlclose(NULL) on some recent versions of Linux appears to cause the
process to segfault.
2021-08-20 00:07:12 +10:00
Luke Howard
a56c3ad819 krb5: fix prototype in pa_gss_finish()
Fix merge issue that arose after merging another fix from the
lukeh/gss-preauth-apple branch.
2021-08-18 08:17:04 +10:00
Luke Howard
386b8d328a kdc: refactor/improve error handling in GSS pa 2021-08-15 16:07:46 +10:00
Luke Howard
432fe0ad56 krb5: treat KRB5_NT_SRV_HST and KRB5_NT_SRV_INST identically
Update previous commit, mapping KRB5_NT_SRV_HST as well as KRB5_NT_SRV_INST to
GSS_C_NT_HOSTBASED_SERVICE.
2021-08-15 15:04:21 +10:00
Luke Howard
95d553e009 krb5: improve krb5 to GSS name conversion
First attempt to import krb5 names as GSS_KRB5_NT_PRINCIPAL_NAME; if that
fails, choose the most appropriate name form based on Kerberos name type.
2021-08-15 14:48:14 +10:00
Luke Howard
e840681451 krb5: try GSS_KRB5_NT_PRINCIPAL_NAME first for PA
When importing a Kerberos name for GSS pre-auth, first try to import the name
as GSS_KRB5_NT_PRINCIPAL_NAME. If that fails, fall back to GSS_C_NT_USER_NAME.
2021-08-15 09:54:45 +10:00
Luke Howard
f73f08eef1 kdc: ensure GSS-API pre-auth acceptor name is TGS
The target (acceptor) name for GSS-API pre-authentication should be the name of
the TGS, not the server name in the AS-REQ, as it is the KDC which is being
mutually authenticated. If the client is not requesting a TGT, they may differ.
2021-08-15 09:14:58 +10:00
Luke Howard
95339c1068 gss: fix Windows preauth build issues
Fix lib/gssapi NTMakefile for GSS-API pre-authentication dependencies
2021-08-12 18:09:40 +10:00
Luke Howard
3302b38e53 gss: fix Windows preauth build issues
Fix kdc/kuser NTMakefiles for GSS-API pre-authentication dependencies
2021-08-12 17:57:17 +10:00
Luke Howard
49f3f5bd99 kdc: support for GSS-API pre-authentication
Add support for GSS-API pre-authentication to the KDC, using a simplified
variation of draft-perez-krb-wg-gss-preauth-02 that encodes GSS-API context
tokens directly in PADATA, and uses FX-COOKIE for state management.

More information on the protocol and implementation may be found in
lib/gssapi/preauth/README.md.
2021-08-12 17:37:01 +10:00
Luke Howard
3a9e7346cc gss: cast HEIM_ERR_EOF to OM_uint32 before check (2)
In gss_import_cred(): HEIM_ERR_EOF is negative, cast to OM_uint32 before
checking against *minor_status
2021-08-12 09:20:22 +10:00
Luke Howard
3f4d894dd3 gss: cast HEIM_ERR_EOF to OM_uint32 before check
In gss_import_cred(): HEIM_ERR_EOF is negative, cast to OM_uint32 before
checking against *minor_status
2021-08-12 09:18:22 +10:00
Luke Howard
1175fa0435 gss: don't include mech_locl.h in spnego
including mech_locl.h in spnego/context_storage.c is unnecessary and
breaks the build on Windows
2021-08-11 19:12:11 +10:00
Luke Howard
a27a3af0c2 gss: store_spnego_context(): remove redundant overflow check
ctx->negoex_step is now a uint8_t, so it can never be bigger than 0xff. Remove
redundant overflow check.
2021-08-10 20:11:35 +10:00
Luke Howard
43858eaf9b gss: add oid/buffer storage helpers to mechglue
The functions for storing and retrieving GSS OIDs and buffers from
krb5_storage, added in 6554dc69, are generally useful. Move those into private
_gss_mg_XXX() API and update gss_{export,import}_{cred,sec_context} to use them
where appropriate.
2021-08-10 10:16:54 +10:00
Luke Howard
490173b417 gss: free exported context buffer after use in store_negoex_auth_mech()
Code style/consistency: store_negoex_auth_mech() should free the exported
context buffer after use. There is no leak as the buffer was freed at the end
of the function.
2021-08-10 10:16:50 +10:00
Andrew Bartlett
3587b8c08c Allow _gss_string_to_oid() without HAVE_DLOPEN
This function became used outside the protection of
HAVE_DLOPEN (which Samba sets) with:

commit 5966c00701
Author: Luke Howard <lukeh@padl.com>
Date:   Sun Aug 8 10:34:28 2021 +1000

    gss: add gss_mg_name_to_oid internal API

    Add a new function for future internal use, gss_mg_name_to_oid(), which takes
    either a dot-separated OID or a "short" mechanism name (such as
    "sanon-x25519"), and returns a mechanism OID.

Signed-off-by: Andrew Bartlett <abartlet@samba.org>
2021-08-09 09:58:58 +10:00
Luke Howard
06232dfcce gss: fix import/export of accumulating contexts
gss_{import,export}_sec_context did not work with partially accumulating
contexts, where the initial context token had not been completely accumulated,

Further, in gss_import_sec_context(), ctx->gc_input.value was not allocated to
a buffer sufficiently large to accumulate the target length.
2021-08-08 14:46:49 +10:00
Luke Howard
3b1f9f0a3f gss: error out in test_context if unknown OID
Fix regression in previous commit: exit with an error if test_context is called
with an unknown mechanism type.
2021-08-08 13:45:32 +10:00
Luke Howard
18c18d84b1 gss: merge gss_name_to_oid and gss_mg_name_to_oid
The recently introduced gss_mg_name_to_oid() function supported looking up
dynamically loaded mechanisms by name, but did not support partial matches or
the legacy "Kerberos 5" name as supported by gss_name_to_oid().

Consolidate these into a single function, and also add support for dynamically
loaded mechanisms to gss_oid_to_name().

API behavior difference: the Kerberos mechanism is now referred to by "krb5"
rather tha "Kerberos 5", although for legacy compatibility gss_name_to_oid()
will recognize the old name. However, gss_oid_to_name() will return "krb5". The
anticipated impact is minimal as these are not standard GSS-APIs and do not
appear to have any public usage outside Heimdal.
2021-08-08 11:37:09 +10:00
Luke Howard
5966c00701 gss: add gss_mg_name_to_oid internal API
Add a new function for future internal use, gss_mg_name_to_oid(), which takes
either a dot-separated OID or a "short" mechanism name (such as
"sanon-x25519"), and returns a mechanism OID.
2021-08-08 10:34:28 +10:00
Luke Howard
6554dc69b0 gss: allow partial accept context export in SPNEGO
Support for exporting partially established acceptor context tokens. With this,
an acceptor can send the initiator an encrypted state cookie containing the
exported context token.

(The concrete mechanism, of course, must either require a single round trip or
support partial context export itself. Kerberos and GSS EAP would work, but
Kerberos with GSS_C_DCE_STYLE would not, as currently implemented.)

Partial context export is not permitted for initiators.
2021-08-07 18:56:33 +10:00
Roland C. Dowdeswell
fe426f7a28 fix type s/MUTAL/MUTUAL/ in gssapi/krb5 2021-08-07 18:54:56 +10:00
Roland C. Dowdeswell
3a6229f64a gss_accept_sec_context: support reassembling split tokens.
Microsoft will sometimes split GSS tokens when they exceed a certain
size in some protocols.  This is specified in

	[MS-SPNG]: Simple and Protected GSS-API Negotiation
	Mechanism (SPNEGO) Extension

https://winprotocoldoc.blob.core.windows.net/productionwindowsarchives/MS-SPNG/%5bMS-SPNG%5d.pdf

sections 3.1.5.4 to 3.1.5.9.

We extend gss_accept_sec_context() to recognise partial tokens and
to accumulate the fragments until an entire token is available to
be processed.  If the entire token is not yet available,
GSS_S_CONTINUE_NEEDED is returned with a zero length output token.
This is specified in RFC2744 page 25-26 to indicate that no reply
need be sent.

We include updates to the test framework to test split tokens when
using SPNEGO.
2021-08-07 18:54:56 +10:00
Roland C. Dowdeswell
80f3194a76 gssapi/krb5/{export,import}_sec_context: make smaller tokens. 2021-08-07 18:54:56 +10:00
Roland C. Dowdeswell
a2cfd2a25c gssapi/krb5/{export,import}_sec_context: fix for init_sec_ctx
When using these functions with gss_init_sec_context(), we noticed
that some things were missing and some needed to be made optional.
ctx->order may be NULL, ctx->ac->authenticator needs to be filled
out, and ctx->state needs be stored.

Note: SPNEGO still needs a little more work.
2021-08-07 18:54:56 +10:00
Roland C. Dowdeswell
6ccb21827b gss-token: initialise input buffer to empty 2021-08-07 08:40:20 +10:00
Luke Howard
8330e45444 gss: set GSS_C_CHANNEL_BOUND_FLAG for SAnon
SAnon includes channel bindings as part of the key derivation function, so they
cannot be ignored. Always set GSS_C_CHANNEL_BOUND_FLAG in the SAnon acceptor.
2021-08-06 13:21:36 +10:00
Isaac Boukris
d83321fdf3 Add channel-bindings tests 2021-08-06 13:15:19 +10:00
Isaac Boukris
a4527a28a3 Implement KERB_AP_OPTIONS_CBT (server side)
if the client asserted knowledge of channel-bindings by
passing KERB_AP_OPTIONS_CBT, and the server passed bindings,
require the bindings to match.
2021-08-06 13:15:19 +10:00
Isaac Boukris
51ce4c8d15 gssapi: add channel-bound return flag
In gss_accept_sec_context, return a new flag to let
the caller know that bindings were provided and verified.
2021-08-06 13:15:19 +10:00
Luke Howard
034bc1649d gss: avoid overflow in gss_inquire_cred()
See #791

Make found a boolean rather than a count of found mechanisms.
2021-08-06 12:23:50 +10:00
Luke Howard
510ee92c11 gss: treat empty cred store as GSS_C_NO_CRED_STORE
In acquire_mech_cred(), treat a credential store with no elements as equivalent
to GSS_C_NO_CRED_STORE, allowing a mechanism's gss_acquire_cred()
implementation to be called.
2021-08-05 15:53:18 +10:00
Luke Howard
d672e49231 gss: don't leak NTLM cred on failure
Do not leak partially complete credential handle in _gss_ntlm_get_user_cred()
2021-08-02 17:09:55 +10:00
Luke Howard
73224fef5a gss: zero spnego mechanism token error buffer
Ensure mechanism error token buffer, which is allocated on the stack, is zero'd
out before freeing containing ASN.1 structure
2021-08-02 15:55:10 +10:00
Luke Howard
221c295d5d gss: honor GM_USE_MG_NAME in gss_accept_sec_context()
GM_USE_MG_NAME was not honored in the case where the mechanism emitted a name,
but the caller of gss_accept_sec_context() did not request it be returned. This
would result in m->gm_release_name() being called on the mechglue name, which
would crash either because that function pointer was NULL or because it would
have expected a mechanism name.
2021-08-01 22:12:25 +10:00
Luke Howard
f72627c3f0 gss: make IS_DCE_STYLE an inline function
To avoid future regressions such as the one corrected in 0dd19003, make
IS_DCE_STYLE() an inline function (rather than a macro) so that its
argument is typed.
2021-07-08 00:00:44 +10:00
Luke Howard
0dd1900308 gss: fix regression in rc4-hmac krb5 DCE unwrap
4b543b7 introduced a regression in the krb5 mechanism's gss_unwrap for
DCE applications, owing to IS_DCE_STYLE() being called with a krb5
instead of mechanism context handle.
2021-07-07 23:51:06 +10:00
Andrew Bartlett
4a5fc6bcde Move out: label to inside #endif of HAVE_DLOPEN
Samba compiles Heimdal internally without HAVE_DLOPEN to keep
to internally supplied mechanisms and plugins.

Samba compiles with strict warning flags and on Ubuntu 20.04
with gcc version 9.3.0 (Ubuntu 9.3.0-17ubuntu1~20.04) we see:

../../source4/heimdal/lib/gssapi/mech/gss_mech_switch.c: In function ‘_gss_load_mech’:
../../source4/heimdal/lib/gssapi/mech/gss_mech_switch.c:462:1: error: label ‘out’ defined but not used [-Werror=unused-label]
  462 | out:
      | ^~~
cc1: all warnings being treated as errors

Signed-off-by: Andrew Bartlett <abartlet@samba.org>
2021-06-25 09:44:53 +10:00
Luke Howard
497a561b4d gss: avoid string concatenation warning in error message init
Some compilers with -Wstring-concatenation enabled warned about a suspicious
concatenation of string literals in the initialization of the GSS-API error
message array.

At the expense of a long line, avoid this warning but explicitly concatenating
the offending string literal.

Fixes: #775
2021-05-17 10:09:01 +10:00
Nicolas Williams
6abb8bc286 Document OID arcs delegated to Heimdal by SU 2021-03-25 10:38:28 -05:00
Nicolas Williams
db7763ca7b asn1: X.681/682/683 magic handling of open types
Status:

 - And it works!

 - We have an extensive test based on decoding a rich EK certficate.

   This test exercises all of:

    - decoding
    - encoding with and without decoded open types
    - copying of decoded values with decoded open types
    - freeing of decoded values with decoded open types

   Valgrind finds no memory errors.

 - Added a manual page for the compiler.

 - rfc2459.asn1 now has all three primary PKIX types that we care about
   defined as in RFC5912, with IOS constraints and parameterization:

    - `Extension`       (embeds open type in an `OCTET STRING`)
    - `OtherName`       (embeds open type in an        `ANY`-like type)
    - `SingleAttribute` (embeds open type in an        `ANY`-like type)
    - `AttributeSet`    (embeds open type in a  `SET OF ANY`-like type)

   All of these use OIDs as the open type type ID field, but integer
   open type type ID fields are also supported (and needed, for
   Kerberos).

   That will cover every typed hole pattern in all our ASN.1 modules.

   With this we'll be able to automatically and recursively decode
   through all subject DN attributes even when the subject DN is a
   directoryName SAN, and subjectDirectoryAttributes, and all
   extensions, and all SANs, and all authorization-data elements, and
   PA-data, and...

   We're not really using `SingleAttribute` and `AttributeSet` yet
   because various changes are needed in `lib/hx509` for that.

 - `asn1_compile` builds and recognizes the subset of X.681/682/683 that
   we need for, and now use in, rfc2459.asn1.  It builds the necessary
   AST, generates the correct C types, and generates templating for
   object sets and open types!

 - See READMEs for details.

 - Codegen backend not tested; I won't make it implement automatic open
   type handling, but it should at least not crash by substituting
   `heim_any` for open types not embedded in `OCTET STRING`.

 - We're _really_ starting to have problems with the ITU-T ASN.1
   grammar and our version of it...

   Type names have to start with upper-case, value names with
   lower-case, but it's not enough to disambiguate.

   The fact the we've allowed value and type names to violate their
   respective start-with case rules is causing us trouble now that we're
   adding grammar from X.681/682/683, and we're going to have to undo
   that.

   In preparation for that I'm capitalizing the `heim_any` and
   `heim_any_set` types, and doing some additional cleanup, which
   requires changes to other parts of Heimdal (all in this same commit
   for now).

   Problems we have because of this:

    - We cannot IMPORT values into modules because we have no idea if a
      symbol being imported refers to a value or a type because the only
      clue we would have is the symbol's name, so we assume IMPORTed
      symbols are for types.

      This means we can't import OIDs, for example, which is super
      annoying.

      One thing we might be able to do here is mark imported symbols as
      being of an undetermined-but-not-undefined type, then coerce the
      symbol's type the first time it's used in a context where its type
      is inferred as type, value, object, object set, or class.  (Though
      since we don't generate C symbols for objects or classes, we won't
      be able to import them, especially since we need to know them at
      compile time and cannot defer their handling to link- or
      run-time.)

    - The `NULL` type name, and the `NULL` value name now cause two
      reduce/reduce conflicts via the `FieldSetting` production.

    - Various shift/reduce conflicts involving `NULL` values in
      non-top-level contexts (in constraints, for example).

 - Currently I have a bug where to disambiguate the grammar I have a
   CLASS_IDENTIFIER token that is all caps, while TYPE_IDENTIFIER must
   start with a capital but not be all caps, but this breaks Kerberos
   since all its types are all capitalized -- oof!

   To fix this I made it so class names have to be all caps and
   start with an underscore (ick).

TBD:

 - Check all the XXX comments and address them
 - Apply this treatment to Kerberos!  Automatic handling of authz-data
   sounds useful :)
 - Apply this treatment to PKCS#10 (CSRs) and other ASN.1 modules too.
 - Replace various bits of code in `lib/hx509/` with uses of this
   feature.
 - Add JER.
 - Enhance `hxtool` and `asn1_print`.

Getting there!
2021-02-28 18:13:08 -06:00