00001 #ifndef ASN1_H
00002 #define ASN1_H
00003
00004 #ifdef __cplusplus
00005 extern "C" {
00006 #endif
00007
00008 #define PARSE_PACKET 0
00009 #define DUMP_PACKET 1
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040 #ifndef MAX_SUBID
00041 #ifndef EIGHTBIT_SUBIDS
00042 typedef u_long oid;
00043 #define MAX_SUBID 0xFFFFFFFF
00044 #else
00045 typedef u_char oid;
00046 #define MAX_SUBID 0xFF
00047 #endif
00048 #endif
00049
00050 #define MIN_OID_LEN 2
00051 #define MAX_OID_LEN 128
00052 #ifndef MAX_NAME_LEN
00053 #define MAX_NAME_LEN MAX_OID_LEN
00054 #endif
00055
00056 #define OID_LENGTH(x) (sizeof(x)/sizeof(oid))
00057
00058
00059 #define ASN_BOOLEAN ((u_char)0x01)
00060 #define ASN_INTEGER ((u_char)0x02)
00061 #define ASN_BIT_STR ((u_char)0x03)
00062 #define ASN_OCTET_STR ((u_char)0x04)
00063 #define ASN_NULL ((u_char)0x05)
00064 #define ASN_OBJECT_ID ((u_char)0x06)
00065 #define ASN_SEQUENCE ((u_char)0x10)
00066 #define ASN_SET ((u_char)0x11)
00067
00068 #define ASN_UNIVERSAL ((u_char)0x00)
00069 #define ASN_APPLICATION ((u_char)0x40)
00070 #define ASN_CONTEXT ((u_char)0x80)
00071 #define ASN_PRIVATE ((u_char)0xC0)
00072
00073 #define ASN_PRIMITIVE ((u_char)0x00)
00074 #define ASN_CONSTRUCTOR ((u_char)0x20)
00075
00076 #define ASN_LONG_LEN (0x80)
00077 #define ASN_EXTENSION_ID (0x1F)
00078 #define ASN_BIT8 (0x80)
00079
00080 #define IS_CONSTRUCTOR(byte) ((byte) & ASN_CONSTRUCTOR)
00081 #define IS_EXTENSION_ID(byte) (((byte) & ASN_EXTENSION_ID) == ASN_EXTENSION_ID)
00082
00083 struct counter64 {
00084 u_long high;
00085 u_long low;
00086 };
00087
00088 #ifdef OPAQUE_SPECIAL_TYPES
00089 typedef struct counter64 integer64;
00090 typedef struct counter64 unsigned64;
00091
00092
00093
00094
00095
00096
00097
00098
00099
00100
00101
00102
00103
00104
00105
00106
00107
00108
00109 #define ASN_OPAQUE_TAG1 (ASN_CONTEXT | ASN_EXTENSION_ID)
00110
00111
00112
00113
00114 #define ASN_OPAQUE_TAG2 ((u_char)0x30)
00115
00116 #define ASN_OPAQUE_TAG2U ((u_char)0x2f)
00117
00118
00119
00120
00121
00122
00123 #define ASN_APP_OPAQUE (ASN_APPLICATION | 4)
00124 #define ASN_APP_COUNTER64 (ASN_APPLICATION | 6)
00125 #define ASN_APP_FLOAT (ASN_APPLICATION | 8)
00126 #define ASN_APP_DOUBLE (ASN_APPLICATION | 9)
00127 #define ASN_APP_I64 (ASN_APPLICATION | 10)
00128 #define ASN_APP_U64 (ASN_APPLICATION | 11)
00129 #define ASN_APP_UNION (ASN_PRIVATE | 1)
00130
00131
00132
00133
00134 #define ASN_OPAQUE_COUNTER64 (ASN_OPAQUE_TAG2 + ASN_APP_COUNTER64)
00135
00136
00137
00138 #define ASN_OPAQUE_COUNTER64_MX_BER_LEN 12
00139
00140
00141
00142
00143 #define ASN_OPAQUE_FLOAT (ASN_OPAQUE_TAG2 + ASN_APP_FLOAT)
00144
00145
00146
00147 #define ASN_OPAQUE_FLOAT_BER_LEN 7
00148
00149
00150
00151
00152 #define ASN_OPAQUE_DOUBLE (ASN_OPAQUE_TAG2 + ASN_APP_DOUBLE)
00153
00154
00155
00156 #define ASN_OPAQUE_DOUBLE_BER_LEN 11
00157
00158
00159
00160
00161 #define ASN_OPAQUE_I64 (ASN_OPAQUE_TAG2 + ASN_APP_I64)
00162
00163
00164
00165 #define ASN_OPAQUE_I64_MX_BER_LEN 11
00166
00167
00168
00169
00170 #define ASN_OPAQUE_U64 (ASN_OPAQUE_TAG2 + ASN_APP_U64)
00171
00172
00173
00174 #define ASN_OPAQUE_U64_MX_BER_LEN 12
00175
00176 #endif
00177
00178
00179 #define ASN_PRIV_INCL_RANGE (ASN_PRIVATE | 2)
00180 #define ASN_PRIV_EXCL_RANGE (ASN_PRIVATE | 3)
00181 #define ASN_PRIV_DELEGATED (ASN_PRIVATE | 5)
00182 #define ASN_PRIV_IMPLIED_OCTET_STR (ASN_PRIVATE | ASN_OCTET_STR)
00183 #define ASN_PRIV_IMPLIED_OBJECT_ID (ASN_PRIVATE | ASN_OBJECT_ID)
00184 #define ASN_PRIV_RETRY (ASN_PRIVATE | 7)
00185 #define IS_DELEGATED(x) ((x) == ASN_PRIV_DELEGATED)
00186
00187
00188 int asn_check_packet(u_char *, size_t);
00189 u_char *asn_parse_int(u_char *, size_t *, u_char *, long *,
00190 size_t);
00191 u_char *asn_build_int(u_char *, size_t *, u_char, const long *,
00192 size_t);
00193 u_char *asn_parse_unsigned_int(u_char *, size_t *, u_char *,
00194 u_long *, size_t);
00195 u_char *asn_build_unsigned_int(u_char *, size_t *, u_char,
00196 const u_long *, size_t);
00197 u_char *asn_parse_string(u_char *, size_t *, u_char *,
00198 u_char *, size_t *);
00199 u_char *asn_build_string(u_char *, size_t *, u_char,
00200 const u_char *, size_t);
00201 u_char *asn_parse_header(u_char *, size_t *, u_char *);
00202 u_char *asn_parse_sequence(u_char *, size_t *, u_char *, u_char expected_type,
00203 const char *estr);
00204 u_char *asn_build_header(u_char *, size_t *, u_char, size_t);
00205 u_char *asn_build_sequence(u_char *, size_t *, u_char, size_t);
00206 u_char *asn_parse_length(u_char *, u_long *);
00207 u_char *asn_build_length(u_char *, size_t *, size_t);
00208 u_char *asn_parse_objid(u_char *, size_t *, u_char *, oid *,
00209 size_t *);
00210 u_char *asn_build_objid(u_char *, size_t *, u_char, oid *,
00211 size_t);
00212 u_char *asn_parse_null(u_char *, size_t *, u_char *);
00213 u_char *asn_build_null(u_char *, size_t *, u_char);
00214 u_char *asn_parse_bitstring(u_char *, size_t *, u_char *,
00215 u_char *, size_t *);
00216 u_char *asn_build_bitstring(u_char *, size_t *, u_char,
00217 const u_char *, size_t);
00218 u_char *asn_parse_unsigned_int64(u_char *, size_t *, u_char *,
00219 struct counter64 *, size_t);
00220 u_char *asn_build_unsigned_int64(u_char *, size_t *, u_char,
00221 const struct counter64 *, size_t);
00222 u_char *asn_parse_signed_int64(u_char *, size_t *, u_char *,
00223 struct counter64 *, size_t);
00224 u_char *asn_build_signed_int64(u_char *, size_t *, u_char,
00225 const struct counter64 *, size_t);
00226 u_char *asn_build_float(u_char *, size_t *, u_char, const float *,
00227 size_t);
00228 u_char *asn_parse_float(u_char *, size_t *, u_char *, float *,
00229 size_t);
00230 u_char *asn_build_double(u_char *, size_t *, u_char, const double *,
00231 size_t);
00232 u_char *asn_parse_double(u_char *, size_t *, u_char *,
00233 double *, size_t);
00234
00235 #ifdef USE_REVERSE_ASNENCODING
00236
00237
00238
00239
00240
00241 int asn_realloc(u_char **, size_t *);
00242
00243
00244
00245
00246
00247
00248
00249
00250
00251
00252
00253
00254
00255
00256
00257
00258
00259
00260
00261
00262
00263
00264
00265
00266
00267
00268
00269
00270
00271
00272
00273
00274
00275
00276
00277
00278
00279
00280
00281
00282
00283
00284
00285
00286
00287
00288
00289
00290
00291
00292
00293
00294
00295
00296
00297
00298
00299
00300
00301
00302
00303
00304
00305
00306
00307
00308
00309
00310
00311
00312
00313
00314
00315
00316
00317
00318
00319
00320
00321
00322
00323
00324
00325
00326
00327
00328
00329
00330
00331
00332
00333
00334
00335
00336
00337
00338
00339
00340
00341
00342
00343
00344
00345
00346
00347
00348 int asn_realloc_rbuild_int(u_char ** pkt, size_t * pkt_len,
00349 size_t * offset,
00350 int allow_realloc, u_char type,
00351 const long *data, size_t data_size);
00352
00353 int asn_realloc_rbuild_string(u_char ** pkt,
00354 size_t * pkt_len,
00355 size_t * offset,
00356 int allow_realloc,
00357 u_char type,
00358 const u_char * data,
00359 size_t data_size);
00360
00361 int asn_realloc_rbuild_unsigned_int(u_char ** pkt,
00362 size_t * pkt_len,
00363 size_t * offset,
00364 int allow_realloc,
00365 u_char type,
00366 const u_long * data,
00367 size_t data_size);
00368
00369 int asn_realloc_rbuild_header(u_char ** pkt,
00370 size_t * pkt_len,
00371 size_t * offset,
00372 int allow_realloc,
00373 u_char type,
00374 size_t data_size);
00375
00376 int asn_realloc_rbuild_sequence(u_char ** pkt,
00377 size_t * pkt_len,
00378 size_t * offset,
00379 int allow_realloc,
00380 u_char type,
00381 size_t data_size);
00382
00383 int asn_realloc_rbuild_length(u_char ** pkt,
00384 size_t * pkt_len,
00385 size_t * offset,
00386 int allow_realloc,
00387 size_t data_size);
00388
00389 int asn_realloc_rbuild_objid(u_char ** pkt,
00390 size_t * pkt_len,
00391 size_t * offset,
00392 int allow_realloc,
00393 u_char type, const oid *,
00394 size_t);
00395
00396 int asn_realloc_rbuild_null(u_char ** pkt,
00397 size_t * pkt_len,
00398 size_t * offset,
00399 int allow_realloc,
00400 u_char type);
00401
00402 int asn_realloc_rbuild_bitstring(u_char ** pkt,
00403 size_t * pkt_len,
00404 size_t * offset,
00405 int allow_realloc,
00406 u_char type,
00407 const u_char * data,
00408 size_t data_size);
00409
00410 int asn_realloc_rbuild_unsigned_int64(u_char ** pkt,
00411 size_t * pkt_len,
00412 size_t * offset,
00413 int allow_realloc,
00414 u_char type,
00415 struct counter64
00416 const *data, size_t);
00417
00418 int asn_realloc_rbuild_signed_int64(u_char ** pkt,
00419 size_t * pkt_len,
00420 size_t * offset,
00421 int allow_realloc,
00422 u_char type,
00423 const struct counter64 *data,
00424 size_t);
00425
00426 int asn_realloc_rbuild_float(u_char ** pkt,
00427 size_t * pkt_len,
00428 size_t * offset,
00429 int allow_realloc,
00430 u_char type, const float *data,
00431 size_t data_size);
00432
00433 int asn_realloc_rbuild_double(u_char ** pkt,
00434 size_t * pkt_len,
00435 size_t * offset,
00436 int allow_realloc,
00437 u_char type, const double *data,
00438 size_t data_size);
00439 #endif
00440
00441 #ifdef __cplusplus
00442 }
00443 #endif
00444 #endif