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.shared.kerberos.codec.actions;
21  
22  
23  import org.apache.directory.api.asn1.DecoderException;
24  import org.apache.directory.api.asn1.ber.Asn1Container;
25  import org.apache.directory.api.asn1.ber.grammar.GrammarAction;
26  import org.apache.directory.api.asn1.ber.tlv.BerValue;
27  import org.apache.directory.api.asn1.ber.tlv.IntegerDecoder;
28  import org.apache.directory.api.asn1.ber.tlv.IntegerDecoderException;
29  import org.apache.directory.api.asn1.ber.tlv.TLV;
30  import org.apache.directory.api.i18n.I18n;
31  import org.apache.directory.api.util.Strings;
32  import org.apache.directory.shared.kerberos.KerberosMessageType;
33  import org.apache.directory.shared.kerberos.codec.kdcRep.KdcRepContainer;
34  import org.apache.directory.shared.kerberos.codec.kdcReq.KdcReqContainer;
35  import org.slf4j.Logger;
36  import org.slf4j.LoggerFactory;
37  
38  
39  /**
40   * The action used to read and validate the msg-type
41   *
42   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
43   */
44  public abstract class AbstractReadMsgType<E extends Asn1Container> extends GrammarAction<E>
45  {
46      /** The logger */
47      private static final Logger LOG = LoggerFactory.getLogger( AbstractReadMsgType.class );
48  
49      /** The msgType to decode */
50      private KerberosMessageType msgType = null;
51  
52  
53      /**
54       * Instantiates a new StoreMsgType action.
55       */
56      public AbstractReadMsgType( String name )
57      {
58          super( name );
59      }
60  
61  
62      /**
63       * Instantiates a new StoreMsgType action.
64       */
65      public AbstractReadMsgType( String name, KerberosMessageType msgType )
66      {
67          super( name );
68          this.msgType = msgType;
69      }
70  
71  
72      /**
73       * {@inheritDoc}
74       */
75      public final void action( E container ) throws DecoderException
76      {
77          TLV tlv = container.getCurrentTLV();
78  
79          // The Length should not be null and should be 1
80          if ( tlv.getLength() != 1 )
81          {
82              LOG.error( I18n.err( I18n.ERR_01308_ZERO_LENGTH_TLV ) );
83  
84              // This will generate a PROTOCOL_ERROR
85              throw new DecoderException( I18n.err( I18n.ERR_01309_EMPTY_TLV ) );
86          }
87  
88          BerValue value = tlv.getValue();
89  
90          try
91          {
92              int msgTypeValue = IntegerDecoder.parse( value );
93  
94              if ( msgType != null )
95              {
96                  if ( msgType.getValue() == msgTypeValue )
97                  {
98                      LOG.debug( "msg-type : {}", msgType );
99  
100                     return;
101                 }
102 
103                 String message = I18n.err( I18n.ERR_05102_INVALID_MESSAGE_ID, Strings.dumpBytes( value.getData() ) );
104                 LOG.error( message );
105 
106                 // This will generate a PROTOCOL_ERROR
107                 throw new DecoderException( message );
108             }
109             else
110             {
111                 KerberosMessageType messageType = KerberosMessageType.getTypeByValue( msgTypeValue );
112 
113                 if ( ( container instanceof KdcReqContainer )
114                         &&  ( ( ( KdcReqContainer ) container ).getKdcReq().getMessageType() == messageType ) )
115                 {
116                     return;
117                 }
118                 else if ( ( container instanceof KdcRepContainer )
119                         && ( ( ( KdcRepContainer ) container ).getKdcRep().getMessageType() == messageType ) )
120                 {
121                     return;
122                 }
123 
124                 String message = I18n.err( I18n.ERR_05102_INVALID_MESSAGE_ID, Strings.dumpBytes( value.getData() ) );
125                 LOG.error( message );
126 
127                 // This will generate a PROTOCOL_ERROR
128                 throw new DecoderException( message );
129             }
130         }
131         catch ( IntegerDecoderException ide )
132         {
133             LOG.error( I18n.err( I18n.ERR_05102_INVALID_MESSAGE_ID, Strings.dumpBytes( value.getData() ), ide
134                 .getLocalizedMessage() ) );
135 
136             // This will generate a PROTOCOL_ERROR
137             throw new DecoderException( ide.getMessage() );
138         }
139     }
140 }