All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.jcp.xml.dsig.internal.dom.DOMKeyValue Maven / Gradle / Ivy

There is a newer version: 1.0.1-2
Show newest version
/*     */ package org.jcp.xml.dsig.internal.dom;
/*     */ 
/*     */ import java.security.KeyException;
/*     */ import java.security.KeyFactory;
/*     */ import java.security.NoSuchAlgorithmException;
/*     */ import java.security.PublicKey;
/*     */ import java.security.interfaces.DSAParams;
/*     */ import java.security.interfaces.DSAPublicKey;
/*     */ import java.security.interfaces.RSAPublicKey;
/*     */ import java.security.spec.DSAPublicKeySpec;
/*     */ import java.security.spec.InvalidKeySpecException;
/*     */ import java.security.spec.KeySpec;
/*     */ import java.security.spec.RSAPublicKeySpec;
/*     */ import javax.xml.crypto.MarshalException;
/*     */ import javax.xml.crypto.dom.DOMCryptoContext;
/*     */ import javax.xml.crypto.dsig.keyinfo.KeyValue;
/*     */ import org.w3c.dom.Document;
/*     */ import org.w3c.dom.Element;
/*     */ import org.w3c.dom.Node;
/*     */ 
/*     */ public final class DOMKeyValue extends DOMStructure
/*     */   implements KeyValue
/*     */ {
/*     */   private KeyFactory rsakf;
/*     */   private KeyFactory dsakf;
/*     */   private PublicKey publicKey;
/*     */   private javax.xml.crypto.dom.DOMStructure externalPublicKey;
/*     */   private DOMCryptoBinary p;
/*     */   private DOMCryptoBinary q;
/*     */   private DOMCryptoBinary g;
/*     */   private DOMCryptoBinary y;
/*     */   private DOMCryptoBinary j;
/*     */   private DOMCryptoBinary seed;
/*     */   private DOMCryptoBinary pgen;
/*     */   private DOMCryptoBinary modulus;
/*     */   private DOMCryptoBinary exponent;
/*     */ 
/*     */   public DOMKeyValue(PublicKey key)
/*     */     throws KeyException
/*     */   {
/*  47 */     if (key == null) {
/*  48 */       throw new NullPointerException("key cannot be null");
/*     */     }
/*  50 */     this.publicKey = key;
/*  51 */     if ((key instanceof DSAPublicKey)) {
/*  52 */       DSAPublicKey dkey = (DSAPublicKey)key;
/*  53 */       DSAParams params = dkey.getParams();
/*  54 */       this.p = new DOMCryptoBinary(params.getP());
/*  55 */       this.q = new DOMCryptoBinary(params.getQ());
/*  56 */       this.g = new DOMCryptoBinary(params.getG());
/*  57 */       this.y = new DOMCryptoBinary(dkey.getY());
/*  58 */     } else if ((key instanceof RSAPublicKey)) {
/*  59 */       RSAPublicKey rkey = (RSAPublicKey)key;
/*  60 */       this.exponent = new DOMCryptoBinary(rkey.getPublicExponent());
/*  61 */       this.modulus = new DOMCryptoBinary(rkey.getModulus());
/*     */     } else {
/*  63 */       throw new KeyException("unsupported key algorithm: " + key.getAlgorithm());
/*     */     }
/*     */   }
/*     */ 
/*     */   public DOMKeyValue(Element kvElem)
/*     */     throws MarshalException
/*     */   {
/*  74 */     Element kvtElem = DOMUtils.getFirstChildElement(kvElem);
/*  75 */     if (kvtElem.getLocalName().equals("DSAKeyValue")) {
/*  76 */       this.publicKey = unmarshalDSAKeyValue(kvtElem);
/*  77 */     } else if (kvtElem.getLocalName().equals("RSAKeyValue")) {
/*  78 */       this.publicKey = unmarshalRSAKeyValue(kvtElem);
/*     */     } else {
/*  80 */       this.publicKey = null;
/*  81 */       this.externalPublicKey = new javax.xml.crypto.dom.DOMStructure(kvtElem);
/*     */     }
/*     */   }
/*     */ 
/*     */   public PublicKey getPublicKey() throws KeyException {
/*  86 */     if (this.publicKey == null) {
/*  87 */       throw new KeyException("can't convert KeyValue to PublicKey");
/*     */     }
/*  89 */     return this.publicKey;
/*     */   }
/*     */ 
/*     */   public void marshal(Node parent, String dsPrefix, DOMCryptoContext context)
/*     */     throws MarshalException
/*     */   {
/*  95 */     Document ownerDoc = DOMUtils.getOwnerDocument(parent);
/*     */ 
/*  98 */     Element kvElem = DOMUtils.createElement(ownerDoc, "KeyValue", "http://www.w3.org/2000/09/xmldsig#", dsPrefix);
/*     */ 
/* 100 */     marshalPublicKey(kvElem, ownerDoc, dsPrefix, context);
/*     */ 
/* 102 */     parent.appendChild(kvElem);
/*     */   }
/*     */ 
/*     */   private void marshalPublicKey(Node parent, Document doc, String dsPrefix, DOMCryptoContext context) throws MarshalException
/*     */   {
/* 107 */     if (this.publicKey != null) {
/* 108 */       if ((this.publicKey instanceof DSAPublicKey))
/*     */       {
/* 110 */         marshalDSAPublicKey(parent, doc, dsPrefix, context);
/* 111 */       } else if ((this.publicKey instanceof RSAPublicKey))
/*     */       {
/* 113 */         marshalRSAPublicKey(parent, doc, dsPrefix, context);
/*     */       }
/* 115 */       else throw new MarshalException(this.publicKey.getAlgorithm() + " public key algorithm not supported");
/*     */     }
/*     */     else
/*     */     {
/* 119 */       parent.appendChild(this.externalPublicKey.getNode());
/*     */     }
/*     */   }
/*     */ 
/*     */   private void marshalDSAPublicKey(Node parent, Document doc, String dsPrefix, DOMCryptoContext context) throws MarshalException
/*     */   {
/* 125 */     Element dsaElem = DOMUtils.createElement(doc, "DSAKeyValue", "http://www.w3.org/2000/09/xmldsig#", dsPrefix);
/*     */ 
/* 128 */     Element pElem = DOMUtils.createElement(doc, "P", "http://www.w3.org/2000/09/xmldsig#", dsPrefix);
/*     */ 
/* 130 */     Element qElem = DOMUtils.createElement(doc, "Q", "http://www.w3.org/2000/09/xmldsig#", dsPrefix);
/*     */ 
/* 132 */     Element gElem = DOMUtils.createElement(doc, "G", "http://www.w3.org/2000/09/xmldsig#", dsPrefix);
/*     */ 
/* 134 */     Element yElem = DOMUtils.createElement(doc, "Y", "http://www.w3.org/2000/09/xmldsig#", dsPrefix);
/*     */ 
/* 136 */     this.p.marshal(pElem, dsPrefix, context);
/* 137 */     this.q.marshal(qElem, dsPrefix, context);
/* 138 */     this.g.marshal(gElem, dsPrefix, context);
/* 139 */     this.y.marshal(yElem, dsPrefix, context);
/* 140 */     dsaElem.appendChild(pElem);
/* 141 */     dsaElem.appendChild(qElem);
/* 142 */     dsaElem.appendChild(gElem);
/* 143 */     dsaElem.appendChild(yElem);
/* 144 */     parent.appendChild(dsaElem);
/*     */   }
/*     */ 
/*     */   private void marshalRSAPublicKey(Node parent, Document doc, String dsPrefix, DOMCryptoContext context) throws MarshalException
/*     */   {
/* 149 */     Element rsaElem = DOMUtils.createElement(doc, "RSAKeyValue", "http://www.w3.org/2000/09/xmldsig#", dsPrefix);
/*     */ 
/* 151 */     Element modulusElem = DOMUtils.createElement(doc, "Modulus", "http://www.w3.org/2000/09/xmldsig#", dsPrefix);
/*     */ 
/* 153 */     Element exponentElem = DOMUtils.createElement(doc, "Exponent", "http://www.w3.org/2000/09/xmldsig#", dsPrefix);
/*     */ 
/* 155 */     this.modulus.marshal(modulusElem, dsPrefix, context);
/* 156 */     this.exponent.marshal(exponentElem, dsPrefix, context);
/* 157 */     rsaElem.appendChild(modulusElem);
/* 158 */     rsaElem.appendChild(exponentElem);
/* 159 */     parent.appendChild(rsaElem);
/*     */   }
/*     */ 
/*     */   private DSAPublicKey unmarshalDSAKeyValue(Element kvtElem) throws MarshalException
/*     */   {
/* 164 */     if (this.dsakf == null) {
/*     */       try {
/* 166 */         this.dsakf = KeyFactory.getInstance("DSA");
/*     */       } catch (NoSuchAlgorithmException e) {
/* 168 */         throw new RuntimeException("unable to create DSA KeyFactory: " + e.getMessage());
/*     */       }
/*     */     }
/*     */ 
/* 172 */     Element curElem = DOMUtils.getFirstChildElement(kvtElem);
/*     */ 
/* 174 */     if (curElem.getLocalName().equals("P")) {
/* 175 */       this.p = new DOMCryptoBinary(curElem.getFirstChild());
/* 176 */       curElem = DOMUtils.getNextSiblingElement(curElem);
/* 177 */       this.q = new DOMCryptoBinary(curElem.getFirstChild());
/* 178 */       curElem = DOMUtils.getNextSiblingElement(curElem);
/*     */     }
/* 180 */     if (curElem.getLocalName().equals("G")) {
/* 181 */       this.g = new DOMCryptoBinary(curElem.getFirstChild());
/* 182 */       curElem = DOMUtils.getNextSiblingElement(curElem);
/*     */     }
/* 184 */     this.y = new DOMCryptoBinary(curElem.getFirstChild());
/* 185 */     curElem = DOMUtils.getNextSiblingElement(curElem);
/* 186 */     if ((curElem != null) && (curElem.getLocalName().equals("J"))) {
/* 187 */       this.j = new DOMCryptoBinary(curElem.getFirstChild());
/* 188 */       curElem = DOMUtils.getNextSiblingElement(curElem);
/*     */     }
/* 190 */     if (curElem != null) {
/* 191 */       this.seed = new DOMCryptoBinary(curElem.getFirstChild());
/* 192 */       curElem = DOMUtils.getNextSiblingElement(curElem);
/* 193 */       this.pgen = new DOMCryptoBinary(curElem.getFirstChild());
/*     */     }
/*     */ 
/* 196 */     DSAPublicKeySpec spec = new DSAPublicKeySpec(this.y.getBigNum(), this.p.getBigNum(), this.q.getBigNum(), this.g.getBigNum());
/*     */ 
/* 198 */     return (DSAPublicKey)generatePublicKey(this.dsakf, spec);
/*     */   }
/*     */ 
/*     */   private RSAPublicKey unmarshalRSAKeyValue(Element kvtElem) throws MarshalException
/*     */   {
/* 203 */     if (this.rsakf == null) {
/*     */       try {
/* 205 */         this.rsakf = KeyFactory.getInstance("RSA");
/*     */       } catch (NoSuchAlgorithmException e) {
/* 207 */         throw new RuntimeException("unable to create RSA KeyFactory: " + e.getMessage());
/*     */       }
/*     */     }
/*     */ 
/* 211 */     Element modulusElem = DOMUtils.getFirstChildElement(kvtElem);
/* 212 */     this.modulus = new DOMCryptoBinary(modulusElem.getFirstChild());
/* 213 */     Element exponentElem = DOMUtils.getNextSiblingElement(modulusElem);
/* 214 */     this.exponent = new DOMCryptoBinary(exponentElem.getFirstChild());
/* 215 */     RSAPublicKeySpec spec = new RSAPublicKeySpec(this.modulus.getBigNum(), this.exponent.getBigNum());
/*     */ 
/* 217 */     return (RSAPublicKey)generatePublicKey(this.rsakf, spec);
/*     */   }
/*     */ 
/*     */   private PublicKey generatePublicKey(KeyFactory kf, KeySpec keyspec) {
/*     */     try {
/* 222 */       return kf.generatePublic(keyspec);
/*     */     } catch (InvalidKeySpecException e) {
/*     */     }
/* 225 */     return null;
/*     */   }
/*     */ 
/*     */   public boolean equals(Object obj)
/*     */   {
/* 230 */     if (this == obj) {
/* 231 */       return true;
/*     */     }
/* 233 */     if (!(obj instanceof KeyValue))
/* 234 */       return false;
/*     */     try
/*     */     {
/* 237 */       KeyValue kv = (KeyValue)obj;
/* 238 */       if (this.publicKey == null) {
/* 239 */         if (kv.getPublicKey() != null)
/* 240 */           return false;
/*     */       }
/* 242 */       else if (!this.publicKey.equals(kv.getPublicKey()))
/* 243 */         return false;
/*     */     }
/*     */     catch (KeyException ke)
/*     */     {
/* 247 */       return false;
/*     */     }
/*     */ 
/* 250 */     return true;
/*     */   }
/*     */ }

/* Location:           E:\HYN\Java\trunk\ref\lib-dep\xmldsig\xmldsig.jar
 * Qualified Name:     org.jcp.xml.dsig.internal.dom.DOMKeyValue
 * JD-Core Version:    0.6.2
 */




© 2015 - 2024 Weber Informatics LLC | Privacy Policy