runtime icon indicating copy to clipboard operation
runtime copied to clipboard

Implement Cross-Platform Kerberos for .NET Core

Open alexkeh opened this issue 8 years ago • 42 comments

Kerberos is a popular authentication protocol that many Windows .NET applications use. We would like .NET Core to add a cross-platform Kerberos implementation, including support for these MIT Kerberos APIs or equivalent functionality:

  • krb5_init_context((krb5_context *)&context);
  • krb5_sname_to_principal(context, (char *) 0, (char *) 0, KRB5_NT_SRV_HST, &server);
  • krb5_get_default_realm(context, &therealm);
  • krb5_cc_resolve(context, CCNm_, &ccache);
  • krb5_cc_get_principal(context, ccache, &client);
  • krb5_auth_con_init(context, &auth_context);
  • krb5_copy_principal(context, server, &creds.server);
  • krb5_copy_principal(context, client, &creds.client);
  • krb5_get_credentials(context, 0, ccache, &creds, &credsp);
  • krb5_mk_req_extended(context, &auth_context, auth_options, &cksum_data, credsp, &outbuf);

alexkeh avatar Sep 13 '18 21:09 alexkeh

What are the user scenarios taking advantage of the APIs? Do you have some API shape in mind? How should it be integrated into existing networking APIs?

cc @wfurt @davidsh

karelz avatar Sep 14 '18 17:09 karelz

Basically, we are acquiring client Kerberos credentials, then using them and server side credentials to acquire a TGT for the server DB service, and then generating the associated "authenticator" and sending it to the server. ie, pretty standard for authenticating to a service.

ScotMac avatar Sep 14 '18 18:09 ScotMac

We would prefer that it is NOT integrated into any existing Network APIs, since that gives the API user more flexibility in terms of how to use or send the data to the server...which is the same as how MIT Kerberos5 APIs do it. Specifically, this is not at all associated with a web server or client.

ScotMac avatar Sep 14 '18 18:09 ScotMac

So you are looking for general API for the protocol. Does anything prevent anyone from implementing it as separate library? Why does it need to be in CoreFX / .NET Core? Just convenience? I assume there are similar APIs in Windows for Kerberos. Is that correct?

karelz avatar Sep 14 '18 19:09 karelz

https://github.com/SteveSyfuhs/Kerberos.NET ?

jkotas avatar Sep 14 '18 19:09 jkotas

.NET already provides some high-level APIs that use Kerberos functionality internally, such as NegotiateStream. It may make sense to expose the low-level APIs in a platform independent way and layer the high level ones on top of them.

We already had to write platform specific versions of the code in order to support Kerberos authentication within the context of SASL authentication, which is in turn used by application protocols such as SMTP, IMAP or LDAP.

Unfortunately the high-level API already provided is not suitable for this use case since we are bound by another protocol standard, where the on-wire representation is different.

filipnavara avatar Sep 14 '18 19:09 filipnavara

If there is room for reuse of APIs even inside .NET Core itself, then it would definitely make sense. We would need someone familiar with those implementations to help design the right API surface - satisfying the consumer needs & making it reasonable for public consumption.

karelz avatar Sep 14 '18 20:09 karelz

.NET already provides some high-level APIs that use Kerberos functionality internally, such as NegotiateStream. It may make sense to expose the low-level APIs in a platform independent way and layer the high level ones on top of them.

We already had to write platform specific versions of the code in order to support Kerberos authentication within the context of SASL authentication, which is in turn used by application protocols such as SMTP, IMAP or LDAP.

Unfortunately the high-level API already provided is not suitable for this use case since we are bound by another protocol standard, where the on-wire representation is different.

Yes, this is exactly the problem. The windows APIs and .NET APIs are within the context of very specific upper layer protocols, and often a windows domain instead of a generic KDC.

ScotMac avatar Sep 14 '18 21:09 ScotMac

https://github.com/SteveSyfuhs/Kerberos.NET ?

Steve's package does no authenticator generation, only validation.

ScotMac avatar Sep 15 '18 00:09 ScotMac

So you are looking for general API for the protocol. Does anything prevent anyone from implementing it as separate library? Why does it need to be in CoreFX / .NET Core? Just convenience? I assume there are similar APIs in Windows for Kerberos. Is that correct?

