KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > snmp4j > security > UsmSecurityParameters


1 /*_############################################################################
2   _##
3   _## SNMP4J - UsmSecurityParameters.java
4   _##
5   _## Copyright 2003-2007 Frank Fock and Jochen Katz (SNMP4J.org)
6   _##
7   _## Licensed under the Apache License, Version 2.0 (the "License");
8   _## you may not use this file except in compliance with the License.
9   _## You may obtain a copy of the License at
10   _##
11   _## http://www.apache.org/licenses/LICENSE-2.0
12   _##
13   _## Unless required by applicable law or agreed to in writing, software
14   _## distributed under the License is distributed on an "AS IS" BASIS,
15   _## WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   _## See the License for the specific language governing permissions and
17   _## limitations under the License.
18   _##
19   _##########################################################################*/

20
21 package org.snmp4j.security;
22
23 import java.io.*;
24 import org.snmp4j.asn1.*;
25 import org.snmp4j.asn1.BER.*;
26 import org.snmp4j.smi.*;
27 import org.snmp4j.security.SecurityLevel;
28 import org.snmp4j.log.LogFactory;
29 import org.snmp4j.log.LogAdapter;
30
31 public class UsmSecurityParameters implements SecurityParameters {
32
33   private static final LogAdapter logger =
34       LogFactory.getLogger(UsmSecurityParameters.class);
35
36   private static final int MAX_BER_LENGTH_WITHOU_SEC_PARAMS =
37       32+2+ 6 + 6 + 32+2;
38
39   private OctetString authoritativeEngineID = new OctetString();
40   private Integer32 authoritativeEngineBoots = new Integer32();
41   private Integer32 authoritativeEngineTime = new Integer32();
42   private OctetString userName = new OctetString();
43   private AuthenticationProtocol authenticationProtocol = null;
44   private PrivacyProtocol privacyProtocol = null;
45   private byte[] authenticationKey;
46   private byte[] privacyKey;
47   private OctetString privacyParameters = new OctetString();
48   private OctetString authenticationParameters = new OctetString();
49   private int securityParametersPosition = -1;
50   private int authParametersPosition = -1;
51   private int decodedLength = -1;
52
53   public UsmSecurityParameters() {
54   }
55
56   public UsmSecurityParameters(OctetString authoritativeEngineID,
57                                Integer32 authoritativeEngineBoots,
58                                Integer32 authoritativeEngineTime,
59                                OctetString userName,
60                                AuthenticationProtocol authenticationProtocol,
61                                PrivacyProtocol privacyProtocol) {
62     this.authoritativeEngineID = authoritativeEngineID;
63     this.authoritativeEngineBoots = authoritativeEngineBoots;
64     this.authoritativeEngineTime = authoritativeEngineTime;
65     this.privacyProtocol = privacyProtocol;
66     this.userName = userName;
67     this.authenticationProtocol = authenticationProtocol;
68   }
69
70   public byte[] getAuthoritativeEngineID() {
71     return authoritativeEngineID.getValue();
72   }
73
74   public void setAuthoritativeEngineID(byte[] authoritativeEngineID) {
75     if (authoritativeEngineID == null) {
76       throw new NullPointerException JavaDoc("Authoritative engine ID must not be null");
77     }
78     this.authoritativeEngineID.setValue(authoritativeEngineID);
79   }
80   public void setAuthoritativeEngineBoots(int authoritativeEngineBoots) {
81     this.authoritativeEngineBoots.setValue(authoritativeEngineBoots);
82   }
83   public int getAuthoritativeEngineBoots() {
84     return authoritativeEngineBoots.getValue();
85   }
86   public void setAuthoritativeEngineTime(int authoritativeEngineTime) {
87     this.authoritativeEngineTime.setValue(authoritativeEngineTime);
88   }
89   public int getAuthoritativeEngineTime() {
90     return authoritativeEngineTime.getValue();
91   }
92   public void setUserName(org.snmp4j.smi.OctetString userName) {
93     this.userName = userName;
94   }
95   public org.snmp4j.smi.OctetString getUserName() {
96     return userName;
97   }
98   public void setAuthenticationProtocol(AuthenticationProtocol authenticationProtocol) {
99     this.authenticationProtocol = authenticationProtocol;
100   }
101   public AuthenticationProtocol getAuthenticationProtocol() {
102     return authenticationProtocol;
103   }
104   public void setPrivacyProtocol(PrivacyProtocol privacyProtocol) {
105     this.privacyProtocol = privacyProtocol;
106   }
107   public PrivacyProtocol getPrivacyProtocol() {
108     return privacyProtocol;
109   }
110
111   public int getBERLength() {
112     int length = getBERPayloadLength();
113     return length + BER.getBERLengthOfLength(length) + 1;
114   }
115
116   public int getBERPayloadLength() {
117     int length = getBERUsmPayloadLength();
118     length += BER.getBERLengthOfLength(length)+1;
119     return length;
120   }
121
122
123
124   public void decodeBER(BERInputStream inputStream) throws IOException {
125     int pos = (int)inputStream.getPosition();
126     this.decodedLength = pos;
127     MutableByte mutableByte = new MutableByte();
128     int octetLength = BER.decodeHeader(inputStream, mutableByte);
129     long startPos = inputStream.getPosition();
130     if (mutableByte.getValue() != BER.OCTETSTRING) {
131       String JavaDoc txt =
132           "BER decoding error: Expected BER OCTETSTRING but found: " +
133           mutableByte.getValue();
134       logger.warn(txt);
135       throw new IOException(txt);
136     }
137     int length = BER.decodeHeader(inputStream, mutableByte);
138     long startPosSeq = inputStream.getPosition();
139     if (mutableByte.getValue() != BER.SEQUENCE) {
140       String JavaDoc txt =
141           "BER decoding error: Expected BER SEQUENCE but found: " +
142           mutableByte.getValue();
143       logger.warn(txt);
144       throw new IOException(txt);
145     }
146     authoritativeEngineID.decodeBER(inputStream);
147     authoritativeEngineBoots.decodeBER(inputStream);
148     authoritativeEngineTime.decodeBER(inputStream);
149     userName.decodeBER(inputStream);
150     this.authParametersPosition = (int)(inputStream.getPosition() - pos);
151     pos = (int)inputStream.getPosition();
152     authenticationParameters.decodeBER(inputStream);
153     this.authParametersPosition +=
154         (inputStream.getPosition() - pos) -
155         authenticationParameters.getBERPayloadLength();
156
157     privacyParameters.decodeBER(inputStream);
158     this.decodedLength = (int) (inputStream.getPosition() - decodedLength);
159     if (BER.isCheckSequenceLength()) {
160       // check length
161
BER.checkSequenceLength(length,
162                               (int) (inputStream.getPosition() - startPosSeq),
163                               this);
164       BER.checkSequenceLength(octetLength,
165                               (int) (inputStream.getPosition() - startPos),
166                               this);
167     }
168   }
169
170   private int getBEREncodedAuthParamsPosition() {
171     int length = getBERLength()-
172         (authenticationParameters.getBERPayloadLength() +
173          privacyParameters.getBERLength());
174     return length;
175   }
176
177   public void encodeBER(OutputStream outputStream) throws IOException {
178     BER.encodeHeader(outputStream, BER.OCTETSTRING, getBERPayloadLength());
179     BER.encodeHeader(outputStream, BER.SEQUENCE, getBERUsmPayloadLength());
180     authoritativeEngineID.encodeBER(outputStream);
181     authoritativeEngineBoots.encodeBER(outputStream);
182     authoritativeEngineTime.encodeBER(outputStream);
183     userName.encodeBER(outputStream);
184     authenticationParameters.encodeBER(outputStream);
185     privacyParameters.encodeBER(outputStream);
186   }
187
188   /**
189    * getBERUsmPayloadLength
190    *
191    * @return int
192    */

193   public int getBERUsmPayloadLength() {
194     int length = authoritativeEngineID.getBERLength();
195     length += authoritativeEngineBoots.getBERLength();
196     length += authoritativeEngineTime.getBERLength();
197     length += userName.getBERLength();
198     length += authenticationParameters.getBERLength();
199     length += privacyParameters.getBERLength();
200     return length;
201   }
202
203   public int getBERMaxLength(int securityLevel) {
204     SecurityProtocols secProtocol = SecurityProtocols.getInstance();
205     int securityParamsLength = 2;
206     if (securityLevel > SecurityLevel.NOAUTH_NOPRIV) {
207       securityParamsLength = secProtocol.getMaxAuthDigestLength() +
208           BER.getBERLengthOfLength(secProtocol.getMaxAuthDigestLength()) + 1;
209
210       if (securityLevel == SecurityLevel.AUTH_PRIV) {
211         securityParamsLength += secProtocol.getMaxPrivDecryptParamsLength() +
212             BER.getBERLengthOfLength(secProtocol.getMaxPrivDecryptParamsLength())
213             + 1;
214       }
215     }
216     return MAX_BER_LENGTH_WITHOU_SEC_PARAMS + securityParamsLength +
217         BER.getBERLengthOfLength(MAX_BER_LENGTH_WITHOU_SEC_PARAMS +
218                                  securityParamsLength) + 1;
219   }
220
221   public byte[] getAuthenticationKey() {
222     return authenticationKey;
223   }
224   public void setAuthenticationKey(byte[] authenticationKey) {
225     this.authenticationKey = authenticationKey;
226   }
227   public byte[] getPrivacyKey() {
228     return privacyKey;
229   }
230   public void setPrivacyKey(byte[] privacyKey) {
231     this.privacyKey = privacyKey;
232   }
233   public org.snmp4j.smi.OctetString getPrivacyParameters() {
234     return privacyParameters;
235   }
236   public void setPrivacyParameters(org.snmp4j.smi.OctetString privacyParameters) {
237     this.privacyParameters = privacyParameters;
238   }
239   public org.snmp4j.smi.OctetString getAuthenticationParameters() {
240     return authenticationParameters;
241   }
242   public void setAuthenticationParameters(org.snmp4j.smi.OctetString authenticationParameters) {
243     this.authenticationParameters = authenticationParameters;
244   }
245   public int getSecurityParametersPosition() {
246     return securityParametersPosition;
247   }
248   public void setSecurityParametersPosition(int securityParametersPosition) {
249     this.securityParametersPosition = securityParametersPosition;
250   }
251   public int getAuthParametersPosition() {
252     if (authParametersPosition >= 0) {
253       return authParametersPosition;
254     }
255     else {
256       return getBEREncodedAuthParamsPosition();
257     }
258   }
259
260   /**
261    * getScopedPduPosition
262    *
263    * @return int
264    */

265   public int getScopedPduPosition() {
266     if (decodedLength >= 0) {
267       return decodedLength + getSecurityParametersPosition();
268     }
269     else {
270       return getSecurityParametersPosition()+getBERLength();
271     }
272   }
273
274 }
275
Popular Tags