blob: 73273060c17fb3c34a14dd94c1c24f51002b45c5 [file] [log] [blame]
J. Duke319a3b92007-12-01 00:00:00 +00001/*
2 * Portions Copyright 2000-2005 Sun Microsystems, Inc. All Rights Reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Sun designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Sun in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
22 * CA 95054 USA or visit www.sun.com if you need additional information or
23 * have any questions.
24 */
25
26/*
27 *
28 * (C) Copyright IBM Corp. 1999 All Rights Reserved.
29 * Copyright 1997 The Open Group Research Institute. All rights reserved.
30 */
31
32package sun.security.krb5.internal.crypto;
33
34import sun.security.krb5.Config;
35import sun.security.krb5.Checksum;
36import sun.security.krb5.EncryptedData;
37import sun.security.krb5.KrbException;
38import sun.security.krb5.KrbCryptoException;
39import sun.security.krb5.internal.*;
40
41public abstract class CksumType {
42
43 private static boolean DEBUG = Krb5.DEBUG;
44
45 public static CksumType getInstance(int cksumTypeConst)
46 throws KdcErrException {
47 CksumType cksumType = null;
48 String cksumTypeName = null;
49 switch (cksumTypeConst) {
50 case Checksum.CKSUMTYPE_CRC32:
51 cksumType = new Crc32CksumType();
52 cksumTypeName = "sun.security.krb5.internal.crypto.Crc32CksumType";
53 break;
54 case Checksum.CKSUMTYPE_DES_MAC:
55 cksumType = new DesMacCksumType();
56 cksumTypeName = "sun.security.krb5.internal.crypto.DesMacCksumType";
57 break;
58 case Checksum.CKSUMTYPE_DES_MAC_K:
59 cksumType = new DesMacKCksumType();
60 cksumTypeName =
61 "sun.security.krb5.internal.crypto.DesMacKCksumType";
62 break;
63 case Checksum.CKSUMTYPE_RSA_MD5:
64 cksumType = new RsaMd5CksumType();
65 cksumTypeName = "sun.security.krb5.internal.crypto.RsaMd5CksumType";
66 break;
67 case Checksum.CKSUMTYPE_RSA_MD5_DES:
68 cksumType = new RsaMd5DesCksumType();
69 cksumTypeName =
70 "sun.security.krb5.internal.crypto.RsaMd5DesCksumType";
71 break;
72
73 case Checksum.CKSUMTYPE_HMAC_SHA1_DES3_KD:
74 cksumType = new HmacSha1Des3KdCksumType();
75 cksumTypeName =
76 "sun.security.krb5.internal.crypto.HmacSha1Des3KdCksumType";
77 break;
78
79 case Checksum.CKSUMTYPE_HMAC_SHA1_96_AES128:
80 cksumType = new HmacSha1Aes128CksumType();
81 cksumTypeName =
82 "sun.security.krb5.internal.crypto.HmacSha1Aes128CksumType";
83 break;
84 case Checksum.CKSUMTYPE_HMAC_SHA1_96_AES256:
85 cksumType = new HmacSha1Aes256CksumType();
86 cksumTypeName =
87 "sun.security.krb5.internal.crypto.HmacSha1Aes256CksumType";
88 break;
89
90 case Checksum.CKSUMTYPE_HMAC_MD5_ARCFOUR:
91 cksumType = new HmacMd5ArcFourCksumType();
92 cksumTypeName =
93 "sun.security.krb5.internal.crypto.HmacMd5ArcFourCksumType";
94 break;
95
96 // currently we don't support MD4.
97 case Checksum.CKSUMTYPE_RSA_MD4_DES_K:
98 // cksumType = new RsaMd4DesKCksumType();
99 // cksumTypeName =
100 // "sun.security.krb5.internal.crypto.RsaMd4DesKCksumType";
101 case Checksum.CKSUMTYPE_RSA_MD4:
102 // cksumType = new RsaMd4CksumType();
103 // linux box support rsamd4, how to solve conflict?
104 // cksumTypeName =
105 // "sun.security.krb5.internal.crypto.RsaMd4CksumType";
106 case Checksum.CKSUMTYPE_RSA_MD4_DES:
107 // cksumType = new RsaMd4DesCksumType();
108 // cksumTypeName =
109 // "sun.security.krb5.internal.crypto.RsaMd4DesCksumType";
110
111 default:
112 throw new KdcErrException(Krb5.KDC_ERR_SUMTYPE_NOSUPP);
113 }
114 if (DEBUG) {
115 System.out.println(">>> CksumType: " + cksumTypeName);
116 }
117 return cksumType;
118 }
119
120
121 /**
122 * Returns default checksum type.
123 */
124 public static CksumType getInstance() throws KdcErrException {
125 // this method provided for Kerberos applications.
126 int cksumType = Checksum.CKSUMTYPE_RSA_MD5; // default
127 try {
128 Config c = Config.getInstance();
129 if ((cksumType = (c.getType(c.getDefault("ap_req_checksum_type",
130 "libdefaults")))) == - 1) {
131 if ((cksumType = c.getType(c.getDefault("checksum_type",
132 "libdefaults"))) == -1) {
133 cksumType = Checksum.CKSUMTYPE_RSA_MD5; // default
134 }
135 }
136 } catch (KrbException e) {
137 }
138 return getInstance(cksumType);
139 }
140
141 public abstract int confounderSize();
142
143 public abstract int cksumType();
144
145 public abstract boolean isSafe();
146
147 public abstract int cksumSize();
148
149 public abstract int keyType();
150
151 public abstract int keySize();
152
153 public abstract byte[] calculateChecksum(byte[] data, int size)
154 throws KrbCryptoException;
155
156 public abstract byte[] calculateKeyedChecksum(byte[] data, int size,
157 byte[] key, int usage) throws KrbCryptoException;
158
159 public abstract boolean verifyKeyedChecksum(byte[] data, int size,
160 byte[] key, byte[] checksum, int usage) throws KrbCryptoException;
161
162 public static boolean isChecksumEqual(byte[] cksum1, byte[] cksum2) {
163 if (cksum1 == cksum2)
164 return true;
165 if ((cksum1 == null && cksum2 != null) ||
166 (cksum1 != null && cksum2 == null))
167 return false;
168 if (cksum1.length != cksum2.length)
169 return false;
170 for (int i = 0; i < cksum1.length; i++)
171 if (cksum1[i] != cksum2[i])
172 return false;
173 return true;
174 }
175
176}