/contrib/bsnmp/lib/asn1.h

https://bitbucket.org/freebsd/freebsd-head/ · C++ Header · 183 lines · 94 code · 35 blank · 54 comment · 1 complexity · 5e4027de8b18b57806ac0221c113f77a MD5 · raw file

  1. /*
  2. * Copyright (c) 2001-2003
  3. * Fraunhofer Institute for Open Communication Systems (FhG Fokus).
  4. * All rights reserved.
  5. *
  6. * Author: Harti Brandt <harti@freebsd.org>
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions
  10. * are met:
  11. * 1. Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. * 2. Redistributions in binary form must reproduce the above copyright
  14. * notice, this list of conditions and the following disclaimer in the
  15. * documentation and/or other materials provided with the distribution.
  16. *
  17. * THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND
  18. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  19. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  20. * ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
  21. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  22. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  23. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  24. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  25. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  26. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  27. * SUCH DAMAGE.
  28. *
  29. * $Begemot: bsnmp/lib/asn1.h,v 1.20 2005/10/05 16:43:11 brandt_h Exp $
  30. *
  31. * ASN.1 for SNMP
  32. */
  33. #ifndef asn1_h_
  34. #define asn1_h_
  35. #include <sys/types.h>
  36. struct asn_buf {
  37. union {
  38. u_char *ptr;
  39. const u_char *cptr;
  40. } asn_u;
  41. size_t asn_len;
  42. };
  43. #define asn_cptr asn_u.cptr
  44. #define asn_ptr asn_u.ptr
  45. /* these restrictions are in the SMI */
  46. #define ASN_MAXID 0xffffffff
  47. #define ASN_MAXOIDLEN 128
  48. /* the string needed for this (with trailing zero) */
  49. #define ASN_OIDSTRLEN (ASN_MAXOIDLEN * (10 + 1) - 1 + 1)
  50. /* type of subidentifiers */
  51. typedef uint32_t asn_subid_t;
  52. struct asn_oid {
  53. u_int len;
  54. asn_subid_t subs[ASN_MAXOIDLEN];
  55. };
  56. enum asn_err {
  57. /* conversion was ok */
  58. ASN_ERR_OK = 0,
  59. /* conversion failed and stopped */
  60. ASN_ERR_FAILED = 1 | 0x1000,
  61. /* length field bad, value skipped */
  62. ASN_ERR_BADLEN = 2,
  63. /* out of buffer, stopped */
  64. ASN_ERR_EOBUF = 3 | 0x1000,
  65. /* length ok, but value is out of range */
  66. ASN_ERR_RANGE = 4,
  67. /* not the expected tag, stopped */
  68. ASN_ERR_TAG = 5 | 0x1000,
  69. };
  70. #define ASN_ERR_STOPPED(E) (((E) & 0x1000) != 0)
  71. /* type for the length field of encoded values. The length is restricted
  72. * to 65535, but using uint16_t would give conversion warnings on gcc */
  73. typedef uint32_t asn_len_t; /* could be also uint16_t */
  74. /* maximal length of a long length field without the length of the length */
  75. #define ASN_MAXLEN 65535
  76. #define ASN_MAXLENLEN 2 /* number of bytes in a length */
  77. /* maximum size of an octet string as per SMIv2 */
  78. #define ASN_MAXOCTETSTRING 65535
  79. extern void (*asn_error)(const struct asn_buf *, const char *, ...);
  80. enum asn_err asn_get_header(struct asn_buf *, u_char *, asn_len_t *);
  81. enum asn_err asn_put_header(struct asn_buf *, u_char, asn_len_t);
  82. enum asn_err asn_put_temp_header(struct asn_buf *, u_char, u_char **);
  83. enum asn_err asn_commit_header(struct asn_buf *, u_char *, size_t *);
  84. enum asn_err asn_get_integer_raw(struct asn_buf *, asn_len_t, int32_t *);
  85. enum asn_err asn_get_integer(struct asn_buf *, int32_t *);
  86. enum asn_err asn_put_integer(struct asn_buf *, int32_t);
  87. enum asn_err asn_get_octetstring_raw(struct asn_buf *, asn_len_t, u_char *, u_int *);
  88. enum asn_err asn_get_octetstring(struct asn_buf *, u_char *, u_int *);
  89. enum asn_err asn_put_octetstring(struct asn_buf *, const u_char *, u_int);
  90. enum asn_err asn_get_null_raw(struct asn_buf *b, asn_len_t);
  91. enum asn_err asn_get_null(struct asn_buf *);
  92. enum asn_err asn_put_null(struct asn_buf *);
  93. enum asn_err asn_put_exception(struct asn_buf *, u_int);
  94. enum asn_err asn_get_objid_raw(struct asn_buf *, asn_len_t, struct asn_oid *);
  95. enum asn_err asn_get_objid(struct asn_buf *, struct asn_oid *);
  96. enum asn_err asn_put_objid(struct asn_buf *, const struct asn_oid *);
  97. enum asn_err asn_get_sequence(struct asn_buf *, asn_len_t *);
  98. enum asn_err asn_get_ipaddress_raw(struct asn_buf *, asn_len_t, u_char *);
  99. enum asn_err asn_get_ipaddress(struct asn_buf *, u_char *);
  100. enum asn_err asn_put_ipaddress(struct asn_buf *, const u_char *);
  101. enum asn_err asn_get_uint32_raw(struct asn_buf *, asn_len_t, uint32_t *);
  102. enum asn_err asn_put_uint32(struct asn_buf *, u_char, uint32_t);
  103. enum asn_err asn_get_counter64_raw(struct asn_buf *, asn_len_t, uint64_t *);
  104. enum asn_err asn_put_counter64(struct asn_buf *, uint64_t);
  105. enum asn_err asn_get_timeticks(struct asn_buf *, uint32_t *);
  106. enum asn_err asn_put_timeticks(struct asn_buf *, uint32_t);
  107. enum asn_err asn_skip(struct asn_buf *, asn_len_t);
  108. enum asn_err asn_pad(struct asn_buf *, asn_len_t);
  109. /*
  110. * Utility functions for OIDs
  111. */
  112. /* get a sub-OID from the middle of another OID */
  113. void asn_slice_oid(struct asn_oid *, const struct asn_oid *, u_int, u_int);
  114. /* append an OID to another one */
  115. void asn_append_oid(struct asn_oid *, const struct asn_oid *);
  116. /* compare two OIDs */
  117. int asn_compare_oid(const struct asn_oid *, const struct asn_oid *);
  118. /* check whether the first is a suboid of the second one */
  119. int asn_is_suboid(const struct asn_oid *, const struct asn_oid *);
  120. /* format an OID into a user buffer of size ASN_OIDSTRLEN */
  121. char *asn_oid2str_r(const struct asn_oid *, char *);
  122. /* format an OID into a private static buffer */
  123. char *asn_oid2str(const struct asn_oid *);
  124. enum {
  125. ASN_TYPE_BOOLEAN = 0x01,
  126. ASN_TYPE_INTEGER = 0x02,
  127. ASN_TYPE_BITSTRING = 0x03,
  128. ASN_TYPE_OCTETSTRING = 0x04,
  129. ASN_TYPE_NULL = 0x05,
  130. ASN_TYPE_OBJID = 0x06,
  131. ASN_TYPE_SEQUENCE = 0x10,
  132. ASN_TYPE_CONSTRUCTED = 0x20,
  133. ASN_CLASS_UNIVERSAL = 0x00,
  134. ASN_CLASS_APPLICATION = 0x40,
  135. ASN_CLASS_CONTEXT = 0x80,
  136. ASN_CLASS_PRIVATE = 0xc0,
  137. ASN_TYPE_MASK = 0x1f,
  138. ASN_APP_IPADDRESS = 0x00,
  139. ASN_APP_COUNTER = 0x01,
  140. ASN_APP_GAUGE = 0x02,
  141. ASN_APP_TIMETICKS = 0x03,
  142. ASN_APP_OPAQUE = 0x04, /* not implemented */
  143. ASN_APP_COUNTER64 = 0x06,
  144. ASN_EXCEPT_NOSUCHOBJECT = 0x00,
  145. ASN_EXCEPT_NOSUCHINSTANCE = 0x01,
  146. ASN_EXCEPT_ENDOFMIBVIEW = 0x02,
  147. };
  148. #endif