2017-05-19 87 views
0

我正在研究一個項目,希望能夠使用證書或密鑰作爲SNMPv3的身份驗證方法。我們正在使用java庫SNMP4J如何在SNMP4J中使用非對稱密鑰或證書身份驗證?

在我的研究過程中,我發現SNMP使用TLS/DTLS進行消息加密,據推測也用於驗證。 Source 1 | Source 2 | Source 3

縱觀SNMP4J的小文檔,我發現它允許使用TLS證書來加密流量。但我不確定如何使用公鑰/私鑰對完成身份驗證。 TLS Traffic Encryption Example | SNMP4J Documentation

任何幫助,將不勝感激。

回答

0

我能夠使用與示例TLS Traffic Encryption Example中所述類似的方法進行驗證。

所以當人們從例子期待,我可以證實,SNMP4J使用密鑰存儲在Java屬性javax.net.ssl.keystorejavax.net.ssl.keyStorePasswordjavax.net.ssl.trustStorejavax.net.ssl.trustStorePassword設置。

下面是我對示例使其工作的更改。

需要在CertifiedTarget構造函數中設置別名(或文檔中的安全名稱),以便知道使用哪個證書。

CertifiedTarget ct = new CertifiedTarget(new OctetString(alias)); 

必須設置安全級別,否則SNMP代理將發出抱怨和驗證失敗。

ct.setSecurityLevel(SecurityLevel.AUTH_PRIV); 

SecurityCallback主題DN必須完全就是了否則會拒絕所有響應的方式與服務器證書的主題。

securityCallback.addAcceptedSubjectDN("[email protected], CN=snmpagent, OU=Development, O=Net-SNMP, L=Davis, ST=CA, C=US"); 

最後,您必須使用地址註冊服務器公用證書別名(安全名稱)。

securityCallback.addLocalCertMapping(ct.getAddress(), "snmpagent"); 

它一起來看起來像這樣。

// Set java keystore manually 
System.setProperty("javax.net.ssl.keyStore", KEYSTORE_DIR); 
System.setProperty("javax.net.ssl.keyStorePassword", "changeit"); 
System.setProperty("javax.net.ssl.trustStore", KEYSTORE_DIR); 
System.setProperty("javax.net.ssl.trustStorePassword", "changeit"); 

// create the TLS transport mapping: 
TLSTM transport = new TLSTM(); 

// set the security callback (only required for command responder, 
// but also recommended for command generators) - 
// the callback will be configured later: 
DefaultTlsTmSecurityCallback securityCallback = new DefaultTlsTmSecurityCallback(); 
((TLSTM) transport).setSecurityCallback(securityCallback); 
MessageDispatcher md = new MessageDispatcherImpl(); 
// we need MPv3 for TLSTM: 
MPv3 mpv3 = new MPv3(); 
md.addMessageProcessingModel(mpv3); 

Snmp snmp = new Snmp(md, transport); 

// create and initialize the TransportSecurityModel TSM: 
SecurityModels.getInstance().addSecurityModel(new TSM(new OctetString(mpv3.getLocalEngineID()), false)); 

// do not forget to listen for responses: 
snmp.listen(); 

CertifiedTarget ct = new CertifiedTarget(new OctetString("alias")); 
ct.setVersion(SnmpConstants.version3); 
ct.setSecurityModel(SecurityModel.SECURITY_MODEL_TSM); 
ct.setAddress(GenericAddress.parse(myAddress)); 
ct.setSecurityLevel(SecurityLevel.AUTH_PRIV); 

securityCallback.addAcceptedSubjectDN("[email protected], CN=snmpagent, OU=Development, O=Net-SNMP, L=Davis, ST=CA, C=US"); 
securityCallback.addLocalCertMapping(ct.getAddress(), "snmpagentalias"); 

PDU pdu = new ScopedPDU(); 
pdu.add(new VariableBinding(new OID(someOid))); 
pdu.setType(PDU.GET); 

ResponseEvent response = snmp.send(pdu, ct); 

您還必須確保所有證書都正確配置,以便它實際上需要它們。

作爲一個側面提示,在發現我的團隊時,我發現了SNMP4J處理TLS中的幾個錯誤,主要是在傳輸層中。這似乎是一個計時問題(競爭條件可能?),它將獲取SNMP數據,但忽略它。我們能夠通過設置CertifiedTarget超時並且重試非常高來避開它。當我們有更多的信息時,我們將正式報告。

+0

謝謝你的提示。但是,您能否詳細說明此聲明:「最後,您必須使用該地址註冊服務器公鑰證書別名(安全名稱) securityCallback.addLocalCertMapping(ct.getAddress(),」snmpagent「);」具體來說,「服務器公共證書別名」是什麼意思?是否與「CertifiedTarget ct = new CertifiedTarget(new OctetString(alias))」中設置的別名相同?「? – stoneboy

相關問題