001: /*
002: * DerivedKeyTokenImpl.java
003: *
004: * Created on December 23, 2005, 7:11 PM
005: */
006:
007: /*
008: * The contents of this file are subject to the terms
009: * of the Common Development and Distribution License
010: * (the License). You may not use this file except in
011: * compliance with the License.
012: *
013: * You can obtain a copy of the license at
014: * https://glassfish.dev.java.net/public/CDDLv1.0.html.
015: * See the License for the specific language governing
016: * permissions and limitations under the License.
017: *
018: * When distributing Covered Code, include this CDDL
019: * Header Notice in each file and include the License file
020: * at https://glassfish.dev.java.net/public/CDDLv1.0.html.
021: * If applicable, add the following below the CDDL Header,
022: * with the fields enclosed by brackets [] replaced by
023: * you own identifying information:
024: * "Portions Copyrighted [year] [name of copyright owner]"
025: *
026: * Copyright 2006 Sun Microsystems Inc. All Rights Reserved
027: */
028:
029: package com.sun.xml.ws.security.impl;
030:
031: import com.sun.xml.ws.security.DerivedKeyToken;
032: import com.sun.xml.wss.impl.misc.SecurityUtil;
033: import java.io.UnsupportedEncodingException;
034: import java.net.URI;
035: import java.net.URISyntaxException;
036: import java.security.InvalidKeyException;
037: import java.security.SecureRandom;
038: import java.security.NoSuchAlgorithmException;
039: import javax.crypto.SecretKey;
040: import javax.crypto.spec.SecretKeySpec;
041:
042: /**
043: *
044: * @author Ashutosh Shahi
045: */
046: public class DerivedKeyTokenImpl implements DerivedKeyToken {
047:
048: private long length = 32; // Default length
049: private long offset = 0; // Default offset
050: private long generation = 0;
051: private String label = this .DEFAULT_DERIVEDKEYTOKEN_LABEL;
052: private byte[] secret, nonce;
053:
054: /** Creates a new instance of DerivedKeyTokenImpl */
055: public DerivedKeyTokenImpl(long offset, long length, byte[] secret) {
056: this .offset = offset;
057: this .length = length;
058: this .secret = secret;
059: try {
060: nonce = new byte[18];
061: SecureRandom random = SecureRandom.getInstance("SHA1PRNG");
062: random.nextBytes(nonce);
063: } catch (NoSuchAlgorithmException e) {
064: throw new RuntimeException("No such algorithm found"
065: + e.getMessage());
066: }
067: }
068:
069: public DerivedKeyTokenImpl(long offset, long length, byte[] secret,
070: byte[] nonce) {
071: this .offset = offset;
072: this .length = length;
073: this .secret = secret;
074: this .nonce = nonce;
075: }
076:
077: public DerivedKeyTokenImpl(long generation, byte[] secret) {
078: this .generation = generation;
079: this .secret = secret;
080: try {
081: nonce = new byte[18];
082: SecureRandom random = SecureRandom.getInstance("SHA1PRNG");
083: random.nextBytes(nonce);
084: } catch (NoSuchAlgorithmException e) {
085: throw new RuntimeException("No such algorithm found"
086: + e.getMessage());
087: }
088: }
089:
090: public URI getAlgorithm() {
091: try {
092: return new URI(this .DEFAULT_DERIVED_KEY_TOKEN_ALGORITHM);
093: } catch (URISyntaxException ex) {
094: //ignore
095: }
096: return null;
097: }
098:
099: public long getLength() {
100: return length;
101: }
102:
103: public long getOffset() {
104: return offset;
105: }
106:
107: public String getType() {
108: return this .DERIVED_KEY_TOKEN_TYPE;
109: }
110:
111: public Object getTokenValue() {
112: //TODO: implement this method
113: return null;
114: }
115:
116: public long getGeneration() {
117: return generation;
118: }
119:
120: public String getLabel() {
121: return label;
122: }
123:
124: public byte[] getNonce() {
125: return nonce;
126: }
127:
128: public SecretKey generateSymmetricKey(String algorithm)
129: throws InvalidKeyException, NoSuchAlgorithmException,
130: UnsupportedEncodingException {
131:
132: byte[] temp = label.getBytes("UTF-8");
133: byte[] seed = new byte[temp.length + nonce.length];
134: System.arraycopy(temp, 0, seed, 0, temp.length);
135: System.arraycopy(nonce, 0, seed, temp.length, nonce.length);
136:
137: byte[] tempBytes = SecurityUtil.P_SHA1(secret, seed,
138: (int) (offset + length));
139: byte[] key = new byte[(int) length];
140:
141: for (int i = 0; i < key.length; i++)
142: key[i] = tempBytes[i + (int) offset];
143:
144: SecretKeySpec keySpec = new SecretKeySpec(key, algorithm);
145: return (SecretKey) keySpec;
146:
147: }
148:
149: }
|