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.kerberos.client;
21  
22  
23  import java.nio.ByteBuffer;
24  import java.util.Collections;
25  import java.util.LinkedHashSet;
26  import java.util.Set;
27  
28  import org.apache.directory.api.asn1.DecoderException;
29  import org.apache.directory.api.asn1.ber.Asn1Decoder;
30  import org.apache.directory.shared.kerberos.codec.etypeInfo.ETypeInfoContainer;
31  import org.apache.directory.shared.kerberos.codec.etypeInfo2.ETypeInfo2Container;
32  import org.apache.directory.shared.kerberos.codec.methodData.MethodDataContainer;
33  import org.apache.directory.shared.kerberos.codec.types.EncryptionType;
34  import org.apache.directory.shared.kerberos.codec.types.PaDataType;
35  import org.apache.directory.shared.kerberos.components.ETypeInfo;
36  import org.apache.directory.shared.kerberos.components.ETypeInfo2;
37  import org.apache.directory.shared.kerberos.components.ETypeInfo2Entry;
38  import org.apache.directory.shared.kerberos.components.ETypeInfoEntry;
39  import org.apache.directory.shared.kerberos.components.MethodData;
40  import org.apache.directory.shared.kerberos.components.PaData;
41  import org.apache.directory.shared.kerberos.messages.KrbError;
42  
43  
44  /**
45   * A class with utility methods.
46   *
47   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
48   */
49  public class KdcClientUtil
50  {
51      public static String extractRealm( String principal )
52      {
53          int pos = principal.indexOf( '@' );
54  
55          if ( pos > 0 )
56          {
57              return principal.substring( pos + 1 );
58          }
59  
60          throw new IllegalArgumentException( "Not a valid principal, missing realm name" );
61      }
62  
63  
64      public static String extractName( String principal )
65      {
66          int pos = principal.indexOf( '@' );
67  
68          if ( pos < 0 )
69          {
70              return principal;
71          }
72  
73          return principal.substring( 0, pos );
74      }
75  
76  
77      public static Set<EncryptionType> getEtypesFromError( KrbError error )
78      {
79          try
80          {
81              ByteBuffer stream = ByteBuffer.wrap( error.getEData() );
82  
83              MethodDataContainerc/methodData/MethodDataContainer.html#MethodDataContainer">MethodDataContainer container = new MethodDataContainer();
84              container.setStream( stream );
85              Asn1Decoder.decode( stream, container );
86  
87              MethodData methodData = container.getMethodData();
88  
89              for ( PaData pd : methodData.getPaDatas() )
90              {
91                  if ( pd.getPaDataType() == PaDataType.PA_ENCTYPE_INFO2 )
92                  {
93                      return parseEtpeInfo2( pd.getPaDataValue() );
94                  }
95                  else if ( pd.getPaDataType() == PaDataType.PA_ENCTYPE_INFO )
96                  {
97                      return parseEtpeInfo( pd.getPaDataValue() );
98                  }
99              }
100         }
101         catch ( Exception e )
102         {
103             // shouldn't happen, but iff happens blast off
104             throw new RuntimeException( e );
105         }
106 
107         return Collections.emptySet();
108     }
109 
110 
111     private static Set<EncryptionType> parseEtpeInfo2( byte[] data ) throws DecoderException
112     {
113         ByteBuffer stream = ByteBuffer.wrap( data );
114 
115         ETypeInfo2Containerc/etypeInfo2/ETypeInfo2Container.html#ETypeInfo2Container">ETypeInfo2Container container = new ETypeInfo2Container();
116         container.setStream( stream );
117         Asn1Decoder.decode( stream, container );
118 
119         ETypeInfo2 info2 = container.getETypeInfo2();
120 
121         Set<EncryptionType> lstEtypes = new LinkedHashSet<>();
122 
123         for ( ETypeInfo2Entry e2e : info2.getETypeInfo2Entries() )
124         {
125             lstEtypes.add( e2e.getEType() );
126         }
127 
128         return lstEtypes;
129     }
130 
131 
132     private static Set<EncryptionType> parseEtpeInfo( byte[] data ) throws DecoderException
133     {
134         ByteBuffer stream = ByteBuffer.wrap( data );
135 
136         ETypeInfoContainerec/etypeInfo/ETypeInfoContainer.html#ETypeInfoContainer">ETypeInfoContainer container = new ETypeInfoContainer();
137         container.setStream( stream );
138         Asn1Decoder.decode( stream, container );
139 
140         ETypeInfo einfo = container.getETypeInfo();
141 
142         Set<EncryptionType> lstEtypes = new LinkedHashSet<>();
143 
144         for ( ETypeInfoEntry eie : einfo.getETypeInfoEntries() )
145         {
146             lstEtypes.add( eie.getEType() );
147         }
148 
149         return lstEtypes;
150     }
151 
152 }