title: SNMP4J Client gives "Unsupported security level"
body: <p>I'm about to implement a Client/Agent setup in order to learn using 
  SNMP. I want to establish a secure and encrypted communication using SNMPv3 and <code>authPriv</code> as security level.<br>
  Here's my setup:</p>
  
  <p><strong>Agent</strong><br>
  The Agent is running on Ubuntu (within a Docker image). To set it up, I followed <a href="https://www.incredigeek.com/home/setup-snmp-v3-on-debian-or-ubuntu/" rel="nofollow noreferrer">this tutorial</a>.<br>
  My <code>/etc/snmp/snmpd.conf</code> looks like this:</p>
  
  <pre><code>agentAddress udp:161
  ...
  createUser authOnlyUser MD5 "test1234"
  createUser authPrivUser SHA "test1234" DES
  createUser internalUser MD5 "test1234"
  ...
  rouser authOnlyUser
  rwuser authPrivUser authPriv
  </code></pre>
  
  <p>SNMP is running on port <code>161</code>, which is forwarded to port <code>1025</code> by Docker.</p>
  
  <p><strong>Client</strong><br>
  I could ensure that my Agent is working properly using</p>
  
  <ol>
  <li><code>snmpget</code> on the same machine (Ubuntu, within Docker):<br>
  <code>snmpget -v3 -a SHA -A test1234 -X test1234 -l authPriv -u authPrivUser localhost 1.3.6.1.2.1.1.1.0</code></li>
  <li><code>snmpget</code> from a remote machine (Debian):<br>
  <code>snmpget -v3 -u authPrivUser -X test1234 -A test1234 -l authPriv -x DES -a SHA &lt;HOST-IP&gt;:1025 1.3.6.1.2.1.1.1.0</code></li>
  <li>Using <a href="https://www.paessler.com/tools/snmptester" rel="nofollow noreferrer">SNMP-Tester</a>.</li>
  </ol>
  
  <p>In all three cases I get the expected value for the given OID:<br>
  <code>Linux 9b98a8808b1a 4.9.125-linuxkit #1 SMP Fri Sep 7 08:20:28 UTC 2018 x86_64</code>.</p>
  
  <p>Now I wanted to implement a Client with Java, using the <a href="https://mvnrepository.com/artifact/org.snmp4j/snmp4j/3.2.2" rel="nofollow noreferrer">latest version 3.2.2 of snmp4j</a>. This is what I have implemented:</p>
  
  <pre class="lang-java prettyprint-override"><code>public class SNMPTestClient {
  
      public static void main(String[] args) throws Exception {
          // enable logging
          ConsoleLogFactory consoleLogFactory = new ConsoleLogFactory();
          consoleLogFactory.getRootLogger().setLogLevel(LogLevel.DEBUG);
          LogFactory.setLogFactory(consoleLogFactory);
  
          TransportMapping&lt;? extends Address&gt; transport = new DefaultUdpTransportMapping();
          Snmp snmp = new Snmp(transport);
  
          // create and add user security model
          OctetString localEngineId = new OctetString(MPv3.createLocalEngineID());
          USM usm = new USM(SecurityProtocols.getInstance(), localEngineId, 0);
          SecurityModels.getInstance().addSecurityModel(usm);
  
          // user credentials
          OctetString securityName = new OctetString("authPrivUser");
          OctetString authPassphrase = new OctetString("test1234");
          OctetString privPassphrase = new OctetString("test1234");
          OID authProtocol = AuthSHA.ID;
          OID privProtocol = PrivDES.ID;
  
          UsmUser usmUser = new UsmUser(securityName, authProtocol, authPassphrase, privProtocol, privPassphrase);
          snmp.getUSM().addUser(securityName, usmUser);
  
          // create target
          UserTarget target = new UserTarget();
          target.setSecurityLevel(SecurityLevel.AUTH_PRIV);
          target.setSecurityName(securityName);
          target.setAddress(GenericAddress.parse("udp:127.0.0.1/1025"));
          target.setVersion(SnmpConstants.version3);
          target.setRetries(3);
          target.setTimeout(10000);
  
          transport.listen();
  
          ScopedPDU pdu = new ScopedPDU();
          pdu.add(new VariableBinding(new OID("1.3.6.1.2.1.1.1.0")));
          pdu.setType(PDU.GET);
          ResponseEvent event = snmp.send(pdu, target);
          if (event != null) {
              System.out.println(event.getUserObject());
              System.out.println(event.getError());
              System.out.println(event.getPeerAddress());
              PDU responsePDU = event.getResponse();
              System.out.println(responsePDU.getErrorStatus());
              System.out.println(responsePDU.getErrorIndex());
              if (responsePDU.getErrorStatus() == PDU.noError) {
                  for (int k = 0; k &lt; responsePDU.size(); k++) {
                      VariableBinding vb = responsePDU.get(k);
                      if (vb != null) {
                          System.out.println(vb.getOid() + "-" + vb.getVariable().toString());
                      }
                  }
              } else {
                  System.out.println("SNMP Error:" + responsePDU.getErrorStatusText());
              }
          } else {
              System.out.println("SNMP send unsuccessful.");
          }
      }
  }
  </code></pre>
  
  <p>As you can see I'm using the same credentials and protocols like mentioned above. But when I run it I receive the following Exception:<br>
  <code>org.snmp4j.MessageException: Message processing model 3 returned error: Unsupported security level</code></p>
  
  <p>What am I doing wrong here? As the user <code>authPrivUser</code> is configured to use the <code>authPriv</code> security level on the Agent's side and I'm also setting the level to <code>AUTH_PRIV</code> within the client and pass the correct credentials, I don't know why this exception is thrown.</p>
  
  <p>Thanks in advance for any support and hints!</p>
  
  <p><strong>EDIT</strong><br>
  I figured out that the code I posted above <em>does</em> work using SNMP4J v2.7.0. Any version >=3.0.0 throws error <code>Unsupported security level</code>. Unfortunately, using an old version from Nov. 2018 cannot be the solution...</p>