/*
* JBoss, Home of Professional Open Source.
* Copyright 2008, Red Hat Middleware LLC, and individual contributors
* as indicated by the @author tags. See the copyright.txt file in the
* distribution for a full listing of individual contributors.
*
* This is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this software; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
*/
package org.picketlink.identity.xmlsec.w3.xmldsig;
import java.math.BigInteger;
import java.security.KeyFactory;
import java.security.interfaces.DSAPrivateKey;
import java.security.interfaces.DSAPublicKey;
import java.security.spec.DSAPrivateKeySpec;
import java.security.spec.DSAPublicKeySpec;
import org.picketlink.identity.federation.core.exceptions.ProcessingException;
import org.picketlink.identity.federation.core.util.Base64;
import org.picketlink.identity.federation.core.wstrust.WSTrustConstants;
/**
* <p>
* Java class for DSAKeyValueType complex type.
*
* <p>
* The following schema fragment specifies the expected content contained within this class.
*
* <pre>
* <complexType name="DSAKeyValueType">
* <complexContent>
* <restriction base="{http://www.w3.org/2001/XMLSchema}anyType">
* <sequence>
* <sequence minOccurs="0">
* <element name="P" type="{http://www.w3.org/2000/09/xmldsig#}CryptoBinary"/>
* <element name="Q" type="{http://www.w3.org/2000/09/xmldsig#}CryptoBinary"/>
* </sequence>
* <element name="G" type="{http://www.w3.org/2000/09/xmldsig#}CryptoBinary" minOccurs="0"/>
* <element name="Y" type="{http://www.w3.org/2000/09/xmldsig#}CryptoBinary"/>
* <element name="J" type="{http://www.w3.org/2000/09/xmldsig#}CryptoBinary" minOccurs="0"/>
* <sequence minOccurs="0">
* <element name="Seed" type="{http://www.w3.org/2000/09/xmldsig#}CryptoBinary"/>
* <element name="PgenCounter" type="{http://www.w3.org/2000/09/xmldsig#}CryptoBinary"/>
* </sequence>
* </sequence>
* </restriction>
* </complexContent>
* </complexType>
* </pre>
*
*
*/
public class DSAKeyValueType implements KeyValueType {
protected byte[] p;
protected byte[] q;
protected byte[] g;
protected byte[] y;
protected byte[] j;
protected byte[] seed;
protected byte[] pgenCounter;
/**
* Gets the value of the p property.
*
* @return possible object is byte[]
*/
public byte[] getP() {
return p;
}
/**
* Sets the value of the p property.
*
* @param value allowed object is byte[]
*/
public void setP(byte[] value) {
this.p = ((byte[]) value);
}
/**
* Gets the value of the q property.
*
* @return possible object is byte[]
*/
public byte[] getQ() {
return q;
}
/**
* Sets the value of the q property.
*
* @param value allowed object is byte[]
*/
public void setQ(byte[] value) {
this.q = ((byte[]) value);
}
/**
* Gets the value of the g property.
*
* @return possible object is byte[]
*/
public byte[] getG() {
return g;
}
/**
* Sets the value of the g property.
*
* @param value allowed object is byte[]
*/
public void setG(byte[] value) {
this.g = ((byte[]) value);
}
/**
* Gets the value of the y property.
*
* @return possible object is byte[]
*/
public byte[] getY() {
return y;
}
/**
* Sets the value of the y property.
*
* @param value allowed object is byte[]
*/
public void setY(byte[] value) {
this.y = ((byte[]) value);
}
/**
* Gets the value of the j property.
*
* @return possible object is byte[]
*/
public byte[] getJ() {
return j;
}
/**
* Sets the value of the j property.
*
* @param value allowed object is byte[]
*/
public void setJ(byte[] value) {
this.j = ((byte[]) value);
}
/**
* Gets the value of the seed property.
*
* @return possible object is byte[]
*/
public byte[] getSeed() {
return seed;
}
/**
* Sets the value of the seed property.
*
* @param value allowed object is byte[]
*/
public void setSeed(byte[] value) {
this.seed = ((byte[]) value);
}
/**
* Gets the value of the pgenCounter property.
*
* @return possible object is byte[]
*/
public byte[] getPgenCounter() {
return pgenCounter;
}
/**
* Sets the value of the pgenCounter property.
*
* @param value allowed object is byte[]
*/
public void setPgenCounter(byte[] value) {
this.pgenCounter = ((byte[]) value);
}
/**
* Convert to the JDK representation of a DSA Public Key
* @return
* @throws ProcessingException
*/
public DSAPublicKey convertToPublicKey() throws ProcessingException{
BigInteger BigY, BigP, BigQ, BigG;
BigY = new BigInteger(1, massage(Base64.decode(new String(y))));
BigP = new BigInteger(1, massage(Base64.decode(new String(p))));
BigQ = new BigInteger(1, massage(Base64.decode(new String(q))));
BigG = new BigInteger(1, massage(Base64.decode(new String(g))));
try {
KeyFactory dsaKeyFactory = KeyFactory.getInstance("dsa");
DSAPublicKeySpec kspec = new DSAPublicKeySpec(BigY, BigP, BigQ, BigG);
return (DSAPublicKey) dsaKeyFactory.generatePublic(kspec);
} catch (Exception e) {
throw new ProcessingException(e);
}
}
/**
* Convert to the JDK representation of a DSA Private Key
* @return
* @throws ProcessingException
*/
public DSAPrivateKey convertToPrivateKey() throws ProcessingException{
BigInteger BigY, BigP, BigQ, BigG;
BigY = new BigInteger(1, massage(Base64.decode(new String(y))));
BigP = new BigInteger(1, massage(Base64.decode(new String(p))));
BigQ = new BigInteger(1, massage(Base64.decode(new String(q))));
BigG = new BigInteger(1, massage(Base64.decode(new String(g))));
try {
KeyFactory dsaKeyFactory = KeyFactory.getInstance("dsa");
DSAPrivateKeySpec kspec = new DSAPrivateKeySpec(BigY, BigP, BigQ, BigG);
return (DSAPrivateKey) dsaKeyFactory.generatePrivate(kspec);
} catch (Exception e) {
throw new ProcessingException(e);
}
}
public String toString(){
String prefix = WSTrustConstants.XMLDSig.DSIG_PREFIX;
String colon = ":";
String left = "<";
String right = ">";
String slash = "/";
StringBuilder sb = new StringBuilder();
sb.append(left).append(prefix).append(colon).append(WSTrustConstants.XMLDSig.DSA_KEYVALUE).append(right);
if(p != null){
sb.append(left).append(prefix).append(colon).append(WSTrustConstants.XMLDSig.P).append(right);
sb.append(new String(getP()));
sb.append(left).append(slash).append(prefix).append(colon).append(WSTrustConstants.XMLDSig.P).append(right);
}
if(q != null){
sb.append(left).append(prefix).append(colon).append(WSTrustConstants.XMLDSig.Q).append(right);
sb.append(new String(getQ()));
sb.append(left).append(slash).append(prefix).append(colon).append(WSTrustConstants.XMLDSig.Q).append(right);
}
if(g != null){
sb.append(left).append(prefix).append(colon).append(WSTrustConstants.XMLDSig.G).append(right);
sb.append(new String(getG()));
sb.append(left).append(slash).append(prefix).append(colon).append(WSTrustConstants.XMLDSig.G).append(right);
}
if(y != null){
sb.append(left).append(prefix).append(colon).append(WSTrustConstants.XMLDSig.Y).append(right);
sb.append(new String(getY()));
sb.append(left).append(slash).append(prefix).append(colon).append(WSTrustConstants.XMLDSig.Y).append(right);
}
if(seed != null){
sb.append(left).append(prefix).append(colon).append(WSTrustConstants.XMLDSig.SEED).append(right);
sb.append(new String(getSeed()));
sb.append(left).append(slash).append(prefix).append(colon).append(WSTrustConstants.XMLDSig.SEED).append(right);
}
if(pgenCounter != null){
sb.append(left).append(prefix).append(colon).append(WSTrustConstants.XMLDSig.PGEN_COUNTER).append(right);
sb.append(new String(getPgenCounter()));
sb.append(left).append(slash).append(prefix).append(colon).append(WSTrustConstants.XMLDSig.PGEN_COUNTER).append(right);
}
sb.append(left).append(slash).append(prefix).append(colon).append(WSTrustConstants.XMLDSig.DSA_KEYVALUE).append(right);
return sb.toString();
}
private byte[] massage(byte[] byteArray){
if (byteArray[0] == 0){
byte[] substring = new byte[byteArray.length - 1];
System.arraycopy(byteArray, 1, substring, 0, byteArray.length - 1);
return substring;
}
return byteArray;
}
}