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.server.kerberos.sam;
21  
22  
23  import java.io.IOException;
24  
25  import javax.security.auth.kerberos.KerberosKey;
26  
27  import org.apache.directory.server.kerberos.shared.crypto.encryption.CipherTextHandler;
28  import org.apache.directory.server.kerberos.shared.crypto.encryption.EncryptionType;
29  import org.apache.directory.server.kerberos.shared.crypto.encryption.KeyUsage;
30  import org.apache.directory.server.kerberos.shared.exceptions.KerberosException;
31  import org.apache.directory.server.kerberos.shared.io.decoder.EncryptedDataDecoder;
32  import org.apache.directory.server.kerberos.shared.messages.value.EncryptedData;
33  import org.apache.directory.server.kerberos.shared.messages.value.EncryptedTimeStamp;
34  import org.apache.directory.server.kerberos.shared.messages.value.EncryptionKey;
35  import org.apache.directory.server.kerberos.shared.messages.value.KerberosTime;
36  
37  
38  /**
39   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
40   * @version $Rev: 563062 $, $Date: 2007-08-06 10:21:20 +0200 (Mo, 06 Aug 2007) $
41   */
42  public class TimestampChecker implements KeyIntegrityChecker
43  {
44      private static final long FIVE_MINUTES = 300000;
45      private static final CipherTextHandler cipherTextHandler = new CipherTextHandler();
46  
47  
48      public boolean checkKeyIntegrity( byte[] encryptedData, KerberosKey kerberosKey )
49      {
50          EncryptionType keyType = EncryptionType.getTypeByOrdinal( kerberosKey.getKeyType() );
51          EncryptionKey key = new EncryptionKey( keyType, kerberosKey.getEncoded() );
52  
53          try
54          {
55              /*
56               * Since the pre-auth value is of type PA-ENC-TIMESTAMP, it should be a valid
57               * ASN.1 PA-ENC-TS-ENC structure, so we can decode it into EncryptedData.
58               */
59              EncryptedData sadValue = EncryptedDataDecoder.decode( encryptedData );
60  
61              /*
62               * Decrypt the EncryptedData structure to get the PA-ENC-TS-ENC.  Decode the
63               * decrypted timestamp into our timestamp object.
64               */
65              EncryptedTimeStamp timestamp = ( EncryptedTimeStamp ) cipherTextHandler.unseal( EncryptedTimeStamp.class,
66                  key, sadValue, KeyUsage.NUMBER1 );
67  
68              /*
69               * Since we got here we must have a valid timestamp structure that we can
70               * validate to be within a five minute skew.
71               */
72              KerberosTime time = timestamp.getTimeStamp();
73  
74              if ( time.isInClockSkew( FIVE_MINUTES ) )
75              {
76                  return true;
77              }
78          }
79          catch ( IOException ioe )
80          {
81              return false;
82          }
83          catch ( KerberosException ke )
84          {
85              return false;
86          }
87          catch ( ClassCastException cce )
88          {
89              return false;
90          }
91  
92          return false;
93      }
94  }