/* * Copyright 2006-2010 The Apache Software Foundation. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * */ /* * Copyright 2005 Sun Microsystems, Inc. All rights reserved. */ package javax.xml.crypto.test; import java.io.*; import java.security.*; import java.security.cert.*; import java.util.*; import javax.xml.crypto.*; import javax.xml.crypto.dsig.*; import javax.xml.crypto.dsig.keyinfo.*; import javax.crypto.SecretKey; /** * This is a class which supplies several KeySelector implementations * * @author Sean Mullan * @author Valerie Peng */ public class KeySelectors { /** * KeySelector which would always return the secret key specified in its * constructor. */ public static class SecretKeySelector extends KeySelector { private SecretKey key; public SecretKeySelector(byte[] bytes) { key = wrapBytes(bytes); } public SecretKeySelector(SecretKey key) { this.key = key; } public KeySelectorResult select(KeyInfo ki, KeySelector.Purpose purpose, AlgorithmMethod method, XMLCryptoContext context) throws KeySelectorException { return new SimpleKSResult(key); } private SecretKey wrapBytes(final byte[] bytes) { return new SecretKey() { public String getFormat() { return "RAW"; } public String getAlgorithm() { return "Secret key"; } public byte[] getEncoded() { return (byte[]) bytes.clone(); } }; } } /** * KeySelector which would retrieve the X509Certificate out of the * KeyInfo element and return the public key. * NOTE: If there is an X509CRL in the KeyInfo element, then revoked * certificate will be ignored. */ public static class RawX509KeySelector extends KeySelector { public KeySelectorResult select(KeyInfo keyInfo, KeySelector.Purpose purpose, AlgorithmMethod method, XMLCryptoContext context) throws KeySelectorException { if (keyInfo == null) { throw new KeySelectorException("Null KeyInfo object!"); } // search for X509Data in keyinfo Iterator iter = keyInfo.getContent().iterator(); while (iter.hasNext()) { XMLStructure kiType = (XMLStructure) iter.next(); if (kiType instanceof X509Data) { X509Data xd = (X509Data) kiType; Object[] entries = xd.getContent().toArray(); X509CRL crl = null; // Looking for CRL before finding certificates for (int i = 0; (i> 4) & 0x0f); char[] res = new char[2]; res[0] = mapping.charAt(high); res[1] = mapping.charAt(low); return new String(res); } public static String dumpArray(byte[] in) { int numDumped = 0; StringBuffer buf = new StringBuffer(512); buf.append("{"); for (int i=0;i<(in.length/numBytesPerRow); i++) { for (int j=0; j<(numBytesPerRow); j++) { buf.append("(byte)0x" + getHex(in[i*numBytesPerRow+j]) + ", "); } numDumped += numBytesPerRow; } while (numDumped < in.length) { buf.append("(byte)0x" + getHex(in[numDumped]) + " "); numDumped += 1; } buf.append("}"); return buf.toString(); } } } class SimpleKSResult implements KeySelectorResult { private final Key key; SimpleKSResult(Key key) { this.key = key; } public Key getKey() { return key; } }