PageRenderTime 58ms CodeModel.GetById 31ms RepoModel.GetById 0ms app.codeStats 1ms

/projects/jre-1.6.0/src/com/sun/jmx/snmp/SnmpV3Message.java

https://gitlab.com/essere.lab.public/qualitas.class-corpus
Java | 490 lines | 283 code | 48 blank | 159 comment | 17 complexity | 6a1913f8635ffa94728bb96e9d38a0cc MD5 | raw file
  1. /*
  2. * @(#)file SnmpV3Message.java
  3. * @(#)author Sun Microsystems, Inc.
  4. * @(#)version 4.12
  5. * @(#)date 01/01/17
  6. *
  7. * Copyright (c) 2006, Oracle and/or its affiliates. All rights reserved.
  8. * ORACLE PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  9. *
  10. */
  11. package com.sun.jmx.snmp;
  12. // java imports
  13. //
  14. import java.util.Vector;
  15. import java.net.InetAddress;
  16. // import debug stuff
  17. //
  18. import com.sun.jmx.trace.Trace;
  19. import com.sun.jmx.snmp.internal.SnmpMsgProcessingSubSystem;
  20. import com.sun.jmx.snmp.internal.SnmpSecurityModel;
  21. import com.sun.jmx.snmp.internal.SnmpDecryptedPdu;
  22. import com.sun.jmx.snmp.internal.SnmpSecurityCache;
  23. import com.sun.jmx.snmp.SnmpMsg;
  24. import com.sun.jmx.snmp.SnmpPdu;
  25. import com.sun.jmx.snmp.SnmpStatusException;
  26. import com.sun.jmx.snmp.SnmpTooBigException;
  27. import com.sun.jmx.snmp.SnmpScopedPduBulk;
  28. import com.sun.jmx.snmp.BerException;
  29. import com.sun.jmx.snmp.SnmpScopedPduRequest;
  30. import com.sun.jmx.snmp.BerDecoder;
  31. import com.sun.jmx.snmp.SnmpDefinitions;
  32. import com.sun.jmx.snmp.SnmpEngineId;
  33. import com.sun.jmx.snmp.SnmpScopedPduPacket;
  34. import com.sun.jmx.snmp.BerEncoder;
  35. import com.sun.jmx.snmp.SnmpPduRequestType;
  36. import com.sun.jmx.snmp.SnmpPduBulkType;
  37. /**
  38. * Is a partially decoded representation of an SNMP V3 packet.
  39. * <P>
  40. * This class can be used when developing customized manager or agent.
  41. * <P>
  42. * The <CODE>SnmpV3Message</CODE> class is directly mapped onto the
  43. * message syntax defined in RFC 2572.
  44. * <BLOCKQUOTE>
  45. * <PRE>
  46. * SNMPv3Message ::= SEQUENCE {
  47. * msgVersion INTEGER ( 0 .. 2147483647 ),
  48. * -- administrative parameters
  49. * msgGlobalData HeaderData,
  50. * -- security model-specific parameters
  51. * -- format defined by Security Model
  52. * msgSecurityParameters OCTET STRING,
  53. * msgData ScopedPduData
  54. * }
  55. * HeaderData ::= SEQUENCE {
  56. * msgID INTEGER (0..2147483647),
  57. * msgMaxSize INTEGER (484..2147483647),
  58. *
  59. * msgFlags OCTET STRING (SIZE(1)),
  60. * -- .... ...1 authFlag
  61. * -- .... ..1. privFlag
  62. * -- .... .1.. reportableFlag
  63. * -- Please observe:
  64. * -- .... ..00 is OK, means noAuthNoPriv
  65. * -- .... ..01 is OK, means authNoPriv
  66. * -- .... ..10 reserved, must NOT be used.
  67. * -- .... ..11 is OK, means authPriv
  68. *
  69. * msgSecurityModel INTEGER (1..2147483647)
  70. * }
  71. * </BLOCKQUOTE>
  72. * </PRE>
  73. * <p><b>This API is a Sun Microsystems internal API and is subject
  74. * to change without notice.</b></p>
  75. * @since 1.5
  76. */
  77. public class SnmpV3Message extends SnmpMsg {
  78. /**
  79. * Message identifier.
  80. */
  81. public int msgId = 0;
  82. /**
  83. * Message max size the pdu sender can deal with.
  84. */
  85. public int msgMaxSize = 0;
  86. /**
  87. * Message flags. Reportable flag and security level.</P>
  88. *<PRE>
  89. * -- .... ...1 authFlag
  90. * -- .... ..1. privFlag
  91. * -- .... .1.. reportableFlag
  92. * -- Please observe:
  93. * -- .... ..00 is OK, means noAuthNoPriv
  94. * -- .... ..01 is OK, means authNoPriv
  95. * -- .... ..10 reserved, must NOT be used.
  96. * -- .... ..11 is OK, means authPriv
  97. *</PRE>
  98. */
  99. public byte msgFlags = 0;
  100. /**
  101. * The security model the security sub system MUST use in order to deal with this pdu (eg: User based Security Model Id = 3).
  102. */
  103. public int msgSecurityModel = 0;
  104. /**
  105. * The unmarshalled security parameters.
  106. */
  107. public byte[] msgSecurityParameters = null;
  108. /**
  109. * The context engine Id in which the pdu must be handled (Generaly the local engine Id).
  110. */
  111. public byte[] contextEngineId = null;
  112. /**
  113. * The context name in which the OID has to be interpreted.
  114. */
  115. public byte[] contextName = null;
  116. /** The encrypted form of the scoped pdu (Only relevant when dealing with privacy).
  117. */
  118. public byte[] encryptedPdu = null;
  119. /**
  120. * Constructor.
  121. *
  122. */
  123. public SnmpV3Message() {
  124. }
  125. /**
  126. * Encodes this message and puts the result in the specified byte array.
  127. * For internal use only.
  128. *
  129. * @param outputBytes An array to receive the resulting encoding.
  130. *
  131. * @exception ArrayIndexOutOfBoundsException If the result does not fit
  132. * into the specified array.
  133. */
  134. public int encodeMessage(byte[] outputBytes)
  135. throws SnmpTooBigException {
  136. int encodingLength = 0;
  137. if(isTraceOn()) {
  138. trace("encodeMessage", "Can't encode directly V3Message!!!!! Need a SecuritySubSystem");
  139. }
  140. throw new IllegalArgumentException("Can't encode");
  141. }
  142. /**
  143. * Decodes the specified bytes and initializes this message.
  144. * For internal use only.
  145. *
  146. * @param inputBytes The bytes to be decoded.
  147. *
  148. * @exception SnmpStatusException If the specified bytes are not a valid encoding.
  149. */
  150. public void decodeMessage(byte[] inputBytes, int byteCount)
  151. throws SnmpStatusException {
  152. try {
  153. BerDecoder bdec = new BerDecoder(inputBytes);
  154. bdec.openSequence();
  155. version = bdec.fetchInteger();
  156. bdec.openSequence();
  157. msgId = bdec.fetchInteger();
  158. msgMaxSize = bdec.fetchInteger();
  159. msgFlags = bdec.fetchOctetString()[0];
  160. msgSecurityModel =bdec.fetchInteger();
  161. bdec.closeSequence();
  162. msgSecurityParameters = bdec.fetchOctetString();
  163. if( (msgFlags & SnmpDefinitions.privMask) == 0 ) {
  164. bdec.openSequence();
  165. contextEngineId = bdec.fetchOctetString();
  166. contextName = bdec.fetchOctetString();
  167. data = bdec.fetchAny();
  168. dataLength = data.length;
  169. bdec.closeSequence();
  170. }
  171. else {
  172. encryptedPdu = bdec.fetchOctetString();
  173. }
  174. bdec.closeSequence() ;
  175. }
  176. catch(BerException x) {
  177. x.printStackTrace();
  178. throw new SnmpStatusException("Invalid encoding") ;
  179. }
  180. if(isTraceOn()) {
  181. trace("decodeMessage", "Unmarshalled message : \n" +
  182. "version :" + version + "\n" +
  183. "msgId :" + msgId + "\n" +
  184. "msgMaxSize :" + msgMaxSize + "\n" +
  185. "msgFlags :" + msgFlags + "\n" +
  186. "msgSecurityModel :" + msgSecurityModel + "\n" +
  187. "contextEngineId :" + (contextEngineId == null ? null : SnmpEngineId.createEngineId(contextEngineId)) + "\n" +
  188. "contextName :" + (contextName == null ? null : new String(contextName)) + "\n" +
  189. "data :" + data + "\n" +
  190. "dat len :" + ((data == null) ? 0 : data.length) + "\n" +
  191. "encryptedPdu :" + encryptedPdu + "\n");
  192. }
  193. }
  194. /**
  195. * Returns the associated request Id.
  196. * @param data The flat message.
  197. * @return The request Id.
  198. */
  199. public int getRequestId(byte[] data) throws SnmpStatusException {
  200. BerDecoder bdec = null;
  201. int msgId = 0;
  202. try {
  203. bdec = new BerDecoder(data);
  204. bdec.openSequence();
  205. bdec.fetchInteger();
  206. bdec.openSequence();
  207. msgId = bdec.fetchInteger();
  208. }catch(BerException x) {
  209. throw new SnmpStatusException("Invalid encoding") ;
  210. }
  211. try {
  212. bdec.closeSequence();
  213. }
  214. catch(BerException x) {
  215. }
  216. return msgId;
  217. }
  218. /**
  219. * Initializes this message with the specified <CODE>pdu</CODE>.
  220. * <P>
  221. * This method initializes the data field with an array of
  222. * <CODE>maxDataLength</CODE> bytes. It encodes the <CODE>pdu</CODE>.
  223. * The resulting encoding is stored in the data field
  224. * and the length of the encoding is stored in <CODE>dataLength</CODE>.
  225. * <p>
  226. * If the encoding length exceeds <CODE>maxDataLength</CODE>,
  227. * the method throws an exception.
  228. *
  229. * @param p The PDU to be encoded.
  230. * @param maxDataLength The maximum length permitted for the data field.
  231. *
  232. * @exception SnmpStatusException If the specified <CODE>pdu</CODE>
  233. * is not valid.
  234. * @exception SnmpTooBigException If the resulting encoding does not fit
  235. * into <CODE>maxDataLength</CODE> bytes.
  236. * @exception ArrayIndexOutOfBoundsException If the encoding exceeds
  237. * <CODE>maxDataLength</CODE>.
  238. */
  239. public void encodeSnmpPdu(SnmpPdu p,
  240. int maxDataLength)
  241. throws SnmpStatusException, SnmpTooBigException {
  242. SnmpScopedPduPacket pdu = (SnmpScopedPduPacket) p;
  243. if(isTraceOn()) {
  244. trace("encodeSnmpPdu", "Pdu to marshall: \n" +
  245. "security parameters : " + pdu.securityParameters + "\n" +
  246. "type :" + pdu.type + "\n" +
  247. "version :" + pdu.version + "\n" +
  248. "requestId :" + pdu.requestId + "\n" +
  249. "msgId :" + pdu.msgId + "\n" +
  250. "msgMaxSize :" + pdu.msgMaxSize + "\n" +
  251. "msgFlags :" + pdu.msgFlags + "\n" +
  252. "msgSecurityModel :" + pdu.msgSecurityModel + "\n" +
  253. "contextEngineId :" + pdu.contextEngineId + "\n" +
  254. "contextName :" + pdu.contextName + "\n");
  255. }
  256. version = pdu.version;
  257. address = pdu.address;
  258. port = pdu.port;
  259. msgId = pdu.msgId;
  260. msgMaxSize = pdu.msgMaxSize;
  261. msgFlags = pdu.msgFlags;
  262. msgSecurityModel = pdu.msgSecurityModel;
  263. contextEngineId = pdu.contextEngineId;
  264. contextName = pdu.contextName;
  265. securityParameters = pdu.securityParameters;
  266. //
  267. // Allocate the array to receive the encoding.
  268. //
  269. data = new byte[maxDataLength];
  270. //
  271. // Encode the pdu
  272. // Reminder: BerEncoder does backward encoding !
  273. //
  274. try {
  275. BerEncoder benc = new BerEncoder(data) ;
  276. benc.openSequence() ;
  277. encodeVarBindList(benc, pdu.varBindList) ;
  278. switch(pdu.type) {
  279. case pduGetRequestPdu :
  280. case pduGetNextRequestPdu :
  281. case pduInformRequestPdu :
  282. case pduGetResponsePdu :
  283. case pduSetRequestPdu :
  284. case pduV2TrapPdu :
  285. case pduReportPdu :
  286. SnmpPduRequestType reqPdu = (SnmpPduRequestType) pdu;
  287. benc.putInteger(reqPdu.getErrorIndex());
  288. benc.putInteger(reqPdu.getErrorStatus());
  289. benc.putInteger(pdu.requestId);
  290. break;
  291. case pduGetBulkRequestPdu :
  292. SnmpPduBulkType bulkPdu = (SnmpPduBulkType) pdu;
  293. benc.putInteger(bulkPdu.getMaxRepetitions());
  294. benc.putInteger(bulkPdu.getNonRepeaters());
  295. benc.putInteger(pdu.requestId);
  296. break ;
  297. default:
  298. throw new SnmpStatusException("Invalid pdu type " + String.valueOf(pdu.type)) ;
  299. }
  300. benc.closeSequence(pdu.type) ;
  301. dataLength = benc.trim() ;
  302. }
  303. catch(ArrayIndexOutOfBoundsException x) {
  304. throw new SnmpTooBigException() ;
  305. }
  306. }
  307. /**
  308. * Gets the PDU encoded in this message.
  309. * <P>
  310. * This method decodes the data field and returns the resulting PDU.
  311. *
  312. * @return The resulting PDU.
  313. * @exception SnmpStatusException If the encoding is not valid.
  314. */
  315. public SnmpPdu decodeSnmpPdu()
  316. throws SnmpStatusException {
  317. SnmpScopedPduPacket pdu = null;
  318. BerDecoder bdec = new BerDecoder(data) ;
  319. try {
  320. int type = bdec.getTag() ;
  321. bdec.openSequence(type) ;
  322. switch(type) {
  323. case pduGetRequestPdu :
  324. case pduGetNextRequestPdu :
  325. case pduInformRequestPdu :
  326. case pduGetResponsePdu :
  327. case pduSetRequestPdu :
  328. case pduV2TrapPdu :
  329. case pduReportPdu :
  330. SnmpScopedPduRequest reqPdu = new SnmpScopedPduRequest() ;
  331. reqPdu.requestId = bdec.fetchInteger() ;
  332. reqPdu.setErrorStatus(bdec.fetchInteger());
  333. reqPdu.setErrorIndex(bdec.fetchInteger());
  334. pdu = reqPdu ;
  335. break ;
  336. case pduGetBulkRequestPdu :
  337. SnmpScopedPduBulk bulkPdu = new SnmpScopedPduBulk() ;
  338. bulkPdu.requestId = bdec.fetchInteger() ;
  339. bulkPdu.setNonRepeaters(bdec.fetchInteger());
  340. bulkPdu.setMaxRepetitions(bdec.fetchInteger());
  341. pdu = bulkPdu ;
  342. break ;
  343. default:
  344. throw new SnmpStatusException(snmpRspWrongEncoding) ;
  345. }
  346. pdu.type = type;
  347. pdu.varBindList = decodeVarBindList(bdec);
  348. bdec.closeSequence() ;
  349. } catch(BerException e) {
  350. if (isDebugOn()) {
  351. debug("decodeSnmpPdu", e);
  352. }
  353. throw new SnmpStatusException(snmpRspWrongEncoding);
  354. }
  355. //
  356. // The easy work.
  357. //
  358. pdu.address = address;
  359. pdu.port = port;
  360. pdu.msgFlags = msgFlags;
  361. pdu.version = version;
  362. pdu.msgId = msgId;
  363. pdu.msgMaxSize = msgMaxSize;
  364. pdu.msgSecurityModel = msgSecurityModel;
  365. pdu.contextEngineId = contextEngineId;
  366. pdu.contextName = contextName;
  367. pdu.securityParameters = securityParameters;
  368. if(isTraceOn()) {
  369. trace("decodeSnmpPdu", "Unmarshalled pdu : \n" +
  370. "type :" + pdu.type + "\n" +
  371. "version :" + pdu.version + "\n" +
  372. "requestId :" + pdu.requestId + "\n" +
  373. "msgId :" + pdu.msgId + "\n" +
  374. "msgMaxSize :" + pdu.msgMaxSize + "\n" +
  375. "msgFlags :" + pdu.msgFlags + "\n" +
  376. "msgSecurityModel :" + pdu.msgSecurityModel + "\n" +
  377. "contextEngineId :" + pdu.contextEngineId + "\n" +
  378. "contextName :" + pdu.contextName + "\n");
  379. }
  380. return pdu ;
  381. }
  382. /**
  383. * Dumps this message in a string.
  384. *
  385. * @return The string containing the dump.
  386. */
  387. public String printMessage() {
  388. StringBuffer sb = new StringBuffer();
  389. sb.append("msgId : " + msgId + "\n");
  390. sb.append("msgMaxSize : " + msgMaxSize + "\n");
  391. sb.append("msgFlags : " + msgFlags + "\n");
  392. sb.append("msgSecurityModel : " + msgSecurityModel + "\n");
  393. if (contextEngineId == null) {
  394. sb.append("contextEngineId : null");
  395. }
  396. else {
  397. sb.append("contextEngineId : {\n");
  398. sb.append(dumpHexBuffer(contextEngineId,
  399. 0,
  400. contextEngineId.length));
  401. sb.append("\n}\n");
  402. }
  403. if (contextName == null) {
  404. sb.append("contextName : null");
  405. }
  406. else {
  407. sb.append("contextName : {\n");
  408. sb.append(dumpHexBuffer(contextName,
  409. 0,
  410. contextName.length));
  411. sb.append("\n}\n");
  412. }
  413. return sb.append(super.printMessage()).toString();
  414. }
  415. // TRACES & DEBUG
  416. //---------------
  417. boolean isTraceOn() {
  418. return Trace.isSelected(Trace.LEVEL_TRACE, Trace.INFO_SNMP);
  419. }
  420. void trace(String clz, String func, String info) {
  421. Trace.send(Trace.LEVEL_TRACE, Trace.INFO_SNMP, clz, func, info);
  422. }
  423. void trace(String func, String info) {
  424. trace(dbgTag, func, info);
  425. }
  426. boolean isDebugOn() {
  427. return Trace.isSelected(Trace.LEVEL_DEBUG, Trace.INFO_SNMP);
  428. }
  429. void debug(String clz, String func, String info) {
  430. Trace.send(Trace.LEVEL_DEBUG, Trace.INFO_SNMP, clz, func, info);
  431. }
  432. void debug(String clz, String func, Throwable exception) {
  433. Trace.send(Trace.LEVEL_DEBUG, Trace.INFO_SNMP, clz, func, exception);
  434. }
  435. void debug(String func, String info) {
  436. debug(dbgTag, func, info);
  437. }
  438. void debug(String func, Throwable exception) {
  439. debug(dbgTag, func, exception);
  440. }
  441. String dbgTag = "SnmpV3Message";
  442. }