This repository has been archived by the owner on Mar 1, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
/
osxkeychain.go
321 lines (274 loc) · 9.97 KB
/
osxkeychain.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
package osxkeychain
// See https://developer.apple.com/library/mac/documentation/Security/Reference/keychainservices/index.html for the APIs used below.
// Also see https://developer.apple.com/library/ios/documentation/Security/Conceptual/keychainServConcepts/01introduction/introduction.html .
/*
#cgo CFLAGS: -mmacosx-version-min=10.6 -D__MAC_OS_X_VERSION_MAX_ALLOWED=1060
#cgo LDFLAGS: -framework CoreFoundation -framework Security
#include <stdlib.h>
#include <CoreFoundation/CoreFoundation.h>
#include <Security/Security.h>
*/
import "C"
import (
"errors"
"fmt"
"math"
"unicode/utf8"
"unsafe"
)
type ProtocolType C.SecProtocolType
// TODO: Fill this out.
const (
ProtocolHTTP ProtocolType = C.kSecProtocolTypeHTTP
ProtocolHTTPS = C.kSecProtocolTypeHTTPS
ProtocolAny = C.kSecProtocolTypeAny
)
type AuthenticationType C.SecAuthenticationType
// TODO: Fill this out.
const (
AuthenticationHTTPBasic AuthenticationType = C.kSecAuthenticationTypeHTTPBasic
AuthenticationDefault = C.kSecAuthenticationTypeDefault
AuthenticationAny = C.kSecAuthenticationTypeAny
)
// A password for an Internet server, such as a Web or FTP server. Internet
// password items on the keychain include attributes such as the security domain
// and IP address.
//
// All string fields must have size that fits in 32 bits. All string
// fields except for Password must be encoded in UTF-8.
type InternetPassword struct {
ServerName string
SecurityDomain string
AccountName string
Path string
Port int // Use 0 to ignore
Password string
Protocol ProtocolType
AuthType AuthenticationType
}
func check32Bit(paramName, paramValue string) error {
if uint64(len(paramValue)) > math.MaxUint32 {
return errors.New(paramName + " has size overflowing 32 bits")
}
return nil
}
func check32BitUTF8(paramName, paramValue string) error {
if err := check32Bit(paramName, paramValue); err != nil {
return err
}
if !utf8.ValidString(paramValue) {
return errors.New(paramName + " is not a valid UTF-8 string")
}
return nil
}
func (pass *InternetPassword) CheckValidity() error {
if err := check32BitUTF8("ServerName", pass.ServerName); err != nil {
return err
}
if err := check32BitUTF8("SecurityDomain", pass.SecurityDomain); err != nil {
return err
}
if err := check32BitUTF8("AccountName", pass.AccountName); err != nil {
return err
}
if err := check32BitUTF8("Path", pass.Path); err != nil {
return err
}
if err := check32Bit("Password", pass.Password); err != nil {
return err
}
return nil
}
type keychainError C.OSStatus
// Error codes from https://developer.apple.com/library/mac/documentation/security/Reference/keychainservices/Reference/reference.html#//apple_ref/doc/uid/TP30000898-CH5g-CJBEABHG
const (
ErrUnimplemented keychainError = C.errSecUnimplemented
ErrParam keychainError = C.errSecParam
ErrAllocate keychainError = C.errSecAllocate
ErrNotAvailable keychainError = C.errSecNotAvailable
ErrReadOnly keychainError = C.errSecReadOnly
ErrAuthFailed keychainError = C.errSecAuthFailed
ErrNoSuchKeychain keychainError = C.errSecNoSuchKeychain
ErrInvalidKeychain keychainError = C.errSecInvalidKeychain
ErrDuplicateKeychain keychainError = C.errSecDuplicateKeychain
ErrDuplicateCallback keychainError = C.errSecDuplicateCallback
ErrInvalidCallback keychainError = C.errSecInvalidCallback
ErrDuplicateItem keychainError = C.errSecDuplicateItem
ErrItemNotFound keychainError = C.errSecItemNotFound
ErrBufferTooSmall keychainError = C.errSecBufferTooSmall
ErrDataTooLarge keychainError = C.errSecDataTooLarge
ErrNoSuchAttr keychainError = C.errSecNoSuchAttr
ErrInvalidItemRef keychainError = C.errSecInvalidItemRef
ErrInvalidSearchRef keychainError = C.errSecInvalidSearchRef
ErrNoSuchClass keychainError = C.errSecNoSuchClass
ErrNoDefaultKeychain keychainError = C.errSecNoDefaultKeychain
ErrReadOnlyAttr keychainError = C.errSecReadOnlyAttr
// TODO: Fill out more of these?
)
func newKeychainError(errCode C.OSStatus) error {
if errCode == C.noErr {
return nil
}
return keychainError(errCode)
}
func (ke keychainError) Error() string {
errorMessageCFString := C.SecCopyErrorMessageString(C.OSStatus(ke), nil)
defer C.CFRelease(C.CFTypeRef(errorMessageCFString))
errorMessageCString := C.CFStringGetCStringPtr(errorMessageCFString, C.kCFStringEncodingASCII)
if errorMessageCString != nil {
return C.GoString(errorMessageCString)
}
return fmt.Sprintf("keychainError with unknown error code %d", C.OSStatus(ke))
}
func protocolTypeFromRef(proto C.CFTypeRef) ProtocolType {
// TODO: Fill this out.
switch proto {
case C.kSecAttrProtocolHTTP:
return ProtocolHTTP
case C.kSecAttrProtocolHTTPS:
return ProtocolHTTPS
}
panic(fmt.Sprintf("unknown proto in protocolTypeToGo: %v", proto))
}
func authenticationTypeFromRef(authType C.CFTypeRef) AuthenticationType {
// TODO: Fill this out.
switch authType {
case C.kSecAttrAuthenticationTypeHTTPBasic:
return AuthenticationHTTPBasic
case C.kSecAttrAuthenticationTypeDefault:
return AuthenticationDefault
}
panic(fmt.Sprintf("unknown authType in authenticationTypeFromStringRef: %v", authType))
}
// Adds an Internet password to the user's default keychain.
func AddInternetPassword(pass *InternetPassword) error {
if err := pass.CheckValidity(); err != nil {
return err
}
serverName := C.CString(pass.ServerName)
defer C.free(unsafe.Pointer(serverName))
var securityDomain *C.char
if pass.SecurityDomain != "" {
securityDomain = C.CString(pass.SecurityDomain)
defer C.free(unsafe.Pointer(securityDomain))
}
accountName := C.CString(pass.AccountName)
defer C.free(unsafe.Pointer(accountName))
path := C.CString(pass.Path)
defer C.free(unsafe.Pointer(path))
password := unsafe.Pointer(C.CString(pass.Password))
defer C.free(password)
errCode := C.SecKeychainAddInternetPassword(
nil, // default keychain
C.UInt32(len(pass.ServerName)),
serverName,
C.UInt32(len(pass.SecurityDomain)),
securityDomain,
C.UInt32(len(pass.AccountName)),
accountName,
C.UInt32(len(pass.Path)),
path,
C.UInt16(pass.Port),
C.SecProtocolType(pass.Protocol),
C.SecAuthenticationType(pass.AuthType),
C.UInt32(len(pass.Password)),
password,
nil,
)
return newKeychainError(errCode)
}
// Finds the first Internet password item that matches the attributes you
// provide in pass. Some attributes, such as ServerName and AccountName may be
// left blank, in which case they will be ignored in the search.
//
// Returns an error if the lookup was unsuccessful.
func FindInternetPassword(pass *InternetPassword) (*InternetPassword, error) {
if err := pass.CheckValidity(); err != nil {
return nil, err
}
serverName := C.CString(pass.ServerName)
defer C.free(unsafe.Pointer(serverName))
var securityDomain *C.char
if pass.SecurityDomain != "" {
securityDomain = C.CString(pass.SecurityDomain)
defer C.free(unsafe.Pointer(securityDomain))
}
accountName := C.CString(pass.AccountName)
defer C.free(unsafe.Pointer(accountName))
path := C.CString(pass.Path)
defer C.free(unsafe.Pointer(path))
var passwordLength C.UInt32
var password unsafe.Pointer
var itemRef C.SecKeychainItemRef
errCode := C.SecKeychainFindInternetPassword(
nil, // default keychain
C.UInt32(len(pass.ServerName)),
serverName,
C.UInt32(len(pass.SecurityDomain)),
securityDomain,
C.UInt32(len(pass.AccountName)),
accountName,
C.UInt32(len(pass.Path)),
path,
C.UInt16(pass.Port),
C.SecProtocolType(pass.Protocol),
C.SecAuthenticationType(pass.AuthType),
&passwordLength,
&password,
&itemRef,
)
if err := newKeychainError(errCode); err != nil {
return nil, err
}
defer C.CFRelease(C.CFTypeRef(itemRef))
defer C.SecKeychainItemFreeContent(nil, password)
resp := InternetPassword{}
resp.Password = C.GoStringN((*C.char)(password), C.int(passwordLength))
// Get remaining attributes
items := C.CFArrayCreateMutable(nil, 1, nil)
defer C.CFRelease(C.CFTypeRef(items))
C.CFArrayAppendValue(items, unsafe.Pointer(itemRef))
dict := C.CFDictionaryCreateMutable(nil, 0, nil, nil)
defer C.CFRelease(C.CFTypeRef(dict))
C.CFDictionaryAddValue(dict, unsafe.Pointer(C.kSecClass), unsafe.Pointer(C.kSecClassInternetPassword))
C.CFDictionaryAddValue(dict, unsafe.Pointer(C.kSecMatchItemList), unsafe.Pointer(items))
C.CFDictionaryAddValue(dict, unsafe.Pointer(C.kSecReturnAttributes), unsafe.Pointer(C.kCFBooleanTrue))
var result C.CFTypeRef = nil
errCode = C.SecItemCopyMatching(dict, &result)
if err := newKeychainError(errCode); err != nil {
return nil, err
}
defer C.CFRelease(result)
// get attributes out of attribute dictionary
resultdict := (C.CFDictionaryRef)(result) // type cast attribute dictionary
resp.ServerName = getCFDictValueUTF8String(resultdict, C.kSecAttrServer)
resp.SecurityDomain = getCFDictValueUTF8String(resultdict, C.kSecAttrSecurityDomain)
resp.AccountName = getCFDictValueUTF8String(resultdict, C.kSecAttrAccount)
resp.Path = getCFDictValueUTF8String(resultdict, C.kSecAttrPath)
resp.Port = (int)(getCFDictValueInt32(resultdict, C.kSecAttrPort))
resp.Protocol = protocolTypeFromRef(getCFDictValueRef(resultdict, C.kSecAttrProtocol))
resp.AuthType = authenticationTypeFromRef(getCFDictValueRef(resultdict, C.kSecAttrAuthenticationType))
return &resp, nil
}
func getCFDictValueRef(dict C.CFDictionaryRef, key C.CFTypeRef) C.CFTypeRef {
return (C.CFTypeRef)(C.CFDictionaryGetValue(dict, unsafe.Pointer(key)))
}
func getCFDictValueUTF8String(dict C.CFDictionaryRef, key C.CFTypeRef) string {
val := getCFDictValueRef(dict, key)
if val == nil {
return ""
}
valcstr := C.CFStringGetCStringPtr((C.CFStringRef)(val), C.kCFStringEncodingUTF8)
return C.GoString(valcstr)
}
func getCFDictValueInt32(dict C.CFDictionaryRef, key C.CFTypeRef) (ret int32) {
val := getCFDictValueRef(dict, key)
if val == nil {
return
}
if C.CFNumberGetValue((C.CFNumberRef)(val), C.kCFNumberSInt32Type, unsafe.Pointer(&ret)) == C.false {
ret = 0
return
}
return
}