oracle 11g高階安全元件測試透明資料加密(表空間加密)記錄

湖湘文化發表於2014-01-03
 

測試目的:

Oracle 11g表空間加密

測試環境:

Oracle 11.2.0.3 單例項,紅旗linux6.1

說明:以下配置操作,如果未特別說明,預設都為oracle使用者操作

透明資料加密簡介

透明資料加密包括列加密和表空間加密,它是oracle高階安全元件的一部分;通常,oracle11g企業版預設安裝時會安裝上oracle高階安全元件,oracle聲稱這是一個單獨另外收費的元件,作為商業用途使用如果沒有付費意味著侵權。

     為了使用TDE表空間加密,必須執行oracle 11.1或更高版本。如果從較早版本升級,資料庫的相容性引數必須被設定為11.0.0或更高。在oracle11.2版本中,要使用增強的表空間加密特性,資料庫的相容性引數必須被設定為11.2或更高。

表空間加密的一些特點:

加密整個表空間,該表空間裡建立的物件加密和自動解密;

利用批次加密和快取來提高效能,對應用效能估計有5%8%的影響;

加密支援內部大物件,如BLOBCLOB等,不支援外部物件如BFILE

加密表空間裡的所有資料以加密的格式被儲存在磁碟上;

有必要的許可權來檢視或修改的授權使用者可以透明解密資料;

磁碟或備份介質被盜,資料不會受到損害。

TDE表空間加密使用兩層、關鍵基礎架構透明加密和解密表空間;

TDE的主金鑰儲存在外部安全模組(Oracle Wallet中或HSM)。這TDE主金鑰用於加密TDE表空間加密金鑰,而這又是用來加密和解密資料表空間。

以下為詳細測試配置步驟:

1、檢視oracle資料庫安裝了哪些元件

SQL>select * from v$option;

看一下,當前的資料庫是否安裝了oracle 高階安全元件,oracle是否安裝了相應的加密演算法。

$adapters

2、指定錢包存放位置

在目錄 $ORACLE_HOME/network/admin下找到檔案sqlnet.ora,新增如下內容:

# Oracle Advanced Security Transparent Data Encryption

ENCRYPTION_WALLET_LOCATION=

(SOURCE=(METHOD=FILE)(METHOD_DATA=

(DIRECTORY=/u01/app/oracle/product/11.2/network/admin/encryption_wallet)))

注意:如果是RAC環境,oracle建議將錢包位置放置共享儲存上,以便各個節點共享訪問。

3、建立目錄:

$cd /u01/app/oracle/product/11.2/network/admin/

$mkdir  encryption_wallet

4、建立主加密鍵(萬能金鑰)

SQL> select * from v$encryption_wallet;

WRL_TYPE     WRL_PARAMETER                      STATUS

----------   ----------------------------   ------------------

file  /u01/app/oracle/product/11.2/network/admin/encryption_wallet

CLOSED

SQL> ALTER SYSTEM SET ENCRYPTION KEY IDENTIFIED BY "Test123456";

System altered.

SQL> select * from v$encryption_wallet;

WRL_TYPE     WRL_PARAMETER                      STATUS

----------   ----------------------------   ------------------

file  /u01/app/oracle/product/11.2/network/admin/encryption_wallet

OPEN

SQL> ALTER SYSTEM SET ENCRYPTION KEY IDENTIFIED BY "Test123456";

以上命令如果指定的目錄下沒有錢包,則建立一個加密的錢包(ewallet.p12)並且被開啟,而且TDE的主加密鍵被建立或重建;

如果指定的目錄下有錢包,則開啟錢包,而且TDE的主加密鍵被建立或重建;

注意:

主加密鍵應當只被建立一次,除非你想要用一個新的加密鍵來重新加密資料;

只有alter system許可權的使用者才能建立主加密鍵或開啟錢包。

密碼設定建議最少10位字母和數字的組合。

錢包加密密碼和萬能金鑰密碼不是一回事,是兩個不同的密碼。

5、開啟錢包(第一次設定萬能金鑰會自動開啟錢包)

每次資料庫被關閉,錢包也關閉。在加密或解密之前必須確保錢包被開啟。可以配置自動登入開啟(後面第10節內容有講到)。

