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