Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1542953
  • 博文数量: 596
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 173
  • 用 户 组: 普通用户
  • 注册时间: 2016-07-06 15:50
个人简介

在线笔记

文章分类

全部博文(596)

文章存档

2016年(1)

2015年(104)

2014年(228)

2013年(226)

2012年(26)

2011年(11)

分类: 网络与安全

2014-11-03 15:30:40


  1. EVP_PKEY *load_key(BIO *err, const char *file, int format, int maybe_stdin,
  2.                    const char *pass, ENGINE *e, const char *key_descrip)
  3. {
  4.     BIO *key=NULL;
  5.     EVP_PKEY *pkey=NULL;
  6.     PW_CB_DATA cb_data;

  7.     cb_data.password = pass;
  8.     cb_data.prompt_info = file;

  9.     if (file == NULL && (!maybe_stdin || format == FORMAT_ENGINE))
  10.     {
  11.         BIO_printf(err,"no keyfile specified\n");
  12.         goto end;
  13.     }
  14. #ifndef OPENSSL_NO_ENGINE
  15.     if (format == FORMAT_ENGINE)
  16.     {
  17.         if (!e)
  18.             BIO_printf(err,"no engine specified\n");
  19.         else
  20.         {
  21.             pkey = ENGINE_load_private_key(e, file,
  22.                                            ui_method, &cb_data);
  23.             if (!pkey)
  24.             {
  25.                 BIO_printf(err,"cannot load %s from engine\n",key_descrip);
  26.                 ERR_print_errors(err);
  27.             }
  28.         }
  29.         goto end;
  30.     }
  31. #endif
  32.     key=BIO_new(BIO_s_file());
  33.     if (key == NULL)
  34.     {
  35.         ERR_print_errors(err);
  36.         goto end;
  37.     }
  38.     if (file == NULL && maybe_stdin)
  39.     {
  40. #ifdef _IONBF
  41. # ifndef OPENSSL_NO_SETVBUF_IONBF
  42.         setvbuf(stdin, NULL, _IONBF, 0);
  43. # endif /* ndef OPENSSL_NO_SETVBUF_IONBF */
  44. #endif
  45.         BIO_set_fp(key,stdin,BIO_NOCLOSE);
  46.     }
  47.     else
  48.         if (BIO_read_filename(key,file) <= 0)
  49.         {
  50.             BIO_printf(err, "Error opening %s %s\n",
  51.                        key_descrip, file);
  52.             ERR_print_errors(err);
  53.             goto end;
  54.         }
  55.     if (format == FORMAT_ASN1)
  56.     {
  57.         pkey=d2i_PrivateKey_bio(key, NULL);
  58.     }
  59.     else if (format == FORMAT_PEM)
  60.     {
  61.         pkey=PEM_read_bio_PrivateKey(key,NULL,
  62.                                      (pem_password_cb *)password_callback, &cb_data);
  63.     }
  64. #if !defined(OPENSSL_NO_RC4) && !defined(OPENSSL_NO_RSA)
  65.     else if (format == FORMAT_NETSCAPE || format == FORMAT_IISSGC)
  66.         pkey = load_netscape_key(err, key, file, key_descrip, format);
  67. #endif
  68.     else if (format == FORMAT_PKCS12)
  69.     {
  70.         if (!load_pkcs12(err, key, key_descrip,
  71.                          (pem_password_cb *)password_callback, &cb_data,
  72.                          &pkey, NULL, NULL))
  73.             goto end;
  74.     }
  75. #if !defined(OPENSSL_NO_RSA) && !defined(OPENSSL_NO_DSA) && !defined (OPENSSL_NO_RC4)
  76.     else if (format == FORMAT_MSBLOB)
  77.         pkey = b2i_PrivateKey_bio(key);
  78.     else if (format == FORMAT_PVK)
  79.         pkey = b2i_PVK_bio(key, (pem_password_cb *)password_callback,
  80.                            &cb_data);
  81. #endif
  82.     else
  83.     {
  84.         BIO_printf(err,"bad input format specified for key file\n");
  85.         goto end;
  86.     }
  87. end:
  88.     if (key != NULL) BIO_free(key);
  89.     if (pkey == NULL)
  90.     {
  91.         BIO_printf(err,"unable to load %s\n", key_descrip);
  92.         ERR_print_errors(err);
  93.     }
  94.     return(pkey);
  95. }

阅读(3232) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~