SQL> ALTER SYSTEM SET ENCRYPTION WALLET OPEN IDENTIFIED BY "Test123456";

Open wallet之前,資料庫必須處於mount 狀態。

(關閉錢包:ALTER SYSTEM SET ENCRYPTION WALLET CLOSE IDENTIFIED BY "Test123456";)

6、建立表空間

建立普通表空間:

SQL> CREATE TABLESPACE test1 DATAFILE '/oradata/test1/test01.dbf' SIZE 200M autoextend on maxsize unlimited;

建立加密表空間:

使用ENCRYPTION 選項,透過USING 選項指定加密演算法,預設使用AES128演算法。注意,storage 選項必須指定ENCRYPT

注意: 存在在加密表空間裡的資料,我們不能直接透過HEX的編輯器或者strings命令直接檢視資料檔案裡的內容。 而如果是普通的資料檔案,可以直接在作業系統層面檢視資料檔案裡的內容。

SQL> CREATE TABLESPACE secure DATAFILE '/oradata/test1/secure01.dbf' SIZE 200M autoextend on maxsize unlimited ENCRYPTION DEFAULT STORAGE(ENCRYPT);

--檢視錶空間加密情況:

SQL> SELECT tablespace_name, encrypted FROM dba_tablespaces;

--建立和檢查記錄:

SQL> CREATE TABLESPACE secure DATAFILE '/oradata/test1/secure01.dbf' SIZE 200M autoextend on maxsize unlimited ENCRYPTION DEFAULT STORAGE(ENCRYPT);

Tablespace created.

SQL> SELECT tablespace_name, encrypted FROM dba_tablespaces;

TABLESPACE_NAME                ENC

------------------------------ ---

SYSTEM                         NO

SYSAUX                         NO

UNDOTBS1                       NO

TEMP                           NO

USERS                          NO

TEST1                          NO

SECURE                         YES

7 rows selected.

7、建立使用者

普通使用者test1,預設表空間為普通非加密表空間

SQL>create user test1 identified by test1 default tablespace test1;

SQL>grant dba to test1;

SQL> conn / as sysdba                                               

Connected.

SQL> create user test1 identified by test1 default tablespace test1;

User created.

SQL> grant dba to test1;

Grant succeeded.

普通使用者test2,預設表空間為加密表空間:

SQL>create user test2 identified by test2 default tablespace secure;

SQL>grant dba to test2;

SQL> conn / as sysdba                                               

Connected.

SQL> create user test2 identified by test2 default tablespace secure;

User created.

SQL> grant dba to test2;

Grant succeeded.

8、在表空間中建立表

普通非加密表空間:

SQL>create table test1 as select * from all_objects;

Table created.

SQL> select count(*) from test1;

  COUNT(*)

----------

     71928

加密表空間:

SQL> create table test2 as select * from test1.test1;

Table created.

SQL> select count(*) from test2;

  COUNT(*)

----------

     71928

9、測試建立索引,驗證加密和非加密表空間

--驗證建立索引

SQL> CREATE INDEX idx_objectid ON test2(OBJECT_ID)TABLESPACE secure;

Index created.

--驗證加密表空間和非加密表空間

SQL> conn test2/test2

SQL> CREATE TABLE test (id NUMBER(10),data VARCHAR2(50));

SQL> INSERT INTO test (id, data) VALUES(1, 'This is a secret!');

SQL> COMMIT;

SQL> select * from test;

ID DATA

------------------------------------------------------------

1 This is a secret!

--flush buffer cache,確保資料刷入到資料檔案:

SQL> ALTER SYSTEM FLUSH BUFFER_CACHE;

--當資料存放在加密的表空間之後,使用HEX editor,如UltraEdit,則不會顯示data,而普通表空間是可以直接檢視的:

$strings secure01.dbf|grep "secret"

--這裡返回為空。

--驗證非加密表空間:

SQL>conn test1/test1

SQL> CREATE TABLE test (id NUMBER(10),data VARCHAR2(50));

SQL> INSERT INTO test (id, data) VALUES(1, 'This is a secret!');

SQL> COMMIT;

SQL> select * from test;

ID DATA

------------------------------------------------------------

1 This is a secret!

--flush buffer cache,確保資料刷入到資料檔案:

SQL> ALTER SYSTEM FLUSH BUFFER_CACHE;

