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 * Pursuant to title 15 Untied States Code Section 105, works of NIST
7 * employees are not subject to copyright protection in the United States
8 * and are considered to be in the public domain.  As a result, a formal
9 * license is not needed to use the software.
10 *
11 * This software is provided by NIST as a service and is expressly
12 * provided "AS IS."  NIST MAKES NO WARRANTY OF ANY KIND, EXPRESS, IMPLIED
13 * OR STATUTORY, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTY OF
14 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, NON-INFRINGEMENT
15 * AND DATA ACCURACY.  NIST does not warrant or make any representations
16 * regarding the use of the software or the results thereof, including but
17 * not limited to the correctness, accuracy, reliability or usefulness of
18 * the software.
19 *
20 * Permission to use this software is contingent upon your acceptance
21 * of the terms of this agreement
22 *
23 * .
24 *
25 */
26 /************************************************************************************************
27  * PRODUCT OF PT INOVACAO - EST DEPARTMENT and Telecommunications Institute (Aveiro, Portugal)  *
28  ************************************************************************************************/
29 
30 
31 package gov.nist.javax.sip.parser.ims;
32 
33 /**
34  * Security Agreement for SIP.
35  * <p>headers: Security-Client, Security-Server and Security-Verify</p>
36  *
37  * @author Miguel Freitas (IT) PT-Inovacao
38  */
39 
40 
41 import gov.nist.core.NameValue;
42 import gov.nist.core.Token;
43 import gov.nist.javax.sip.header.SIPHeaderList;
44 import gov.nist.javax.sip.header.ims.*;
45 import gov.nist.javax.sip.parser.HeaderParser;
46 import gov.nist.javax.sip.parser.Lexer;
47 import gov.nist.javax.sip.parser.TokenTypes;
48 
49 import java.text.ParseException;
50 
51 
52 
53 public class SecurityAgreeParser extends HeaderParser
54 {
55 
SecurityAgreeParser(String security)56     public SecurityAgreeParser(String security) {
57         super(security);
58     }
59 
60 
SecurityAgreeParser(Lexer lexer)61     protected SecurityAgreeParser(Lexer lexer) {
62         super(lexer);
63     }
64 
65 
parseParameter(SecurityAgree header)66     protected void parseParameter(SecurityAgree header)
67         throws ParseException
68     {
69         if (debug)
70             dbg_enter("parseParameter");
71         try {
72             NameValue nv = this.nameValue('=');
73             header.setParameter(nv);
74         } finally {
75             if (debug)
76                 dbg_leave("parseParameter");
77         }
78     }
79 
80 
parse(SecurityAgree header)81     public SIPHeaderList parse(SecurityAgree header) throws ParseException
82     {
83 
84         SIPHeaderList list;
85 
86         if (header.getClass().isInstance(new SecurityClient())) {
87             list = new SecurityClientList();
88         } else if (header.getClass().isInstance(new SecurityServer())) {
89             list = new SecurityServerList();
90         } else if (header.getClass().isInstance(new SecurityVerify())) {
91             list = new SecurityVerifyList();
92         }
93         else
94             return null;
95 
96 
97         // the security-mechanism:
98         this.lexer.SPorHT();
99         lexer.match(TokenTypes.ID);
100         Token type = lexer.getNextToken();
101         header.setSecurityMechanism(type.getTokenValue());
102         this.lexer.SPorHT();
103 
104         char la = lexer.lookAhead(0);
105         if (la == '\n')
106         {
107             list.add(header);
108             return list;
109         }
110         else if (la == ';')
111             this.lexer.match(';');
112 
113         this.lexer.SPorHT();
114 
115         // The parameters:
116         try {
117             while (lexer.lookAhead(0) != '\n') {
118 
119                 this.parseParameter(header);
120                 this.lexer.SPorHT();
121                 char laInLoop = lexer.lookAhead(0);
122                 if (laInLoop == '\n' || laInLoop == '\0')
123                     break;
124                 else if (laInLoop == ',')
125                 {
126                     list.add(header);
127                     if (header.getClass().isInstance(new SecurityClient())) {
128                         header = new SecurityClient();
129                     } else if (header.getClass().isInstance(new SecurityServer())) {
130                         header = new SecurityServer();
131                     } else if (header.getClass().isInstance(new SecurityVerify())) {
132                         header = new SecurityVerify();
133                     }
134 
135                     this.lexer.match(',');
136                     // the security-mechanism:
137                     this.lexer.SPorHT();
138                     lexer.match(TokenTypes.ID);
139                     type = lexer.getNextToken();
140                     header.setSecurityMechanism(type.getTokenValue());
141 
142                 }
143                 this.lexer.SPorHT();
144 
145                 if (lexer.lookAhead(0) == ';')
146                     this.lexer.match(';');
147 
148                 this.lexer.SPorHT();
149 
150             }
151             list.add(header);
152 
153             return list;
154 
155         } catch (ParseException ex) {
156             throw ex;
157         }
158 
159 
160     }
161 
162 
163 
164 
165 }
166 
167 
168