Logo Search packages:      
Sourcecode: g2ipmsg version File versions  Download package

int pcrypt_load_rsa_key ( const ipmsg_cap_t  key_type,
const gchar *  passwd 
)

RSAのキーペア(公開鍵/秘密鍵)をファイルから読み込む

Parameters:
[in] key_type IPMSGの公開鍵暗号化能力ID
[in] passwd 鍵保存時のPKCS#5鍵導出に使用するパスフレーズ NULLの場合は, 暗号化なしのファイルを仮定する.
Return values:
0 正常終了
-EPERM 読み取り権限が無い
-ENOMEM メモリ不足

Definition at line 2060 of file pubcrypt.c.

References check_secure_directory(), G2IPMSG_CRYPT_EBUFSIZ, G2IPMSG_KEY_DIR, get_envval(), load_private_key(), load_public_key(), pcrypt_crypt_set_rsa_key(), pcrypt_get_key_filename(), and pcrypt_get_rsa_key_length().

Referenced by pcrypt_crypt_init_keys().

                                                                    {
      int               rc = 0;
      gchar     *store_dir = NULL;
      char       *home_dir = NULL;
      size_t       key_len = 0;
      RSA             *rsa = NULL;
      int                j = 0;
      size_t key_fname_len = 0;
      char  *priv_key_file = NULL;
      char   *pub_key_file = NULL;
      char      *file_path = NULL;
      char          errbuf[G2IPMSG_CRYPT_EBUFSIZ];

      rc = get_envval("HOME", &home_dir);
      if (rc != 0) 
            goto no_free_out;

      store_dir = g_build_filename(home_dir, G2IPMSG_KEY_DIR, NULL);
      if (store_dir == NULL) {
            rc = -EPERM;
            goto home_dir_free_out;
      }

      rc = check_secure_directory(store_dir);
      if  (rc != 0) {
            err_out("Directory %s is not secure or does not exist.\n",store_dir);
            goto store_dir_free_out;
      }

      dbg_out("Directory check:%s OK\n",store_dir);

      rc = pcrypt_get_key_filename(key_type, &pub_key_file, &priv_key_file);
      if (rc != 0)
            goto store_dir_free_out;

      /*
       * Private key
       */
      key_fname_len = 
            strlen(store_dir) + strlen(priv_key_file) + 3;
      file_path = g_malloc(key_fname_len);
      if (file_path == NULL)
            goto filename_free_out;

      snprintf(file_path, key_fname_len, "%s" G_DIR_SEPARATOR_S "%s",
          store_dir, priv_key_file);

      rc = load_private_key(file_path, &rsa, passwd);
      if (rc != 0)
            goto filename_free_out;
      if (file_path != NULL) {
            g_free(file_path);
            file_path = NULL;
      }

      /*
       * Public key
       */
      key_fname_len = 
            strlen(store_dir) + strlen(pub_key_file) + 3;
      file_path = g_malloc(key_fname_len);
      if (file_path == NULL)
            goto filename_free_out;

      snprintf(file_path, key_fname_len,"%s" G_DIR_SEPARATOR_S "%s",
          store_dir, pub_key_file);

      rc = load_public_key(file_path, &rsa);
      if (rc != 0)
            goto filename_free_out;

      if (file_path != NULL) {
            g_free(file_path);
            file_path=NULL;
      }

      rc = RSA_check_key(rsa);
      if (rc == 0) {
            rc = ERR_get_error();
            err_out("Invalid RSA key : err=%s\n", ERR_error_string(rc, errbuf));
            rc = -rc;
            RSA_free(rsa);
            goto filename_free_out;
      }

      rc = pcrypt_crypt_set_rsa_key(key_type, rsa);
      if (rc != 0) {
            RSA_free(rsa);
            goto filename_free_out;
      }

      rc = pcrypt_get_rsa_key_length(key_type, &key_len);
      g_assert(rc == 0);
      dbg_out("RSA key length: %d has been loaded successfully.\n", key_len);

      rc = 0;

filename_free_out:
      if (file_path != NULL)
            g_free(file_path);

pub_key_file_free_out:
      if (pub_key_file != NULL)
            g_free(pub_key_file);
      if (priv_key_file != NULL)
            g_free(priv_key_file);

store_dir_free_out:
      if (store_dir != NULL)
            g_free(store_dir);

home_dir_free_out:
      if (home_dir != NULL)
            g_free(home_dir);

no_free_out:
      return rc;
}


Generated by  Doxygen 1.6.0   Back to index