--注意: 這裡對非加密表空間而言,可以直接檢視加密的資料:

$strings test01.dbf|grep "secret"

驗證加密成功:

[oracle@oracle-test1 admin]$ cd /oradata/

[oracle@oracle-test1 oradata]$ cd test1/

[oracle@oracle-test1 test1]$ ll

total 409624

-rw-r----- 1 oracle oinstall 209723392 Jun  7 15:59 secure01.dbf

-rw-r----- 1 oracle oinstall 209723392 Jun  7 16:01 test01.dbf

drwxr-x--- 2 oracle oinstall      4096 Jun  7 15:43 test1

[oracle@oracle-test1 test1]$ pwd

/oradata/test1

[oracle@oracle-test1 test1]$ strings secure01.dbf|grep "secret"

[oracle@oracle-test1 test1]$ strings test01.dbf|grep "secret"

This is a secret!

[oracle@oracle-test1 test1]$

以上表明:

表空間加密配置成功;使用表空間加密,儲存在磁碟上的資料被加密了。

10、配置自動開啟錢包(可選)

預設情況下,每次資料庫被關閉,錢包也關閉。在加密或解密之前必須確保錢包被開啟。

可以配置自動開啟

[oracle@oracle-test1 admin]$ cd encryption_wallet/

[oracle@oracle-test1 encryption_wallet]$ ll

total 4

-rw-r--r-- 1 oracle oinstall 1573 Jun  7 15:50 ewallet.p12

[oracle@oracle-test1 encryption_wallet]$ pwd

/u01/app/oracle/product/11.2/network/admin/encryption_wallet

圖形化顯示方式下,以oracle使用者執行owm,開啟錢包管理工具;

找到存放錢包的路徑,輸入正確的密碼,開啟錢包;

然後從選單欄裡選中"Auto Login",表示自動登入開啟;

配置完成後儲存退出,錢包存放目錄下會生成一個新檔案cwallet.sso

說明:

什麼是錢夾?

錢夾是一個容器,用於儲存認證和簽名證照,包括 TDE 萬能金鑰、PKI 私鑰、許可證和 SSL 需要的信託證照。藉助 TDE,可以在伺服器上使用錢夾來保護 TDE 萬能金鑰。此外,Oracle 要求在 SSL 上通訊的實體包含一個錢夾。除 Diffie-Hellman 外,該錢夾應當含有 X.509 版本 3 許可證、私鑰、信託證照列表。

Oracle 提供兩種型別錢夾:加密錢夾和自動開啟的錢夾。我們為 TDE 推薦加密錢夾(檔名為 ewallet.p12)。資料庫啟動後和訪問 TDE 加密資料前,需手動開啟該錢夾。如果未開啟該錢夾,查詢受 TDE 保護的資料時資料庫將返回錯誤。自動開啟的錢夾(檔名是 cwallet.sso)在資料庫啟動時會自動開啟。因此它適用於無人值守的 Data Guard 環境,在該環境中加密後的列會傳送到二級站點。

如何建立自動開啟的錢夾?

自動開啟的錢夾 ('cwallet.sso') 需要利用現有的加密錢夾 ('ewallet.p12') 建立,這樣萬能金鑰就能在自動開啟的錢夾中使用了。

您可以在 Oracle Wallet Manager (OWM) 中開啟加密錢夾,選中“Auto Login”核取方塊,然後選擇“Save”將自動開啟的錢夾寫到磁碟上,也可以使用命令列工具“orapki”

  orapki wallet create -wallet -auto_login

兩種情況都要求提供錢夾口令。

最重要的,wallet是一次性的,即便使用相同的密碼和檔案路徑,每次建立的證照都是不一樣的,經過試驗,建立包含加密列的表,然後關閉WALLET,並刪除WALLET證照檔案,然後用相同的密碼建立的wallet證照,再開啟WALLET,訪問同一張表,系統報錯"master key not found".將證照復原重新開啟WALLET後恢復正常。所以證照檔案一定要備份。因為它是一次性的。如果丟失證照檔案(或者誤刪除),即便用相同的密碼再生成一次證照也是無效的。一切都晚了。

檢驗是否自動開啟

SQL> shutdown immediate;

Database closed.

