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 }
85 $this->info(false);
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);
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 function getSerialNumber()
138 {
139 return($this->info['serialNumber']);
140 }
142 function isvalid($returnstr = false)
143 {
144 if(($this->type != false)&&(count($this->info)>1))
145 {
146 if($returnstr){
147 return(_("valid"));
148 }else{
149 return(true);
150 }
151 }else{
152 if($returnstr){
153 return(_("invalid"));
154 }else{
155 return(false);
156 }
158 }
159 }
163 /* Export Certificate to specified file, with specified method*/
164 function export($type,$filename="temp")
165 {
166 /* Check if valid cert is loaded*/
167 if($this->type!=false){
168 /* Check if we must convert the cert */
169 if($this->type!= $type){
170 $strConv = $this->type."TO".$type;
171 $this->$strConv();
172 }
174 /* open file for writing */
175 $fp = fopen($filename,"w+");
177 if(!$fp){
178 $this->error= _("Can't create/open File");
179 return(false);
180 }else{
181 fwrite($fp,$this->data,strlen($this->data));
182 }
183 return(true);
184 }else{
185 $this->error= _("No valid certificate loaded");
186 return(false);
187 }
188 return(false);
189 }
192 /* Convert der to pem Certificate */
193 function derTOpem()
194 {
195 /* if type is DER start convert */
196 if($this->type == DER) {
197 /* converting */
198 $this->type= PEM;
199 $str = base64_encode($this->data);
200 $len = strlen($str);
202 $end = "";
204 while($len > 0 ) {
205 $len = $len - 64;
206 $str1 = substr($str,0,64)."\n";
207 $str = substr($str,64,$len);
208 $end.= $str1;
209 }
211 $strend = "-----BEGIN CERTIFICATE-----\n".$end;
212 $strend .= "-----END CERTIFICATE-----";
214 $this->data = $strend;
215 return(true);
216 }
217 return(false);
218 }
220 /*Convert pem to der Certificate */
221 function pemTOder()
222 {
223 if($this->type == PEM) {
224 $this->type= DER;
226 $str = $this->data;
228 $str = str_replace("-----BEGIN CERTIFICATE-----","",$str);
229 $str = str_replace("-----END CERTIFICATE-----","",$str);
231 $str = base64_decode($str);
233 $this->data = $str;
234 return(true);
235 }
236 return(false);
237 }
239 }
241 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
242 ?>