CMS_使用ECDH密钥加密
我正在尝试使用ECDH密钥实现CMS加密。Ref命令行工作正常,但我的程序出现错误-CMS_使用ECDH密钥加密,c,cryptography,openssl,C,Cryptography,Openssl,我正在尝试使用ECDH密钥实现CMS加密。Ref命令行工作正常,但我的程序出现错误- Error Encrypting Data 140508524291744:error:2E06507D:CMS routines:CMS_add1_recipient_cert:not supported for this key type:cms_env.c:210: 在下面的代码中,recip.pem是使用以下步骤创建的 #包括 #包括 #包括 #包括 int main(int argc,字符**arg
Error Encrypting Data
140508524291744:error:2E06507D:CMS routines:CMS_add1_recipient_cert:not supported for this key type:cms_env.c:210:
在下面的代码中,recip.pem是使用以下步骤创建的
#包括
#包括
#包括
#包括
int main(int argc,字符**argv)
{
CMS_ContentInfo*cms1=NULL;
BIO*in=NULL,*out=NULL,*tbio=NULL;
X509*rcert=NULL;
int-ret=1;
int flags=CMS\u流;
OpenSSL_添加_所有算法();
错误加载加密字符串();
tbio=BIO_new_文件(“recip.pem”、“r”);
如果(!tbio)
后悔莫及;
rcert=PEM_read_bio_X509(tbio,NULL,0,NULL);
如果(!rcert)
后悔莫及;
in=BIO_new_文件(“encr.txt”、“r”);
标志|=CMS|u部分;
cms1=CMS_encrypt(NULL、in、EVP_des_ede3_cbc()、标志);
CMS_RecipientInfo*ri;
ri=CMS地址1收件人证书(cms1、rcert、标志);
如果(!ri)
后悔莫及;
如果(!CMS_final(cms1,in,NULL,标志))
后悔莫及;
如果(!cms1)
后悔莫及;
out=BIO_new_文件(“cms1.pem”、“w”);
如果(!out)
后悔莫及;
如果(!PEM_write_bio_CMS_stream(输出、cms1、输入、标志))
后悔莫及;
ret=0;
错误:
如果(ret){
fprintf(stderr,“数据加密错误”);
错误打印错误fp(stderr);
}
如果(cms1)
CMS_内容信息免费(cms1);
如果(rcert)
X509_自由(rcert);
如果(在)
不含BIO_(in);
如果(出)
无生物素(out);
如果(待定)
无生物素(tbio);
返回ret;
}
文档说明ECC密钥不受支持,但命令行cms应用程序可以毫无问题地执行此操作。我浏览了cms应用程序,但无法找出我的不同之处。还尝试了demos文件夹中的cms_enc.c,但也不能处理ecc密钥
我希望我的程序能做的是
> openssl cms -encrypt -aes256 -in "x.txt" -out "y.bin" -outformat PEM ecc.crt
> openssl asn1parse -dump -in "y.bin"
0:d=0 hl=4 l= 855 cons: SEQUENCE
4:d=1 hl=2 l= 9 prim: OBJECT :pkcs7-envelopedData
15:d=1 hl=4 l= 840 cons: cont [ 0 ]
19:d=2 hl=4 l= 836 cons: SEQUENCE
23:d=3 hl=2 l= 1 prim: INTEGER :02
26:d=3 hl=4 l= 379 cons: SET
30:d=4 hl=4 l= 375 cons: cont [ 1 ]
34:d=5 hl=2 l= 1 prim: INTEGER :03
37:d=5 hl=3 l= 151 cons: cont [ 0 ]
40:d=6 hl=3 l= 148 cons: cont [ 1 ]
43:d=7 hl=2 l= 9 cons: SEQUENCE
45:d=8 hl=2 l= 7 prim: OBJECT :id-ecPublicKey
54:d=7 hl=3 l= 134 prim: BIT STRING
0000 - 00 04 00 3e 4c 80 5b bb-10 ff 83 30 33 1f 44 ab ...>L.[....03.D.
0010 - ad 2c a2 9d a9 5a 19 5f-8e 5a dc 30 a2 4d f5 19 .,...Z._.Z.0.M..
0020 - 51 49 e6 cb ff 63 08 e7-87 e8 d3 66 60 f5 0f 67 QI...c.....f`..g
0030 - c1 94 3a aa b9 88 3c 7c-5c cd 5a cd 4e f2 5f ba ..:...<|\.Z.N._.
0040 - 15 05 0d bc 01 be 6c 2c-32 fd b1 3d d1 d1 52 f8 ......l,2..=..R.
0050 - e0 43 a5 03 25 05 2a 63-32 47 d9 e6 15 71 75 d0 .C..%.*c2G...qu.
0060 - 5b 45 ca f5 db 91 e2 68-4c 65 29 1c 6d c3 1f f1 [E.....hLe).m...
0070 - 3a e4 55 78 1b dd 95 b5-bd 24 06 31 34 fa 26 c5 :.Ux.....$.14.&.
0080 - 52 8f c9 24 37 4c R..$7L
191:d=5 hl=2 l= 23 cons: SEQUENCE
193:d=6 hl=2 l= 6 prim: OBJECT :1.3.132.1.11.1
201:d=6 hl=2 l= 13 cons: SEQUENCE
203:d=7 hl=2 l= 9 prim: OBJECT :id-aes256-wrap
214:d=7 hl=2 l= 0 prim: NULL
216:d=5 hl=3 l= 190 cons: SEQUENCE
219:d=6 hl=3 l= 187 cons: SEQUENCE
222:d=7 hl=3 l= 142 cons: SEQUENCE
225:d=8 hl=3 l= 128 cons: SEQUENCE
228:d=9 hl=2 l= 11 cons: SET
230:d=10 hl=2 l= 9 cons: SEQUENCE
232:d=11 hl=2 l= 3 prim: OBJECT :countryName
237:d=11 hl=2 l= 2 prim: PRINTABLESTRING :US
241:d=9 hl=2 l= 19 cons: SET
243:d=10 hl=2 l= 17 cons: SEQUENCE
245:d=11 hl=2 l= 3 prim: OBJECT :stateOrProvinceName
250:d=11 hl=2 l= 10 prim: UTF8STRING :California
## other cert params ##
356:d=8 hl=2 l= 9 prim: INTEGER :FEEAB1D7F247D77B
367:d=7 hl=2 l= 40 prim: OCTET STRING
0000 - d5 fd 5c 37 ac 0b b7 ad-07 b7 42 38 5a 3e a5 bc ..\7......B8Z>..
0010 - 5e eb 1f bf a4 b9 63 bb-6d 58 87 a5 3c 96 34 68 ^.....c.mX..<.4h
0020 - 55 26 4e 63 f0 16 92 0a- U&Nc....
409:d=3 hl=4 l= 446 cons: SEQUENCE
413:d=4 hl=2 l= 9 prim: OBJECT :pkcs7-data
424:d=4 hl=2 l= 29 cons: SEQUENCE
426:d=5 hl=2 l= 9 prim: OBJECT :aes-256-cbc
437:d=5 hl=2 l= 16 prim: OCTET STRING
0000 - 82 cd 6e 4b 7a 2a bc 59-63 c0 55 a3 bb b1 9a e6 ..nKz*.Yc.U.....
455:d=4 hl=4 l= 400 prim: cont [ 0 ]
>openssl cms-encrypt-aes256-in“x.txt”-out“y.bin”-格式超出PEM ecc.crt
>openssl asn1parse-dump-in“y.bin”中
0:d=0 hl=4 l=855 cons:序列
4:d=1 hl=2 l=9原始:对象:pkcs7信封数据
15:d=1 hl=4 l=840 cons:cont[0]
19:d=2 hl=4 l=836 cons:序列
23:d=3 hl=2 l=1初始值:整数:02
26:d=3 hl=4 l=379 cons:设置
30:d=4 hl=4 l=375 cons:cont[1]
34:d=5 hl=2 l=1初始值:整数:03
37:d=5 hl=3 l=151 cons:cont[0]
40:d=6 hl=3 l=148 cons:cont[1]
43:d=7 hl=2 l=9 cons:序列
45:d=8 hl=2 l=7 prim:OBJECT:id ecPublicKey
54:d=7 hl=3 l=134原始:位字符串
0000-00 04 00 3e 4c 80 5b bb-10 ff 83 30 33 1f 44 ab.>L.[…..03.D。
0010-ad 2c a2 9d a9 5a 19 5f-8e 5a dc 30 a2 4d f5 19.,…Z.uz.0.M。。
0020-51 49 e6 cb ff 63 08 e7-87 e8 d3 66 60 f5 0f 67 QI…c…f`…g
0030-c1 94 3a aa b9 88 3c 7c-5c cd 5a cd 4e f2 5f ba。。。。。
0010-5e eb 1f bf a4 b9 63 bb-6d 58 87 a5 3c 96 34 68^…..c.mX。。
您是否建立了openssl
二进制文件的本地副本,正如您引用的链接所假定的那样?如果是,那么不同的行为很可能是由不同版本的openssl引起的(假设您将示例代码与系统库相链接)。最近添加了对EC的支持;我的系统OpenSSL版本1.0.1f报告了与您显示的相同的错误,而本地安装的版本1.0.2a使用EC密钥成功加密
因此,如果您需要二进制文件来处理EC密钥,请在您的系统上安装一个更新的OpenSSL,或者将二进制文件静态链接到本地构建的OpenSSL库(libssl和libcrypto)。我似乎记得,像这样使用DH参数存在一些理论问题(但我可能错了)。这里是“像这样”意味着在一个数据加密密钥下加密一条消息,然后在多个用户密钥下加密数据加密密钥。我认为这就是为什么没有类似于方案的多方密码。也许你应该跳过去看看这样做是否有效。(我希望我能找到我读到的那篇关于它的文章……它太糟糕了。你开始忘记一些事情了……)。我认为加密是可以的。我添加了使用openssl的cms实用程序完成的等效操作的输出。我构建了一个新的openssl并将其符号链接到它-从而错误地假设openssl库也被链接到它。
> openssl cms -encrypt -aes256 -in "x.txt" -out "y.bin" -outformat PEM ecc.crt
> openssl asn1parse -dump -in "y.bin"
0:d=0 hl=4 l= 855 cons: SEQUENCE
4:d=1 hl=2 l= 9 prim: OBJECT :pkcs7-envelopedData
15:d=1 hl=4 l= 840 cons: cont [ 0 ]
19:d=2 hl=4 l= 836 cons: SEQUENCE
23:d=3 hl=2 l= 1 prim: INTEGER :02
26:d=3 hl=4 l= 379 cons: SET
30:d=4 hl=4 l= 375 cons: cont [ 1 ]
34:d=5 hl=2 l= 1 prim: INTEGER :03
37:d=5 hl=3 l= 151 cons: cont [ 0 ]
40:d=6 hl=3 l= 148 cons: cont [ 1 ]
43:d=7 hl=2 l= 9 cons: SEQUENCE
45:d=8 hl=2 l= 7 prim: OBJECT :id-ecPublicKey
54:d=7 hl=3 l= 134 prim: BIT STRING
0000 - 00 04 00 3e 4c 80 5b bb-10 ff 83 30 33 1f 44 ab ...>L.[....03.D.
0010 - ad 2c a2 9d a9 5a 19 5f-8e 5a dc 30 a2 4d f5 19 .,...Z._.Z.0.M..
0020 - 51 49 e6 cb ff 63 08 e7-87 e8 d3 66 60 f5 0f 67 QI...c.....f`..g
0030 - c1 94 3a aa b9 88 3c 7c-5c cd 5a cd 4e f2 5f ba ..:...<|\.Z.N._.
0040 - 15 05 0d bc 01 be 6c 2c-32 fd b1 3d d1 d1 52 f8 ......l,2..=..R.
0050 - e0 43 a5 03 25 05 2a 63-32 47 d9 e6 15 71 75 d0 .C..%.*c2G...qu.
0060 - 5b 45 ca f5 db 91 e2 68-4c 65 29 1c 6d c3 1f f1 [E.....hLe).m...
0070 - 3a e4 55 78 1b dd 95 b5-bd 24 06 31 34 fa 26 c5 :.Ux.....$.14.&.
0080 - 52 8f c9 24 37 4c R..$7L
191:d=5 hl=2 l= 23 cons: SEQUENCE
193:d=6 hl=2 l= 6 prim: OBJECT :1.3.132.1.11.1
201:d=6 hl=2 l= 13 cons: SEQUENCE
203:d=7 hl=2 l= 9 prim: OBJECT :id-aes256-wrap
214:d=7 hl=2 l= 0 prim: NULL
216:d=5 hl=3 l= 190 cons: SEQUENCE
219:d=6 hl=3 l= 187 cons: SEQUENCE
222:d=7 hl=3 l= 142 cons: SEQUENCE
225:d=8 hl=3 l= 128 cons: SEQUENCE
228:d=9 hl=2 l= 11 cons: SET
230:d=10 hl=2 l= 9 cons: SEQUENCE
232:d=11 hl=2 l= 3 prim: OBJECT :countryName
237:d=11 hl=2 l= 2 prim: PRINTABLESTRING :US
241:d=9 hl=2 l= 19 cons: SET
243:d=10 hl=2 l= 17 cons: SEQUENCE
245:d=11 hl=2 l= 3 prim: OBJECT :stateOrProvinceName
250:d=11 hl=2 l= 10 prim: UTF8STRING :California
## other cert params ##
356:d=8 hl=2 l= 9 prim: INTEGER :FEEAB1D7F247D77B
367:d=7 hl=2 l= 40 prim: OCTET STRING
0000 - d5 fd 5c 37 ac 0b b7 ad-07 b7 42 38 5a 3e a5 bc ..\7......B8Z>..
0010 - 5e eb 1f bf a4 b9 63 bb-6d 58 87 a5 3c 96 34 68 ^.....c.mX..<.4h
0020 - 55 26 4e 63 f0 16 92 0a- U&Nc....
409:d=3 hl=4 l= 446 cons: SEQUENCE
413:d=4 hl=2 l= 9 prim: OBJECT :pkcs7-data
424:d=4 hl=2 l= 29 cons: SEQUENCE
426:d=5 hl=2 l= 9 prim: OBJECT :aes-256-cbc
437:d=5 hl=2 l= 16 prim: OCTET STRING
0000 - 82 cd 6e 4b 7a 2a bc 59-63 c0 55 a3 bb b1 9a e6 ..nKz*.Yc.U.....
455:d=4 hl=4 l= 400 prim: cont [ 0 ]
The command line works flawlessly but my program is giving an error