Database dismounted.

ORACLE instance shut down.

SQL> startup;    

ORACLE instance started.

Total System Global Area 1152450560 bytes

Fixed Size                  2227704 bytes

Variable Size             385876488 bytes

Database Buffers          754974720 bytes

Redo Buffers                9371648 bytes

Database mounted.

Database opened.

SQL> select * from v$encryption_wallet;

WRL_TYPE    WRL_PARAMETER                     STATUS

--------------------  --------------------------------------------------------------------------------   ------------------

file  /u01/app/oracle/product/11.2/network/admin/encryption_wallet    OPEN

注意

經常備份錢夾(備份到與加密資料不同的位置)

11、普通表空間和加密表空間測試查詢返回時間

建立普通表空間test1和使用者test1

SQL> create tablespace test1 datafile '/oradata/test1/test01.dbf' size 200m autoextend on maxsize unlimited;

Tablespace created.

SQL> create user test1 identified by test1 default tablespace test1;

User created.

SQL> conn / as sysdba

Connected.

SQL> grant dba to test1;

Grant succeeded.

SQL> conn test1/test1

Connected.

SQL> create table test1 as select * from all_objects;

Table created.

SQL> select count(*) from test1;

  COUNT(*)

----------

     71928

加密表空間測試查詢返回時間

SQL> conn / as sysdba                                                

Connected.

SQL> create user test2 identified by test2 default tablespace secure;

User created.

SQL> grant dba to test2;

Grant succeeded.

SQL> conn test2/test2

Connected.

SQL> create table test2 as select * from test1.test1;

Table created.

SQL> select count(*) from test2;

  COUNT(*)

----------

     71928

測試指令碼內容:

 [oracle@oracle-test1 ~]$ more testOracle.sh

#!/bin/bash

