Debian Codegraph @

README.md

raw symbols SCIP
1 # gokrb5
2 [![GoDoc](https://godoc.org/gopkg.in/jcmturner/gokrb5.v5?status.svg)](https://godoc.org/gopkg.in/jcmturner/gokrb5.v5) [![Go Report Card](https://goreportcard.com/badge/gopkg.in/jcmturner/gokrb5.v5)](https://goreportcard.com/report/gopkg.in/jcmturner/gokrb5.v5) [![Build Status](https://travis-ci.org/jcmturner/gokrb5.svg?branch=master)](https://travis-ci.org/jcmturner/gokrb5)
3
4 To get the package, execute:
5 ```
6 go get -d gopkg.in/jcmturner/gokrb5.v5/...
7 ```
8 To import this package, add the following line to your code:
9 ```go
10 import "gopkg.in/jcmturner/gokrb5.v5/<sub package>"
11 ```
12
13 ## Features
14 * Pure Go - no dependency on external libraries
15 * No platform specific code
16 * Server Side
17 * HTTP handler wrapper implements SPNEGO Kerberos authentication
18 * HTTP handler wrapper decodes Microsoft AD PAC authorization data
19 * Client Side
20 * Client that can authenticate to an SPNEGO Kerberos authenticated web service
21 * Ability to change client's password
22 * General
23 * Kerberos libraries for custom integration
24 * Parsing Keytab files
25 * Parsing krb5.conf files
26 * Parsing client credentials cache files such as `/tmp/krb5cc_$(id -u $(whoami))`
27
28 #### Implemented Encryption & Checksum Types
29
30 | Implementation | Encryption ID | Checksum ID | RFC |
31 |-------|-------------|------------|------|
32 | des3-cbc-sha1-kd | 16 | 12 | 3961 |
33 | aes128-cts-hmac-sha1-96 | 17 | 15 | 3962 |
34 | aes256-cts-hmac-sha1-96 | 18 | 16 | 3962 |
35 | aes128-cts-hmac-sha256-128 | 19 | 19 | 8009 |
36 | aes256-cts-hmac-sha384-192 | 20 | 20 | 8009 |
37 | rc4-hmac | 23 | -138 | 4757 |
38
39
40 Currently the following is working/tested:
41 * Tested against MIT KDC (1.6.3 is the oldest version tested against) and Microsoft Active Directory (Windows 2008 R2)
42 * Tested against a KDC that supports PA-FX-FAST.
43 * Tested against users that have pre-authentication required using PA-ENC-TIMESTAMP.
44 * Microsoft PAC Authorization Data is processed and exposed in the HTTP request context. Available if Microsoft Active Directory is used as the KDC.
45
46 ## Contributing
47 If you are interested in contributing to gokrb5, great! Please read the [contribution guidelines](https://github.com/jcmturner/gokrb5/blob/master/CONTRIBUTING.md).
48
49 ## Usage
50
51 ---
52
53 ### Configuration
54 The gokrb5 libraries use the same krb5.conf configuration file format as MIT Kerberos, described [here](https://web.mit.edu/kerberos/krb5-latest/doc/admin/conf_files/krb5_conf.html).
55 Config instances can be created by loading from a file path or by passing a string, io.Reader or bufio.Scanner to the relevant method:
56 ```go
57 import "gopkg.in/jcmturner/gokrb5.v5/config"
58 cfg, err := config.Load("/path/to/config/file")
59 cfg, err := config.NewConfigFromString(krb5Str) //String must have appropriate newline separations
60 cfg, err := config.NewConfigFromReader(reader)
61 cfg, err := config.NewConfigFromScanner(scanner)
62 ```
63 ### Keytab files
64 Standard keytab files can be read from a file or from a slice of bytes:
65 ```go
66 import "gopkg.in/jcmturner/gokrb5.v5/keytab"
67 ktFromFile, err := keytab.Load("/path/to/file.keytab")
68 ktFromBytes, err := keytab.Parse(b)
69
70 ```
71
72 ---
73
74 ### Kerberos Client
75 Create a client instance with either a password or a keytab:
76 ```go
77 import "gopkg.in/jcmturner/gokrb5.v5/client"
78 cl := client.NewClientWithPassword("username", "REALM.COM", "password")
79 cl := client.NewClientWithKeytab("username", "REALM.COM", kt)
80
81 ```
82 Provide configuration to the client:
83 ```go
84 cl.WithConfig(cfg)
85 ```
86 Login:
87 ```go
88 err := cl.Login()
89 ```
90 Kerberos Ticket Granting Tickets (TGT) will be automatically renewed unless the client was created from a CCache.
91
92 A client can be destroyed with the following method:
93 ```go
94 cl.Destroy()
95 ```
96
97 #### Active Directory KDC and FAST negotiation
98 Active Directory does not commonly support FAST negotiation so you will need to disable this on the client.
99 If this is the case you will see this error:
100 ```KDC did not respond appropriately to FAST negotiation```
101 To resolve this disable PA-FX-Fast on the client before performing Login() with the line below.
102 ```go
103 cl.GoKrb5Conf.DisablePAFXFast = true
104 ```
105
106 #### Authenticate to a Service
107
108 ##### HTTP SPNEGO
109 Create the HTTP request object and then call the client's SetSPNEGOHeader method passing the Service Principal Name (SPN) or to auto generate the SPN from the request object pass a null string ""
110 ```go
111 r, _ := http.NewRequest("GET", "http://host.test.gokrb5/index.html", nil)
112 spn := ""
113 cl.SetSPNEGOHeader(r, spn)
114 HTTPResp, err := http.DefaultClient.Do(r)
115 ```
116
117 ##### Generic Kerberos Client
118 To authenticate to a service a client will need to request a service ticket for a Service Principal Name (SPN) and form into an AP_REQ message along with an authenticator encrypted with the session key that was delivered from the KDC along with the service ticket.
119
120 The steps below outline how to do this.
121 * Get the service ticket and session key for the service the client is authenticating to.
122 The following method will use the client's cache either returning a valid cached ticket, renewing a cached ticket with the KDC or requesting a new ticket from the KDC.
123 Therefore the GetServiceTicket method can be continually used for the most efficient interaction with the KDC.
124 ```go
125 tkt, key, err := cl.GetServiceTicket("HTTP/host.test.gokrb5")
126 ```
127
128 The steps after this will be specific to the application protocol but it will likely involve a client/server Authentication Protocol exchange (AP exchange).
129 This will involve these steps:
130
131 * Generate a new Authenticator and generate a sequence number and subkey:
132 ```go
133 auth, _ := types.NewAuthenticator(cl.Credentials.Realm, cl.Credentials.CName)
134 etype, _ := crypto.GetEtype(key.KeyType)
135 auth.GenerateSeqNumberAndSubKey(key.KeyType, etype.GetKeyByteSize())
136 ```
137 * Set the checksum on the authenticator
138 The checksum is an application specific value. Set as follows:
139 ```go
140 auth.Cksum = types.Checksum{
141 CksumType: checksumIDint,
142 Checksum: checksumBytesSlice,
143 }
144 ```
145 * Create the AP_REQ:
146 ```go
147 APReq, err := messages.NewAPReq(tkt, key, auth)
148 ```
149
150 Now send the AP_REQ to the service. How this is done will be specific to the application use case.
151
152 #### Changing a Client Password
153 This feature uses the Microsoft Kerberos Password Change protocol (RFC 3244).
154 This is implemented in Microsoft Active Directory and in MIT krb5kdc as of version 1.7.
155 Typically the kpasswd server listens on port 464.
156
157 Below is example code for how to use this feature:
158 ```go
159 cfg, err := config.Load("/path/to/config/file")
160 if err != nil {
161 panic(err.Error())
162 }
163 kt, err := keytab.Load("/path/to/file.keytab")
164 if err != nil {
165 panic(err.Error())
166 }
167 cl := client.NewClientWithKeytab("username", "REALM.COM", kt)
168 cl.WithConfig(cfg)
169
170 ok, err := cl.ChangePasswd("newpassword")
171 if err != nil {
172 panic(err.Error())
173 }
174 if !ok {
175 panic("failed to change password")
176 }
177 ```
178
179 The client kerberos config (krb5.conf) will need to have either the kpassd_server or admin_server defined in the relevant [realms] section.
180 For example:
181 ```
182 REALM.COM = {
183 kdc = 127.0.0.1:88
184 kpasswd_server = 127.0.0.1:464
185 default_domain = realm.com
186 }
187 ```
188 See https://web.mit.edu/kerberos/krb5-latest/doc/admin/conf_files/krb5_conf.html#realms for more information.
189
190 ---
191
192 ### Kerberised Service
193
194 #### SPNEGO/Kerberos HTTP Service
195 A HTTP handler wrapper can be used to implement Kerberos SPNEGO authentication for web services.
196 To configure the wrapper the keytab for the SPN and a Logger are required:
197 ```go
198 kt, err := keytab.Load("/path/to/file.keytab")
199 l := log.New(os.Stderr, "GOKRB5 Service: ", log.Ldate|log.Ltime|log.Lshortfile)
200 ```
201 Create a handler function of the application's handling method (apphandler in the example below):
202 ```go
203 h := http.HandlerFunc(apphandler)
204 ```
205 Configure the HTTP handler:
206 ```go
207 serviceAccountName = ""
208 http.Handler("/", service.SPNEGOKRB5Authenticate(h, kt, serviceAccountName, l))
209 ```
210 The serviceAccountName needs to be defined when using Active Directory where the SPN is mapped to a user account.
211 If this is not required it should be set to an empty string "".
212 If authentication succeeds then the request's context will have the following values added so they can be accessed within the application's handler:
213 * service.CTXKeyAuthenticated - Boolean indicating if the user is authenticated. Use of this value should also handle that this value may not be set and should assume "false" in that case.
214 * service.CTXKeyCredentials - The authenticated user's credentials.
215 If Microsoft Active Directory is used as the KDC then additional ADCredentials are available in the credentials.Attributes map under the key credentials.AttributeKeyADCredentials. For example the SIDs of the users group membership are available and can be used by your application for authorization.
216
217 Access the credentials within your application:
218 ```go
219 ctx := r.Context()
220 if validuser, ok := ctx.Value(service.CTXKeyAuthenticated).(bool); ok && validuser {
221 if creds, ok := ctx.Value(service.CTXKeyCredentials).(credentials.Credentials); ok {
222 if ADCreds, ok := creds.Attributes[credentials.AttributeKeyADCredentials].(credentials.ADCredentials); ok {
223 // Now access the fields of the ADCredentials struct. For example:
224 groupSids := ADCreds.GroupMembershipSIDs
225 }
226 }
227
228 }
229 ```
230
231 #### Generic Kerberised Service - Validating Client Details
232 To validate the AP_REQ sent by the client on the service side call this method:
233 ```go
234 import "gopkg.in/jcmturner/gokrb5.v5/service"
235 var ktprinc string //The SPN of the service to find the key in the keytab.
236 var requireHostAddr bool //Whether to force requiring the ticket to contain host addresses to check the client against.
237 if ok, creds, err := service.ValidateAPREQ(mt.APReq, kt, ktprinc, r.RemoteAddr, requireHostAddr); ok {
238 // Perform application specifc actions
239 // creds object has details about the client identity
240 }
241 ```
242
243 ---
244
245 ## References
246 * [RFC 3244 Microsoft Windows 2000 Kerberos Change Password and Set Password Protocols](https://tools.ietf.org/html/rfc3244)
247 * [RFC 4120 The Kerberos Network Authentication Service (V5)](https://tools.ietf.org/html/rfc4120)
248 * [RFC 3961 Encryption and Checksum Specifications for Kerberos 5](https://tools.ietf.org/html/rfc3961)
249 * [RFC 3962 Advanced Encryption Standard (AES) Encryption for Kerberos 5](https://tools.ietf.org/html/rfc3962)
250 * [RFC 4121 The Kerberos Version 5 GSS-API Mechanism](https://tools.ietf.org/html/rfc4121)
251 * [RFC 4178 The Simple and Protected Generic Security Service Application Program Interface (GSS-API) Negotiation Mechanism](https://tools.ietf.org/html/rfc4178.html)
252 * [RFC 4559 SPNEGO-based Kerberos and NTLM HTTP Authentication in Microsoft Windows](https://tools.ietf.org/html/rfc4559.html)
253 * [RFC 4757 The RC4-HMAC Kerberos Encryption Types Used by Microsoft Windows](https://tools.ietf.org/html/rfc4757)
254 * [RFC 6806 Kerberos Principal Name Canonicalization and Cross-Realm Referrals](https://tools.ietf.org/html/rfc6806.html)
255 * [RFC 6113 A Generalized Framework for Kerberos Pre-Authentication](https://tools.ietf.org/html/rfc6113.html)
256 * [RFC 8009 AES Encryption with HMAC-SHA2 for Kerberos 5](https://tools.ietf.org/html/rfc8009)
257 * [IANA Assigned Kerberos Numbers](http://www.iana.org/assignments/kerberos-parameters/kerberos-parameters.xhtml)
258 * [HTTP-Based Cross-Platform Authentication by Using the Negotiate Protocol - Part 1](https://msdn.microsoft.com/en-us/library/ms995329.aspx)
259 * [HTTP-Based Cross-Platform Authentication by Using the Negotiate Protocol - Part 2](https://msdn.microsoft.com/en-us/library/ms995330.aspx)
260 * [Microsoft PAC Validation](https://blogs.msdn.microsoft.com/openspecification/2009/04/24/understanding-microsoft-kerberos-pac-validation/)
261 * [Microsoft Kerberos Protocol Extensions](https://msdn.microsoft.com/en-us/library/cc233855.aspx)
262 * [Windows Data Types](https://msdn.microsoft.com/en-us/library/cc230273.aspx)
263
264 ### Useful Links
265 * https://en.wikipedia.org/wiki/Ciphertext_stealing#CBC_ciphertext_stealing
266
267 ## Thanks
268 * Greg Hudson from the MIT Consortium for Kerberos and Internet Trust for providing useful advice.
269
270 ## Contributing
271 Thank you for your interest in contributing to gokrb5 please read the
272 [contribution guide](https://github.com/jcmturner/gokrb5/blob/master/CONTRIBUTING.md) as it should help you get started.
273
274 ## Known Issues
275 | Issue | Worked around? | References |
276 |-------|-------------|------------|
277 | The Go standard library's encoding/asn1 package cannot unmarshal into slice of asn1.RawValue | Yes | https://github.com/golang/go/issues/17321 |
278 | The Go standard library's encoding/asn1 package cannot marshal into a GeneralString | Yes - using https://github.com/jcmturner/gofork/tree/master/encoding/asn1 | https://github.com/golang/go/issues/18832 |
279 | The Go standard library's encoding/asn1 package cannot marshal into slice of strings and pass stringtype parameter tags to members | Yes - using https://github.com/jcmturner/gofork/tree/master/encoding/asn1 | https://github.com/golang/go/issues/18834 |
280 | The Go standard library's encoding/asn1 package cannot marshal with application tags | Yes | |
281 | The Go standard library's x/crypto/pbkdf2.Key function uses the int type for iteraction count limiting meaning the 4294967296 count specified in https://tools.ietf.org/html/rfc3962 section 4 cannot be met on 32bit systems | Yes - using https://github.com/jcmturner/gofork/tree/master/x/crypto/pbkdf2 | https://go-review.googlesource.com/c/crypto/+/85535 |