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

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

The newest version!
/*     */ package org.jcp.xml.dsig.internal.dom;
/*     */ 
/*     */ import java.security.InvalidAlgorithmParameterException;
/*     */ import java.security.InvalidKeyException;
/*     */ import java.security.Key;
/*     */ import java.security.SignatureException;
/*     */ import java.security.spec.AlgorithmParameterSpec;
/*     */ import javax.xml.crypto.MarshalException;
/*     */ import javax.xml.crypto.dom.DOMCryptoContext;
/*     */ import javax.xml.crypto.dsig.SignatureMethod;
/*     */ import javax.xml.crypto.dsig.XMLSignContext;
/*     */ import javax.xml.crypto.dsig.XMLSignatureException;
/*     */ import javax.xml.crypto.dsig.XMLValidateContext;
/*     */ import javax.xml.crypto.dsig.spec.SignatureMethodParameterSpec;
/*     */ import org.w3c.dom.Document;
/*     */ import org.w3c.dom.Element;
/*     */ import org.w3c.dom.Node;
/*     */ 
/*     */ public abstract class DOMSignatureMethod extends DOMStructure
/*     */   implements SignatureMethod
/*     */ {
/*     */   private String algorithm;
/*     */   private SignatureMethodParameterSpec params;
/*     */ 
/*     */   protected DOMSignatureMethod(String algorithm, AlgorithmParameterSpec params)
/*     */     throws InvalidAlgorithmParameterException
/*     */   {
/*  44 */     if (algorithm == null) {
/*  45 */       throw new NullPointerException("algorithm cannot be null");
/*     */     }
/*  47 */     if ((params != null) && (!(params instanceof SignatureMethodParameterSpec)))
/*     */     {
/*  49 */       throw new InvalidAlgorithmParameterException("params must be of type SignatureMethodParameterSpec");
/*     */     }
/*     */ 
/*  52 */     checkParams((SignatureMethodParameterSpec)params);
/*  53 */     this.algorithm = algorithm;
/*  54 */     this.params = ((SignatureMethodParameterSpec)params);
/*     */   }
/*     */ 
/*     */   protected DOMSignatureMethod(Element smElem)
/*     */     throws MarshalException
/*     */   {
/*  65 */     this.algorithm = DOMUtils.getAttributeValue(smElem, "Algorithm");
/*  66 */     Element paramsElem = DOMUtils.getFirstChildElement(smElem);
/*  67 */     if (paramsElem != null)
/*  68 */       this.params = unmarshalParams(paramsElem);
/*     */     try
/*     */     {
/*  71 */       checkParams(this.params);
/*     */     } catch (InvalidAlgorithmParameterException iape) {
/*  73 */       throw new MarshalException(iape);
/*     */     }
/*     */   }
/*     */ 
/*     */   static SignatureMethod unmarshal(Element smElem) throws MarshalException {
/*  78 */     String alg = DOMUtils.getAttributeValue(smElem, "Algorithm");
/*  79 */     if (alg.equals("http://www.w3.org/2000/09/xmldsig#hmac-sha1"))
/*  80 */       return new DOMHMACSignatureMethod(smElem);
/*  81 */     if (alg.equals("http://www.w3.org/2000/09/xmldsig#rsa-sha1"))
/*  82 */       return new DOMRSASignatureMethod(smElem);
/*  83 */     if (alg.equals("http://www.w3.org/2000/09/xmldsig#dsa-sha1")) {
/*  84 */       return new DOMDSASignatureMethod(smElem);
/*     */     }
/*  86 */     throw new MarshalException("unsupported signature algorithm: " + alg);
/*     */   }
/*     */ 
/*     */   protected abstract void checkParams(SignatureMethodParameterSpec paramSignatureMethodParameterSpec)
/*     */     throws InvalidAlgorithmParameterException;
/*     */ 
/*     */   public final AlgorithmParameterSpec getParameterSpec()
/*     */   {
/* 102 */     return this.params;
/*     */   }
/*     */ 
/*     */   public final String getAlgorithm() {
/* 106 */     return this.algorithm;
/*     */   }
/*     */ 
/*     */   protected abstract SignatureMethodParameterSpec unmarshalParams(Element paramElement)
/*     */     throws MarshalException;
/*     */ 
/*     */   public void marshal(Node parent, String dsPrefix, DOMCryptoContext context)
/*     */     throws MarshalException
/*     */   {
/* 127 */     Document ownerDoc = DOMUtils.getOwnerDocument(parent);
/*     */ 
/* 129 */     Element smElem = DOMUtils.createElement(ownerDoc, "SignatureMethod", "http://www.w3.org/2000/09/xmldsig#", dsPrefix);
/*     */ 
/* 131 */     DOMUtils.setAttribute(smElem, "Algorithm", this.algorithm);
/*     */ 
/* 133 */     if (this.params != null) {
/* 134 */       marshalParams(smElem, dsPrefix);
/*     */     }
/*     */ 
/* 137 */     parent.appendChild(smElem);
/*     */   }
/*     */ 
/*     */   public abstract boolean verify(Key paramKey, DOMSignedInfo paramDOMSignedInfo, byte[] paramArrayOfByte, XMLValidateContext paramXMLValidateContext)
/*     */     throws InvalidKeyException, SignatureException, XMLSignatureException;
/*     */ 
/*     */   public abstract byte[] sign(Key paramKey, DOMSignedInfo paramDOMSignedInfo, XMLSignContext paramXMLSignContext)
/*     */     throws InvalidKeyException, XMLSignatureException;
/*     */ 
/*     */   protected abstract void marshalParams(Element paramElement, String paramString)
/*     */     throws MarshalException;
/*     */ 
/*     */   protected abstract boolean paramsEqual(AlgorithmParameterSpec paramAlgorithmParameterSpec);
/*     */ 
/*     */   public boolean equals(Object o)
/*     */   {
/* 199 */     if (this == o) {
/* 200 */       return true;
/*     */     }
/*     */ 
/* 203 */     if (!(o instanceof SignatureMethod)) {
/* 204 */       return false;
/*     */     }
/* 206 */     SignatureMethod osm = (SignatureMethod)o;
/*     */ 
/* 208 */     return (this.algorithm.equals(osm.getAlgorithm())) && (paramsEqual(osm.getParameterSpec()));
/*     */   }
/*     */ }

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




© 2015 - 2024 Weber Informatics LLC | Privacy Policy