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 if(isset($this->info['validTo_time_t'])){
110 return($this->info['validTo_time_t']);
111 }else{
112 return(false);
113 }
114 }
116 function getvalidfrom_date()
117 {
118 if(isset($this->info['validFrom_time_t'])){
119 return($this->info['validFrom_time_t']);
120 }else{
121 return(false);
122 }
124 }
126 function getname()
127 {
128 if(isset($this->info['name'])){
129 return($this->info['name']);
130 }else{
131 return(false);
132 }
133 }
135 function getCN()
136 {
137 if(isset($this->info['subject']['CN'])){
138 return($this->info['subject']['CN']);
139 }else{
140 return(false);
141 }
142 }
144 function getO()
145 {
146 if(isset($this->info['subject']['O'])){
147 return($this->info['subject']['O']);
148 }else{
149 return(false);
150 }
151 }
153 function getOU()
154 {
155 if(isset($this->info['subject']['OU'])){
156 return($this->info['subject']['OU']);
157 }else{
158 return(false);
159 }
160 }
162 function getSerialNumber()
163 {
164 if(isset($this->info['serialNumber'])){
165 return($this->info['serialNumber']);
166 }else{
167 return(false);
168 }
169 }
171 function isvalid()
172 {
173 return (($this->type != false)&&(count($this->info)>1));
174 }
177 /* Export Certificate to specified file, with specified method*/
178 function export($type,$filename="temp")
179 {
180 /* Check if valid cert is loaded*/
181 if($this->type!=false){
182 /* Check if we must convert the cert */
183 if($this->type!= $type){
184 $strConv = $this->type."TO".$type;
185 $this->$strConv();
186 }
188 /* open file for writing */
189 $fp = fopen($filename,"w+");
191 if(!$fp){
192 $this->error= _("Can't create/open File");
193 return(false);
194 }else{
195 fwrite($fp,$this->data,strlen($this->data));
196 }
197 return(true);
198 }else{
199 $this->error= _("No valid certificate loaded");
200 return(false);
201 }
202 return(false);
203 }
206 /* Convert der to pem Certificate */
207 function derTOpem()
208 {
209 /* if type is DER start convert */
210 if($this->type == DER) {
211 /* converting */
212 $this->type= PEM;
213 $str = base64_encode($this->data);
214 $len = strlen($str);
216 $end = "";
218 while($len > 0 ) {
219 $len = $len - 64;
220 $str1 = substr($str,0,64)."\n";
221 $str = substr($str,64,$len);
222 $end.= $str1;
223 }
225 $strend = "-----BEGIN CERTIFICATE-----\n".$end;
226 $strend .= "-----END CERTIFICATE-----";
228 $this->data = $strend;
229 return(true);
230 }
231 return(false);
232 }
234 /*Convert pem to der Certificate */
235 function pemTOder()
236 {
237 if($this->type == PEM) {
238 $this->type= DER;
240 $str = $this->data;
242 $str = str_replace("-----BEGIN CERTIFICATE-----","",$str);
243 $str = str_replace("-----END CERTIFICATE-----","",$str);
245 $str = base64_decode($str);
247 $this->data = $str;
248 return(true);
249 }
250 return(false);
251 }
253 }
255 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
256 ?>