1/*
2 *  ccasn1.h
3 *  corecrypto
4 *
5 *  Created by Michael Brouwer on 8/6/10.
6 *  Copyright 2010-2012 Apple Inc. All rights reserved.
7 *
8 */
9
10#ifndef _CORECRYPTO_CCASN1_H_
11#define _CORECRYPTO_CCASN1_H_
12
13#include <corecrypto/cc.h>
14#include <stdbool.h>
15#include <string.h>
16
17/* ASN.1 types for on the fly ASN.1 BER/DER encoding/decoding. Don't use
18   these with the ccder interface, use the CCDER_ types instead. */
19enum {
20    CCASN1_EOL               = 0x00,
21    CCASN1_BOOLEAN           = 0x01,
22    CCASN1_INTEGER           = 0x02,
23    CCASN1_BIT_STRING        = 0x03,
24    CCASN1_OCTET_STRING      = 0x04,
25    CCASN1_NULL              = 0x05,
26    CCASN1_OBJECT_IDENTIFIER = 0x06,
27    CCASN1_OBJECT_DESCRIPTOR = 0x07,
28    /* External or instance-of 0x08 */
29    CCASN1_REAL              = 0x09,
30    CCASN1_ENUMERATED        = 0x0a,
31    CCASN1_EMBEDDED_PDV      = 0x0b,
32    CCASN1_UTF8_STRING       = 0x0c,
33    /*                         0x0d */
34    /*                         0x0e */
35    /*                         0x0f */
36    CCASN1_SEQUENCE          = 0x10,
37    CCASN1_SET               = 0x11,
38    CCASN1_NUMERIC_STRING    = 0x12,
39    CCASN1_PRINTABLE_STRING  = 0x13,
40    CCASN1_T61_STRING        = 0x14,
41    CCASN1_VIDEOTEX_STRING   = 0x15,
42    CCASN1_IA5_STRING        = 0x16,
43    CCASN1_UTC_TIME          = 0x17,
44    CCASN1_GENERALIZED_TIME  = 0x18,
45    CCASN1_GRAPHIC_STRING    = 0x19,
46    CCASN1_VISIBLE_STRING    = 0x1a,
47    CCASN1_GENERAL_STRING    = 0x1b,
48    CCASN1_UNIVERSAL_STRING  = 0x1c,
49    /*                         0x1d */
50    CCASN1_BMP_STRING        = 0x1e,
51    CCASN1_HIGH_TAG_NUMBER   = 0x1f,
52    CCASN1_TELETEX_STRING    = CCASN1_T61_STRING,
53
54    CCASN1_TAG_MASK			 = 0xff,
55    CCASN1_TAGNUM_MASK		 = 0x1f,
56
57    CCASN1_METHOD_MASK		 = 0x20,
58    CCASN1_PRIMITIVE		 = 0x00,
59    CCASN1_CONSTRUCTED		 = 0x20,
60
61    CCASN1_CLASS_MASK		 = 0xc0,
62    CCASN1_UNIVERSAL		 = 0x00,
63    CCASN1_APPLICATION		 = 0x40,
64    CCASN1_CONTEXT_SPECIFIC	 = 0x80,
65    CCASN1_PRIVATE			 = 0xc0,
66
67    CCASN1_CONSTRUCTED_SET = CCASN1_SET | CCASN1_CONSTRUCTED,
68    CCASN1_CONSTRUCTED_SEQUENCE = CCASN1_SEQUENCE | CCASN1_CONSTRUCTED,
69
70    // TODO: Remove these 2: */
71    // ASN1_INTEGER = 0x02,
72    ASN1_CONSTRUCTED_SEQUENCE = 0x30
73};
74
75typedef union {
76    const unsigned char *oid;
77} ccoid_t __attribute__((transparent_union));
78
79/* Returns *der iff *der points to a DER encoded oid that fits within *der_len. */
80ccoid_t ccoid_for_der(size_t *der_len, const uint8_t **der);
81
82/* Returns the size of an oid including it's tag and length. */
83CC_INLINE CC_PURE CC_NONNULL_TU((1))
84size_t ccoid_size(ccoid_t oid) {
85    return 2 + oid.oid[1];
86}
87
88CC_INLINE CC_PURE CC_NONNULL_TU((1)) CC_NONNULL_TU((2))
89bool ccoid_equal(ccoid_t oid1, ccoid_t oid2) {
90    return(ccoid_size(oid1) == ccoid_size(oid2) && memcmp(oid1.oid, oid2.oid, ccoid_size(oid1))== 0);
91}
92
93extern const unsigned char *ccsha1_oid;
94
95#endif /* _CORECRYPTO_CCASN1_H_ */
96