jdk/src/share/classes/com/sun/org/apache/xml/internal/security/signature/SignedInfo.java
author duke
Sat, 01 Dec 2007 00:00:00 +0000
changeset 2 90ce3da70b43
child 1337 e8d6cef36199
permissions -rw-r--r--
Initial load
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * reserved comment block
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT REMOVE OR ALTER!
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * Copyright  1999-2004 The Apache Software Foundation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 *  Licensed under the Apache License, Version 2.0 (the "License");
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 *  you may not use this file except in compliance with the License.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *  You may obtain a copy of the License at
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 *      http://www.apache.org/licenses/LICENSE-2.0
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 *  Unless required by applicable law or agreed to in writing, software
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 *  distributed under the License is distributed on an "AS IS" BASIS,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 *  See the License for the specific language governing permissions and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 *  limitations under the License.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
package com.sun.org.apache.xml.internal.security.signature;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
import java.io.ByteArrayInputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
import java.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
import java.io.OutputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import javax.crypto.SecretKey;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import javax.crypto.spec.SecretKeySpec;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import javax.xml.parsers.ParserConfigurationException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import com.sun.org.apache.xml.internal.security.algorithms.SignatureAlgorithm;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import com.sun.org.apache.xml.internal.security.c14n.CanonicalizationException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import com.sun.org.apache.xml.internal.security.c14n.Canonicalizer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import com.sun.org.apache.xml.internal.security.c14n.InvalidCanonicalizerException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import com.sun.org.apache.xml.internal.security.exceptions.XMLSecurityException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import com.sun.org.apache.xml.internal.security.utils.Constants;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import com.sun.org.apache.xml.internal.security.utils.XMLUtils;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import com.sun.org.apache.xml.internal.security.transforms.params.InclusiveNamespaces;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
import org.w3c.dom.Document;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
import org.w3c.dom.Element;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
import org.w3c.dom.Node;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
import org.xml.sax.SAXException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * Handles <code>&lt;ds:SignedInfo&gt;</code> elements
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * This <code>SignedInfo<code> element includes the canonicalization algorithm,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * a signature algorithm, and one or more references
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * @author Christian Geuer-Pollmann
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
public class SignedInfo extends Manifest {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
   /** Field _signatureAlgorithm */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
   private SignatureAlgorithm _signatureAlgorithm = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
   /** Field _c14nizedBytes           */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
   private byte[] _c14nizedBytes = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
   /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    * Overwrites {@link Manifest#addDocument} because it creates another Element.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    * @param doc the {@link Document} in which <code>XMLsignature</code> will be placed
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    * @throws XMLSecurityException
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
   public SignedInfo(Document doc) throws XMLSecurityException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
      this(doc, XMLSignature.ALGO_ID_SIGNATURE_DSA, Canonicalizer.ALGO_ID_C14N_OMIT_COMMENTS);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
   }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
   /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    * Constructs {@link SignedInfo} using given Canoicaliztion algorithm and Signature algorithm
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    * @param doc <code>SignedInfo</code> is placed in this document
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    * @param CanonicalizationMethodURI URI representation of the Canonicalization method
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    * @param SignatureMethodURI URI representation of the Digest and Signature algorithm
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    * @throws XMLSecurityException
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
   public SignedInfo(
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
           Document doc, String SignatureMethodURI, String CanonicalizationMethodURI)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
              throws XMLSecurityException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
      this(doc, SignatureMethodURI, 0, CanonicalizationMethodURI);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
   }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
   /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    * Constructor SignedInfo
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    * @param doc
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    * @param CanonicalizationMethodURI
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    * @param SignatureMethodURI
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    * @param HMACOutputLength
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    * @throws XMLSecurityException
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
   public SignedInfo(
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
           Document doc, String SignatureMethodURI, int HMACOutputLength, String CanonicalizationMethodURI)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
              throws XMLSecurityException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
      super(doc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
      // XMLUtils.addReturnToElement(this._constructionElement);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
      {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
         Element canonElem = XMLUtils.createElementInSignatureSpace(this._doc,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
                                Constants._TAG_CANONICALIZATIONMETHOD);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
         canonElem.setAttributeNS(null, Constants._ATT_ALGORITHM,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
                                CanonicalizationMethodURI);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
         this._constructionElement.appendChild(canonElem);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
         XMLUtils.addReturnToElement(this._constructionElement);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
      {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
         if (HMACOutputLength > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
            this._signatureAlgorithm = new SignatureAlgorithm(this._doc,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
                    SignatureMethodURI, HMACOutputLength);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
         } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
            this._signatureAlgorithm = new SignatureAlgorithm(this._doc,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
                    SignatureMethodURI);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
         }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
         this._constructionElement
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
            .appendChild(this._signatureAlgorithm.getElement());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
         XMLUtils.addReturnToElement(this._constructionElement);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
   }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
   /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    * @param doc
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    * @param SignatureMethodElem
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
    * @param CanonicalizationMethodElem
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
    * @throws XMLSecurityException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
    */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
   public SignedInfo(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
           Document doc, Element SignatureMethodElem, Element CanonicalizationMethodElem)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
              throws XMLSecurityException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
      super(doc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
      this._constructionElement.appendChild(CanonicalizationMethodElem);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
      XMLUtils.addReturnToElement(this._constructionElement);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
      this._signatureAlgorithm = new SignatureAlgorithm(SignatureMethodElem, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
      this._constructionElement
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
         .appendChild(this._signatureAlgorithm.getElement());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
      XMLUtils.addReturnToElement(this._constructionElement);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
   }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
   /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
    * Build a {@link SignedInfo} from an {@link Element}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
    *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
    * @param element <code>SignedInfo</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
    * @param BaseURI the URI of the resource where the XML instance was stored
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
    * @throws XMLSecurityException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
    * @see <A HREF="http://lists.w3.org/Archives/Public/w3c-ietf-xmldsig/2001OctDec/0033.html">Question</A>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
    * @see <A HREF="http://lists.w3.org/Archives/Public/w3c-ietf-xmldsig/2001OctDec/0054.html">Answer</A>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
    */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
   public SignedInfo(Element element, String BaseURI)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
           throws XMLSecurityException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
      // Parse the Reference children and Id attribute in the Manifest
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
      super(element, BaseURI);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
      /* canonicalize ds:SignedInfo, reparse it into a new document
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
       * and replace the original not-canonicalized ds:SignedInfo by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
       * the re-parsed canonicalized one.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
       */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
      String c14nMethodURI=this.getCanonicalizationMethodURI();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
     if (!(c14nMethodURI.equals("http://www.w3.org/TR/2001/REC-xml-c14n-20010315") ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
                c14nMethodURI.equals("http://www.w3.org/TR/2001/REC-xml-c14n-20010315#WithComments") ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
                        c14nMethodURI.equals("http://www.w3.org/2001/10/xml-exc-c14n#") ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
                        c14nMethodURI.equals("http://www.w3.org/2001/10/xml-exc-c14n#WithComments"))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        //The c14n is not a secure one and can rewrite the URIs or like that reparse the SignedInfo to be sure
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
      try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
         Canonicalizer c14nizer =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
            Canonicalizer.getInstance(this.getCanonicalizationMethodURI());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
         this._c14nizedBytes =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
            c14nizer.canonicalizeSubtree(this._constructionElement);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
         javax.xml.parsers.DocumentBuilderFactory dbf =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
            javax.xml.parsers.DocumentBuilderFactory.newInstance();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
         dbf.setNamespaceAware(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
         javax.xml.parsers.DocumentBuilder db = dbf.newDocumentBuilder();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
         org.w3c.dom.Document newdoc =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
            db.parse(new ByteArrayInputStream(this._c14nizedBytes));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
         Node imported = this._doc.importNode(newdoc.getDocumentElement(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
                                              true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
         this._constructionElement.getParentNode().replaceChild(imported,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
                 this._constructionElement);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
         this._constructionElement = (Element) imported;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
      } catch (ParserConfigurationException ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
         throw new XMLSecurityException("empty", ex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
      } catch (IOException ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
         throw new XMLSecurityException("empty", ex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
      } catch (SAXException ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
         throw new XMLSecurityException("empty", ex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
      this._signatureAlgorithm =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
         new SignatureAlgorithm(this.getSignatureMethodElement(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
                                this.getBaseURI());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
   }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
   /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
    * Tests core validation process
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
    *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
    * @return true if verification was successful
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
    * @throws MissingResourceFailureException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
    * @throws XMLSecurityException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
    */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
   public boolean verify()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
           throws MissingResourceFailureException, XMLSecurityException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
      return super.verifyReferences(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
   }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
   /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
    * Tests core validation process
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
    *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
    * @param followManifests defines whether the verification process has to verify referenced <CODE>ds:Manifest</CODE>s, too
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
    * @return true if verification was successful
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
    * @throws MissingResourceFailureException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
    * @throws XMLSecurityException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
    */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
   public boolean verify(boolean followManifests)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
           throws MissingResourceFailureException, XMLSecurityException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
      return super.verifyReferences(followManifests);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
   }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
   /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
    * Returns getCanonicalizedOctetStream
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
    *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
    * @return the canonicalization result octedt stream of <code>SignedInfo</code> element
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
    * @throws CanonicalizationException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
    * @throws InvalidCanonicalizerException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
    * @throws XMLSecurityException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
    */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
   public byte[] getCanonicalizedOctetStream()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
           throws CanonicalizationException, InvalidCanonicalizerException,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
                 XMLSecurityException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
      if ((this._c14nizedBytes == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
              /*&& (this._state == ElementProxy.MODE_SIGN)*/) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
         Canonicalizer c14nizer =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
            Canonicalizer.getInstance(this.getCanonicalizationMethodURI());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
         this._c14nizedBytes =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
            c14nizer.canonicalizeSubtree(this._constructionElement);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
      // make defensive copy
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
      byte[] output = new byte[this._c14nizedBytes.length];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
      System.arraycopy(this._c14nizedBytes, 0, output, 0, output.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
      return output;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
   }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
   /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
    *  Output the C14n stream to the give outputstream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
    * @param os
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
    * @throws CanonicalizationException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
    * @throws InvalidCanonicalizerException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
    * @throws XMLSecurityException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
    */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
   public void signInOctectStream(OutputStream os)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
       throws CanonicalizationException, InvalidCanonicalizerException,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
           XMLSecurityException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
        if ((this._c14nizedBytes == null)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
       Canonicalizer c14nizer =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
          Canonicalizer.getInstance(this.getCanonicalizationMethodURI());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
       c14nizer.setWriter(os);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
       String inclusiveNamespaces = this.getInclusiveNamespaces();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
       if(inclusiveNamespaces == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
        c14nizer.canonicalizeSubtree(this._constructionElement);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
       else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
        c14nizer.canonicalizeSubtree(this._constructionElement, inclusiveNamespaces);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
                        os.write(this._c14nizedBytes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
                } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
                        throw new RuntimeException(""+e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
   }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
   /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
    * Returns the Canonicalization method URI
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
    *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
    * @return the Canonicalization method URI
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
    */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
   public String getCanonicalizationMethodURI() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
    Element el= XMLUtils.selectDsNode(this._constructionElement.getFirstChild(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
     Constants._TAG_CANONICALIZATIONMETHOD,0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
     if (el==null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
     }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
     return el.getAttributeNS(null, Constants._ATT_ALGORITHM);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
   }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
   /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
    * Returns the Signature method URI
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
    *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
    * @return the Signature method URI
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
    */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
   public String getSignatureMethodURI() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
      Element signatureElement = this.getSignatureMethodElement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
      if (signatureElement != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
         return signatureElement.getAttributeNS(null, Constants._ATT_ALGORITHM);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
      return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
   }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
   /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
    * Method getSignatureMethodElement
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
    * @return gets The SignatureMethod Node.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
    *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
    */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
   public Element getSignatureMethodElement() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
      return XMLUtils.selectDsNode(this._constructionElement.getFirstChild(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
        Constants._TAG_SIGNATUREMETHOD,0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
   }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
   /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
    * Creates a SecretKey for the appropriate Mac algorithm based on a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
    * byte[] array password.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
    *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
    * @param secretKeyBytes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
    * @return the secret key for the SignedInfo element.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
    */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
   public SecretKey createSecretKey(byte[] secretKeyBytes)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
   {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
      return new SecretKeySpec(secretKeyBytes,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
                               this._signatureAlgorithm
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
                                  .getJCEAlgorithmString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
   }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
   /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
    * Method getBaseLocalName
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
    * @inheritDoc
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
    *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
    */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
   public String getBaseLocalName() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
      return Constants._TAG_SIGNEDINFO;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
   }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
   public String getInclusiveNamespaces() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
    Element el= XMLUtils.selectDsNode(this._constructionElement.getFirstChild(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
     Constants._TAG_CANONICALIZATIONMETHOD,0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
     if (el==null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
     }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
     String c14nMethodURI = el.getAttributeNS(null, Constants._ATT_ALGORITHM);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
     if(!(c14nMethodURI.equals("http://www.w3.org/2001/10/xml-exc-c14n#") ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
                        c14nMethodURI.equals("http://www.w3.org/2001/10/xml-exc-c14n#WithComments"))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
     Element inclusiveElement = XMLUtils.selectNode(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
             el.getFirstChild(),InclusiveNamespaces.ExclusiveCanonicalizationNamespace,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
        InclusiveNamespaces._TAG_EC_INCLUSIVENAMESPACES,0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
     if(inclusiveElement != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
     {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
         try
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
         {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
             String inclusiveNamespaces = new InclusiveNamespaces(inclusiveElement,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
                         InclusiveNamespaces.ExclusiveCanonicalizationNamespace).getInclusiveNamespaces();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
             return inclusiveNamespaces;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
         }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
         catch (XMLSecurityException e)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
         {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
             return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
         }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
     }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
     return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
}