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.shared.crypto.encryption;
21  
22  
23  import java.security.GeneralSecurityException;
24  import java.security.MessageDigest;
25  import java.security.NoSuchAlgorithmException;
26  import java.security.spec.AlgorithmParameterSpec;
27  import java.util.Arrays;
28  
29  import javax.crypto.Cipher;
30  import javax.crypto.SecretKey;
31  import javax.crypto.spec.IvParameterSpec;
32  import javax.crypto.spec.SecretKeySpec;
33  
34  import org.apache.directory.server.kerberos.shared.exceptions.ErrorType;
35  import org.apache.directory.server.kerberos.shared.exceptions.KerberosException;
36  import org.apache.directory.server.kerberos.shared.messages.value.EncryptedData;
37  import org.apache.directory.server.kerberos.shared.messages.value.EncryptionKey;
38  import org.apache.directory.shared.ldap.constants.LdapSecurityConstants;
39  
40  
41  /**
42   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
43   * @version $Rev: 602574 $, $Date: 2007-12-09 00:10:21 +0100 (So, 09 Dez 2007) $
44   */
45  class DesCbcMd5Encryption extends EncryptionEngine
46  {
47      private static final byte[] iv = new byte[]
48          { ( byte ) 0x00, ( byte ) 0x00, ( byte ) 0x00, ( byte ) 0x00, ( byte ) 0x00, ( byte ) 0x00, ( byte ) 0x00,
49              ( byte ) 0x00 };
50  
51  
52      public EncryptionType getEncryptionType()
53      {
54          return EncryptionType.DES_CBC_MD5;
55      }
56  
57  
58      public int getConfounderLength()
59      {
60          return 8;
61      }
62  
63  
64      public int getChecksumLength()
65      {
66          return 16;
67      }
68  
69  
70      public byte[] calculateIntegrity( byte[] data, byte[] key, KeyUsage usage )
71      {
72          try
73          {
74              MessageDigest digester = MessageDigest.getInstance( LdapSecurityConstants.HASH_METHOD_MD5.getName() );
75              return digester.digest( data );
76          }
77          catch ( NoSuchAlgorithmException nsae )
78          {
79              return null;
80          }
81      }
82  
83  
84      public byte[] getDecryptedData( EncryptionKey key, EncryptedData data, KeyUsage usage ) throws KerberosException
85      {
86          // decrypt the data
87          byte[] decryptedData = decrypt( data.getCipher(), key.getKeyValue() );
88  
89          // extract the old checksum
90          byte[] oldChecksum = new byte[getChecksumLength()];
91          System.arraycopy( decryptedData, getConfounderLength(), oldChecksum, 0, oldChecksum.length );
92  
93          // zero out the old checksum in the cipher text
94          for ( int i = getConfounderLength(); i < getConfounderLength() + getChecksumLength(); i++ )
95          {
96              decryptedData[i] = 0;
97          }
98  
99          // calculate a new checksum
100         byte[] newChecksum = calculateIntegrity( decryptedData, key.getKeyValue(), usage );
101 
102         // compare checksums
103         if ( !Arrays.equals( oldChecksum, newChecksum ) )
104         {
105             throw new KerberosException( ErrorType.KRB_AP_ERR_BAD_INTEGRITY );
106         }
107 
108         // remove leading confounder and checksum
109         return removeLeadingBytes( decryptedData, getConfounderLength(), getChecksumLength() );
110     }
111 
112 
113     public EncryptedData getEncryptedData( EncryptionKey key, byte[] plainText, KeyUsage usage )
114     {
115         // build the ciphertext structure
116         byte[] conFounder = getRandomBytes( getConfounderLength() );
117         byte[] zeroedChecksum = new byte[getChecksumLength()];
118         byte[] dataBytes = concatenateBytes( conFounder, concatenateBytes( zeroedChecksum, plainText ) );
119         byte[] paddedDataBytes = padString( dataBytes );
120         byte[] checksumBytes = calculateIntegrity( paddedDataBytes, null, usage );
121 
122         // lay the checksum into the ciphertext
123         for ( int i = getConfounderLength(); i < getConfounderLength() + getChecksumLength(); i++ )
124         {
125             paddedDataBytes[i] = checksumBytes[i - getConfounderLength()];
126         }
127 
128         byte[] encryptedData = encrypt( paddedDataBytes, key.getKeyValue() );
129 
130         return new EncryptedData( getEncryptionType(), key.getKeyVersion(), encryptedData );
131     }
132 
133 
134     public byte[] encrypt( byte[] plainText, byte[] keyBytes )
135     {
136         return processCipher( true, plainText, keyBytes );
137     }
138 
139 
140     public byte[] decrypt( byte[] cipherText, byte[] keyBytes )
141     {
142         return processCipher( false, cipherText, keyBytes );
143     }
144 
145 
146     private byte[] processCipher( boolean isEncrypt, byte[] data, byte[] keyBytes )
147     {
148         try
149         {
150             Cipher cipher = Cipher.getInstance( "DES/CBC/NoPadding" );
151             SecretKey key = new SecretKeySpec( keyBytes, "DES" );
152 
153             AlgorithmParameterSpec paramSpec = new IvParameterSpec( iv );
154 
155             if ( isEncrypt )
156             {
157                 cipher.init( Cipher.ENCRYPT_MODE, key, paramSpec );
158             }
159             else
160             {
161                 cipher.init( Cipher.DECRYPT_MODE, key, paramSpec );
162             }
163 
164             return cipher.doFinal( data );
165         }
166         catch ( GeneralSecurityException nsae )
167         {
168             nsae.printStackTrace();
169             return null;
170         }
171     }
172 }