2 lappend auto_path [file dirname [info script]]
3 package require ossltest
5 set testname [file rootname [file tail $::argv0]]
7 start_tests "Тесты на cms -encrypt"
9 return [string map {":" "_"} $alg]
15 lappend result [subst $str]
20 if {![file exist encrypt.dat]} {
21 makeFile encrypt.dat [string repeat "Test data to encrypt.\n" 100]
24 if {![info exist env(NO_RSA)]} {
25 test "Creating RSA CA" {
26 makeCA ${testname}CA-RSA rsa:512
29 foreach user {U_cms_enc_rsa_1 U_cms_enc_rsa_2} {
30 test "Make registered user $user" {
31 makeRegisteredUser $user rsa:512 CAname ${testname}CA-RSA
36 test -createsfiles cms_enc_rsa.msg "RSA User 1 encrypts message for RSA user 2" {
37 openssl "cms -encrypt -in encrypt.dat -aes-128-cbc -out cms_enc_rsa.msg U_cms_enc_rsa_2/cert.pem"
38 file isfile cms_enc_rsa.msg
41 test "RSA User 1 cannot decrypt message for RSA user 2" {
42 grep "Error" [openssl "cms -decrypt -in cms_enc_rsa.msg -recip U_cms_enc_rsa_1/cert.pem -inkey U_cms_enc_rsa_1/seckey.pem"]
43 } 1 {Error decrypting CMS}
45 test -createsfiles cms_decrypt.rsa "RSA User 2 (with cert) can decrypt message for RSA user 2" {
46 set expected [getFile encrypt.dat]
47 openssl "cms -decrypt -in cms_enc_rsa.msg -recip U_cms_enc_rsa_2/cert.pem -inkey U_cms_enc_rsa_2/seckey.pem -out cms_decrypt.rsa"
48 set result [getFile cms_decrypt.rsa]
49 string eq $expected $result
52 test -createsfiles cms_decrypt_nocert.rsa "RSA User 2 (without cert) can decrypt message for RSA user 2" {
53 set expected [getFile encrypt.dat]
54 openssl "cms -decrypt -in cms_enc_rsa.msg -inkey U_cms_enc_rsa_2/seckey.pem -out cms_decrypt_nocert.rsa"
55 set result [getFile cms_decrypt_nocert.rsa]
56 string eq $expected $result
60 test "Creating CA 2001" {
61 makeCA ${testname}CA gost2001:A
64 test "Creating CA 2012" {
68 if {[info exist env(ENC_LIST)]} {
69 set enc_list $env(ENC_LIST)
71 switch -exact [engine_name] {
72 "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:}}
73 "open" {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:}}
77 save_env2 {OPENSSL_CONF CRYPT_PARAMS}
78 makeFile cmsenc1.cnf [regsub -all "\n\\s*CRYPT_PARAMS\\s*=\[\^\n]*" [getConfig] ""]
79 set ::env(OPENSSL_CONF) [file join [pwd] cmsenc1.cnf]
81 foreach enc_tuple $enc_list {
82 if {![regexp {^([^:]*:[^:]*):(.*)$} $enc_tuple -> alg crypt_param]} {
86 set alg_fn [make_fn $enc_tuple]
87 set username U_cms_enc_$alg_fn
89 gost2012* {set ::test::ca ${testname}CA-2012}
90 * {set ::test::ca ${testname}CA}
93 test "Creating user $username with key $alg" {
94 makeRegisteredUser $username $alg
96 if {![file exists $username/req.pem]&&[file exists $username/cert.pem]} {
97 file delete $username/cert.pem
99 file exists $username/cert.pem
102 if {[string length $crypt_param]} {
103 set env(CRYPT_PARAMS) $crypt_param
105 if {[info exists env(CRYPT_PARAMS)]} {unset env(CRYPT_PARAMS)}
108 test -createsfiles cms_enc_$alg_fn.msg "Encrypting for $username" {
109 grep "rror" [openssl "cms -encrypt -in encrypt.dat -gost89 -out cms_enc_$alg_fn.msg U_cms_enc_$alg_fn/cert.pem"]
112 if {[info exists env(CRYPT_PARAMS)]} {unset env(CRYPT_PARAMS)}
114 test -createsfiles cms_enc_$alg_fn.pem "Extracting CMS from encrypted structure for $username" {
115 openssl "cms -cmsout -out cms_enc_$alg_fn.pem -outform PEM -in cms_enc_$alg_fn.msg"
116 file isfile cms_enc_$alg_fn.pem
119 test -skip {![file exists cms_enc_$alg_fn.pem]} "Checking oids in pkcs7 structure for $username" {
120 extract_oids cms_enc_$alg_fn.pem PEM
121 } 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]]
123 test -createsfiles cms_decrypt.$alg_fn "Decrypting file encrypted for $username" {
124 set expected [getFile encrypt.dat]
125 openssl "cms -decrypt -debug_decrypt -in cms_enc_$alg_fn.msg -recip U_cms_enc_$alg_fn/cert.pem -inkey U_cms_enc_$alg_fn/seckey.pem -out cms_decrypt.$alg_fn"
126 set result [getFile cms_decrypt.$alg_fn]
127 string eq $expected $result
130 if {[string length $crypt_param]} {
131 set env(CRYPT_PARAMS) $crypt_param
133 if {[info exists env(CRYPT_PARAMS)]} {unset env(CRYPT_PARAMS)}
136 test -createsfiles cms_enc_t_$alg_fn.msg "Encrypting for $username - text format" {
137 grep "rror" [openssl "cms -encrypt -text -in encrypt.dat -gost89 -out cms_enc_t_$alg_fn.msg U_cms_enc_$alg_fn/cert.pem"]
140 if {[info exists env(CRYPT_PARAMS)]} {unset env(CRYPT_PARAMS)}
142 test -createsfiles cms_decrypt_t.$alg_fn "Decrypting file text-encrypted for $username" {
143 set expected [getFile encrypt.dat]
144 openssl "cms -decrypt -debug_decrypt -text -in cms_enc_t_$alg_fn.msg -recip U_cms_enc_$alg_fn/cert.pem -inkey U_cms_enc_$alg_fn/seckey.pem -out cms_decrypt_t.$alg_fn"
145 set result [getFile cms_decrypt_t.$alg_fn]
146 string eq $expected $result
149 test -createsfiles cms_decrypt_t_nocert.$alg_fn "Decrypting file text-encrypted for $username without cert" {
150 set expected [getFile encrypt.dat]
151 openssl "cms -decrypt -debug_decrypt -text -in cms_enc_t_$alg_fn.msg -inkey U_cms_enc_$alg_fn/seckey.pem -out cms_decrypt_t_nocert.$alg_fn"
152 set result [getFile cms_decrypt_t_nocert.$alg_fn]
153 string eq $expected $result
159 test -createfiles cms_enc_4all "Encrypt for all" {
160 grep "rror" [openssl "cms -encrypt -in encrypt.dat -gost89 -out cms_enc_4all.msg [map {U_cms_enc_[make_fn $a]/cert.pem} $enc_list]"]
163 foreach enc_tuple $enc_list {
164 if {![regexp {^([^:]*:[^:]*):(.*)$} $enc_tuple -> alg crypt_param]} {
168 set alg_fn [make_fn $enc_tuple]
169 set username U_cms_enc_$alg_fn
171 test -skip {![file exists cms_enc_4all.msg]} -createsfiles cms_decrypt_4all.$alg_fn "Decrypting gost-encrypted file, recipient $alg_fn" {
172 set expected [getFile encrypt.dat]
173 openssl "cms -decrypt -in cms_enc_4all.msg -recip $username/cert.pem -inkey $username/seckey.pem -out cms_decrypt_4all.$alg_fn"
174 set result [getFile cms_decrypt_4all.$alg_fn]
175 string eq $expected $result
178 test -skip {![file exists cms_enc_4all.msg]} -createsfiles cms_decrypt_4all_nocert.$alg_fn "Decrypting gost-encrypted file without cert, recipient $alg_fn" {
179 set expected [getFile encrypt.dat]
180 openssl "cms -decrypt -debug_decrypt -in cms_enc_4all.msg -inkey $username/seckey.pem -out cms_decrypt_4all_nocert.$alg_fn"
181 set result [getFile cms_decrypt_4all_nocert.$alg_fn]
182 string eq $expected $result
187 restore_env2 {OPENSSL_CONF CRYPT_PARAMS}