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