Code

Added certificate
[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   } 
24   /* Reads specified Certfile/string and convert it to PEM*/
25   function import($data,$type=false)
26   {
27     /* if is file read from file, else use string as it is*/
28     if(is_file($data))
29     {
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     /* Loaded a readable cert */
85     return(true);
86   }
88   /* Returns Array with all containing data */
89   function info()
90   {
91     if($this->type != PEM){
92       $this->error = _("The Format must be PEM, to output certificate informations");
93       return(false);
94     } else {
95     /* return an array with all given information */
96     return(openssl_x509_parse($this->data));
97     }
98   }
101   /* Export Certificate to specified file, with specified method*/
102   function export($type,$filename="temp")
103   {
104     /* Check if valid cert is loaded*/
105     if($this->type!=false){
106       /* Check if we must convert the cert */
107       if($this->type!= $type){
108         $strConv = $this->type."TO".$type;
109         $this->$strConv();
110       }    
112       /* open file for writing */
113       $fp = fopen($filename,"w+");
115       if(!$fp){
116         $this->error= _("Can't create/open File");
117         return(false);
118       }else{
119         fwrite($fp,$this->data,strlen($this->data));        
120       }
121       return(true);
122     }else{
123       $this->error= _("No valid certificate loaded");
124       return(false);    
125     }
126     return(false);
127   } 
130   /* Convert der to pem Certificate */
131   function derTOpem()
132   {
133     /* if type is DER start convert */
134     if($this->type == DER)
135     {
136       /* converting */
137       $this->type= PEM; 
138       $str = base64_encode($this->data);
139       $len = strlen($str);
141       $end = "";
143       while($len > 0 )
144       {
145         $len = $len - 64;
146         $str1 = substr($str,0,64)."\n";
147         $str  = substr($str,64,$len);
148         $end.= $str1;
149       }
151       $strend = "-----BEGIN CERTIFICATE-----\n".$end;
152       $strend .= "-----END CERTIFICATE-----";
154       $this->data     = $strend;
155       return(true); 
156     }
157     return(false);
158   }
160   /*Convert pem to der Certificate */
161   function pemTOder()
162   {
163     if($this->type == PEM)
164     {
165       $this->type= DER;
167       $str = $this->data;
169       $str = str_replace("-----BEGIN CERTIFICATE-----","",$str);
170       $str = str_replace("-----END CERTIFICATE-----","",$str);
172       $str = base64_decode($str);
174       $this->data = $str;
175       return(true);
176     }
177     return(false);
178   }
181 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
182 ?>