Yes, it doesn't have to be in .NET/.NET Core. For example, if Steve's package could be extended to do the GENERATION of the authenticator, that would be a great first step.

But yes, it would be a lot "nicer" if it was included in .NET/.NET Core.

ScotMac avatar Sep 15 '18 00:09 ScotMac

For what it's worth, the library has been extended to do generation (and issuance and what not).

SteveSyfuhs avatar Aug 09 '19 21:08 SteveSyfuhs

@SteveSyfuhs

Does that mean we can generate Kerberos tokens in .net Core now ? I .net 4.8 we could do it with something like this:

        private string KerberosToken(Uri uri)
        {
            // Uri uri = new Uri(url);
            var serviceName = "HTTP/" + uri.Host + (uri.Port != 80 && uri.Port != 443 ? ":" + uri.Port : "");

            var tokenProvider = new KerberosSecurityTokenProvider(serviceName, TokenImpersonationLevel.Impersonation, CredentialCache.DefaultNetworkCredentials);
            var securityToken = tokenProvider.GetToken(TimeSpan.FromMinutes(1)) as KerberosRequestorSecurityToken;
            return Convert.ToBase64String(securityToken.GetRequest());
        }

tornie2 avatar Aug 21 '19 10:08 tornie2

That class is not available in Core AFAIK. It's also very Windows-specific. The library people are referring to above is 3rd party and is an implementation of the Kerberos protocol. It does not call into GSSAPI, which is probably not the most useful for corefx.

SteveSyfuhs avatar Aug 21 '19 15:08 SteveSyfuhs

The APIs seem to be too low-level for us to take into the BCL. We think the APIs won't be used by too many people. It sounds like a good candidate for independent package. Closing.

karelz avatar Sep 05 '19 18:09 karelz

Whereas it is great that Steve's package has been extended to client side, it doesn't help for the reason that this issue was created, since Steve noted they are only available on Windows. ie, the point here is cross platform (Linux and MacOS Core) support for Kerberos client side functionality.

ScotMac avatar Sep 05 '19 19:09 ScotMac

@ScotMac I think maybe you misinterpreted what I said. KerberosSecurityTokenProvider is Windows-only, and AFAIK doesn't exist in Core in any form. The library I built is cross-platform (or at least intends to be though it's not rigorously tested off Windows).

You can more or less replace calls to KerberosSecurityTokenProvider with KerberosClient, but it doesn't rely on GSSAPI so the behavior and usage is different.

SteveSyfuhs avatar Sep 05 '19 19:09 SteveSyfuhs

Got it Steve, thanks.

So, your package now DOES handle client side authenticator generation? Can you please give an example of the generation (sorry if you already have it somewhere else).

ScotMac avatar Sep 05 '19 20:09 ScotMac

Also, does your package support BOTH framework and core?

ScotMac avatar Sep 05 '19 20:09 ScotMac

Yes, you can do client side authenticator generation. It's all wrapped in the KerberosClient class. Here's an E2E test that shows how it's done: https://github.com/SteveSyfuhs/Kerberos.NET/blob/master/Tests/Tests.Kerberos.NET/E2EKdcClientTest.cs#L135-L158

The gist of it is:

  1. Create a client (don't pass override KDC unless you need to pin it to a particular DC)
  2. Authenticate the client to get the TGT
  3. Request a service ticket to some SPN for that authenticated user
  4. Send the ticket to some service

It's built as netstandard2.0 so that should in principle work for both framework and core.

Here's the thing though. This doesn't have the same level of security as say Kerberos on Windows through SSPI, because it doesn't rely on LSA protecting it's secrets. This changes the security model significantly and you need to consider whether this is something to account for in your build.

SteveSyfuhs avatar Sep 05 '19 20:09 SteveSyfuhs

MIT client libraries support BOTH the ability to see/utilize the client authentication via LSA and a Kerberos5 specific local credential cache (kinit/klist and okinit/oklist for Oracle). Are you saying that you don't support the LSA portion, and only support a local credential cache file?

ScotMac avatar Sep 09 '19 19:09 ScotMac