Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/com/sun/jmx/snmp/SnmpV3Message.java
38924 views
/*1* Copyright (c) 2001, 2006, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/24package com.sun.jmx.snmp;2526// java imports27//28import java.util.Vector;29import java.util.logging.Level;30import java.net.InetAddress;3132// import debug stuff33//34import static com.sun.jmx.defaults.JmxProperties.SNMP_LOGGER;35import com.sun.jmx.snmp.internal.SnmpMsgProcessingSubSystem;36import com.sun.jmx.snmp.internal.SnmpSecurityModel;37import com.sun.jmx.snmp.internal.SnmpDecryptedPdu;38import com.sun.jmx.snmp.internal.SnmpSecurityCache;3940import com.sun.jmx.snmp.SnmpMsg;41import com.sun.jmx.snmp.SnmpPdu;42import com.sun.jmx.snmp.SnmpStatusException;43import com.sun.jmx.snmp.SnmpTooBigException;44import com.sun.jmx.snmp.SnmpScopedPduBulk;45import com.sun.jmx.snmp.BerException;46import com.sun.jmx.snmp.SnmpScopedPduRequest;47import com.sun.jmx.snmp.BerDecoder;48import com.sun.jmx.snmp.SnmpDefinitions;49import com.sun.jmx.snmp.SnmpEngineId;50import com.sun.jmx.snmp.SnmpScopedPduPacket;51import com.sun.jmx.snmp.BerEncoder;52import com.sun.jmx.snmp.SnmpPduRequestType;53import com.sun.jmx.snmp.SnmpPduBulkType;5455/**56* Is a partially decoded representation of an SNMP V3 packet.57* <P>58* This class can be used when developing customized manager or agent.59* <P>60* The <CODE>SnmpV3Message</CODE> class is directly mapped onto the61* message syntax defined in RFC 2572.62* <BLOCKQUOTE>63* <PRE>64* SNMPv3Message ::= SEQUENCE {65* msgVersion INTEGER ( 0 .. 2147483647 ),66* -- administrative parameters67* msgGlobalData HeaderData,68* -- security model-specific parameters69* -- format defined by Security Model70* msgSecurityParameters OCTET STRING,71* msgData ScopedPduData72* }73* HeaderData ::= SEQUENCE {74* msgID INTEGER (0..2147483647),75* msgMaxSize INTEGER (484..2147483647),76*77* msgFlags OCTET STRING (SIZE(1)),78* -- .... ...1 authFlag79* -- .... ..1. privFlag80* -- .... .1.. reportableFlag81* -- Please observe:82* -- .... ..00 is OK, means noAuthNoPriv83* -- .... ..01 is OK, means authNoPriv84* -- .... ..10 reserved, must NOT be used.85* -- .... ..11 is OK, means authPriv86*87* msgSecurityModel INTEGER (1..2147483647)88* }89* </BLOCKQUOTE>90* </PRE>91* <p><b>This API is a Sun Microsystems internal API and is subject92* to change without notice.</b></p>93* @since 1.594*/95public class SnmpV3Message extends SnmpMsg {9697/**98* Message identifier.99*/100public int msgId = 0;101102/**103* Message max size the pdu sender can deal with.104*/105public int msgMaxSize = 0;106/**107* Message flags. Reportable flag and security level.</P>108*<PRE>109* -- .... ...1 authFlag110* -- .... ..1. privFlag111* -- .... .1.. reportableFlag112* -- Please observe:113* -- .... ..00 is OK, means noAuthNoPriv114* -- .... ..01 is OK, means authNoPriv115* -- .... ..10 reserved, must NOT be used.116* -- .... ..11 is OK, means authPriv117*</PRE>118*/119public byte msgFlags = 0;120/**121* The security model the security sub system MUST use in order to deal with this pdu (eg: User based Security Model Id = 3).122*/123public int msgSecurityModel = 0;124/**125* The unmarshalled security parameters.126*/127public byte[] msgSecurityParameters = null;128/**129* The context engine Id in which the pdu must be handled (Generaly the local engine Id).130*/131public byte[] contextEngineId = null;132/**133* The context name in which the OID has to be interpreted.134*/135public byte[] contextName = null;136/** The encrypted form of the scoped pdu (Only relevant when dealing with privacy).137*/138public byte[] encryptedPdu = null;139140/**141* Constructor.142*143*/144public SnmpV3Message() {145}146/**147* Encodes this message and puts the result in the specified byte array.148* For internal use only.149*150* @param outputBytes An array to receive the resulting encoding.151*152* @exception ArrayIndexOutOfBoundsException If the result does not fit153* into the specified array.154*/155public int encodeMessage(byte[] outputBytes)156throws SnmpTooBigException {157int encodingLength = 0;158if (SNMP_LOGGER.isLoggable(Level.FINER)) {159SNMP_LOGGER.logp(Level.FINER, SnmpV3Message.class.getName(),160"encodeMessage",161"Can't encode directly V3Message! Need a SecuritySubSystem");162}163throw new IllegalArgumentException("Can't encode");164}165166/**167* Decodes the specified bytes and initializes this message.168* For internal use only.169*170* @param inputBytes The bytes to be decoded.171*172* @exception SnmpStatusException If the specified bytes are not a valid encoding.173*/174public void decodeMessage(byte[] inputBytes, int byteCount)175throws SnmpStatusException {176177try {178BerDecoder bdec = new BerDecoder(inputBytes);179bdec.openSequence();180version = bdec.fetchInteger();181bdec.openSequence();182msgId = bdec.fetchInteger();183msgMaxSize = bdec.fetchInteger();184msgFlags = bdec.fetchOctetString()[0];185msgSecurityModel =bdec.fetchInteger();186bdec.closeSequence();187msgSecurityParameters = bdec.fetchOctetString();188if( (msgFlags & SnmpDefinitions.privMask) == 0 ) {189bdec.openSequence();190contextEngineId = bdec.fetchOctetString();191contextName = bdec.fetchOctetString();192data = bdec.fetchAny();193dataLength = data.length;194bdec.closeSequence();195}196else {197encryptedPdu = bdec.fetchOctetString();198}199bdec.closeSequence() ;200}201catch(BerException x) {202x.printStackTrace();203throw new SnmpStatusException("Invalid encoding") ;204}205206if (SNMP_LOGGER.isLoggable(Level.FINER)) {207final StringBuilder strb = new StringBuilder()208.append("Unmarshalled message : \n")209.append("version : ").append(version)210.append("\n")211.append("msgId : ").append(msgId)212.append("\n")213.append("msgMaxSize : ").append(msgMaxSize)214.append("\n")215.append("msgFlags : ").append(msgFlags)216.append("\n")217.append("msgSecurityModel : ").append(msgSecurityModel)218.append("\n")219.append("contextEngineId : ").append(contextEngineId == null ? null :220SnmpEngineId.createEngineId(contextEngineId))221.append("\n")222.append("contextName : ").append(contextName)223.append("\n")224.append("data : ").append(data)225.append("\n")226.append("dat len : ").append((data == null) ? 0 : data.length)227.append("\n")228.append("encryptedPdu : ").append(encryptedPdu)229.append("\n");230SNMP_LOGGER.logp(Level.FINER, SnmpV3Message.class.getName(),231"decodeMessage", strb.toString());232}233}234235/**236* Returns the associated request Id.237* @param data The flat message.238* @return The request Id.239*/240public int getRequestId(byte[] data) throws SnmpStatusException {241BerDecoder bdec = null;242int msgId = 0;243try {244bdec = new BerDecoder(data);245bdec.openSequence();246bdec.fetchInteger();247bdec.openSequence();248msgId = bdec.fetchInteger();249}catch(BerException x) {250throw new SnmpStatusException("Invalid encoding") ;251}252try {253bdec.closeSequence();254}255catch(BerException x) {256}257258return msgId;259}260261/**262* Initializes this message with the specified <CODE>pdu</CODE>.263* <P>264* This method initializes the data field with an array of265* <CODE>maxDataLength</CODE> bytes. It encodes the <CODE>pdu</CODE>.266* The resulting encoding is stored in the data field267* and the length of the encoding is stored in <CODE>dataLength</CODE>.268* <p>269* If the encoding length exceeds <CODE>maxDataLength</CODE>,270* the method throws an exception.271*272* @param p The PDU to be encoded.273* @param maxDataLength The maximum length permitted for the data field.274*275* @exception SnmpStatusException If the specified <CODE>pdu</CODE>276* is not valid.277* @exception SnmpTooBigException If the resulting encoding does not fit278* into <CODE>maxDataLength</CODE> bytes.279* @exception ArrayIndexOutOfBoundsException If the encoding exceeds280* <CODE>maxDataLength</CODE>.281*/282public void encodeSnmpPdu(SnmpPdu p,283int maxDataLength)284throws SnmpStatusException, SnmpTooBigException {285286SnmpScopedPduPacket pdu = (SnmpScopedPduPacket) p;287288if (SNMP_LOGGER.isLoggable(Level.FINER)) {289final StringBuilder strb = new StringBuilder()290.append("PDU to marshall: \n")291.append("security parameters : ").append(pdu.securityParameters)292.append("\n")293.append("type : ").append(pdu.type)294.append("\n")295.append("version : ").append(pdu.version)296.append("\n")297.append("requestId : ").append(pdu.requestId)298.append("\n")299.append("msgId : ").append(pdu.msgId)300.append("\n")301.append("msgMaxSize : ").append(pdu.msgMaxSize)302.append("\n")303.append("msgFlags : ").append(pdu.msgFlags)304.append("\n")305.append("msgSecurityModel : ").append(pdu.msgSecurityModel)306.append("\n")307.append("contextEngineId : ").append(pdu.contextEngineId)308.append("\n")309.append("contextName : ").append(pdu.contextName)310.append("\n");311SNMP_LOGGER.logp(Level.FINER, SnmpV3Message.class.getName(),312"encodeSnmpPdu", strb.toString());313}314315version = pdu.version;316address = pdu.address;317port = pdu.port;318msgId = pdu.msgId;319msgMaxSize = pdu.msgMaxSize;320msgFlags = pdu.msgFlags;321msgSecurityModel = pdu.msgSecurityModel;322323contextEngineId = pdu.contextEngineId;324contextName = pdu.contextName;325326securityParameters = pdu.securityParameters;327328//329// Allocate the array to receive the encoding.330//331data = new byte[maxDataLength];332333//334// Encode the pdu335// Reminder: BerEncoder does backward encoding !336//337338try {339BerEncoder benc = new BerEncoder(data) ;340benc.openSequence() ;341encodeVarBindList(benc, pdu.varBindList) ;342343switch(pdu.type) {344345case pduGetRequestPdu :346case pduGetNextRequestPdu :347case pduInformRequestPdu :348case pduGetResponsePdu :349case pduSetRequestPdu :350case pduV2TrapPdu :351case pduReportPdu :352SnmpPduRequestType reqPdu = (SnmpPduRequestType) pdu;353benc.putInteger(reqPdu.getErrorIndex());354benc.putInteger(reqPdu.getErrorStatus());355benc.putInteger(pdu.requestId);356break;357358case pduGetBulkRequestPdu :359SnmpPduBulkType bulkPdu = (SnmpPduBulkType) pdu;360benc.putInteger(bulkPdu.getMaxRepetitions());361benc.putInteger(bulkPdu.getNonRepeaters());362benc.putInteger(pdu.requestId);363break ;364365default:366throw new SnmpStatusException("Invalid pdu type " + String.valueOf(pdu.type)) ;367}368benc.closeSequence(pdu.type) ;369dataLength = benc.trim() ;370}371catch(ArrayIndexOutOfBoundsException x) {372throw new SnmpTooBigException() ;373}374}375376377/**378* Gets the PDU encoded in this message.379* <P>380* This method decodes the data field and returns the resulting PDU.381*382* @return The resulting PDU.383* @exception SnmpStatusException If the encoding is not valid.384*/385386public SnmpPdu decodeSnmpPdu()387throws SnmpStatusException {388389SnmpScopedPduPacket pdu = null;390391BerDecoder bdec = new BerDecoder(data) ;392try {393int type = bdec.getTag() ;394bdec.openSequence(type) ;395switch(type) {396397case pduGetRequestPdu :398case pduGetNextRequestPdu :399case pduInformRequestPdu :400case pduGetResponsePdu :401case pduSetRequestPdu :402case pduV2TrapPdu :403case pduReportPdu :404SnmpScopedPduRequest reqPdu = new SnmpScopedPduRequest() ;405reqPdu.requestId = bdec.fetchInteger() ;406reqPdu.setErrorStatus(bdec.fetchInteger());407reqPdu.setErrorIndex(bdec.fetchInteger());408pdu = reqPdu ;409break ;410411case pduGetBulkRequestPdu :412SnmpScopedPduBulk bulkPdu = new SnmpScopedPduBulk() ;413bulkPdu.requestId = bdec.fetchInteger() ;414bulkPdu.setNonRepeaters(bdec.fetchInteger());415bulkPdu.setMaxRepetitions(bdec.fetchInteger());416pdu = bulkPdu ;417break ;418default:419throw new SnmpStatusException(snmpRspWrongEncoding) ;420}421pdu.type = type;422pdu.varBindList = decodeVarBindList(bdec);423bdec.closeSequence() ;424} catch(BerException e) {425if (SNMP_LOGGER.isLoggable(Level.FINEST)) {426SNMP_LOGGER.logp(Level.FINEST, SnmpV3Message.class.getName(),427"decodeSnmpPdu", "BerException", e);428}429throw new SnmpStatusException(snmpRspWrongEncoding);430}431432//433// The easy work.434//435pdu.address = address;436pdu.port = port;437pdu.msgFlags = msgFlags;438pdu.version = version;439pdu.msgId = msgId;440pdu.msgMaxSize = msgMaxSize;441pdu.msgSecurityModel = msgSecurityModel;442pdu.contextEngineId = contextEngineId;443pdu.contextName = contextName;444445pdu.securityParameters = securityParameters;446447if (SNMP_LOGGER.isLoggable(Level.FINER)) {448final StringBuilder strb = new StringBuilder()449.append("Unmarshalled PDU : \n")450.append("type : ").append(pdu.type)451.append("\n")452.append("version : ").append(pdu.version)453.append("\n")454.append("requestId : ").append(pdu.requestId)455.append("\n")456.append("msgId : ").append(pdu.msgId)457.append("\n")458.append("msgMaxSize : ").append(pdu.msgMaxSize)459.append("\n")460.append("msgFlags : ").append(pdu.msgFlags)461.append("\n")462.append("msgSecurityModel : ").append(pdu.msgSecurityModel)463.append("\n")464.append("contextEngineId : ").append(pdu.contextEngineId)465.append("\n")466.append("contextName : ").append(pdu.contextName)467.append("\n");468SNMP_LOGGER.logp(Level.FINER, SnmpV3Message.class.getName(),469"decodeSnmpPdu", strb.toString());470}471return pdu ;472}473474/**475* Dumps this message in a string.476*477* @return The string containing the dump.478*/479public String printMessage() {480StringBuffer sb = new StringBuffer();481sb.append("msgId : " + msgId + "\n");482sb.append("msgMaxSize : " + msgMaxSize + "\n");483sb.append("msgFlags : " + msgFlags + "\n");484sb.append("msgSecurityModel : " + msgSecurityModel + "\n");485486if (contextEngineId == null) {487sb.append("contextEngineId : null");488}489else {490sb.append("contextEngineId : {\n");491sb.append(dumpHexBuffer(contextEngineId,4920,493contextEngineId.length));494sb.append("\n}\n");495}496497if (contextName == null) {498sb.append("contextName : null");499}500else {501sb.append("contextName : {\n");502sb.append(dumpHexBuffer(contextName,5030,504contextName.length));505sb.append("\n}\n");506}507return sb.append(super.printMessage()).toString();508}509510}511512513