📄 usm.java
字号:
/*_############################################################################
_##
_## SNMP4J - USM.java
_##
_## Copyright (C) 2003-2008 Frank Fock and Jochen Katz (SNMP4J.org)
_##
_## Licensed under the Apache License, Version 2.0 (the "License");
_## you may not use this file except in compliance with the License.
_## You may obtain a copy of the License at
_##
_## http://www.apache.org/licenses/LICENSE-2.0
_##
_## Unless required by applicable law or agreed to in writing, software
_## distributed under the License is distributed on an "AS IS" BASIS,
_## WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
_## See the License for the specific language governing permissions and
_## limitations under the License.
_##
_##########################################################################*/
package org.snmp4j.security;
import java.io.*;
import java.nio.ByteBuffer;
import java.util.Vector;
import org.snmp4j.log.*;
import org.snmp4j.asn1.*;
import org.snmp4j.asn1.BER.*;
import org.snmp4j.event.*;
import org.snmp4j.mp.*;
import org.snmp4j.smi.*;
/**
* The <code>USM</code> class implements the User Based Security Model (USM)
* as defined in RFC3414.
* <p>
* When a user is added or removed from the USM, a <code>UsmUserEvent</code>
* is fired and forwarded to registered listeners.
*
* @author Frank Fock
* @version 1.2
*/
public class USM implements SecurityModel {
private static final int MAXLEN_USMUSERNAME = 32;
private static final LogAdapter logger = LogFactory.getLogger(USM.class);
// Table containing localized and non-localized users
private UsmUserTable userTable;
private UsmTimeTable timeTable;
private OctetString localEngineID;
private boolean engineDiscoveryEnabled = true;
private SecurityProtocols securityProtocols;
private transient Vector usmUserListeners;
private CounterSupport counterSupport;
/**
* Creates a USM with the support for the supplied security protocols.
*
* @param securityProtocols
* the security protocols to support.
* @param localEngineID
* the local engine ID.
* @param engineBoots
* the number of engine boots.
* @since 1.2
*/
public USM(SecurityProtocols securityProtocols,
OctetString localEngineID, int engineBoots) {
this.localEngineID = localEngineID;
timeTable = new UsmTimeTable(localEngineID, engineBoots);
userTable = new UsmUserTable();
this.securityProtocols = securityProtocols;
counterSupport = CounterSupport.getInstance();
}
public int getID() {
return SECURITY_MODEL_USM;
}
/**
* Sets the local engine ID, number of boots, and time after boot.
* @param localEngineID
* the local engine ID.
* @param engineBoots
* the number of engine boots.
* @param engineTime
* the number sendonds since the last boot.
*/
public void setLocalEngine(OctetString localEngineID,
int engineBoots, int engineTime) {
timeTable.removeEntry(this.localEngineID);
this.localEngineID = localEngineID;
timeTable.addEntry(new UsmTimeEntry(localEngineID, engineBoots, engineTime));
}
/**
* Returns the local engine ID.
* @return
* the local engine ID.
* @since 1.2
*/
public OctetString getLocalEngineID() {
return localEngineID;
}
/**
* Sets the number of engine boots.
* @param engineBoots
* the number of engine boots.
*/
public void setEngineBoots(int engineBoots) {
this.timeTable.setEngineBoots(engineBoots);
}
/**
* Returns the number of engine boots counted for the local engine ID.
* @return
* the number of engine boots (zero based).
*/
public int getEngineBoots() {
return this.timeTable.getEngineBoots();
}
/**
* Returns the number of seconds since the value of
* the engineBoots object last changed. When incrementing this object's value
* would cause it to exceed its maximum, engineBoots is incremented as if a
* re-initialization had occurred, and this
* object's value consequently reverts to zero.
*
* @return
* a positive integer value denoting the number of seconds since
* the engineBoots value has been changed.
* @since 1.2
*/
public int getEngineTime() {
return this.timeTable.getEngineTime();
}
public SecurityParameters newSecurityParametersInstance() {
return new UsmSecurityParameters();
}
public SecurityStateReference newSecurityStateReference() {
return new UsmSecurityStateReference();
}
private static byte[] buildMessageBuffer(BERInputStream scopedPDU)
throws IOException
{
// return scopedPDU.getBuffer().array();
scopedPDU.mark(16);
int readLengthBytes = (int)scopedPDU.getPosition();
MutableByte mutableByte = new MutableByte();
int length = BER.decodeHeader(scopedPDU, mutableByte);
readLengthBytes = (int)scopedPDU.getPosition() - readLengthBytes;
byte[] buf = new byte[length + readLengthBytes];
scopedPDU.reset();
int offset = 0;
int avail = scopedPDU.available();
while ((offset < buf.length) && (avail > 0)) {
int read = scopedPDU.read(buf, offset, buf.length - offset);
if (read < 0) {
break;
}
offset += read;
}
return buf;
}
private static byte[] buildWholeMessage(Integer32 snmpVersion,
byte[] scopedPdu,
byte[] globalData,
UsmSecurityParameters
usmSecurityParameters)
throws IOException
{
int length =
snmpVersion.getBERLength() +
globalData.length +
usmSecurityParameters.getBERLength() +
scopedPdu.length;
int totalLength = BER.getBERLengthOfLength(length) + length + 1;
ByteArrayOutputStream os = new ByteArrayOutputStream(totalLength);
BER.encodeHeader(os, BER.SEQUENCE, length);
snmpVersion.encodeBER(os);
os.write(globalData);
usmSecurityParameters.encodeBER(os);
os.write(scopedPdu);
int secParamsPos = 1 + snmpVersion.getBERLength() +
BER.getBERLengthOfLength(length) + globalData.length;
usmSecurityParameters.setSecurityParametersPosition(secParamsPos);
return os.toByteArray();
}
public int generateRequestMessage(int snmpVersion,
byte[] globalData,
int maxMessageSize,
int securityModel,
byte[] securityEngineID,
byte[] securityName,
int securityLevel,
BERInputStream scopedPDU,
SecurityParameters securityParameters,
BEROutputStream wholeMsg) throws IOException {
return generateResponseMessage(snmpVersion,
globalData,
maxMessageSize,
securityModel,
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -