| 1 |
// Copyright (C) MongoDB, Inc. 2017-present. |
| 2 |
// |
| 3 |
// Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 4 |
// not use this file except in compliance with the License. You may obtain |
| 5 |
// a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 |
| 6 |
|
| 7 |
//go:build gssapi && windows |
| 8 |
// +build gssapi,windows |
| 9 |
|
| 10 |
package gssapi |
| 11 |
|
| 12 |
// #include "sspi_wrapper.h" |
| 13 |
import "C" |
| 14 |
import ( |
| 15 |
"context" |
| 16 |
"fmt" |
| 17 |
"net" |
| 18 |
"strconv" |
| 19 |
"strings" |
| 20 |
"sync" |
| 21 |
"unsafe" |
| 22 |
) |
| 23 |
|
| 24 |
// New creates a new SaslClient. The target parameter should be a hostname with no port. |
| 25 |
func New(target, username, password string, passwordSet bool, props map[string]string) (*SaslClient, error) { |
| 26 |
initOnce.Do(initSSPI) |
| 27 |
if initError != nil { |
| 28 |
return nil, initError |
| 29 |
} |
| 30 |
|
| 31 |
var err error |
| 32 |
serviceName := "mongodb" |
| 33 |
serviceRealm := "" |
| 34 |
canonicalizeHostName := false |
| 35 |
var serviceHostSet bool |
| 36 |
|
| 37 |
for key, value := range props { |
| 38 |
switch strings.ToUpper(key) { |
| 39 |
case "CANONICALIZE_HOST_NAME": |
| 40 |
canonicalizeHostName, err = strconv.ParseBool(value) |
| 41 |
if err != nil { |
| 42 |
return nil, fmt.Errorf("%s must be a boolean (true, false, 0, 1) but got '%s'", key, value) |
| 43 |
} |
| 44 |
|
| 45 |
case "SERVICE_REALM": |
| 46 |
serviceRealm = value |
| 47 |
case "SERVICE_NAME": |
| 48 |
serviceName = value |
| 49 |
case "SERVICE_HOST": |
| 50 |
serviceHostSet = true |
| 51 |
target = value |
| 52 |
} |
| 53 |
} |
| 54 |
|
| 55 |
if canonicalizeHostName { |
| 56 |
// Should not canonicalize the SERVICE_HOST |
| 57 |
if serviceHostSet { |
| 58 |
return nil, fmt.Errorf("CANONICALIZE_HOST_NAME and SERVICE_HOST canonot both be specified") |
| 59 |
} |
| 60 |
|
| 61 |
names, err := net.LookupAddr(target) |
| 62 |
if err != nil || len(names) == 0 { |
| 63 |
return nil, fmt.Errorf("unable to canonicalize hostname: %s", err) |
| 64 |
} |
| 65 |
target = names[0] |
| 66 |
if target[len(target)-1] == '.' { |
| 67 |
target = target[:len(target)-1] |
| 68 |
} |
| 69 |
} |
| 70 |
|
| 71 |
servicePrincipalName := fmt.Sprintf("%s/%s", serviceName, target) |
| 72 |
if serviceRealm != "" { |
| 73 |
servicePrincipalName += "@" + serviceRealm |
| 74 |
} |
| 75 |
|
| 76 |
return &SaslClient{ |
| 77 |
servicePrincipalName: servicePrincipalName, |
| 78 |
username: username, |
| 79 |
password: password, |
| 80 |
passwordSet: passwordSet, |
| 81 |
}, nil |
| 82 |
} |
| 83 |
|
| 84 |
type SaslClient struct { |
| 85 |
servicePrincipalName string |
| 86 |
username string |
| 87 |
password string |
| 88 |
passwordSet bool |
| 89 |
|
| 90 |
// state |
| 91 |
state C.sspi_client_state |
| 92 |
contextComplete bool |
| 93 |
done bool |
| 94 |
} |
| 95 |
|
| 96 |
func (sc *SaslClient) Close() { |
| 97 |
C.sspi_client_destroy(&sc.state) |
| 98 |
} |
| 99 |
|
| 100 |
func (sc *SaslClient) Start() (string, []byte, error) { |
| 101 |
const mechName = "GSSAPI" |
| 102 |
|
| 103 |
var cusername *C.char |
| 104 |
var cpassword *C.char |
| 105 |
if sc.username != "" { |
| 106 |
cusername = C.CString(sc.username) |
| 107 |
defer C.free(unsafe.Pointer(cusername)) |
| 108 |
if sc.passwordSet { |
| 109 |
cpassword = C.CString(sc.password) |
| 110 |
defer C.free(unsafe.Pointer(cpassword)) |
| 111 |
} |
| 112 |
} |
| 113 |
status := C.sspi_client_init(&sc.state, cusername, cpassword) |
| 114 |
|
| 115 |
if status != C.SSPI_OK { |
| 116 |
return mechName, nil, sc.getError("unable to initialize client") |
| 117 |
} |
| 118 |
|
| 119 |
payload, err := sc.Next(nil, nil) |
| 120 |
|
| 121 |
return mechName, payload, err |
| 122 |
} |
| 123 |
|
| 124 |
func (sc *SaslClient) Next(_ context.Context, challenge []byte) ([]byte, error) { |
| 125 |
|
| 126 |
var outBuf C.PVOID |
| 127 |
var outBufLen C.ULONG |
| 128 |
|
| 129 |
if sc.contextComplete { |
| 130 |
if sc.username == "" { |
| 131 |
var cusername *C.char |
| 132 |
status := C.sspi_client_username(&sc.state, &cusername) |
| 133 |
if status != C.SSPI_OK { |
| 134 |
return nil, sc.getError("unable to acquire username") |
| 135 |
} |
| 136 |
defer C.free(unsafe.Pointer(cusername)) |
| 137 |
sc.username = C.GoString((*C.char)(unsafe.Pointer(cusername))) |
| 138 |
} |
| 139 |
|
| 140 |
bytes := append([]byte{1, 0, 0, 0}, []byte(sc.username)...) |
| 141 |
buf := (C.PVOID)(unsafe.Pointer(&bytes[0])) |
| 142 |
bufLen := C.ULONG(len(bytes)) |
| 143 |
status := C.sspi_client_wrap_msg(&sc.state, buf, bufLen, &outBuf, &outBufLen) |
| 144 |
if status != C.SSPI_OK { |
| 145 |
return nil, sc.getError("unable to wrap authz") |
| 146 |
} |
| 147 |
|
| 148 |
sc.done = true |
| 149 |
} else { |
| 150 |
var buf C.PVOID |
| 151 |
var bufLen C.ULONG |
| 152 |
if len(challenge) > 0 { |
| 153 |
buf = (C.PVOID)(unsafe.Pointer(&challenge[0])) |
| 154 |
bufLen = C.ULONG(len(challenge)) |
| 155 |
} |
| 156 |
cservicePrincipalName := C.CString(sc.servicePrincipalName) |
| 157 |
defer C.free(unsafe.Pointer(cservicePrincipalName)) |
| 158 |
|
| 159 |
status := C.sspi_client_negotiate(&sc.state, cservicePrincipalName, buf, bufLen, &outBuf, &outBufLen) |
| 160 |
switch status { |
| 161 |
case C.SSPI_OK: |
| 162 |
sc.contextComplete = true |
| 163 |
case C.SSPI_CONTINUE: |
| 164 |
default: |
| 165 |
return nil, sc.getError("unable to negotiate with server") |
| 166 |
} |
| 167 |
} |
| 168 |
|
| 169 |
if outBuf != C.PVOID(nil) { |
| 170 |
defer C.free(unsafe.Pointer(outBuf)) |
| 171 |
} |
| 172 |
|
| 173 |
return C.GoBytes(unsafe.Pointer(outBuf), C.int(outBufLen)), nil |
| 174 |
} |
| 175 |
|
| 176 |
func (sc *SaslClient) Completed() bool { |
| 177 |
return sc.done |
| 178 |
} |
| 179 |
|
| 180 |
func (sc *SaslClient) getError(prefix string) error { |
| 181 |
return getError(prefix, sc.state.status) |
| 182 |
} |
| 183 |
|
| 184 |
var initOnce sync.Once |
| 185 |
var initError error |
| 186 |
|
| 187 |
func initSSPI() { |
| 188 |
rc := C.sspi_init() |
| 189 |
if rc != 0 { |
| 190 |
initError = fmt.Errorf("error initializing sspi: %v", rc) |
| 191 |
} |
| 192 |
} |
| 193 |
|
| 194 |
func getError(prefix string, status C.SECURITY_STATUS) error { |
| 195 |
var s string |
| 196 |
switch status { |
| 197 |
case C.SEC_E_ALGORITHM_MISMATCH: |
| 198 |
s = "The client and server cannot communicate because they do not possess a common algorithm." |
| 199 |
case C.SEC_E_BAD_BINDINGS: |
| 200 |
s = "The SSPI channel bindings supplied by the client are incorrect." |
| 201 |
case C.SEC_E_BAD_PKGID: |
| 202 |
s = "The requested package identifier does not exist." |
| 203 |
case C.SEC_E_BUFFER_TOO_SMALL: |
| 204 |
s = "The buffers supplied to the function are not large enough to contain the information." |
| 205 |
case C.SEC_E_CANNOT_INSTALL: |
| 206 |
s = "The security package cannot initialize successfully and should not be installed." |
| 207 |
case C.SEC_E_CANNOT_PACK: |
| 208 |
s = "The package is unable to pack the context." |
| 209 |
case C.SEC_E_CERT_EXPIRED: |
| 210 |
s = "The received certificate has expired." |
| 211 |
case C.SEC_E_CERT_UNKNOWN: |
| 212 |
s = "An unknown error occurred while processing the certificate." |
| 213 |
case C.SEC_E_CERT_WRONG_USAGE: |
| 214 |
s = "The certificate is not valid for the requested usage." |
| 215 |
case C.SEC_E_CONTEXT_EXPIRED: |
| 216 |
s = "The application is referencing a context that has already been closed. A properly written application should not receive this error." |
| 217 |
case C.SEC_E_CROSSREALM_DELEGATION_FAILURE: |
| 218 |
s = "The server attempted to make a Kerberos-constrained delegation request for a target outside the server's realm." |
| 219 |
case C.SEC_E_CRYPTO_SYSTEM_INVALID: |
| 220 |
s = "The cryptographic system or checksum function is not valid because a required function is unavailable." |
| 221 |
case C.SEC_E_DECRYPT_FAILURE: |
| 222 |
s = "The specified data could not be decrypted." |
| 223 |
case C.SEC_E_DELEGATION_REQUIRED: |
| 224 |
s = "The requested operation cannot be completed. The computer must be trusted for delegation" |
| 225 |
case C.SEC_E_DOWNGRADE_DETECTED: |
| 226 |
s = "The system detected a possible attempt to compromise security. Verify that the server that authenticated you can be contacted." |
| 227 |
case C.SEC_E_ENCRYPT_FAILURE: |
| 228 |
s = "The specified data could not be encrypted." |
| 229 |
case C.SEC_E_ILLEGAL_MESSAGE: |
| 230 |
s = "The message received was unexpected or badly formatted." |
| 231 |
case C.SEC_E_INCOMPLETE_CREDENTIALS: |
| 232 |
s = "The credentials supplied were not complete and could not be verified. The context could not be initialized." |
| 233 |
case C.SEC_E_INCOMPLETE_MESSAGE: |
| 234 |
s = "The message supplied was incomplete. The signature was not verified." |
| 235 |
case C.SEC_E_INSUFFICIENT_MEMORY: |
| 236 |
s = "Not enough memory is available to complete the request." |
| 237 |
case C.SEC_E_INTERNAL_ERROR: |
| 238 |
s = "An error occurred that did not map to an SSPI error code." |
| 239 |
case C.SEC_E_INVALID_HANDLE: |
| 240 |
s = "The handle passed to the function is not valid." |
| 241 |
case C.SEC_E_INVALID_TOKEN: |
| 242 |
s = "The token passed to the function is not valid." |
| 243 |
case C.SEC_E_ISSUING_CA_UNTRUSTED: |
| 244 |
s = "An untrusted certification authority (CA) was detected while processing the smart card certificate used for authentication." |
| 245 |
case C.SEC_E_ISSUING_CA_UNTRUSTED_KDC: |
| 246 |
s = "An untrusted CA was detected while processing the domain controller certificate used for authentication. The system event log contains additional information." |
| 247 |
case C.SEC_E_KDC_CERT_EXPIRED: |
| 248 |
s = "The domain controller certificate used for smart card logon has expired." |
| 249 |
case C.SEC_E_KDC_CERT_REVOKED: |
| 250 |
s = "The domain controller certificate used for smart card logon has been revoked." |
| 251 |
case C.SEC_E_KDC_INVALID_REQUEST: |
| 252 |
s = "A request that is not valid was sent to the KDC." |
| 253 |
case C.SEC_E_KDC_UNABLE_TO_REFER: |
| 254 |
s = "The KDC was unable to generate a referral for the service requested." |
| 255 |
case C.SEC_E_KDC_UNKNOWN_ETYPE: |
| 256 |
s = "The requested encryption type is not supported by the KDC." |
| 257 |
case C.SEC_E_LOGON_DENIED: |
| 258 |
s = "The logon has been denied" |
| 259 |
case C.SEC_E_MAX_REFERRALS_EXCEEDED: |
| 260 |
s = "The number of maximum ticket referrals has been exceeded." |
| 261 |
case C.SEC_E_MESSAGE_ALTERED: |
| 262 |
s = "The message supplied for verification has been altered." |
| 263 |
case C.SEC_E_MULTIPLE_ACCOUNTS: |
| 264 |
s = "The received certificate was mapped to multiple accounts." |
| 265 |
case C.SEC_E_MUST_BE_KDC: |
| 266 |
s = "The local computer must be a Kerberos domain controller (KDC)" |
| 267 |
case C.SEC_E_NO_AUTHENTICATING_AUTHORITY: |
| 268 |
s = "No authority could be contacted for authentication." |
| 269 |
case C.SEC_E_NO_CREDENTIALS: |
| 270 |
s = "No credentials are available." |
| 271 |
case C.SEC_E_NO_IMPERSONATION: |
| 272 |
s = "No impersonation is allowed for this context." |
| 273 |
case C.SEC_E_NO_IP_ADDRESSES: |
| 274 |
s = "Unable to accomplish the requested task because the local computer does not have any IP addresses." |
| 275 |
case C.SEC_E_NO_KERB_KEY: |
| 276 |
s = "No Kerberos key was found." |
| 277 |
case C.SEC_E_NO_PA_DATA: |
| 278 |
s = "Policy administrator (PA) data is needed to determine the encryption type" |
| 279 |
case C.SEC_E_NO_S4U_PROT_SUPPORT: |
| 280 |
s = "The Kerberos subsystem encountered an error. A service for user protocol request was made against a domain controller which does not support service for a user." |
| 281 |
case C.SEC_E_NO_TGT_REPLY: |
| 282 |
s = "The client is trying to negotiate a context and the server requires a user-to-user connection" |
| 283 |
case C.SEC_E_NOT_OWNER: |
| 284 |
s = "The caller of the function does not own the credentials." |
| 285 |
case C.SEC_E_OK: |
| 286 |
s = "The operation completed successfully." |
| 287 |
case C.SEC_E_OUT_OF_SEQUENCE: |
| 288 |
s = "The message supplied for verification is out of sequence." |
| 289 |
case C.SEC_E_PKINIT_CLIENT_FAILURE: |
| 290 |
s = "The smart card certificate used for authentication is not trusted." |
| 291 |
case C.SEC_E_PKINIT_NAME_MISMATCH: |
| 292 |
s = "The client certificate does not contain a valid UPN or does not match the client name in the logon request." |
| 293 |
case C.SEC_E_QOP_NOT_SUPPORTED: |
| 294 |
s = "The quality of protection attribute is not supported by this package." |
| 295 |
case C.SEC_E_REVOCATION_OFFLINE_C: |
| 296 |
s = "The revocation status of the smart card certificate used for authentication could not be determined." |
| 297 |
case C.SEC_E_REVOCATION_OFFLINE_KDC: |
| 298 |
s = "The revocation status of the domain controller certificate used for smart card authentication could not be determined. The system event log contains additional information." |
| 299 |
case C.SEC_E_SECPKG_NOT_FOUND: |
| 300 |
s = "The security package was not recognized." |
| 301 |
case C.SEC_E_SECURITY_QOS_FAILED: |
| 302 |
s = "The security context could not be established due to a failure in the requested quality of service (for example" |
| 303 |
case C.SEC_E_SHUTDOWN_IN_PROGRESS: |
| 304 |
s = "A system shutdown is in progress." |
| 305 |
case C.SEC_E_SMARTCARD_CERT_EXPIRED: |
| 306 |
s = "The smart card certificate used for authentication has expired." |
| 307 |
case C.SEC_E_SMARTCARD_CERT_REVOKED: |
| 308 |
s = "The smart card certificate used for authentication has been revoked. Additional information may exist in the event log." |
| 309 |
case C.SEC_E_SMARTCARD_LOGON_REQUIRED: |
| 310 |
s = "Smart card logon is required and was not used." |
| 311 |
case C.SEC_E_STRONG_CRYPTO_NOT_SUPPORTED: |
| 312 |
s = "The other end of the security negotiation requires strong cryptography" |
| 313 |
case C.SEC_E_TARGET_UNKNOWN: |
| 314 |
s = "The target was not recognized." |
| 315 |
case C.SEC_E_TIME_SKEW: |
| 316 |
s = "The clocks on the client and server computers do not match." |
| 317 |
case C.SEC_E_TOO_MANY_PRINCIPALS: |
| 318 |
s = "The KDC reply contained more than one principal name." |
| 319 |
case C.SEC_E_UNFINISHED_CONTEXT_DELETED: |
| 320 |
s = "A security context was deleted before the context was completed. This is considered a logon failure." |
| 321 |
case C.SEC_E_UNKNOWN_CREDENTIALS: |
| 322 |
s = "The credentials provided were not recognized." |
| 323 |
case C.SEC_E_UNSUPPORTED_FUNCTION: |
| 324 |
s = "The requested function is not supported." |
| 325 |
case C.SEC_E_UNSUPPORTED_PREAUTH: |
| 326 |
s = "An unsupported preauthentication mechanism was presented to the Kerberos package." |
| 327 |
case C.SEC_E_UNTRUSTED_ROOT: |
| 328 |
s = "The certificate chain was issued by an authority that is not trusted." |
| 329 |
case C.SEC_E_WRONG_CREDENTIAL_HANDLE: |
| 330 |
s = "The supplied credential handle does not match the credential associated with the security context." |
| 331 |
case C.SEC_E_WRONG_PRINCIPAL: |
| 332 |
s = "The target principal name is incorrect." |
| 333 |
case C.SEC_I_COMPLETE_AND_CONTINUE: |
| 334 |
s = "The function completed successfully" |
| 335 |
case C.SEC_I_COMPLETE_NEEDED: |
| 336 |
s = "The function completed successfully" |
| 337 |
case C.SEC_I_CONTEXT_EXPIRED: |
| 338 |
s = "The message sender has finished using the connection and has initiated a shutdown. For information about initiating or recognizing a shutdown" |
| 339 |
case C.SEC_I_CONTINUE_NEEDED: |
| 340 |
s = "The function completed successfully" |
| 341 |
case C.SEC_I_INCOMPLETE_CREDENTIALS: |
| 342 |
s = "The credentials supplied were not complete and could not be verified. Additional information can be returned from the context." |
| 343 |
case C.SEC_I_LOCAL_LOGON: |
| 344 |
s = "The logon was completed" |
| 345 |
case C.SEC_I_NO_LSA_CONTEXT: |
| 346 |
s = "There is no LSA mode context associated with this context." |
| 347 |
case C.SEC_I_RENEGOTIATE: |
| 348 |
s = "The context data must be renegotiated with the peer." |
| 349 |
default: |
| 350 |
return fmt.Errorf("%s: 0x%x", prefix, uint32(status)) |
| 351 |
} |
| 352 |
|
| 353 |
return fmt.Errorf("%s: %s(0x%x)", prefix, s, uint32(status)) |
| 354 |
} |