openssl 加载私钥

http://stackoverflow.com/questions/18829485/how-to-load-rsa-key-pair-without-p-q-etc-openssl
  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. }

<script>window._bd_share_config={"common":{"bdSnsKey":{},"bdText":"","bdMini":"2","bdMiniList":false,"bdPic":"","bdStyle":"0","bdSize":"16"},"share":{}};with(document)0[(getElementsByTagName('head')[0]||body).appendChild(createElement('script')).src='http://bdimg.share.baidu.com/static/api/js/share.js?v=89860593.js?cdnversion='+~(-new Date()/36e5)];</script>
阅读(1096) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~
评论热议
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

kelsel

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值