|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* MIT License |
|
4
|
|
|
* |
|
5
|
|
|
* Copyright (c) 2016 MZ Desenvolvimento de Sistemas LTDA |
|
6
|
|
|
* |
|
7
|
|
|
* @author Francimar Alves <[email protected]> |
|
8
|
|
|
* |
|
9
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy |
|
10
|
|
|
* of this software and associated documentation files (the "Software"), to deal |
|
11
|
|
|
* in the Software without restriction, including without limitation the rights |
|
12
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
|
13
|
|
|
* copies of the Software, and to permit persons to whom the Software is |
|
14
|
|
|
* furnished to do so, subject to the following conditions: |
|
15
|
|
|
* |
|
16
|
|
|
* The above copyright notice and this permission notice shall be included in all |
|
17
|
|
|
* copies or substantial portions of the Software. |
|
18
|
|
|
* |
|
19
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
|
20
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
|
21
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
|
22
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
|
23
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
|
24
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
|
25
|
|
|
* SOFTWARE. |
|
26
|
|
|
* |
|
27
|
|
|
*/ |
|
28
|
|
|
namespace NFe\Common; |
|
29
|
|
|
|
|
30
|
|
|
use NFe\Common\Node; |
|
31
|
|
|
use NFe\Common\Util; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* Certificado digital |
|
35
|
|
|
*/ |
|
36
|
|
|
class Certificado implements Node |
|
37
|
|
|
{ |
|
38
|
|
|
/** |
|
39
|
|
|
* @var string |
|
40
|
|
|
*/ |
|
41
|
|
|
private $chave_publica; |
|
42
|
|
|
/** |
|
43
|
|
|
* @var string |
|
44
|
|
|
*/ |
|
45
|
|
|
private $chave_privada; |
|
46
|
|
|
/** |
|
47
|
|
|
* @var string |
|
48
|
|
|
*/ |
|
49
|
|
|
private $arquivo_chave_publica; |
|
50
|
|
|
/** |
|
51
|
|
|
* @var string |
|
52
|
|
|
*/ |
|
53
|
|
|
private $arquivo_chave_privada; |
|
54
|
|
|
/** |
|
55
|
|
|
* @var int |
|
56
|
|
|
*/ |
|
57
|
|
|
private $expiracao; |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* @param mixed $certificado array ou instância |
|
61
|
|
|
*/ |
|
62
|
107 |
|
public function __construct($certificado = []) |
|
63
|
|
|
{ |
|
64
|
107 |
|
$this->fromArray($certificado); |
|
65
|
107 |
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* Carrega o certificado PFX e permite salvar em arquivos PEM |
|
69
|
|
|
* @param string $arquivo_pfx caminho do arquivo PFX |
|
70
|
|
|
* @param string $senha senha do certificado digital |
|
71
|
|
|
* @param bool $extrair informa se deve salvar as chave em arquivos |
|
72
|
|
|
* @return self |
|
73
|
|
|
*/ |
|
74
|
3 |
|
public function carrega($arquivo_pfx, $senha, $extrair = false) |
|
75
|
|
|
{ |
|
76
|
3 |
|
if (!file_exists($arquivo_pfx)) { |
|
77
|
1 |
|
throw new \Exception(sprintf('O certificado "%s" não existe', $arquivo_pfx), 404); |
|
78
|
|
|
} |
|
79
|
2 |
|
$cert_store = file_get_contents($arquivo_pfx); |
|
80
|
2 |
|
if (!openssl_pkcs12_read($cert_store, $cert_info, $senha)) { |
|
81
|
1 |
|
throw new \Exception(sprintf('Não foi possível ler o certificado "%s"', $arquivo_pfx), 404); |
|
82
|
|
|
} |
|
83
|
1 |
|
$certinfo = openssl_x509_parse($cert_info['cert']); |
|
|
|
|
|
|
84
|
1 |
|
$this->setChavePrivada($cert_info['pkey']); |
|
85
|
1 |
|
$this->setChavePublica($cert_info['cert']); |
|
86
|
1 |
|
if ($extrair) { |
|
87
|
1 |
|
Util::createDirectory(dirname($this->getArquivoChavePrivada())); |
|
88
|
1 |
|
file_put_contents($this->getArquivoChavePrivada(), $this->getChavePrivada()); |
|
89
|
1 |
|
Util::createDirectory(dirname($this->getArquivoChavePublica())); |
|
90
|
1 |
|
file_put_contents($this->getArquivoChavePublica(), $this->getChavePublica()); |
|
91
|
|
|
} |
|
92
|
1 |
|
return $this; |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* Conteúdo da chave pública ou certificado no formato PEM |
|
97
|
|
|
* @return string |
|
98
|
|
|
*/ |
|
99
|
110 |
|
public function getChavePublica() |
|
100
|
|
|
{ |
|
101
|
110 |
|
return $this->chave_publica; |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
/** |
|
105
|
|
|
* Conteúdo da chave pública ou certificado no formato PEM |
|
106
|
|
|
* @param string $chave_publica |
|
107
|
|
|
* @return self |
|
108
|
|
|
*/ |
|
109
|
107 |
|
public function setChavePublica($chave_publica) |
|
110
|
|
|
{ |
|
111
|
107 |
|
$this->chave_publica = $chave_publica; |
|
112
|
107 |
|
$this->carregaChavePublica(); |
|
113
|
107 |
|
return $this; |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
/** |
|
117
|
|
|
* Conteúdo da chave privada do certificado no formato PEM |
|
118
|
|
|
* @return string |
|
119
|
|
|
*/ |
|
120
|
35 |
|
public function getChavePrivada() |
|
121
|
|
|
{ |
|
122
|
35 |
|
return $this->chave_privada; |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
/** |
|
126
|
|
|
* Conteúdo da chave privada do certificado no formato PEM |
|
127
|
|
|
* @param string $chave_privada |
|
128
|
|
|
* @return self |
|
129
|
|
|
*/ |
|
130
|
107 |
|
public function setChavePrivada($chave_privada) |
|
131
|
|
|
{ |
|
132
|
107 |
|
$this->chave_privada = $chave_privada; |
|
133
|
107 |
|
return $this; |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
/** |
|
137
|
|
|
* Informa o caminho do arquivo da chave pública ou certificado no formato |
|
138
|
|
|
* PEM |
|
139
|
|
|
* @return string |
|
140
|
|
|
*/ |
|
141
|
26 |
|
public function getArquivoChavePublica() |
|
142
|
|
|
{ |
|
143
|
26 |
|
return $this->arquivo_chave_publica; |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
|
|
/** |
|
147
|
|
|
* Informa o caminho do arquivo da chave pública ou certificado no formato |
|
148
|
|
|
* PEM |
|
149
|
|
|
* @param string $arquivo_chave_publica |
|
150
|
|
|
* @return self |
|
151
|
|
|
*/ |
|
152
|
107 |
|
public function setArquivoChavePublica($arquivo_chave_publica) |
|
|
|
|
|
|
153
|
|
|
{ |
|
154
|
107 |
|
$this->arquivo_chave_publica = $arquivo_chave_publica; |
|
155
|
107 |
|
if (file_exists($arquivo_chave_publica)) { |
|
156
|
103 |
|
$this->setChavePublica(file_get_contents($arquivo_chave_publica)); |
|
157
|
|
|
} |
|
158
|
107 |
|
return $this; |
|
159
|
|
|
} |
|
160
|
|
|
|
|
161
|
|
|
/** |
|
162
|
|
|
* Caminho do arquivo da chave privada do certificado no formato PEM |
|
163
|
|
|
* @return string |
|
164
|
|
|
*/ |
|
165
|
26 |
|
public function getArquivoChavePrivada() |
|
166
|
|
|
{ |
|
167
|
26 |
|
return $this->arquivo_chave_privada; |
|
168
|
|
|
} |
|
169
|
|
|
|
|
170
|
|
|
/** |
|
171
|
|
|
* Altera o caminho do arquivo da chave privada do certificado no formato PEM |
|
172
|
|
|
* @param string $arquivo_chave_privada |
|
173
|
|
|
* @return self |
|
174
|
|
|
*/ |
|
175
|
107 |
|
public function setArquivoChavePrivada($arquivo_chave_privada) |
|
|
|
|
|
|
176
|
|
|
{ |
|
177
|
107 |
|
$this->arquivo_chave_privada = $arquivo_chave_privada; |
|
178
|
107 |
|
if (file_exists($arquivo_chave_privada)) { |
|
179
|
103 |
|
$this->setChavePrivada(file_get_contents($arquivo_chave_privada)); |
|
180
|
|
|
} |
|
181
|
107 |
|
return $this; |
|
182
|
|
|
} |
|
183
|
|
|
|
|
184
|
|
|
/** |
|
185
|
|
|
* Data de expiração do certificado em timestamp |
|
186
|
|
|
* @return int |
|
187
|
|
|
*/ |
|
188
|
6 |
|
public function getExpiracao() |
|
189
|
|
|
{ |
|
190
|
6 |
|
return $this->expiracao; |
|
191
|
|
|
} |
|
192
|
|
|
|
|
193
|
|
|
/** |
|
194
|
|
|
* Informa a data de expiração do certificado em timestamp |
|
195
|
|
|
* @param int $expiracao |
|
196
|
|
|
* @return self |
|
197
|
|
|
*/ |
|
198
|
107 |
|
private function setExpiracao($expiracao) |
|
199
|
|
|
{ |
|
200
|
107 |
|
$this->expiracao = $expiracao; |
|
201
|
107 |
|
return $this; |
|
202
|
|
|
} |
|
203
|
|
|
|
|
204
|
|
|
/** |
|
205
|
|
|
* @param bool $recursive |
|
206
|
|
|
* @return array |
|
207
|
|
|
*/ |
|
208
|
3 |
|
public function toArray($recursive = false) |
|
|
|
|
|
|
209
|
|
|
{ |
|
210
|
3 |
|
$certificado = []; |
|
211
|
3 |
|
$certificado['arquivo_chave_publica'] = $this->getArquivoChavePublica(); |
|
212
|
3 |
|
$certificado['arquivo_chave_privada'] = $this->getArquivoChavePrivada(); |
|
213
|
3 |
|
$certificado['expiracao'] = $this->getExpiracao(); |
|
214
|
3 |
|
return $certificado; |
|
215
|
|
|
} |
|
216
|
|
|
|
|
217
|
|
|
/** |
|
218
|
|
|
* @param mixed $certificado array ou instância |
|
219
|
|
|
* @return self |
|
220
|
|
|
*/ |
|
221
|
107 |
|
public function fromArray($certificado = []) |
|
222
|
|
|
{ |
|
223
|
107 |
|
if ($certificado instanceof Certificado) { |
|
224
|
3 |
|
$certificado = $certificado->toArray(); |
|
225
|
107 |
|
} elseif (!is_array($certificado)) { |
|
226
|
1 |
|
return $this; |
|
227
|
|
|
} |
|
228
|
107 |
|
if (isset($certificado['chave_publica'])) { |
|
229
|
1 |
|
$this->setChavePublica($certificado['chave_publica']); |
|
230
|
|
|
} else { |
|
231
|
107 |
|
$this->setChavePublica(null); |
|
232
|
|
|
} |
|
233
|
107 |
|
if (isset($certificado['chave_privada'])) { |
|
234
|
1 |
|
$this->setChavePrivada($certificado['chave_privada']); |
|
235
|
|
|
} else { |
|
236
|
107 |
|
$this->setChavePrivada(null); |
|
237
|
|
|
} |
|
238
|
107 |
|
if (isset($certificado['arquivo_chave_publica'])) { |
|
239
|
3 |
|
$this->setArquivoChavePublica($certificado['arquivo_chave_publica']); |
|
240
|
|
|
} else { |
|
241
|
107 |
|
$this->setArquivoChavePublica(null); |
|
242
|
|
|
} |
|
243
|
107 |
|
if (isset($certificado['arquivo_chave_privada'])) { |
|
244
|
3 |
|
$this->setArquivoChavePrivada($certificado['arquivo_chave_privada']); |
|
245
|
|
|
} else { |
|
246
|
107 |
|
$this->setArquivoChavePrivada(null); |
|
247
|
|
|
} |
|
248
|
107 |
|
return $this; |
|
249
|
|
|
} |
|
250
|
|
|
|
|
251
|
|
|
/** |
|
252
|
|
|
* Carrega a data de exipiração pela chave pública |
|
253
|
|
|
*/ |
|
254
|
107 |
|
private function carregaChavePublica() |
|
255
|
|
|
{ |
|
256
|
107 |
|
if (is_null($this->getChavePublica())) { |
|
257
|
107 |
|
$this->setExpiracao(null); |
|
258
|
|
|
} else { |
|
259
|
104 |
|
$cert = @openssl_x509_read($this->getChavePublica()); |
|
260
|
104 |
|
$cert_data = openssl_x509_parse($cert); |
|
261
|
104 |
|
$this->setExpiracao(isset($cert_data['validTo_time_t']) ? $cert_data['validTo_time_t'] : null); |
|
262
|
|
|
} |
|
263
|
107 |
|
} |
|
264
|
|
|
|
|
265
|
|
|
/** |
|
266
|
|
|
* Ao chamar essa função o certificado precisa estar válido (não expirado) |
|
267
|
|
|
* @throws \Exception quando o certificado estiver expirado ou não informado |
|
268
|
|
|
*/ |
|
269
|
2 |
|
public function requerValido() |
|
270
|
|
|
{ |
|
271
|
2 |
|
if (is_null($this->getExpiracao())) { |
|
272
|
1 |
|
throw new \Exception('A data de expiração do certificado não foi informada', 401); |
|
273
|
1 |
|
} elseif ($this->getExpiracao() < time()) { |
|
274
|
1 |
|
throw new \Exception('O certificado digital expirou', 500); |
|
275
|
|
|
} |
|
276
|
|
|
} |
|
277
|
|
|
|
|
278
|
|
|
/** |
|
279
|
|
|
* Obtém o certificado representado como XML |
|
280
|
|
|
* @param string $name nome da tag raiz do XML |
|
281
|
|
|
* @return \DOMElement |
|
282
|
|
|
*/ |
|
283
|
1 |
|
public function getNode($name = null) |
|
284
|
|
|
{ |
|
285
|
1 |
|
throw new \Exception('Não implementado', 500); |
|
286
|
|
|
} |
|
287
|
|
|
|
|
288
|
|
|
/** |
|
289
|
|
|
* Carrega o certificado de um XML |
|
290
|
|
|
* @param \DOMElement $element elemento do xml que será carregado |
|
291
|
|
|
* @param string $name nome da tag raiz do XML |
|
292
|
|
|
* @return \DOMElement elemento que foi carregado |
|
293
|
|
|
*/ |
|
294
|
1 |
|
public function loadNode($element, $name = null) |
|
295
|
|
|
{ |
|
296
|
1 |
|
throw new \Exception('Não implementado', 500); |
|
297
|
|
|
} |
|
298
|
|
|
} |
|
299
|
|
|
|
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVarassignment in line 1 and the$higherassignment in line 2 are dead. The first because$myVaris never used and the second because$higheris always overwritten for every possible time line.