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

org.jcp.xml.dsig.internal.dom.DOMRSASignatureMethod 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.NoSuchAlgorithmException;
/*     */ import java.security.PrivateKey;
/*     */ import java.security.PublicKey;
/*     */ import java.security.Signature;
/*     */ import java.security.SignatureException;
/*     */ import java.security.spec.AlgorithmParameterSpec;
/*     */ import java.util.logging.Level;
/*     */ import java.util.logging.Logger;
/*     */ import javax.xml.crypto.MarshalException;
/*     */ 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.jcp.xml.dsig.internal.SignerOutputStream;
/*     */ import org.w3c.dom.Element;
/*     */ 
/*     */ public final class DOMRSASignatureMethod extends DOMSignatureMethod
/*     */ {
/*  37 */   private static Logger log = Logger.getLogger("org.jcp.xml.dsig.internal.dom");
/*     */   private Signature signature;
/*     */ 
/*     */   public DOMRSASignatureMethod(AlgorithmParameterSpec params)
/*     */     throws InvalidAlgorithmParameterException
/*     */   {
/*  50 */     super("http://www.w3.org/2000/09/xmldsig#rsa-sha1", params);
/*     */   }
/*     */ 
/*     */   public DOMRSASignatureMethod(Element smElem)
/*     */     throws MarshalException
/*     */   {
/*  59 */     super(smElem);
/*     */   }
/*     */ 
/*     */   protected void checkParams(SignatureMethodParameterSpec params) throws InvalidAlgorithmParameterException
/*     */   {
/*  64 */     if (params != null)
/*  65 */       throw new InvalidAlgorithmParameterException("no parameters should be specified for RSA signature algorithm");
/*     */   }
/*     */ 
/*     */   protected SignatureMethodParameterSpec unmarshalParams(Element paramsElem)
/*     */     throws MarshalException
/*     */   {
/*  72 */     throw new MarshalException("no parameters should be specified for RSA signature algorithm");
/*     */   }
/*     */ 
/*     */   protected void marshalParams(Element parent, String dsPrefix)
/*     */     throws MarshalException
/*     */   {
/*  79 */     throw new MarshalException("no parameters should be specified for RSA signature algorithm");
/*     */   }
/*     */ 
/*     */   protected boolean paramsEqual(AlgorithmParameterSpec spec)
/*     */   {
/*  85 */     return getParameterSpec() == spec;
/*     */   }
/*     */ 
/*     */   public boolean verify(Key key, DOMSignedInfo si, byte[] sig, XMLValidateContext context)
/*     */     throws InvalidKeyException, SignatureException, XMLSignatureException
/*     */   {
/*  91 */     if ((key == null) || (si == null) || (sig == null)) {
/*  92 */       throw new NullPointerException("key, signed info or signature cannot be null");
/*     */     }
/*     */ 
/*  96 */     if (!(key instanceof PublicKey)) {
/*  97 */       throw new InvalidKeyException("key must be PublicKey");
/*     */     }
/*  99 */     if (this.signature == null) {
/*     */       try
/*     */       {
/* 102 */         this.signature = Signature.getInstance("SHA1withRSA");
/*     */       } catch (NoSuchAlgorithmException nsae) {
/* 104 */         throw new SignatureException("SHA1withRSA Signature not found");
/*     */       }
/*     */     }
/* 107 */     this.signature.initVerify((PublicKey)key);
/* 108 */     if (log.isLoggable(Level.FINE)) {
/* 109 */       log.log(Level.FINE, "Signature provider:" + this.signature.getProvider());
/* 110 */       log.log(Level.FINE, "verifying with key: " + key);
/*     */     }
/* 112 */     si.canonicalize(context, new SignerOutputStream(this.signature));
/*     */ 
/* 114 */     return this.signature.verify(sig);
/*     */   }
/*     */ 
/*     */   public byte[] sign(Key key, DOMSignedInfo si, XMLSignContext context) throws InvalidKeyException, XMLSignatureException
/*     */   {
/* 119 */     if ((key == null) || (si == null)) {
/* 120 */       throw new NullPointerException();
/*     */     }
/*     */ 
/* 123 */     if (!(key instanceof PrivateKey)) {
/* 124 */       throw new InvalidKeyException("key must be PrivateKey");
/*     */     }
/* 126 */     if (this.signature == null) {
/*     */       try
/*     */       {
/* 129 */         this.signature = Signature.getInstance("SHA1withRSA");
/*     */       } catch (NoSuchAlgorithmException nsae) {
/* 131 */         throw new InvalidKeyException("SHA1withRSA Signature not found");
/*     */       }
/*     */     }
/* 134 */     this.signature.initSign((PrivateKey)key);
/* 135 */     if (log.isLoggable(Level.FINE)) {
/* 136 */       log.log(Level.FINE, "Signature provider:" + this.signature.getProvider());
/* 137 */       log.log(Level.FINE, "Signing with key: " + key);
/*     */     }
/*     */ 
/* 140 */     si.canonicalize(context, new SignerOutputStream(this.signature));
/*     */     try
/*     */     {
/* 143 */       return this.signature.sign();
/*     */     }
/*     */     catch (SignatureException se) {
/* 146 */       throw new RuntimeException(se.getMessage());
/*     */     }
/*     */   }
/*     */ }

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




© 2015 - 2024 Weber Informatics LLC | Privacy Policy