Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

This PR adds the functionality to retrieve Certificates from the keychain. #103

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,4 @@ _testmain.go
*.prof

vendor
/.idea
44 changes: 44 additions & 0 deletions corefoundation.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,15 @@ CFDictionaryRef CFDictionaryCreateSafe2(CFAllocatorRef allocator, const uintptr_
return CFDictionaryCreate(allocator, (const void **)keys, (const void **)values, numValues, keyCallBacks, valueCallBacks);
}

// See: https://developer.apple.com/documentation/corefoundation/1388741-cfarraycreate
CFArrayRef CFArrayCreateSafe2(CFAllocatorRef allocator, const uintptr_t *values, CFIndex numValues, const CFArrayCallBacks *callBacks) {
return CFArrayCreate(allocator, (const void **)values, numValues, callBacks);
}

// See: https://developer.apple.com/documentation/corefoundation/1388770-cfarraycreatemutable
CFMutableArrayRef CFArrayCreateMutableSafe2(CFAllocatorRef allocator, CFIndex numValues, const CFArrayCallBacks *callBacks) {
return CFArrayCreateMutable(allocator, numValues, callBacks);
}
*/
import "C"
import (
Expand All @@ -42,6 +48,11 @@ func Release(ref C.CFTypeRef) {
C.CFRelease(ref)
}

// Retain retains memory pointed to by a CFTypeRef.
func Retain(ref C.CFTypeRef) {
C.CFRetain(ref)
}

// BytesToCFData will return a CFDataRef and if non-nil, must be released with
// Release(ref).
func BytesToCFData(b []byte) (C.CFDataRef, error) {
Expand All @@ -52,6 +63,8 @@ func BytesToCFData(b []byte) (C.CFDataRef, error) {
if len(b) > 0 {
p = (*C.UInt8)(&b[0])
}

// https://developer.apple.com/documentation/corefoundation/1542359-cfdatacreate
cfData := C.CFDataCreate(C.kCFAllocatorDefault, p, C.CFIndex(len(b)))
if cfData == 0 {
return 0, fmt.Errorf("CFDataCreate failed")
Expand Down Expand Up @@ -146,6 +159,17 @@ func CFStringToString(s C.CFStringRef) string {
return string(buf[:usedBufLen])
}

// CFBooleanToBool convers a C.CFBooleanRef to a golang bool
func CFBooleanToBool(b C.CFBooleanRef) bool {
return C.CFBooleanGetValue(b) != 0
}

func CFKeyTypeEnumToString(cfNumber C.CFNumberRef) string {
var value C.int
C.CFNumberGetValue(cfNumber, C.kCFNumberIntType, unsafe.Pointer(&value)) //nolint
return fmt.Sprintf("%d", value)
}

// ArrayToCFArray will return a CFArrayRef and if non-nil, must be released with
// Release(ref).
func ArrayToCFArray(a []C.CFTypeRef) C.CFArrayRef {
Expand Down Expand Up @@ -368,3 +392,23 @@ func CFNumberToInterface(cfNumber C.CFNumberRef) interface{} {
}
panic("Unknown CFNumber type")
}

// CFErrorError returns an error for a CFErrorRef unless it is nil.
func CFErrorError(cfErr C.CFErrorRef) error {
var err C.CFErrorRef
if cfErr == err {
return nil
}
code := int(C.CFErrorGetCode(cfErr))
cDescription := C.CFErrorCopyDescription(cfErr)
defer Release(C.CFTypeRef(cDescription))
return fmt.Errorf("CFError %d (%s)", code, CFStringToString(cDescription))
}

// newEmptyArray returns an empty array created by CFArrayCreate.
// See: https://developer.apple.com/documentation/corefoundation/1388741-cfarraycreate
func newEmptyArray(capacity int) C.CFMutableArrayRef {
arrayRef := C.CFArrayCreateMutableSafe2(C.kCFAllocatorDefault, C.CFIndex(capacity), &C.kCFTypeArrayCallBacks) //nolint

return arrayRef
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ require (
github.com/keybase/dbus v0.0.0-20220506165403-5aa21ea2c23a
github.com/pkg/errors v0.9.1
github.com/stretchr/testify v1.8.0
golang.org/x/crypto v0.1.0
golang.org/x/crypto v0.11.0
)

require (
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSS
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.0 h1:pSgiaMZlXftHpm5L7V1+rVB+AZJydKsMxsQBIJw4PKk=
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
golang.org/x/crypto v0.1.0 h1:MDRAIl0xIo9Io2xV565hzXHw3zVseKrJKodhohM5CjU=
golang.org/x/crypto v0.1.0/go.mod h1:RecgLatLF4+eUMCP1PoPZQb+cVrJcOPbHkTkbkB9sbw=
golang.org/x/crypto v0.11.0 h1:6Ewdq3tDic1mg5xRO4milcWCfMVQhI4NkqWWvqejpuA=
golang.org/x/crypto v0.11.0/go.mod h1:xgJhtzW8F9jGdVFWZESrid1U1bjeNy4zgy5cRr/CIio=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
Expand Down
Loading