net-snmp 5.7
asn1.h
00001 #ifndef ASN1_H
00002 #define ASN1_H
00003 
00004 #include <net-snmp/library/oid.h>
00005 
00006 #ifdef __cplusplus
00007 extern          "C" {
00008 #endif
00009 
00010 #define PARSE_PACKET    0
00011 #define DUMP_PACKET     1
00012 
00013     /*
00014      * Definitions for Abstract Syntax Notation One, ASN.1
00015      * As defined in ISO/IS 8824 and ISO/IS 8825
00016      *
00017      *
00018      */
00019 /***********************************************************
00020         Copyright 1988, 1989 by Carnegie Mellon University
00021 
00022                       All Rights Reserved
00023 
00024 Permission to use, copy, modify, and distribute this software and its 
00025 documentation for any purpose and without fee is hereby granted, 
00026 provided that the above copyright notice appear in all copies and that
00027 both that copyright notice and this permission notice appear in 
00028 supporting documentation, and that the name of CMU not be
00029 used in advertising or publicity pertaining to distribution of the
00030 software without specific, written prior permission.  
00031 
00032 CMU DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
00033 ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
00034 CMU BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
00035 ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
00036 WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
00037 ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
00038 SOFTWARE.
00039 ******************************************************************/
00040 
00041 
00042 #define MIN_OID_LEN         2
00043 #define MAX_OID_LEN         128 /* max subid's in an oid */
00044 #ifndef MAX_NAME_LEN            /* conflicts with some libraries */
00045 #define MAX_NAME_LEN        MAX_OID_LEN /* obsolete. use MAX_OID_LEN */
00046 #endif
00047 
00048 #define OID_LENGTH(x)  (sizeof(x)/sizeof(oid))
00049 
00050 
00051 #define ASN_BOOLEAN         ((u_char)0x01)
00052 #define ASN_INTEGER         ((u_char)0x02)
00053 #define ASN_BIT_STR         ((u_char)0x03)
00054 #define ASN_OCTET_STR       ((u_char)0x04)
00055 #define ASN_NULL            ((u_char)0x05)
00056 #define ASN_OBJECT_ID       ((u_char)0x06)
00057 #define ASN_SEQUENCE        ((u_char)0x10)
00058 #define ASN_SET             ((u_char)0x11)
00059 
00060 #define ASN_UNIVERSAL       ((u_char)0x00)
00061 #define ASN_APPLICATION     ((u_char)0x40)
00062 #define ASN_CONTEXT         ((u_char)0x80)
00063 #define ASN_PRIVATE         ((u_char)0xC0)
00064 
00065 #define ASN_PRIMITIVE       ((u_char)0x00)
00066 #define ASN_CONSTRUCTOR     ((u_char)0x20)
00067 
00068 #define ASN_LONG_LEN        (0x80)
00069 #define ASN_EXTENSION_ID    (0x1F)
00070 #define ASN_BIT8            (0x80)
00071 
00072 #define IS_CONSTRUCTOR(byte)    ((byte) & ASN_CONSTRUCTOR)
00073 #define IS_EXTENSION_ID(byte)   (((byte) & ASN_EXTENSION_ID) == ASN_EXTENSION_ID)
00074 
00075     struct counter64 {
00076         u_long          high;
00077         u_long          low;
00078     };
00079 
00080 #ifdef NETSNMP_WITH_OPAQUE_SPECIAL_TYPES
00081     typedef struct counter64 integer64;
00082     typedef struct counter64 unsigned64;
00083 
00084     /*
00085      * The BER inside an OPAQUE is an context specific with a value of 48 (0x30)
00086      * plus the "normal" tag. For a Counter64, the tag is 0x46 (i.e., an
00087      * applications specific tag with value 6). So the value for a 64 bit
00088      * counter is 0x46 + 0x30, or 0x76 (118 base 10). However, values
00089      * greater than 30 can not be encoded in one octet. So the first octet
00090      * has the class, in this case context specific (ASN_CONTEXT), and
00091      * the special value (i.e., 31) to indicate that the real value follows
00092      * in one or more octets. The high order bit of each following octet
00093      * indicates if the value is encoded in additional octets. A high order
00094      * bit of zero, indicates the last. For this "hack", only one octet
00095      * will be used for the value. 
00096      */
00097 
00098     /*
00099      * first octet of the tag 
00100      */
00101 #define ASN_OPAQUE_TAG1 (ASN_CONTEXT | ASN_EXTENSION_ID)
00102     /*
00103      * base value for the second octet of the tag - the
00104      * second octet was the value for the tag 
00105      */
00106 #define ASN_OPAQUE_TAG2 ((u_char)0x30)
00107 
00108 #define ASN_OPAQUE_TAG2U ((u_char)0x2f) /* second octet of tag for union */
00109 
00110     /*
00111      * All the ASN.1 types for SNMP "should have been" defined in this file,
00112      * but they were not. (They are defined in snmp_impl.h)  Thus, the tag for
00113      * Opaque and Counter64 is defined, again, here with a different names. 
00114      */
00115 #define ASN_APP_OPAQUE (ASN_APPLICATION | 4)
00116 #define ASN_APP_COUNTER64 (ASN_APPLICATION | 6)
00117 #define ASN_APP_FLOAT (ASN_APPLICATION | 8)
00118 #define ASN_APP_DOUBLE (ASN_APPLICATION | 9)
00119 #define ASN_APP_I64 (ASN_APPLICATION | 10)
00120 #define ASN_APP_U64 (ASN_APPLICATION | 11)
00121 #define ASN_APP_UNION (ASN_PRIVATE | 1) /* or ASN_PRIV_UNION ? */
00122 
00123     /*
00124      * value for Counter64 
00125      */
00126 #define ASN_OPAQUE_COUNTER64 (ASN_OPAQUE_TAG2 + ASN_APP_COUNTER64)
00127     /*
00128      * max size of BER encoding of Counter64 
00129      */
00130 #define ASN_OPAQUE_COUNTER64_MX_BER_LEN 12
00131 
00132     /*
00133      * value for Float 
00134      */
00135 #define ASN_OPAQUE_FLOAT (ASN_OPAQUE_TAG2 + ASN_APP_FLOAT)
00136     /*
00137      * size of BER encoding of Float 
00138      */
00139 #define ASN_OPAQUE_FLOAT_BER_LEN 7
00140 
00141     /*
00142      * value for Double 
00143      */
00144 #define ASN_OPAQUE_DOUBLE (ASN_OPAQUE_TAG2 + ASN_APP_DOUBLE)
00145     /*
00146      * size of BER encoding of Double 
00147      */
00148 #define ASN_OPAQUE_DOUBLE_BER_LEN 11
00149 
00150     /*
00151      * value for Integer64 
00152      */
00153 #define ASN_OPAQUE_I64 (ASN_OPAQUE_TAG2 + ASN_APP_I64)
00154     /*
00155      * max size of BER encoding of Integer64 
00156      */
00157 #define ASN_OPAQUE_I64_MX_BER_LEN 11
00158 
00159     /*
00160      * value for Unsigned64 
00161      */
00162 #define ASN_OPAQUE_U64 (ASN_OPAQUE_TAG2 + ASN_APP_U64)
00163     /*
00164      * max size of BER encoding of Unsigned64 
00165      */
00166 #define ASN_OPAQUE_U64_MX_BER_LEN 12
00167 
00168 #endif                          /* NETSNMP_WITH_OPAQUE_SPECIAL_TYPES */
00169 
00170 
00171 #define ASN_PRIV_INCL_RANGE (ASN_PRIVATE | 2)
00172 #define ASN_PRIV_EXCL_RANGE (ASN_PRIVATE | 3)
00173 #define ASN_PRIV_DELEGATED  (ASN_PRIVATE | 5)
00174 #define ASN_PRIV_IMPLIED_OCTET_STR  (ASN_PRIVATE | ASN_OCTET_STR)       /* 4 */
00175 #define ASN_PRIV_IMPLIED_OBJECT_ID  (ASN_PRIVATE | ASN_OBJECT_ID)       /* 6 */
00176 #define ASN_PRIV_RETRY      (ASN_PRIVATE | 7)   /* 199 */
00177 #define IS_DELEGATED(x)   ((x) == ASN_PRIV_DELEGATED)
00178 
00179 
00180     int             asn_check_packet(u_char *, size_t);
00181     NETSNMP_IMPORT
00182     u_char         *asn_parse_int(u_char *, size_t *, u_char *, long *,
00183                                   size_t);
00184     NETSNMP_IMPORT
00185     u_char         *asn_build_int(u_char *, size_t *, u_char, const long *,
00186                                   size_t);
00187     NETSNMP_IMPORT
00188     u_char         *asn_parse_unsigned_int(u_char *, size_t *, u_char *,
00189                                            u_long *, size_t);
00190     NETSNMP_IMPORT
00191     u_char         *asn_build_unsigned_int(u_char *, size_t *, u_char,
00192                                            const u_long *, size_t);
00193     NETSNMP_IMPORT
00194     u_char         *asn_parse_string(u_char *, size_t *, u_char *,
00195                                      u_char *, size_t *);
00196     NETSNMP_IMPORT
00197     u_char         *asn_build_string(u_char *, size_t *, u_char,
00198                                      const u_char *, size_t);
00199     NETSNMP_IMPORT
00200     u_char         *asn_parse_header(u_char *, size_t *, u_char *);
00201     u_char         *asn_parse_sequence(u_char *, size_t *, u_char *, u_char expected_type,      /* must be this type */
00202                                        const char *estr);       /* error message prefix */
00203     NETSNMP_IMPORT
00204     u_char         *asn_build_header(u_char *, size_t *, u_char, size_t);
00205     NETSNMP_IMPORT
00206     u_char         *asn_build_sequence(u_char *, size_t *, u_char, size_t);
00207     NETSNMP_IMPORT
00208     u_char         *asn_parse_length(u_char *, u_long *);
00209     NETSNMP_IMPORT
00210     u_char         *asn_build_length(u_char *, size_t *, size_t);
00211     NETSNMP_IMPORT
00212     u_char         *asn_parse_objid(u_char *, size_t *, u_char *, oid *,
00213                                     size_t *);
00214     NETSNMP_IMPORT
00215     u_char         *asn_build_objid(u_char *, size_t *, u_char, oid *,
00216                                     size_t);
00217     NETSNMP_IMPORT
00218     u_char         *asn_parse_null(u_char *, size_t *, u_char *);
00219     NETSNMP_IMPORT
00220     u_char         *asn_build_null(u_char *, size_t *, u_char);
00221     NETSNMP_IMPORT
00222     u_char         *asn_parse_bitstring(u_char *, size_t *, u_char *,
00223                                         u_char *, size_t *);
00224     NETSNMP_IMPORT
00225     u_char         *asn_build_bitstring(u_char *, size_t *, u_char,
00226                                         const u_char *, size_t);
00227     NETSNMP_IMPORT
00228     u_char         *asn_parse_unsigned_int64(u_char *, size_t *, u_char *,
00229                                              struct counter64 *, size_t);
00230     NETSNMP_IMPORT
00231     u_char         *asn_build_unsigned_int64(u_char *, size_t *, u_char,
00232                                              const struct counter64 *, size_t);
00233     u_char         *asn_parse_signed_int64(u_char *, size_t *, u_char *,
00234                                            struct counter64 *, size_t);
00235     u_char         *asn_build_signed_int64(u_char *, size_t *, u_char,
00236                                            const struct counter64 *, size_t);
00237     u_char         *asn_build_float(u_char *, size_t *, u_char, const float *,
00238                                     size_t);
00239     u_char         *asn_parse_float(u_char *, size_t *, u_char *, float *,
00240                                     size_t);
00241     u_char         *asn_build_double(u_char *, size_t *, u_char, const double *,
00242                                      size_t);
00243     u_char         *asn_parse_double(u_char *, size_t *, u_char *,
00244                                      double *, size_t);
00245 
00246 #ifdef NETSNMP_USE_REVERSE_ASNENCODING
00247 
00248     /*
00249      * Re-allocator function for below.  
00250      */
00251 
00252     int             asn_realloc(u_char **, size_t *);
00253 
00254     /*
00255      * Re-allocating reverse ASN.1 encoder functions.  Synopsis:
00256      * 
00257      * u_char *buf = (u_char*)malloc(100);
00258      * u_char type = (ASN_UNIVERSAL | ASN_PRIMITIVE | ASN_INTEGER);
00259      * size_t buf_len = 100, offset = 0;
00260      * long data = 12345;
00261      * int allow_realloc = 1;
00262      * 
00263      * if (asn_realloc_rbuild_int(&buf, &buf_len, &offset, allow_realloc,
00264      * type, &data, sizeof(long)) == 0) {
00265      * error;
00266      * }
00267      * 
00268      * NOTE WELL: after calling one of these functions with allow_realloc
00269      * non-zero, buf might have moved, buf_len might have grown and
00270      * offset will have increased by the size of the encoded data.
00271      * You should **NEVER** do something like this:
00272      * 
00273      * u_char *buf = (u_char *)malloc(100), *ptr;
00274      * u_char type = (ASN_UNIVERSAL | ASN_PRIMITIVE | ASN_INTEGER);
00275      * size_t buf_len = 100, offset = 0;
00276      * long data1 = 1234, data2 = 5678;
00277      * int rc = 0, allow_realloc = 1;
00278      * 
00279      * rc  = asn_realloc_rbuild_int(&buf, &buf_len, &offset, allow_realloc,
00280      * type, &data1, sizeof(long));
00281      * ptr = buf[buf_len - offset];   / * points at encoding of data1 * /
00282      * if (rc == 0) {
00283      * error;
00284      * }
00285      * rc  = asn_realloc_rbuild_int(&buf, &buf_len, &offset, allow_realloc,
00286      * type, &data2, sizeof(long));
00287      * make use of ptr here;
00288      * 
00289      * 
00290      * ptr is **INVALID** at this point.  In general, you should store the
00291      * offset value and compute pointers when you need them:
00292      * 
00293      * 
00294      * 
00295      * u_char *buf = (u_char *)malloc(100), *ptr;
00296      * u_char type = (ASN_UNIVERSAL | ASN_PRIMITIVE | ASN_INTEGER);
00297      * size_t buf_len = 100, offset = 0, ptr_offset;
00298      * long data1 = 1234, data2 = 5678;
00299      * int rc = 0, allow_realloc = 1;
00300      * 
00301      * rc  = asn_realloc_rbuild_int(&buf, &buf_len, &offset, allow_realloc,
00302      * type, &data1, sizeof(long));
00303      * ptr_offset = offset;
00304      * if (rc == 0) {
00305      * error;
00306      * }
00307      * rc  = asn_realloc_rbuild_int(&buf, &buf_len, &offset, allow_realloc,
00308      * type, &data2, sizeof(long));
00309      * ptr = buf + buf_len - ptr_offset
00310      * make use of ptr here;
00311      * 
00312      * 
00313      * 
00314      * Here, you can see that ptr will be a valid pointer even if the block of
00315      * memory has been moved, as it may well have been.  Plenty of examples of
00316      * usage all over asn1.c, snmp_api.c, snmpusm.c.
00317      * 
00318      * The other thing you should **NEVER** do is to pass a pointer to a buffer
00319      * on the stack as the first argument when allow_realloc is non-zero, unless
00320      * you really know what you are doing and your machine/compiler allows you to
00321      * free non-heap memory.  There are rumours that such things exist, but many
00322      * consider them no more than the wild tales of a fool.
00323      * 
00324      * Of course, you can pass allow_realloc as zero, to indicate that you do not
00325      * wish the packet buffer to be reallocated for some reason; perhaps because
00326      * it is on the stack.  This may be useful to emulate the functionality of
00327      * the old API:
00328      * 
00329      * u_char my_static_buffer[100], *cp = NULL;
00330      * size_t my_static_buffer_len = 100;
00331      * float my_pi = (float)22/(float)7;
00332      * 
00333      * cp = asn_rbuild_float(my_static_buffer, &my_static_buffer_len,
00334      * ASN_OPAQUE_FLOAT, &my_pi, sizeof(float));
00335      * if (cp == NULL) {
00336      * error;
00337      * }
00338      * 
00339      * 
00340      * IS EQUIVALENT TO:
00341      * 
00342      * 
00343      * u_char my_static_buffer[100];
00344      * size_t my_static_buffer_len = 100, my_offset = 0;
00345      * float my_pi = (float)22/(float)7;
00346      * int rc = 0;
00347      * 
00348      * rc = asn_realloc_rbuild_float(&my_static_buffer, &my_static_buffer_len,
00349      * &my_offset, 0,
00350      * ASN_OPAQUE_FLOAT, &my_pi, sizeof(float));
00351      * if (rc == 0) {
00352      * error;
00353      * }
00354      * 
00355      * 
00356      */
00357 
00358 
00359     int             asn_realloc_rbuild_int(u_char ** pkt, size_t * pkt_len,
00360                                            size_t * offset,
00361                                            int allow_realloc, u_char type,
00362                                            const long *data, size_t data_size);
00363 
00364     int             asn_realloc_rbuild_string(u_char ** pkt,
00365                                               size_t * pkt_len,
00366                                               size_t * offset,
00367                                               int allow_realloc,
00368                                               u_char type,
00369                                               const u_char * data,
00370                                               size_t data_size);
00371 
00372     int             asn_realloc_rbuild_unsigned_int(u_char ** pkt,
00373                                                     size_t * pkt_len,
00374                                                     size_t * offset,
00375                                                     int allow_realloc,
00376                                                     u_char type,
00377                                                     const u_long * data,
00378                                                     size_t data_size);
00379 
00380     int             asn_realloc_rbuild_header(u_char ** pkt,
00381                                               size_t * pkt_len,
00382                                               size_t * offset,
00383                                               int allow_realloc,
00384                                               u_char type,
00385                                               size_t data_size);
00386 
00387     int             asn_realloc_rbuild_sequence(u_char ** pkt,
00388                                                 size_t * pkt_len,
00389                                                 size_t * offset,
00390                                                 int allow_realloc,
00391                                                 u_char type,
00392                                                 size_t data_size);
00393 
00394     int             asn_realloc_rbuild_length(u_char ** pkt,
00395                                               size_t * pkt_len,
00396                                               size_t * offset,
00397                                               int allow_realloc,
00398                                               size_t data_size);
00399 
00400     int             asn_realloc_rbuild_objid(u_char ** pkt,
00401                                              size_t * pkt_len,
00402                                              size_t * offset,
00403                                              int allow_realloc,
00404                                              u_char type, const oid *,
00405                                              size_t);
00406 
00407     int             asn_realloc_rbuild_null(u_char ** pkt,
00408                                             size_t * pkt_len,
00409                                             size_t * offset,
00410                                             int allow_realloc,
00411                                             u_char type);
00412 
00413     int             asn_realloc_rbuild_bitstring(u_char ** pkt,
00414                                                  size_t * pkt_len,
00415                                                  size_t * offset,
00416                                                  int allow_realloc,
00417                                                  u_char type,
00418                                                  const u_char * data,
00419                                                  size_t data_size);
00420 
00421     int             asn_realloc_rbuild_unsigned_int64(u_char ** pkt,
00422                                                       size_t * pkt_len,
00423                                                       size_t * offset,
00424                                                       int allow_realloc,
00425                                                       u_char type,
00426                                                       struct counter64
00427                                                       const *data, size_t);
00428 
00429     int             asn_realloc_rbuild_signed_int64(u_char ** pkt,
00430                                                     size_t * pkt_len,
00431                                                     size_t * offset,
00432                                                     int allow_realloc,
00433                                                     u_char type,
00434                                                     const struct counter64 *data,
00435                                                     size_t);
00436 
00437     int             asn_realloc_rbuild_float(u_char ** pkt,
00438                                              size_t * pkt_len,
00439                                              size_t * offset,
00440                                              int allow_realloc,
00441                                              u_char type, const float *data,
00442                                              size_t data_size);
00443 
00444     int             asn_realloc_rbuild_double(u_char ** pkt,
00445                                               size_t * pkt_len,
00446                                               size_t * offset,
00447                                               int allow_realloc,
00448                                               u_char type, const double *data,
00449                                               size_t data_size);
00450 #endif
00451 
00452 #ifdef __cplusplus
00453 }
00454 #endif
00455 #endif                          /* ASN1_H */