View Javadoc
1   /*
2    *  Licensed to the Apache Software Foundation (ASF) under one
3    *  or more contributor license agreements.  See the NOTICE file
4    *  distributed with this work for additional information
5    *  regarding copyright ownership.  The ASF licenses this file
6    *  to you under the Apache License, Version 2.0 (the
7    *  "License"); you may not use this file except in compliance
8    *  with the License.  You may obtain a copy of the License at
9    *  
10   *    http://www.apache.org/licenses/LICENSE-2.0
11   *  
12   *  Unless required by applicable law or agreed to in writing,
13   *  software distributed under the License is distributed on an
14   *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   *  KIND, either express or implied.  See the License for the
16   *  specific language governing permissions and limitations
17   *  under the License. 
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   * This class implements the Certificate generation extended operation's ASN.1 grammer. 
41   * All the actions are declared in this class. As it is a singleton, 
42   * these declaration are only done once. The grammar is :
43   * 
44   * <pre>
45   *   CertGenerateObject ::= SEQUENCE 
46   *   {
47   *      targetDN        IA5String,
48   *      issuerDN        IA5String,
49   *      subjectDN       IA5String,
50   *      keyAlgorithm    IA5String
51   *   }
52   * </pre>
53   * 
54   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
55   */
56  
57  public class CertGenerationGrammar extends AbstractGrammar<CertGenerationContainer>
58  {
59  
60      /** logger */
61      private static final Logger LOG = LoggerFactory.getLogger( CertGenerationGrammar.class );
62  
63      /** Speedup for logs */
64      static final boolean IS_DEBUG = LOG.isDebugEnabled();
65  
66      /** The instance of grammar. CertGenerationObjectGrammar is a singleton */
67      private static Grammar<CertGenerationContainer> instance = new CertGenerationGrammar();
68  
69  
70      /**
71       * Creates a new CertGenerationGrammar object.
72       */
73      @SuppressWarnings("unchecked")
74      public CertGenerationGrammar()
75      {
76          setName( CertGenerationGrammar.class.getName() );
77  
78          // Create the transitions table
79          super.transitions = new GrammarTransition[CertGenerationStatesEnum.LAST_CERT_GENERATION_STATE.ordinal()][256];
80  
81          /**
82           * Transition from init state to certificate generation
83           * 
84           * CertGenerationObject ::= SEQUENCE {
85           *     ...
86           *     
87           * Creates the CertGenerationObject object
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          * Transition from certificate generation request to targetDN
105          *
106          * CertGenerationObject ::= SEQUENCE { 
107          *     targetDN IA5String,
108          *     ...
109          *     
110          * Set the targetDN value into the CertGenerationObject instance.
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          * Transition from targetDN state to issuerDN
152          *
153          * CertGenerationObject ::= SEQUENCE { 
154          *     ...
155          *     issuerDN IA5String,
156          *     ...
157          *     
158          * Set the issuerDN value into the CertGenerationObject instance.
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          * Transition from issuerDN state to subjectDN
192          *
193          * CertGenerationObject ::= SEQUENCE {
194          *     ... 
195          *     subjectDN IA5String,
196          *     ...
197          *     
198          * Set the subjectDN value into the CertGenerationObject instance.
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          * Transition from subjectDN state to keyAlgo
238          *
239          * CertGenerationObject ::= SEQUENCE { 
240          *     ...
241          *     keyAlgorithm IA5String
242          *     
243          * Set the key algorithm value into the CertGenerationObject instance.
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      * This class is a singleton.
276      * 
277      * @return An instance on this grammar
278      */
279     public static Grammar<CertGenerationContainer> getInstance()
280     {
281         return instance;
282     }
283 }