/**
  * Creates a <code>DOMPGPData</code> from an element.
  *
  * @param pdElem a PGPData element
  */
 public DOMPGPData(Element pdElem) throws MarshalException {
   // get all children nodes
   byte[] keyId = null;
   byte[] keyPacket = null;
   NodeList nl = pdElem.getChildNodes();
   int length = nl.getLength();
   List other = new ArrayList(length);
   for (int x = 0; x < length; x++) {
     Node n = nl.item(x);
     if (n.getNodeType() == Node.ELEMENT_NODE) {
       Element childElem = (Element) n;
       String localName = childElem.getLocalName();
       try {
         if (localName.equals("PGPKeyID")) {
           keyId = Base64.decode(childElem);
         } else if (localName.equals("PGPKeyPacket")) {
           keyPacket = Base64.decode(childElem);
         } else {
           other.add(new javax.xml.crypto.dom.DOMStructure(childElem));
         }
       } catch (Base64DecodingException bde) {
         throw new MarshalException(bde);
       }
     }
   }
   this.keyId = keyId;
   this.keyPacket = keyPacket;
   this.externalElements = Collections.unmodifiableList(other);
 }
Ejemplo n.º 2
0
  private boolean equalsContent(List otherContent) {
    if (content.size() != otherContent.size()) {
      return false;
    }
    for (int i = 0, osize = otherContent.size(); i < osize; i++) {
      XMLStructure oxs = (XMLStructure) otherContent.get(i);
      XMLStructure xs = (XMLStructure) content.get(i);
      if (oxs instanceof javax.xml.crypto.dom.DOMStructure) {
        if (!(xs instanceof javax.xml.crypto.dom.DOMStructure)) {
          return false;
        }
        Node onode = ((javax.xml.crypto.dom.DOMStructure) oxs).getNode();
        Node node = ((javax.xml.crypto.dom.DOMStructure) xs).getNode();
        if (!DOMUtils.nodesEqual(node, onode)) {
          return false;
        }
      } else {
        if (!(xs.equals(oxs))) {
          return false;
        }
      }
    }

    return true;
  }
  public void marshal(Node parent, String dsPrefix, DOMCryptoContext context)
      throws MarshalException {
    Document ownerDoc = DOMUtils.getOwnerDocument(parent);

    Element pdElem = DOMUtils.createElement(ownerDoc, "PGPData", XMLSignature.XMLNS, dsPrefix);

    // create and append PGPKeyID element
    if (keyId != null) {
      Element keyIdElem =
          DOMUtils.createElement(ownerDoc, "PGPKeyID", XMLSignature.XMLNS, dsPrefix);
      keyIdElem.appendChild(ownerDoc.createTextNode(Base64.encode(keyId)));
      pdElem.appendChild(keyIdElem);
    }

    // create and append PGPKeyPacket element
    if (keyPacket != null) {
      Element keyPktElem =
          DOMUtils.createElement(ownerDoc, "PGPKeyPacket", XMLSignature.XMLNS, dsPrefix);
      keyPktElem.appendChild(ownerDoc.createTextNode(Base64.encode(keyPacket)));
      pdElem.appendChild(keyPktElem);
    }

    // create and append any elements
    for (int i = 0, size = externalElements.size(); i < size; i++) {
      DOMUtils.appendChild(
          pdElem, ((javax.xml.crypto.dom.DOMStructure) externalElements.get(i)).getNode());
    }

    parent.appendChild(pdElem);
  }
 /**
  * Creates a <code>DOMPGPData</code> containing the specified key id and optional key packet and
  * list of external elements.
  *
  * @param keyId a PGP public key id as defined in section 11.2 of <a
  *     href="http://www.ietf.org/rfc/rfc2440.txt"/>RFC 2440</a>. The array is cloned to prevent
  *     subsequent modification.
  * @param keyPacket a PGP Key Material Packet as defined in section 5.5 of <a
  *     href="http://www.ietf.org/rfc/rfc2440.txt"/>RFC 2440</a> (may be <code>null</code>). The
  *     array is cloned to prevent subsequent modification.
  * @param other a list of {@link XMLStructure}s representing elements from an external namespace.
  *     The list is defensively copied to prevent subsequent modification. May be <code>null</code>
  *     or empty.
  * @throws NullPointerException if <code>keyId</code> is <code>null</code>
  * @throws IllegalArgumentException if the key id or packet is not in the correct format
  * @throws ClassCastException if <code>other</code> contains any entries that are not of type
  *     {@link XMLStructure}
  */
 public DOMPGPData(byte[] keyId, byte[] keyPacket, List other) {
   if (keyId == null) {
     throw new NullPointerException("keyId cannot be null");
   }
   // key ids must be 8 bytes
   if (keyId.length != 8) {
     throw new IllegalArgumentException("keyId must be 8 bytes long");
   }
   if (other == null || other.isEmpty()) {
     this.externalElements = Collections.EMPTY_LIST;
   } else {
     List otherCopy = new ArrayList(other);
     for (int i = 0, size = otherCopy.size(); i < size; i++) {
       if (!(otherCopy.get(i) instanceof XMLStructure)) {
         throw new ClassCastException("other[" + i + "] is not a valid PGPData type");
       }
     }
     this.externalElements = Collections.unmodifiableList(otherCopy);
   }
   this.keyId = (byte[]) keyId.clone();
   this.keyPacket = keyPacket == null ? null : (byte[]) keyPacket.clone();
   if (keyPacket != null) {
     checkKeyPacket(keyPacket);
   }
 }
