author | weijun |
Tue, 19 Jun 2018 08:06:35 +0800 | |
changeset 50614 | 3810c9a2efa1 |
parent 47216 | 71c04702a3d5 |
child 53998 | d870bb08194a |
permissions | -rw-r--r-- |
2 | 1 |
/* |
1337 | 2 |
* reserved comment block |
3 |
* DO NOT REMOVE OR ALTER! |
|
2 | 4 |
*/ |
18780
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
5506
diff
changeset
|
5 |
/** |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
5506
diff
changeset
|
6 |
* Licensed to the Apache Software Foundation (ASF) under one |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
5506
diff
changeset
|
7 |
* or more contributor license agreements. See the NOTICE file |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
5506
diff
changeset
|
8 |
* distributed with this work for additional information |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
5506
diff
changeset
|
9 |
* regarding copyright ownership. The ASF licenses this file |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
5506
diff
changeset
|
10 |
* to you under the Apache License, Version 2.0 (the |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
5506
diff
changeset
|
11 |
* "License"); you may not use this file except in compliance |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
5506
diff
changeset
|
12 |
* with the License. You may obtain a copy of the License at |
1337 | 13 |
* |
18780
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
5506
diff
changeset
|
14 |
* http://www.apache.org/licenses/LICENSE-2.0 |
1337 | 15 |
* |
18780
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
5506
diff
changeset
|
16 |
* Unless required by applicable law or agreed to in writing, |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
5506
diff
changeset
|
17 |
* software distributed under the License is distributed on an |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
5506
diff
changeset
|
18 |
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
5506
diff
changeset
|
19 |
* KIND, either express or implied. See the License for the |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
5506
diff
changeset
|
20 |
* specific language governing permissions and limitations |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
5506
diff
changeset
|
21 |
* under the License. |
1337 | 22 |
*/ |
23 |
/* |
|
50614
3810c9a2efa1
8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents:
47216
diff
changeset
|
24 |
* Copyright (c) 2005, 2018, Oracle and/or its affiliates. All rights reserved. |
1337 | 25 |
*/ |
26 |
/* |
|
50614
3810c9a2efa1
8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents:
47216
diff
changeset
|
27 |
* $Id: DOMTransform.java 1788465 2017-03-24 15:10:51Z coheigea $ |
2 | 28 |
*/ |
29 |
package org.jcp.xml.dsig.internal.dom; |
|
30 |
||
31 |
import java.io.OutputStream; |
|
32 |
import java.security.InvalidAlgorithmParameterException; |
|
33 |
import java.security.NoSuchAlgorithmException; |
|
1337 | 34 |
import java.security.Provider; |
2 | 35 |
import java.security.spec.AlgorithmParameterSpec; |
36 |
||
50614
3810c9a2efa1
8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents:
47216
diff
changeset
|
37 |
import javax.xml.crypto.Data; |
3810c9a2efa1
8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents:
47216
diff
changeset
|
38 |
import javax.xml.crypto.MarshalException; |
3810c9a2efa1
8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents:
47216
diff
changeset
|
39 |
import javax.xml.crypto.XMLCryptoContext; |
3810c9a2efa1
8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents:
47216
diff
changeset
|
40 |
import javax.xml.crypto.dsig.Transform; |
3810c9a2efa1
8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents:
47216
diff
changeset
|
41 |
import javax.xml.crypto.dsig.TransformException; |
3810c9a2efa1
8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents:
47216
diff
changeset
|
42 |
import javax.xml.crypto.dsig.TransformService; |
3810c9a2efa1
8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents:
47216
diff
changeset
|
43 |
import javax.xml.crypto.dsig.XMLSignature; |
3810c9a2efa1
8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents:
47216
diff
changeset
|
44 |
import javax.xml.crypto.dsig.dom.DOMSignContext; |
3810c9a2efa1
8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents:
47216
diff
changeset
|
45 |
|
2 | 46 |
import org.w3c.dom.Element; |
47 |
import org.w3c.dom.Node; |
|
48 |
||
49 |
/** |
|
50 |
* DOM-based abstract implementation of Transform. |
|
51 |
* |
|
52 |
*/ |
|
50614
3810c9a2efa1
8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents:
47216
diff
changeset
|
53 |
public class DOMTransform extends BaseStructure implements Transform { |
2 | 54 |
|
55 |
protected TransformService spi; |
|
56 |
||
57 |
/** |
|
32003 | 58 |
* Creates a {@code DOMTransform}. |
2 | 59 |
* |
60 |
* @param spi the TransformService |
|
61 |
*/ |
|
62 |
public DOMTransform(TransformService spi) { |
|
63 |
this.spi = spi; |
|
64 |
} |
|
65 |
||
66 |
/** |
|
50614
3810c9a2efa1
8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents:
47216
diff
changeset
|
67 |
* Creates a {@code DOMTransform} from an element. It unmarshals any |
3810c9a2efa1
8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents:
47216
diff
changeset
|
68 |
* algorithm-specific input parameters. |
2 | 69 |
* |
70 |
* @param transElem a Transform element |
|
71 |
*/ |
|
1337 | 72 |
public DOMTransform(Element transElem, XMLCryptoContext context, |
18780
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
5506
diff
changeset
|
73 |
Provider provider) |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
5506
diff
changeset
|
74 |
throws MarshalException |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
5506
diff
changeset
|
75 |
{ |
2 | 76 |
String algorithm = DOMUtils.getAttributeValue(transElem, "Algorithm"); |
18780
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
5506
diff
changeset
|
77 |
if (provider == null) { |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
5506
diff
changeset
|
78 |
try { |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
5506
diff
changeset
|
79 |
spi = TransformService.getInstance(algorithm, "DOM"); |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
5506
diff
changeset
|
80 |
} catch (NoSuchAlgorithmException e1) { |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
5506
diff
changeset
|
81 |
throw new MarshalException(e1); |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
5506
diff
changeset
|
82 |
} |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
5506
diff
changeset
|
83 |
} else { |
1337 | 84 |
try { |
85 |
spi = TransformService.getInstance(algorithm, "DOM", provider); |
|
18780
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
5506
diff
changeset
|
86 |
} catch (NoSuchAlgorithmException nsae) { |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
5506
diff
changeset
|
87 |
try { |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
5506
diff
changeset
|
88 |
spi = TransformService.getInstance(algorithm, "DOM"); |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
5506
diff
changeset
|
89 |
} catch (NoSuchAlgorithmException e2) { |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
5506
diff
changeset
|
90 |
throw new MarshalException(e2); |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
5506
diff
changeset
|
91 |
} |
1337 | 92 |
} |
2 | 93 |
} |
94 |
try { |
|
95 |
spi.init(new javax.xml.crypto.dom.DOMStructure(transElem), context); |
|
96 |
} catch (InvalidAlgorithmParameterException iape) { |
|
97 |
throw new MarshalException(iape); |
|
98 |
} |
|
99 |
} |
|
100 |
||
50614
3810c9a2efa1
8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents:
47216
diff
changeset
|
101 |
@Override |
2 | 102 |
public final AlgorithmParameterSpec getParameterSpec() { |
103 |
return spi.getParameterSpec(); |
|
104 |
} |
|
105 |
||
50614
3810c9a2efa1
8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents:
47216
diff
changeset
|
106 |
@Override |
2 | 107 |
public final String getAlgorithm() { |
108 |
return spi.getAlgorithm(); |
|
109 |
} |
|
110 |
||
111 |
/** |
|
50614
3810c9a2efa1
8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents:
47216
diff
changeset
|
112 |
* This method marshals any algorithm-specific parameters. |
2 | 113 |
*/ |
50614
3810c9a2efa1
8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents:
47216
diff
changeset
|
114 |
public void marshal(XmlWriter xwriter, String dsPrefix, XMLCryptoContext context) |
18780
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
5506
diff
changeset
|
115 |
throws MarshalException |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
5506
diff
changeset
|
116 |
{ |
50614
3810c9a2efa1
8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents:
47216
diff
changeset
|
117 |
String parentLocalName = xwriter.getCurrentLocalName(); |
3810c9a2efa1
8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents:
47216
diff
changeset
|
118 |
String localName = "Transforms".equals(parentLocalName) ? "Transform" : "CanonicalizationMethod"; |
3810c9a2efa1
8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents:
47216
diff
changeset
|
119 |
xwriter.writeStartElement(dsPrefix, localName, XMLSignature.XMLNS); |
3810c9a2efa1
8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents:
47216
diff
changeset
|
120 |
xwriter.writeAttribute("", "", "Algorithm", getAlgorithm()); |
2 | 121 |
|
50614
3810c9a2efa1
8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents:
47216
diff
changeset
|
122 |
javax.xml.crypto.XMLStructure xmlStruct = xwriter.getCurrentNodeAsStructure(); |
3810c9a2efa1
8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents:
47216
diff
changeset
|
123 |
spi.marshalParams(xmlStruct, context); |
2 | 124 |
|
50614
3810c9a2efa1
8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents:
47216
diff
changeset
|
125 |
xwriter.writeEndElement(); // "Transforms" or "CanonicalizationMethod" |
2 | 126 |
} |
127 |
||
128 |
/** |
|
129 |
* Transforms the specified data using the underlying transform algorithm. |
|
130 |
* |
|
131 |
* @param data the data to be transformed |
|
32003 | 132 |
* @param xc the {@code XMLCryptoContext} containing |
50614
3810c9a2efa1
8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents:
47216
diff
changeset
|
133 |
* additional context (may be {@code null} if not applicable) |
2 | 134 |
* @return the transformed data |
32003 | 135 |
* @throws NullPointerException if {@code data} is {@code null} |
2 | 136 |
* @throws XMLSignatureException if an unexpected error occurs while |
50614
3810c9a2efa1
8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents:
47216
diff
changeset
|
137 |
* executing the transform |
2 | 138 |
*/ |
50614
3810c9a2efa1
8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents:
47216
diff
changeset
|
139 |
@Override |
2 | 140 |
public Data transform(Data data, XMLCryptoContext xc) |
18780
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
5506
diff
changeset
|
141 |
throws TransformException |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
5506
diff
changeset
|
142 |
{ |
2 | 143 |
return spi.transform(data, xc); |
144 |
} |
|
145 |
||
146 |
/** |
|
147 |
* Transforms the specified data using the underlying transform algorithm. |
|
148 |
* |
|
149 |
* @param data the data to be transformed |
|
50614
3810c9a2efa1
8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents:
47216
diff
changeset
|
150 |
* @param xc the {@code XMLCryptoContext} containing |
3810c9a2efa1
8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents:
47216
diff
changeset
|
151 |
* additional context (may be {@code null} if not applicable) |
32003 | 152 |
* @param os the {@code OutputStream} that should be used to write |
50614
3810c9a2efa1
8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents:
47216
diff
changeset
|
153 |
* the transformed data to |
2 | 154 |
* @return the transformed data |
32003 | 155 |
* @throws NullPointerException if {@code data} is {@code null} |
2 | 156 |
* @throws XMLSignatureException if an unexpected error occurs while |
50614
3810c9a2efa1
8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents:
47216
diff
changeset
|
157 |
* executing the transform |
2 | 158 |
*/ |
50614
3810c9a2efa1
8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents:
47216
diff
changeset
|
159 |
@Override |
2 | 160 |
public Data transform(Data data, XMLCryptoContext xc, OutputStream os) |
18780
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
5506
diff
changeset
|
161 |
throws TransformException |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
5506
diff
changeset
|
162 |
{ |
2 | 163 |
return spi.transform(data, xc, os); |
164 |
} |
|
165 |
||
18780
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
5506
diff
changeset
|
166 |
@Override |
2 | 167 |
public boolean equals(Object o) { |
168 |
if (this == o) { |
|
169 |
return true; |
|
170 |
} |
|
171 |
||
172 |
if (!(o instanceof Transform)) { |
|
173 |
return false; |
|
174 |
} |
|
18780
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
5506
diff
changeset
|
175 |
Transform otransform = (Transform)o; |
2 | 176 |
|
50614
3810c9a2efa1
8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents:
47216
diff
changeset
|
177 |
return getAlgorithm().equals(otransform.getAlgorithm()) && |
18780
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
5506
diff
changeset
|
178 |
DOMUtils.paramsEqual(getParameterSpec(), |
50614
3810c9a2efa1
8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents:
47216
diff
changeset
|
179 |
otransform.getParameterSpec()); |
18780
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
5506
diff
changeset
|
180 |
} |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
5506
diff
changeset
|
181 |
|
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
5506
diff
changeset
|
182 |
@Override |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
5506
diff
changeset
|
183 |
public int hashCode() { |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
5506
diff
changeset
|
184 |
int result = 17; |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
5506
diff
changeset
|
185 |
result = 31 * result + getAlgorithm().hashCode(); |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
5506
diff
changeset
|
186 |
AlgorithmParameterSpec spec = getParameterSpec(); |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
5506
diff
changeset
|
187 |
if (spec != null) { |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
5506
diff
changeset
|
188 |
result = 31 * result + spec.hashCode(); |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
5506
diff
changeset
|
189 |
} |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
5506
diff
changeset
|
190 |
|
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
5506
diff
changeset
|
191 |
return result; |
2 | 192 |
} |
193 |
||
194 |
/** |
|
195 |
* Transforms the specified data using the underlying transform algorithm. |
|
196 |
* This method invokes the {@link #marshal marshal} method and passes it |
|
32003 | 197 |
* the specified {@code DOMSignContext} before transforming the data. |
2 | 198 |
* |
199 |
* @param data the data to be transformed |
|
32003 | 200 |
* @param sc the {@code XMLCryptoContext} containing |
201 |
* additional context (may be {@code null} if not applicable) |
|
2 | 202 |
* @param context the marshalling context |
203 |
* @return the transformed data |
|
204 |
* @throws MarshalException if an exception occurs while marshalling |
|
32003 | 205 |
* @throws NullPointerException if {@code data} or {@code context} |
206 |
* is {@code null} |
|
2 | 207 |
* @throws XMLSignatureException if an unexpected error occurs while |
208 |
* executing the transform |
|
209 |
*/ |
|
210 |
Data transform(Data data, XMLCryptoContext xc, DOMSignContext context) |
|
18780
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
5506
diff
changeset
|
211 |
throws MarshalException, TransformException |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
5506
diff
changeset
|
212 |
{ |
50614
3810c9a2efa1
8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents:
47216
diff
changeset
|
213 |
Node parent = context.getParent(); |
3810c9a2efa1
8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents:
47216
diff
changeset
|
214 |
XmlWriter xwriter = new XmlWriterToTree(Marshaller.getMarshallers(), parent); |
3810c9a2efa1
8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents:
47216
diff
changeset
|
215 |
marshal(xwriter, DOMUtils.getSignaturePrefix(context), context); |
2 | 216 |
return transform(data, xc); |
217 |
} |
|
218 |
} |