001: /*
002: * DigestClientAlgorithm.java
003: *
004: * Created on January 7, 2003, 10:45 AM
005: */
006:
007: package examples.authorization;
008:
009: import java.security.*;
010:
011: /**
012: * Get this interface from the nist-sip IM
013: * @author olivier deruelle
014: */
015: public class DigestClientAuthenticationMethod implements
016: ClientAuthenticationMethod {
017:
018: private String realm;
019: private String userName;
020: private String uri;
021: private String nonce;
022: private String password;
023: private String method;
024: private String cnonce;
025: private MessageDigest messageDigest;
026:
027: /**
028: * to hex converter
029: */
030: private static final char[] toHex = { '0', '1', '2', '3', '4', '5',
031: '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
032:
033: /**
034: * convert an array of bytes to an hexadecimal string
035: * @return a string
036: * @param b bytes array to convert to a hexadecimal
037: * string
038: */
039:
040: public static String toHexString(byte b[]) {
041: int pos = 0;
042: char[] c = new char[b.length * 2];
043: for (int i = 0; i < b.length; i++) {
044: c[pos++] = toHex[(b[i] >> 4) & 0x0F];
045: c[pos++] = toHex[b[i] & 0x0f];
046: }
047: return new String(c);
048: }
049:
050: public void initialize(String realm, String userName, String uri,
051: String nonce, String password, String method,
052: String cnonce, String algorithm) throws Exception {
053: if (realm == null)
054: throw new Exception("The realm parameter is null");
055: this .realm = realm;
056: if (userName == null)
057: throw new Exception("The userName parameter is null");
058: this .userName = userName;
059: if (uri == null)
060: throw new Exception("The uri parameter is null");
061: this .uri = uri;
062: if (nonce == null)
063: throw new Exception("The nonce parameter is null");
064: this .nonce = nonce;
065: if (password == null)
066: throw new Exception("The password parameter is null");
067: this .password = password;
068: if (method == null)
069: throw new Exception("The method parameter is null");
070: this .method = method;
071: this .cnonce = cnonce;
072: if (algorithm == null)
073: throw new Exception("The algorithm parameter is null");
074: try {
075: messageDigest = MessageDigest.getInstance(algorithm);
076: } catch (NoSuchAlgorithmException ex) {
077: System.out
078: .println("DEBUG, DigestClientAuthenticationMethod, initialize(): "
079: + "ERROR: Digest algorithm does not exist.");
080: throw new Exception(
081: "ERROR: Digest algorithm does not exist.");
082: }
083: }
084:
085: /**
086: * generate the response
087: */
088: public String generateResponse() {
089: if (userName == null) {
090: System.out
091: .println("DEBUG, DigestClientAuthenticationMethod, generateResponse(): "
092: + "ERROR: no userName parameter");
093: return null;
094: }
095: if (realm == null) {
096: System.out
097: .println("DEBUG, DigestClientAuthenticationMethod, generateResponse(): "
098: + "ERROR: no realm parameter");
099: return null;
100: }
101:
102: System.out
103: .println("DEBUG, DigestClientAuthenticationMethod, generateResponse(): "
104: + "Trying to generate a response for the user: "
105: + userName + " , with " + "the realm: " + realm);
106:
107: if (password == null) {
108: System.out
109: .println("DEBUG, DigestClientAuthenticationMethod, generateResponse(): "
110: + "ERROR: no password parameter");
111: return null;
112: }
113: if (method == null) {
114: System.out
115: .println("DEBUG, DigestClientAuthenticationMethod, generateResponse(): "
116: + "ERROR: no method parameter");
117: return null;
118: }
119: if (uri == null) {
120: System.out
121: .println("DEBUG, DigestClientAuthenticationMethod, generateResponse(): "
122: + "ERROR: no uri parameter");
123: return null;
124: }
125: if (nonce == null) {
126: System.out
127: .println("DEBUG, DigestClientAuthenticationMethod, generateResponse(): "
128: + "ERROR: no nonce parameter");
129: return null;
130: }
131: if (messageDigest == null) {
132: System.out
133: .println("DEBUG, DigestClientAuthenticationMethod, generateResponse(): "
134: + "ERROR: the algorithm is not set");
135: return null;
136: }
137:
138: /******* GENERATE RESPONSE ************************************/
139: System.out
140: .println("DEBUG, DigestClientAuthenticationMethod, generateResponse(), userName:"
141: + userName + "!");
142: System.out
143: .println("DEBUG, DigestClientAuthenticationMethod, generateResponse(), realm:"
144: + realm + "!");
145: System.out
146: .println("DEBUG, DigestClientAuthenticationMethod, generateResponse(), password:"
147: + password + "!");
148: System.out
149: .println("DEBUG, DigestClientAuthenticationMethod, generateResponse(), uri:"
150: + uri + "!");
151: System.out
152: .println("DEBUG, DigestClientAuthenticationMethod, generateResponse(), nonce:"
153: + nonce + "!");
154: System.out
155: .println("DEBUG, DigestClientAuthenticationMethod, generateResponse(), method:"
156: + method + "!");
157: // A1
158: String A1 = userName + ":" + realm + ":" + password;
159: byte mdbytes[] = messageDigest.digest(A1.getBytes());
160: String HA1 = toHexString(mdbytes);
161: System.out
162: .println("DEBUG, DigestClientAuthenticationMethod, generateResponse(), HA1:"
163: + HA1 + "!");
164: //A2
165: String A2 = method.toUpperCase() + ":" + uri;
166: mdbytes = messageDigest.digest(A2.getBytes());
167: String HA2 = toHexString(mdbytes);
168: System.out
169: .println("DEBUG, DigestClientAuthenticationMethod, generateResponse(), HA2:"
170: + HA2 + "!");
171: //KD
172: String KD = HA1 + ":" + nonce;
173: if (cnonce != null) {
174: if (cnonce.length() > 0)
175: KD += ":" + cnonce;
176: }
177: KD += ":" + HA2;
178: mdbytes = messageDigest.digest(KD.getBytes());
179: String response = toHexString(mdbytes);
180:
181: System.out
182: .println("DEBUG, DigestClientAlgorithm, generateResponse():"
183: + " response generated: " + response);
184:
185: return response;
186: }
187:
188: }
|