typedef struct pkcs9_attribute_st { ASN1_OBJECT *object; ASN1_STRING *randomvalues; }PKCS9_ATTRIBUTE; ASN1_SEQUENCE(PKCS9_ATTRIBUTE) = { ASN1_SIMPLE(PKCS9_ATTRIBUTE, object, ASN1_OBJECT), ASN1_SET_OF(PKCS9_ATTRIBUTE, randomvalues, ASN1_ANY) } ASN1_SEQUENCE_END(PKCS9_ATTRIBUTE) IMPLEMENT_ASN1_FUNCTIONS(PKCS9_ATTRIBUTE) IMPLEMENT_ASN1_DUP_FUNCTION(PKCS9_ATTRIBUTE) #if 1 int PKCS9_ATTRIBUTE_set1_object(PKCS9_ATTRIBUTE *attr, const ASN1_OBJECT *obj) { if ((attr == NULL) || (obj == NULL)) return 0; ASN1_OBJECT_free(attr->object); attr->object = OBJ_dup(obj); return attr->object != NULL; } int PKCS9_ATTRIBUTE_set1_randomvalues(PKCS9_ATTRIBUTE *attr, int attrtype, const void *data, int len) { ASN1_TYPE *ttmp = NULL; ASN1_STRING *stmp = NULL; int atype = 0; if (!attr) return 0; if (attrtype & MBSTRING_FLAG) { stmp = ASN1_STRING_set_by_NID(NULL, data, len, attrtype, OBJ_obj2nid(attr->object)); if (!stmp) { printf("PKCS9_F_PKCS9_ATTRIBUTE_SET1_DATA\n"); return 0; } atype = stmp->type; } else if (len != -1) { if ((stmp = ASN1_STRING_type_new(attrtype)) == NULL) goto err; if (!ASN1_STRING_set(stmp, data, len)) goto err; atype = attrtype; } /* * This is a bit naughty because the attribute should really have at * least one value but some types use and zero length SET and require * this. */ if (attrtype == 0) { ASN1_STRING_free(stmp); return 1; } if ((ttmp = ASN1_TYPE_new()) == NULL) goto err; if ((len == -1) && !(attrtype & MBSTRING_FLAG)) { if (!ASN1_TYPE_set1(ttmp, attrtype, data)) goto err; } else { ASN1_TYPE_set(ttmp, atype, stmp); stmp = NULL; } if (!sk_ASN1_TYPE_push(attr->randomvalues, ttmp)) goto err; return 1; err: ASN1_TYPE_free(ttmp); ASN1_STRING_free(stmp); return 0; } #endif使用以上代码定义了一个PKCS9_ATTRIBUTE结构,请根据以上定义,将-----BEGIN RKRD.der----- MCAGCiqGSIb3DQEJGQMxEgQQFn6w5yeB5JQBEiM0RVZneA== -----END RKRD.der-----数据进行解码成PKCS9_ATTRIBUTE结构的数据
时间: 2024-03-09 12:44:34 浏览: 180
根据以上定义,可以将 BASE64 编码的 DER 数据解码成 PKCS9_ATTRIBUTE 结构,代码如下:
```c
#include <openssl/pem.h>
#include <openssl/bio.h>
#include <openssl/evp.h>
int main() {
const char* encoded = "MCAGCiqGSIb3DQEJGQMxEgQQFn6w5yeB5JQBEiM0RVZneA==";
BIO* bio = BIO_new(BIO_s_mem());
BIO_write(bio, encoded, strlen(encoded));
BIO* b64 = BIO_new(BIO_f_base64());
BIO_push(b64, bio);
unsigned char outbuf[1024];
int outlen = BIO_read(b64, outbuf, sizeof(outbuf));
BIO_free_all(b64);
PKCS9_ATTRIBUTE* attr = d2i_PKCS9_ATTRIBUTE(NULL, &outbuf, outlen);
if (attr == NULL) {
printf("Failed to parse PKCS9_ATTRIBUTE\n");
return 1;
}
// Do something with the PKCS9_ATTRIBUTE...
ASN1_OBJECT_free(attr->object);
sk_ASN1_TYPE_pop_free(attr->randomvalues, ASN1_TYPE_free);
OPENSSL_free(attr);
return 0;
}
```
这样就可以将 BASE64 编码的 DER 数据解码成 PKCS9_ATTRIBUTE 结构。注意,这里只是将数据解码成了结构体,结构体中的具体数据需要根据实际情况进行处理。
阅读全文