1 <?php
2 /*
3 * This code is part of GOsa (http://www.gosa-project.org)
4 * Copyright (C) 2003-2008 GONICUS GmbH
5 *
6 * ID: $$Id$$
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 */
23 /* definitions */
25 /* certificates */
26 define("PEM","pem");
27 define("DER","der");
29 class certificate
30 {
31 /* vars */
32 var $data;
33 var $type;
34 var $error;
36 /* Initialize all vars*/
37 function certificate()
38 {
39 $this->data= "";
40 $this->type= false;
41 $this->error="";
42 $this->info = array();
43 }
45 /* Reads specified Certfile/string and convert it to PEM*/
46 function import($data,$type=false)
47 {
48 /* if is file read from file, else use string as it is*/
49 if(is_file($data)) {
50 $fp = fopen($data,"r+");
51 $str = "";
53 if(!$fp){
54 $this->certificate();
55 $this->error=_("Can't open specified file, check accessibility and or existence");
56 return(false);
57 }else{
58 /* Reading data*/
59 while(!feof($fp)){
60 $str.=fgets($fp,1024);
61 }
62 }
63 /* Filename given, so we use the data from the file */
64 $this->data = $str;
65 } else {
66 /* Cert as String, use this string */
67 $this->data = $data;
68 }
70 /* Data can't be empty */
71 if($data = ""){
72 $this->certificate();
73 $this->error = _("Can't read specified certificate / or empty string given");
74 return(false);
75 }
77 /* Prefer specified certtype*/
78 if($type) {
79 $this->type = $type;
80 }else{
81 /* Detect certtype, cause there is none specified */
83 /* PEM allways starts with ----BEGIN CERTIFICATE-----*/
84 if(strstr($this->data,"CERTIFICATE")) {
85 $this->type=PEM;
86 } else {
87 /* We test DER now, on fail abort */
88 $this->type=DER;
89 }
90 }
92 /* Convert to PEM to give $this->info the ability to read the cert */
93 if($this->type == DER ) {
94 $this->derTOpem();
95 }
97 /* If cert is loaded correctly and is PEM now, we could read some data out of it */
98 if(count($this->info()) <=1) {
99 $this->certificate();
100 $this->error = _("Can't load certificate, possibly unsupported format (use PEM/DER) ");
101 /* Reset*/
102 return(false);
103 }
105 $this->info(false);
107 /* Loaded a readable cert */
108 return(true);
109 }
111 /* Returns Array with all containing data */
112 function info($ret = true)
113 {
114 if($this->type != PEM){
115 $this->error = _("The Format must be PEM, to output certificate informations");
116 return(false);
117 } else {
118 /* return an array with all given information */
119 $this->info=openssl_x509_parse($this->data);
121 if($ret)
122 return($this->info);
123 }
124 }
126 /* Return Functions */
127 function getvalidto_date()
128 {
129 if(isset($this->info['validTo_time_t'])){
130 return($this->info['validTo_time_t']);
131 }else{
132 return(false);
133 }
134 }
136 function getvalidfrom_date()
137 {
138 if(isset($this->info['validFrom_time_t'])){
139 return($this->info['validFrom_time_t']);
140 }else{
141 return(false);
142 }
144 }
146 function getname()
147 {
148 if(isset($this->info['name'])){
149 return($this->info['name']);
150 }else{
151 return(false);
152 }
153 }
155 function getCN()
156 {
157 if(isset($this->info['subject']['CN'])){
158 return($this->info['subject']['CN']);
159 }else{
160 return(false);
161 }
162 }
164 function getO()
165 {
166 if(isset($this->info['subject']['O'])){
167 return($this->info['subject']['O']);
168 }else{
169 return(false);
170 }
171 }
173 function getOU()
174 {
175 if(isset($this->info['subject']['OU'])){
176 return($this->info['subject']['OU']);
177 }else{
178 return(false);
179 }
180 }
182 function getSerialNumber()
183 {
184 if(isset($this->info['serialNumber'])){
185 return($this->info['serialNumber']);
186 }else{
187 return(false);
188 }
189 }
191 function isvalid()
192 {
193 return (($this->type != false)&&(count($this->info)>1));
194 }
197 /* Export Certificate to specified file, with specified method*/
198 function export($type,$filename="temp")
199 {
200 /* Check if valid cert is loaded*/
201 if($this->type!=false){
202 /* Check if we must convert the cert */
203 if($this->type!= $type){
204 $strConv = $this->type."TO".$type;
205 $this->$strConv();
206 }
208 /* open file for writing */
209 $fp = fopen($filename,"w+");
211 if(!$fp){
212 $this->error= _("Can't create/open File");
213 return(false);
214 }else{
215 fwrite($fp,$this->data,strlen($this->data));
216 }
217 return(true);
218 }else{
219 $this->error= _("No valid certificate loaded");
220 return(false);
221 }
222 return(false);
223 }
226 /* Convert der to pem Certificate */
227 function derTOpem()
228 {
229 /* if type is DER start convert */
230 if($this->type == DER) {
231 /* converting */
232 $this->type= PEM;
233 $str = base64_encode($this->data);
234 $len = strlen($str);
236 $end = "";
238 while($len > 0 ) {
239 $len = $len - 64;
240 $str1 = substr($str,0,64)."\n";
241 $str = substr($str,64,$len);
242 $end.= $str1;
243 }
245 $strend = "-----BEGIN CERTIFICATE-----\n".$end;
246 $strend .= "-----END CERTIFICATE-----";
248 $this->data = $strend;
249 return(true);
250 }
251 return(false);
252 }
254 /*Convert pem to der Certificate */
255 function pemTOder()
256 {
257 if($this->type == PEM) {
258 $this->type= DER;
260 $str = $this->data;
262 $str = str_replace("-----BEGIN CERTIFICATE-----","",$str);
263 $str = str_replace("-----END CERTIFICATE-----","",$str);
265 $str = base64_decode($str);
267 $this->data = $str;
268 return(true);
269 }
270 return(false);
271 }
273 }
275 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
276 ?>