Ejemplo n.º 5
0
 public DOMReference(
     String uri,
     String type,
     DigestMethod dm,
     List appliedTransforms,
     Data result,
     List transforms,
     String id,
     byte[] digestValue) {
   if (dm == null) {
     throw new NullPointerException("DigestMethod must be non-null");
   }
   if (appliedTransforms == null || appliedTransforms.isEmpty()) {
     this.appliedTransforms = Collections.EMPTY_LIST;
   } else {
     List transformsCopy = new ArrayList(appliedTransforms);
     for (int i = 0, size = transformsCopy.size(); i < size; i++) {
       if (!(transformsCopy.get(i) instanceof Transform)) {
         throw new ClassCastException("appliedTransforms[" + i + "] is not a valid type");
       }
     }
     this.appliedTransforms = Collections.unmodifiableList(transformsCopy);
   }
   if (transforms == null || transforms.isEmpty()) {
     this.transforms = Collections.EMPTY_LIST;
   } else {
     List transformsCopy = new ArrayList(transforms);
     for (int i = 0, size = transformsCopy.size(); i < size; i++) {
       if (!(transformsCopy.get(i) instanceof Transform)) {
         throw new ClassCastException("transforms[" + i + "] is not a valid type");
       }
     }
     this.transforms = Collections.unmodifiableList(transformsCopy);
   }
   List all = new ArrayList(this.appliedTransforms);
   all.addAll(this.transforms);
   this.allTransforms = Collections.unmodifiableList(all);
   this.digestMethod = dm;
   this.uri = uri;
   if ((uri != null) && (!uri.equals(""))) {
     try {
       new URI(uri);
     } catch (URISyntaxException e) {
       throw new IllegalArgumentException(e.getMessage());
     }
   }
   this.type = type;
   this.id = id;
   if (digestValue != null) {
     this.digestValue = (byte[]) digestValue.clone();
     this.digested = true;
   }
   this.appliedTransformData = result;
 }
Ejemplo n.º 6
0
 /**
  * Creates an <code>XMLObject</code> from the specified parameters.
  *
  * @param content a list of {@link XMLStructure}s. The list is defensively copied to protect
  *     against subsequent modification. May be <code>null</code> or empty.
  * @param id the Id (may be <code>null</code>)
  * @param mimeType the mime type (may be <code>null</code>)
  * @param encoding the encoding (may be <code>null</code>)
  * @return an <code>XMLObject</code>
  * @throws ClassCastException if <code>content</code> contains any entries that are not of type
  *     {@link XMLStructure}
  */
 public DOMXMLObject(List content, String id, String mimeType, String encoding) {
   if (content == null || content.isEmpty()) {
     this.content = Collections.EMPTY_LIST;
   } else {
     List contentCopy = new ArrayList(content);
     for (int i = 0, size = contentCopy.size(); i < size; i++) {
       if (!(contentCopy.get(i) instanceof XMLStructure)) {
         throw new ClassCastException("content[" + i + "] is not a valid type");
       }
     }
     this.content = Collections.unmodifiableList(contentCopy);
   }
   this.id = id;
   this.mimeType = mimeType;
   this.encoding = encoding;
 }
 /**
  * Creates a <code>SignatureProperty</code> from the specified parameters.
  *
  * @param content a list of one or more {@link XMLStructure}s. The list is defensively copied to
  *     protect against subsequent modification.
  * @param target the target URI
  * @param id the Id (may be <code>null</code>)
  * @return a <code>SignatureProperty</code>
  * @throws ClassCastException if <code>content</code> contains any entries that are not of type
  *     {@link XMLStructure}
  * @throws IllegalArgumentException if <code>content</code> is empty
  * @throws NullPointerException if <code>content</code> or <code>target</code> is <code>null
  *     </code>
  */
 public DOMSignatureProperty(List content, String target, String id) {
   if (target == null) {
     throw new NullPointerException("target cannot be null");
   } else if (content == null) {
     throw new NullPointerException("content cannot be null");
   } else if (content.isEmpty()) {
     throw new IllegalArgumentException("content cannot be empty");
   } else {
     List contentCopy = new ArrayList(content);
     for (int i = 0, size = contentCopy.size(); i < size; i++) {
       if (!(contentCopy.get(i) instanceof XMLStructure)) {
         throw new ClassCastException("content[" + i + "] is not a valid type");
       }
     }
     this.content = Collections.unmodifiableList(contentCopy);
   }
   this.target = target;
   this.id = id;
 }
