PDF-Make

 view release on metacpan or  search on metacpan

include/pdfmake_asn1.h  view on Meta::CPAN

/*
 * pdfmake_asn1.h — ASN.1 DER encoding/decoding
 *
 * Minimal ASN.1 DER codec for X.509, PKCS#7, PKCS#12 parsing and building.
 * ISO 32000-2:2020 §12.8 (Digital Signatures)
 */

#ifndef PDFMAKE_ASN1_H
#define PDFMAKE_ASN1_H

#include "pdfmake.h"
#include "pdfmake_buf.h"
#include <stdint.h>
#include <stddef.h>

/*============================================================================
 * ASN.1 Tag Classes and Types
 *==========================================================================*/

/* Tag class (bits 7-6) */
#define ASN1_CLASS_UNIVERSAL    0x00
#define ASN1_CLASS_APPLICATION  0x40
#define ASN1_CLASS_CONTEXT      0x80
#define ASN1_CLASS_PRIVATE      0xC0
#define ASN1_CLASS_MASK         0xC0

/* Constructed flag (bit 5) */
#define ASN1_CONSTRUCTED        0x20

/* Universal tags */
#define ASN1_TAG_EOC            0x00
#define ASN1_TAG_BOOLEAN        0x01
#define ASN1_TAG_INTEGER        0x02
#define ASN1_TAG_BIT_STRING     0x03
#define ASN1_TAG_OCTET_STRING   0x04
#define ASN1_TAG_NULL           0x05
#define ASN1_TAG_OID            0x06
#define ASN1_TAG_OBJECT_DESC    0x07
#define ASN1_TAG_EXTERNAL       0x08
#define ASN1_TAG_REAL           0x09
#define ASN1_TAG_ENUMERATED     0x0A
#define ASN1_TAG_EMBEDDED_PDV   0x0B
#define ASN1_TAG_UTF8STRING     0x0C
#define ASN1_TAG_RELATIVE_OID   0x0D
#define ASN1_TAG_SEQUENCE       0x10
#define ASN1_TAG_SET            0x11
#define ASN1_TAG_NUMERICSTRING  0x12
#define ASN1_TAG_PRINTABLESTRING 0x13
#define ASN1_TAG_T61STRING      0x14
#define ASN1_TAG_VIDEOTEXSTRING 0x15
#define ASN1_TAG_IA5STRING      0x16
#define ASN1_TAG_UTCTIME        0x17
#define ASN1_TAG_GENERALIZEDTIME 0x18
#define ASN1_TAG_GRAPHICSTRING  0x19
#define ASN1_TAG_VISIBLESTRING  0x1A
#define ASN1_TAG_GENERALSTRING  0x1B
#define ASN1_TAG_UNIVERSALSTRING 0x1C
#define ASN1_TAG_BMPSTRING      0x1E

/*============================================================================
 * ASN.1 Node Structure
 *==========================================================================*/

typedef struct pdfmake_asn1_node pdfmake_asn1_node_t;

struct pdfmake_asn1_node {
    uint8_t             tag;        /* Full tag byte (class + constructed + number) */
    size_t              length;     /* Content length */
    const uint8_t      *data;       /* Pointer to content (within original buffer) */
    
    /* For constructed types */
    pdfmake_asn1_node_t *children;  /* Linked list of children */
    pdfmake_asn1_node_t *next;      /* Next sibling */
    
    /* Parent reference (for navigation) */
    pdfmake_asn1_node_t *parent;
};

/*============================================================================
 * Tag predicates (inline)
 *==========================================================================*/

/* True iff `node` is a constructed SEQUENCE (0x30). Safe on NULL. */
static PDFMAKE_INLINE int pdfmake_asn1_is_sequence(const pdfmake_asn1_node_t *node) {
    return node && node->tag == (ASN1_TAG_SEQUENCE | ASN1_CONSTRUCTED);
}

/* True iff `node` is a constructed SET (0x31). Safe on NULL. */
static PDFMAKE_INLINE int pdfmake_asn1_is_set(const pdfmake_asn1_node_t *node) {
    return node && node->tag == (ASN1_TAG_SET | ASN1_CONSTRUCTED);
}

/*============================================================================
 * Parsing API
 *==========================================================================*/

/*
 * Parse DER-encoded data into an ASN.1 tree.
 * Returns root node allocated from arena, or NULL on error.
 */
pdfmake_asn1_node_t *pdfmake_asn1_parse(
    pdfmake_arena_t *arena,



( run in 1.428 second using v1.01-cache-2.11-cpan-0b58ddf2af1 )