1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.apache.directory.api.ldap.extras.extended.ads_impl.certGeneration;
21
22
23 import org.apache.directory.api.asn1.DecoderException;
24 import org.apache.directory.api.asn1.ber.grammar.AbstractGrammar;
25 import org.apache.directory.api.asn1.ber.grammar.Grammar;
26 import org.apache.directory.api.asn1.ber.grammar.GrammarAction;
27 import org.apache.directory.api.asn1.ber.grammar.GrammarTransition;
28 import org.apache.directory.api.asn1.ber.tlv.BerValue;
29 import org.apache.directory.api.asn1.ber.tlv.UniversalTag;
30 import org.apache.directory.api.i18n.I18n;
31 import org.apache.directory.api.ldap.codec.api.LdapApiServiceFactory;
32 import org.apache.directory.api.ldap.extras.extended.certGeneration.CertGenerationRequestImpl;
33 import org.apache.directory.api.ldap.model.name.Dn;
34 import org.apache.directory.api.util.Strings;
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57 public class CertGenerationGrammar extends AbstractGrammar<CertGenerationContainer>
58 {
59
60
61 private static final Logger LOG = LoggerFactory.getLogger( CertGenerationGrammar.class );
62
63
64 static final boolean IS_DEBUG = LOG.isDebugEnabled();
65
66
67 private static Grammar<CertGenerationContainer> instance = new CertGenerationGrammar();
68
69
70
71
72
73 @SuppressWarnings("unchecked")
74 public CertGenerationGrammar()
75 {
76 setName( CertGenerationGrammar.class.getName() );
77
78
79 super.transitions = new GrammarTransition[CertGenerationStatesEnum.LAST_CERT_GENERATION_STATE.ordinal()][256];
80
81
82
83
84
85
86
87
88
89 super.transitions[CertGenerationStatesEnum.START_STATE.ordinal()][UniversalTag.SEQUENCE.getValue()] =
90 new GrammarTransition<CertGenerationContainer>(
91 CertGenerationStatesEnum.START_STATE, CertGenerationStatesEnum.CERT_GENERATION_REQUEST_SEQUENCE_STATE,
92 UniversalTag.SEQUENCE.getValue(), new GrammarAction<CertGenerationContainer>(
93 "Init CertGenerationObject" )
94 {
95 public void action( CertGenerationContainer container )
96 {
97 CertGenerationRequestDecorator certGenerationRequest = new CertGenerationRequestDecorator(
98 LdapApiServiceFactory.getSingleton(), new CertGenerationRequestImpl() );
99 container.setCertGenerationRequest( certGenerationRequest );
100 }
101 } );
102
103
104
105
106
107
108
109
110
111
112 super.transitions[CertGenerationStatesEnum.CERT_GENERATION_REQUEST_SEQUENCE_STATE.ordinal()][UniversalTag.OCTET_STRING
113 .getValue()] =
114 new GrammarTransition<CertGenerationContainer>(
115 CertGenerationStatesEnum.CERT_GENERATION_REQUEST_SEQUENCE_STATE,
116 CertGenerationStatesEnum.TARGETDN_STATE, UniversalTag.OCTET_STRING.getValue(),
117 new GrammarAction<CertGenerationContainer>( "Set Cert Generation target Dn value" )
118 {
119 public void action( CertGenerationContainer container ) throws DecoderException
120 {
121 BerValue value = container.getCurrentTLV().getValue();
122
123 String targetDN = Strings.utf8ToString( value.getData() );
124
125 if ( IS_DEBUG )
126 {
127 LOG.debug( "Target Dn = " + targetDN );
128 }
129
130 if ( ( targetDN != null ) && ( targetDN.trim().length() > 0 ) )
131 {
132 if ( !Dn.isValid( targetDN ) )
133 {
134 String msg = I18n.err( I18n.ERR_04032, targetDN );
135 LOG.error( msg );
136 throw new DecoderException( msg );
137 }
138
139 container.getCertGenerationRequest().setTargetDN( targetDN );
140 }
141 else
142 {
143 String msg = I18n.err( I18n.ERR_04033, Strings.dumpBytes( value.getData() ) );
144 LOG.error( msg );
145 throw new DecoderException( msg );
146 }
147 }
148 } );
149
150
151
152
153
154
155
156
157
158
159
160 super.transitions[CertGenerationStatesEnum.TARGETDN_STATE.ordinal()][UniversalTag.OCTET_STRING.getValue()] =
161 new GrammarTransition<CertGenerationContainer>( CertGenerationStatesEnum.TARGETDN_STATE,
162 CertGenerationStatesEnum.ISSUER_STATE, UniversalTag.OCTET_STRING.getValue(),
163 new GrammarAction<CertGenerationContainer>( "Set Cert Generation issuer Dn value" )
164 {
165 public void action( CertGenerationContainer container ) throws DecoderException
166 {
167 BerValue value = container.getCurrentTLV().getValue();
168
169 String issuerDN = Strings.utf8ToString( value.getData() );
170
171 if ( IS_DEBUG )
172 {
173 LOG.debug( "Issuer Dn = " + issuerDN );
174 }
175
176 if ( ( issuerDN != null ) && ( issuerDN.trim().length() > 0 ) )
177 {
178 if ( !Dn.isValid( issuerDN ) )
179 {
180 String msg = I18n.err( I18n.ERR_04034, issuerDN );
181 LOG.error( msg );
182 throw new DecoderException( msg );
183 }
184
185 container.getCertGenerationRequest().setIssuerDN( issuerDN );
186 }
187 }
188 } );
189
190
191
192
193
194
195
196
197
198
199
200 super.transitions[CertGenerationStatesEnum.ISSUER_STATE.ordinal()][UniversalTag.OCTET_STRING.getValue()] =
201 new GrammarTransition<CertGenerationContainer>( CertGenerationStatesEnum.ISSUER_STATE,
202 CertGenerationStatesEnum.SUBJECT_STATE, UniversalTag.OCTET_STRING.getValue(),
203 new GrammarAction<CertGenerationContainer>( "Set Cert Generation subject Dn value" )
204 {
205 public void action( CertGenerationContainer container ) throws DecoderException
206 {
207 BerValue value = container.getCurrentTLV().getValue();
208
209 String subjectDN = Strings.utf8ToString( value.getData() );
210
211 if ( IS_DEBUG )
212 {
213 LOG.debug( "subject Dn = " + subjectDN );
214 }
215
216 if ( ( subjectDN != null ) && ( subjectDN.trim().length() > 0 ) )
217 {
218 if ( !Dn.isValid( subjectDN ) )
219 {
220 String msg = I18n.err( I18n.ERR_04035, subjectDN );
221 LOG.error( msg );
222 throw new DecoderException( msg );
223 }
224
225 container.getCertGenerationRequest().setSubjectDN( subjectDN );
226 }
227 else
228 {
229 String msg = I18n.err( I18n.ERR_04033, Strings.dumpBytes( value.getData() ) );
230 LOG.error( msg );
231 throw new DecoderException( msg );
232 }
233 }
234 } );
235
236
237
238
239
240
241
242
243
244
245 super.transitions[CertGenerationStatesEnum.SUBJECT_STATE.ordinal()][UniversalTag.OCTET_STRING.getValue()] =
246 new GrammarTransition<CertGenerationContainer>( CertGenerationStatesEnum.SUBJECT_STATE,
247 CertGenerationStatesEnum.KEY_ALGORITHM_STATE,
248 UniversalTag.OCTET_STRING.getValue(),
249 new GrammarAction<CertGenerationContainer>( "Set Cert Generation key algorithm value" )
250 {
251 public void action( CertGenerationContainer container ) throws DecoderException
252 {
253 BerValue value = container.getCurrentTLV().getValue();
254
255 String keyAlgorithm = Strings.utf8ToString( value.getData() );
256
257 if ( IS_DEBUG )
258 {
259 LOG.debug( "key algorithm = " + keyAlgorithm );
260 }
261
262 if ( keyAlgorithm != null && ( keyAlgorithm.trim().length() > 0 ) )
263 {
264 container.getCertGenerationRequest().setKeyAlgorithm( keyAlgorithm );
265 }
266
267 container.setGrammarEndAllowed( true );
268 }
269 } );
270
271 }
272
273
274
275
276
277
278
279 public static Grammar<CertGenerationContainer> getInstance()
280 {
281 return instance;
282 }
283 }