001    /*
002     *  Licensed to the Apache Software Foundation (ASF) under one
003     *  or more contributor license agreements.  See the NOTICE file
004     *  distributed with this work for additional information
005     *  regarding copyright ownership.  The ASF licenses this file
006     *  to you under the Apache License, Version 2.0 (the
007     *  "License"); you may not use this file except in compliance
008     *  with the License.  You may obtain a copy of the License at
009     *  
010     *    http://www.apache.org/licenses/LICENSE-2.0
011     *  
012     *  Unless required by applicable law or agreed to in writing,
013     *  software distributed under the License is distributed on an
014     *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015     *  KIND, either express or implied.  See the License for the
016     *  specific language governing permissions and limitations
017     *  under the License. 
018     *  
019     */
020    package org.apache.directory.server.kerberos.shared.crypto.encryption;
021    
022    
023    import java.util.Arrays;
024    import java.util.Collections;
025    import java.util.List;
026    
027    
028    /**
029     * From RFC 4120, "The Kerberos Network Authentication Service (V5)":
030     * 
031     * 7.5.1.  Key Usage Numbers
032     * 
033     * The encryption and checksum specifications in [RFC3961] require as
034     * input a "key usage number", to alter the encryption key used in any
035     * specific message in order to make certain types of cryptographic
036     * attack more difficult.  These are the key usage values assigned in
037     * [RFC 4120]:
038     * 
039     * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
040     * @version $Rev$, $Date$
041     */
042    public final class KeyUsage implements Comparable<KeyUsage>
043    {
044        /**
045         * AS-REQ PA-ENC-TIMESTAMP padata timestamp, encrypted with the client key (Section 5.2.7.2)
046         */
047        public static final KeyUsage NUMBER1 = new KeyUsage( 1,
048            "AS-REQ PA-ENC-TIMESTAMP padata timestamp, encrypted with the client key" );
049    
050        /**
051         * AS-REP Ticket and TGS-REP Ticket (includes TGS session key or application session key), encrypted with the service key (Section 5.3)
052         */
053        public static final KeyUsage NUMBER2 = new KeyUsage(
054            2,
055            "AS-REP Ticket and TGS-REP Ticket (includes TGS session key or application session key), encrypted with the service key" );
056    
057        /**
058         * AS-REP encrypted part (includes TGS session key or application session key), encrypted with the client key (Section 5.4.2)
059         */
060        public static final KeyUsage NUMBER3 = new KeyUsage( 3,
061            "AS-REP encrypted part (includes TGS session key or application session key), encrypted with the client key" );
062    
063        /**
064         * TGS-REQ KDC-REQ-BODY AuthorizationData, encrypted with the TGS session key (Section 5.4.1)
065         */
066        public static final KeyUsage NUMBER4 = new KeyUsage( 4,
067            "TGS-REQ KDC-REQ-BODY AuthorizationData, encrypted with the TGS session key" );
068    
069        /**
070         * TGS-REQ KDC-REQ-BODY AuthorizationData, encrypted with the TGS authenticator subkey (Section 5.4.1)
071         */
072        public static final KeyUsage NUMBER5 = new KeyUsage( 5,
073            "TGS-REQ KDC-REQ-BODY AuthorizationData, encrypted with the TGS authenticator subkey" );
074    
075        /**
076         * TGS-REQ PA-TGS-REQ padata AP-REQ Authenticator cksum, keyed with the TGS session key (Section 5.5.1)
077         */
078        public static final KeyUsage NUMBER6 = new KeyUsage( 6,
079            "TGS-REQ PA-TGS-REQ padata AP-REQ Authenticator cksum, keyed with the TGS session key" );
080    
081        /**
082         * TGS-REQ PA-TGS-REQ padata AP-REQ Authenticator (includes TGS authenticator subkey), encrypted with the TGS session key (Section 5.5.1)
083         */
084        public static final KeyUsage NUMBER7 = new KeyUsage(
085            7,
086            "TGS-REQ PA-TGS-REQ padata AP-REQ Authenticator (includes TGS authenticator subkey), encrypted with the TGS session key" );
087    
088        /**
089         * TGS-REP encrypted part (includes application session key), encrypted with the TGS session key (Section 5.4.2)
090         */
091        public static final KeyUsage NUMBER8 = new KeyUsage( 8,
092            "TGS-REP encrypted part (includes application session key), encrypted with the TGS session key" );
093    
094        /**
095         * TGS-REP encrypted part (includes application session key), encrypted with the TGS authenticator subkey (Section 5.4.2)
096         */
097        public static final KeyUsage NUMBER9 = new KeyUsage( 9,
098            "TGS-REP encrypted part (includes application session key), encrypted with the TGS authenticator subkey" );
099    
100        /**
101         * AP-REQ Authenticator cksum, keyed with the application session key (Section 5.5.1)
102         */
103        public static final KeyUsage NUMBER10 = new KeyUsage( 10,
104            "AP-REQ Authenticator cksum, keyed with the application session key" );
105    
106        /**
107         * AP-REQ Authenticator (includes application authenticator subkey), encrypted with the application session key (Section 5.5.1)
108         */
109        public static final KeyUsage NUMBER11 = new KeyUsage( 11,
110            "AP-REQ Authenticator (includes application authenticator subkey), encrypted with the application session key" );
111    
112        /**
113         * AP-REP encrypted part (includes application session subkey), encrypted with the application session key (Section 5.5.2)
114         */
115        public static final KeyUsage NUMBER12 = new KeyUsage( 12,
116            "AP-REP encrypted part (includes application session subkey), encrypted with the application session key" );
117    
118        /**
119         * KRB-PRIV encrypted part, encrypted with a key chosen by the application (Section 5.7.1)
120         */
121        public static final KeyUsage NUMBER13 = new KeyUsage( 13,
122            "KRB-PRIV encrypted part, encrypted with a key chosen by the application" );
123    
124        /**
125         * These two lines are all that's necessary to export a List of VALUES.
126         */
127        private static final KeyUsage[] values =
128            { NUMBER1, NUMBER2, NUMBER3, NUMBER4, NUMBER5, NUMBER6, NUMBER7, NUMBER8, NUMBER9, NUMBER10, NUMBER11,
129                NUMBER12, NUMBER13 };
130    
131        /**
132         * VALUES needs to be located here, otherwise illegal forward reference.
133         */
134        public static final List<KeyUsage> VALUES = Collections.unmodifiableList( Arrays.asList( values ) );
135    
136        private final int ordinal;
137        private final String name;
138    
139    
140        /**
141         * Private constructor prevents construction outside of this class.
142         */
143        private KeyUsage( int ordinal, String name )
144        {
145            this.ordinal = ordinal;
146            this.name = name;
147        }
148    
149    
150        /**
151         * Returns the key usage number type when specified by its ordinal.
152         *
153         * @param type
154         * @return The key usage number type.
155         */
156        public static KeyUsage getTypeByOrdinal( int type )
157        {
158            for ( int ii = 0; ii < values.length; ii++ )
159            {
160                if ( values[ii].ordinal == type )
161                {
162                    return values[ii];
163                }
164            }
165    
166            return NUMBER1;
167        }
168    
169    
170        /**
171         * Returns the number associated with this key usage number.
172         *
173         * @return The key usage number
174         */
175        public int getOrdinal()
176        {
177            return ordinal;
178        }
179    
180    
181        public int compareTo( KeyUsage that )
182        {
183            return ordinal - that.ordinal;
184        }
185    
186    
187        public String toString()
188        {
189            return name + " (" + ordinal + ")";
190        }
191    }