yuezonghe | 824eb0c | 2024-06-27 02:32:26 -0700 | [diff] [blame^] | 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_STRING_free wolfSSL_ASN1_STRING_free |
| 34 | |
| 35 | #define ASN1_get_object wolfSSL_ASN1_get_object |
| 36 | #define d2i_ASN1_OBJECT wolfSSL_d2i_ASN1_OBJECT |
| 37 | #define c2i_ASN1_OBJECT wolfSSL_c2i_ASN1_OBJECT |
| 38 | |
| 39 | #define V_ASN1_INTEGER 0x02 |
| 40 | #define V_ASN1_OCTET_STRING 0x04 /* tag for ASN1_OCTET_STRING */ |
| 41 | #define V_ASN1_NEG 0x100 |
| 42 | #define V_ASN1_NEG_INTEGER (2 | V_ASN1_NEG) |
| 43 | #define V_ASN1_NEG_ENUMERATED (10 | V_ASN1_NEG) |
| 44 | |
| 45 | /* Type for ASN1_print_ex */ |
| 46 | # define ASN1_STRFLGS_ESC_2253 1 |
| 47 | # define ASN1_STRFLGS_ESC_CTRL 2 |
| 48 | # define ASN1_STRFLGS_ESC_MSB 4 |
| 49 | # define ASN1_STRFLGS_ESC_QUOTE 8 |
| 50 | # define ASN1_STRFLGS_UTF8_CONVERT 0x10 |
| 51 | # define ASN1_STRFLGS_IGNORE_TYPE 0x20 |
| 52 | # define ASN1_STRFLGS_SHOW_TYPE 0x40 |
| 53 | # define ASN1_STRFLGS_DUMP_ALL 0x80 |
| 54 | # define ASN1_STRFLGS_DUMP_UNKNOWN 0x100 |
| 55 | # define ASN1_STRFLGS_DUMP_DER 0x200 |
| 56 | # define ASN1_STRFLGS_RFC2253 (ASN1_STRFLGS_ESC_2253 | \ |
| 57 | ASN1_STRFLGS_ESC_CTRL | \ |
| 58 | ASN1_STRFLGS_ESC_MSB | \ |
| 59 | ASN1_STRFLGS_UTF8_CONVERT | \ |
| 60 | ASN1_STRFLGS_DUMP_UNKNOWN | \ |
| 61 | ASN1_STRFLGS_DUMP_DER) |
| 62 | |
| 63 | #define MBSTRING_UTF8 0x1000 |
| 64 | #define MBSTRING_ASC 0x1001 |
| 65 | #define MBSTRING_BMP 0x1002 |
| 66 | #define MBSTRING_UNIV 0x1004 |
| 67 | |
| 68 | #define ASN1_UTCTIME_print wolfSSL_ASN1_UTCTIME_print |
| 69 | #define ASN1_TIME_check wolfSSL_ASN1_TIME_check |
| 70 | #define ASN1_TIME_diff wolfSSL_ASN1_TIME_diff |
| 71 | #define ASN1_TIME_set wolfSSL_ASN1_TIME_set |
| 72 | |
| 73 | #define V_ASN1_EOC 0 |
| 74 | #define V_ASN1_OBJECT 6 |
| 75 | #define V_ASN1_UTF8STRING 12 |
| 76 | #define V_ASN1_SEQUENCE 16 |
| 77 | #define V_ASN1_SET 17 |
| 78 | #define V_ASN1_PRINTABLESTRING 19 |
| 79 | #define V_ASN1_T61STRING 20 |
| 80 | #define V_ASN1_IA5STRING 22 |
| 81 | #define V_ASN1_UTCTIME 23 |
| 82 | #define V_ASN1_GENERALIZEDTIME 24 |
| 83 | #define V_ASN1_UNIVERSALSTRING 28 |
| 84 | #define V_ASN1_BMPSTRING 30 |
| 85 | |
| 86 | |
| 87 | #define V_ASN1_CONSTRUCTED 0x20 |
| 88 | |
| 89 | #define ASN1_STRING_FLAG_BITS_LEFT 0x008 |
| 90 | #define ASN1_STRING_FLAG_NDEF 0x010 |
| 91 | #define ASN1_STRING_FLAG_CONT 0x020 |
| 92 | #define ASN1_STRING_FLAG_MSTRING 0x040 |
| 93 | #define ASN1_STRING_FLAG_EMBED 0x080 |
| 94 | |
| 95 | |
| 96 | WOLFSSL_API WOLFSSL_ASN1_INTEGER *wolfSSL_BN_to_ASN1_INTEGER( |
| 97 | const WOLFSSL_BIGNUM*, WOLFSSL_ASN1_INTEGER*); |
| 98 | |
| 99 | WOLFSSL_API void wolfSSL_ASN1_TYPE_set(WOLFSSL_ASN1_TYPE *a, int type, void *value); |
| 100 | |
| 101 | WOLFSSL_API int wolfSSL_ASN1_get_object(const unsigned char **in, long *len, int *tag, |
| 102 | int *cls, long inLen); |
| 103 | |
| 104 | WOLFSSL_API WOLFSSL_ASN1_OBJECT *wolfSSL_c2i_ASN1_OBJECT(WOLFSSL_ASN1_OBJECT **a, |
| 105 | const unsigned char **pp, long len); |
| 106 | |
| 107 | #ifdef OPENSSL_ALL |
| 108 | /* IMPLEMENT_ASN1_FUNCTIONS is strictly for external use only. Internally |
| 109 | * we don't use this. Some projects use OpenSSL to implement ASN1 types and |
| 110 | * this section is only to provide those projects with ASN1 functionality. */ |
| 111 | typedef struct { |
| 112 | size_t offset; /* Offset of this field in structure */ |
| 113 | byte type; /* The type of the member as defined in |
| 114 | * WOLFSSL_ASN1_TYPES */ |
| 115 | } WOLFSSL_ASN1_TEMPLATE; |
| 116 | |
| 117 | typedef struct { |
| 118 | byte type; /* One of the ASN_Tags types */ |
| 119 | const WOLFSSL_ASN1_TEMPLATE *members; /* If SEQUENCE or CHOICE this |
| 120 | * contains the contents */ |
| 121 | size_t mcount; /* Number of members if SEQUENCE |
| 122 | * or CHOICE */ |
| 123 | size_t size; /* Structure size */ |
| 124 | } WOLFSSL_ASN1_ITEM; |
| 125 | |
| 126 | typedef enum { |
| 127 | WOLFSSL_X509_ALGOR_ASN1 = 0, |
| 128 | WOLFSSL_ASN1_BIT_STRING_ASN1, |
| 129 | } WOLFSSL_ASN1_TYPES; |
| 130 | |
| 131 | #define ASN1_SEQUENCE(type) \ |
| 132 | static type __##type##_dummy_struct;\ |
| 133 | static const WOLFSSL_ASN1_TEMPLATE type##_member_data[] |
| 134 | |
| 135 | #define ASN1_SIMPLE(type, member, member_type) \ |
| 136 | { (char*)&__##type##_dummy_struct.member - (char*)&__##type##_dummy_struct, \ |
| 137 | WOLFSSL_##member_type##_ASN1 } |
| 138 | |
| 139 | #define ASN1_SEQUENCE_END(type) \ |
| 140 | ; \ |
| 141 | const WOLFSSL_ASN1_ITEM type##_template_data = { \ |
| 142 | ASN_SEQUENCE, \ |
| 143 | type##_member_data, \ |
| 144 | sizeof(type##_member_data) / sizeof(WOLFSSL_ASN1_TEMPLATE), \ |
| 145 | sizeof(type) \ |
| 146 | }; |
| 147 | |
| 148 | WOLFSSL_API void *wolfSSL_ASN1_item_new(const WOLFSSL_ASN1_ITEM *tpl); |
| 149 | WOLFSSL_API void wolfSSL_ASN1_item_free(void *val, const WOLFSSL_ASN1_ITEM *tpl); |
| 150 | WOLFSSL_API int wolfSSL_ASN1_item_i2d(const void *src, byte **dest, |
| 151 | const WOLFSSL_ASN1_ITEM *tpl); |
| 152 | |
| 153 | /* Need function declaration otherwise compiler complains */ |
| 154 | #define IMPLEMENT_ASN1_FUNCTIONS(type) \ |
| 155 | type *type##_new(void); \ |
| 156 | type *type##_new(void){ \ |
| 157 | return (type*)wolfSSL_ASN1_item_new(&type##_template_data); \ |
| 158 | } \ |
| 159 | void type##_free(type *t); \ |
| 160 | void type##_free(type *t){ \ |
| 161 | wolfSSL_ASN1_item_free(t, &type##_template_data); \ |
| 162 | } \ |
| 163 | int i2d_##type(type *src, byte **dest); \ |
| 164 | int i2d_##type(type *src, byte **dest) \ |
| 165 | { \ |
| 166 | return wolfSSL_ASN1_item_i2d(src, dest, &type##_template_data);\ |
| 167 | } |
| 168 | |
| 169 | #endif /* OPENSSL_ALL */ |
| 170 | |
| 171 | #define BN_to_ASN1_INTEGER wolfSSL_BN_to_ASN1_INTEGER |
| 172 | #define ASN1_TYPE_set wolfSSL_ASN1_TYPE_set |
| 173 | |
| 174 | #endif /* WOLFSSL_ASN1_H_ */ |