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

int pcrypt_decrypt_message ( ipmsg_cap_t  cap,
const char *  encrypted_skey,
char **  ret_buff,
size_t *  decrypted_len 
)

自ホストの秘密鍵で電文復号化用共通鍵を復号化する

Parameters:
[in] cap 暗号化に使用した鍵種別
[in] encrypted_skey 暗号化された共通鍵(hex形式)
[out] ret_buff 復号化した共通鍵(バイナリ形式)の返却領域
[out] decrypted_len 復号化した共通鍵長を返却する領域のアドレス
Return values:
0 正常終了
-EINVAL 引数異常 (encrypted_skey, ret_buff, decrypted_lenの いずれかがNULL).
-ENOMEM メモリ不足

Definition at line 1465 of file pubcrypt.c.

References G2IPMSG_CRYPT_EBUFSIZ, and pcrypt_crypt_refer_rsa_key().

Referenced by ipmsg_decrypt_message().

                                            {
      int                      rc = 0;
      RSA                *privkey = NULL;
      char        *decrypted_skey = NULL;
      BIGNUM    *bn_encrypted_key = NULL;
      char              *bin_skey = NULL;
      size_t bn_encrypted_key_len = 0;
      size_t   decrypted_skey_len = 0;
      char                 errbuf[G2IPMSG_CRYPT_EBUFSIZ];

      if ( (encrypted_skey == NULL) || (ret_buff == NULL) || (decrypted_len == NULL) )
            return -EINVAL;

      rc = pcrypt_crypt_refer_rsa_key(cap, &privkey);
      if (rc != 0)
            return rc;

      /*
       * ネットワークバイトオーダ形式の共通鍵格納領域確保
       */
      rc = -ENOMEM;
      bn_encrypted_key = BN_new(); 
      if (bn_encrypted_key == NULL)
            goto no_free_out;

      /*
       * hex形式をネットワークバイトオーダ形式に変換
       */
      rc = BN_hex2bn(&bn_encrypted_key, encrypted_skey);
      if (rc == 0) {
            rc = ERR_get_error();
            err_out("Can not convert hex-key to BIGNUM: err=%s\n", 
                ERR_error_string(rc, errbuf));
            rc = -rc;
            goto free_bn_encrypted_key_out;
      }
  
      /*
       * バイナリ形式の電文暗号化鍵格納領域を確保
       */
      bn_encrypted_key_len = BN_num_bytes(bn_encrypted_key); 

      rc = -ENOMEM;
      bin_skey = g_malloc(bn_encrypted_key_len);
      if (bin_skey == NULL)
            goto free_bn_encrypted_key_out;
      
      /*
       * バイナリ形式に変換
       */
      rc = BN_bn2bin(bn_encrypted_key, bin_skey);
      if (rc == 0) {
            rc = ERR_get_error();
            err_out("Can not convert BIGNUM:to binary err=%s\n", 
                ERR_error_string(rc, errbuf));
            rc = -rc;
            goto free_bin_skey_out;
      }

      /*
       * 秘密鍵長から電文暗号化鍵長を算出
       */
      decrypted_skey_len = BN_num_bytes(privkey->n);

      /*
       * 電文暗号化鍵格納領域を確保
       */
      rc = -ENOMEM;
      decrypted_skey = g_malloc(decrypted_skey_len);
      if (decrypted_skey == NULL)
            goto free_bin_skey_out;

      /*
       * 電文暗号化鍵を復号化
       */
      rc = RSA_private_decrypt(bn_encrypted_key_len, bin_skey, 
          decrypted_skey, privkey, RSA_PKCS1_PADDING);

      if (rc <= 0) {
            if (decrypted_skey != NULL)
                  g_free(decrypted_skey);
            rc = ERR_get_error();
            err_out("Can not decrypt secret key err=%s\n", 
                ERR_error_string(rc, errbuf));
            rc = -rc;
            goto free_bin_skey_out;
      }

      /*
       * 復号化した鍵を返却
       */
      *decrypted_len = decrypted_skey_len;
      *ret_buff = decrypted_skey;

      rc = 0;

free_bin_skey_out:
      if (bin_skey != NULL)
            g_free(bin_skey);

free_bn_encrypted_key_out:
      if (bn_encrypted_key != NULL)
            BN_free(bn_encrypted_key);
no_free_out:
      return rc;
}


Generated by  Doxygen 1.6.0   Back to index