b444af
Backported for 8.0 from
b444af
b444af
b444af
From 718e91343fddb8817a004f96f111c424843bf746 Mon Sep 17 00:00:00 2001
b444af
From: Remi Collet <remi@php.net>
b444af
Date: Wed, 11 Aug 2021 13:02:18 +0200
b444af
Subject: [PATCH] add SHA256 and SHA512 for security protocol
b444af
b444af
---
b444af
 ext/snmp/config.m4                            | 18 +++++++++-
b444af
 ext/snmp/snmp.c                               | 33 ++++++++++++++++++-
b444af
 .../tests/snmp-object-setSecurity_error.phpt  |  2 +-
b444af
 ext/snmp/tests/snmp3-error.phpt               |  2 +-
b444af
 4 files changed, 51 insertions(+), 4 deletions(-)
b444af
b444af
diff --git a/ext/snmp/config.m4 b/ext/snmp/config.m4
b444af
index 1475ddfe2b7f0..f285a572de9cb 100644
b444af
--- a/ext/snmp/config.m4
b444af
+++ b/ext/snmp/config.m4
b444af
@@ -30,7 +30,7 @@ if test "$PHP_SNMP" != "no"; then
b444af
         AC_MSG_ERROR([Could not find the required paths. Please check your net-snmp installation.])
b444af
       fi
b444af
     else
b444af
-      AC_MSG_ERROR([Net-SNMP version 5.3 or greater reqired (detected $snmp_full_version).])
b444af
+      AC_MSG_ERROR([Net-SNMP version 5.3 or greater required (detected $snmp_full_version).])
b444af
     fi
b444af
   else
b444af
     AC_MSG_ERROR([Could not find net-snmp-config binary. Please check your net-snmp installation.])
b444af
@@ -54,6 +54,22 @@ if test "$PHP_SNMP" != "no"; then
b444af
     $SNMP_SHARED_LIBADD
b444af
   ])
b444af
 
b444af
+  dnl Check whether usmHMAC192SHA256AuthProtocol exists.
b444af
+  PHP_CHECK_LIBRARY($SNMP_LIBNAME, usmHMAC192SHA256AuthProtocol,
b444af
+  [
b444af
+    AC_DEFINE(HAVE_SNMP_SHA256, 1, [ ])
b444af
+  ], [], [
b444af
+    $SNMP_SHARED_LIBADD
b444af
+  ])
b444af
+
b444af
+  dnl Check whether usmHMAC384SHA512AuthProtocol exists.
b444af
+  PHP_CHECK_LIBRARY($SNMP_LIBNAME, usmHMAC384SHA512AuthProtocol,
b444af
+  [
b444af
+    AC_DEFINE(HAVE_SNMP_SHA512, 1, [ ])
b444af
+  ], [], [
b444af
+    $SNMP_SHARED_LIBADD
b444af
+  ])
b444af
+
b444af
   PHP_NEW_EXTENSION(snmp, snmp.c, $ext_shared)
b444af
   PHP_SUBST(SNMP_SHARED_LIBADD)
b444af
 fi
b444af
diff --git a/ext/snmp/snmp.c b/ext/snmp/snmp.c
b444af
index 69d6549405b17..f0917501751f5 100644
b444af
--- a/ext/snmp/snmp.c
b444af
+++ b/ext/snmp/snmp.c
b444af
@@ -29,6 +29,7 @@
b444af
 #include "php_snmp.h"
b444af
 
b444af
 #include "zend_exceptions.h"
b444af
+#include "zend_smart_string.h"
b444af
 #include "ext/spl/spl_exceptions.h"
b444af
 #include "snmp_arginfo.h"
b444af
 
240c49
@@ -938,16 +939,48 @@ static int netsnmp_session_set_auth_protocol(struct snmp_session *s, char *prot)
b444af
 	if (!strcasecmp(prot, "MD5")) {
b444af
 		s->securityAuthProto = usmHMACMD5AuthProtocol;
b444af
 		s->securityAuthProtoLen = USM_AUTH_PROTO_MD5_LEN;
b444af
-	} else
240c49
+		return 0;
b444af
+	}
b444af
 #endif
