-
Notifications
You must be signed in to change notification settings - Fork 127
/
Key.php
389 lines (341 loc) · 12.1 KB
/
Key.php
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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
<?php
/**
* Copyright 2015 Horde LLC (http://www.horde.org/)
*
* See the enclosed file COPYING for license information (LGPL). If you
* did not receive this file, see http://www.horde.org/licenses/lgpl21.
*
* @category Horde
* @copyright 2015 Horde LLC
* @license http://www.horde.org/licenses/lgpl21 LGPL 2.1
* @package Pgp
*/
/**
* Abstract class representing a PGP key.
*
* @author Michael Slusarz <slusarz@horde.org>
* @category Horde
* @copyright 2015 Horde LLC
* @license http://www.horde.org/licenses/lgpl21 LGPL 2.1
* @package Pgp
*
* @property-read string $fingerprint Fingerprint of the base key.
* @property-read string $id ID of the base key.
*/
abstract class Horde_Pgp_Element_Key
extends Horde_Pgp_Element
{
/** Revocation reasons. */
const REVOKE_UNKNOWN = 0;
const REVOKE_SUPERSEDED = 1;
const REVOKE_COMPROMISED = 2;
const REVOKE_RETIRED = 3;
const REVOKE_NOTUSED = 4;
/**
* Cached data.
*
* @var array
*/
protected $_cache;
/**
*/
public function __get($name)
{
switch ($name) {
case 'fingerprint':
case 'id':
$base = $this->getSignKeys();
return $base[0]->$name;
}
}
/**
* Return the list of key fingerprints.
*
* @return array Keys are key IDs; values are fingerprints.
*/
public function getFingerprints()
{
$keys = array_merge($this->getSignKeys(), $this->getEncryptKeys());
$out = array();
foreach ($keys as $val) {
$out[$val->id] = $val->fingerprint;
}
return $out;
}
/**
* Returns the list of signing subkeys within this key.
*
* @return array An array of objects, with these keys:
* - created: (DateTime) Creation time.
* - fingerprint: (string) Key fingerprint.
* - id: (string) Key ID.
* - key: (OpenPGP_PublicKeyPacket) Key packet.
* - revoke: (object) Revocation information. Elements:
* - created: (DateTime) Creation time.
* - info: (string) Human readable reason string.
* - reason: (integer) Revocation reason.
*/
public function getSignKeys()
{
$this->_parse();
return $this->_cache['sign'];
}
/**
* Returns the list of user ID information associated with this key.
*
* @return array An array of objects, with these keys:
* - comment: (string) Comment.
* - created: (DateTime) Creation time.
* - email: (Horde_Mail_Rfc822_Address) E-mail address.
* - key: (OpenPGP_PublicKeyPacket) Key packet.
* - revoke: (object) Revocation information. Elements:
* - created: (DateTime) Creation time.
* - info: (string) Human readable reason string.
* - reason: (integer) Revocation reason.
* - sig: (OpenPGP_SignaturePacket) Signature packet.
*/
public function getUserIds()
{
$this->_parse();
return $this->_cache['userid'];
}
/**
* Does this key contain an e-mail address?
*
* @return boolean True if the key contains the e-mail address.
*/
public function containsEmail($email)
{
foreach ($this->getUserIds() as $val) {
if ($val->email->match($email)) {
return true;
}
}
return false;
}
/**
* Return the list of verified encryption subkeys in this key.
*
* @return array An array of objects, with these keys:
* - created: (DateTime) Creation time.
* - fingerprint: (string) Key fingerprint.
* - id: (string) Key ID.
* - key: (OpenPGP_PublicKeyPacket) Key packet.
* - revoke: (object) Revocation information. Elements:
* - created: (DateTime) Creation time.
* - info: (string) Human readable reason string.
* - reason: (integer) Revocation reason.
*/
public function getEncryptKeys()
{
$this->_parse();
return $this->_cache['encrypt'];
}
/**
* Return the public key.
*
* @return Horde_Pgp_Element_PublicKey Public key.
*/
abstract public function getPublicKey();
/**
* Parse the message data, verifying the key contents.
*/
protected function _parse()
{
if (isset($this->_cache)) {
return;
}
$this->_cache = array(
'encrypt' => array(),
'userid' => array()
);
$fallback = $p = $topkey = $userid = $userid_p = null;
$sub = false;
$create_out = function ($p, $s) {
$out = new stdClass;
$out->key = $p;
$out->sig = $s;
return $out;
};
/* Search for the key flag indicating that a key may be used to
* encrypt communications (RFC 4880 [5.2.3.21]). In the absence
* of finding this flag (i.e. v3 packets), use the first subkey and,
* in the absence of that, use the main key. */
foreach ($this->message as $val) {
if ($val instanceof OpenPGP_PublicKeyPacket) {
if (is_null($topkey)) {
$topkey = new stdClass;
$topkey->created = new DateTime('@' . $val->timestamp);
$topkey->fingerprint = $val->fingerprint;
$topkey->id = $val->key_id;
$topkey->key = $val;
$this->_cache['sign'][] = $topkey;
}
$p = $val;
$p_revoke = null;
} elseif ($val instanceof OpenPGP_UserIDPacket) {
if ($userid && isset($userid->key)) {
$this->_cache['userid'][] = $userid;
}
$userid = new stdClass;
$userid->email = new Horde_Mail_Rfc822_Address($val->email);
$userid->email->personal = $val->name;
$userid->comment = $val->comment;
$userid_p = $val;
} elseif ($val instanceof OpenPGP_SignaturePacket) {
/* Signature types: RFC 4880 [5.2.1] */
switch ($val->signature_type) {
case 0x10:
case 0x11:
case 0x12:
case 0x13:
/* Certification of User ID. */
if ($topkey &&
$userid_p &&
$this->_parseVerify($topkey->key, $userid_p, $val)) {
$userid->key = $topkey->key;
$userid->created = $this->_parseCreation($val);
$userid->sig = $val;
} else {
$userid_p = null;
}
break;
case 0x18:
/* Verify first. */
if (!$p ||
(($topkey->key !== $p) &&
!$this->_parseVerify($topkey->key, $p, $val))) {
continue;
}
$encrypt = new stdClass;
if (isset($topkey->revoke)) {
$encrypt->revoke = $topkey->revoke;
} elseif (!is_null($p_revoke)) {
$encrypt->revoke = $p_revoke;
}
/* Check for explicit key flag subpacket. Require
* this information to be in hashed subpackets. */
if ($val->version === 4) {
$encrypt->created = $this->_parseCreation($val);
foreach ($val->hashed_subpackets as $val2) {
if ($val2 instanceof OpenPGP_SignaturePacket_KeyFlagsPacket) {
foreach ($val2->flags as $val3) {
if ($val3 & 0x04) {
$encrypt->key = $create_out($p, $val);
$encrypt->fingerprint = $p->fingerprint;
$encrypt->id = $p->key_id;
$this->_cache['encrypt'][] = $encrypt;
continue 3;
}
}
/* If the flag wasn't set, we know explicitly
* that this is not an encrypting key. */
continue 2;
}
}
}
if (is_null($fallback)) {
$encrypt->key = $create_out($p, $val);
$fallback = $encrypt;
} elseif (!$sub &&
($p instanceof OpenPGP_PublicSubkeyPacket) ||
($p instanceof OpenPGP_SecretSubkeyPacket)) {
$encrypt->key = $create_out($p, $val);
$fallback = $encrypt;
$sub = true;
}
break;
case 0x20:
/* Key revocation. */
if ($this->_parseVerify($p, false, $val)) {
$topkey->revoke = $this->_parseRevokePacket($val);
}
break;
case 0x28:
/* Subkey revocation. */
if ($this->_parseVerify($topkey->key, $p, $val)) {
$p_revoke = $this->_parseRevokePacket($val);
}
break;
case 0x30:
/* Revocation of User ID. */
if ($this->_parseVerify($topkey->key, $userid_p, $val)) {
$userid->revoke = $this->_parseRevokePacket($val);
}
break;
}
}
}
if ($userid && isset($userid->key)) {
$this->_cache['userid'][] = $userid;
}
if ($fallback && empty($this->_cache['encrypt'])) {
$fallback->fingerprint = $fallback->key->fingerprint;
$fallback->id = $fallback->key->key_id;
$this->_cache['encrypt'][] = $fallback;
}
}
/**
*/
protected function _parseVerify($key, $data, $sig)
{
if (is_null($key) || is_null($data)) {
return false;
}
$pgp = new Horde_Pgp_Backend_Openpgp();
$v = $pgp->verify(
new Horde_Pgp_Element_Message(
new OpenPGP_Message(
($data === false)
? array($key, $sig)
: array($key, $data, $sig)
)
),
$this
);
return isset($v[0][($data === false) ? 1 : 2][0]);
}
/**
*/
protected function _parseCreation($p)
{
/* Creation time is a MUST hashed subpacket (RFC 4880 [5.2.3.4]). */
foreach ($p->hashed_subpackets as $val) {
if ($val instanceof OpenPGP_SignaturePacket_SignatureCreationTimePacket) {
return new DateTime('@' . $val->data);
}
}
return new DateTime('@0');
}
/**
*/
protected function _parseRevokePacket($p)
{
$revoke = new stdClass;
$revoke->created = $this->_parseCreation($p);
$revoke->reason = self::REVOKE_UNKNOWN;
foreach ($p->hashed_subpackets as $val) {
if ($val instanceof OpenPGP_SignaturePacket_ReasonForRevocationPacket) {
switch ($val->code) {
case 0x00:
$revoke->reason = self::REVOKE_UNKNOWN;
break;
case 0x01:
$revoke->reason = self::REVOKE_SUPERSEDED;
break;
case 0x02:
$revoke->reason = self::REVOKE_COMPROMISED;
break;
case 0x03:
$revoke->reason = self::REVOKE_RETIRED;
break;
case 0x20:
$revoke->reason = self::REVOKE_NOTUSED;
break;
}
$revoke->info = $val->data;
}
}
return $revoke;
}
}