Skip to content

Commit 718e913

Browse files
committed
add SHA256 and SHA512 for security protocol
1 parent 28500fe commit 718e913

File tree

4 files changed

+51
-4
lines changed

4 files changed

+51
-4
lines changed

ext/snmp/config.m4

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ if test "$PHP_SNMP" != "no"; then
3030
AC_MSG_ERROR([Could not find the required paths. Please check your net-snmp installation.])
3131
fi
3232
else
33-
AC_MSG_ERROR([Net-SNMP version 5.3 or greater reqired (detected $snmp_full_version).])
33+
AC_MSG_ERROR([Net-SNMP version 5.3 or greater required (detected $snmp_full_version).])
3434
fi
3535
else
3636
AC_MSG_ERROR([Could not find net-snmp-config binary. Please check your net-snmp installation.])
@@ -54,6 +54,22 @@ if test "$PHP_SNMP" != "no"; then
5454
$SNMP_SHARED_LIBADD
5555
])
5656

57+
dnl Check whether usmHMAC192SHA256AuthProtocol exists.
58+
PHP_CHECK_LIBRARY($SNMP_LIBNAME, usmHMAC192SHA256AuthProtocol,
59+
[
60+
AC_DEFINE(HAVE_SNMP_SHA256, 1, [ ])
61+
], [], [
62+
$SNMP_SHARED_LIBADD
63+
])
64+
65+
dnl Check whether usmHMAC384SHA512AuthProtocol exists.
66+
PHP_CHECK_LIBRARY($SNMP_LIBNAME, usmHMAC384SHA512AuthProtocol,
67+
[
68+
AC_DEFINE(HAVE_SNMP_SHA512, 1, [ ])
69+
], [], [
70+
$SNMP_SHARED_LIBADD
71+
])
72+
5773
PHP_NEW_EXTENSION(snmp, snmp.c, $ext_shared)
5874
PHP_SUBST(SNMP_SHARED_LIBADD)
5975
fi

ext/snmp/snmp.c

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
#include "php_snmp.h"
3030

3131
#include "zend_exceptions.h"
32+
#include "zend_smart_string.h"
3233
#include "ext/spl/spl_exceptions.h"
3334
#include "snmp_arginfo.h"
3435

@@ -936,7 +937,37 @@ static bool netsnmp_session_set_auth_protocol(struct snmp_session *s, zend_strin
936937
return true;
937938
}
938939

939-
zend_value_error("Authentication protocol must be either \"MD5\" or \"SHA\"");
940+
#ifdef HAVE_SNMP_SHA256
941+
if (zend_string_equals_literal_ci(prot, "SHA256")) {
942+
s->securityAuthProto = usmHMAC192SHA256AuthProtocol;
943+
s->securityAuthProtoLen = sizeof(usmHMAC192SHA256AuthProtocol) / sizeof(oid);
944+
return true;
945+
}
946+
#endif
947+
948+
#ifdef HAVE_SNMP_SHA512
949+
if (zend_string_equals_literal_ci(prot, "SHA512")) {
950+
s->securityAuthProto = usmHMAC384SHA512AuthProtocol;
951+
s->securityAuthProtoLen = sizeof(usmHMAC384SHA512AuthProtocol) / sizeof(oid);
952+
return true;
953+
}
954+
#endif
955+
956+
smart_string err = {0};
957+
958+
smart_string_appends(&err, "Authentication protocol must be \"SHA\"");
959+
#ifdef HAVE_SNMP_SHA256
960+
smart_string_appends(&err, " or \"SHA256\"");
961+
#endif
962+
#ifdef HAVE_SNMP_SHA512
963+
smart_string_appends(&err, " or \"SHA512\"");
964+
#endif
965+
#ifndef DISABLE_MD5
966+
smart_string_appends(&err, " or \"MD5\"");
967+
#endif
968+
smart_string_0(&err);
969+
zend_value_error("%s", err.c);
970+
smart_string_free(&err);
940971
return false;
941972
}
942973
/* }}} */

ext/snmp/tests/snmp-object-setSecurity_error.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ var_dump($session->close());
6161
--EXPECTF--
6262
Security level must be one of "noAuthNoPriv", "authNoPriv", or "authPriv"
6363
Security level must be one of "noAuthNoPriv", "authNoPriv", or "authPriv"
64-
Authentication protocol must be either "MD5" or "SHA"
64+
Authentication protocol must be %s
6565

6666
Warning: SNMP::setSecurity(): Error generating a key for authentication pass phrase '': Generic error (The supplied password length is too short.) in %s on line %d
6767
bool(false)

ext/snmp/tests/snmp3-error.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ try {
6060
Checking error handling
6161
Security level must be one of "noAuthNoPriv", "authNoPriv", or "authPriv"
6262
Security level must be one of "noAuthNoPriv", "authNoPriv", or "authPriv"
63-
Authentication protocol must be either "MD5" or "SHA"
63+
Authentication protocol must be %s
6464

6565
Warning: snmp3_get(): Error generating a key for authentication pass phrase '': Generic error (The supplied password length is too short.) in %s on line %d
6666
bool(false)

0 commit comments

Comments
 (0)