2013-10-22 72 views
2

我想給PKCS7編碼封裝的內容信息進行編碼上下文特定的ASN.1:如何使用OpenSSL

Sequence: 
    OID 
    [0] Context-specific 
    OCTET STRING 

我的第一個問題是編碼上下文具體:

[0] 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 documentation

我在正確的軌道上嗎?

回答

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

我的第一個問題是要編碼上下文特定

A Layman's Guide to a Subset of ASN.1, BER, and 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等。

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);