Debian Codegraph @

src/grid_credential_handling/x509_handling/lcas_x509_utils.c

raw symbols SCIP
1 /**
2 * Copyright (c) Members of the EGEE Collaboration. 2004-2010.
3 * See http://www.eu-egee.org/partners/ for details on the copyright
4 * holders.
5 *
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 *
18 *
19 * Authors:
20 * 2009-
21 * Oscar Koeroo <[email protected]>
22 * Mischa Sall\'e <[email protected]>
23 * David Groep <[email protected]>
24 * NIKHEF Amsterdam, the Netherlands
25 * <[email protected]>
26 *
27 * 2007-2009
28 * Oscar Koeroo <[email protected]>
29 * David Groep <[email protected]>
30 * NIKHEF Amsterdam, the Netherlands
31 *
32 * 2003-2007
33 * Martijn Steenbakkers <[email protected]>
34 * Oscar Koeroo <[email protected]>
35 * David Groep <[email protected]>
36 * NIKHEF Amsterdam, the Netherlands
37 *
38 */
39
40
41 /*!
42 \file lcas_x509_utils.c
43 \brief This file contains utility functions needed for x509 credential
44 handling (openssl)
45 \author Martijn Steenbakkers for EGEE
46 This file contains utility functions needed for x509 credential
47 handling (openssl).
48 Contains code contributed by Andrew McNab.
49
50 */
51
52 #ifndef LCAS_X509_UTILS_C
53 #define LCAS_X509_UTILS_Ccxx . . $ `src/grid_credential_handling/x509_handling/lcas_x509_utils.c:53:9`!
54
55 /******************************************************************************
56 Include header files
57 ******************************************************************************/
58 #define DEBUGcxx . . $ `src/grid_credential_handling/x509_handling/lcas_x509_utils.c:58:9`! 0
59 #define HACKcxx . . $ `src/grid_credential_handling/x509_handling/lcas_x509_utils.c:59:9`! 0
60
61 #include <stdio.h>
62 #include <stdlib.h>
63 #include <string.h>
64
65 /* For X509 and STACK_OF(X509) structs (output) */
66 #include <openssl/x509.h>
67 #include <openssl/pem.h>
68 #include <openssl/x509v3.h>
69 #include <openssl/err.h>
70
71 /* LCAS includes */
72 #include "lcas_log.h"cxx . . $ `<file>/interface/lcas_log.h`/
73 #include "_lcas_x509_utils.h"cxx . . $ `<file>/src/grid_credential_handling/x509_handling/_lcas_x509_utils.h`/
74
75 /******************************************************************************
76 Defines
77 ******************************************************************************/
78
79 void lcas_print_x509_to_stringcxx . . $ lcas_print_x509_to_string(70cec7e8a25417c5). (X509cxx . . $ X509# *, char *);
80 void lcas_print_stack_of_x509_to_stringcxx . . $ lcas_print_stack_of_x509_to_string(de7c0b2f76b471aa). (STACK_OFcxx . . $ `<external>/13247083851530363302/safestack.h:33:9`!(X509) *, char *);
81
82
83 /******************************************************************************
84 Function: lcas_x509_free_chain()
85 Description: Properly free a previously allocated STACK_OF(X509) *chain
86 hence needs a STACK_OF(X509) **chain
87 Parameters:
88 chain: pointer to STACK_OF(X509)
89 ******************************************************************************/
90 void lcas_x509_free_chaincxx . . $ lcas_x509_free_chain(286d922ac5a1ecfe).(
91 STACK_OFcxx . . $ `<external>/13247083851530363302/safestack.h:33:9`!(X509) ** chain
92 )
93 {
94 if (*chain)
95 {
96 sk_X509_pop_freecxx . . $ `<external>/1406364094028619707/x509.h:96:9`!( *chain, X509_free );
97 /* sk_X509_free( chain ); */
98 *chain = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!;
99 }
100 }
101
102 /******************************************************************************
103 Function: lcas_x509_to_dn()
104 Description: Get the name of the X509 cert
105 Parameters:
106 px509: x509 certificate
107 Returns: dn (which should be freed)
108 ******************************************************************************/
109 char * lcas_x509_to_dncxx . . $ lcas_x509_to_dn(3d7621fa61c2bba8).(
110 X509cxx . . $ X509# * px509
111 )
112 {
113 char * logstr = "lcas_x509_to_dn()";
114 if (!px509)
115 {
116 lcas_logcxx . . $ lcas_log(63df4bdff494190).(1, "%s: No certificate found as input.\n", logstr);
117 return NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!;
118 }
119
120 return X509_NAME_onelinecxx . . $ X509_NAME_oneline(596b9437468c842c).(X509_get_subject_namecxx . . $ X509_get_subject_name(38fd56961e51b042).(px509),NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!,0);
121 }
122
123 /******************************************************************************
124 Function: lcas_x509_chain_to_dn()
125 Description: Get the base DN of the X509 proxy cert
126 Parameters:
127 px509: x509 certificate
128 certstack: certificate chain
129 Returns: dn (which should be freed)
130 ******************************************************************************/
131 char * lcas_x509_chain_to_dncxx . . $ lcas_x509_chain_to_dn(94aa51c76c1b90f9).(
132 X509cxx . . $ X509# * px509, STACK_OFcxx . . $ `<external>/13247083851530363302/safestack.h:33:9`!(X509) *certstack
133 )
134 {
135 char * logstr = "lcas_x509_chain_to_dn()";
136 int i = 0;
137 int amount_of_CAs = 0;
138 int depth = sk_X509_numcxx . . $ `<external>/1406364094028619707/x509.h:82:9`!(certstack);
139
140 /* The px509 is not used */
141
142 if (!certstack)
143 {
144 lcas_logcxx . . $ lcas_log(63df4bdff494190).(1, "%s: No stack of certificates found as input.\n", logstr);
145 return NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!;
146 }
147
148
149 /* Do we have a chain at all... */
150 if (depth > 0)
151 {
152 /* Amount of CAs in chain */
153 for (i = 0; i < depth; i++)
154 {
155 if (lcas_x509IsCAcxx . . $ lcas_x509IsCA(2343a232dbf8d287).(sk_X509_valuecxx . . $ `<external>/1406364094028619707/x509.h:83:9`!(certstack, i)))
156 amount_of_CAs++;
157 }
158
159 /* Select the User certificate */
160 i = depth - (amount_of_CAs + 1);
161
162 if (i < 0)
163 {
164 i = 0;
165 return NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!;
166 }
167
168 /* This returned value must be free'd */
169 return lcas_x509_to_dncxx . . $ lcas_x509_to_dn(3d7621fa61c2bba8). (sk_X509_valuecxx . . $ `<external>/1406364094028619707/x509.h:83:9`!(certstack, i));
170 }
171 else
172 return NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!;
173 }
174
175 /******************************************************************************
176 Function: lcas_pem_string_to_x509
177 Description: Reads a X509 certificate from a PEM-encoded string
178 Parameters:
179 px: pointer to a X509 certificate
180 cert_string: PEM-encoded string
181 Returns: 0 on success, non-zero otherwise
182 ******************************************************************************/
183 int lcas_pem_string_to_x509cxx . . $ lcas_pem_string_to_x509(facfd5879306c175).(X509cxx . . $ X509# **px, char *certstring)
184 {
185 /* BIO *certbio = NULL; */
186
187 // *px = X509_new();
188 // if (*px == NULL) return -1;
189 // /* clean it first */
190 // X509_free(*px);
191
192 /*
193 * Simular fix to this plugin as for LCMAPS
194 */
195
196 // certbio = BIO_new_mem_buf(certstring, -1);
197 // if (certbio)
198 // {
199 // /* This call allocates new X509 struct, so no X509_new() needed */
200 // *px = PEM_read_bio_X509(certbio, NULL, 0, NULL);
201 // }
202 //
203 // BIO_free(certbio);
204 // return 0;
205 // int rval = 0;
206
207
208 STACK_OFcxx . . $ `<external>/13247083851530363302/safestack.h:33:9`! (X509) * chain = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!;
209 char *dn;
210
211
212 if (lcas_pem_string_to_x509_chaincxx . . $ lcas_pem_string_to_x509_chain(cae2bb66f39d5e20). (&chain, certstring) == 0)
213 {
214 int amount_of_CAs = 0;
215 int depth = sk_X509_numcxx . . $ `<external>/1406364094028619707/x509.h:82:9`!(chain);
216 int i = 0;
217 X509cxx . . $ X509# * cert = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!;
218
219
220 // How many CA certs are there in the chain?
221 for (i = 0; i < depth; i++)
222 {
223 if (lcas_x509IsCAcxx . . $ lcas_x509IsCA(2343a232dbf8d287).(sk_X509_valuecxx . . $ `<external>/1406364094028619707/x509.h:83:9`!(chain, i)))
224 amount_of_CAs++;
225 }
226
227 /* Was: Getting the final proxy (the Leaf proxy) */
228 /* Is : Getting the first and primary proxy (could be the MyProxy... 00ps ... yes, this is a problem) */
229 /* Is again: Getting the final proxy (the Leaf proxy) */
230
231 /* i = depth - (amount_of_CAs + 2); */
232
233 /* if (i < 0) */
234 /* i = 0; */
235
236 /* lcas_log(0, "Value was %d, hardwired to 0\n", i);
237
238 i = 0;
239 */
240
241 /* Zero is the LEAF proxy, the final certificate (most likely a proxy) in the chain */
242 i = 0;
243 cert = sk_X509_valuecxx . . $ `<external>/1406364094028619707/x509.h:83:9`!(chain, i);
244 *px = X509_dupcxx . . $ X509_dup(a815e8049bc17e84). (cert);
245 }
246
247 /*
248 * DEBUG the chain, this will output the chain or just one specific certificate to a file - like: openssl x509 -text -noout -in <cert_file>
249 *
250 * lcas_print_x509_to_string (*px, "/tmp/lcas-output.cert.log");
251 * lcas_print_stack_of_x509_to_string (px, "/tmp/lcas-output.chain.log");
252 */
253
254
255
256 /* if (chain) sk_X509_free (chain); */
257 lcas_x509_free_chaincxx . . $ lcas_x509_free_chain(286d922ac5a1ecfe).( &chain ); /* NOTE: needs to change chain -> NULL */
258
259 if (!px) return -1;
260 dn = lcas_x509_to_dncxx . . $ lcas_x509_to_dn(3d7621fa61c2bba8).( *px );
261 if (dn)
262 {
263 lcas_log_debugcxx . . $ lcas_log_debug(63df4bdff494190). (5, "Got individual certificate with subject: %s\n", dn);
264 freecxx . . $ free(5d22bdacc48458e8).( dn );
265 } else
266 return -1;
267
268 return 0;
269 }
270
271 /******************************************************************************
272 Function: lcas_pem_string_to_x509_chain
273 Description: Creates a dynamically allocated stack of X509 certificate objects
274 by walking through the PEM-encoded X509 certificates.
275 Copied from gridsite (Andrew McNab), original name:
276 GRSTx509StringToChain()
277 Parameters:
278 certstack: pointer to a stack of X509 certificates
279 cert_string: PEM-encoded string
280 Returns: 0 on success, non-zero otherwise
281 ******************************************************************************/
282 int lcas_pem_string_to_x509_chaincxx . . $ lcas_pem_string_to_x509_chain(cae2bb66f39d5e20).(STACK_OFcxx . . $ `<external>/13247083851530363302/safestack.h:33:9`!(X509) **certstack, char *certstring)
283 {
284 STACK_OFcxx . . $ `<external>/13247083851530363302/safestack.h:33:9`!(X509_INFO) *sk=NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!;
285 BIOcxx . . $ BIO# *certbio = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!;
286 X509_INFOcxx . . $ X509_INFO# *xi;
287 #if DEBUGcxx . . $ `src/grid_credential_handling/x509_handling/lcas_x509_utils.c:58:9`!
288 int stack_size = 0;
289 #endif
290
291 *certstack = sk_X509_new_nullcxx . . $ `<external>/1406364094028619707/x509.h:85:9`!();
292 if ((*certstack == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!) || (certstring == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!)) return -1;
293
294 certbio = BIO_new_mem_bufcxx . . $ BIO_new_mem_buf(fff3f1d170340e13).(certstring, -1);
295
296 if (!(sk=PEM_X509_INFO_read_biocxx . . $ PEM_X509_INFO_read_bio(2e711dba1cfbd16c).(certbio, NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!, NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!, NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!)))
297 {
298 #if DEBUGcxx . . $ `src/grid_credential_handling/x509_handling/lcas_x509_utils.c:58:9`!
299 fprintf(stderr, "Error reading stack from certbio\n");
300 #endif
301 BIO_freecxx . . $ BIO_free(835332762aa40168).(certbio);
302 sk_X509_INFO_freecxx . . $ `<external>/1406364094028619707/x509.h:403:9`!(sk);
303 lcas_x509_free_chaincxx . . $ lcas_x509_free_chain(286d922ac5a1ecfe).( certstack ); /* *certstack -> NULL */
304 return 1;
305 }
306
307 while (sk_X509_INFO_numcxx . . $ `<external>/1406364094028619707/x509.h:397:9`!(sk))
308 {
309 #if DEBUGcxx . . $ `src/grid_credential_handling/x509_handling/lcas_x509_utils.c:58:9`!
310 char * dn = NULL;
311 fprintf(stderr, "current X509_INFO stack size: %d, DN = ", sk_X509_INFO_num(sk));
312 #endif
313 xi=sk_X509_INFO_shiftcxx . . $ `<external>/1406364094028619707/x509.h:410:9`!(sk);
314 if (xi->x509cxx . . $ X509_info_st#x509. != NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!)
315 {
316 sk_X509_pushcxx . . $ `<external>/1406364094028619707/x509.h:92:9`!(*certstack, xi->x509);
317 #if DEBUGcxx . . $ `src/grid_credential_handling/x509_handling/lcas_x509_utils.c:58:9`!
318 dn = lcas_x509_to_dn(xi->x509);
319 if (dn) {
320 fprintf(stderr, "%s\n", dn);
321 free(dn);
322 }
323 else
324 {
325 fprintf(stderr, "NULL!\n");
326 }
327 #endif
328 xi->x509cxx . . $ X509_info_st#x509.=NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!;
329 }
330 X509_INFO_freecxx . . $ X509_INFO_free(a406acb26ca80c4c).(xi);
331 }
332 #if DEBUGcxx . . $ `src/grid_credential_handling/x509_handling/lcas_x509_utils.c:58:9`!
333 stack_size = sk_X509_INFO_num(sk);
334 fprintf(stderr, "final size of stack of X509_INFO: %d, so it's %s\n",
335 stack_size, (stack_size?"not yet empty! (NOT OK)":"empty (OK)."));
336 fprintf(stderr, "size of stack of X509: %d\n", sk_X509_num(*certstack));
337 {
338 char * subject = NULL;
339 X509 * cert = NULL;
340 int i = 0;
341
342 for (i = 0; i < sk_X509_num(*certstack); i++)
343 {
344 cert = sk_X509_value(*certstack, i);
345 subject = lcas_x509_to_dn(cert);
346 if (subject) {
347 fprintf(stderr, " dn of cert %d: %s\n", i, subject);
348 free(subject);
349 }
350 else
351 {
352 fprintf(stderr, "NULL!\n");
353 }
354 }
355 }
356 #endif
357
358 /*
359 * DEBUG the chain, this will output the chain or just one specific certificate to a file - like: openssl x509 -text -noout -in <cert_file>
360 *
361 * lcas_print_x509_to_string (px509_cred, "/tmp/lcaps-output.cert.log");
362 * lcas_print_stack_of_x509_to_string (px509_chain, "/tmp/lcaps-output.chain.log");
363 */
364
365
366 if (!sk_X509_numcxx . . $ `<external>/1406364094028619707/x509.h:82:9`!(*certstack))
367 {
368 #if DEBUGcxx . . $ `src/grid_credential_handling/x509_handling/lcas_x509_utils.c:58:9`!
369 fprintf(stderr, "Found empty stack!\n");
370 #endif
371 BIO_freecxx . . $ BIO_free(835332762aa40168).(certbio);
372 sk_X509_INFO_freecxx . . $ `<external>/1406364094028619707/x509.h:403:9`!(sk);
373 lcas_x509_free_chaincxx . . $ lcas_x509_free_chain(286d922ac5a1ecfe).( certstack ); /* *certstack -> NULL */
374 return 1;
375 }
376
377 BIO_freecxx . . $ BIO_free(835332762aa40168).(certbio);
378 sk_X509_INFO_freecxx . . $ `<external>/1406364094028619707/x509.h:403:9`!(sk);
379 #if OPENSSL_VERSION_NUMBERcxx . . $ `<external>/4166512202731279593/opensslv.h:115:10`! < 0x010100000L
380 /* Claim is it is deprecated since 1.0.0, but for 1.0.1i I still see memory
381 * leak from ERR_get_state() */
382 ERR_remove_state(0);
383 #endif
384
385 return 0;
386 }
387
388
389
390 /******************************************************************************
391 Function: lcas_x509IsCA
392 Description:
393 Tests if the X509 * cert is a CA certificate or not
394 Parameters:
395 A X509 pointer
396 Returns:
397 0 : Not a CA cert
398 1 : This is a CA cert
399 ******************************************************************************/
400 int lcas_x509IsCAcxx . . $ lcas_x509IsCA(2343a232dbf8d287). (X509cxx . . $ X509# *cert)
401 {
402 /* int idret, purpose_id; */
403 int purpose_id;
404
405 purpose_id = X509_PURPOSE_get_by_snamecxx . . $ X509_PURPOSE_get_by_sname(5c4306d8553b3696).("sslclient");
406
407 /* final argument to X509_check_purpose() is whether to check for CAness */
408
409 if (X509_check_purposecxx . . $ X509_check_purpose(ccddb1d52be9c6d0).(cert, purpose_id + X509_PURPOSE_MINcxx . . $ `<external>/8547403039021081366/x509v3.h:777:9`!, 1))
410 return 1;
411 else return 0;
412 }
413
414
415 /******************************************************************************
416 Function: lcas_print_stack_of_x509_to_string
417 Description:
418 Extract from X509 certificate chain (STACK_OF(X509)) and
419 write the output to a file.
420 Simulair to `openssl x509 -in <file.pem> -text -noout
421
422 Note: only for debugging purposes
423 Parameters:
424 px509_chain: pointer to a x509 chain
425 Returns:
426 void
427 ******************************************************************************/
428 void lcas_print_stack_of_x509_to_stringcxx . . $ lcas_print_stack_of_x509_to_string(de7c0b2f76b471aa). (STACK_OFcxx . . $ `<external>/13247083851530363302/safestack.h:33:9`!(X509) * px509_chain, char * output_file)
429 {
430 char * logstr = "\tlcas_print_stack_of_x509_to_string()";
431
432 if (px509_chain)
433 {
434 STACK_OFcxx . . $ `<external>/13247083851530363302/safestack.h:33:9`!(X509) * dupChain = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!;
435 X509cxx . . $ X509# * cert = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!;
436
437 /* dupChain = (STACK_OF(type) *) sk_X509_dup(px509_chain);*/
438 dupChain = sk_X509_dupcxx . . $ `<external>/1406364094028619707/x509.h:104:9`!(px509_chain);
439 lcas_logcxx . . $ lcas_log(63df4bdff494190).(0,"%s\n", logstr);
440
441 while ((cert = sk_X509_popcxx . . $ `<external>/1406364094028619707/x509.h:94:9`!(dupChain)))
442 {
443 lcas_print_x509_to_stringcxx . . $ lcas_print_x509_to_string(70cec7e8a25417c5). (cert, output_file);
444 }
445
446 lcas_x509_free_chaincxx . . $ lcas_x509_free_chain(286d922ac5a1ecfe).( &dupChain ); /* dupChain -> NULL */
447 }
448 else
449 {
450 lcas_logcxx . . $ lcas_log(63df4bdff494190).(0,"%s: no input X509 chain!\n", logstr);
451 }
452 }
453
454
455 /******************************************************************************
456 Function: lcas_print_x509_to_string
457 Description:
458 Extract from X509 certificate and
459 write the output to a file.
460 Simulair to `openssl x509 -in <file.pem> -text -noout
461
462 Note: only for debugging purposes
463 Parameters:
464 px509: pointer to a x509 cert
465 Returns:
466 void
467 ******************************************************************************/
468 void lcas_print_x509_to_stringcxx . . $ lcas_print_x509_to_string(70cec7e8a25417c5). (X509cxx . . $ X509# * px509, char * output_file)
469 {
470 char * logstr = "\tlcas_print_x509_to_string()";
471
472 if (px509)
473 {
474 FILEcxx . . $ FILE# * tmpFile = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!;
475
476 lcas_logcxx . . $ lcas_log(63df4bdff494190).(0,"%s\n", logstr);
477
478 tmpFile = fopencxx . . $ fopen(82ec0787cd37e0a3).(output_file, "a");
479 X509_print_fpcxx . . $ X509_print_fp(c9b7d766149525ff).(tmpFile, px509);
480 fclosecxx . . $ fclose(89bc1efcdac0ca22). (tmpFile);
481 }
482 else
483 {
484 lcas_logcxx . . $ lcas_log(63df4bdff494190).(0,"%s: no input X509 cert!\n", logstr);
485 }
486 }
487
488 #endif /* LCAS_X509_UTILS_C */
489
490 /******************************************************************************
491 CVS Information:
492 $Source: /srv/home/dennisvd/svn/mw-security/lcas/src/grid_credential_handling/x509_handling/lcas_x509_utils.c,v $
493 $Date: 2010-05-03 10:42:50 $
494 $Revision: 1.24 $
495 $Author: okoeroo $
496 ******************************************************************************/