Zohar81
Zohar81

Reputation: 5136

Using Xcode security framework to parse asn1 format

I'd like to parse asn1 format under OS-X 10.11.

Unfortunately, Apple doesn't include openssl as part of their SDK anymore. instead, there's an internal package I was advised to use exposed in the following header :

SDKs/MacOSX10.11.sdk/System/Library/Frameworks/Security.framework/Versions/A/Headers/SecAsn1Coder.h

Unfortunately, the API I needed to parse the asn1 file and extract a given field, seems very different from the original openssl API.

In openssl, the function "asn1parse" defined in include/openssl/asn1.h, gets a DER formatted file, decoding it and return output text that represent the asn1 tree.

In Apple implementation, I've found "SecAsn1Decode" that may provide the same functionality. The documentation says that the output argument (void *dest) is a pointer to "a template-specific struct allocated by the caller", but i don't understand what struct should I expect and how much memory should I allocate ?

perhaps you can help me understand how to use it. any references are welcome.

Upvotes: 3

Views: 2149

Answers (1)

zoul
zoul

Reputation: 104065

There are now several snippets on GitHub showing how to call the SecAsn1Decode function, see here for example:

typedef struct {
    size_t          length;
    unsigned char   *data;
} ASN1_Data;

typedef struct {
    ASN1_Data type;     // INTEGER
    ASN1_Data version;  // INTEGER
    ASN1_Data value;    // OCTET STRING
} RVNReceiptAttribute;

typedef struct {
    RVNReceiptAttribute **attrs;
} RVNReceiptPayload;

// ASN.1 receipt attribute template
static const SecAsn1Template kReceiptAttributeTemplate[] = {
    { SEC_ASN1_SEQUENCE, 0, NULL, sizeof(RVNReceiptAttribute) },
    { SEC_ASN1_INTEGER, offsetof(RVNReceiptAttribute, type), NULL, 0 },
    { SEC_ASN1_INTEGER, offsetof(RVNReceiptAttribute, version), NULL, 0 },
    { SEC_ASN1_OCTET_STRING, offsetof(RVNReceiptAttribute, value), NULL, 0 },
    { 0, 0, NULL, 0 }
};

// ASN.1 receipt template set
static const SecAsn1Template kSetOfReceiptAttributeTemplate[] = {
    { SEC_ASN1_SET_OF, 0, kReceiptAttributeTemplate, sizeof(RVNReceiptPayload) },
    { 0, 0, NULL, 0 }
};

And later:

NSData *payloadData = …
RVNReceiptPayload payload = { NULL };
status = SecAsn1Decode(asn1Decoder, payloadData.bytes, payloadData.length, kSetOfReceiptAttributeTemplate, &payload);

Upvotes: 2

Related Questions