2

我正在尝试对 pkcs7 封装的内容信息进行编码:

Sequence:
  OID
  [0] Context-specific
     OCTET STRING

我的第一个问题是编码特定于上下文的:

[0] Context-specific

所以我尝试使用“八位组字符串”创建一个“SET Context-specific”,但没有成功:

// Create ASN1_OCTET
ASN1_OCTET_STRING *obj = ASN1_OCTET_STRING_new();
const BYTE* ptr = dataToSign.getData();
ASN1_OCTET_STRING_set(obj, ptr, dataToSign.getSize());

// Create ASN1_TYPE using ASN1_OCTET
ASN1_TYPE   *asn1Type   = ASN1_TYPE_new();
asn1Type->type = V_ASN1_OCTET_STRING;
asn1Type->value.octet_string = obj;

// Using i2d_ASN1_SET_OF_ASN1_TYPE
stack_st_ASN1_TYPE* sk = sk_ASN1_TYPE_new_null();
sk_ASN1_TYPE_push(sk,asn1Type);
int tamanho = i2d_ASN1_SET_OF_ASN1_TYPE(sk,(unsigned char **) NULL, i2d_ASN1_TYPE,V_ASN1_SET, V_ASN1_CONTEXT_SPECIFIC, IS_SET);
unsigned char* data = new BYTE[tamanho];
tamanho = i2d_ASN1_SET_OF_ASN1_TYPE(sk,(unsigned char **) &data, i2d_ASN1_TYPE,V_ASN1_SET, V_ASN1_CONTEXT_SPECIFIC, IS_SET);

我没有在openssl的网站上找到文档。这个地方更好umich - Openssl 文档

我在正确的轨道上吗?

4

2 回答 2

1

使用 i2d_ASN1_bytes 函数:

// Initialize ASN1_STRING inplace (no need to free)
ASN1_STRING s = { 0, 0, NULL, 0};
// Initialize with our data
ASN1_STRING_set0(&obj, dataToSign.getData(), dataToSign.getSize());

// Get resulting object length
int data_len = i2d_ASN1_bytes(obj, NULL, 0, V_ASN1_CONTEXT_SPECIFIC)
// Encode object with context tag 0
unsigned char* data = new BYTE[data_len];
unsigned char* p = data;
i2d_ASN1_bytes(obj, &p, 0, V_ASN1_CONTEXT_SPECIFIC);
于 2015-04-27T22:35:59.047 回答
0

如何使用 Openssl 对 ASN.1 特定于上下文的编码?

来自asn1.h

#define V_ASN1_UNIVERSAL        0x00
#define V_ASN1_APPLICATION      0x40
#define V_ASN1_CONTEXT_SPECIFIC     0x80
#define V_ASN1_PRIVATE          0xc0
...

#define V_ASN1_BOOLEAN          1   /**/
#define V_ASN1_INTEGER          2
...
#define V_ASN1_UTF8STRING       12
#define V_ASN1_SEQUENCE         16
#define V_ASN1_SET          17
...

所以你需要使用标签V_ASN1_CONTEXT_SPECIFIC

我的第一个问题是编码特定于上下文的

外行指南到 ASN.1、BER 和 DER 的子集(第 12 页):

> Example 1: PKCS #7's ContentInfo type has an optional
> content component with an explicit, context-specific tag:
>
> ContentInfo ::= SEQUENCE {
>    contentType ContentType,
>    content
>    [0] EXPLICIT ANY DEFINED BY contentType OPTIONAL }
> 
> Here the underlying type is ANY DEFINED BY contentType, the
> class is absent (i.e., context-specific), and the tag number
> within the class is 0.

然后,该文档继续讨论ContentInfo、标识符八位字节、ANY基于的编码contentInfo等。

于 2014-01-18T20:11:57.610 回答