source: trunk/seguranca/classes_BKP/funcoes_auxiliares.php @ 1035

Revision 1035, 18.8 KB checked in by rafaelraymundo, 15 years ago (diff)

Ticket #558 - Adicionada funcionalidade de assinatura e criptografia de e-mails.

Line 
1<?php
2//  Conjunto de funcoes de apoio as Classes de tratamento do certificado digital.
3               
4        function data_hora ($valor)
5        {
6                $year  = substr($valor, 0, 2);
7                $month = substr($valor, 2, 2);
8                $day   = substr($valor, 4, 2);
9                $hour  = substr($valor, 6, 2);
10                $min   = substr($valor, 8, 2);
11                $sec   = substr($valor, 10, 2);
12                return gmdate('YmdHis',gmmktime($hour, $min, $sec, $month, $day, $year));
13        }
14
15        function gera_nome_arquivo_temporario($tab_arqs)
16        {
17                //include('Verifica_Certificado_conf.php');
18                if(!is_dir($GLOBALS['dirtemp']))
19                {
20                        #Se nao existe uma area de trabalho aborta a funcao..
21                        return false;
22                }
23                $lista = array('A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z');
24                $N = $lista[rand(0,count($lista)-1)].date('U').$lista[rand(0,count($lista)-1)].RAND(12345,9999999999).$lista[rand(0,count($lista)-1)].$lista[rand(0,count($lista)-1)].RAND(12345,9999999999).'.tmp';
25                $aux = $GLOBALS['dirtemp'].'/'.$N;;
26                array_push($tab_arqs ,$aux);
27                return  $aux;           
28        }
29       
30        function grava_arquivo($arquivo,$dados)
31        {
32                $ret = file_put_contents($arquivo,$dados);
33                if(!$ret)
34                {
35                        return false;
36                }
37                else
38                        return $ret;
39        }
40       
41       
42        function deleta_arquivos_temporarios($tab_arqs)
43        {
44                foreach($tab_arqs as $arquivo ){ unlink($arquivo); }   
45        }
46
47        function verificaopenssl()
48        {
49                if(extension_loaded('openssl'))
50                {
51                        return true;
52                }
53                else
54                        return false;
55        }
56   
57        function Crl_parseASN($data)
58        { 
59                $_oids = array(
60                        '2.5.4.3' => 'CN',
61                        '2.5.4.4' => 'Surname',
62                        '2.5.4.6' => 'C',
63                        '2.5.4.7' => 'StateOrProvince',
64                        '2.5.4.8' => 'Location',
65                        '2.5.4.9' => 'StreetAddress',
66                        '2.5.4.10' => 'O',
67                        '2.5.4.11' => 'OU',
68                        '2.5.4.12' => 'Title',
69                        '2.5.4.20' => 'TelephoneNumber',
70                        '2.5.4.42' => 'GivenName',
71                        '2.5.29.14' => 'id-ce-subjectKeyIdentifier',
72                        '2.5.29.15' => 'id-ce-keyUsage',
73                        '2.5.29.17' => 'id-ce-subjectAltName',
74                        '2.5.29.19' => 'id-ce-basicConstraints',
75                        '2.5.29.20' => 'id-ce-cRLNumber',
76                        '2.5.29.31' => 'id-ce-CRLDistributionPoints',
77                        '2.5.29.32' => 'id-ce-certificatePolicies',
78                        '2.5.29.35' => 'id-ce-authorityKeyIdentifier',
79                        '2.5.29.37' => 'id-ce-extKeyUsage',
80                        '1.2.840.113549.1.9.1' => 'Email',
81                        '1.2.840.113549.1.1.1' => 'RSAEncryption',
82                        '1.2.840.113549.1.1.2' => 'md2WithRSAEncryption',
83                        '1.2.840.113549.1.1.4' => 'md5withRSAEncryption',
84                        '1.2.840.113549.1.1.5' => 'SHA-1WithRSAEncryption',
85                        '1.2.840.10040.4.3' => 'id-dsa-with-sha-1',
86                        '1.3.6.1.5.5.7.3.2' => 'id_kp_clientAuth',
87                        '1.3.6.1.5.5.7.3.4' => 'id_kp_securityemail',   
88                        '1.3.6.1.5.5.7.2.1' => 'id_certificatePolicies',
89                        '2.16.840.1.113730.1.1' => 'netscape-cert-type',
90                        '2.16.840.1.113730.1.2' => 'netscape-base-url',
91                        '2.16.840.1.113730.1.3' => 'netscape-revocation-url',
92                        '2.16.840.1.113730.1.4' => 'netscape-ca-revocation-url',
93                        '2.16.840.1.113730.1.7' => 'netscape-cert-renewal-url',
94                        '2.16.840.1.113730.1.8' => 'netscape-ca-policy-url',
95                        '2.16.840.1.113730.1.12' => 'netscape-ssl-server-name',
96                        '2.16.840.1.113730.1.13' => 'netscape-comment',
97                        '2.16.76.1.2.1' => 'A1',       
98                        '2.16.76.1.2.3' => 'A3',       
99                        '2.16.76.1.2.1.16' => 'Certification Practice Statement pointer',
100                        '2.16.76.1.3.1' => 'Dados do cert parte 1',
101                        '2.16.76.1.3.5' => 'Dados do cert parte 2',
102                        '2.16.76.1.3.6' => 'Dados do cert parte 3',     
103                                );
104   
105                $result = array();
106                $rxyz = array();
107               
108                while (strlen($data) > 1)
109                {
110                        $class = ord($data[0]);
111                        switch ($class)
112                        {
113                        case 0x30:
114                                // Sequence
115                                $len = ord($data[1]);
116                                $bytes = 0;
117                                if ($len & 0x80)
118                                {
119                                        $bytes = $len & 0x0f;
120                                        $len = 0;
121                                        for ($i = 0; $i < $bytes; $i++)
122                                        {
123                                                $len = ($len << 8) | ord($data[$i + 2]);
124                                        }
125                                }
126                               
127                                $sequence_data = substr($data, 2 + $bytes, $len);
128                                $data = substr($data, 2 + $bytes + $len);
129
130                                $values = Crl_parseASN($sequence_data);
131                                if (!is_array($values) || is_string($values[0]))
132                                {
133                                        $values = array($values);
134                                }
135                                $sequence_values = array();
136                                $i = 0;
137                                foreach ($values as $val)
138                                {
139                                        if ($val[0] == 'extension')
140                                        {
141                                                $sequence_values['extensions'][] = $val;
142                                        }
143                                        else
144                                        {
145                                                $sequence_values[$i++] = $val;
146                                        }
147                                }
148                                $result[] = array('sequence', $sequence_values);
149                                break;
150
151                        case 0x31:
152                                // Set of
153                                $len = ord($data[1]);
154                                $bytes = 0;
155                                if ($len & 0x80)
156                                {
157                                        $bytes = $len & 0x0f;
158                                        $len = 0;
159                                        for ($i = 0; $i < $bytes; $i++)
160                                        {
161                                                $len = ($len << 8) | ord($data[$i + 2]);
162                                        }
163                                }
164                                $sequence_data = substr($data, 2 + $bytes, $len);
165                                $data = substr($data, 2 + $bytes + $len);
166                                $result[] = array('set', Crl_parseASN($sequence_data));
167                                break;
168
169                        case 0x01:
170                                // Boolean type
171                                $boolean_value = (ord($data[2]) == 0xff);
172                                $data = substr($data, 3);
173                                $result[] = array('boolean', $boolean_value);
174                                break;
175                       
176                        case 0x02:
177                                // Integer type
178                                $len = ord($data[1]);
179                                $bytes = 0;
180                                if ($len & 0x80)
181                                {
182                                        $bytes = $len & 0x0f;
183                                        $len = 0;
184                                        for ($i = 0; $i < $bytes; $i++)
185                                        {
186                                                $len = ($len << 8) | ord($data[$i + 2]);
187                                        }
188                                }
189
190                                $integer_data = substr($data, 2 + $bytes, $len);
191                                $data = substr($data, 2 + $bytes + $len);
192                                if($len == 16)
193                                {
194                                        $result[] = array('integer(' . $len . ')', $integer_data);
195                                        break;
196                                }
197                                else
198                                {
199                                                                $value = 0;
200                                if ($len <= 4)
201                                {
202                                        // Method works fine for small integers
203                                        for ($i = 0; $i < strlen($integer_data); $i++)
204                                        {
205                                                $value = ($value << 8) | ord($integer_data[$i]);
206                                        }
207                                }
208                                else
209                                {
210                                        // Method works for arbitrary length integers
211                                        if (extension_loaded('bcmath'))
212                                        {
213                                                for ($i = 0; $i < strlen($integer_data); $i++)
214                                                {
215                                                        $value = bcadd(bcmul($value, 256), ord($integer_data[$i]));
216                                                }
217                                        }
218                                        else
219                                        {
220                                                $value = -1;
221                                        }
222                                }
223                                $result[] = array('integer(' . $len . ')', $value);
224                                break;
225                                }
226                        /*
227                        case 0x02:
228                                // Integer type
229                                $len = ord($data[1]);
230                                $bytes = 0;
231                                if ($len & 0x80)
232                                {
233                                        $bytes = $len & 0x0f;
234                                        $len = 0;
235                                        for ($i = 0; $i < $bytes; $i++)
236                                        {
237                                                $len = ($len << 8) | ord($data[$i + 2]);
238                                        }
239                                }
240
241                                $integer_data = substr($data, 2 + $bytes, $len);
242                                $data = substr($data, 2 + $bytes + $len);
243
244                                $value = 0;
245                                if ($len <= 4)
246                                {
247                                        // Method works fine for small integers
248                                        for ($i = 0; $i < strlen($integer_data); $i++)
249                                        {
250                                                $value = ($value << 8) | ord($integer_data[$i]);
251                                        }
252                                }
253                                else
254                                {
255                                        // Method works for arbitrary length integers
256                                        if (extension_loaded('bcmath'))
257                                        {
258                                                for ($i = 0; $i < strlen($integer_data); $i++)
259                                                {
260                                                        $value = bcadd(bcmul($value, 256), ord($integer_data[$i]));
261                                                }
262                                        }
263                                        else
264                                        {
265                                                $value = -1;
266                                        }
267                                }
268                                $result[] = array('integer(' . $len . ')', $value);
269                                break;
270                        */ 
271                        case 0x03:
272                                // Bitstring type
273                                $len = ord($data[1]);
274                                $bytes = 0;
275                                if ($len & 0x80)
276                                {
277                                        $bytes = $len & 0x0f;
278                                        $len = 0;
279                                        for ($i = 0; $i < $bytes; $i++)
280                                        {
281                                                $len = ($len << 8) | ord($data[$i + 2]);
282                                        }
283                                }
284                                $bitstring_data = substr($data, 3 + $bytes, $len);
285                                $data = substr($data, 2 + $bytes + $len);
286                                $result[] = array('bit string',$bitstring_data);
287                                break;
288
289                        case 0x04:
290                                // Octetstring type
291                                $len = ord($data[1]);
292                                $bytes = 0;
293                                if ($len & 0x80)
294                                {
295                                        $bytes = $len & 0x0f;
296                                        $len = 0;
297                                        for ($i = 0; $i < $bytes; $i++)
298                                        {
299                                                $len = ($len << 8) | ord($data[$i + 2]);
300                                        }
301                                }
302                                $octectstring_data = substr($data, 2 + $bytes, $len);
303                                $data = substr($data, 2 + $bytes + $len);
304                                $result[] = array('octet string', $octectstring_data);
305                                break;
306
307                        case 0x05:
308                                // Null type
309                                $data = substr($data, 2);
310                                $result[] = array('null', null);
311                                break;
312
313                        case 0x06:
314                                // Object identifier type
315                                $len = ord($data[1]);
316                                $bytes = 0;
317                                if ($len & 0x80)
318                                {
319                                        $bytes = $len & 0x0f;
320                                        $len = 0;
321                                        for ($i = 0; $i < $bytes; $i++)
322                                        {
323                                                $len = ($len << 8) | ord($data[$i + 2]);
324                                        }
325                                }
326                                $oid_data = substr($data, 2 + $bytes, $len);
327                                $data = substr($data, 2 + $bytes + $len);
328
329                                // Unpack the OID
330                                $plain  = floor(ord($oid_data[0]) / 40);
331                                $plain .= '.' . ord($oid_data[0]) % 40;
332
333                                $value = 0;
334                                $i = 1;
335                                while ($i < strlen($oid_data))
336                                {
337                                        $value = $value << 7;
338                                        $value = $value | (ord($oid_data[$i]) & 0x7f);
339
340                                        if (!(ord($oid_data[$i]) & 0x80))
341                                        {
342                                                $plain .= '.' . $value;
343                                                $value = 0;
344                                        }
345                                        $i++;
346                                }
347                                //echo $plain.' === '.$_oids[$plain].'<br>';
348                                if (isset($_oids[$plain]))
349                                {
350                                        $result[] = array('oid', $_oids[$plain]);
351                                }
352                                else
353                                {
354                                        $result[] = array('oid', $plain);
355                                }
356                                break;
357
358                        case 0x12:
359                        case 0x13:
360                        case 0x14:
361                        case 0x15:
362                        case 0x16:
363                        case 0x81:
364                        case 0x80:
365                                // Character string type
366                                $len = ord($data[1]);
367                                $bytes = 0;
368                                if ($len & 0x80)
369                                {
370                                        $bytes = $len & 0x0f;
371                                        $len = 0;
372                                        for ($i = 0; $i < $bytes; $i++)
373                                        {
374                                                $len = ($len << 8) | ord($data[$i + 2]);
375                                        }
376                                }
377                                $string_data = substr($data, 2 + $bytes, $len);
378                                $data = substr($data, 2 + $bytes + $len);
379                                $result[] = array('string', $string_data);
380                                break;
381
382                        case 0x17:
383                                // Time types
384                                $len = ord($data[1]);
385                                $bytes = 0;
386                                if ($len & 0x80)
387                                {
388                                        $bytes = $len & 0x0f;
389                                        $len = 0;
390                                        for ($i = 0; $i < $bytes; $i++)
391                                        {
392                                                $len = ($len << 8) | ord($data[$i + 2]);
393                                        }
394                                }
395                                $time_data = substr($data, 2 + $bytes, $len);
396                                $data = substr($data, 2 + $bytes + $len);
397                                $result[] = array('utctime', $time_data);
398                                break;
399
400                        case 0x82:
401                                // X509v3 extensions?
402                                $len = ord($data[1]);
403                                $bytes = 0;
404                                if ($len & 0x80)
405                                {
406                                        $bytes = $len & 0x0f;
407                                        $len = 0;
408                                        for ($i = 0; $i < $bytes; $i++)
409                                        {
410                                                $len = ($len << 8) | ord($data[$i + 2]);
411                                        }
412                                }
413                                $sequence_data = substr($data, 2 + $bytes, $len);
414                                $data = substr($data, 2 + $bytes + $len);
415                                $result[] = array('extension', 'X509v3 extensions');
416                                $result[] = Crl_parseASN($sequence_data);
417                                break;
418
419                        case 0xa0:
420                        case 0xa3:
421                                // Extensions
422                                $extension_data = substr($data, 0, 2);
423                                $data = substr($data, 2);
424                                $result[] = array('extension', dechex($extension_data));
425                                break;
426
427                        case 0xe6:
428                                $extension_data = substr($data, 0, 1);
429                                $data = substr($data, 1);
430                                $result[] = array('extension', dechex($extension_data));
431                                break;
432
433                        case 0xa1:
434                                $extension_data = substr($data, 0, 1);
435                                $data = substr($data, 6);
436                                $result[] = array('extension', dechex($extension_data));
437                                break;
438
439                        default:
440                                // Unknown
441                                $result[] = array('UNKNOWN', dechex($data));
442                                $data = '';
443                                break;
444                        }
445        }
446
447        return (count($result) > 1) ? $result : array_pop($result);
448
449   }
450   
451   function openssl_to_timestamp ($in)
452   {
453        $year  = substr($in, 0, 4); /* NOTE: Yes, this returns a two digit year */
454        $month = substr($in, 4, 2);
455        $day   = substr($in, 6, 2);
456        $hour  = substr($in, 8, 2);
457        $min   = substr($in, 10, 2);
458        $sec   = substr($in, 12, 2);
459   
460        return gmmktime($hour, $min, $sec, $month, $day, $year);
461        }
462
463#============================================================================================
464#============================================================================================
465#============================================================================================
466
467# Transforma o certificado do formato PEM para o formato DER ...
468function pem2der($pem_data)
469        {
470                $begin = "CERTIFICATE-----";
471                $end   = "-----END";
472                $pem_data = substr($pem_data, strpos($pem_data, $begin)+strlen($begin));   
473                $pem_data = substr($pem_data, 0, strpos($pem_data, $end));
474                $der = base64_decode($pem_data);
475                return $der;
476        }
477
478# Recupera dados da oid passada como parametro.....
479function parse($oid,$valor)
480        {
481                $oids = array('1.3.1' => array('1'=>array('NASCIMENTO',8),
482                                                               '2'=>array('CPF',11),
483                                                               '3'=>array('NIS',11),
484                                                               '4'=>array('RG',15),
485                                                               '5'=>array('ORGAOUF',6)),
486                                     '1.3.5' => array('1'=>array('TITULO',12),
487                                                              '2'=>array('ZONA',3),
488                                                              '3'=>array('SECAO',4),
489                                                              '4'=>array('TITULO_CIDADE_UF',0)),
490                                     '1.3.6' => array('1'=>array('CADINSS',12)));
491                                     
492                $resultado = array();
493                $esta_oid = $oids[$oid];
494                $X = intval(substr($oid,4,1));
495                $p = 0;
496                for($i=1;$i < count($esta_oid) + 1; $i++)
497                        {
498                                if($esta_oid[$i][1] == 0)
499                                        {
500                                                # se igual a zero, então esta apontando um ultimo elemento, iniciando em $p, até o fim dos dados
501                                                $tamanho = strlen($valor) - $p;
502                                        }
503                                else
504                                        {
505                                                $tamanho = $esta_oid[$i][1];
506                                        }
507                                $resultado[$esta_oid[$i][0]] = substr($valor,$p,$tamanho);
508                                $p = $p + $esta_oid[$i][1];
509                        }
510                return $resultado;
511        } 
512
513function subjectAltName($cert_data,$KK)
514        {
515                $dados = array();
516                $AUX = Crl_parseASN( $cert_data[1][0][1][$KK][1][1][1]);
517                $P1 = $AUX[1][1][1];
518                $P2 = $AUX[1][3][1];
519                $P3 = $AUX[1][5][1];
520                $P4 = $AUX[1][6][1];   
521                $dados = array_merge(parse('1.3.1',$P1),
522                                        parse('1.3.5',$P2),
523                                        parse('1.3.6',$P3),$dados);
524                $dados['EMAIL'] = $P4;
525               
526                return $dados;
527        }
528       
529function subjectAltName_B($cert_data,$KK)
530        {
531                $dados = array();
532                $AUX = Crl_parseASN($KK);
533                //echo '<pre>';
534                //print_r($AUX);
535                //echo '</pre>';
536                if(is_array($AUX[0]))
537                {
538                        $AUXx = $AUX[0];
539                        $AUX = $AUXx;
540                }
541                $P1 = $AUX[1][1][1];
542                $P2 = $AUX[1][3][1];
543                $P3 = $AUX[1][5][1];
544                $P4 = $AUX[1][6][1];   
545                $dados = array_merge(parse('1.3.1',$P1),
546                                        parse('1.3.5',$P2),
547                                        parse('1.3.6',$P3),$dados);
548                if($P4 != '1.3.6.1.4.1.311.20.2.3')   // esta oid indica domínio de login em estações de trabalho . Pegar o 2. item como email.
549                        {
550                                $dados['EMAIL'] = $P4;
551                        }
552                else
553                        {
554                                $dados['EMAIL'] = $AUX[1][8][1];                       
555                        }
556                       
557                return $dados;
558        }
559
560function CRLDistributionPoints($cert_data,$KK)
561        {
562                $dados = array();
563                $AUX = explode('http:',$cert_data[1][0][1][$KK][1][1][1]);
564                if($AUX[1])
565                        {
566                                $dados['CRLDISTRIBUTIONPOINTS'] = 'http:' . $AUX[1];
567                        }
568                else
569                        {
570                                $dados['CRLDISTRIBUTIONPOINTS'] = '';
571                        }
572                       
573                return $dados;         
574        }       
575       
576function SERIALNUMBER($cert_data,$KK)
577        {
578                $dados = array();
579                $dados['SERIALNUMBER'] = $cert_data[1][0][1][$KK][1];
580
581                return $dados;                 
582        }
583       
584function SUBJECT($cert_data,$KK)
585        {
586                $dados = array();
587                $AUX1 = '/';
588                foreach($cert_data[1][0][1][$KK][1] as $AUX2)
589                        {
590                                $AUX1 .= $AUX2[1][1][0][1] . '=' . $AUX2[1][1][1][1] . '/';
591                        }
592                $dados['SUBJECT'] = substr($AUX1,0,strlen($AUX1)-1);
593                $AUX1 = explode('CN=',$dados['SUBJECT']);
594                $AUX2 = explode('/',$AUX1[1]);
595                $AUX3 = explode(':',$AUX2[0]);
596                $dados['NOME'] = $AUX3[0];
597                return $dados;                 
598        }
599               
600function ISSUER($cert_data,$KK)         
601        {
602                $dados = array();       
603                $AUX1 = '/';
604                foreach($cert_data[1][0][1][$KK][1] as $AUX2)
605                        {
606                                $AUX1 .= $AUX2[1][1][0][1] . '=' . $AUX2[1][1][1][1] . '/';
607                        }
608                $AUX1 = explode('CN=',substr($AUX1,0,strlen($AUX1)-1));
609                $AUX2 = explode('/',$AUX1[1]);
610                $dados['EMISSOR'] = $AUX2[0];
611               
612                return $dados; 
613               
614                }               
615
616function BEFOREAFTER($cert_data,$KK)
617        {
618                $dados = array();
619                $dados['INICIO_VALIDADE'] = data_hora($cert_data[1][0][1][$KK][1][0][1]);
620                $dados['FIM_VALIDADE'] = data_hora($cert_data[1][0][1][$KK][1][1][1]);
621                $agora = date('YmdHis');
622                if(($agora < $dados['INICIO_VALIDADE']) || ($agora > $dados['FIM_VALIDADE']))
623                        {
624                                $dados['EXPIRADO'] = true;
625                        }
626                else
627                        {
628                                $dados['EXPIRADO'] = false;
629                        }       
630                return $dados;         
631                }       
632
633function AUTHORITYKEYIDENTIFIER($cert_data,$KK)
634        {
635                $dados = array();
636                $aux1 = Crl_parseASN( $cert_data[1][0][1][$KK][1][1][1]);
637                $aux2='';
638                if (isset($aux1[1][0][1]))
639                {
640                        $tab_val = array('0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F');
641                        for($A=0;$A<strlen($aux1[1][0][1]);$A++)
642                                {
643                                        $aux_parte_numerica =ord(substr($aux1[1][0][1],$A,1)) % 16;
644                                        $aux_parte_zona = (ord(substr($aux1[1][0][1],$A,1)) - $aux_parte_numerica) / 16;
645                                        $parte_numerica = $tab_val[$aux_parte_numerica];
646                                        $parte_zona = $tab_val[$aux_parte_zona];
647                                        $aux2 .=  $parte_zona.$parte_numerica;
648                                }
649                        $dados['AUTHORITYKEYIDENTIFIER'] = $aux2;
650                }
651                else
652                        {
653                                $dados['AUTHORITYKEYIDENTIFIER'] = "auto-assinado";
654                        }
655                return $dados;                 
656        }
657       
658function AUTHORITYKEYIDENTIFIER_B($cert_data,$KK)
659        {
660                $dados = array();
661                $aux2='';
662                if (isset($KK))
663                {
664                        $tab_val = array('0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F');
665                        for($A=0;$A<strlen($KK);$A++)
666                                {
667                                        $aux_parte_numerica =ord(substr($KK,$A,1)) % 16;
668                                        $aux_parte_zona = (ord(substr($KK,$A,1)) - $aux_parte_numerica) / 16;
669                                        $parte_numerica = $tab_val[$aux_parte_numerica];
670                                        $parte_zona = $tab_val[$aux_parte_zona];
671                                        $aux2 .=  $parte_zona.$parte_numerica;
672                                }
673                        $dados['AUTHORITYKEYIDENTIFIER'] = $aux2;
674                }
675                else
676                        {
677                                $dados['AUTHORITYKEYIDENTIFIER'] = "auto-assinado";
678                        }
679                return $dados;                 
680        }
681
682function recupera_dados_do_ceritificado_digital($certificado_digital_formato_pem)
683        {
684                $X509_PARSE = openssl_x509_parse($certificado_digital_formato_pem);
685                $cert_der =  pem2der($certificado_digital_formato_pem);
686                $aux11 = explode(chr(0x30).chr(0x16).chr(0x80).chr(0x14),$cert_der);
687                //echo '<pre>';
688                //print_r( $aux11);
689                //echo '</pre>';
690                $caid = substr($aux11[1],0,20);
691                $aux11 = explode(chr(0x30).chr(0x81),$cert_der);
692               
693                //echo '<pre>';
694                //print_r( Crl_parseASN(chr(0x30).chr(0x81).$aux11[6]));
695                //echo '</pre>';
696               
697                //$oids_icp = $X509_PARSE['extensions']['subjectAltName'];
698                if(count($aux11)<9)
699                        {
700                                $ky = 6;
701                        }
702                else
703                        {
704                                $ky = 5;
705                        }
706                $oids_icp = chr(0x30).chr(0x81).$aux11[$ky];
707               
708                $funcoes = array(1 => SERIALNUMBER,
709                           3 =>  ISSUER,
710                           4 => BEFOREAFTER,
711                           5 => SUBJECT,
712                           $caid => AUTHORITYKEYIDENTIFIER_B,
713                           $oids_icp => subjectAltName_B);
714                           
715                $dados=array();
716                $cert_data = Crl_parseASN( $cert_der);
717                foreach($funcoes as $parametro => $funcao)
718                        {
719                                $dados= array_merge($dados,$funcao($cert_data,$parametro));
720                        }
721
722                if(!$dados['EMAIL'])
723                        {
724                                $AUX2 = explode('Email=',$dados['SUBJECT']);
725                                $dados['EMAIL'] = $AUX2[1];
726                        }
727                       
728                if($dados['EMAIL'] == '')
729                        {
730                                $x = ord(substr($aux11[$ky+1],0));
731                                $dados['EMAIL'] = substr($aux11[$ky+1],1,$x);
732                        }
733
734                if(!$dados['CRLDISTRIBUTIONPOINTS'])
735                        {
736                                $AUX1 =  explode('http:',$X509_PARSE['extensions']['crlDistributionPoints']);
737                                $AUX2 = explode('.crl',$AUX1[1]);
738                                if($AUX2[0])
739                                        {
740                                                $dados['CRLDISTRIBUTIONPOINTS'] = 'http:' . $AUX2[0] . '.crl';
741                                        }
742                                else
743                                        {
744                                                $AUX1 = explode('.crl', $cert_der);
745                                                $AUX2 = explode('http:',$AUX1[0]);
746                                                $dados['CRLDISTRIBUTIONPOINTS'] = 'http:' .$AUX2[1] . '.crl';
747                                        }
748                        }
749                       
750                return $dados;         
751       
752        }
753
754#============================================================================================
755#============================================================================================
756#============================================================================================
757
758?>
Note: See TracBrowser for help on using the repository browser.