if (($# != 2));

then

  printf "使用方法:$0  最大次數  時間間隔()\n\n最大次數為-1表示無限,時間間隔可以為0.1\n"

  exit 0

fi

iMaxCount=$1

fTime=$2

iCount=0

while ((iCount != iMaxCount))

do

  ((iCount=$iCount+1))

  printf "%d次執行\n" $iCount

  printf "set timing on;\nselect * from test1;\nquit;\n" | sqlplus test1/test1 | egrep "已用時間|Elapsed"

  sleep $fTime

  printf "\n"

done

[oracle@oracle-test1 ~]$./testOracle.sh -1 0

加密表空間查詢測試返回時間

查詢表名稱改為test2,使用者名稱和密碼都為test2

測試結論:

初步測試,加密後查詢時間平均增加5%左右。

官方說法:

被加密的資料檔案,臨時表空間、undo表空間和redo日誌、記憶體中的資料都是被保護的。

官方文件,5%-8%的效能下降。

In addition, TDE tablespace encryption takes advantage of bulk encryption and caching to provide enhanced performance. While the actual performance impact on applications can vary, the performance overhead is roughly estimated to be in between 5% and 8%.

12.官方文件配置例子:

Encryption and TDE Tablespace Encryption

This section uses a tutorial approach to help you get started with TDE column

encryption and TDE tablespace encryption. We illustrate the following tasks using

sample scenarios:

Prepare the Database for Transparent Data Encryption

Create a Table with an Encrypted Column

Create an Index on an Encrypted Column

Alter a Table to Encrypt an Existing Column

Create an Encrypted Tablespace

Create a Table in an Encrypted Tablespace

3.3.1 Prepare the Database for Transparent Data Encryption

In order to start using Transparent Data Encryption (TDE), let us first prepare the

database by specifying an Oracle wallet location and setting the master encryption

key. The following steps prepare the database to use TDE:

1. Specify an Oracle Wallet Location in the sqlnet.ora File

2. Create the Master Encryption Key

3. Open the Oracle Wallet

Specify an Oracle Wallet Location in the sqlnet.ora File

Open the sqlnet.ora file located in $ORACLE_HOME/network/admin. Enter the

following line at the end of the file:

ENCRYPTION_WALLET_LOCATION=

(SOURCE=(METHOD=FILE)(METHOD_DATA=

(DIRECTORY=/app/wallet)))

Save the changes and close the file.

Note: You can choose any directory for the encrypted wallet, but the path should not point to the standard obfuscated wallet (cwallet.sso) created during the database installation.

sqlnet.ora檔案中指定錢包位置

Create the Master Encryption Key

Next, we need to create the master encryption key, which is used to encrypt the table

keys. Enter the following commands to create the master encryption key:

SQL> ALTER SYSTEM SET ENCRYPTION KEY IDENTIFIED BY "Easy2rem";

The preceding command achieves the following:

If no encrypted wallet is present in the directory specified, an encrypted wallet is created (ewallet.p12), the wallet is opened, and the master encryption key for TDE is created/re-created.

If an encrypted wallet is present in the directory specified, the wallet is opened, and the master encryption key for TDE is created/re-created.

Note:

The master encryption key should only be created once, unless you want  to reencrypt your data with a new encryption key.

Only users with the ALTER SYSTEM privilege can create a master encryption key or open the wallet.

建立主加密鍵

Open the Oracle Wallet

Every time the database is shut down, the Oracle wallet is closed. You can also explicitly close the wallet.

You need to make sure that the Oracle wallet is open before you can perform any encryption or decryption operation. Use the following command to open the wallet containing the master encryption key:

SQL> ALTER SYSTEM SET ENCRYPTION WALLET OPEN IDENTIFIED BY "Easy2rem";

Note: The password used with the preceding command is the same that you used to create the master encryption key. This becomes the password to open the wallet and make the master encryption key accessible.

在執行加密或解密操作前使用建立主加密鍵時指定的密碼開啟錢包;或者配置自動開啟

3.3.2 Create a Table with an Encrypted Column

CREATE TABLE cust_payment_info

(first_name VARCHAR2(11),

last_name VARCHAR2(10),

order_number NUMBER(5),

credit_card_number VARCHAR2(16) ENCRYPT NO SALT,

active_card VARCHAR2(3));

3.3.3 Create an Encrypted Tablespace

TDE tablespace encryption enables you to encrypt an entire tablespace. All data stored

in the tablespace is encrypted by default. Thus, if you create any table in an encrypted

tablespace, it is encrypted by default. You do not need to perform a granular analysis

of each table column to determine the columns that need encryption.

Let us create an encrypted tablespace to store encrypted tables. The following

command creates an encrypted tablespace called securespace:

SQL> CREATE TABLESPACE securespace DATAFILE '/home/oracle/oracle3/product/11.1.0/db_1/secure01.dbf' SIZE 150M ENCRYPTION DEFAULT STORAGE(ENCRYPT);

Tablespace created.

3.3.4 Create a Table in an Encrypted Tablespace

If we create a table in an encrypted tablespace, then all data in the table is stored in

encrypted form on the disk. The following command creates a table called,

customer_info_payment in an encrypted tablespace called, securespace.

SQL> CREATE TABLE customer_payment_info

2 (first_name VARCHAR2(11),

3 last_name VARCHAR2(10),

4 order_number NUMBER(5),

5 credit_card_number VARCHAR2(16),

6 active_card VARCHAR2(3))TABLESPACE securespace;

Table created.

附:統一的萬能加密金鑰

TDE 表空間加密和 TDE 列加密的萬能加密金鑰現在組合成一個統一的萬能加密金鑰。這就可以對 TDE 表空間加密和 TDE 列加密進行透明的重新生成金鑰操作。

Table 3–7 Transparent Data Encryption SQL Commands Quick Reference

Task SQL Command

Add encrypted

column to existing

table

ALTER TABLE table_name ADD (column_name datatype ENCRYPT);

Create table and

encrypt column

CREATE TABLE table_name (column_name datatype ENCRYPT);

Encrypt unencrypted

existing column

ALTER TABLE table_name MODIFY (column_name ENCRYPT);

Master encryption key:

set or reset

ALTER SYSTEM SET ENCRYPTION KEY IDENTIFIED BY "password";

Master encryption key:

set or reset to use PKI

certificate

ALTER SYSTEM SET ENCRYPTION KEY "certificate_ID" IDENTIFIED BY "password";

Wallet: open to access

master encryption key

ALTER SYSTEM SET ENCRYPTION WALLET OPEN IDENTIFIED BY "password";

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/21256317/viewspace-1066873/,如需轉載,請註明出處,否則將追究法律責任。

相關文章