asn1.h 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486
  1. #ifndef ASN1_H
  2. #define ASN1_H
  3. #include <net-snmp/library/oid.h>
  4. #ifdef HAVE_SYS_TYPES_H
  5. #include <sys/types.h>
  6. #endif
  7. #ifdef __cplusplus
  8. extern "C" {
  9. #endif
  10. #define PARSE_PACKET 0
  11. #define DUMP_PACKET 1
  12. /*
  13. * Definitions for Abstract Syntax Notation One, ASN.1
  14. * As defined in ISO/IS 8824 and ISO/IS 8825
  15. *
  16. *
  17. */
  18. /***********************************************************
  19. Copyright 1988, 1989 by Carnegie Mellon University
  20. All Rights Reserved
  21. Permission to use, copy, modify, and distribute this software and its
  22. documentation for any purpose and without fee is hereby granted,
  23. provided that the above copyright notice appear in all copies and that
  24. both that copyright notice and this permission notice appear in
  25. supporting documentation, and that the name of CMU not be
  26. used in advertising or publicity pertaining to distribution of the
  27. software without specific, written prior permission.
  28. CMU DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
  29. ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
  30. CMU BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
  31. ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
  32. WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
  33. ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
  34. SOFTWARE.
  35. ******************************************************************/
  36. /*
  37. * Portions of this file are copyrighted by:
  38. * Copyright (c) 2016 VMware, Inc. All rights reserved.
  39. * Use is subject to license terms specified in the COPYING file
  40. */
  41. #define MIN_OID_LEN 2
  42. #define MAX_OID_LEN 128 /* max subid's in an oid */
  43. #ifndef MAX_NAME_LEN /* conflicts with some libraries */
  44. #define MAX_NAME_LEN MAX_OID_LEN /* obsolete. use MAX_OID_LEN */
  45. #endif
  46. #define OID_LENGTH(x) (sizeof(x)/sizeof(oid))
  47. #define ASN_BOOLEAN 0x01U
  48. #define ASN_INTEGER 0x02U
  49. #define ASN_BIT_STR 0x03U
  50. #define ASN_OCTET_STR 0x04U
  51. #define ASN_NULL 0x05U
  52. #define ASN_OBJECT_ID 0x06U
  53. #define ASN_SEQUENCE 0x10U
  54. #define ASN_SET 0x11U
  55. #define ASN_UNIVERSAL 0x00U
  56. #define ASN_APPLICATION 0x40U
  57. #define ASN_CONTEXT 0x80U
  58. #define ASN_PRIVATE 0xC0U
  59. #define ASN_PRIMITIVE 0x00U
  60. #define ASN_CONSTRUCTOR 0x20U
  61. #define ASN_LONG_LEN 0x80U
  62. #define ASN_EXTENSION_ID 0x1FU
  63. #define ASN_BIT8 0x80U
  64. #define IS_CONSTRUCTOR(byte) ((byte) & ASN_CONSTRUCTOR)
  65. #define IS_EXTENSION_ID(byte) (((byte) & ASN_EXTENSION_ID) == ASN_EXTENSION_ID)
  66. struct counter64 {
  67. u_long high;
  68. u_long low;
  69. };
  70. #ifdef NETSNMP_WITH_OPAQUE_SPECIAL_TYPES
  71. typedef struct counter64 integer64;
  72. typedef struct counter64 unsigned64;
  73. /*
  74. * The BER inside an OPAQUE is an context specific with a value of 48 (0x30)
  75. * plus the "normal" tag. For a Counter64, the tag is 0x46 (i.e., an
  76. * applications specific tag with value 6). So the value for a 64 bit
  77. * counter is 0x46 + 0x30, or 0x76 (118 base 10). However, values
  78. * greater than 30 can not be encoded in one octet. So the first octet
  79. * has the class, in this case context specific (ASN_CONTEXT), and
  80. * the special value (i.e., 31) to indicate that the real value follows
  81. * in one or more octets. The high order bit of each following octet
  82. * indicates if the value is encoded in additional octets. A high order
  83. * bit of zero, indicates the last. For this "hack", only one octet
  84. * will be used for the value.
  85. */
  86. /*
  87. * first octet of the tag
  88. */
  89. #define ASN_OPAQUE_TAG1 (ASN_CONTEXT | ASN_EXTENSION_ID)
  90. /*
  91. * base value for the second octet of the tag - the
  92. * second octet was the value for the tag
  93. */
  94. #define ASN_OPAQUE_TAG2 0x30U
  95. #define ASN_OPAQUE_TAG2U 0x2fU /* second octet of tag for union */
  96. /*
  97. * All the ASN.1 types for SNMP "should have been" defined in this file,
  98. * but they were not. (They are defined in snmp_impl.h) Thus, the tag for
  99. * Opaque and Counter64 is defined, again, here with a different names.
  100. */
  101. #define ASN_APP_OPAQUE (ASN_APPLICATION | 4)
  102. #define ASN_APP_COUNTER64 (ASN_APPLICATION | 6)
  103. #define ASN_APP_FLOAT (ASN_APPLICATION | 8)
  104. #define ASN_APP_DOUBLE (ASN_APPLICATION | 9)
  105. #define ASN_APP_I64 (ASN_APPLICATION | 10)
  106. #define ASN_APP_U64 (ASN_APPLICATION | 11)
  107. #define ASN_APP_UNION (ASN_PRIVATE | 1) /* or ASN_PRIV_UNION ? */
  108. /*
  109. * value for Counter64
  110. */
  111. #define ASN_OPAQUE_COUNTER64 (ASN_OPAQUE_TAG2 + ASN_APP_COUNTER64)
  112. /*
  113. * max size of BER encoding of Counter64
  114. */
  115. #define ASN_OPAQUE_COUNTER64_MX_BER_LEN 12
  116. /*
  117. * value for Float
  118. */
  119. #define ASN_OPAQUE_FLOAT (ASN_OPAQUE_TAG2 + ASN_APP_FLOAT)
  120. /*
  121. * size of BER encoding of Float
  122. */
  123. #define ASN_OPAQUE_FLOAT_BER_LEN 7
  124. /*
  125. * value for Double
  126. */
  127. #define ASN_OPAQUE_DOUBLE (ASN_OPAQUE_TAG2 + ASN_APP_DOUBLE)
  128. /*
  129. * size of BER encoding of Double
  130. */
  131. #define ASN_OPAQUE_DOUBLE_BER_LEN 11
  132. /*
  133. * value for Integer64
  134. */
  135. #define ASN_OPAQUE_I64 (ASN_OPAQUE_TAG2 + ASN_APP_I64)
  136. /*
  137. * max size of BER encoding of Integer64
  138. */
  139. #define ASN_OPAQUE_I64_MX_BER_LEN 11
  140. /*
  141. * value for Unsigned64
  142. */
  143. #define ASN_OPAQUE_U64 (ASN_OPAQUE_TAG2 + ASN_APP_U64)
  144. /*
  145. * max size of BER encoding of Unsigned64
  146. */
  147. #define ASN_OPAQUE_U64_MX_BER_LEN 12
  148. #endif /* NETSNMP_WITH_OPAQUE_SPECIAL_TYPES */
  149. #define ASN_PRIV_INCL_RANGE (ASN_PRIVATE | 2)
  150. #define ASN_PRIV_EXCL_RANGE (ASN_PRIVATE | 3)
  151. #define ASN_PRIV_DELEGATED (ASN_PRIVATE | 5)
  152. #define ASN_PRIV_IMPLIED_OCTET_STR (ASN_PRIVATE | ASN_OCTET_STR) /* 4 */
  153. #define ASN_PRIV_IMPLIED_OBJECT_ID (ASN_PRIVATE | ASN_OBJECT_ID) /* 6 */
  154. #define ASN_PRIV_RETRY (ASN_PRIVATE | 7) /* 199 */
  155. #define ASN_PRIV_STOP (ASN_PRIVATE | 8) /* 200 */
  156. #define IS_DELEGATED(x) ((x) == ASN_PRIV_DELEGATED)
  157. NETSNMP_IMPORT
  158. int asn_check_packet(u_char *, size_t);
  159. NETSNMP_IMPORT
  160. u_char *asn_parse_int(u_char *, size_t *, u_char *, long *,
  161. size_t);
  162. NETSNMP_IMPORT
  163. u_char *asn_build_int(u_char *, size_t *, u_char, const long *,
  164. size_t);
  165. NETSNMP_IMPORT
  166. u_char *asn_parse_unsigned_int(u_char *, size_t *, u_char *,
  167. u_long *, size_t);
  168. NETSNMP_IMPORT
  169. u_char *asn_build_unsigned_int(u_char *, size_t *, u_char,
  170. const u_long *, size_t);
  171. NETSNMP_IMPORT
  172. u_char *asn_parse_string(u_char *, size_t *, u_char *,
  173. u_char *, size_t *);
  174. NETSNMP_IMPORT
  175. u_char *asn_build_string(u_char *, size_t *, u_char,
  176. const u_char *, size_t);
  177. NETSNMP_IMPORT
  178. u_char *asn_parse_header(u_char *, size_t *, u_char *);
  179. NETSNMP_IMPORT
  180. u_char *asn_parse_sequence(u_char *, size_t *, u_char *, u_char expected_type, /* must be this type */
  181. const char *estr); /* error message prefix */
  182. NETSNMP_IMPORT
  183. u_char *asn_build_header(u_char *, size_t *, u_char, size_t);
  184. NETSNMP_IMPORT
  185. u_char *asn_build_sequence(u_char *, size_t *, u_char, size_t);
  186. NETSNMP_IMPORT
  187. u_char *asn_parse_length(u_char *, u_long *);
  188. NETSNMP_IMPORT
  189. u_char *asn_build_length(u_char *, size_t *, size_t);
  190. NETSNMP_IMPORT
  191. u_char *asn_parse_objid(u_char *, size_t *, u_char *, oid *,
  192. size_t *);
  193. NETSNMP_IMPORT
  194. u_char *asn_build_objid(u_char *, size_t *, u_char, oid *,
  195. size_t);
  196. NETSNMP_IMPORT
  197. u_char *asn_parse_null(u_char *, size_t *, u_char *);
  198. NETSNMP_IMPORT
  199. u_char *asn_build_null(u_char *, size_t *, u_char);
  200. NETSNMP_IMPORT
  201. u_char *asn_parse_bitstring(u_char *, size_t *, u_char *,
  202. u_char *, size_t *);
  203. NETSNMP_IMPORT
  204. u_char *asn_build_bitstring(u_char *, size_t *, u_char,
  205. const u_char *, size_t);
  206. NETSNMP_IMPORT
  207. u_char *asn_parse_unsigned_int64(u_char *, size_t *, u_char *,
  208. struct counter64 *, size_t);
  209. NETSNMP_IMPORT
  210. u_char *asn_build_unsigned_int64(u_char *, size_t *, u_char,
  211. const struct counter64 *, size_t);
  212. NETSNMP_IMPORT
  213. u_char *asn_parse_signed_int64(u_char *, size_t *, u_char *,
  214. struct counter64 *, size_t);
  215. NETSNMP_IMPORT
  216. u_char *asn_build_signed_int64(u_char *, size_t *, u_char,
  217. const struct counter64 *, size_t);
  218. NETSNMP_IMPORT
  219. u_char *asn_build_float(u_char *, size_t *, u_char, const float *,
  220. size_t);
  221. NETSNMP_IMPORT
  222. u_char *asn_parse_float(u_char *, size_t *, u_char *, float *,
  223. size_t);
  224. NETSNMP_IMPORT
  225. u_char *asn_build_double(u_char *, size_t *, u_char, const double *,
  226. size_t);
  227. NETSNMP_IMPORT
  228. u_char *asn_parse_double(u_char *, size_t *, u_char *,
  229. double *, size_t);
  230. #ifdef NETSNMP_USE_REVERSE_ASNENCODING
  231. /*
  232. * Re-allocator function for below.
  233. */
  234. NETSNMP_IMPORT
  235. int asn_realloc(u_char **, size_t *);
  236. /*
  237. * Re-allocating reverse ASN.1 encoder functions. Synopsis:
  238. *
  239. * u_char *buf = (u_char*)malloc(100);
  240. * u_char type = (ASN_UNIVERSAL | ASN_PRIMITIVE | ASN_INTEGER);
  241. * size_t buf_len = 100, offset = 0;
  242. * long data = 12345;
  243. * int allow_realloc = 1;
  244. *
  245. * if (asn_realloc_rbuild_int(&buf, &buf_len, &offset, allow_realloc,
  246. * type, &data, sizeof(long)) == 0) {
  247. * error;
  248. * }
  249. *
  250. * NOTE WELL: after calling one of these functions with allow_realloc
  251. * non-zero, buf might have moved, buf_len might have grown and
  252. * offset will have increased by the size of the encoded data.
  253. * You should **NEVER** do something like this:
  254. *
  255. * u_char *buf = (u_char *)malloc(100), *ptr;
  256. * u_char type = (ASN_UNIVERSAL | ASN_PRIMITIVE | ASN_INTEGER);
  257. * size_t buf_len = 100, offset = 0;
  258. * long data1 = 1234, data2 = 5678;
  259. * int rc = 0, allow_realloc = 1;
  260. *
  261. * rc = asn_realloc_rbuild_int(&buf, &buf_len, &offset, allow_realloc,
  262. * type, &data1, sizeof(long));
  263. * ptr = buf[buf_len - offset]; / * points at encoding of data1 * /
  264. * if (rc == 0) {
  265. * error;
  266. * }
  267. * rc = asn_realloc_rbuild_int(&buf, &buf_len, &offset, allow_realloc,
  268. * type, &data2, sizeof(long));
  269. * make use of ptr here;
  270. *
  271. *
  272. * ptr is **INVALID** at this point. In general, you should store the
  273. * offset value and compute pointers when you need them:
  274. *
  275. *
  276. *
  277. * u_char *buf = (u_char *)malloc(100), *ptr;
  278. * u_char type = (ASN_UNIVERSAL | ASN_PRIMITIVE | ASN_INTEGER);
  279. * size_t buf_len = 100, offset = 0, ptr_offset;
  280. * long data1 = 1234, data2 = 5678;
  281. * int rc = 0, allow_realloc = 1;
  282. *
  283. * rc = asn_realloc_rbuild_int(&buf, &buf_len, &offset, allow_realloc,
  284. * type, &data1, sizeof(long));
  285. * ptr_offset = offset;
  286. * if (rc == 0) {
  287. * error;
  288. * }
  289. * rc = asn_realloc_rbuild_int(&buf, &buf_len, &offset, allow_realloc,
  290. * type, &data2, sizeof(long));
  291. * ptr = buf + buf_len - ptr_offset
  292. * make use of ptr here;
  293. *
  294. *
  295. *
  296. * Here, you can see that ptr will be a valid pointer even if the block of
  297. * memory has been moved, as it may well have been. Plenty of examples of
  298. * usage all over asn1.c, snmp_api.c, snmpusm.c.
  299. *
  300. * The other thing you should **NEVER** do is to pass a pointer to a buffer
  301. * on the stack as the first argument when allow_realloc is non-zero, unless
  302. * you really know what you are doing and your machine/compiler allows you to
  303. * free non-heap memory. There are rumours that such things exist, but many
  304. * consider them no more than the wild tales of a fool.
  305. *
  306. * Of course, you can pass allow_realloc as zero, to indicate that you do not
  307. * wish the packet buffer to be reallocated for some reason; perhaps because
  308. * it is on the stack. This may be useful to emulate the functionality of
  309. * the old API:
  310. *
  311. * u_char my_static_buffer[100], *cp = NULL;
  312. * size_t my_static_buffer_len = 100;
  313. * float my_pi = (float)22/(float)7;
  314. *
  315. * cp = asn_rbuild_float(my_static_buffer, &my_static_buffer_len,
  316. * ASN_OPAQUE_FLOAT, &my_pi, sizeof(float));
  317. * if (cp == NULL) {
  318. * error;
  319. * }
  320. *
  321. *
  322. * IS EQUIVALENT TO:
  323. *
  324. *
  325. * u_char my_static_buffer[100];
  326. * size_t my_static_buffer_len = 100, my_offset = 0;
  327. * float my_pi = (float)22/(float)7;
  328. * int rc = 0;
  329. *
  330. * rc = asn_realloc_rbuild_float(&my_static_buffer, &my_static_buffer_len,
  331. * &my_offset, 0,
  332. * ASN_OPAQUE_FLOAT, &my_pi, sizeof(float));
  333. * if (rc == 0) {
  334. * error;
  335. * }
  336. *
  337. *
  338. */
  339. NETSNMP_IMPORT
  340. int asn_realloc_rbuild_int(u_char ** pkt, size_t * pkt_len,
  341. size_t * offset,
  342. int allow_realloc, u_char type,
  343. const long *data, size_t data_size);
  344. NETSNMP_IMPORT
  345. int asn_realloc_rbuild_string(u_char ** pkt,
  346. size_t * pkt_len,
  347. size_t * offset,
  348. int allow_realloc,
  349. u_char type,
  350. const u_char * data,
  351. size_t data_size);
  352. NETSNMP_IMPORT
  353. int asn_realloc_rbuild_unsigned_int(u_char ** pkt,
  354. size_t * pkt_len,
  355. size_t * offset,
  356. int allow_realloc,
  357. u_char type,
  358. const u_long * data,
  359. size_t data_size);
  360. NETSNMP_IMPORT
  361. int asn_realloc_rbuild_header(u_char ** pkt,
  362. size_t * pkt_len,
  363. size_t * offset,
  364. int allow_realloc,
  365. u_char type,
  366. size_t data_size);
  367. NETSNMP_IMPORT
  368. int asn_realloc_rbuild_sequence(u_char ** pkt,
  369. size_t * pkt_len,
  370. size_t * offset,
  371. int allow_realloc,
  372. u_char type,
  373. size_t data_size);
  374. NETSNMP_IMPORT
  375. int asn_realloc_rbuild_length(u_char ** pkt,
  376. size_t * pkt_len,
  377. size_t * offset,
  378. int allow_realloc,
  379. size_t data_size);
  380. NETSNMP_IMPORT
  381. int asn_realloc_rbuild_objid(u_char ** pkt,
  382. size_t * pkt_len,
  383. size_t * offset,
  384. int allow_realloc,
  385. u_char type, const oid *,
  386. size_t);
  387. NETSNMP_IMPORT
  388. int asn_realloc_rbuild_null(u_char ** pkt,
  389. size_t * pkt_len,
  390. size_t * offset,
  391. int allow_realloc,
  392. u_char type);
  393. NETSNMP_IMPORT
  394. int asn_realloc_rbuild_bitstring(u_char ** pkt,
  395. size_t * pkt_len,
  396. size_t * offset,
  397. int allow_realloc,
  398. u_char type,
  399. const u_char * data,
  400. size_t data_size);
  401. NETSNMP_IMPORT
  402. int asn_realloc_rbuild_unsigned_int64(u_char ** pkt,
  403. size_t * pkt_len,
  404. size_t * offset,
  405. int allow_realloc,
  406. u_char type,
  407. struct counter64
  408. const *data, size_t);
  409. NETSNMP_IMPORT
  410. int asn_realloc_rbuild_signed_int64(u_char ** pkt,
  411. size_t * pkt_len,
  412. size_t * offset,
  413. int allow_realloc,
  414. u_char type,
  415. const struct counter64 *data,
  416. size_t);
  417. NETSNMP_IMPORT
  418. int asn_realloc_rbuild_float(u_char ** pkt,
  419. size_t * pkt_len,
  420. size_t * offset,
  421. int allow_realloc,
  422. u_char type, const float *data,
  423. size_t data_size);
  424. NETSNMP_IMPORT
  425. int asn_realloc_rbuild_double(u_char ** pkt,
  426. size_t * pkt_len,
  427. size_t * offset,
  428. int allow_realloc,
  429. u_char type, const double *data,
  430. size_t data_size);
  431. #endif
  432. #ifdef __cplusplus
  433. }
  434. #endif
  435. #endif /* ASN1_H */