c396ac0b211a95d954e25f9acd3ce06cbf3eca2b
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 $fp = fopen($data,"r+");
30 $str = "";
32 if(!$fp){
33 $this->certificate();
34 $this->error=_("Can't open specified file, check accessibility and or existence");
35 return(false);
36 }else{
37 /* Reading data*/
38 while(!feof($fp)){
39 $str.=fgets($fp,1024);
40 }
41 }
42 /* Filename given, so we use the data from the file */
43 $this->data = $str;
44 } else {
45 /* Cert as String, use this string */
46 $this->data = $data;
47 }
49 /* Data can't be empty */
50 if($data = ""){
51 $this->certificate();
52 $this->error = _("Can't read specified certificate / or empty string given");
53 return(false);
54 }
56 /* Prefer specified certtype*/
57 if($type) {
58 $this->type = $type;
59 }else{
60 /* Detect certtype, cause there is none specified */
62 /* PEM allways starts with ----BEGIN CERTIFICATE-----*/
63 if(strstr($this->data,"CERTIFICATE")) {
64 $this->type=PEM;
65 } else {
66 /* We test DER now, on fail abort */
67 $this->type=DER;
68 }
69 }
71 /* Convert to PEM to give $this->info the ability to read the cert */
72 if($this->type == DER ) {
73 $this->derTOpem();
74 }
76 /* If cert is loaded correctly and is PEM now, we could read some data out of it */
77 if(count($this->info()) <=1) {
78 $this->certificate();
79 $this->error = _("Can't load certificate, possibly unsupported format (use PEM/DER) ");
80 /* Reset*/
81 return(false);
82 }
83 /* Loaded a readable cert */
84 return(true);
85 }
87 /* Returns Array with all containing data */
88 function info()
89 {
90 if($this->type != PEM){
91 $this->error = _("The Format must be PEM, to output certificate informations");
92 return(false);
93 } else {
94 /* return an array with all given information */
95 return(openssl_x509_parse($this->data));
96 }
97 }
100 /* Export Certificate to specified file, with specified method*/
101 function export($type,$filename="temp")
102 {
103 /* Check if valid cert is loaded*/
104 if($this->type!=false){
105 /* Check if we must convert the cert */
106 if($this->type!= $type){
107 $strConv = $this->type."TO".$type;
108 $this->$strConv();
109 }
111 /* open file for writing */
112 $fp = fopen($filename,"w+");
114 if(!$fp){
115 $this->error= _("Can't create/open File");
116 return(false);
117 }else{
118 fwrite($fp,$this->data,strlen($this->data));
119 }
120 return(true);
121 }else{
122 $this->error= _("No valid certificate loaded");
123 return(false);
124 }
125 return(false);
126 }
129 /* Convert der to pem Certificate */
130 function derTOpem()
131 {
132 /* if type is DER start convert */
133 if($this->type == DER) {
134 /* converting */
135 $this->type= PEM;
136 $str = base64_encode($this->data);
137 $len = strlen($str);
139 $end = "";
141 while($len > 0 ) {
142 $len = $len - 64;
143 $str1 = substr($str,0,64)."\n";
144 $str = substr($str,64,$len);
145 $end.= $str1;
146 }
148 $strend = "-----BEGIN CERTIFICATE-----\n".$end;
149 $strend .= "-----END CERTIFICATE-----";
151 $this->data = $strend;
152 return(true);
153 }
154 return(false);
155 }
157 /*Convert pem to der Certificate */
158 function pemTOder()
159 {
160 if($this->type == PEM) {
161 $this->type= DER;
163 $str = $this->data;
165 $str = str_replace("-----BEGIN CERTIFICATE-----","",$str);
166 $str = str_replace("-----END CERTIFICATE-----","",$str);
168 $str = base64_decode($str);
170 $this->data = $str;
171 return(true);
172 }
173 return(false);
174 }
176 }
178 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
179 ?>