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