-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkeychain-switch-default.cc
More file actions
48 lines (37 loc) · 1.44 KB
/
keychain-switch-default.cc
File metadata and controls
48 lines (37 loc) · 1.44 KB
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
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
/*
* Copyright (c) 2013, Yingdi Yu
* Author: Yingdi Yu <[email protected]>
*/
#include <CoreFoundation/CoreFoundation.h>
#include <Security/Security.h>
#include <CoreServices/CoreServices.h>
#include <iostream>
using namespace std;
int main (int argc, char** argv)
{
OSStatus res;
SecKeychainRef targetKeychain;
SecKeychainRef originKeychain;
SecKeychainRef newDftKeychain;
res = SecKeychainCreate ("example.keychain", 4, "1234", false, NULL, &targetKeychain);
if(errSecDuplicateKeychain == res)
res = SecKeychainOpen ("example.keychain", &targetKeychain);
cerr << "Create/Open keychain res: " << res << endl;
res = SecKeychainCopyDefault (&originKeychain);
cerr << "Get default keychain res: " << res << endl;
res = SecKeychainSetDefault (targetKeychain);
cerr << "Set default keychain res: " << res << endl;
res = SecKeychainCopyDefault (&newDftKeychain);
char pathName[256] = {0};
UInt32 pathLength = 256;
res = SecKeychainGetPath (newDftKeychain, &pathLength, pathName);
cerr << "New default keychain: " << pathName << endl;
res = SecKeychainSetDefault (originKeychain);
cerr << "Set default keychain back res: " << res << endl;
res = SecKeychainCopyDefault (&newDftKeychain);
memset(pathName, 0, pathLength);
res = SecKeychainGetPath (newDftKeychain, &pathLength, pathName);
cerr << "Now default keychain: " << pathName << endl;
return 0;
}