]> git.donarmstrong.com Git - roundcube.git/blob - plugins/enigma/lib/Crypt/GPG/VerifyStatusHandler.php
Imported Upstream version 0.6+dfsg
[roundcube.git] / plugins / enigma / lib / Crypt / GPG / VerifyStatusHandler.php
1 <?php
2
3 /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
4
5 /**
6  * Crypt_GPG is a package to use GPG from PHP
7  *
8  * This file contains an object that handles GPG's status output for the verify
9  * operation.
10  *
11  * PHP version 5
12  *
13  * LICENSE:
14  *
15  * This library is free software; you can redistribute it and/or modify
16  * it under the terms of the GNU Lesser General Public License as
17  * published by the Free Software Foundation; either version 2.1 of the
18  * License, or (at your option) any later version.
19  *
20  * This library is distributed in the hope that it will be useful,
21  * but WITHOUT ANY WARRANTY; without even the implied warranty of
22  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
23  * Lesser General Public License for more details.
24  *
25  * You should have received a copy of the GNU Lesser General Public
26  * License along with this library; if not, write to the Free Software
27  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
28  *
29  * @category  Encryption
30  * @package   Crypt_GPG
31  * @author    Michael Gauthier <mike@silverorange.com>
32  * @copyright 2008 silverorange
33  * @license   http://www.gnu.org/copyleft/lesser.html LGPL License 2.1
34  * @version   CVS: $Id: VerifyStatusHandler.php 302908 2010-08-31 03:56:54Z gauthierm $
35  * @link      http://pear.php.net/package/Crypt_GPG
36  * @link      http://www.gnupg.org/
37  */
38
39 /**
40  * Signature object class definition
41  */
42 require_once 'Crypt/GPG/Signature.php';
43
44 /**
45  * Status line handler for the verify operation
46  *
47  * This class is used internally by Crypt_GPG and does not need be used
48  * directly. See the {@link Crypt_GPG} class for end-user API.
49  *
50  * This class is responsible for building signature objects that are returned
51  * by the {@link Crypt_GPG::verify()} method. See <b>doc/DETAILS</b> in the
52  * {@link http://www.gnupg.org/download/ GPG distribution} for detailed
53  * information on GPG's status output for the verify operation.
54  *
55  * @category  Encryption
56  * @package   Crypt_GPG
57  * @author    Michael Gauthier <mike@silverorange.com>
58  * @copyright 2008 silverorange
59  * @license   http://www.gnu.org/copyleft/lesser.html LGPL License 2.1
60  * @link      http://pear.php.net/package/Crypt_GPG
61  * @link      http://www.gnupg.org/
62  */
63 class Crypt_GPG_VerifyStatusHandler
64 {
65     // {{{ protected properties
66
67     /**
68      * The current signature id
69      *
70      * Ths signature id is emitted by GPG before the new signature line so we
71      * must remember it temporarily.
72      *
73      * @var string
74      */
75     protected $signatureId = '';
76
77     /**
78      * List of parsed {@link Crypt_GPG_Signature} objects
79      *
80      * @var array
81      */
82     protected $signatures = array();
83
84     /**
85      * Array index of the current signature
86      *
87      * @var integer
88      */
89     protected $index = -1;
90
91     // }}}
92     // {{{ handle()
93
94     /**
95      * Handles a status line
96      *
97      * @param string $line the status line to handle.
98      *
99      * @return void
100      */
101     public function handle($line)
102     {
103         $tokens = explode(' ', $line);
104         switch ($tokens[0]) {
105         case 'GOODSIG':
106         case 'EXPSIG':
107         case 'EXPKEYSIG':
108         case 'REVKEYSIG':
109         case 'BADSIG':
110             $signature = new Crypt_GPG_Signature();
111
112             // if there was a signature id, set it on the new signature
113             if ($this->signatureId != '') {
114                 $signature->setId($this->signatureId);
115                 $this->signatureId = '';
116             }
117
118             // Detect whether fingerprint or key id was returned and set
119             // signature values appropriately. Key ids are strings of either
120             // 16 or 8 hexadecimal characters. Fingerprints are strings of 40
121             // hexadecimal characters. The key id is the last 16 characters of
122             // the key fingerprint.
123             if (strlen($tokens[1]) > 16) {
124                 $signature->setKeyFingerprint($tokens[1]);
125                 $signature->setKeyId(substr($tokens[1], -16));
126             } else {
127                 $signature->setKeyId($tokens[1]);
128             }
129
130             // get user id string
131             $string = implode(' ', array_splice($tokens, 2));
132             $string = rawurldecode($string);
133
134             $signature->setUserId(Crypt_GPG_UserId::parse($string));
135
136             $this->index++;
137             $this->signatures[$this->index] = $signature;
138             break;
139
140         case 'ERRSIG':
141             $signature = new Crypt_GPG_Signature();
142
143             // if there was a signature id, set it on the new signature
144             if ($this->signatureId != '') {
145                 $signature->setId($this->signatureId);
146                 $this->signatureId = '';
147             }
148
149             // Detect whether fingerprint or key id was returned and set
150             // signature values appropriately. Key ids are strings of either
151             // 16 or 8 hexadecimal characters. Fingerprints are strings of 40
152             // hexadecimal characters. The key id is the last 16 characters of
153             // the key fingerprint.
154             if (strlen($tokens[1]) > 16) {
155                 $signature->setKeyFingerprint($tokens[1]);
156                 $signature->setKeyId(substr($tokens[1], -16));
157             } else {
158                 $signature->setKeyId($tokens[1]);
159             }
160
161             $this->index++;
162             $this->signatures[$this->index] = $signature;
163
164             break;
165
166         case 'VALIDSIG':
167             if (!array_key_exists($this->index, $this->signatures)) {
168                 break;
169             }
170
171             $signature = $this->signatures[$this->index];
172
173             $signature->setValid(true);
174             $signature->setKeyFingerprint($tokens[1]);
175
176             if (strpos($tokens[3], 'T') === false) {
177                 $signature->setCreationDate($tokens[3]);
178             } else {
179                 $signature->setCreationDate(strtotime($tokens[3]));
180             }
181
182             if (array_key_exists(4, $tokens)) {
183                 if (strpos($tokens[4], 'T') === false) {
184                     $signature->setExpirationDate($tokens[4]);
185                 } else {
186                     $signature->setExpirationDate(strtotime($tokens[4]));
187                 }
188             }
189
190             break;
191
192         case 'SIG_ID':
193             // note: signature id comes before new signature line and may not
194             // exist for some signature types
195             $this->signatureId = $tokens[1];
196             break;
197         }
198     }
199
200     // }}}
201     // {{{ getSignatures()
202
203     /**
204      * Gets the {@link Crypt_GPG_Signature} objects parsed by this handler
205      *
206      * @return array the signature objects parsed by this handler.
207      */
208     public function getSignatures()
209     {
210         return $this->signatures;
211     }
212
213     // }}}
214 }
215
216 ?>