将signedInfo添加到OpenSSL CMS签名

将signedInfo添加到OpenSSL CMS签名,openssl,Openssl,我正在尝试使用OpenSSL创建CMS签名。虽然我可以创建常规签名,但我的应用程序有一些我无法满足的要求 我的应用程序要求我在签名中添加OID为1.2.840.113635.100.9.1的signedAttr,但我无法这样做。我也找不到任何API可以让我完成同样的任务 我在和上传了两条符合规范的CMS消息 如果这里有人能给我指出正确的方向,我将不胜感激。诀窍是将attr添加到SignerInfo,然后使用它来签署CMS\u ContentInfo #include <openssl/cm

我正在尝试使用OpenSSL创建CMS签名。虽然我可以创建常规签名,但我的应用程序有一些我无法满足的要求

我的应用程序要求我在签名中添加OID为1.2.840.113635.100.9.1的signedAttr,但我无法这样做。我也找不到任何API可以让我完成同样的任务

我在和上传了两条符合规范的CMS消息


如果这里有人能给我指出正确的方向,我将不胜感激。

诀窍是将attr添加到SignerInfo,然后使用它来签署CMS\u ContentInfo

#include <openssl/cms.h>
#include <openssl/err.h>
#include <openssl/pem.h>
#include <string.h>

char *lol = "roflcopter";

int main(int argc, char **argv) {
    OpenSSL_add_all_algorithms();
    ERR_load_crypto_strings();
    int flags = CMS_BINARY | CMS_PARTIAL | CMS_NOSMIMECAP | CMS_DETACHED;
    int ret = -1;

    BIO *bio_cert_and_key = BIO_new_file("/home/umang/.isign/pair.pem", "r");
    if (!bio_cert_and_key)
        goto err;

    X509 *scert = PEM_read_bio_X509(bio_cert_and_key, NULL, 0, NULL);
    BIO_reset(bio_cert_and_key);
    EVP_PKEY *skey = PEM_read_bio_PrivateKey(bio_cert_and_key, NULL, 0, NULL);
    if (!scert || !skey)
        goto err;

    BIO *in = BIO_new_file("/tmp/test2", "r");

    CMS_ContentInfo *cms = CMS_sign(NULL, NULL, NULL, in, flags);
    if (!cms)
        goto err;

    CMS_SignerInfo *si = CMS_add1_signer(cms, scert, skey, NULL, flags);
    ASN1_OBJECT *obj = OBJ_txt2obj("1.2.840.113635.100.9.1", 1);

    if (!si || !obj)
        goto err;

    if (!CMS_signed_add1_attr_by_OBJ(si, obj, 0x4, lol, strlen(lol)) || !CMS_SignerInfo_sign(si))
        goto err;

    int len = i2d_CMS_ContentInfo(cms, NULL);
    unsigned char *buf = malloc(len);
    unsigned char *buf_2 = buf;
    i2d_CMS_ContentInfo(cms, &buf);

    BIO *out = BIO_new_file("/tmp/test", "wb");
    if (!out)
        goto err;
    BIO_write(out, buf_2, len);

    ret = 0;

err:
    BIO_free(bio_cert_and_key);
    BIO_free(in);
    BIO_free(out);
    CMS_ContentInfo_free(cms);
    ASN1_OBJECT_free(obj);
    EVP_PKEY_free(skey);
    X509_free(scert);
    free(buf_2);

    ERR_print_errors_fp(stderr);
    return ret;
}
#包括
#包括
#包括
#包括
char*lol=“roflcopter”;
int main(int argc,字符**argv){
OpenSSL_添加_所有算法();
错误加载加密字符串();
int flags=CMS|u BINARY | CMS|u PARTIAL | CMS|u NOSMIMECAP | CMS|u DETACHED;
int-ret=-1;
BIO*BIO_cert_和_key=BIO_new_文件(“/home/umang/.isign/pair.pem”,“r”);
如果(!生物证书和密钥)
后悔莫及;
X509*scert=PEM_read_bio_X509(bio_cert_和_key,NULL,0,NULL);
BIO_重置(BIO_证书和密钥);
EVP_PKEY*skey=PEM_read_bio_PrivateKey(bio_cert_and_key,NULL,0,NULL);
如果(!scert | |!skey)
后悔莫及;
BIO*in=BIO_new_文件(“/tmp/test2”,“r”);
CMS_ContentInfo*CMS=CMS_符号(NULL、NULL、NULL、in、flags);
如果(!cms)
后悔莫及;
CMS_SignerInfo*si=CMS_add1_signer(CMS、scert、skey、NULL、标志);
ASN1_OBJECT*obj=obj_txt2obj(“1.2.840.113635.100.9.1”,1);
如果(!si | |!obj)
后悔莫及;
如果(!CMS_signed_add1_attr_by_OBJ(si,OBJ,0x4,lol,strlen(lol))| |!CMS_SignerInfo_sign(si))
后悔莫及;
int len=i2d\u CMS\u ContentInfo(CMS,NULL);
无符号字符*buf=malloc(len);
无符号字符*buf_2=buf;
i2d_CMS_内容信息(CMS和buf);
BIO*out=BIO_新文件(“/tmp/test”、“wb”);
如果(!out)
后悔莫及;
BIO_写入(输出,buf_2,len);
ret=0;
错误:
BIO_免费(BIO_证书和密钥);
不含BIO_(in);
无生物素(out);
内容信息免费(CMS);
ASN1_对象_自由(obj);
免费执行副总裁(skey);
X509_自由(scert);
免费(buf_2);
错误打印错误fp(stderr);
返回ret;
}

按照惯例,我将再次回答自己的问题。