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