1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190
|
/*
* Copyright 1999-2006 University of Chicago
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef GLOBUS_DONT_DOCUMENT_INTERNAL
/**
* @file grid_proxy_init.h
* Globus GSI Proxy Utils
* @author Sam Lang, Sam Meder
*
* $RCSfile: grid_proxy_init.c,v $
* $Revision: 1.49.4.1 $
* $Date: 2010/06/28 19:17:37 $
*/
#endif
#include "globus_common.h"
#include "globus_error.h"
#include "globus_gsi_cert_utils.h"
#include "globus_gsi_system_config.h"
#include "globus_gsi_proxy.h"
#include "globus_gsi_credential.h"
#include "globus_stdio_ui.h"
#include "proxycertinfo.h"
#include "openssl/asn1.h"
#ifdef WIN32
#include "openssl/applink.c"
#endif
#define SHORT_USAGE_FORMAT \
"\nSyntax: %s [-help][-pwstdin][-limited][-valid H:M] ...\n"
static int quiet = 0;
static int debug = 0;
static char * LONG_USAGE = \
"\n" \
" Options\n" \
" -help, -usage Displays usage\n" \
" -version Displays version\n" \
" -debug Enables extra debug output\n" \
" -q Quiet mode, minimal output\n" \
" -verify Verifies certificate to make proxy for\n" \
" -pwstdin Allows passphrase from stdin\n" \
" -limited Creates a limited proxy\n" \
" -independent Creates a independent proxy\n" \
" -draft Creates a draft (GSI-3) proxy\n" \
" -old Creates a legacy globus proxy\n" \
" -rfc Creates a RFC 3820 compliant proxy\n" \
" -valid <h:m> Proxy is valid for h hours and m \n" \
" minutes (default:12:00)\n" \
" -hours <hours> Deprecated support of hours option\n" \
" -bits <bits> Number of bits in key {512|1024|2048|4096}\n" \
" -policy <policyfile> File containing policy to store in the\n" \
" ProxyCertInfo extension\n" \
" -pl <oid>, OID string for the policy language\n" \
" -policy-language <oid> used in the policy file\n" \
" -path-length <l> Allow a chain of at most l proxies to be \n" \
" generated from this one\n" \
" -cert <certfile> Non-standard location of user certificate\n" \
" -key <keyfile> Non-standard location of user key\n" \
" -certdir <certdir> Non-standard location of trusted cert dir\n" \
" -out <proxyfile> Non-standard location of new proxy cert\n" \
"\n" ;
# define args_show_version() \
{ \
char buf[64]; \
sprintf( buf, \
"%s-%s", \
PACKAGE, \
VERSION); \
fprintf(stderr, "%s\n", buf); \
globus_module_deactivate_all(); \
exit(0); \
}
# define args_show_short_help() \
{ \
fprintf(stderr, \
SHORT_USAGE_FORMAT \
"\nUse -help to display full usage.\n", \
program); \
globus_module_deactivate_all(); \
}
# define args_show_full_usage() \
{ \
fprintf(stderr, SHORT_USAGE_FORMAT \
"%s", \
program, \
LONG_USAGE); \
globus_module_deactivate_all(); \
exit(0); \
}
# define args_error_message(errmsg) \
{ \
fprintf(stderr, "\nERROR: %s\n", errmsg); \
args_show_short_help(); \
globus_module_deactivate_all(); \
exit(1); \
}
# define args_error(argval, errmsg) \
{ \
char buf[1024]; \
sprintf(buf, "option %s : %s", argval, errmsg); \
args_error_message(buf); \
}
# define args_verify_next(argnum, argval, errmsg) \
{ \
if ((argnum+1 >= argc) || (argv[argnum+1][0] == '-')) \
args_error(argval,errmsg); \
}
void
globus_i_gsi_proxy_utils_print_error(
globus_result_t result,
int debug,
const char * filename,
int line,
const char * fmt,
...);
static int
globus_i_gsi_proxy_utils_pwstdin_callback(
char * buf,
int num,
int w);
static void
globus_i_gsi_proxy_utils_key_gen_callback(
int p,
int n,
void * dummy);
static int
globus_l_gsi_proxy_utils_extension_callback(
globus_gsi_callback_data_t callback_data,
X509_EXTENSION * extension);
int
main(
int argc,
char ** argv)
{
globus_result_t result = GLOBUS_SUCCESS;
/* default proxy to 512 bits */
int key_bits = 512;
/* default to a 12 hour cert */
int valid = 12*60;
int verify = 0;
int arg_index;
char * user_cert_filename = NULL;
char * user_key_filename = NULL;
char * tmp_user_cert_filename = NULL;
char * tmp_user_key_filename = NULL;
char * proxy_out_filename = NULL;
char * ca_cert_dir = NULL;
char * argp;
char * program = NULL;
globus_gsi_proxy_handle_t proxy_handle = NULL;
globus_gsi_proxy_handle_attrs_t proxy_handle_attrs = NULL;
globus_gsi_callback_data_t callback_data = NULL;
globus_gsi_cred_handle_t cred_handle = NULL;
globus_gsi_cred_handle_t proxy_cred_handle = NULL;
globus_gsi_cert_utils_cert_type_t cert_type = 0;
BIO * pem_proxy_bio = NULL;
time_t goodtill;
time_t lifetime;
unsigned char * policy_buf = NULL;
size_t policy_buf_len = 0;
char * policy_filename = NULL;
char * policy_language = NULL;
int policy_NID;
long path_length = -1;
int (*pw_cb)() = NULL;
int return_value = 0;
if(globus_module_activate(GLOBUS_GSI_PROXY_MODULE) != (int)GLOBUS_SUCCESS)
{
globus_libc_fprintf(
stderr,
"\nERROR: Couldn't load module: GLOBUS_GSI_PROXY_MODULE.\n"
"Make sure Globus is installed correctly.\n\n");
exit(1);
}
if(globus_module_activate(GLOBUS_GSI_CALLBACK_MODULE) != (int)GLOBUS_SUCCESS)
{
globus_libc_fprintf(
stderr,
"\nERROR: Couldn't load module: GLOBUS_GSI_CALLBACK_MODULE.\n"
"Make sure Globus is installed correctly.\n\n");
globus_module_deactivate_all();
exit(1);
}
/* get the program name */
if (strrchr(argv[0], '/'))
{
program = strrchr(argv[0], '/') + 1;
}
else
{
program = argv[0];
}
/* parse the arguments */
for(arg_index = 1; arg_index < argc; ++arg_index)
{
argp = argv[arg_index];
if (strncmp(argp, "--", 2) == 0)
{
if (argp[2] != '\0')
{
args_error(argp, "double-dashed options are not allowed");
}
else
{
/* no more parsing */
arg_index = argc + 1;
continue;
}
}
if((strcmp(argp, "-help") == 0) ||
(strcmp(argp, "-usage") == 0))
{
args_show_full_usage();
}
else if(strcmp(argp, "-version") == 0)
{
args_show_version();
}
else if(strcmp(argp, "-cert") == 0)
{
args_verify_next(arg_index, argp, "need a file name argument");
user_cert_filename = argv[++arg_index];
result = GLOBUS_GSI_SYSCONFIG_CHECK_CERTFILE(user_cert_filename);
if(result != GLOBUS_SUCCESS)
{
args_error(argp, globus_error_print_friendly(
globus_error_get(result)));
}
}
else if(strcmp(argp, "-certdir") == 0)
{
args_verify_next(arg_index, argp, "need a file name argument");
ca_cert_dir = strdup(argv[++arg_index]);
result = GLOBUS_GSI_SYSCONFIG_DIR_EXISTS(ca_cert_dir);
if(result != GLOBUS_SUCCESS)
{
args_error(argp, globus_error_print_friendly(
globus_error_get(result)));
}
}
else if(strcmp(argp, "-out") == 0)
{
args_verify_next(arg_index, argp, "need a file name argument");
proxy_out_filename = strdup(argv[++arg_index]);
}
else if(strcmp(argp, "-key") == 0)
{
args_verify_next(arg_index, argp, "need a file name argument");
user_key_filename = argv[++arg_index];
result = GLOBUS_GSI_SYSCONFIG_CHECK_KEYFILE(user_key_filename);
if(result != GLOBUS_SUCCESS)
{
args_error(argp, globus_error_print_friendly(
globus_error_get(result)));
}
}
else if(strcmp(argp, "-valid") == 0)
{
int hours;
int minutes;
args_verify_next(arg_index, argp,
"valid time argument H:M missing");
if(sscanf(argv[++arg_index], "%d:%d", &hours, &minutes) < 2)
{
args_error(argp, "value must be in the format: H:M");
}
if(hours < 0)
{
args_error(argp, "specified hours must be a non-negative integer");
}
if(minutes < 0 || minutes > 60)
{
args_error(argp, "specified minutes must "
"be in the range 0-60");
}
/* error on overflow */
if(hours > (((time_t)(~0U>>1))/3600-1))
{
hours = (((time_t)(~0U>>1))/3600-1);
}
valid = (hours * 60) + minutes;
}
else if(strcmp(argp, "-hours") == 0)
{
int hours;
args_verify_next(arg_index, argp, "integer argument missing");
hours = atoi(argv[arg_index + 1]);
/* error on overflow */
if(hours > ((time_t)(~0U>>1))/3600)
{
hours = ((time_t)(~0U>>1))/3600;
}
valid = hours * 60;
arg_index++;
}
else if(strcmp(argp, "-bits") == 0)
{
args_verify_next(arg_index, argp, "integer argument missing");
key_bits = atoi(argv[arg_index + 1]);
if((key_bits != 512) && (key_bits != 1024) &&
(key_bits != 2048) && (key_bits != 4096))
{
args_error(argp, "value must be one of 512,1024,2048,4096");
}
arg_index++;
}
else if(strcmp(argp, "-debug") == 0)
{
debug++;
}
else if(strcmp(argp, "-limited") == 0)
{
if (cert_type & GLOBUS_GSI_CERT_UTILS_TYPE_PROXY_MASK)
{
args_error(argp,
"-independent, -limited and -policy/-policy-language are mutually exclusive");
}
else
{
cert_type |= GLOBUS_GSI_CERT_UTILS_TYPE_LIMITED_PROXY;
}
}
else if(strcmp(argp, "-independent") == 0)
{
if (cert_type & GLOBUS_GSI_CERT_UTILS_TYPE_PROXY_MASK)
{
args_error(argp,
"-independent, -limited and -policy/-policy-language are mutually exclusive");
}
else
{
cert_type |= GLOBUS_GSI_CERT_UTILS_TYPE_INDEPENDENT_PROXY;
}
}
else if(strcmp(argp, "-old") == 0)
{
if ((cert_type & GLOBUS_GSI_CERT_UTILS_TYPE_FORMAT_MASK) != 0)
{
args_error(argp,
"-old, -rfc, and -draft are mutually exclusive");
}
else
{
cert_type |= GLOBUS_GSI_CERT_UTILS_TYPE_GSI_2;
}
}
else if(strcmp(argp, "-rfc") == 0)
{
if ((cert_type & GLOBUS_GSI_CERT_UTILS_TYPE_FORMAT_MASK) != 0)
{
args_error(argp, "-old, -rfc, and -draft are mutually exclusive");
}
else
{
cert_type |= GLOBUS_GSI_CERT_UTILS_TYPE_RFC;
}
}
else if(strcmp(argp, "-draft") == 0)
{
if ((cert_type & GLOBUS_GSI_CERT_UTILS_TYPE_FORMAT_MASK) != 0)
{
args_error(argp, "-old, -rfc, and -draft are mutually exclusive");
}
else
{
cert_type |= GLOBUS_GSI_CERT_UTILS_TYPE_GSI_3;
}
}
else if(strcmp(argp, "-verify") == 0)
{
verify++;
}
else if(strcmp(argp, "-q") == 0)
{
quiet++;
}
else if(strcmp(argp, "-pwstdin") == 0)
{
pw_cb = globus_i_gsi_proxy_utils_pwstdin_callback;
}
else if(strcmp(argp, "-policy") == 0)
{
if ((cert_type & GLOBUS_GSI_CERT_UTILS_TYPE_PROXY_MASK) != 0 &&
(cert_type & GLOBUS_GSI_CERT_UTILS_TYPE_PROXY_MASK) != GLOBUS_GSI_CERT_UTILS_TYPE_RESTRICTED_PROXY)
{
args_error(argp,
"-independent, -limited and -policy/-policy-language are mutually exclusive");
}
args_verify_next(arg_index, argp,
"policy file name missing");
policy_filename = argv[++arg_index];
cert_type |= GLOBUS_GSI_CERT_UTILS_TYPE_RESTRICTED_PROXY;
}
else if(strcmp(argp, "-pl") == 0 ||
strcmp(argp, "-policy-language") == 0)
{
if ((cert_type & GLOBUS_GSI_CERT_UTILS_TYPE_PROXY_MASK) != 0 &&
(cert_type & GLOBUS_GSI_CERT_UTILS_TYPE_PROXY_MASK) != GLOBUS_GSI_CERT_UTILS_TYPE_RESTRICTED_PROXY)
{
args_error(argp,
"-independent, -limited and -policy/-policy-language are mutually exclusive");
}
args_verify_next(arg_index, argp, "policy language missing");
policy_language = argv[++arg_index];
cert_type |= GLOBUS_GSI_CERT_UTILS_TYPE_RESTRICTED_PROXY;
}
else if(strcmp(argp, "-path-length") == 0)
{
char * remaining;
args_verify_next(arg_index, argp, "integer argument missing");
path_length = strtol(argv[arg_index + 1], &remaining, 0);
if(*remaining != '\0')
{
args_error(argp, "requires an integer argument");
}
arg_index++;
}
else
{
args_error(argp, "unrecognized option");
}
}
umask(0077);
/* A few sanity checks */
if(policy_filename && !policy_language)
{
globus_libc_fprintf(stderr,
"\nERROR: If you specify a policy file "
"you also need to specify a policy language.\n");
exit(1);
}
result = globus_gsi_proxy_handle_attrs_init(&proxy_handle_attrs);
if(result != GLOBUS_SUCCESS)
{
globus_i_gsi_proxy_utils_print_error(result, debug, __FILE__, __LINE__,
"Couldn't initialize "
"the proxy handle attributes.");
}
/* set the key bits for the proxy cert in the proxy handle
* attributes
*/
result = globus_gsi_proxy_handle_attrs_set_keybits(
proxy_handle_attrs, key_bits);
if(result != GLOBUS_SUCCESS)
{
globus_i_gsi_proxy_utils_print_error(
result, debug, __FILE__, __LINE__,
"Couldn't set the key bits for "
"the private key of the proxy certificate.");
}
result = globus_gsi_proxy_handle_attrs_set_key_gen_callback(
proxy_handle_attrs,
globus_i_gsi_proxy_utils_key_gen_callback);
if(result != GLOBUS_SUCCESS)
{
globus_i_gsi_proxy_utils_print_error(
result, debug, __FILE__, __LINE__,
"Couldn't set the key generation callback function.");
}
result = globus_gsi_proxy_handle_init(&proxy_handle, proxy_handle_attrs);
if(result != GLOBUS_SUCCESS)
{
globus_i_gsi_proxy_utils_print_error(
result, debug, __FILE__, __LINE__,
"Couldn't initialize the proxy handle.");
}
result = globus_gsi_proxy_handle_attrs_destroy(proxy_handle_attrs);
if(result != GLOBUS_SUCCESS)
{
globus_i_gsi_proxy_utils_print_error(
result, debug, __FILE__, __LINE__,
"Couldn't destroy proxy handle attributes.");
}
/* set the time valid in the proxy handle
* used to be hours - now the time valid needs to be set in minutes
*/
result = globus_gsi_proxy_handle_set_time_valid(proxy_handle, valid);
if(result != GLOBUS_SUCCESS)
{
globus_i_gsi_proxy_utils_print_error(
result, debug, __FILE__, __LINE__,
"Couldn't set the validity time of the proxy cert to %d minutes.",
valid);
}
/* set the type of proxy to be generated
*/
result = globus_gsi_proxy_handle_set_type(proxy_handle, cert_type);
if(result != GLOBUS_SUCCESS)
{
globus_i_gsi_proxy_utils_print_error(
result, debug, __FILE__, __LINE__,
"Couldn't set the type of the proxy cert.");
}
if(!user_cert_filename || !user_key_filename)
{
result = GLOBUS_GSI_SYSCONFIG_GET_USER_CERT_FILENAME(
user_cert_filename ? NULL : &tmp_user_cert_filename,
user_key_filename ? NULL : &tmp_user_key_filename);
if(result != GLOBUS_SUCCESS)
{
globus_i_gsi_proxy_utils_print_error(
result, debug, __FILE__, __LINE__,
"Couldn't find valid credentials to generate a proxy.");
}
if(tmp_user_cert_filename &&
tmp_user_cert_filename == tmp_user_key_filename)
{
/* supposed to be a pkcs12 formated credential */
user_cert_filename = user_key_filename
= tmp_user_key_filename;
}
}
if(!user_cert_filename)
{
user_cert_filename = tmp_user_cert_filename;
}
if(!user_key_filename)
{
user_key_filename = tmp_user_key_filename;
}
if(debug)
{
globus_libc_fprintf(stderr,
"\nUser Cert File: %s\nUser Key File: %s\n",
user_cert_filename, user_key_filename);
}
if (!strncmp(user_cert_filename, "SC:", 3))
{
EVP_set_pw_prompt("Enter card pin:");
}
else
{
EVP_set_pw_prompt(quiet? "Enter GRID pass phrase:" :
"Enter GRID pass phrase for this identity:");
}
if(!ca_cert_dir && verify)
{
result = GLOBUS_GSI_SYSCONFIG_GET_CERT_DIR(&ca_cert_dir);
if(result != GLOBUS_SUCCESS)
{
globus_i_gsi_proxy_utils_print_error(
result, debug, __FILE__, __LINE__,
"Couldn't find a valid trusted certificate "
"directory.");
}
}
if(debug)
{
globus_libc_fprintf(stderr,
"\nTrusted CA Cert Dir: %s\n",
ca_cert_dir ? ca_cert_dir : "(null)");
}
if(!proxy_out_filename)
{
result = GLOBUS_GSI_SYSCONFIG_GET_PROXY_FILENAME(
&proxy_out_filename,
GLOBUS_PROXY_FILE_OUTPUT);
if(result != GLOBUS_SUCCESS)
{
globus_i_gsi_proxy_utils_print_error(
result, debug, __FILE__, __LINE__,
"Couldn't find a valid location "
"to write the proxy file.");
}
}
else
{
/* verify that the directory path of proxy_out_filename
* exists and is writeable
*/
char * proxy_absolute_path = NULL;
char * temp_filename = NULL;
char * temp_dir = NULL;
/* first, make absolute path */
result = GLOBUS_GSI_SYSCONFIG_MAKE_ABSOLUTE_PATH_FOR_FILENAME(
proxy_out_filename,
&proxy_absolute_path);
if(result != GLOBUS_SUCCESS)
{
globus_i_gsi_proxy_utils_print_error(
result, debug, __FILE__, __LINE__,
"Can't create the absolute path "
"of the proxy filename: %s",
proxy_out_filename);
}
if(proxy_out_filename)
{
free(proxy_out_filename);
}
proxy_out_filename = proxy_absolute_path;
/* then split */
result = GLOBUS_GSI_SYSCONFIG_SPLIT_DIR_AND_FILENAME(
proxy_absolute_path,
&temp_dir,
&temp_filename);
if(result != GLOBUS_SUCCESS)
{
globus_i_gsi_proxy_utils_print_error(
result, debug, __FILE__, __LINE__,
"Can't split the full path into "
"directory and filename. The full path is: %s",
proxy_absolute_path);
if(proxy_absolute_path)
{
free(proxy_absolute_path);
proxy_absolute_path = NULL;
}
}
result = GLOBUS_GSI_SYSCONFIG_DIR_EXISTS(temp_dir);
if(result != GLOBUS_SUCCESS)
{
if(result != GLOBUS_SUCCESS)
{
globus_i_gsi_proxy_utils_print_error(
result, debug, __FILE__, __LINE__,
"%s is not a valid directory for writing the "
"proxy certificate.",
temp_dir);
}
else
{
globus_module_deactivate_all();
exit(1);
}
}
if(temp_dir)
{
free(temp_dir);
temp_dir = NULL;
}
if(temp_filename)
{
free(temp_filename);
temp_filename = NULL;
}
}
if(debug)
{
globus_libc_fprintf(stderr, "\nOutput File: %s\n", proxy_out_filename);
}
result = globus_gsi_cred_handle_init(&cred_handle, NULL);
if(result != GLOBUS_SUCCESS)
{
globus_i_gsi_proxy_utils_print_error(
result, debug, __FILE__, __LINE__,
"Couldn't initialize credential handle.");
}
if(strstr(user_cert_filename, ".p12"))
{
if (pw_cb != NULL)
{
globus_module_activate(GLOBUS_STDIO_UI_MODULE);
}
/* we have a pkcs12 credential */
result = globus_gsi_cred_read_pkcs12(
cred_handle,
user_cert_filename);
if(result != GLOBUS_SUCCESS)
{
globus_i_gsi_proxy_utils_print_error(
result, debug, __FILE__, __LINE__,
"Couldn't read in PKCS12 credential "
"from file: %s\n", user_cert_filename);
}
if (pw_cb != NULL)
{
globus_module_deactivate(GLOBUS_STDIO_UI_MODULE);
}
if (!quiet)
{
char * subject = NULL;
result = globus_gsi_cred_get_identity_name(cred_handle, &subject);
if(result != GLOBUS_SUCCESS)
{
globus_i_gsi_proxy_utils_print_error(
result, debug, __FILE__, __LINE__,
"The subject name of the "
"user certificate could "
"not be retrieved.");
}
printf("Your identity: %s\n", subject);
if(subject)
{
free(subject);
subject = NULL;
}
}
}
else
{
result = globus_gsi_cred_read_cert(
cred_handle,
user_cert_filename);
if(result != GLOBUS_SUCCESS)
{
globus_i_gsi_proxy_utils_print_error(
result, debug, __FILE__, __LINE__,
"Couldn't read user certificate\n"
"cert file location: %s.",
user_cert_filename);
}
if (!quiet)
{
char * subject = NULL;
result = globus_gsi_cred_get_identity_name(cred_handle, &subject);
if(result != GLOBUS_SUCCESS)
{
globus_i_gsi_proxy_utils_print_error(
result, debug, __FILE__, __LINE__,
"The subject name of the "
"user certificate could "
"not be retrieved.");
}
printf("Your identity: %s\n", subject);
if(subject)
{
#ifdef WIN32
X509_free((void *) subject);
#else
free(subject);
#endif
subject = NULL;
}
}
result = globus_gsi_cred_read_key(
cred_handle,
user_key_filename,
pw_cb);
if(result != GLOBUS_SUCCESS)
{
globus_object_t * error;
error = globus_error_peek(result);
if(globus_error_match_openssl_error(error,
ERR_LIB_PEM,
PEM_F_PEM_DO_HEADER,
PEM_R_BAD_DECRYPT)
== GLOBUS_TRUE)
{
globus_i_gsi_proxy_utils_print_error(
result, debug, __FILE__, __LINE__,
"Couldn't read user key: Bad passphrase for key in %s",
user_key_filename);
}
else
{
globus_i_gsi_proxy_utils_print_error(
result, debug, __FILE__, __LINE__,
"Couldn't read user key in %s.",
user_key_filename);
}
}
}
/* add path length constraint */
if(path_length >= 0)
{
result = globus_gsi_proxy_handle_set_pathlen(proxy_handle,
path_length);
if(result != GLOBUS_SUCCESS)
{
globus_i_gsi_proxy_utils_print_error(
result, debug, __FILE__, __LINE__,
"Can't set the path length in the proxy handle.");
}
}
/* add policies now */
if(policy_filename)
{
int policy_buf_size = 0;
FILE * policy_fp = NULL;
policy_fp = fopen(policy_filename, "r");
if(!policy_fp)
{
fprintf(stderr,
"\nERROR: Unable to open policies "
" file: %s\n\n", policy_filename);
exit(1);
}
do
{
policy_buf_size += 512;
/* First time through this is a essentially a malloc() */
policy_buf = realloc(policy_buf,
policy_buf_size);
if (policy_buf == NULL)
{
fprintf(stderr,
"\nAllocation of space for "
"policy buffer failed\n\n");
exit(1);
}
policy_buf_len +=
fread(&policy_buf[policy_buf_len], 1,
512, policy_fp);
/*
* If we read 512 bytes then policy_buf_len and
* policy_buf_size will be equal and there is
* probably more to read. Even if there isn't more
* to read, no harm is done, we just allocate 512
* bytes we don't end up using.
*/
}
while (policy_buf_len == policy_buf_size);
if (policy_buf_len > 0)
{
policy_NID =
OBJ_create(policy_language,
policy_language,
policy_language);
result = globus_gsi_proxy_handle_set_policy(
proxy_handle,
policy_buf,
policy_buf_len,
policy_NID);
if(result != GLOBUS_SUCCESS)
{
globus_i_gsi_proxy_utils_print_error(
result, debug, __FILE__, __LINE__,
"Can't set the policy in the proxy handle.");
}
}
fclose(policy_fp);
}
if (!quiet)
{
printf("Creating proxy ");
fflush(stdout);
}
result = globus_gsi_proxy_create_signed(
proxy_handle,
cred_handle,
&proxy_cred_handle);
if(result != GLOBUS_SUCCESS)
{
globus_i_gsi_proxy_utils_print_error(
result, debug, __FILE__, __LINE__,
"Couldn't create proxy certificate.");
}
if (!quiet)
{
fprintf(stdout, " Done\n");
}
if(verify)
{
result = globus_gsi_callback_data_init(&callback_data);
if(result != GLOBUS_SUCCESS)
{
globus_i_gsi_proxy_utils_print_error(
result, debug, __FILE__, __LINE__,
"Couldn't initialize callback data for credential "
"verification.");
}
result = globus_gsi_callback_set_extension_cb(
callback_data,
globus_l_gsi_proxy_utils_extension_callback);
if(result != GLOBUS_SUCCESS)
{
globus_i_gsi_proxy_utils_print_error(
result, debug, __FILE__, __LINE__,
"Couldn't set the X.509 extension callback in the callback "
"data.");
}
result = globus_gsi_callback_set_cert_dir(
callback_data,
ca_cert_dir);
if(result != GLOBUS_SUCCESS)
{
globus_i_gsi_proxy_utils_print_error(
result, debug, __FILE__, __LINE__,
"Couldn't set the trusted certificate directory in the "
"callback data.");
}
result = globus_gsi_cred_verify_cert_chain(
proxy_cred_handle,
callback_data);
if(result != GLOBUS_SUCCESS)
{
globus_i_gsi_proxy_utils_print_error(
result, debug, __FILE__, __LINE__,
"Couldn't verify the authenticity of the user's "
"credential to generate a proxy from.");
}
globus_libc_fprintf(
stdout,
"Proxy Verify OK\n");
}
else
{
result = globus_gsi_cred_verify(proxy_cred_handle);
if(result != GLOBUS_SUCCESS)
{
globus_i_gsi_proxy_utils_print_error(
result, debug, __FILE__, __LINE__,
"Could not verify the signature of the generated "
"proxy certificate.\n"
"This is likely due to a non-matching user key and cert.");
}
}
if(ca_cert_dir)
{
free(ca_cert_dir);
ca_cert_dir = NULL;
}
result = globus_gsi_cred_write_proxy(proxy_cred_handle,
proxy_out_filename);
if(result != GLOBUS_SUCCESS)
{
globus_i_gsi_proxy_utils_print_error(
result, debug, __FILE__, __LINE__,
"The proxy credential could not be "
"written to the output file.");
}
if(proxy_out_filename)
{
free(proxy_out_filename);
proxy_out_filename = NULL;
}
result = globus_gsi_cred_get_lifetime(
cred_handle,
&lifetime);
if(result != GLOBUS_SUCCESS)
{
globus_i_gsi_proxy_utils_print_error(
result, debug, __FILE__, __LINE__,
"Can't get the lifetime of the proxy credential.");
}
result = globus_gsi_cred_get_goodtill(
proxy_cred_handle,
&goodtill);
if(result != GLOBUS_SUCCESS)
{
globus_i_gsi_proxy_utils_print_error(
result, debug, __FILE__, __LINE__,
"Can't get the expiration date of the proxy credential.");
}
if(lifetime < 0)
{
globus_libc_fprintf(
stderr,
"\nERROR: Your certificate has expired: %s\n\n",
asctime(localtime(&goodtill)));
globus_module_deactivate_all();
exit(2);
}
else if(lifetime < (valid * 60))
{
globus_libc_fprintf(
stderr,
"\nWarning: your certificate and proxy will expire %s "
"which is within the requested lifetime of the proxy\n",
asctime(localtime(&goodtill)));
}
else if(!quiet)
{
globus_libc_fprintf(
stdout,
"Your proxy is valid until: %s",
asctime(localtime(&goodtill)));
}
BIO_free(pem_proxy_bio);
globus_gsi_proxy_handle_destroy(proxy_handle);
globus_gsi_cred_handle_destroy(cred_handle);
globus_gsi_cred_handle_destroy(proxy_cred_handle);
globus_gsi_callback_data_destroy(callback_data);
if(tmp_user_cert_filename)
{
free(tmp_user_cert_filename);
}
if(tmp_user_key_filename)
{
free(tmp_user_key_filename);
}
globus_module_deactivate_all();
exit(return_value);
}
static int
globus_i_gsi_proxy_utils_pwstdin_callback(
char * buf,
int num,
int w)
{
int i;
if (!(fgets(buf, num, stdin))) {
fprintf(stderr, "Failed to read pass-phrase from stdin\n");
return -1;
}
i = strlen(buf);
if (buf[i-1] == '\n') {
buf[i-1] = '\0';
i--;
}
return i;
}
static void
globus_i_gsi_proxy_utils_key_gen_callback(int p, int n, void * dummy)
{
char c='B';
if (quiet) return;
if (p == 0) c='.';
if (p == 1) c='+';
if (p == 2) c='*';
if (p == 3) c='\n';
if (!debug) c = '.';
fputc(c, stdout);
fflush(stdout);
}
void
globus_i_gsi_proxy_utils_print_error(
globus_result_t result,
int debug,
const char * filename,
int line,
const char * fmt,
...)
{
globus_object_t * error_obj;
char * error_string = NULL;
va_list ap;
if (fmt == NULL)
{
debug++;
}
else
{
fprintf(stderr, "Error: ");
va_start(ap, fmt);
vfprintf(stderr, fmt, ap);
va_end(ap);
fprintf(stderr, "\n");
}
error_obj = globus_error_get(result);
error_string = globus_error_print_chain(error_obj);
if(debug)
{
globus_libc_fprintf(stderr, " %s:%d: %s", filename, line, error_string);
}
else
{
globus_libc_fprintf(stderr, "Use -debug for further information.\n");
}
if(error_string)
{
globus_libc_free(error_string);
}
globus_object_free(error_obj);
globus_module_deactivate_all();
exit(1);
}
static int
globus_l_gsi_proxy_utils_extension_callback(
globus_gsi_callback_data_t callback_data,
X509_EXTENSION * extension)
{
ASN1_OBJECT * extension_object = NULL;
int nid;
int pci_NID;
int pci_old_NID;
pci_NID = OBJ_txt2nid(PROXYCERTINFO_OID);
pci_old_NID = OBJ_txt2nid(PROXYCERTINFO_OLD_OID);
extension_object = X509_EXTENSION_get_object(extension);
nid = OBJ_obj2nid(extension_object);
if(nid == pci_NID || nid == pci_old_NID)
{
/* Assume that we either put it there or that it will be recognized */
return GLOBUS_TRUE;
}
else
{
/* not a PCI extension */
return GLOBUS_FALSE;
}
}
|