msasn1.h 11 KB
Newer Older
1 2
/*
 * Copyright (C) 2015 Austin English
3
 * Copyright (C) 2020 Vijay Kiran Kamuju
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
 */
#ifndef __MS_ASN1_H__
#define __MS_ASN1_H__

22 23
#include <pshpack8.h>

24 25 26 27 28 29 30 31 32 33 34 35
#ifdef __cplusplus
extern "C" {
#endif

typedef char ASN1char_t;
typedef signed char ASN1int8_t;
typedef unsigned char ASN1uint8_t;
typedef unsigned short ASN1uint16_t;
typedef signed short ASN1int16_t;
typedef ULONG ASN1uint32_t;
typedef LONG ASN1int32_t;

36 37 38
typedef ASN1uint16_t ASN1char16_t;
typedef ASN1uint32_t ASN1char32_t;

39 40 41 42 43 44 45 46 47 48
typedef ASN1char_t *ASN1ztcharstring_t;
typedef ASN1char16_t *ASN1ztchar16string_t;
typedef ASN1char32_t *ASN1ztchar32string_t;
typedef ASN1int32_t ASN1enum_t;
typedef ASN1uint8_t ASN1octet_t;
typedef ASN1uint8_t ASN1bool_t;
typedef ASN1uint16_t ASN1choice_t;
typedef ASN1uint32_t ASN1magic_t;
typedef ASN1ztcharstring_t ASN1objectdescriptor_t;

49 50 51 52 53
#define ASN1_MAKE_VERSION(major,minor) (((major) << 16) | (minor))
#define ASN1_THIS_VERSION ASN1_MAKE_VERSION(1,0)

#define ASN1DECFREE_NON_PDU_ID ((ASN1uint32_t)-1)

54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
typedef void (WINAPI *ASN1FreeFun_t)(void *data);
typedef void (WINAPI *ASN1GenericFun_t)(void);

typedef struct ASN1encoding_s *ASN1encoding_t;
typedef struct ASN1decoding_s *ASN1decoding_t;
typedef ASN1int32_t (WINAPI *ASN1PerEncFun_t)(ASN1encoding_t enc,void *data);
typedef ASN1int32_t (WINAPI *ASN1PerDecFun_t)(ASN1decoding_t enc,void *data);

typedef struct tagASN1PerFunArr_t {
  const ASN1PerEncFun_t *apfnEncoder;
  const ASN1PerDecFun_t *apfnDecoder;
} ASN1PerFunArr_t;

typedef ASN1int32_t (WINAPI *ASN1BerEncFun_t)(ASN1encoding_t enc,ASN1uint32_t tag,void *data);
typedef ASN1int32_t (WINAPI *ASN1BerDecFun_t)(ASN1decoding_t enc,ASN1uint32_t tag,void *data);

typedef struct tagASN1BerFunArr_t {
  const ASN1BerEncFun_t *apfnEncoder;
  const ASN1BerDecFun_t *apfnDecoder;
} ASN1BerFunArr_t;

75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
enum
{
    ASN1FLAGS_NONE     = 0x00000000,
    ASN1FLAGS_NOASSERT = 0x00001000
};

enum
{
    ASN1ENCODE_APPEND         = 0x00000001,
    ASN1ENCODE_REUSEBUFFER    = 0x00000004,
    ASN1ENCODE_SETBUFFER      = 0x00000008,
    ASN1ENCODE_ALLOCATEBUFFER = 0x00000010,
    ASN1ENCODE_NOASSERT       = ASN1FLAGS_NOASSERT
};

enum
{
    ASN1DECODE_APPENDED       = 0x00000001,
    ASN1DECODE_REWINDBUFFER   = 0x00000004,
    ASN1DECODE_SETBUFFER      = 0x00000008,
    ASN1DECODE_AUTOFREEBUFFER = 0x00000010,
    ASN1DECODE_NOASSERT       = ASN1FLAGS_NOASSERT
};

99 100 101 102 103 104
typedef struct tagASN1bitstring_t
{
  ASN1uint32_t length;
  ASN1octet_t *value;
} ASN1bitstring_t;

105 106 107 108 109
typedef enum tagASN1blocktype_e
{
  ASN1_DER_SET_OF_BLOCK,
} ASN1blocktype_e;

110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
typedef struct tagASN1charstring_t
{
  ASN1uint32_t length;
  ASN1char_t *value;
} ASN1charstring_t;

typedef struct tagASN1char16string_t
{
  ASN1uint32_t length;
  ASN1char16_t *value;
} ASN1char16string_t;

typedef struct tagASN1char32string_t
{
  ASN1uint32_t length;
  ASN1char32_t *value;
} ASN1char32string_t;

128 129 130 131 132 133
typedef struct tagASN1encodedOID_t
{
  ASN1uint16_t length;
  ASN1octet_t  *value;
} ASN1encodedOID_t;

134 135 136 137 138 139
typedef enum {
  ASN1_PER_RULE_ALIGNED = 0x0001,ASN1_PER_RULE_UNALIGNED = 0x0002,ASN1_PER_RULE = ASN1_PER_RULE_ALIGNED | ASN1_PER_RULE_UNALIGNED,
  ASN1_BER_RULE_BER = 0x0100,ASN1_BER_RULE_CER = 0x0200,ASN1_BER_RULE_DER = 0x0400,
  ASN1_BER_RULE = ASN1_BER_RULE_BER | ASN1_BER_RULE_CER | ASN1_BER_RULE_DER
} ASN1encodingrule_e;

140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162
typedef enum tagASN1error_e
{
  ASN1_SUCCESS = 0,
  ASN1_ERR_INTERNAL = -1001,
  ASN1_ERR_EOD = -1002,
  ASN1_ERR_CORRUPT = -1003,
  ASN1_ERR_LARGE = -1004,
  ASN1_ERR_CONSTRAINT = -1005,
  ASN1_ERR_MEMORY = -1006,
  ASN1_ERR_OVERFLOW = -1007,
  ASN1_ERR_BADPDU = -1008,
  ASN1_ERR_BADARGS = -1009,
  ASN1_ERR_BADREAL = -1010,
  ASN1_ERR_BADTAG = -1011,
  ASN1_ERR_CHOICE = -1012,
  ASN1_ERR_RULE = -1013,
  ASN1_ERR_UTF8 = -1014,
  ASN1_ERR_PDU_TYPE = -1051,
  ASN1_ERR_NYI = -1052,
  ASN1_WRN_EXTENDED = 1001,
  ASN1_WRN_NOEOD = 1002,
} ASN1error_e;

163 164 165 166 167 168 169
enum
{
  ASN1_CHOICE_BASE = 1,
  ASN1_CHOICE_INVALID = -1,
  ASN1_CHOICE_EXTENSION = 0
};

170 171 172
#define ASN1_SUCCEEDED(ret) (((int)(ret)) >= 0)
#define ASN1_FAILED(ret)    (((int)(ret)) < 0)

173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191
typedef struct tagASN1generalizedtime_t
{
  ASN1uint16_t year;
  ASN1uint8_t  month;
  ASN1uint8_t  day;
  ASN1uint8_t  hour;
  ASN1uint8_t  minute;
  ASN1uint8_t  second;
  ASN1uint16_t millisecond;
  ASN1bool_t   universal;
  ASN1int16_t  diff;
} ASN1generalizedtime_t;

typedef struct tagASN1intx_t
{
  ASN1uint32_t length;
  ASN1octet_t *value;
} ASN1intx_t;

192 193
typedef struct tagASN1module_t
{
194 195 196 197 198 199
  ASN1magic_t nModuleName;
  ASN1encodingrule_e eRule;
  ASN1uint32_t dwFlags;
  ASN1uint32_t cPDUs;
  const ASN1FreeFun_t *apfnFreeMemory;
  const ASN1uint32_t *acbStructSize;
200 201 202 203
  union
  {
      ASN1PerFunArr_t PER;
      ASN1BerFunArr_t BER;
204 205 206
  };
} *ASN1module_t;

207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237
struct ASN1encoding_s
{
  ASN1magic_t magic;
  ASN1uint32_t version;
  ASN1module_t module;
  ASN1octet_t *buf;
  ASN1uint32_t size;
  ASN1uint32_t len;
  ASN1error_e err;
  ASN1uint32_t bit;
  ASN1octet_t *pos;
  ASN1uint32_t cbExtraHeader;
  ASN1encodingrule_e eRule;
  ASN1uint32_t dwFlags;
};

struct ASN1decoding_s
{
  ASN1magic_t magic;
  ASN1uint32_t version;
  ASN1module_t module;
  ASN1octet_t *buf;
  ASN1uint32_t size;
  ASN1uint32_t len;
  ASN1error_e err;
  ASN1uint32_t bit;
  ASN1octet_t *pos;
  ASN1encodingrule_e eRule;
  ASN1uint32_t dwFlags;
};

238 239 240 241 242 243 244 245 246 247 248 249
typedef struct ASN1objectidentifier_s
{
  struct ASN1objectidentifier_s *next;
  ASN1uint32_t value;
} *ASN1objectidentifier_t;

typedef struct tagASN1objectidentifier2_t
{
  ASN1uint16_t count;
  ASN1uint32_t value[16];
} ASN1objectidentifier2_t;

250 251 252 253 254 255
typedef struct tagASN1octetstring_t
{
  ASN1uint32_t length;
  ASN1octet_t *value;
} ASN1octetstring_t;

256 257 258 259 260 261 262 263 264 265
typedef struct tagASN1open_t
{
  ASN1uint32_t    length;
  union
  {
    void  *encoded;
    void  *value;
  };
} ASN1open_t;

266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290
typedef enum tagASN1option_e
{
  ASN1OPT_CHANGE_RULE = 0x101,
  ASN1OPT_GET_RULE = 0x201,
  ASN1OPT_NOT_REUSE_BUFFER = 0x301,
  ASN1OPT_REWIND_BUFFER = 0x302,
  ASN1OPT_SET_DECODED_BUFFER = 0x501,
  ASN1OPT_DEL_DECODED_BUFFER = 0x502,
  ASN1OPT_GET_DECODED_BUFFER_SIZE = 0x601,
} ASN1option_e;

typedef struct tagASN1optionparam_t {
  ASN1option_e eOption;
  union
  {
    ASN1encodingrule_e eRule;
    ASN1uint32_t cbRequiredDecodedBufSize;
    struct
    {
      ASN1octet_t* pbBuf;
      ASN1uint32_t cbBufSize;
    } Buffer;
  };
} ASN1optionparam_t, ASN1optionparam_s;

291 292 293 294 295 296 297 298 299 300 301 302
typedef struct tagASN1utctime_t
{
  ASN1uint8_t year;
  ASN1uint8_t month;
  ASN1uint8_t day;
  ASN1uint8_t hour;
  ASN1uint8_t minute;
  ASN1uint8_t second;
  ASN1bool_t  universal;
  ASN1int16_t diff;
} ASN1utctime_t;

303 304 305 306 307 308
typedef struct tagASN1wstring_t
{
  ASN1uint32_t length;
  WCHAR *value;
} ASN1wstring_t;

309 310 311 312 313
void         WINAPI ASN1_CloseDecoder(ASN1decoding_t);
void         WINAPI ASN1_CloseEncoder(ASN1encoding_t);
void         WINAPI ASN1_CloseEncoder2(ASN1encoding_t);
void         WINAPI ASN1_CloseModule(ASN1module_t);
ASN1error_e  WINAPI ASN1_CreateDecoder(ASN1module_t, ASN1decoding_t*,ASN1octet_t*, ASN1uint32_t, ASN1decoding_t);
314
ASN1error_e  WINAPI ASN1_CreateDecoderEx(ASN1module_t, ASN1decoding_t*,ASN1octet_t*, ASN1uint32_t, ASN1decoding_t, ASN1uint32_t);
315 316 317 318 319 320 321 322 323 324
ASN1error_e  WINAPI ASN1_CreateEncoder(ASN1module_t, ASN1encoding_t*, ASN1octet_t*, ASN1uint32_t, ASN1encoding_t);
ASN1module_t WINAPI ASN1_CreateModule(ASN1uint32_t,ASN1encodingrule_e,ASN1uint32_t,ASN1uint32_t,const ASN1GenericFun_t [],const ASN1GenericFun_t [],const ASN1FreeFun_t [],const ASN1uint32_t [],ASN1magic_t);
ASN1error_e  WINAPI ASN1_Decode(ASN1decoding_t, void** , ASN1uint32_t, ASN1uint32_t, ASN1octet_t*, ASN1uint32_t);
ASN1error_e  WINAPI ASN1_Encode(ASN1encoding_t, void*, ASN1uint32_t, ASN1uint32_t, ASN1octet_t*, ASN1uint32_t);
void         WINAPI ASN1_FreeDecoded(ASN1decoding_t, void*, ASN1uint32_t);
void         WINAPI ASN1_FreeEncoded(ASN1encoding_t, void*);
ASN1error_e  WINAPI ASN1_GetDecoderOption(ASN1decoding_t, ASN1optionparam_t*);
ASN1error_e  WINAPI ASN1_GetEncoderOption(ASN1encoding_t, ASN1optionparam_t*);
ASN1error_e  WINAPI ASN1_SetDecoderOption(ASN1decoding_t, ASN1optionparam_t*);
ASN1error_e  WINAPI ASN1_SetEncoderOption(ASN1encoding_t, ASN1optionparam_t*);
325 326 327 328 329 330 331 332
int          WINAPI ASN1bitstring_cmp(ASN1bitstring_t*,ASN1bitstring_t*);
void         WINAPI ASN1bitstring_free(ASN1bitstring_t*);
int          WINAPI ASN1char16string_cmp(ASN1char16string_t*,ASN1char16string_t*);
void         WINAPI ASN1char16string_free(ASN1char16string_t*);
int          WINAPI ASN1char32string_cmp(ASN1char32string_t*,ASN1char32string_t*);
void         WINAPI ASN1char32string_free(ASN1char32string_t*);
int          WINAPI ASN1charstring_cmp(ASN1charstring_t*,ASN1charstring_t*);
void         WINAPI ASN1charstring_free(ASN1charstring_t*);
333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348
LPVOID       WINAPI ASN1DecAlloc(ASN1decoding_t,ASN1uint32_t);
LPVOID       WINAPI ASN1DecRealloc(ASN1decoding_t,LPVOID,ASN1uint32_t);
ASN1error_e  WINAPI ASN1DecSetError(ASN1decoding_t,ASN1error_e);
ASN1error_e  WINAPI ASN1EncSetError(ASN1encoding_t,ASN1error_e);
void         WINAPI ASN1Free(LPVOID);
int          WINAPI ASN1generalizedtime_cmp(ASN1generalizedtime_t*,ASN1generalizedtime_t*);
int          WINAPI ASN1intx_cmp(ASN1intx_t*,ASN1intx_t*);
void         WINAPI ASN1intx_free(ASN1intx_t*);
void         WINAPI ASN1intx_setuint32(ASN1intx_t*,ASN1uint32_t);
ASN1uint32_t WINAPI ASN1intx_uoctets(ASN1intx_t*);
ASN1int32_t  WINAPI ASN1intx2int32(ASN1intx_t*);
ASN1uint32_t WINAPI ASN1intx2uint32(ASN1intx_t*);
int          WINAPI ASN1intxisuint32(ASN1intx_t*);
int          WINAPI ASN1objectidentifier_cmp(ASN1objectidentifier_t*,ASN1objectidentifier_t*);
void         WINAPI ASN1objectidentifier_free(ASN1objectidentifier_t*);
int          WINAPI ASN1objectidentifier2_cmp(ASN1objectidentifier2_t*,ASN1objectidentifier2_t*);
349 350
int          WINAPI ASN1octetstring_cmp(ASN1octetstring_t*,ASN1octetstring_t*);
void         WINAPI ASN1octetstring_free(ASN1octetstring_t*);
351 352 353
int          WINAPI ASN1open_cmp(ASN1open_t*,ASN1open_t*);
void         WINAPI ASN1open_free(ASN1open_t*);
ASN1uint32_t WINAPI ASN1uint32_uoctets(ASN1uint32_t);
354
void         WINAPI ASN1utf8string_free(ASN1wstring_t*);
355
int          WINAPI ASN1utctime_cmp(ASN1utctime_t*,ASN1utctime_t*);
356 357 358 359 360
int          WINAPI ASN1ztchar16string_cmp(ASN1ztchar16string_t*,ASN1ztchar16string_t*);
void         WINAPI ASN1ztchar16string_free(ASN1ztchar16string_t*);
void         WINAPI ASN1ztchar32string_free(ASN1ztchar32string_t*);
int          WINAPI ASN1ztcharstring_cmp(ASN1ztcharstring_t*,ASN1ztcharstring_t*);
void         WINAPI ASN1ztcharstring_free(ASN1charstring_t*);
361

362 363 364 365
#ifdef __cplusplus
}
#endif

366 367
#include <poppack.h>

368
#endif /* __MS_ASN1_H__ */