1# See the file LICENSE for redistribution information.
2#
3# Copyright (c) 1999,2008 Oracle.  All rights reserved.
4#
5# $Id: sec001.tcl,v 12.6 2008/01/08 20:58:53 bostic Exp $
6#
7# TEST	sec001
8# TEST	Test of security interface
9proc sec001 { } {
10	global errorInfo
11	global errorCode
12	global has_crypto
13	global is_hp_test
14
15	source ./include.tcl
16	# Skip test if release does not support encryption.
17	if { $has_crypto == 0 } {
18		puts "Skipping test sec001 for non-crypto release."
19		return
20	}
21
22	set testfile1 env1.db
23	set testfile2 $testdir/env2.db
24	set subdb1 sub1
25	set subdb2 sub2
26
27	puts "Sec001: Test of basic encryption interface."
28	env_cleanup $testdir
29
30	set passwd1 "passwd1"
31	set passwd1_bad "passwd1_bad"
32	set passwd2 "passwd2"
33	set key "key"
34	set data "data"
35
36	#
37	# This first group tests bad create scenarios and also
38	# tests attempting to use encryption after creating a
39	# non-encrypted env/db to begin with.
40	#
41	set nopass ""
42	puts "\tSec001.a.1: Create db with encryption."
43	set db [berkdb_open -create -encryptaes $passwd1 -btree $testfile2]
44	error_check_good db [is_valid_db $db] TRUE
45	error_check_good dbput [$db put $key $data] 0
46	error_check_good dbclose [$db close] 0
47
48	puts "\tSec001.a.2: Open db without encryption."
49	set stat [catch {berkdb_open_noerr $testfile2} ret]
50	error_check_good db:nocrypto $stat 1
51	error_check_good db:fail [is_substr $ret "no encryption key"] 1
52
53	set ret [berkdb dbremove -encryptaes $passwd1 $testfile2]
54
55	puts "\tSec001.b.1: Create db without encryption or checksum."
56	set db [berkdb_open -create -btree $testfile2]
57	error_check_good db [is_valid_db $db] TRUE
58	error_check_good dbput [$db put $key $data] 0
59	error_check_good dbclose [$db close] 0
60
61	puts "\tSec001.b.2: Open db with encryption."
62	set stat [catch {berkdb_open_noerr -encryptaes $passwd1 $testfile2} ret]
63	error_check_good db:nocrypto $stat 1
64	error_check_good db:fail [is_substr $ret "supplied encryption key"] 1
65
66	set ret [berkdb dbremove $testfile2]
67
68	puts "\tSec001.c.1: Create db with checksum."
69	set db [berkdb_open -create -chksum -btree $testfile2]
70	error_check_good db [is_valid_db $db] TRUE
71	error_check_good dbput [$db put $key $data] 0
72	error_check_good dbclose [$db close] 0
73
74	puts "\tSec001.c.2: Open db with encryption."
75	set stat [catch {berkdb_open_noerr -encryptaes $passwd1 $testfile2} ret]
76	error_check_good db:nocrypto $stat 1
77	error_check_good db:fail [is_substr $ret "supplied encryption key"] 1
78
79	set ret [berkdb dbremove $testfile2]
80
81	puts "\tSec001.d.1: Create subdb with encryption."
82	set db [berkdb_open -create -encryptaes $passwd1 -btree \
83	    $testfile2 $subdb1]
84	error_check_good subdb [is_valid_db $db] TRUE
85	error_check_good dbput [$db put $key $data] 0
86	error_check_good dbclose [$db close] 0
87
88	puts "\tSec001.d.2: Create 2nd subdb without encryption."
89	set stat [catch {berkdb_open_noerr -create -btree \
90	    $testfile2 $subdb2} ret]
91	error_check_good subdb:nocrypto $stat 1
92	error_check_good subdb:fail [is_substr $ret "no encryption key"] 1
93
94	set ret [berkdb dbremove -encryptaes $passwd1 $testfile2]
95
96	puts "\tSec001.e.1: Create subdb without encryption or checksum."
97	set db [berkdb_open -create -btree $testfile2 $subdb1]
98	error_check_good db [is_valid_db $db] TRUE
99	error_check_good dbput [$db put $key $data] 0
100	error_check_good dbclose [$db close] 0
101
102	puts "\tSec001.e.2: Create 2nd subdb with encryption."
103	set stat [catch {berkdb_open_noerr -create -btree -encryptaes $passwd1 \
104	    $testfile2 $subdb2} ret]
105	error_check_good subdb:nocrypto $stat 1
106	error_check_good subdb:fail [is_substr $ret "supplied encryption key"] 1
107
108	env_cleanup $testdir
109
110	puts "\tSec001.f.1: Open env with encryption, empty passwd."
111	set stat [catch {berkdb_env_noerr -create -home $testdir \
112	    -encryptaes $nopass} ret]
113	error_check_good env:nopass $stat 1
114	error_check_good env:fail [is_substr $ret "Empty password"] 1
115
116	puts "\tSec001.f.2: Create without encryption algorithm (DB_ENCRYPT_ANY)."
117	set stat [catch {berkdb_env_noerr -create -home $testdir \
118	    -encryptany $passwd1} ret]
119	error_check_good env:any $stat 1
120	error_check_good env:fail [is_substr $ret "algorithm not supplied"] 1
121
122	puts "\tSec001.f.3: Create without encryption."
123	set env [berkdb_env -create -home $testdir]
124	error_check_good env [is_valid_env $env] TRUE
125
126	# Skip this piece of the test on HP-UX, where we can't
127	# join the env.
128	if { $is_hp_test != 1 } {
129		puts "\tSec001.f.4: Open again with encryption."
130		set stat [catch {berkdb_env_noerr -home $testdir \
131		    -encryptaes $passwd1} ret]
132		error_check_good env:unencrypted $stat 1
133		error_check_good env:fail [is_substr $ret \
134		    "Joining non-encrypted environment"] 1
135	}
136
137	error_check_good envclose [$env close] 0
138
139	env_cleanup $testdir
140
141	#
142	# This second group tests creating and opening a secure env.
143	# We test that others can join successfully, and that other's with
144	# bad/no passwords cannot.  Also test that we cannot use the
145	# db->set_encrypt method when we've already got a secure dbenv.
146	#
147	puts "\tSec001.g.1: Open with encryption."
148	set env [berkdb_env_noerr -create -home $testdir -encryptaes $passwd1]
149	error_check_good env [is_valid_env $env] TRUE
150
151	# We can't open an env twice in HP-UX, so skip the rest.
152	if { $is_hp_test == 1 } {
153		puts "Skipping remainder of test for HP-UX."
154		error_check_good env_close [$env close] 0
155		return
156	}
157
158	puts "\tSec001.g.2: Open again with encryption - same passwd."
159	set env1 [berkdb_env -home $testdir -encryptaes $passwd1]
160	error_check_good env [is_valid_env $env1] TRUE
161	error_check_good envclose [$env1 close] 0
162
163	puts "\tSec001.g.3: Open again with any encryption (DB_ENCRYPT_ANY)."
164	set env1 [berkdb_env -home $testdir -encryptany $passwd1]
165	error_check_good env [is_valid_env $env1] TRUE
166	error_check_good envclose [$env1 close] 0
167
168	puts "\tSec001.g.4: Open with encryption - different length passwd."
169	set stat [catch {berkdb_env_noerr -home $testdir \
170	    -encryptaes $passwd1_bad} ret]
171	error_check_good env:$passwd1_bad $stat 1
172	error_check_good env:fail [is_substr $ret "Invalid password"] 1
173
174	puts "\tSec001.g.5: Open with encryption - different passwd."
175	set stat [catch {berkdb_env_noerr -home $testdir \
176	    -encryptaes $passwd2} ret]
177	error_check_good env:$passwd2 $stat 1
178	error_check_good env:fail [is_substr $ret "Invalid password"] 1
179
180	puts "\tSec001.g.6: Open env without encryption."
181	set stat [catch {berkdb_env_noerr -home $testdir} ret]
182	error_check_good env:$passwd2 $stat 1
183	error_check_good env:fail [is_substr $ret "Encrypted environment"] 1
184
185	puts "\tSec001.g.7: Open database with encryption in env"
186	set stat [catch {berkdb_open_noerr -env $env -btree -create \
187	    -encryptaes $passwd2 $testfile1} ret]
188	error_check_good db:$passwd2 $stat 1
189	error_check_good env:fail [is_substr $ret "method not permitted"] 1
190
191	puts "\tSec001.g.8: Close creating env"
192	error_check_good envclose [$env close] 0
193
194	#
195	# This third group tests opening the env after the original env
196	# handle is closed.  Just to make sure we can reopen it in
197	# the right fashion even if no handles are currently open.
198	#
199	puts "\tSec001.h.1: Reopen without encryption."
200	set stat [catch {berkdb_env_noerr -home $testdir} ret]
201	error_check_good env:noencrypt $stat 1
202	error_check_good env:fail [is_substr $ret "Encrypted environment"] 1
203
204	puts "\tSec001.h.2: Reopen with bad passwd."
205	set stat [catch {berkdb_env_noerr -home $testdir -encryptaes \
206	    $passwd1_bad} ret]
207	error_check_good env:$passwd1_bad $stat 1
208	error_check_good env:fail [is_substr $ret "Invalid password"] 1
209
210	puts "\tSec001.h.3: Reopen with encryption."
211	set env [berkdb_env -create -home $testdir -encryptaes $passwd1]
212	error_check_good env [is_valid_env $env] TRUE
213
214	puts "\tSec001.h.4: 2nd Reopen with encryption."
215	set env1 [berkdb_env -home $testdir -encryptaes $passwd1]
216	error_check_good env [is_valid_env $env1] TRUE
217
218	error_check_good envclose [$env1 close] 0
219	error_check_good envclose [$env close] 0
220
221	puts "\tSec001 complete."
222}
223