Debian Codegraph @

src/proxy/proxycertinfo.c

raw
1 /*********************************************************************
2 *
3 * Authors: Valerio Venturi - [email protected]
4 * Vincenzo Ciaschini - [email protected]
5 *
6 * Copyright (c) Members of the EGEE Collaboration. 2004-2010.
7 * See http://www.eu-egee.org/partners/ for details on the copyright holders.
8 *
9 * Licensed under the Apache License, Version 2.0 (the "License");
10 * you may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
12 *
13 * http://www.apache.org/licenses/LICENSE-2.0
14 *
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
20 *
21 * Parts of this code may be based upon or even include verbatim pieces,
22 * originally written by other people, in which case the original header
23 * follows.
24 *
25 *********************************************************************/
26 #include "config.h"
27
28 #include <stdio.h>
29
30 #include <openssl/err.h>
31 #include <openssl/objects.h>
32
33 #include "myproxycertinfo.h"
34 #include "doio.h"
35
36 /* myPROXYPOLICY function */
37
38 myPROXYPOLICY * myPROXYPOLICY_new()
39 {
40 ASN1_CTX c;
41 myPROXYPOLICY * ret;
42
43 ret = NULL;
44
45 M_ASN1_New_Malloc(ret, myPROXYPOLICY);
46 ret->policy_language = OBJ_nid2obj(OBJ_sn2nid(IMPERSONATION_PROXY_SN));
47 ret->policy = NULL;
48 return (ret);
49 M_ASN1_New_Error(ASN1_F_PROXYPOLICY_NEW);
50 }
51
52 void myPROXYPOLICY_free(myPROXYPOLICY * policy)
53 {
54 if(policy == NULL) return;
55
56 ASN1_OBJECT_free(policy->policy_language);
57 M_ASN1_OCTET_STRING_free(policy->policy);
58 OPENSSL_free(policy);
59 }
60
61 /* duplicate */
62 myPROXYPOLICY * myPROXYPOLICY_dup(myPROXYPOLICY * policy)
63 {
64 #ifdef TYPEDEF_I2D_OF
65 return ((myPROXYPOLICY *) ASN1_dup((i2d_of_void *)i2d_myPROXYPOLICY,
66 (d2i_of_void *)d2i_myPROXYPOLICY,
67 (char *)policy));
68 #else
69 return ((myPROXYPOLICY *) ASN1_dup((int (*)())i2d_myPROXYPOLICY,
70 (char *(*)())d2i_myPROXYPOLICY,
71 (char *)policy));
72 #endif
73 }
74
75 /* set policy language */
76 int myPROXYPOLICY_set_policy_language(myPROXYPOLICY * policy, ASN1_OBJECT * policy_language)
77 {
78 if(policy_language != NULL) {
79 ASN1_OBJECT_free(policy->policy_language);
80 policy->policy_language = OBJ_dup(policy_language);
81 return 1;
82 }
83
84 return 0;
85 }
86
87 /* get policy language */
88 ASN1_OBJECT * myPROXYPOLICY_get_policy_language(myPROXYPOLICY * policy)
89 {
90 return policy->policy_language;
91 }
92
93 /* set policy */
94 int myPROXYPOLICY_set_policy(myPROXYPOLICY * proxypolicy, unsigned char * policy, int length)
95 {
96 if(policy != NULL) {
97 /* if member policy of proxypolicy non set */
98 if(!proxypolicy->policy)
99 proxypolicy->policy = ASN1_OCTET_STRING_new();
100
101 /* set member policy of proxypolicy */
102 ASN1_OCTET_STRING_set(proxypolicy->policy, policy, length);
103 }
104 else
105 ASN1_OCTET_STRING_free(proxypolicy->policy);
106
107 return 1;
108 }
109
110 /* get policy */
111 unsigned char * myPROXYPOLICY_get_policy(myPROXYPOLICY * proxypolicy, int * length)
112 {
113 /* assure field policy is set */
114
115 if(proxypolicy->policy) {
116 *length = proxypolicy->policy->length;
117
118 /* assure ASN1_OCTET_STRING is full */
119 if (*length>0 && proxypolicy->policy->data) {
120 unsigned char * copy = malloc(*length);
121 memcpy(copy, proxypolicy->policy->data, *length);
122 return copy;
123 }
124 }
125 return NULL;
126 }
127
128 /* internal to der conversion */
129 int i2d_myPROXYPOLICY(myPROXYPOLICY * policy, unsigned char ** pp)
130 {
131 M_ASN1_I2D_vars(policy);
132
133 M_ASN1_I2D_len(policy->policy_language, i2d_ASN1_OBJECT);
134
135 if(policy->policy) {
136 M_ASN1_I2D_len(policy->policy, i2d_ASN1_OCTET_STRING);
137 }
138
139 M_ASN1_I2D_seq_total();
140 M_ASN1_I2D_put(policy->policy_language, i2d_ASN1_OBJECT);
141
142 if(policy->policy) {
143 M_ASN1_I2D_put(policy->policy, i2d_ASN1_OCTET_STRING);
144 }
145
146 M_ASN1_I2D_finish();
147 }
148
149 myPROXYPOLICY * d2i_myPROXYPOLICY(myPROXYPOLICY ** a, unsigned char ** pp, long length)
150 {
151 M_ASN1_D2I_vars(a, myPROXYPOLICY *, myPROXYPOLICY_new);
152
153 M_ASN1_D2I_Init();
154 M_ASN1_D2I_start_sequence();
155 M_ASN1_D2I_get(ret->policy_language, d2i_ASN1_OBJECT);
156
157 /* need to try getting the policy using
158 * a) a call expecting no tags
159 * b) a call expecting tags
160 * one of which should succeed
161 */
162
163 M_ASN1_D2I_get_opt(ret->policy,
164 d2i_ASN1_OCTET_STRING,
165 V_ASN1_OCTET_STRING);
166 M_ASN1_D2I_get_IMP_opt(ret->policy,
167 d2i_ASN1_OCTET_STRING,
168 0,
169 V_ASN1_OCTET_STRING);
170 M_ASN1_D2I_Finish(a,
171 myPROXYPOLICY_free,
172 ASN1_F_D2I_PROXYPOLICY);
173 }
174
175
176
177 /* myPROXYCERTINFO function */
178
179 myPROXYCERTINFO * myPROXYCERTINFO_new()
180 {
181 myPROXYCERTINFO * ret = NULL;
182 ASN1_CTX c;
183
184 M_ASN1_New_Malloc(ret, myPROXYCERTINFO);
185 memset(ret, 0, sizeof(myPROXYCERTINFO));
186 ret->path_length = NULL;
187 ret->proxypolicy = myPROXYPOLICY_new();
188 return (ret);
189 M_ASN1_New_Error(ASN1_F_PROXYCERTINFO_NEW);
190 }
191
192 void myPROXYCERTINFO_free(myPROXYCERTINFO * proxycertinfo)
193 {
194 /* assure proxycertinfo not empty */
195 if(proxycertinfo == NULL) return;
196
197 ASN1_INTEGER_free(proxycertinfo->path_length);
198 myPROXYPOLICY_free(proxycertinfo->proxypolicy);
199 OPENSSL_free(proxycertinfo);
200 }
201
202 /* set path_length */
203 int myPROXYCERTINFO_set_path_length(myPROXYCERTINFO * proxycertinfo, long path_length)
204 {
205 /* assure proxycertinfo is not empty */
206 if(proxycertinfo != NULL) {
207
208 if(path_length != -1) {
209 /* if member path_length is empty allocate memory then set */
210 if(proxycertinfo->path_length == NULL)
211 proxycertinfo->path_length = ASN1_INTEGER_new();
212 return ASN1_INTEGER_set(proxycertinfo->path_length, path_length);
213 }
214 else {
215 ASN1_INTEGER_free(proxycertinfo->path_length);
216 proxycertinfo->path_length = NULL;
217 }
218
219 return 1;
220 }
221
222 return 0;
223 }
224
225 int myPROXYCERTINFO_set_version(myPROXYCERTINFO * proxycertinfo, int version)
226 {
227 if (proxycertinfo != NULL) {
228 proxycertinfo->version = version;
229 return 1;
230 }
231
232 return 0;
233 }
234
235 int myPROXYCERTINFO_get_version(myPROXYCERTINFO * proxycertinfo)
236 {
237 if (proxycertinfo)
238 return proxycertinfo->version;
239 return -1;
240 }
241
242
243 /* get path length */
244 long myPROXYCERTINFO_get_path_length(myPROXYCERTINFO * proxycertinfo)
245 {
246 if(proxycertinfo && proxycertinfo->path_length)
247 return ASN1_INTEGER_get(proxycertinfo->path_length);
248 else
249 return -1;
250 }
251
252 /* set policy */
253 int myPROXYCERTINFO_set_proxypolicy(myPROXYCERTINFO * proxycertinfo, myPROXYPOLICY * proxypolicy)
254 {
255 myPROXYPOLICY_free(proxycertinfo->proxypolicy);
256
257 if(proxypolicy != NULL)
258 proxycertinfo->proxypolicy = myPROXYPOLICY_dup(proxypolicy);
259 else
260 proxycertinfo->proxypolicy = NULL;
261
262 return 1;
263 }
264
265 /* get policy */
266 myPROXYPOLICY * myPROXYCERTINFO_get_proxypolicy(myPROXYCERTINFO * proxycertinfo)
267 {
268 if(proxycertinfo)
269 return proxycertinfo->proxypolicy;
270
271 return NULL;
272 }
273
274 /* internal to der conversion */
275 static int i2d_myPROXYCERTINFO_v3(myPROXYCERTINFO * proxycertinfo, unsigned char ** pp)
276 {
277 int v1;
278
279 M_ASN1_I2D_vars(proxycertinfo);
280
281 v1 = 0;
282
283 M_ASN1_I2D_len(proxycertinfo->proxypolicy, i2d_myPROXYPOLICY);
284
285 M_ASN1_I2D_len_EXP_opt(proxycertinfo->path_length,i2d_ASN1_INTEGER, 1, v1);
286 M_ASN1_I2D_seq_total();
287 M_ASN1_I2D_put(proxycertinfo->proxypolicy, i2d_myPROXYPOLICY);
288 M_ASN1_I2D_put_EXP_opt(proxycertinfo->path_length, i2d_ASN1_INTEGER, 1, v1);
289 M_ASN1_I2D_finish();
290 }
291
292 static int i2d_myPROXYCERTINFO_v4(myPROXYCERTINFO * proxycertinfo, unsigned char ** pp)
293 {
294 M_ASN1_I2D_vars(proxycertinfo);
295
296 if(proxycertinfo->path_length)
297 {
298 M_ASN1_I2D_len(proxycertinfo->path_length, i2d_ASN1_INTEGER);
299 }
300
301 M_ASN1_I2D_len(proxycertinfo->proxypolicy, i2d_myPROXYPOLICY);
302
303 M_ASN1_I2D_seq_total();
304 if(proxycertinfo->path_length)
305 {
306 M_ASN1_I2D_put(proxycertinfo->path_length, i2d_ASN1_INTEGER);
307 }
308 M_ASN1_I2D_put(proxycertinfo->proxypolicy, i2d_myPROXYPOLICY);
309 M_ASN1_I2D_finish();
310 }
311
312 int i2d_myPROXYCERTINFO(myPROXYCERTINFO * proxycertinfo, unsigned char ** pp)
313 {
314 switch(proxycertinfo->version) {
315 case 3:
316 return i2d_myPROXYCERTINFO_v3(proxycertinfo, pp);
317 break;
318
319 case 4:
320 return i2d_myPROXYCERTINFO_v4(proxycertinfo, pp);
321 break;
322
323 default:
324 return -1;
325 break;
326 }
327 }
328
329 static myPROXYCERTINFO * d2i_myPROXYCERTINFO_v3(myPROXYCERTINFO ** cert_info, unsigned char ** pp, long length)
330 {
331 M_ASN1_D2I_vars(cert_info, myPROXYCERTINFO *, myPROXYCERTINFO_new);
332
333 M_ASN1_D2I_Init();
334 M_ASN1_D2I_start_sequence();
335
336 M_ASN1_D2I_get((ret->proxypolicy), d2i_myPROXYPOLICY);
337
338 M_ASN1_D2I_get_EXP_opt(ret->path_length, d2i_ASN1_INTEGER, 1);
339
340 ret->version = 3;
341 M_ASN1_D2I_Finish(cert_info, myPROXYCERTINFO_free, ASN1_F_D2I_PROXYCERTINFO);
342 }
343
344 static myPROXYCERTINFO * d2i_myPROXYCERTINFO_v4(myPROXYCERTINFO ** cert_info, unsigned char ** pp, long length)
345 {
346 M_ASN1_D2I_vars(cert_info, myPROXYCERTINFO *, myPROXYCERTINFO_new);
347
348 M_ASN1_D2I_Init();
349 M_ASN1_D2I_start_sequence();
350
351 M_ASN1_D2I_get_EXP_opt(ret->path_length, d2i_ASN1_INTEGER, 1);
352
353 M_ASN1_D2I_get_opt(ret->path_length, d2i_ASN1_INTEGER, V_ASN1_INTEGER);
354
355 M_ASN1_D2I_get((ret->proxypolicy),d2i_myPROXYPOLICY);
356
357 ret->version = 4;
358 M_ASN1_D2I_Finish(cert_info, myPROXYCERTINFO_free, ASN1_F_D2I_PROXYCERTINFO);
359 }
360
361 myPROXYCERTINFO * d2i_myPROXYCERTINFO(myPROXYCERTINFO ** cert_info, unsigned char ** pp, long length)
362 {
363 myPROXYCERTINFO *info = d2i_myPROXYCERTINFO_v3(cert_info, pp, length);
364 if (!info)
365 info = d2i_myPROXYCERTINFO_v4(cert_info, pp, length);
366 return info;
367 }
368
369
370 static int nativeopenssl = 0;
371
372 static char *norep()
373 {
374 static char *buffer="";
375 return buffer;
376 }
377
378 static void *myproxycertinfo_s2i(UNUSED(struct v3_ext_method *method), UNUSED(struct v3_ext_ctx *ctx), UNUSED(char *data))
379 {
380 return (myPROXYCERTINFO*)data;
381 }
382
383 static char *myproxycertinfo_i2s(UNUSED(struct v3_ext_method *method), void *ext)
384 {
385 myPROXYCERTINFO *pci = NULL;
386 char *encoding = NULL;
387 char *output = NULL;
388 myPROXYPOLICY *pp;
389 int dooid = 0;
390 char oid[256];
391
392 pci = (myPROXYCERTINFO *)ext;
393
394 if (!pci)
395 return norep();
396
397 if (pci->path_length) {
398 int j = ASN1_INTEGER_get(pci->path_length);
399
400 char *buffer = snprintf_wrap("%X", j);
401 output = snprintf_wrap("Path Length Constraint: %s%s\n\n", strlen(buffer)%2 ? "0" : "", buffer);
402 free(buffer);
403 }
404 else
405 output = strdup("Path Length Constraint: unlimited\n");
406
407 pp = pci->proxypolicy;
408
409 if (pp && i2t_ASN1_OBJECT(oid, 256, pp->policy_language)) {
410 dooid = 1;
411 }
412
413 encoding = snprintf_wrap("%sPolicy Language: %s%s%s%s\n",
414 output,
415 ( dooid ? oid : ""),
416 ( (pp && pp->policy) ? "\nPolicy Text: " : ""),
417 ( (pp && pp->policy) ? (char*)ASN1_STRING_data(pp->policy) : ""),
418 ( (pp && pp->policy) ? "\n" : ""));
419
420 free(output);
421 return encoding;
422 }
423
424 void InitProxyCertInfoExtension(int full)
425 {
426 #define PROXYCERTINFO_V3 "1.3.6.1.4.1.3536.1.222"
427 #define PROXYCERTINFO_V4 "1.3.6.1.5.5.7.1.14"
428 #define OBJC(c,n) OBJ_create(c,n,n)
429
430 X509V3_EXT_METHOD *pcert;
431 static int set = 0;
432 ASN1_OBJECT *objv3;
433 ASN1_OBJECT *objv4;
434
435 if (set)
436 return;
437
438 set = 1;
439
440
441 objv3 = OBJ_txt2obj(PROXYCERTINFO_V3,1);
442 objv4 = OBJ_txt2obj(PROXYCERTINFO_V4,1);
443
444 /* Proxy Certificate Extension's related objects */
445 if (OBJ_obj2nid(objv3) == 0) {
446 ERR_clear_error();
447 OBJC(PROXYCERTINFO_V3, "Proxy Certificate Information");
448 if (full) {
449 pcert = (X509V3_EXT_METHOD *)OPENSSL_malloc(sizeof(X509V3_EXT_METHOD));
450
451 if (pcert) {
452 memset(pcert, 0, sizeof(*pcert));
453 pcert->ext_nid = OBJ_txt2nid(PROXYCERTINFO_V3);
454 pcert->ext_flags = 0;
455 pcert->ext_new = (X509V3_EXT_NEW) myPROXYCERTINFO_new;
456 pcert->ext_free = (X509V3_EXT_FREE)myPROXYCERTINFO_free;
457 pcert->d2i = (X509V3_EXT_D2I) d2i_myPROXYCERTINFO;
458 pcert->i2d = (X509V3_EXT_I2D) i2d_myPROXYCERTINFO;
459 pcert->i2s = (X509V3_EXT_I2S) myproxycertinfo_i2s;
460 pcert->s2i = (X509V3_EXT_S2I) myproxycertinfo_s2i;
461 pcert->v2i = (X509V3_EXT_V2I) NULL;
462 pcert->r2i = (X509V3_EXT_R2I) NULL;
463 pcert->i2v = (X509V3_EXT_I2V) NULL;
464 pcert->i2r = (X509V3_EXT_I2R) NULL;
465
466 X509V3_EXT_add(pcert);
467 }
468 }
469 }
470
471 if (OBJ_obj2nid(objv4) == 0) {
472 ERR_clear_error();
473 OBJC(PROXYCERTINFO_V4, "Proxy Certificate Information");
474 if (full) {
475 pcert = (X509V3_EXT_METHOD *)OPENSSL_malloc(sizeof(X509V3_EXT_METHOD));
476
477 if (pcert) {
478 memset(pcert, 0, sizeof(*pcert));
479 pcert->ext_nid = OBJ_txt2nid(PROXYCERTINFO_V4);
480 pcert->ext_flags = 0;
481 pcert->ext_new = (X509V3_EXT_NEW) myPROXYCERTINFO_new;
482 pcert->ext_free = (X509V3_EXT_FREE)myPROXYCERTINFO_free;
483 pcert->d2i = (X509V3_EXT_D2I) d2i_myPROXYCERTINFO;
484 pcert->i2d = (X509V3_EXT_I2D) i2d_myPROXYCERTINFO;
485 pcert->i2s = (X509V3_EXT_I2S) myproxycertinfo_i2s;
486 pcert->s2i = (X509V3_EXT_S2I) myproxycertinfo_s2i;
487 pcert->v2i = (X509V3_EXT_V2I) NULL;
488 pcert->r2i = (X509V3_EXT_R2I) NULL;
489 pcert->i2v = (X509V3_EXT_I2V) NULL;
490 pcert->i2r = (X509V3_EXT_I2R) NULL;
491
492 X509V3_EXT_add(pcert);
493 }
494 }
495 }
496
497 #ifdef X509_V_FLAG_ALLOW_PROXY_CERTS
498 nativeopenssl = 1;
499 #endif
500
501 ASN1_OBJECT_free(objv3);
502 ASN1_OBJECT_free(objv4);
503
504 return;
505 }
506
507 int proxynative(void)
508 {
509 return nativeopenssl;
510 }