Ejemplo n.º 8
0
  /**
   * Creates a <code>DOMReference</code> from an element.
   *
   * @param refElem a Reference element
   */
  public DOMReference(Element refElem, XMLCryptoContext context) throws MarshalException {
    // unmarshal Transforms, if specified
    Element nextSibling = DOMUtils.getFirstChildElement(refElem);
    List transforms = new ArrayList(5);
    if (nextSibling.getLocalName().equals("Transforms")) {
      Element transformElem = DOMUtils.getFirstChildElement(nextSibling);
      while (transformElem != null) {
        transforms.add(new DOMTransform(transformElem, context));
        transformElem = DOMUtils.getNextSiblingElement(transformElem);
      }
      nextSibling = DOMUtils.getNextSiblingElement(nextSibling);
    }

    // unmarshal DigestMethod
    Element dmElem = nextSibling;
    this.digestMethod = DOMDigestMethod.unmarshal(dmElem);

    // unmarshal DigestValue
    try {
      Element dvElem = DOMUtils.getNextSiblingElement(dmElem);
      this.digestValue = Base64.decode(dvElem);
    } catch (Base64DecodingException bde) {
      throw new MarshalException(bde);
    }

    // unmarshal attributes
    this.uri = DOMUtils.getAttributeValue(refElem, "URI");
    this.id = DOMUtils.getAttributeValue(refElem, "Id");

    this.type = DOMUtils.getAttributeValue(refElem, "Type");
    this.here = refElem.getAttributeNodeNS(null, "URI");
    this.refElem = refElem;

    if (transforms.isEmpty()) {
      this.transforms = Collections.EMPTY_LIST;
    } else {
      this.transforms = Collections.unmodifiableList(transforms);
    }
    this.appliedTransforms = Collections.EMPTY_LIST;
    this.allTransforms = transforms;
    this.appliedTransformData = null;
  }
  /**
   * Creates a <code>DOMSignatureProperty</code> from an element.
   *
   * @param propElem a SignatureProperty element
   */
  public DOMSignatureProperty(Element propElem) throws MarshalException {
    // unmarshal attributes
    target = DOMUtils.getAttributeValue(propElem, "Target");
    if (target == null) {
      throw new MarshalException("target cannot be null");
    }
    id = DOMUtils.getAttributeValue(propElem, "Id");

    NodeList nodes = propElem.getChildNodes();
    int length = nodes.getLength();
    List content = new ArrayList(length);
    for (int i = 0; i < length; i++) {
      content.add(new javax.xml.crypto.dom.DOMStructure(nodes.item(i)));
    }
    if (content.isEmpty()) {
      throw new MarshalException("content cannot be empty");
    } else {
      this.content = Collections.unmodifiableList(content);
    }
  }
  public void marshal(Node parent, String dsPrefix, DOMCryptoContext context)
      throws MarshalException {
    Document ownerDoc = DOMUtils.getOwnerDocument(parent);

    Element propElem =
        DOMUtils.createElement(ownerDoc, "SignatureProperty", XMLSignature.XMLNS, dsPrefix);

    // set attributes
    DOMUtils.setAttributeID(propElem, "Id", id);
    DOMUtils.setAttribute(propElem, "Target", target);

    // create and append any elements and mixed content
    for (int i = 0, size = content.size(); i < size; i++) {
      javax.xml.crypto.dom.DOMStructure property =
          (javax.xml.crypto.dom.DOMStructure) content.get(i);
      DOMUtils.appendChild(propElem, property.getNode());
    }

    parent.appendChild(propElem);
  }
