2 lappend auto_path [file dirname [info script]]
3 package require ossltest
5 set testname [file rootname [file tail $::argv0]]
7 start_tests "Тесты на smime -encrypt"
9 return [string map {":" "_"} $alg]
15 lappend result [subst $str]
19 if {![file exist encrypt.dat]} {
20 makeFile encrypt.dat [string repeat "Test data to encrypt.\n" 100]
23 if {![info exist env(NO_RSA)]} {
24 test "Creating RSA CA" {
25 makeCA ${testname}CA-RSA rsa:512
28 foreach user {U_enc_rsa_1 U_enc_rsa_2} {
29 test "Make registered user $user" {
30 makeRegisteredUser $user rsa:512 CAname ${testname}CA-RSA
35 test "RSA User 1 encrypts message for RSA user 2" {
36 openssl "smime -encrypt -in encrypt.dat -aes-128-cbc -out enc_rsa.msg U_enc_rsa_2/cert.pem"
37 file isfile enc_rsa.msg
40 test "RSA User 1 cannot decrypt message for RSA user 2" {
41 grep "Error" [openssl "smime -decrypt -in enc_rsa.msg -recip U_enc_rsa_1/cert.pem -inkey U_enc_rsa_1/seckey.pem"]
42 } 1 {Error decrypting PKCS#7 structure}
44 test -createsfiles decrypt.rsa "RSA User 2 (with cert) can decrypt message for RSA user 2" {
45 set expected [getFile encrypt.dat]
46 openssl "smime -decrypt -in enc_rsa.msg -recip U_enc_rsa_2/cert.pem -inkey U_enc_rsa_2/seckey.pem -out decrypt.rsa"
47 set result [getFile decrypt.rsa]
48 string eq $expected $result
51 test -createsfiles decrypt_nocert.rsa "RSA User 2 (without cert) can decrypt message for RSA user 2" {
52 set expected [getFile encrypt.dat]
53 openssl "smime -decrypt -in enc_rsa.msg -inkey U_enc_rsa_2/seckey.pem -out decrypt_nocert.rsa"
54 set result [getFile decrypt_nocert.rsa]
55 string eq $expected $result
59 test "Creating CA 2001" {
60 makeCA ${testname}CA gost2001:A
63 test "Creating CA 2012" {
67 if {[info exist env(ENC_LIST)]} {
68 set enc_list $env(ENC_LIST)
70 switch -exact [engine_name] {
71 "ccore" {set enc_list {gost2001:XA:1.2.643.2.2.31.3 gost2001:XB:1.2.643.2.2.31.4 gost2001:XA: gost2012_256:XA:1.2.643.2.2.31.1 gost2012_256:XB:1.2.643.7.1.2.5.1.1 gost2012_256:XA: gost2012_512:A:1.2.643.2.2.31.3 gost2012_512:B:1.2.643.7.1.2.5.1.1 gost2012_512:A:}}
72 "open" {set enc_list {gost2001:XA:1.2.643.2.2.31.3 gost2001:XB:1.2.643.2.2.31.4 gost2012_256:XA:1.2.643.2.2.31.1 gost2012_256:XB:1.2.643.7.1.2.5.1.1 gost2012_512:A:1.2.643.2.2.31.3 gost2012_512:B:1.2.643.7.1.2.5.1.1}}
76 foreach enc_tuple $enc_list {
77 if {![regexp {^([^:]*:[^:]*):(.*)$} $enc_tuple -> alg crypt_param]} {
81 set alg_fn [make_fn $enc_tuple]
82 set username U_enc_$alg_fn
84 gost2012* {set ::test::ca ${testname}CA-2012}
85 * {set ::test::ca ${testname}CA}
88 test "Creating user $username with key $alg" {
89 makeRegisteredUser $username $alg
91 if {![file exists $username/req.pem]&&[file exists $username/cert.pem]} {
92 file delete $username/cert.pem
94 file exists $username/cert.pem
97 if {[string length $crypt_param]} {
98 set env(CRYPT_PARAMS) $crypt_param
100 if {[info exists env(CRYPT_PARAMS)]} {unset env(CRYPT_PARAMS)}
103 test -createsfiles enc_$alg_fn.msg "Encrypting for $username" {
104 grep "rror" [openssl "smime -encrypt -in encrypt.dat -gost89 -out enc_$alg_fn.msg U_enc_$alg_fn/cert.pem"]
107 if {[info exists env(CRYPT_PARAMS)]} {unset env(CRYPT_PARAMS)}
109 test -createsfiles enc_$alg_fn.pem "Extracting PKCS7 from encrypted structure for $username" {
110 openssl "smime -pk7out -out enc_$alg_fn.pem -in enc_$alg_fn.msg"
111 file isfile enc_$alg_fn.pem
114 test -skip {![file exists enc_$alg_fn.pem]} "Checking oids in pkcs7 structure for $username" {
115 extract_oids enc_$alg_fn.pem PEM
116 } 0 [mkObjList [alg_long_name $alg] [pubkey_long_name $alg] [param_hash_long_name [param_hash $alg]] "GOST 28147-89" [encr_long_name $crypt_param]]
118 test -createsfiles decrypt.$alg_fn "Decrypting file encrypted for $username" {
119 set expected [getFile encrypt.dat]
120 openssl "smime -decrypt -in enc_$alg_fn.msg -recip U_enc_$alg_fn/cert.pem -inkey U_enc_$alg_fn/seckey.pem -out decrypt.$alg_fn"
121 set result [getFile decrypt.$alg_fn]
122 string eq $expected $result
125 if {[string length $crypt_param]} {
126 set env(CRYPT_PARAMS) $crypt_param
128 if {[info exists env(CRYPT_PARAMS)]} {unset env(CRYPT_PARAMS)}
131 test -createsfiles enc_t_$alg_fn.msg "Encrypting for $username - text format" {
132 grep "rror" [openssl "smime -encrypt -text -in encrypt.dat -gost89 -out enc_t_$alg_fn.msg U_enc_$alg_fn/cert.pem"]
135 if {[info exists env(CRYPT_PARAMS)]} {unset env(CRYPT_PARAMS)}
137 test -createsfiles decrypt_t.$alg_fn "Decrypting file text-encrypted for $username" {
138 set expected [getFile encrypt.dat]
139 openssl "smime -decrypt -text -in enc_t_$alg_fn.msg -recip U_enc_$alg_fn/cert.pem -inkey U_enc_$alg_fn/seckey.pem -out decrypt_t.$alg_fn"
140 set result [getFile decrypt_t.$alg_fn]
141 string eq $expected $result
144 test -createsfiles decrypt_t_nocert.$alg_fn "Decrypting file text-encrypted for $username without cert" {
145 set expected [getFile encrypt.dat]
146 openssl "smime -decrypt -text -in enc_t_$alg_fn.msg -inkey U_enc_$alg_fn/seckey.pem -out decrypt_t_nocert.$alg_fn"
147 set result [getFile decrypt_t_nocert.$alg_fn]
148 string eq $expected $result
152 if {[info exists env(CRYPT_PARAMS)]} {unset env(CRYPT_PARAMS)}
154 # FIXME этот тест и парное расшифрование надо прогнать с разными параметрами шифрования
155 test -createfiles enc_4all "Encrypt for all" {
156 grep "rror" [openssl "smime -encrypt -in encrypt.dat -gost89 -out enc_4all.msg [map {U_enc_[make_fn $a]/cert.pem} $enc_list]"]
159 foreach enc_tuple $enc_list {
160 if {![regexp {^([^:]*:[^:]*):(.*)$} $enc_tuple -> alg crypt_param]} {
164 set alg_fn [make_fn $enc_tuple]
165 set username U_enc_$alg_fn
167 test -skip {![file exists enc_4all.msg]} -createsfiles decrypt_4all.$alg_fn "Decrypting gost-encrypted file, recipient $alg_fn" {
168 set expected [getFile encrypt.dat]
169 openssl "smime -decrypt -in enc_4all.msg -recip $username/cert.pem -inkey $username/seckey.pem -out decrypt_4all.$alg_fn"
170 set result [getFile decrypt_4all.$alg_fn]
171 string eq $expected $result
174 test -skip {![file exists enc_4all.msg]} -createsfiles decrypt_4all_nocert.$alg_fn "Decrypting gost-encrypted file without cert, recipient $alg_fn" {
175 set expected [getFile encrypt.dat]
176 openssl "smime -decrypt -in enc_4all.msg -inkey $username/seckey.pem -out decrypt_4all_nocert.$alg_fn"
177 set result [getFile decrypt_4all_nocert.$alg_fn]
178 string eq $expected $result
182 restore_env2 {OPENSSL_CONF CRYPT_PARAMS}