2013-09-16 28 views
5

我試圖找到一種方法,只使用n,e,d將RSA密鑰對加載到Openssl。 根據RSA的openssl文檔,這些組件(p,q等)可以爲NULL,但唯一用於加載我設法找到的密鑰的函數是i2d_RSAPrivateKey/i2d_RSAPublicKey。不幸的是,這些功能只適用於DER格式的密鑰。如何加載沒有p,q等的RSA密鑰對

那麼有什麼方法可以加載我的密鑰(n,e,d),除非直接將它們應用到RSA結構中?

回答

2

......這些函數只適用於DER格式的密鑰。

OpenSSL的app.c有工具使用從文件加載密鑰代碼(出於實用目的,有文件或內存沒有任何區別,因爲你可以使用不同的BIO)。它的轉載如下,它提供了一些格式。

那麼有什麼方法可以加載我的密鑰(n,e,d),除非直接將它們應用到RSA結構中?

你的鑰匙是什麼格式?


EVP_PKEY *load_key(BIO *err, const char *file, int format, int maybe_stdin, 
        const char *pass, ENGINE *e, const char *key_descrip) 
{ 
    BIO *key=NULL; 
    EVP_PKEY *pkey=NULL; 
    PW_CB_DATA cb_data; 

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

    if (file == NULL && (!maybe_stdin || format == FORMAT_ENGINE)) 
    { 
     BIO_printf(err,"no keyfile specified\n"); 
     goto end; 
    } 
#ifndef OPENSSL_NO_ENGINE 
    if (format == FORMAT_ENGINE) 
    { 
     if (!e) 
      BIO_printf(err,"no engine specified\n"); 
     else 
     { 
      pkey = ENGINE_load_private_key(e, file, 
              ui_method, &cb_data); 
      if (!pkey) 
      { 
       BIO_printf(err,"cannot load %s from engine\n",key_descrip); 
       ERR_print_errors(err); 
      } 
     } 
     goto end; 
    } 
#endif 
    key=BIO_new(BIO_s_file()); 
    if (key == NULL) 
    { 
     ERR_print_errors(err); 
     goto end; 
    } 
    if (file == NULL && maybe_stdin) 
    { 
#ifdef _IONBF 
# ifndef OPENSSL_NO_SETVBUF_IONBF 
     setvbuf(stdin, NULL, _IONBF, 0); 
# endif /* ndef OPENSSL_NO_SETVBUF_IONBF */ 
#endif 
     BIO_set_fp(key,stdin,BIO_NOCLOSE); 
    } 
    else 
     if (BIO_read_filename(key,file) <= 0) 
     { 
      BIO_printf(err, "Error opening %s %s\n", 
         key_descrip, file); 
      ERR_print_errors(err); 
      goto end; 
     } 
    if (format == FORMAT_ASN1) 
    { 
     pkey=d2i_PrivateKey_bio(key, NULL); 
    } 
    else if (format == FORMAT_PEM) 
    { 
     pkey=PEM_read_bio_PrivateKey(key,NULL, 
            (pem_password_cb *)password_callback, &cb_data); 
    } 
#if !defined(OPENSSL_NO_RC4) && !defined(OPENSSL_NO_RSA) 
    else if (format == FORMAT_NETSCAPE || format == FORMAT_IISSGC) 
     pkey = load_netscape_key(err, key, file, key_descrip, format); 
#endif 
    else if (format == FORMAT_PKCS12) 
    { 
     if (!load_pkcs12(err, key, key_descrip, 
         (pem_password_cb *)password_callback, &cb_data, 
         &pkey, NULL, NULL)) 
      goto end; 
    } 
#if !defined(OPENSSL_NO_RSA) && !defined(OPENSSL_NO_DSA) && !defined (OPENSSL_NO_RC4) 
    else if (format == FORMAT_MSBLOB) 
     pkey = b2i_PrivateKey_bio(key); 
    else if (format == FORMAT_PVK) 
     pkey = b2i_PVK_bio(key, (pem_password_cb *)password_callback, 
          &cb_data); 
#endif 
    else 
    { 
     BIO_printf(err,"bad input format specified for key file\n"); 
     goto end; 
    } 
end: 
    if (key != NULL) BIO_free(key); 
    if (pkey == NULL) 
    { 
     BIO_printf(err,"unable to load %s\n", key_descrip); 
     ERR_print_errors(err); 
    } 
    return(pkey); 
}