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.protocol;
21  
22  
23  import javax.security.auth.kerberos.KerberosPrincipal;
24  
25  import org.apache.directory.server.kerberos.kdc.KdcServer;
26  import org.apache.directory.server.kerberos.shared.crypto.encryption.CipherTextHandler;
27  import org.apache.directory.server.kerberos.shared.messages.ErrorMessage;
28  import org.apache.directory.server.kerberos.shared.messages.KdcRequest;
29  import org.apache.directory.server.kerberos.shared.messages.components.EncTicketPartModifier;
30  import org.apache.directory.server.kerberos.shared.messages.components.Ticket;
31  import org.apache.directory.server.kerberos.shared.messages.value.EncryptionKey;
32  import org.apache.directory.server.kerberos.shared.messages.value.KdcOptions;
33  import org.apache.directory.server.kerberos.shared.messages.value.KerberosTime;
34  import org.apache.directory.server.kerberos.shared.messages.value.RequestBody;
35  import org.apache.directory.server.kerberos.shared.messages.value.RequestBodyModifier;
36  import org.apache.directory.server.kerberos.shared.store.PrincipalStore;
37  
38  
39  /**
40   * Test case for RFC 4120 Section 3.7. "User-to-User Authentication Exchanges."  This
41   * is option "ENC-TKT-IN-SKEY."
42   * 
43   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
44   * @version $Rev$, $Date$
45   */
46  public class EncTktInSkeyTest extends AbstractTicketGrantingServiceTest
47  {
48      private KdcServer config;
49      private PrincipalStore store;
50      private KerberosProtocolHandler handler;
51      private DummySession session;
52  
53  
54      /**
55       * Creates a new instance of {@link EncTktInSkeyTest}.
56       */
57      public EncTktInSkeyTest()
58      {
59          config = new KdcServer();
60  
61          /*
62           * Body checksum verification must be disabled because we are bypassing
63           * the codecs, where the body bytes are set on the KdcRequest message.
64           */
65          config.setBodyChecksumVerified( false );
66  
67          store = new MapPrincipalStoreImpl();
68          handler = new KerberosProtocolHandler( config, store );
69          session = new DummySession();
70          lockBox = new CipherTextHandler();
71      }
72  
73  
74      /**
75       * If the ENC-TKT-IN-SKEY option has been specified and an additional ticket
76       * has been included in the request, it indicates that the client is using
77       * user-to-user authentication to prove its identity to a server that does
78       * not have access to a persistent key.  Section 3.7 describes the effect
79       * of this option on the entire Kerberos protocol.  When generating the
80       * KRB_TGS_REP message, this option in the KRB_TGS_REQ message tells the KDC
81       * to decrypt the additional ticket using the key for the server to which the
82       * additional ticket was issued and to verify that it is a TGT.  If the name
83       * of the requested server is missing from the request, the name of the client
84       * in the additional ticket will be used.  Otherwise, the name of the requested
85       * server will be compared to the name of the client in the additional ticket.
86       * If it is different, the request will be rejected.  If the request succeeds,
87       * the session key from the additional ticket will be used to encrypt the new
88       * ticket that is issued instead of using the key of the server for which the
89       * new ticket will be used.
90       * 
91       * @throws Exception 
92       */
93      public void testEncTktInSkey() throws Exception
94      {
95          // Get the mutable ticket part.
96          KerberosPrincipal clientPrincipal = new KerberosPrincipal( "hnelson@EXAMPLE.COM" );
97          EncTicketPartModifier encTicketPartModifier = getTicketArchetype( clientPrincipal );
98  
99          // Make changes to test.
100 
101         // Seal the ticket for the server.
102         KerberosPrincipal serverPrincipal = new KerberosPrincipal( "krbtgt/EXAMPLE.COM@EXAMPLE.COM" );
103         String passPhrase = "randomKey";
104         EncryptionKey serverKey = getEncryptionKey( serverPrincipal, passPhrase );
105         Ticket tgt = getTicket( encTicketPartModifier, serverPrincipal, serverKey );
106 
107         RequestBodyModifier modifier = new RequestBodyModifier();
108         modifier.setServerName( getPrincipalName( "ldap/ldap.example.com@EXAMPLE.COM" ) );
109         modifier.setRealm( "EXAMPLE.COM" );
110         modifier.setEType( config.getEncryptionTypes() );
111         modifier.setNonce( random.nextInt() );
112 
113         KdcOptions kdcOptions = new KdcOptions();
114         kdcOptions.set( KdcOptions.ENC_TKT_IN_SKEY );
115         modifier.setKdcOptions( kdcOptions );
116 
117         long now = System.currentTimeMillis();
118 
119         KerberosTime requestedEndTime = new KerberosTime( now + 1 * KerberosTime.DAY );
120         modifier.setTill( requestedEndTime );
121 
122         KerberosTime requestedRenewTillTime = new KerberosTime( now + KerberosTime.WEEK / 2 );
123         modifier.setRtime( requestedRenewTillTime );
124 
125         RequestBody requestBody = modifier.getRequestBody();
126         KdcRequest message = getKdcRequest( tgt, requestBody );
127 
128         handler.messageReceived( session, message );
129 
130         ErrorMessage error = ( ErrorMessage ) session.getMessage();
131         assertEquals( "KDC cannot accommodate requested option", 13, error.getErrorCode() );
132     }
133 }