1 /*
2 * Conditions Of Use
3 *
4 * This software was developed by employees of the National Institute of
5 * Standards and Technology (NIST), an agency of the Federal Government,
6 * and others.
7 * Pursuant to title 15 Untied States Code Section 105, works of NIST
8 * employees are not subject to copyright protection in the United States
9 * and are considered to be in the public domain.  As a result, a formal
10 * license is not needed to use the software.
11 *
12 * This software is provided by NIST as a service and is expressly
13 * provided "AS IS."  NIST MAKES NO WARRANTY OF ANY KIND, EXPRESS, IMPLIED
14 * OR STATUTORY, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTY OF
15 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, NON-INFRINGEMENT
16 * AND DATA ACCURACY.  NIST does not warrant or make any representations
17 * regarding the use of the software or the results thereof, including but
18 * not limited to the correctness, accuracy, reliability or usefulness of
19 * the software.
20 *
21 * Permission to use this software is contingent upon your acceptance
22 * of the terms of this agreement
23 *
24 * .
25 *
26 */
27 
28 /************************************************************************************************
29  * PRODUCT OF PT INOVACAO - EST DEPARTMENT and Telecommunications Institute (Aveiro, Portugal)  *
30  ************************************************************************************************/
31 
32 package gov.nist.javax.sip.header.ims;
33 
34 
35 
36 import java.text.ParseException;
37 import javax.sip.InvalidArgumentException;
38 import javax.sip.header.Parameters;
39 
40 import gov.nist.core.NameValue;
41 import gov.nist.core.Separators;
42 import gov.nist.javax.sip.header.ims.ParameterNamesIms;
43 import gov.nist.javax.sip.header.ParametersHeader;
44 
45 
46 /**
47  * "Security Mechanism Agreemet for SIP Sessions"
48  *  - sec-agree: RFC 3329 + 3GPP TS33.203 (Annex H).
49  *
50  * <p>Headers: Security-Server + Security-Client + Security-Verify</p>
51  *
52  * @author Miguel Freitas (IT) PT-Inovacao
53  */
54 
55 
56 public abstract class SecurityAgree
57     extends ParametersHeader
58 {
59     //TODO serialVersionUID
60     //private static final long serialVersionUID = -6671234553927258745L;
61 
62     //public static final String EALG = ParameterNamesIms.EALG;
63     // ...
64 
65     /**
66      * Security Mechanism value
67      */
68     private String secMechanism;
69 
70 
71     /**
72      * Constructor
73      * @param name - name of the Security Agree header to create
74      */
SecurityAgree(String name)75     public SecurityAgree(String name)
76     {
77         super(name);
78         parameters.setSeparator(Separators.SEMICOLON);
79     }
80 
81     /**
82      * Default constructor
83      */
SecurityAgree()84     public SecurityAgree()
85     {
86         super();
87         parameters.setSeparator(Separators.SEMICOLON);
88     }
89 
90 
setParameter(String name, String value)91     public void setParameter(String name, String value) throws ParseException
92     {
93         if (value == null)
94             throw new NullPointerException("null value");
95 
96         NameValue nv = super.parameters.getNameValue(name.toLowerCase());
97         if (nv == null)
98         {
99             nv = new NameValue(name, value);
100 
101             // quoted values
102             if (name.equalsIgnoreCase(ParameterNamesIms.D_VER))
103             {
104                 nv.setQuotedValue();
105 
106                 if (value.startsWith(Separators.DOUBLE_QUOTE))
107                     throw new ParseException(value
108                             + " : Unexpected DOUBLE_QUOTE", 0);
109             }
110 
111             super.setParameter(nv);
112         }
113         else
114         {
115             nv.setValueAsObject(value);
116         }
117 
118     }
119 
encodeBody()120     public String encodeBody()
121     {
122         return this.secMechanism + SEMICOLON + SP + parameters.encode();
123     }
124 
125 
126 
127     /**
128      * Set security mechanism.
129      * <p>eg: Security-Client: ipsec-3gpp</p>
130      * @param secMech - security mechanism name
131      */
setSecurityMechanism(String secMech)132     public void setSecurityMechanism(String secMech) throws ParseException {
133         if (secMech == null)
134             throw new NullPointerException(
135                 "JAIN-SIP "
136                     + "Exception, SecurityAgree, setSecurityMechanism(), the sec-mechanism parameter is null");
137         this.secMechanism = secMech;
138     }
139 
140     /**
141      * Set Encryption Algorithm (ealg parameter)
142      * @param ealg - encryption algorithm value
143      * @throws ParseException
144      */
setEncryptionAlgorithm(String ealg)145     public void setEncryptionAlgorithm(String ealg) throws ParseException {
146         if (ealg == null)
147             throw new NullPointerException(
148                 "JAIN-SIP "
149                     + "Exception, SecurityClient, setEncryptionAlgorithm(), the encryption-algorithm parameter is null");
150 
151         setParameter(ParameterNamesIms.EALG, ealg);
152     }
153 
154     /**
155      * Set Algorithm (alg parameter)
156      * @param alg - algorithm value
157      * @throws ParseException
158      */
setAlgorithm(String alg)159     public void setAlgorithm(String alg) throws ParseException {
160         if (alg == null)
161             throw new NullPointerException(
162                 "JAIN-SIP "
163                     + "Exception, SecurityClient, setAlgorithm(), the algorithm parameter is null");
164         setParameter(ParameterNamesIms.ALG, alg);
165     }
166 
167     /**
168      * Set Protocol (prot paramater)
169      * @param prot - protocol value
170      * @throws ParseException
171      */
setProtocol(String prot)172     public void setProtocol(String prot) throws ParseException {
173         if (prot == null)
174             throw new NullPointerException(
175                 "JAIN-SIP "
176                     + "Exception, SecurityClient, setProtocol(), the protocol parameter is null");
177         setParameter(ParameterNamesIms.PROT, prot);
178     }
179 
180     /**
181      * Set Mode (mod parameter)
182      * @param mod - mode value
183      * @throws ParseException
184      */
setMode(String mod)185     public void setMode(String mod) throws ParseException {
186         if (mod == null)
187             throw new NullPointerException(
188                 "JAIN-SIP "
189                     + "Exception, SecurityClient, setMode(), the mode parameter is null");
190         setParameter(ParameterNamesIms.MOD, mod);
191     }
192 
193     /**
194      * Set Client SPI (spi-c parameter)
195      * @param spic - spi-c value
196      * @throws InvalidArgumentException
197      */
setSPIClient(int spic)198     public void setSPIClient(int spic) throws InvalidArgumentException {
199         if (spic < 0)
200             throw new InvalidArgumentException(
201                 "JAIN-SIP "
202                     + "Exception, SecurityClient, setSPIClient(), the spi-c parameter is <0");
203         setParameter(ParameterNamesIms.SPI_C, spic);
204     }
205 
206     /**
207      * Set Server SPI (spi-s parameter)
208      * @param spis - spi-s value
209      * @throws InvalidArgumentException - when value is not valid
210      */
setSPIServer(int spis)211     public void setSPIServer(int spis) throws InvalidArgumentException {
212         if (spis < 0)
213             throw new InvalidArgumentException(
214                 "JAIN-SIP "
215                     + "Exception, SecurityClient, setSPIServer(), the spi-s parameter is <0");
216         setParameter(ParameterNamesIms.SPI_S, spis);
217     }
218 
219     /**
220      * Set Client Port (port-c parameter)
221      * @param portC - port-c value
222      * @throws InvalidArgumentException - when value is not valid
223      */
setPortClient(int portC)224     public void setPortClient(int portC) throws InvalidArgumentException {
225         if (portC < 0)
226             throw new InvalidArgumentException(
227                 "JAIN-SIP "
228                     + "Exception, SecurityClient, setPortClient(), the port-c parameter is <0");
229         setParameter(ParameterNamesIms.PORT_C, portC);
230     }
231 
232     /**
233      * Set Server Port (port-s parameter)
234      * @param portS - port-s value
235      * @throws InvalidArgumentException - when value is not valid
236      */
setPortServer(int portS)237     public void setPortServer(int portS) throws InvalidArgumentException {
238         if (portS < 0)
239             throw new InvalidArgumentException(
240                 "JAIN-SIP "
241                     + "Exception, SecurityClient, setPortServer(), the port-s parameter is <0");
242         setParameter(ParameterNamesIms.PORT_S, portS);
243     }
244 
245     /**
246      * <p>Set Preference.
247      * The "q" parameter indicates a relative preference for the particular mechanism.
248      * The higher the value the more preferred the mechanism is.
249      * Range from 0.001 to 0.999.</p>
250      * @param q - q parameter value
251      * @throws InvalidArgumentException - when value is not valid
252      */
setPreference(float q)253     public void setPreference(float q) throws InvalidArgumentException {
254         if (q < 0.0f)
255             throw new InvalidArgumentException(
256                 "JAIN-SIP "
257                     + "Exception, SecurityClient, setPreference(), the preference (q) parameter is <0");
258         setParameter(ParameterNamesIms.Q, q);
259     }
260 
261 
262 
263     // get param
264 
265     /**
266      * Get Security Mechanism
267      * @return security mechanims value
268      */
getSecurityMechanism()269     public String getSecurityMechanism() {
270         return this.secMechanism;
271     }
272     /**
273      * Get Encryption Algorithm
274      * @return ealg parameter value
275      */
getEncryptionAlgorithm()276     public String getEncryptionAlgorithm() {
277         return getParameter(ParameterNamesIms.EALG);
278     }
279 
280     /**
281      * Get Algorithm
282      * @return alg parameter value
283      */
getAlgorithm()284     public String getAlgorithm() {
285         return getParameter(ParameterNamesIms.ALG);
286     }
287 
288     /**
289      * Get Protocol
290      * @return prot parameter value
291      */
getProtocol()292     public String getProtocol() {
293         return getParameter(ParameterNamesIms.PROT);
294     }
295 
296     /**
297      * Get Mode
298      * @return mod parameter value
299      */
getMode()300     public String getMode() {
301         return getParameter(ParameterNamesIms.MOD);
302 
303     }
304     /**
305      * Get Client SPI
306      * @return spi-c parameter value
307      */
getSPIClient()308     public int getSPIClient() {
309         return (Integer.parseInt(getParameter(ParameterNamesIms.SPI_C)));
310     }
311 
312     /**
313      * Get Server SPI
314      * @return spi-s parameter value
315      */
getSPIServer()316     public int getSPIServer() {
317         return (Integer.parseInt(getParameter(ParameterNamesIms.SPI_S)));
318     }
319 
320     /**
321      * Get Client Port
322      * @return port-c parameter value
323      */
getPortClient()324     public int getPortClient() {
325         return (Integer.parseInt(getParameter(ParameterNamesIms.PORT_C)));
326     }
327 
328     /**
329      * Get Server Port
330      * @return port-s parameter value
331      */
getPortServer()332     public int getPortServer() {
333         return (Integer.parseInt(getParameter(ParameterNamesIms.PORT_S)));
334     }
335 
336     /**
337      * Get Preference
338      * @return q parameter value
339      */
getPreference()340     public float getPreference() {
341         return (Float.parseFloat(getParameter(ParameterNamesIms.Q)));
342     }
343 
344 
equals(Object other)345     public boolean equals(Object other)
346     {
347 
348         if(other instanceof SecurityAgreeHeader)
349         {
350             SecurityAgreeHeader o = (SecurityAgreeHeader) other;
351             return (this.getSecurityMechanism().equals( o.getSecurityMechanism() )
352                 && this.equalParameters( (Parameters) o ));
353         }
354         return false;
355 
356     }
357 
358 
clone()359     public Object clone() {
360         SecurityAgree retval = (SecurityAgree) super.clone();
361         if (this.secMechanism != null)
362             retval.secMechanism = this.secMechanism;
363         return retval;
364     }
365 
366 
367 }
368 
369 
370