xref: /aosp_15_r20/external/ms-tpm-20-ref/TPMCmd/tpm/include/TpmASN1.h (revision 5c591343844d1f9da7da26467c4bf7efc8a7a413)
1*5c591343SA. Cody Schuffelen /* Microsoft Reference Implementation for TPM 2.0
2*5c591343SA. Cody Schuffelen  *
3*5c591343SA. Cody Schuffelen  *  The copyright in this software is being made available under the BSD License,
4*5c591343SA. Cody Schuffelen  *  included below. This software may be subject to other third party and
5*5c591343SA. Cody Schuffelen  *  contributor rights, including patent rights, and no such rights are granted
6*5c591343SA. Cody Schuffelen  *  under this license.
7*5c591343SA. Cody Schuffelen  *
8*5c591343SA. Cody Schuffelen  *  Copyright (c) Microsoft Corporation
9*5c591343SA. Cody Schuffelen  *
10*5c591343SA. Cody Schuffelen  *  All rights reserved.
11*5c591343SA. Cody Schuffelen  *
12*5c591343SA. Cody Schuffelen  *  BSD License
13*5c591343SA. Cody Schuffelen  *
14*5c591343SA. Cody Schuffelen  *  Redistribution and use in source and binary forms, with or without modification,
15*5c591343SA. Cody Schuffelen  *  are permitted provided that the following conditions are met:
16*5c591343SA. Cody Schuffelen  *
17*5c591343SA. Cody Schuffelen  *  Redistributions of source code must retain the above copyright notice, this list
18*5c591343SA. Cody Schuffelen  *  of conditions and the following disclaimer.
19*5c591343SA. Cody Schuffelen  *
20*5c591343SA. Cody Schuffelen  *  Redistributions in binary form must reproduce the above copyright notice, this
21*5c591343SA. Cody Schuffelen  *  list of conditions and the following disclaimer in the documentation and/or
22*5c591343SA. Cody Schuffelen  *  other materials provided with the distribution.
23*5c591343SA. Cody Schuffelen  *
24*5c591343SA. Cody Schuffelen  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ""AS IS""
25*5c591343SA. Cody Schuffelen  *  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26*5c591343SA. Cody Schuffelen  *  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
27*5c591343SA. Cody Schuffelen  *  DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
28*5c591343SA. Cody Schuffelen  *  ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
29*5c591343SA. Cody Schuffelen  *  (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
30*5c591343SA. Cody Schuffelen  *  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
31*5c591343SA. Cody Schuffelen  *  ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
32*5c591343SA. Cody Schuffelen  *  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
33*5c591343SA. Cody Schuffelen  *  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34*5c591343SA. Cody Schuffelen  */
35*5c591343SA. Cody Schuffelen //** Introduction
36*5c591343SA. Cody Schuffelen // This file contains the macro and structure definitions for the X509 commands and
37*5c591343SA. Cody Schuffelen // functions.
38*5c591343SA. Cody Schuffelen 
39*5c591343SA. Cody Schuffelen #ifndef _TPMASN1_H_
40*5c591343SA. Cody Schuffelen #define _TPMASN1_H_
41*5c591343SA. Cody Schuffelen 
42*5c591343SA. Cody Schuffelen //** Includes
43*5c591343SA. Cody Schuffelen 
44*5c591343SA. Cody Schuffelen #include "Tpm.h"
45*5c591343SA. Cody Schuffelen #include "OIDs.h"
46*5c591343SA. Cody Schuffelen 
47*5c591343SA. Cody Schuffelen //** Defined Constants
48*5c591343SA. Cody Schuffelen //*** ASN.1 Universal Types (Class 00b)
49*5c591343SA. Cody Schuffelen #define ASN1_EOC                0x00
50*5c591343SA. Cody Schuffelen #define ASN1_BOOLEAN            0x01
51*5c591343SA. Cody Schuffelen #define ASN1_INTEGER            0x02
52*5c591343SA. Cody Schuffelen #define ASN1_BITSTRING          0x03
53*5c591343SA. Cody Schuffelen #define ASN1_OCTET_STRING       0x04
54*5c591343SA. Cody Schuffelen #define ASN1_NULL               0x05
55*5c591343SA. Cody Schuffelen #define ASN1_OBJECT_IDENTIFIER  0x06
56*5c591343SA. Cody Schuffelen #define ASN1_OBJECT_DESCRIPTOR  0x07
57*5c591343SA. Cody Schuffelen #define ASN1_EXTERNAL           0x08
58*5c591343SA. Cody Schuffelen #define ASN1_REAL               0x09
59*5c591343SA. Cody Schuffelen #define ASN1_ENUMERATED         0x0A
60*5c591343SA. Cody Schuffelen #define ASN1_EMBEDDED           0x0B
61*5c591343SA. Cody Schuffelen #define ASN1_UTF8String         0x0C
62*5c591343SA. Cody Schuffelen #define ASN1_RELATIVE_OID       0x0D
63*5c591343SA. Cody Schuffelen #define ASN1_SEQUENCE           0x10        // Primitive + Constructed + 0x10
64*5c591343SA. Cody Schuffelen #define ASN1_SET                0x11        // Primitive + Constructed + 0x11
65*5c591343SA. Cody Schuffelen #define ASN1_NumericString      0x12
66*5c591343SA. Cody Schuffelen #define ASN1_PrintableString    0x13
67*5c591343SA. Cody Schuffelen #define ASN1_T61String          0x14
68*5c591343SA. Cody Schuffelen #define ASN1_VideoString        0x15
69*5c591343SA. Cody Schuffelen #define ASN1_IA5String          0x16
70*5c591343SA. Cody Schuffelen #define ASN1_UTCTime            0x17
71*5c591343SA. Cody Schuffelen #define ASN1_GeneralizeTime     0x18
72*5c591343SA. Cody Schuffelen #define ASN1_VisibleString	    0x1A
73*5c591343SA. Cody Schuffelen #define ASN1_GeneralString	    0x1B
74*5c591343SA. Cody Schuffelen #define ASN1_UniversalString	0x1C
75*5c591343SA. Cody Schuffelen #define ASN1_CHARACTER STRING	0x1D
76*5c591343SA. Cody Schuffelen #define ASN1_BMPString	        0x1E
77*5c591343SA. Cody Schuffelen #define ASN1_CONSTRUCTED        0x20
78*5c591343SA. Cody Schuffelen 
79*5c591343SA. Cody Schuffelen #define ASN1_APPLICAIION_SPECIFIC   0xA0
80*5c591343SA. Cody Schuffelen 
81*5c591343SA. Cody Schuffelen #define ASN1_CONSTRUCTED_SEQUENCE (ASN1_SEQUENCE + ASN1_CONSTRUCTED)
82*5c591343SA. Cody Schuffelen 
83*5c591343SA. Cody Schuffelen #define MAX_DEPTH              10  // maximum push depth for marshaling context.
84*5c591343SA. Cody Schuffelen 
85*5c591343SA. Cody Schuffelen //** Macros
86*5c591343SA. Cody Schuffelen 
87*5c591343SA. Cody Schuffelen //*** Unmarshaling Macros
88*5c591343SA. Cody Schuffelen #ifndef VERIFY
89*5c591343SA. Cody Schuffelen #define VERIFY(_X_) {if(!(_X_)) goto Error; }
90*5c591343SA. Cody Schuffelen #endif
91*5c591343SA. Cody Schuffelen // Checks the validity of the size making sure that there is no wrap around
92*5c591343SA. Cody Schuffelen #define CHECK_SIZE(context, length)                                                 \
93*5c591343SA. Cody Schuffelen             VERIFY(   (((length) + (context)->offset) >= (context)->offset)         \
94*5c591343SA. Cody Schuffelen                    && (((length) + (context)->offset) <= (context)->size))
95*5c591343SA. Cody Schuffelen #define NEXT_OCTET(context) ((context)->buffer[(context)->offset++])
96*5c591343SA. Cody Schuffelen #define PEEK_NEXT(context) ((context)->buffer[(context)->offset])
97*5c591343SA. Cody Schuffelen 
98*5c591343SA. Cody Schuffelen //*** Marshaling Macros
99*5c591343SA. Cody Schuffelen 
100*5c591343SA. Cody Schuffelen // Marshaling works in reverse order. The offset is set to the top of the buffer and,
101*5c591343SA. Cody Schuffelen // as the buffer is filled, 'offset' counts down to zero. When the full thing is
102*5c591343SA. Cody Schuffelen // encoded it can be moved to the top of the buffer. This happens when the last
103*5c591343SA. Cody Schuffelen // context is closed.
104*5c591343SA. Cody Schuffelen 
105*5c591343SA. Cody Schuffelen #define CHECK_SPACE(context, length)    VERIFY(context->offset > length)
106*5c591343SA. Cody Schuffelen 
107*5c591343SA. Cody Schuffelen //** Structures
108*5c591343SA. Cody Schuffelen 
109*5c591343SA. Cody Schuffelen typedef struct ASN1UnmarshalContext {
110*5c591343SA. Cody Schuffelen     BYTE            *buffer;    // pointer to the buffer
111*5c591343SA. Cody Schuffelen     INT16            size;      // size of the buffer (a negative number indicates
112*5c591343SA. Cody Schuffelen                                 // a parsing failure).
113*5c591343SA. Cody Schuffelen     INT16            offset;    // current offset into the buffer (a negative number
114*5c591343SA. Cody Schuffelen                                 // indicates a parsing failure). Not used
115*5c591343SA. Cody Schuffelen     BYTE             tag;       // The last unmarshaled tag
116*5c591343SA. Cody Schuffelen } ASN1UnmarshalContext;
117*5c591343SA. Cody Schuffelen 
118*5c591343SA. Cody Schuffelen typedef struct ASN1MarshalContext {
119*5c591343SA. Cody Schuffelen     BYTE            *buffer;    // pointer to the start of the buffer
120*5c591343SA. Cody Schuffelen     INT16            offset;    // place on the top where the last entry was added
121*5c591343SA. Cody Schuffelen                                 // items are added from the bottom up.
122*5c591343SA. Cody Schuffelen     INT16            end;       // the end offset of the current value
123*5c591343SA. Cody Schuffelen     INT16            depth;     // how many pushed end values.
124*5c591343SA. Cody Schuffelen     INT16            ends[MAX_DEPTH];
125*5c591343SA. Cody Schuffelen } ASN1MarshalContext;
126*5c591343SA. Cody Schuffelen 
127*5c591343SA. Cody Schuffelen #endif // _TPMASN1_H_
128