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

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

There is a newer version: 1.0.1-2
Show newest version
/*     */ package org.jcp.xml.dsig.internal.dom;
/*     */ 
/*     */ import com.sun.org.apache.xml.internal.security.exceptions.Base64DecodingException;
/*     */ import com.sun.org.apache.xml.internal.security.utils.Base64;
/*     */ import java.util.ArrayList;
/*     */ import java.util.Collections;
/*     */ import java.util.List;
/*     */ import javax.xml.crypto.MarshalException;
/*     */ import javax.xml.crypto.XMLStructure;
/*     */ import javax.xml.crypto.dom.DOMCryptoContext;
/*     */ import javax.xml.crypto.dsig.keyinfo.PGPData;
/*     */ import org.w3c.dom.Document;
/*     */ import org.w3c.dom.Element;
/*     */ import org.w3c.dom.Node;
/*     */ import org.w3c.dom.NodeList;
/*     */ 
/*     */ public final class DOMPGPData extends DOMStructure
/*     */   implements PGPData
/*     */ {
/*     */   private final byte[] keyId;
/*     */   private final byte[] keyPacket;
/*     */   private final List externalElements;
/*     */ 
/*     */   public DOMPGPData(byte[] keyPacket, List other)
/*     */   {
/*  51 */     if (keyPacket == null) {
/*  52 */       throw new NullPointerException("keyPacket cannot be null");
/*     */     }
/*  54 */     if ((other == null) || (other.isEmpty())) {
/*  55 */       this.externalElements = Collections.EMPTY_LIST;
/*     */     } else {
/*  57 */       List otherCopy = new ArrayList(other);
/*  58 */       int i = 0; for (int size = otherCopy.size(); i < size; i++) {
/*  59 */         if (!(otherCopy.get(i) instanceof XMLStructure)) {
/*  60 */           throw new ClassCastException("other[" + i + "] is not a valid PGPData type");
/*     */         }
/*     */       }
/*     */ 
/*  64 */       this.externalElements = Collections.unmodifiableList(otherCopy);
/*     */     }
/*  66 */     this.keyPacket = ((byte[])keyPacket.clone());
/*  67 */     checkKeyPacket(keyPacket);
/*  68 */     this.keyId = null;
/*     */   }
/*     */ 
/*     */   public DOMPGPData(byte[] keyId, byte[] keyPacket, List other)
/*     */   {
/*  92 */     if (keyId == null) {
/*  93 */       throw new NullPointerException("keyId cannot be null");
/*     */     }
/*     */ 
/*  96 */     if (keyId.length != 8) {
/*  97 */       throw new IllegalArgumentException("keyId must be 8 bytes long");
/*     */     }
/*  99 */     if ((other == null) || (other.isEmpty())) {
/* 100 */       this.externalElements = Collections.EMPTY_LIST;
/*     */     } else {
/* 102 */       List otherCopy = new ArrayList(other);
/* 103 */       int i = 0; for (int size = otherCopy.size(); i < size; i++) {
/* 104 */         if (!(otherCopy.get(i) instanceof XMLStructure)) {
/* 105 */           throw new ClassCastException("other[" + i + "] is not a valid PGPData type");
/*     */         }
/*     */       }
/*     */ 
/* 109 */       this.externalElements = Collections.unmodifiableList(otherCopy);
/*     */     }
/* 111 */     this.keyId = ((byte[])keyId.clone());
/* 112 */     this.keyPacket = (keyPacket == null ? null : (byte[])keyPacket.clone());
/* 113 */     if (keyPacket != null)
/* 114 */       checkKeyPacket(keyPacket);
/*     */   }
/*     */ 
/*     */   public DOMPGPData(Element pdElem)
/*     */     throws MarshalException
/*     */   {
/* 125 */     byte[] keyId = null;
/* 126 */     byte[] keyPacket = null;
/* 127 */     NodeList nl = pdElem.getChildNodes();
/* 128 */     int length = nl.getLength();
/* 129 */     List other = new ArrayList(length);
/* 130 */     for (int x = 0; x < length; x++) {
/* 131 */       Node n = nl.item(x);
/* 132 */       if (n.getNodeType() == 1) {
/* 133 */         Element childElem = (Element)n;
/* 134 */         String localName = childElem.getLocalName();
/*     */         try {
/* 136 */           if (localName.equals("PGPKeyID"))
/* 137 */             keyId = Base64.decode(childElem);
/* 138 */           else if (localName.equals("PGPKeyPacket"))
/* 139 */             keyPacket = Base64.decode(childElem);
/*     */           else
/* 141 */             other.add(new javax.xml.crypto.dom.DOMStructure(childElem));
/*     */         }
/*     */         catch (Base64DecodingException bde)
/*     */         {
/* 145 */           throw new MarshalException(bde);
/*     */         }
/*     */       }
/*     */     }
/* 149 */     this.keyId = keyId;
/* 150 */     this.keyPacket = keyPacket;
/* 151 */     this.externalElements = Collections.unmodifiableList(other);
/*     */   }
/*     */ 
/*     */   public byte[] getKeyId() {
/* 155 */     return this.keyId == null ? null : (byte[])this.keyId.clone();
/*     */   }
/*     */ 
/*     */   public byte[] getKeyPacket() {
/* 159 */     return this.keyPacket == null ? null : (byte[])this.keyPacket.clone();
/*     */   }
/*     */ 
/*     */   public List getExternalElements() {
/* 163 */     return this.externalElements;
/*     */   }
/*     */ 
/*     */   public void marshal(Node parent, String dsPrefix, DOMCryptoContext context) throws MarshalException
/*     */   {
/* 168 */     Document ownerDoc = DOMUtils.getOwnerDocument(parent);
/*     */ 
/* 170 */     Element pdElem = DOMUtils.createElement(ownerDoc, "PGPData", "http://www.w3.org/2000/09/xmldsig#", dsPrefix);
/*     */ 
/* 174 */     if (this.keyId != null) {
/* 175 */       Element keyIdElem = DOMUtils.createElement(ownerDoc, "PGPKeyID", "http://www.w3.org/2000/09/xmldsig#", dsPrefix);
/*     */ 
/* 177 */       keyIdElem.appendChild(ownerDoc.createTextNode(Base64.encode(this.keyId)));
/*     */ 
/* 179 */       pdElem.appendChild(keyIdElem);
/*     */     }
/*     */ 
/* 183 */     if (this.keyPacket != null) {
/* 184 */       Element keyPktElem = DOMUtils.createElement(ownerDoc, "PGPKeyPacket", "http://www.w3.org/2000/09/xmldsig#", dsPrefix);
/*     */ 
/* 186 */       keyPktElem.appendChild(ownerDoc.createTextNode(Base64.encode(this.keyPacket)));
/*     */ 
/* 188 */       pdElem.appendChild(keyPktElem);
/*     */     }
/*     */ 
/* 192 */     int i = 0; for (int size = this.externalElements.size(); i < size; i++) {
/* 193 */       DOMUtils.appendChild(pdElem, ((javax.xml.crypto.dom.DOMStructure)this.externalElements.get(i)).getNode());
/*     */     }
/*     */ 
/* 197 */     parent.appendChild(pdElem);
/*     */   }
/*     */ 
/*     */   private void checkKeyPacket(byte[] keyPacket)
/*     */   {
/* 210 */     if (keyPacket.length < 3) {
/* 211 */       throw new IllegalArgumentException("keypacket must be at least 3 bytes long");
/*     */     }
/*     */ 
/* 215 */     int tag = keyPacket[0];
/*     */ 
/* 217 */     if ((tag & 0x80) != 128) {
/* 218 */       throw new IllegalArgumentException("keypacket tag is invalid: bit 7 is not set");
/*     */     }
/*     */ 
/* 222 */     if ((tag & 0x40) != 64) {
/* 223 */       throw new IllegalArgumentException("old keypacket tag format is unsupported");
/*     */     }
/*     */ 
/* 228 */     if (((tag & 0x6) != 6) && ((tag & 0xE) != 14) && ((tag & 0x5) != 5) && ((tag & 0x7) != 7))
/*     */     {
/* 230 */       throw new IllegalArgumentException("keypacket tag is invalid: must be 6, 14, 5, or 7");
/*     */     }
/*     */   }
/*     */ }

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




© 2015 - 2024 Weber Informatics LLC | Privacy Policy