KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > maverick > ssl > SSLCipherSuiteWithMD5MAC


1 /*
2  * SSL-Explorer
3  *
4  * Copyright (C) 2003-2006 3SP LTD. All Rights Reserved
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2 of
9  * the License, or (at your option) any later version.
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public
16  * License along with this program; if not, write to the Free Software
17  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18  */

19             
20 package com.maverick.ssl;
21
22 import com.maverick.crypto.digests.MD5Digest;
23
24 /**
25  * An abstract {@link SSLCipherSuite} that uses an MD5 message digest.
26  *
27  * @author Lee David Painter <a HREF="mailto:lee@3sp.com">&lt;lee@3sp.com&gt;</a>
28  */

29 public abstract class SSLCipherSuiteWithMD5MAC implements SSLCipherSuite {
30
31     MD5Digest generateDigest = new MD5Digest();
32     MD5Digest verifyDigest = new MD5Digest();
33
34     byte[] encryptMAC;
35     byte[] decryptMAC;
36
37     byte[] padding1 = new byte[] { 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36,
38         0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36,
39         0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36 };
40
41     byte[] padding2 = new byte[] { 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c,
42         0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c,
43         0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c };
44
45     public SSLCipherSuiteWithMD5MAC() {
46
47     }
48
49     public abstract int getKeyLength();
50
51     public abstract int getIVLength();
52
53     public abstract void encrypt(byte[] b, int offset, int len);
54
55     public abstract void decrypt(byte[] b, int offset, int len);
56
57     public final void init(byte[] encryptKey, byte[] encryptIV, byte[] encryptMAC, byte[] decryptKey, byte[] decryptIV,
58                            byte[] decryptMAC) {
59         this.encryptMAC = encryptMAC;
60         this.decryptMAC = decryptMAC;
61
62         init(encryptKey, encryptIV, decryptKey, decryptIV);
63     }
64
65     protected abstract void init(byte[] encryptKey, byte[] encryptIV, byte[] decryptKey, byte[] decryptIV);
66
67     public final int getMACLength() {
68         return generateDigest.getDigestSize();
69     }
70
71     public byte[] generateMAC(byte[] b, int offset, int len, int type, long sequenceNo) {
72         return calculateMAC(generateDigest, b, offset, len, type, sequenceNo, encryptMAC);
73     }
74
75     public boolean verifyMAC(byte[] b, int offset, int len, int type, long sequenceNo, byte[] mac, int macoff, int maclen) {
76         byte[] gen = calculateMAC(verifyDigest, b, offset, len, type, sequenceNo, decryptMAC);
77         for (int i = 0; i < gen.length; i++) {
78             if (gen[i] != mac[i + macoff]) {
79                 return false;
80             }
81         }
82         return true;
83     }
84
85     private byte[] calculateMAC(MD5Digest digest, byte[] b, int off, int len, int type, long sequenceNo, byte[] key) {
86         digest.reset();
87
88         digest.update(key, 0, key.length);
89         digest.update(padding1, 0, padding1.length);
90         digest.update((byte) ((sequenceNo >> 56) & 0xFF));
91         digest.update((byte) ((sequenceNo >> 48) & 0xFF));
92         digest.update((byte) ((sequenceNo >> 40) & 0xFF));
93         digest.update((byte) ((sequenceNo >> 32) & 0xFF));
94         digest.update((byte) ((sequenceNo >> 24) & 0xFF));
95         digest.update((byte) ((sequenceNo >> 16) & 0xFF));
96         digest.update((byte) ((sequenceNo >> 8) & 0xFF));
97         digest.update((byte) ((sequenceNo >> 0) & 0xFF));
98
99         digest.update((byte) type);
100         digest.update((byte) ((len >> 8) & 0xFF));
101         digest.update((byte) (len & 0xFF));
102
103         digest.update(b, off, len);
104
105         byte[] temp = new byte[digest.getDigestSize()];
106         digest.doFinal(temp, 0);
107
108         digest.reset();
109
110         digest.update(key, 0, key.length);
111         digest.update(padding2, 0, padding2.length);
112         digest.update(temp, 0, temp.length);
113
114         digest.doFinal(temp, 0);
115
116         return temp;
117
118     }
119
120 }
Popular Tags