Code

Certificate informations shown now
[gosa.git] / include / class_certificate.inc
1 <?php
3 /* definitions */
5 /* certificates */
6 define("PEM","pem");
7 define("DER","der");
9 class certificate 
10 {
11   /* vars */
12   var $data;
13   var $type;
14   var $error;
16   /* Initialize all vars*/ 
17   function certificate()
18   {
19     $this->data= "";
20     $this->type= false;
21     $this->error="";  
22     $this->info = array();
23   } 
25   /* Reads specified Certfile/string and convert it to PEM*/
26   function import($data,$type=false)
27   {
28     /* if is file read from file, else use string as it is*/
29     if(is_file($data))  {
30       $fp = fopen($data,"r+");
31       $str = "";
33       if(!$fp){
34         $this->certificate();
35         $this->error=_("Can't open specified file, check accessibility and or existence");
36         return(false);
37       }else{
38         /* Reading data*/
39         while(!feof($fp)){
40           $str.=fgets($fp,1024);
41         }
42       }
43       /* Filename given, so we use the data from the file */
44       $this->data = $str;
45     } else {
46       /* Cert as String, use this string */
47       $this->data = $data;
48     }
50     /* Data can't be empty */
51     if($data = ""){
52       $this->certificate();
53       $this->error = _("Can't read specified certificate / or empty string given"); 
54       return(false);
55     }
57     /* Prefer specified certtype*/
58     if($type) {
59       $this->type = $type;  
60     }else{
61       /* Detect certtype, cause there is none specified */      
63       /* PEM allways starts with ----BEGIN CERTIFICATE-----*/
64       if(strstr($this->data,"CERTIFICATE"))  {
65         $this->type=PEM;
66       } else {
67         /* We test DER now, on fail abort */
68         $this->type=DER;
69       }
70     }
72     /* Convert to PEM to give $this->info the ability to read the cert */  
73     if($this->type == DER )    {
74       $this->derTOpem();
75     }
77     /* If cert is loaded correctly and is PEM now, we could read some data out of it */
78     if(count($this->info()) <=1)  {
79       $this->certificate();
80       $this->error = _("Can't load certificate, possibly unsupported format (use PEM/DER) ");
81       /* Reset*/
82       return(false);
83     }
84   
85     $this->info(false);
86   
87     /* Loaded a readable cert */
88     return(true);
89   }
91   /* Returns Array with all containing data */
92   function info($ret = true)
93   {
94     if($this->type != PEM){
95       $this->error = _("The Format must be PEM, to output certificate informations");
96       return(false);
97     } else {
98     /* return an array with all given information */
99     $this->info=openssl_x509_parse($this->data);
100     
101     if($ret)
102       return($this->info);
103     }
104   }
106   /* Return Functions */
107   function getvalidto_date()
108   {
109     return($this->info['validTo_time_t']);
110   } 
112   function getvalidfrom_date()
113   {
114     return($this->info['validFrom_time_t']);
115   }
117   function getname()
118   {
119     return($this->info['name']);
120   }
122   function getCN()
123   {
124     return($this->info['subject']['CN']);
125   }
127   function getO()
128   {
129     return($this->info['subject']['O']);
130   }
132   function getOU()
133   {
134     return($this->info['subject']['OU']);
135   }
137   
139   /* Export Certificate to specified file, with specified method*/
140   function export($type,$filename="temp")
141   {
142     /* Check if valid cert is loaded*/
143     if($this->type!=false){
144       /* Check if we must convert the cert */
145       if($this->type!= $type){
146         $strConv = $this->type."TO".$type;
147         $this->$strConv();
148       }    
150       /* open file for writing */
151       $fp = fopen($filename,"w+");
153       if(!$fp){
154         $this->error= _("Can't create/open File");
155         return(false);
156       }else{
157         fwrite($fp,$this->data,strlen($this->data));        
158       }
159       return(true);
160     }else{
161       $this->error= _("No valid certificate loaded");
162       return(false);    
163     }
164     return(false);
165   } 
168   /* Convert der to pem Certificate */
169   function derTOpem()
170   {
171     /* if type is DER start convert */
172     if($this->type == DER)    {
173       /* converting */
174       $this->type= PEM; 
175       $str = base64_encode($this->data);
176       $len = strlen($str);
178       $end = "";
180       while($len > 0 )      {
181         $len = $len - 64;
182         $str1 = substr($str,0,64)."\n";
183         $str  = substr($str,64,$len);
184         $end.= $str1;
185       }
187       $strend = "-----BEGIN CERTIFICATE-----\n".$end;
188       $strend .= "-----END CERTIFICATE-----";
190       $this->data     = $strend;
191       return(true); 
192     }
193     return(false);
194   }
196   /*Convert pem to der Certificate */
197   function pemTOder()
198   {
199     if($this->type == PEM)    {
200       $this->type= DER;
202       $str = $this->data;
204       $str = str_replace("-----BEGIN CERTIFICATE-----","",$str);
205       $str = str_replace("-----END CERTIFICATE-----","",$str);
207       $str = base64_decode($str);
209       $this->data = $str;
210       return(true);
211     }
212     return(false);
213   }
217 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
218 ?>