b444af
+
b444af
 	if (!strcasecmp(prot, "SHA")) {
b444af
 		s->securityAuthProto = usmHMACSHA1AuthProtocol;
b444af
 		s->securityAuthProtoLen = USM_AUTH_PROTO_SHA_LEN;
b444af
-	} else {
b444af
-		zend_value_error("Authentication protocol must be either \"MD5\" or \"SHA\"");
b444af
-		return (-1);
240c49
+		return 0;
b444af
 	}
b444af
-	return (0);
b444af
+
b444af
+#ifdef HAVE_SNMP_SHA256
b444af
+	if (!strcasecmp(prot, "SHA256")) {
b444af
+		s->securityAuthProto = usmHMAC192SHA256AuthProtocol;
b444af
+		s->securityAuthProtoLen = sizeof(usmHMAC192SHA256AuthProtocol) / sizeof(oid);
240c49
+		return 0;
b444af
+	}
b444af
+#endif
b444af
+
b444af
+#ifdef HAVE_SNMP_SHA512
b444af
+	if (!strcasecmp(prot, "SHA512")) {
b444af
+		s->securityAuthProto = usmHMAC384SHA512AuthProtocol;
b444af
+		s->securityAuthProtoLen = sizeof(usmHMAC384SHA512AuthProtocol) / sizeof(oid);
240c49
+		return 0;
b444af
+	}
b444af
+#endif
b444af
+
b444af
+	smart_string err = {0};
b444af
+
b444af
+	smart_string_appends(&err, "Authentication protocol must be \"SHA\"");
b444af
+#ifdef HAVE_SNMP_SHA256
b444af
+	smart_string_appends(&err, " or \"SHA256\"");
b444af
+#endif
b444af
+#ifdef HAVE_SNMP_SHA512
b444af
+	smart_string_appends(&err, " or \"SHA512\"");
b444af
+#endif
b444af
+#ifndef DISABLE_MD5
b444af
+	smart_string_appends(&err, " or \"MD5\"");
b444af
+#endif
b444af
+	smart_string_0(&err;;
b444af
+	zend_value_error("%s", err.c);
b444af
+	smart_string_free(&err;;
240c49
+	return -1;
b444af
 }
b444af
 /* }}} */
b444af
 
b444af
diff --git a/ext/snmp/tests/snmp-object-setSecurity_error.phpt b/ext/snmp/tests/snmp-object-setSecurity_error.phpt
b444af
index f8de846492a75..cf4f928837773 100644
b444af
--- a/ext/snmp/tests/snmp-object-setSecurity_error.phpt
b444af
+++ b/ext/snmp/tests/snmp-object-setSecurity_error.phpt
b444af
@@ -59,7 +59,7 @@ var_dump($session->close());
b444af
 --EXPECTF--
b444af
 Security level must be one of "noAuthNoPriv", "authNoPriv", or "authPriv"
b444af
 Security level must be one of "noAuthNoPriv", "authNoPriv", or "authPriv"
b444af
-Authentication protocol must be either "MD5" or "SHA"
b444af
+Authentication protocol must be %s
b444af
 
b444af
 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
b444af
 bool(false)
b444af
diff --git a/ext/snmp/tests/snmp3-error.phpt b/ext/snmp/tests/snmp3-error.phpt
b444af
index 849e363b45058..389800dad6b28 100644
b444af
--- a/ext/snmp/tests/snmp3-error.phpt
b444af
+++ b/ext/snmp/tests/snmp3-error.phpt
b444af
@@ -58,7 +58,7 @@ try {
b444af
 Checking error handling
b444af
 Security level must be one of "noAuthNoPriv", "authNoPriv", or "authPriv"
b444af
 Security level must be one of "noAuthNoPriv", "authNoPriv", or "authPriv"
b444af
-Authentication protocol must be either "MD5" or "SHA"
b444af
+Authentication protocol must be %s
b444af
 
b444af
 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
b444af
 bool(false)