asn1.h

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      * Definitions for Abstract Syntax Notation One, ASN.1
00013      * As defined in ISO/IS 8824 and ISO/IS 8825
00014      *
00015      *
00016      */
00017 /***********************************************************
00018         Copyright 1988, 1989 by Carnegie Mellon University
00019 
00020                       All Rights Reserved
00021 
00022 Permission to use, copy, modify, and distribute this software and its 
00023 documentation for any purpose and without fee is hereby granted, 
00024 provided that the above copyright notice appear in all copies and that
00025 both that copyright notice and this permission notice appear in 
00026 supporting documentation, and that the name of CMU not be
00027 used in advertising or publicity pertaining to distribution of the
00028 software without specific, written prior permission.  
00029 
00030 CMU DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
00031 ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
00032 CMU BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
00033 ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
00034 WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
00035 ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
00036 SOFTWARE.
00037 ******************************************************************/
00038 
00039 
00040 #ifndef MAX_SUBID               /* temporary - duplicate definition protection */
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 /* max subid's in an oid */
00052 #ifndef MAX_NAME_LEN            /* conflicts with some libraries */
00053 #define MAX_NAME_LEN        MAX_OID_LEN /* obsolete. use 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      * The BER inside an OPAQUE is an context specific with a value of 48 (0x30)
00094      * plus the "normal" tag. For a Counter64, the tag is 0x46 (i.e., an
00095      * applications specific tag with value 6). So the value for a 64 bit
00096      * counter is 0x46 + 0x30, or 0x76 (118 base 10). However, values
00097      * greater than 30 can not be encoded in one octet. So the first octet
00098      * has the class, in this case context specific (ASN_CONTEXT), and
00099      * the special value (i.e., 31) to indicate that the real value follows
00100      * in one or more octets. The high order bit of each following octet
00101      * indicates if the value is encoded in additional octets. A high order
00102      * bit of zero, indicates the last. For this "hack", only one octet
00103      * will be used for the value. 
00104      */
00105 
00106     /*
00107      * first octet of the tag 
00108      */
00109 #define ASN_OPAQUE_TAG1 (ASN_CONTEXT | ASN_EXTENSION_ID)
00110     /*
00111      * base value for the second octet of the tag - the
00112      * second octet was the value for the tag 
00113      */
00114 #define ASN_OPAQUE_TAG2 ((u_char)0x30)
00115 
00116 #define ASN_OPAQUE_TAG2U ((u_char)0x2f) /* second octet of tag for union */
00117 
00118     /*
00119      * All the ASN.1 types for SNMP "should have been" defined in this file,
00120      * but they were not. (They are defined in snmp_impl.h)  Thus, the tag for
00121      * Opaque and Counter64 is defined, again, here with a different names. 
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) /* or ASN_PRIV_UNION ? */
00130 
00131     /*
00132      * value for Counter64 
00133      */
00134 #define ASN_OPAQUE_COUNTER64 (ASN_OPAQUE_TAG2 + ASN_APP_COUNTER64)
00135     /*
00136      * max size of BER encoding of Counter64 
00137      */
00138 #define ASN_OPAQUE_COUNTER64_MX_BER_LEN 12
00139 
00140     /*
00141      * value for Float 
00142      */
00143 #define ASN_OPAQUE_FLOAT (ASN_OPAQUE_TAG2 + ASN_APP_FLOAT)
00144     /*
00145      * size of BER encoding of Float 
00146      */
00147 #define ASN_OPAQUE_FLOAT_BER_LEN 7
00148 
00149     /*
00150      * value for Double 
00151      */
00152 #define ASN_OPAQUE_DOUBLE (ASN_OPAQUE_TAG2 + ASN_APP_DOUBLE)
00153     /*
00154      * size of BER encoding of Double 
00155      */
00156 #define ASN_OPAQUE_DOUBLE_BER_LEN 11
00157 
00158     /*
00159      * value for Integer64 
00160      */
00161 #define ASN_OPAQUE_I64 (ASN_OPAQUE_TAG2 + ASN_APP_I64)
00162     /*
00163      * max size of BER encoding of Integer64 
00164      */
00165 #define ASN_OPAQUE_I64_MX_BER_LEN 11
00166 
00167     /*
00168      * value for Unsigned64 
00169      */
00170 #define ASN_OPAQUE_U64 (ASN_OPAQUE_TAG2 + ASN_APP_U64)
00171     /*
00172      * max size of BER encoding of Unsigned64 
00173      */
00174 #define ASN_OPAQUE_U64_MX_BER_LEN 12
00175 
00176 #endif                          /* OPAQUE_SPECIAL_TYPES */
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)       /* 4 */
00183 #define ASN_PRIV_IMPLIED_OBJECT_ID  (ASN_PRIVATE | ASN_OBJECT_ID)       /* 6 */
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,      /* must be this type */
00203                                        const char *estr);       /* error message prefix */
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      * Re-allocator function for below.  
00239      */
00240 
00241     int             asn_realloc(u_char **, size_t *);
00242 
00243     /*
00244      * Re-allocating reverse ASN.1 encoder functions.  Synopsis:
00245      * 
00246      * u_char *buf = (u_char*)malloc(100);
00247      * u_char type = (ASN_UNIVERSAL | ASN_PRIMITIVE | ASN_INTEGER);
00248      * size_t buf_len = 100, offset = 0;
00249      * long data = 12345;
00250      * int allow_realloc = 1;
00251      * 
00252      * if (asn_realloc_rbuild_int(&buf, &buf_len, &offset, allow_realloc,
00253      * type, &data, sizeof(long)) == 0) {
00254      * error;
00255      * }
00256      * 
00257      * NOTE WELL: after calling one of these functions with allow_realloc
00258      * non-zero, buf might have moved, buf_len might have grown and
00259      * offset will have increased by the size of the encoded data.
00260      * You should **NEVER** do something like this:
00261      * 
00262      * u_char *buf = (u_char *)malloc(100), *ptr;
00263      * u_char type = (ASN_UNIVERSAL | ASN_PRIMITIVE | ASN_INTEGER);
00264      * size_t buf_len = 100, offset = 0;
00265      * long data1 = 1234, data2 = 5678;
00266      * int rc = 0, allow_realloc = 1;
00267      * 
00268      * rc  = asn_realloc_rbuild_int(&buf, &buf_len, &offset, allow_realloc,
00269      * type, &data1, sizeof(long));
00270      * ptr = buf[buf_len - offset];   / * points at encoding of data1 * /
00271      * if (rc == 0) {
00272      * error;
00273      * }
00274      * rc  = asn_realloc_rbuild_int(&buf, &buf_len, &offset, allow_realloc,
00275      * type, &data2, sizeof(long));
00276      * make use of ptr here;
00277      * 
00278      * 
00279      * ptr is **INVALID** at this point.  In general, you should store the
00280      * offset value and compute pointers when you need them:
00281      * 
00282      * 
00283      * 
00284      * u_char *buf = (u_char *)malloc(100), *ptr;
00285      * u_char type = (ASN_UNIVERSAL | ASN_PRIMITIVE | ASN_INTEGER);
00286      * size_t buf_len = 100, offset = 0, ptr_offset;
00287      * long data1 = 1234, data2 = 5678;
00288      * int rc = 0, allow_realloc = 1;
00289      * 
00290      * rc  = asn_realloc_rbuild_int(&buf, &buf_len, &offset, allow_realloc,
00291      * type, &data1, sizeof(long));
00292      * ptr_offset = offset;
00293      * if (rc == 0) {
00294      * error;
00295      * }
00296      * rc  = asn_realloc_rbuild_int(&buf, &buf_len, &offset, allow_realloc,
00297      * type, &data2, sizeof(long));
00298      * ptr = buf + buf_len - ptr_offset
00299      * make use of ptr here;
00300      * 
00301      * 
00302      * 
00303      * Here, you can see that ptr will be a valid pointer even if the block of
00304      * memory has been moved, as it may well have been.  Plenty of examples of
00305      * usage all over asn1.c, snmp_api.c, snmpusm.c.
00306      * 
00307      * The other thing you should **NEVER** do is to pass a pointer to a buffer
00308      * on the stack as the first argument when allow_realloc is non-zero, unless
00309      * you really know what you are doing and your machine/compiler allows you to
00310      * free non-heap memory.  There are rumours that such things exist, but many
00311      * consider them no more than the wild tales of a fool.
00312      * 
00313      * Of course, you can pass allow_realloc as zero, to indicate that you do not
00314      * wish the packet buffer to be reallocated for some reason; perhaps because
00315      * it is on the stack.  This may be useful to emulate the functionality of
00316      * the old API:
00317      * 
00318      * u_char my_static_buffer[100], *cp = NULL;
00319      * size_t my_static_buffer_len = 100;
00320      * float my_pi = (float)22/(float)7;
00321      * 
00322      * cp = asn_rbuild_float(my_static_buffer, &my_static_buffer_len,
00323      * ASN_OPAQUE_FLOAT, &my_pi, sizeof(float));
00324      * if (cp == NULL) {
00325      * error;
00326      * }
00327      * 
00328      * 
00329      * IS EQUIVALENT TO:
00330      * 
00331      * 
00332      * u_char my_static_buffer[100];
00333      * size_t my_static_buffer_len = 100, my_offset = 0;
00334      * float my_pi = (float)22/(float)7;
00335      * int rc = 0;
00336      * 
00337      * rc = asn_realloc_rbuild_float(&my_static_buffer, &my_static_buffer_len,
00338      * &my_offset, 0,
00339      * ASN_OPAQUE_FLOAT, &my_pi, sizeof(float));
00340      * if (rc == 0) {
00341      * error;
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                          /* ASN1_H */

net-snmpに対してSat Sep 5 13:14:20 2009に生成されました。  doxygen 1.4.7