-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsec-keychain.cpp
175 lines (141 loc) · 5.35 KB
/
sec-keychain.cpp
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
/*
+----------------------------------------------------------------------+
| PHP Version 7 |
+----------------------------------------------------------------------+
| Copyright (c) 1997-2017 The PHP Group |
+----------------------------------------------------------------------+
| This source file is subject to version 3.01 of the PHP license, |
| that is bundled with this package in the file LICENSE, and is |
| available through the world-wide-web at the following url: |
| http://www.php.net/license/3_01.txt |
| If you did not receive a copy of the PHP license and are unable to |
| obtain it through the world-wide-web, please send a note to |
| [email protected] so we can mail you a copy immediately. |
+----------------------------------------------------------------------+
| Author: Sara Golemon <[email protected]> |
+----------------------------------------------------------------------+
*/
#include "darwin.h"
namespace php { namespace darwin {
zend_class_entry *SecKeychain_ce = nullptr;
/****************************************************************************/
static PHP_METHOD(SecKeychain, __construct) {
throw DarwinException(0, "wtf?");
}
#define SECKEYCHAIN(keychain) \
auto keychain = CFObject::get(Z_OBJ_P(getThis()))->as<SecKeychainRef>(); \
if (!keychain) { \
throw DarwinException(0, "Keychain object has no value"); \
} else do {} while (false)
/* {{{ proto SecKeychain SecKeychian::Create(string $name[, ?string $password = NULL]) */
ZEND_BEGIN_ARG_INFO_EX(seckeychain_create_arginfo, 0, ZEND_RETURN_VALUE, 1)
ZEND_ARG_TYPE_INFO(0, name, IS_STRING, 0)
ZEND_ARG_TYPE_INFO(0, password, IS_STRING, 1)
ZEND_END_ARG_INFO();
static PHP_METHOD(SecKeychain, Create) {
zend_string *name;
zend_string *password = nullptr;
SecKeychainRef keychain;
if (FAILURE == zend_parse_parameters_throw(ZEND_NUM_ARGS(), "S|S!", &name, &password)) {
return;
}
OSStatus status;
if (password) {
status = SecKeychainCreate(ZSTR_VAL(name), ZSTR_LEN(password), ZSTR_VAL(password), false, nullptr, &keychain);
} else {
status = SecKeychainCreate(ZSTR_VAL(name), 0, nullptr, true, nullptr, &keychain);
}
if (status != errSecSuccess) {
throw SecurityException(status, "Unable to create keychain %s", ZSTR_VAL(name));
}
RETURN_SECKEYCHAIN(keychain);
}
/* }}} */
/* {{{ proto SecKeychain SecKeychain::Open(string $name) */
ZEND_BEGIN_ARG_INFO_EX(seckeychain_open_arginfo, 0, ZEND_RETURN_VALUE, 1)
ZEND_ARG_INFO(0, name)
ZEND_END_ARG_INFO();
static PHP_METHOD(SecKeychain, Open) {
zend_string *name;
if (FAILURE == zend_parse_parameters_throw(ZEND_NUM_ARGS(), "S", &name)) {
return;
}
SecKeychainRef keychain;
auto status = SecKeychainOpen(ZSTR_VAL(name), &keychain);
if (status != errSecSuccess) {
throw SecurityException(status, "Unable to open keychain %s", ZSTR_VAL(name));
}
RETURN_SECKEYCHAIN(keychain);
}
/* }}} */
/* {{{ proto this SecKeychain::lock() */
static PHP_METHOD(SecKeychain, lock) {
if (zend_parse_parameters_none_throw()) { return; }
SECKEYCHAIN(keychain);
auto status = SecKeychainLock(keychain);
if (status != errSecSuccess) {
throw SecurityException(status, "Unable to lock keychain");
}
RETURN_ZVAL(getThis(), 1, 0);
}
/* }}} */
/* {{{ proto this SecKeychain::unlock([?string $password = NULL]) */
ZEND_BEGIN_ARG_INFO_EX(seckeychain_unlock_arginfo, 0, ZEND_RETURN_VALUE, 0)
ZEND_ARG_INFO(0, password)
ZEND_END_ARG_INFO();
static PHP_METHOD(SecKeychain, unlock) {
zend_string *password = nullptr;
if (zend_parse_parameters(ZEND_NUM_ARGS(), "|S!", &password) == FAILURE) {
return;
}
SECKEYCHAIN(keychain);
OSStatus status;
if (password) {
status = SecKeychainUnlock(keychain, ZSTR_LEN(password), ZSTR_VAL(password), 1);
} else {
status = SecKeychainUnlock(keychain, 0, NULL, 0);
}
if (status != errSecSuccess) {
throw SecurityException(status, "Unable to unlock keychain");
}
RETURN_ZVAL(getThis(), 1, 0);
}
/* }}} */
/* {{{ proto int SecKeychain::getVersion() */
static PHP_METHOD(SecKeychain, getVersion) {
if (zend_parse_parameters_none_throw() == FAILURE) { return; }
uint32_t version;
OSStatus status = SecKeychainGetVersion(&version);
if (status != errSecSuccess) {
throw SecurityException(status, "Unable to determine keychain version");
}
RETURN_LONG(version);
}
/* }}} */
static zend_function_entry seckeychain_methods[] = {
/* Create instances via ::Open or ::Create */
PHP_ME(SecKeychain, __construct, nullptr,
ZEND_ACC_CTOR | ZEND_ACC_PRIVATE | ZEND_ACC_FINAL)
PHP_ME(SecKeychain, Create, seckeychain_create_arginfo, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC)
PHP_ME(SecKeychain, Open, seckeychain_open_arginfo, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC)
PHP_ME(SecKeychain, lock, nullptr, ZEND_ACC_PUBLIC)
PHP_ME(SecKeychain, unlock, seckeychain_unlock_arginfo, ZEND_ACC_PUBLIC)
PHP_ME(SecKeychain, getVersion, nullptr, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC)
PHP_FE_END
};
PHP_MINIT_FUNCTION(darwin_SecKeychain) {
SecKeychain_ce = CFObject::registerClass(
INIT_FUNC_ARGS_PASSTHRU,
"Darwin\\SecKeychain",
seckeychain_methods);
return SUCCESS;
}
}} // namespace php::darwin
/*
* Local variables:
* tab-width: 4
* c-basic-offset: 4
* End:
* vim600: noet sw=4 ts=4 fdm=marker
* vim<600: noet sw=4 ts=4
*/