1 /* asn1.h
2  *
3  * Copyright (C) 2006-2021 wolfSSL Inc.
4  *
5  * This file is part of wolfSSL.
6  *
7  * wolfSSL is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * wolfSSL is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
20  */
21 
22 /* asn1.h for openssl */
23 
24 #ifndef WOLFSSL_ASN1_H_
25 #define WOLFSSL_ASN1_H_
26 
27 #include <wolfssl/openssl/ssl.h>
28 
29 #define ASN1_STRING_new       wolfSSL_ASN1_STRING_new
30 #define ASN1_STRING_type_new  wolfSSL_ASN1_STRING_type_new
31 #define ASN1_STRING_type      wolfSSL_ASN1_STRING_type
32 #define ASN1_STRING_set       wolfSSL_ASN1_STRING_set
33 #define ASN1_OCTET_STRING_set wolfSSL_ASN1_STRING_set
34 #define ASN1_STRING_free      wolfSSL_ASN1_STRING_free
35 
36 #define ASN1_get_object       wolfSSL_ASN1_get_object
37 #define d2i_ASN1_OBJECT       wolfSSL_d2i_ASN1_OBJECT
38 #define c2i_ASN1_OBJECT       wolfSSL_c2i_ASN1_OBJECT
39 
40 #define V_ASN1_INTEGER                   0x02
41 #define V_ASN1_OCTET_STRING              0x04 /* tag for ASN1_OCTET_STRING */
42 #define V_ASN1_NEG                       0x100
43 #define V_ASN1_NEG_INTEGER               (2 | V_ASN1_NEG)
44 #define V_ASN1_NEG_ENUMERATED            (10 | V_ASN1_NEG)
45 
46 /* Type for ASN1_print_ex */
47 # define ASN1_STRFLGS_ESC_2253           1
48 # define ASN1_STRFLGS_ESC_CTRL           2
49 # define ASN1_STRFLGS_ESC_MSB            4
50 # define ASN1_STRFLGS_ESC_QUOTE          8
51 # define ASN1_STRFLGS_UTF8_CONVERT       0x10
52 # define ASN1_STRFLGS_IGNORE_TYPE        0x20
53 # define ASN1_STRFLGS_SHOW_TYPE          0x40
54 # define ASN1_STRFLGS_DUMP_ALL           0x80
55 # define ASN1_STRFLGS_DUMP_UNKNOWN       0x100
56 # define ASN1_STRFLGS_DUMP_DER           0x200
57 # define ASN1_STRFLGS_RFC2253            (ASN1_STRFLGS_ESC_2253 | \
58                                           ASN1_STRFLGS_ESC_CTRL | \
59                                           ASN1_STRFLGS_ESC_MSB | \
60                                           ASN1_STRFLGS_UTF8_CONVERT | \
61                                           ASN1_STRFLGS_DUMP_UNKNOWN | \
62                                           ASN1_STRFLGS_DUMP_DER)
63 
64 #define MBSTRING_UTF8                    0x1000
65 #define MBSTRING_ASC                     0x1001
66 #define MBSTRING_BMP                     0x1002
67 #define MBSTRING_UNIV                    0x1004
68 
69 #define ASN1_UTCTIME_print              wolfSSL_ASN1_UTCTIME_print
70 #define ASN1_TIME_check                 wolfSSL_ASN1_TIME_check
71 #define ASN1_TIME_diff                  wolfSSL_ASN1_TIME_diff
72 #define ASN1_TIME_set                   wolfSSL_ASN1_TIME_set
73 
74 #define V_ASN1_EOC                      0
75 #define V_ASN1_NULL                     5
76 #define V_ASN1_OBJECT                   6
77 #define V_ASN1_UTF8STRING               12
78 #define V_ASN1_SEQUENCE                 16
79 #define V_ASN1_SET                      17
80 #define V_ASN1_PRINTABLESTRING          19
81 #define V_ASN1_T61STRING                20
82 #define V_ASN1_IA5STRING                22
83 #define V_ASN1_UTCTIME                  23
84 #define V_ASN1_GENERALIZEDTIME          24
85 #define V_ASN1_UNIVERSALSTRING          28
86 #define V_ASN1_BMPSTRING                30
87 
88 
89 #define V_ASN1_CONSTRUCTED              0x20
90 
91 #define ASN1_STRING_FLAG_BITS_LEFT       0x008
92 #define ASN1_STRING_FLAG_NDEF            0x010
93 #define ASN1_STRING_FLAG_CONT            0x020
94 #define ASN1_STRING_FLAG_MSTRING         0x040
95 #define ASN1_STRING_FLAG_EMBED           0x080
96 
97 /* X.509 PKI size limits from RFC2459 (appendix A) */
98 /* internally our limit is CTC_NAME_SIZE (64) - overriden with WC_CTC_NAME_SIZE */
99 #define ub_name                    CTC_NAME_SIZE /* 32768 */
100 #define ub_common_name             CTC_NAME_SIZE /* 64 */
101 #define ub_locality_name           CTC_NAME_SIZE /* 128 */
102 #define ub_state_name              CTC_NAME_SIZE /* 128 */
103 #define ub_organization_name       CTC_NAME_SIZE /* 64 */
104 #define ub_organization_unit_name  CTC_NAME_SIZE /* 64 */
105 #define ub_title                   CTC_NAME_SIZE /* 64 */
106 #define ub_email_address           CTC_NAME_SIZE /* 128 */
107 
108 
109 WOLFSSL_API WOLFSSL_ASN1_INTEGER *wolfSSL_BN_to_ASN1_INTEGER(
110     const WOLFSSL_BIGNUM*, WOLFSSL_ASN1_INTEGER*);
111 
112 WOLFSSL_API void wolfSSL_ASN1_TYPE_set(WOLFSSL_ASN1_TYPE *a, int type, void *value);
113 
114 WOLFSSL_API int wolfSSL_ASN1_get_object(const unsigned char **in, long *len, int *tag,
115                                         int *cls, long inLen);
116 
117 WOLFSSL_API WOLFSSL_ASN1_OBJECT *wolfSSL_c2i_ASN1_OBJECT(WOLFSSL_ASN1_OBJECT **a,
118         const unsigned char **pp, long len);
119 
120 #ifdef OPENSSL_ALL
121 /* IMPLEMENT_ASN1_FUNCTIONS is strictly for external use only. Internally
122  * we don't use this. Some projects use OpenSSL to implement ASN1 types and
123  * this section is only to provide those projects with ASN1 functionality. */
124 typedef struct {
125     size_t offset;              /* Offset of this field in structure */
126     byte type;                  /* The type of the member as defined in
127                                  * WOLFSSL_ASN1_TYPES */
128 } WOLFSSL_ASN1_TEMPLATE;
129 
130 typedef struct {
131     byte type;                              /* One of the ASN_Tags types */
132     const WOLFSSL_ASN1_TEMPLATE *members;   /* If SEQUENCE or CHOICE this
133                                              * contains the contents */
134     size_t mcount;                          /* Number of members if SEQUENCE
135                                              * or CHOICE */
136     size_t size;                            /* Structure size */
137 } WOLFSSL_ASN1_ITEM;
138 
139 typedef enum {
140     WOLFSSL_X509_ALGOR_ASN1 = 0,
141     WOLFSSL_ASN1_BIT_STRING_ASN1,
142 } WOLFSSL_ASN1_TYPES;
143 
144 #define ASN1_SEQUENCE(type) \
145     static type __##type##_dummy_struct;\
146     static const WOLFSSL_ASN1_TEMPLATE type##_member_data[]
147 
148 #define ASN1_SIMPLE(type, member, member_type) \
149     { (char*)&__##type##_dummy_struct.member - (char*)&__##type##_dummy_struct, \
150         WOLFSSL_##member_type##_ASN1 }
151 
152 #define ASN1_SEQUENCE_END(type) \
153     ; \
154     const WOLFSSL_ASN1_ITEM type##_template_data = { \
155             ASN_SEQUENCE, \
156             type##_member_data, \
157             sizeof(type##_member_data) / sizeof(WOLFSSL_ASN1_TEMPLATE), \
158             sizeof(type) \
159     };
160 
161 WOLFSSL_API void *wolfSSL_ASN1_item_new(const WOLFSSL_ASN1_ITEM *tpl);
162 WOLFSSL_API void wolfSSL_ASN1_item_free(void *val, const WOLFSSL_ASN1_ITEM *tpl);
163 WOLFSSL_API int wolfSSL_ASN1_item_i2d(const void *src, byte **dest,
164                                       const WOLFSSL_ASN1_ITEM *tpl);
165 
166 /* Need function declaration otherwise compiler complains */
167 #define IMPLEMENT_ASN1_FUNCTIONS(type) \
168     type *type##_new(void); \
169     type *type##_new(void){ \
170         return (type*)wolfSSL_ASN1_item_new(&type##_template_data); \
171     } \
172     void type##_free(type *t); \
173     void type##_free(type *t){ \
174         wolfSSL_ASN1_item_free(t, &type##_template_data); \
175     } \
176     int i2d_##type(type *src, byte **dest); \
177     int i2d_##type(type *src, byte **dest) \
178     { \
179         return wolfSSL_ASN1_item_i2d(src, dest, &type##_template_data);\
180     }
181 
182 #endif /* OPENSSL_ALL */
183 
184 #define BN_to_ASN1_INTEGER          wolfSSL_BN_to_ASN1_INTEGER
185 #define ASN1_TYPE_set               wolfSSL_ASN1_TYPE_set
186 
187 #endif /* WOLFSSL_ASN1_H_ */
188