Tizen_4.0 base
[platform/upstream/docker-engine.git] / vendor / github.com / google / certificate-transparency / go / x509 / root_darwin.go
1 // Copyright 2011 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
4
5 // +build darwin,cgo
6
7 package x509
8
9 /*
10 #cgo CFLAGS: -mmacosx-version-min=10.6 -D__MAC_OS_X_VERSION_MAX_ALLOWED=1060
11 #cgo LDFLAGS: -framework CoreFoundation -framework Security
12
13 #include <CoreFoundation/CoreFoundation.h>
14 #include <Security/Security.h>
15
16 // FetchPEMRootsCTX509 fetches the system's list of trusted X.509 root certificates.
17 //
18 // On success it returns 0 and fills pemRoots with a CFDataRef that contains the extracted root
19 // certificates of the system. On failure, the function returns -1.
20 //
21 // Note: The CFDataRef returned in pemRoots must be released (using CFRelease) after
22 // we've consumed its content.
23 int FetchPEMRootsCTX509(CFDataRef *pemRoots) {
24         if (pemRoots == NULL) {
25                 return -1;
26         }
27
28         CFArrayRef certs = NULL;
29         OSStatus err = SecTrustCopyAnchorCertificates(&certs);
30         if (err != noErr) {
31                 return -1;
32         }
33
34         CFMutableDataRef combinedData = CFDataCreateMutable(kCFAllocatorDefault, 0);
35         int i, ncerts = CFArrayGetCount(certs);
36         for (i = 0; i < ncerts; i++) {
37                 CFDataRef data = NULL;
38                 SecCertificateRef cert = (SecCertificateRef)CFArrayGetValueAtIndex(certs, i);
39                 if (cert == NULL) {
40                         continue;
41                 }
42
43                 // Note: SecKeychainItemExport is deprecated as of 10.7 in favor of SecItemExport.
44                 // Once we support weak imports via cgo we should prefer that, and fall back to this
45                 // for older systems.
46                 err = SecKeychainItemExport(cert, kSecFormatX509Cert, kSecItemPemArmour, NULL, &data);
47                 if (err != noErr) {
48                         continue;
49                 }
50
51                 if (data != NULL) {
52                         CFDataAppendBytes(combinedData, CFDataGetBytePtr(data), CFDataGetLength(data));
53                         CFRelease(data);
54                 }
55         }
56
57         CFRelease(certs);
58
59         *pemRoots = combinedData;
60         return 0;
61 }
62 */
63 import "C"
64 import "unsafe"
65
66 func (c *Certificate) systemVerify(opts *VerifyOptions) (chains [][]*Certificate, err error) {
67         return nil, nil
68 }
69
70 func initSystemRoots() {
71         roots := NewCertPool()
72
73         var data C.CFDataRef = nil
74         err := C.FetchPEMRootsCTX509(&data)
75         if err == -1 {
76                 return
77         }
78
79         defer C.CFRelease(C.CFTypeRef(data))
80         buf := C.GoBytes(unsafe.Pointer(C.CFDataGetBytePtr(data)), C.int(C.CFDataGetLength(data)))
81         roots.AppendCertsFromPEM(buf)
82         systemRoots = roots
83 }