Ejemplo n.º 11
0
  public void marshal(Node parent, String dsPrefix, DOMCryptoContext context)
      throws MarshalException {
    Document ownerDoc = DOMUtils.getOwnerDocument(parent);

    Element objElem = DOMUtils.createElement(ownerDoc, "Object", XMLSignature.XMLNS, dsPrefix);

    // set attributes
    DOMUtils.setAttributeID(objElem, "Id", id);
    DOMUtils.setAttribute(objElem, "MimeType", mimeType);
    DOMUtils.setAttribute(objElem, "Encoding", encoding);

    // create and append any elements and mixed content, if necessary
    for (int i = 0, size = content.size(); i < size; i++) {
      XMLStructure object = (XMLStructure) content.get(i);
      if (object instanceof DOMStructure) {
        ((DOMStructure) object).marshal(objElem, dsPrefix, context);
      } else {
        javax.xml.crypto.dom.DOMStructure domObject = (javax.xml.crypto.dom.DOMStructure) object;
        DOMUtils.appendChild(objElem, domObject.getNode());
      }
    }

    parent.appendChild(objElem);
  }
Ejemplo n.º 12
0
  public boolean equals(Object o) {
    if (this == o) {
      return true;
    }

    if (!(o instanceof Reference)) {
      return false;
    }
    Reference oref = (Reference) o;

    boolean idsEqual = (id == null ? oref.getId() == null : id.equals(oref.getId()));
    boolean urisEqual = (uri == null ? oref.getURI() == null : uri.equals(oref.getURI()));
    boolean typesEqual = (type == null ? oref.getType() == null : type.equals(oref.getType()));
    boolean digestValuesEqual = Arrays.equals(digestValue, oref.getDigestValue());

    return (digestMethod.equals(oref.getDigestMethod())
        && idsEqual
        && urisEqual
        && typesEqual
        && transforms.equals(oref.getTransforms()));
  }
Ejemplo n.º 13
0
  public void marshal(Node parent, String dsPrefix, DOMCryptoContext context)
      throws MarshalException {
    if (log.isLoggable(Level.FINE)) {
      log.log(Level.FINE, "Marshalling Reference");
    }
    Document ownerDoc = DOMUtils.getOwnerDocument(parent);

    refElem = DOMUtils.createElement(ownerDoc, "Reference", XMLSignature.XMLNS, dsPrefix);

    // set attributes
    DOMUtils.setAttributeID(refElem, "Id", id);
    DOMUtils.setAttribute(refElem, "URI", uri);
    DOMUtils.setAttribute(refElem, "Type", type);

    // create and append Transforms element
    if (!transforms.isEmpty() || !appliedTransforms.isEmpty()) {
      Element transformsElem =
          DOMUtils.createElement(ownerDoc, "Transforms", XMLSignature.XMLNS, dsPrefix);
      refElem.appendChild(transformsElem);
      for (int i = 0, size = appliedTransforms.size(); i < size; i++) {
        DOMStructure transform = (DOMStructure) appliedTransforms.get(i);
        transform.marshal(transformsElem, dsPrefix, context);
      }
      for (int i = 0, size = transforms.size(); i < size; i++) {
        DOMStructure transform = (DOMStructure) transforms.get(i);
        transform.marshal(transformsElem, dsPrefix, context);
      }
    }

    // create and append DigestMethod element
    ((DOMDigestMethod) digestMethod).marshal(refElem, dsPrefix, context);

    // create and append DigestValue element
    if (log.isLoggable(Level.FINE)) {
      log.log(Level.FINE, "Adding digestValueElem");
    }
    Element digestValueElem =
        DOMUtils.createElement(ownerDoc, "DigestValue", XMLSignature.XMLNS, dsPrefix);
    if (digestValue != null) {
      digestValueElem.appendChild(ownerDoc.createTextNode(Base64.encode(digestValue)));
    }
    refElem.appendChild(digestValueElem);

    parent.appendChild(refElem);
    here = refElem.getAttributeNodeNS(null, "URI");
  }
