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

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

The newest version!
/*     */ package org.jcp.xml.dsig.internal.dom;
/*     */ 
/*     */ import com.sun.org.apache.xml.internal.security.utils.Base64;
/*     */ import com.sun.org.apache.xml.internal.security.utils.UnsyncBufferedOutputStream;
/*     */ import java.io.ByteArrayInputStream;
/*     */ import java.io.ByteArrayOutputStream;
/*     */ import java.io.IOException;
/*     */ import java.io.InputStream;
/*     */ import java.io.InputStreamReader;
/*     */ import java.io.OutputStream;
/*     */ import java.util.ArrayList;
/*     */ import java.util.Collections;
/*     */ import java.util.List;
/*     */ import java.util.logging.Level;
/*     */ import java.util.logging.Logger;
/*     */ import javax.xml.crypto.MarshalException;
/*     */ import javax.xml.crypto.OctetStreamData;
/*     */ import javax.xml.crypto.XMLCryptoContext;
/*     */ import javax.xml.crypto.dom.DOMCryptoContext;
/*     */ import javax.xml.crypto.dsig.CanonicalizationMethod;
/*     */ import javax.xml.crypto.dsig.Reference;
/*     */ import javax.xml.crypto.dsig.SignatureMethod;
/*     */ import javax.xml.crypto.dsig.SignedInfo;
/*     */ import javax.xml.crypto.dsig.TransformException;
/*     */ import javax.xml.crypto.dsig.XMLSignatureException;
/*     */ import org.w3c.dom.Document;
/*     */ import org.w3c.dom.Element;
/*     */ import org.w3c.dom.Node;
/*     */ 
/*     */ public final class DOMSignedInfo extends DOMStructure
/*     */   implements SignedInfo
/*     */ {
/*  37 */   private static Logger log = Logger.getLogger("org.jcp.xml.dsig.internal.dom");
/*     */   private List references;
/*     */   private CanonicalizationMethod canonicalizationMethod;
/*     */   private SignatureMethod signatureMethod;
/*     */   private String id;
/*     */   private Document ownerDoc;
/*     */   private Element localSiElem;
/*     */   private InputStream canonData;
/*     */ 
/*     */   public DOMSignedInfo(CanonicalizationMethod cm, SignatureMethod sm, List references)
/*     */   {
/*  62 */     if ((cm == null) || (sm == null) || (references == null)) {
/*  63 */       throw new NullPointerException();
/*     */     }
/*  65 */     this.canonicalizationMethod = cm;
/*  66 */     this.signatureMethod = sm;
/*  67 */     this.references = Collections.unmodifiableList(new ArrayList(references));
/*     */ 
/*  69 */     if (this.references.isEmpty()) {
/*  70 */       throw new IllegalArgumentException("list of references must contain at least one entry");
/*     */     }
/*     */ 
/*  73 */     int i = 0; for (int size = this.references.size(); i < size; i++) {
/*  74 */       Object obj = this.references.get(i);
/*  75 */       if (!(obj instanceof Reference))
/*  76 */         throw new ClassCastException("list of references contains an illegal type");
/*     */     }
/*     */   }
/*     */ 
/*     */   public DOMSignedInfo(CanonicalizationMethod cm, SignatureMethod sm, List references, String id)
/*     */   {
/*  99 */     this(cm, sm, references);
/* 100 */     this.id = id;
/*     */   }
/*     */ 
/*     */   public DOMSignedInfo(Element siElem, XMLCryptoContext context)
/*     */     throws MarshalException
/*     */   {
/* 110 */     this.localSiElem = siElem;
/* 111 */     this.ownerDoc = siElem.getOwnerDocument();
/*     */ 
/* 114 */     this.id = DOMUtils.getAttributeValue(siElem, "Id");
/*     */ 
/* 117 */     Element cmElem = DOMUtils.getFirstChildElement(siElem);
/* 118 */     this.canonicalizationMethod = new DOMCanonicalizationMethod(cmElem, context);
/*     */ 
/* 121 */     Element smElem = DOMUtils.getNextSiblingElement(cmElem);
/* 122 */     this.signatureMethod = DOMSignatureMethod.unmarshal(smElem);
/*     */ 
/* 125 */     ArrayList refList = new ArrayList(5);
/* 126 */     Element refElem = DOMUtils.getNextSiblingElement(smElem);
/* 127 */     while (refElem != null) {
/* 128 */       refList.add(new DOMReference(refElem, context));
/* 129 */       refElem = DOMUtils.getNextSiblingElement(refElem);
/*     */     }
/* 131 */     this.references = Collections.unmodifiableList(refList);
/*     */   }
/*     */ 
/*     */   public CanonicalizationMethod getCanonicalizationMethod() {
/* 135 */     return this.canonicalizationMethod;
/*     */   }
/*     */ 
/*     */   public SignatureMethod getSignatureMethod() {
/* 139 */     return this.signatureMethod;
/*     */   }
/*     */ 
/*     */   public String getId() {
/* 143 */     return this.id;
/*     */   }
/*     */ 
/*     */   public List getReferences() {
/* 147 */     return this.references;
/*     */   }
/*     */ 
/*     */   public InputStream getCanonicalizedData() {
/* 151 */     return this.canonData;
/*     */   }
/*     */ 
/*     */   public void canonicalize(XMLCryptoContext context, ByteArrayOutputStream bos)
/*     */     throws XMLSignatureException
/*     */   {
/* 157 */     if (context == null) {
/* 158 */       throw new NullPointerException("context cannot be null");
/*     */     }
/*     */ 
/* 161 */     OutputStream os = new UnsyncBufferedOutputStream(bos);
/*     */     try {
/* 163 */       os.close();
/*     */     }
/*     */     catch (IOException e)
/*     */     {
/*     */     }
/* 168 */     DOMSubTreeData subTree = new DOMSubTreeData(this.localSiElem, true);
/*     */ 
/* 170 */     OctetStreamData data = null;
/*     */     try {
/* 172 */       data = (OctetStreamData)((DOMCanonicalizationMethod)this.canonicalizationMethod).canonicalize(subTree, context, os);
/*     */     }
/*     */     catch (TransformException te) {
/* 175 */       throw new XMLSignatureException(te);
/*     */     }
/*     */ 
/* 178 */     byte[] signedInfoBytes = bos.toByteArray();
/*     */ 
/* 181 */     if (log.isLoggable(Level.FINE)) {
/* 182 */       InputStreamReader isr = new InputStreamReader(new ByteArrayInputStream(signedInfoBytes));
/*     */ 
/* 184 */       char[] siBytes = new char[signedInfoBytes.length];
/*     */       try {
/* 186 */         isr.read(siBytes); } catch (IOException ioex) {
/*     */       }
/* 188 */       log.log(Level.FINE, "Canonicalized SignedInfo:\n" + new String(siBytes));
/*     */ 
/* 190 */       log.log(Level.FINE, "Data to be signed/verified:" + Base64.encode(signedInfoBytes));
/*     */     }
/*     */ 
/* 194 */     this.canonData = new ByteArrayInputStream(signedInfoBytes);
/*     */   }
/*     */ 
/*     */   public void marshal(Node parent, String dsPrefix, DOMCryptoContext context) throws MarshalException
/*     */   {
/* 199 */     this.ownerDoc = DOMUtils.getOwnerDocument(parent);
/*     */ 
/* 201 */     Element siElem = DOMUtils.createElement(this.ownerDoc, "SignedInfo", "http://www.w3.org/2000/09/xmldsig#", dsPrefix);
/*     */ 
/* 205 */     DOMCanonicalizationMethod dcm = (DOMCanonicalizationMethod)this.canonicalizationMethod;
/*     */ 
/* 207 */     dcm.marshal(siElem, dsPrefix, context);
/*     */ 
/* 210 */     ((DOMSignatureMethod)this.signatureMethod).marshal(siElem, dsPrefix, context);
/*     */ 
/* 214 */     int i = 0; for (int size = this.references.size(); i < size; i++) {
/* 215 */       DOMReference reference = (DOMReference)this.references.get(i);
/* 216 */       reference.marshal(siElem, dsPrefix, context);
/*     */     }
/*     */ 
/* 220 */     DOMUtils.setAttributeID(siElem, "Id", this.id);
/*     */ 
/* 222 */     parent.appendChild(siElem);
/* 223 */     this.localSiElem = siElem;
/*     */   }
/*     */ 
/*     */   public boolean equals(Object o) {
/* 227 */     if (this == o) {
/* 228 */       return true;
/*     */     }
/*     */ 
/* 231 */     if (!(o instanceof SignedInfo)) {
/* 232 */       return false;
/*     */     }
/* 234 */     SignedInfo osi = (SignedInfo)o;
/*     */ 
/* 236 */     boolean idEqual = this.id == null ? false : osi.getId() == null ? true : this.id.equals(osi.getId());
/*     */ 
/* 239 */     return (this.canonicalizationMethod.equals(osi.getCanonicalizationMethod())) && (this.signatureMethod.equals(osi.getSignatureMethod())) && (this.references.equals(osi.getReferences())) && (idEqual);
/*     */   }
/*     */ }

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




© 2015 - 2024 Weber Informatics LLC | Privacy Policy