|
| 1 | +package gssapi |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + |
| 6 | + "github.com/jcmturner/gokrb5/v8/client" |
| 7 | + "github.com/jcmturner/gokrb5/v8/config" |
| 8 | + "github.com/jcmturner/gokrb5/v8/keytab" |
| 9 | + "github.com/jcmturner/gokrb5/v8/types" |
| 10 | + |
| 11 | + "github.com/jcmturner/gokrb5/v8/gssapi" |
| 12 | + "github.com/jcmturner/gokrb5/v8/spnego" |
| 13 | + |
| 14 | + "github.com/jcmturner/gokrb5/v8/crypto" |
| 15 | + "github.com/jcmturner/gokrb5/v8/iana/keyusage" |
| 16 | + "github.com/jcmturner/gokrb5/v8/messages" |
| 17 | + |
| 18 | + "github.com/jcmturner/gokrb5/v8/credentials" |
| 19 | +) |
| 20 | + |
| 21 | +// Client implements ldap.GSSAPIClient interface. |
| 22 | +type Client struct { |
| 23 | + *client.Client |
| 24 | + |
| 25 | + ekey types.EncryptionKey |
| 26 | + Subkey types.EncryptionKey |
| 27 | +} |
| 28 | + |
| 29 | +// NewClientWithKeytab creates a new client from a keytab credential. |
| 30 | +// Set the realm to empty string to use the default realm from config. |
| 31 | +func NewClientWithKeytab(username, realm, keytabPath, krb5confPath string, settings ...func(*client.Settings)) (*Client, error) { |
| 32 | + krb5conf, err := config.Load(krb5confPath) |
| 33 | + if err != nil { |
| 34 | + return nil, err |
| 35 | + } |
| 36 | + |
| 37 | + keytab, err := keytab.Load(keytabPath) |
| 38 | + if err != nil { |
| 39 | + return nil, err |
| 40 | + } |
| 41 | + |
| 42 | + client := client.NewWithKeytab(username, realm, keytab, krb5conf, settings...) |
| 43 | + |
| 44 | + return &Client{ |
| 45 | + Client: client, |
| 46 | + }, nil |
| 47 | +} |
| 48 | + |
| 49 | +// NewClientWithPassword creates a new client from a password credential. |
| 50 | +// Set the realm to empty string to use the default realm from config. |
| 51 | +func NewClientWithPassword(username, realm, password string, krb5confPath string, settings ...func(*client.Settings)) (*Client, error) { |
| 52 | + krb5conf, err := config.Load(krb5confPath) |
| 53 | + if err != nil { |
| 54 | + return nil, err |
| 55 | + } |
| 56 | + |
| 57 | + client := client.NewWithPassword(username, realm, password, krb5conf, settings...) |
| 58 | + |
| 59 | + return &Client{ |
| 60 | + Client: client, |
| 61 | + }, nil |
| 62 | +} |
| 63 | + |
| 64 | +// NewClientFromCCache creates a new client from a populated client cache. |
| 65 | +func NewClientFromCCache(ccachePath, krb5confPath string, settings ...func(*client.Settings)) (*Client, error) { |
| 66 | + krb5conf, err := config.Load(krb5confPath) |
| 67 | + if err != nil { |
| 68 | + return nil, err |
| 69 | + } |
| 70 | + |
| 71 | + ccache, err := credentials.LoadCCache(ccachePath) |
| 72 | + if err != nil { |
| 73 | + return nil, err |
| 74 | + } |
| 75 | + |
| 76 | + client, err := client.NewFromCCache(ccache, krb5conf, settings...) |
| 77 | + if err != nil { |
| 78 | + return nil, err |
| 79 | + } |
| 80 | + |
| 81 | + return &Client{ |
| 82 | + Client: client, |
| 83 | + }, nil |
| 84 | +} |
| 85 | + |
| 86 | +// Close deletes any established secure context and closes the client. |
| 87 | +func (client *Client) Close() error { |
| 88 | + client.Client.Destroy() |
| 89 | + return nil |
| 90 | +} |
| 91 | + |
| 92 | +// DeleteSecContext destroys any established secure context. |
| 93 | +func (client *Client) DeleteSecContext() error { |
| 94 | + client.ekey = types.EncryptionKey{} |
| 95 | + client.Subkey = types.EncryptionKey{} |
| 96 | + return nil |
| 97 | +} |
| 98 | + |
| 99 | +// InitSecContext initiates the establishment of a security context for |
| 100 | +// GSS-API between the client and server. |
| 101 | +// See RFC 4752 section 3.1. |
| 102 | +func (client *Client) InitSecContext(target string, input []byte) ([]byte, bool, error) { |
| 103 | + gssapiFlags := []int{gssapi.ContextFlagInteg, gssapi.ContextFlagConf, gssapi.ContextFlagMutual} |
| 104 | + |
| 105 | + switch input { |
| 106 | + case nil: |
| 107 | + tkt, ekey, err := client.Client.GetServiceTicket(target) |
| 108 | + if err != nil { |
| 109 | + return nil, false, err |
| 110 | + } |
| 111 | + client.ekey = ekey |
| 112 | + |
| 113 | + token, err := spnego.NewKRB5TokenAPREQ(client.Client, tkt, ekey, gssapiFlags, []int{}) |
| 114 | + if err != nil { |
| 115 | + return nil, false, err |
| 116 | + } |
| 117 | + |
| 118 | + output, err := token.Marshal() |
| 119 | + if err != nil { |
| 120 | + return nil, false, err |
| 121 | + } |
| 122 | + |
| 123 | + return output, true, nil |
| 124 | + |
| 125 | + default: |
| 126 | + var token spnego.KRB5Token |
| 127 | + |
| 128 | + err := token.Unmarshal(input) |
| 129 | + if err != nil { |
| 130 | + return nil, false, err |
| 131 | + } |
| 132 | + |
| 133 | + var completed bool |
| 134 | + |
| 135 | + if token.IsAPRep() { |
| 136 | + completed = true |
| 137 | + |
| 138 | + encpart, err := crypto.DecryptEncPart(token.APRep.EncPart, client.ekey, keyusage.AP_REP_ENCPART) |
| 139 | + if err != nil { |
| 140 | + return nil, false, err |
| 141 | + } |
| 142 | + |
| 143 | + part := &messages.EncAPRepPart{} |
| 144 | + |
| 145 | + if err = part.Unmarshal(encpart); err != nil { |
| 146 | + return nil, false, err |
| 147 | + } |
| 148 | + client.Subkey = part.Subkey |
| 149 | + } |
| 150 | + |
| 151 | + if token.IsKRBError() { |
| 152 | + return nil, !false, token.KRBError |
| 153 | + } |
| 154 | + |
| 155 | + return make([]byte, 0), !completed, nil |
| 156 | + } |
| 157 | +} |
| 158 | + |
| 159 | +// NegotiateSaslAuth performs the last step of the SASL handshake. |
| 160 | +// See RFC 4752 section 3.1. |
| 161 | +func (client *Client) NegotiateSaslAuth(input []byte, authzid string) ([]byte, error) { |
| 162 | + token := &gssapi.WrapToken{} |
| 163 | + err := token.Unmarshal(input, true) |
| 164 | + if err != nil { |
| 165 | + return nil, err |
| 166 | + } |
| 167 | + |
| 168 | + if (token.Flags & 0b1) == 0 { |
| 169 | + return nil, fmt.Errorf("got a Wrapped token that's not from the server") |
| 170 | + } |
| 171 | + |
| 172 | + key := client.ekey |
| 173 | + if (token.Flags & 0b100) != 0 { |
| 174 | + key = client.Subkey |
| 175 | + } |
| 176 | + |
| 177 | + _, err = token.Verify(key, keyusage.GSSAPI_ACCEPTOR_SEAL) |
| 178 | + if err != nil { |
| 179 | + return nil, err |
| 180 | + } |
| 181 | + |
| 182 | + pl := token.Payload |
| 183 | + if len(pl) != 4 { |
| 184 | + return nil, fmt.Errorf("server send bad final token for SASL GSSAPI Handshake") |
| 185 | + } |
| 186 | + |
| 187 | + // We never want a security layer |
| 188 | + b := [4]byte{0, 0, 0, 0} |
| 189 | + payload := append(b[:], []byte(authzid)...) |
| 190 | + |
| 191 | + encType, err := crypto.GetEtype(key.KeyType) |
| 192 | + if err != nil { |
| 193 | + return nil, err |
| 194 | + } |
| 195 | + |
| 196 | + token = &gssapi.WrapToken{ |
| 197 | + Flags: 0b100, |
| 198 | + EC: uint16(encType.GetHMACBitLength() / 8), |
| 199 | + RRC: 0, |
| 200 | + SndSeqNum: 1, |
| 201 | + Payload: payload, |
| 202 | + } |
| 203 | + |
| 204 | + if err := token.SetCheckSum(key, keyusage.GSSAPI_INITIATOR_SEAL); err != nil { |
| 205 | + return nil, err |
| 206 | + } |
| 207 | + |
| 208 | + output, err := token.Marshal() |
| 209 | + if err != nil { |
| 210 | + return nil, err |
| 211 | + } |
| 212 | + |
| 213 | + return output, nil |
| 214 | +} |
0 commit comments