Ejemplo n.º 14
0
  /**
   * Creates an <code>XMLObject</code> from an element.
   *
   * @param objElem an Object element
   * @throws MarshalException if there is an error when unmarshalling
   */
  public DOMXMLObject(Element objElem, XMLCryptoContext context, Provider provider)
      throws MarshalException {
    // unmarshal attributes
    this.encoding = DOMUtils.getAttributeValue(objElem, "Encoding");

    Attr attr = objElem.getAttributeNodeNS(null, "Id");
    if (attr != null) {
      this.id = attr.getValue();
      objElem.setIdAttributeNode(attr, true);
    } else {
      this.id = null;
    }
    this.mimeType = DOMUtils.getAttributeValue(objElem, "MimeType");

    NodeList nodes = objElem.getChildNodes();
    int length = nodes.getLength();
    List content = new ArrayList(length);
    for (int i = 0; i < length; i++) {
      Node child = nodes.item(i);
      if (child.getNodeType() == Node.ELEMENT_NODE) {
        Element childElem = (Element) child;
        String tag = childElem.getLocalName();
        if (tag.equals("Manifest")) {
          content.add(new DOMManifest(childElem, context, provider));
          continue;
        } else if (tag.equals("SignatureProperties")) {
          content.add(new DOMSignatureProperties(childElem));
          continue;
        } else if (tag.equals("X509Data")) {
          content.add(new DOMX509Data(childElem));
          continue;
        }
        // @@@FIXME: check for other dsig structures
      }
      content.add(new javax.xml.crypto.dom.DOMStructure(child));
    }
    if (content.isEmpty()) {
      this.content = Collections.EMPTY_LIST;
    } else {
      this.content = Collections.unmodifiableList(content);
    }
  }
Ejemplo n.º 15
0
  private static boolean paramsEqual(
      XPathFilter2ParameterSpec spec1, XPathFilter2ParameterSpec spec2) {

    List types = spec1.getXPathList();
    List otypes = spec2.getXPathList();
    int size = types.size();
    if (size != otypes.size()) {
      return false;
    }
    for (int i = 0; i < size; i++) {
      XPathType type = (XPathType) types.get(i);
      XPathType otype = (XPathType) otypes.get(i);
      if (!type.getExpression().equals(otype.getExpression())
          || !type.getNamespaceMap().equals(otype.getNamespaceMap())
          || type.getFilter() != otype.getFilter()) {
        return false;
      }
    }
    return true;
  }
Ejemplo n.º 16
0
  private byte[] transform(Data dereferencedData, XMLCryptoContext context)
      throws XMLSignatureException {

    if (md == null) {
      try {
        md =
            MessageDigest.getInstance(((DOMDigestMethod) digestMethod).getMessageDigestAlgorithm());
      } catch (NoSuchAlgorithmException nsae) {
        throw new XMLSignatureException(nsae);
      }
    }
    md.reset();
    DigesterOutputStream dos;
    Boolean cache = (Boolean) context.getProperty("javax.xml.crypto.dsig.cacheReference");
    if (cache != null && cache.booleanValue() == true) {
      this.derefData = copyDerefData(dereferencedData);
      dos = new DigesterOutputStream(md, true);
    } else {
      dos = new DigesterOutputStream(md);
    }
    OutputStream os = new UnsyncBufferedOutputStream(dos);
    Data data = dereferencedData;
    for (int i = 0, size = transforms.size(); i < size; i++) {
      DOMTransform transform = (DOMTransform) transforms.get(i);
      try {
        if (i < size - 1) {
          data = transform.transform(data, context);
        } else {
          data = transform.transform(data, context, os);
        }
      } catch (TransformException te) {
        throw new XMLSignatureException(te);
      }
    }

    try {
      if (data != null) {
        XMLSignatureInput xi;
        if (data instanceof ApacheData) {
          xi = ((ApacheData) data).getXMLSignatureInput();
        } else if (data instanceof OctetStreamData) {
          xi = new XMLSignatureInput(((OctetStreamData) data).getOctetStream());
        } else if (data instanceof NodeSetData) {
          TransformService spi =
              TransformService.getInstance(CanonicalizationMethod.INCLUSIVE, "DOM");
          data = spi.transform(data, context);
          xi = new XMLSignatureInput(((OctetStreamData) data).getOctetStream());
        } else {
          throw new XMLSignatureException("unrecognized Data type");
        }
        xi.updateOutputStream(os);
      }
      os.flush();
      if (cache != null && cache.booleanValue() == true) {
        this.dis = dos.getInputStream();
      }
      return dos.getDigestValue();
    } catch (Exception e) {
      throw new XMLSignatureException(e);
    }
  }