forked from netcommons/NetCommons2
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathView.class.php
More file actions
219 lines (195 loc) · 5.83 KB
/
View.class.php
File metadata and controls
219 lines (195 loc) · 5.83 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
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
<?php
include_once MAPLE_DIR.'/includes/pear/Crypt/RSA.php';
/**
* 認証クラス
*
* @package NetCommons
* @author Noriko Arai,Ryuji Masukawa
* @copyright 2006-2007 NetCommons Project
* @license http://www.netcommons.org/license.txt NetCommons License
* @project NetCommons Project, supported by National Institute of Informatics
* @access public
*/
class Encryption_View {
/**
* @var DBオブジェクトを保持
*
* @access private
*/
var $_db = null;
var $_container = null;
// 有効期限(日)
var $expiration_day = 365;
// キーの長さ
var $key_length = 1024;
/**
* コンストラクター
*
* @access public
*/
function Encryption_View() {
$this->_container =& DIContainerFactory::getContainer();
//DBオブジェクト取得
$this->_db =& $this->_container->getComponent("DbObject");
}
/**
* パブリックキー取得
* @return string $public_key
* @access public
*/
function getPublickey($expiration_time = null)
{
if($expiration_time != null) {
$where_params = array(
"expiration_time" => $expiration_time
);
} else {
$where_params = array();
}
$order_params = array(
"expiration_time" => "DESC"
);
// 有効期限がいついつの公開鍵を取得
$result = $this->_db->selectExecute("encryption", $where_params, $order_params, 1);
if ($result === false) {
return $result;
}
if(!isset($result[0])) {
// エラー
// 設定有効期限での公開鍵は存在しない
// ダミーで適当に公開鍵を返しておく
$key_pair = new Crypt_RSA_KeyPair($this->key_length);
$public_key = $key_pair->getPublicKey();
} else {
$public_key = $result[0]['public_key'];
}
return $public_key;
}
/**
* プライベートキー取得時使用
* @return array
* @access public
*/
function &getEncryptionKeys()
{
// 有効期限が切れてないものを取得
$int_time = mktime(date("H"), date("i"), date("s"), date("m"), date("d") - $this->expiration_day, date("Y"));
$time = date("YmdHis", $int_time);
$where_params = array(
"expiration_time >= ".$time => null
);
$result = $this->_db->selectExecute("encryption", $where_params, null, 1);
if ($result === false) {
return $result;
}
if(!isset($result[0])) {
// 有効期限が切れている or 新規作成
$key_pair = new Crypt_RSA_KeyPair($this->key_length);
$public_key = $key_pair->getPublicKey();
$private_key = $key_pair->getPrivateKey();
//insert
$update_time = timezone_date();
$container =& DIContainerFactory::getContainer();
$session =& $container->getComponent("Session");
$user_id = $session->getParameter("_user_id");
$int_time = mktime(date("H"), date("i"), date("s"), date("m"), date("d") + $this->expiration_day, date("Y"));
$time = date("YmdHis", $int_time);
$params = array(
"public_key" => $public_key->toString(),
"private_key" => $private_key->toString(),
"key_length" => $this->key_length,
"expiration_time" => $time,
"update_time" => $update_time,
"update_user" => $user_id
);
$result = $this->_db->insertExecute("encryption", $params, false);
if ($result === false) {
return $result;
}
} else {
$params = $result[0];
//$private_key = $result[0]['private_key'];
}
return $params;
}
function encrypt($plain_text,$private_key)
{
$key = Crypt_RSA_Key::fromString($private_key);
$this->_check_error($key);
$rsa_obj = new Crypt_RSA;
$this->_check_error($rsa_obj);
$enc_text = $rsa_obj->encrypt($plain_text, $key);
$this->_check_error($rsa_obj);
return $enc_text;
}
function decrypt($enc_text,$public_key)
{
$key = Crypt_RSA_Key::fromString($public_key);
$this->_check_error($key);
$rsa_obj = new Crypt_RSA;
$this->_check_error($rsa_obj);
$rsa_obj->setParams(array('dec_key' => $key));
$this->_check_error($rsa_obj);
$plain_text = $rsa_obj->decrypt($enc_text);
$this->_check_error($rsa_obj);
return $plain_text;
}
//
// error handler(暫定版)
//
function _check_error(&$obj)
{
if ($obj->isError()) {
$error = $obj->getLastError();
switch ($error->getCode()) {
case CRYPT_RSA_ERROR_WRONG_TAIL :
// nothing to do
break;
default:
// echo error message and exit
echo 'error: ', $error->getMessage();
exit;
}
}
}
//
// 他サイトから取得する場合の文字列作
// 現状、未使用
//
function getRedirectUrl() {
$session =& $this->_container->getComponent("Session");
$url_parameters = "";
$user_id = $this->_session->getParameter("_login_id");
$auth_id = $this->_session->getParameter("_user_auth_id");
$date = timezone_date();
$token = $this->_randomkeys(8);
$url_parameters .= "&_user_id=".$user_id;
$url_parameters .= "&_auth_id=".$auth_id;
//時間付与
$url_parameters .= "&_ts=".$date;
//TODO:ランダム文字列付与 現状、8、後にdefeine等に移動
$url_parameters .= "&_token=".$token;
//暗号化
$encryption_param = md5($user_id.":".$auth_id.":".$date.":".$token);
$private_key = $this->getPrivatekey();
$sig = $this->encrypt($encryption_param,$private_key);
$url_parameters .= "&_sig=".$sig;
//redirect_urlを付与
$url_parameters .= "&_redirect_url=".BASE_URL.INDEX_FILE_NAME;
return $url_parameters;
}
//
// 乱数発生関数
// 現状、未使用
//
function _randomkeys($length)
{
$key = "";
$pattern = "1234567890abcdefghijklmnopqrstuvwxyz";
for($i=0;$i<$length;$i++){
$key .= $pattern{rand(0,35)};
}
return $key;
}
}
?>