snmp_auth.c 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. /*
  2. * Copyright 1998-2007 by egnite Software GmbH
  3. * Copyright 1988, 1989, 1991, 1992 by Carnegie Mellon University
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions
  7. * are met:
  8. *
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. * 3. Neither the name of the copyright holders nor the names of
  15. * contributors may be used to endorse or promote products derived
  16. * from this software without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  19. * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  20. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  21. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  22. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  23. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  24. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  25. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  26. * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  27. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
  28. * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  29. * SUCH DAMAGE.
  30. *
  31. * For additional information see http://www.ethernut.de/
  32. */
  33. #include <pro/snmp.h>
  34. #include <pro/snmp_agent.h>
  35. #include <pro/snmp_auth.h>
  36. /*!
  37. * \addtogroup xgSNMP
  38. */
  39. /*@{*/
  40. /*!
  41. * \brief Parse header of community string based message.
  42. *
  43. * Retrieves version and community.
  44. *
  45. * \param data Points to the message.
  46. * \param length Bytes left in message.
  47. * \param sidp Pointer to a buffer that receives the community string.
  48. * \param slen Length of the community string.
  49. * \param version Message version
  50. */
  51. const uint8_t *SnmpAuthParse(const uint8_t * data, size_t * length, uint8_t * sidp, size_t * slen, long *version)
  52. {
  53. uint8_t type = ASN_SEQUENCE | ASN_CONSTRUCTOR;
  54. /* Check header type. */
  55. if ((data = AsnSequenceParse(data, length, type)) == NULL) {
  56. return NULL;
  57. }
  58. /* Get SNMP version. */
  59. if ((data = AsnIntegerParse(data, length, &type, version)) == NULL) {
  60. return NULL;
  61. }
  62. /* Get SNMP community. */
  63. if ((data = AsnOctetStringParse(data, length, &type, sidp, slen)) == NULL) {
  64. return NULL;
  65. }
  66. if (*version == SNMP_VERSION_1) {
  67. sidp[*slen] = '\0';
  68. }
  69. return data;
  70. }
  71. /*!
  72. * \brief Build header of community string based message.
  73. */
  74. uint8_t *SnmpAuthBuild(SNMP_SESSION * session, uint8_t * data, size_t * length, size_t messagelen)
  75. {
  76. data = AsnSequenceBuild(data, length, ASN_SEQUENCE | ASN_CONSTRUCTOR, messagelen + session->sess_id_len + 5);
  77. if (data == NULL) {
  78. return NULL;
  79. }
  80. data = AsnIntegerBuild(data, length, ASN_UNIVERSAL | ASN_PRIMITIVE | ASN_INTEGER, (long *) &session->sess_version);
  81. if (data == NULL) {
  82. return NULL;
  83. }
  84. data = AsnOctetStringBuild(data, length, ASN_UNIVERSAL | ASN_PRIMITIVE | ASN_OCTET_STR, session->sess_id, session->sess_id_len);
  85. if (data == NULL) {
  86. return NULL;
  87. }
  88. return data;
  89. }
  90. /*@}*/