author | weijun |
Tue, 19 Jun 2018 08:06:35 +0800 | |
changeset 50614 | 3810c9a2efa1 |
parent 47216 | 71c04702a3d5 |
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:
18267
diff
changeset
|
5 |
/** |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
18267
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:
18267
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:
18267
diff
changeset
|
8 |
* distributed with this work for additional information |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
18267
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:
18267
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:
18267
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:
18267
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:
18267
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:
18267
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:
18267
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:
18267
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:
18267
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:
18267
diff
changeset
|
20 |
* specific language governing permissions and limitations |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
18267
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: DOMCanonicalizationMethod.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; |
|
1337 | 33 |
import java.security.Provider; |
18780
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
18267
diff
changeset
|
34 |
import java.security.spec.AlgorithmParameterSpec; |
2 | 35 |
|
36 |
import org.w3c.dom.Element; |
|
37 |
||
38 |
import javax.xml.crypto.*; |
|
39 |
import javax.xml.crypto.dsig.*; |
|
40 |
||
41 |
/** |
|
42 |
* DOM-based abstract implementation of CanonicalizationMethod. |
|
43 |
* |
|
44 |
*/ |
|
45 |
public class DOMCanonicalizationMethod extends DOMTransform |
|
46 |
implements CanonicalizationMethod { |
|
47 |
||
48 |
/** |
|
50614
3810c9a2efa1
8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents:
47216
diff
changeset
|
49 |
* Creates a {@code DOMCanonicalizationMethod}. |
2 | 50 |
* |
51 |
* @param spi TransformService |
|
52 |
*/ |
|
53 |
public DOMCanonicalizationMethod(TransformService spi) |
|
18780
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
18267
diff
changeset
|
54 |
throws InvalidAlgorithmParameterException |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
18267
diff
changeset
|
55 |
{ |
2 | 56 |
super(spi); |
50614
3810c9a2efa1
8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents:
47216
diff
changeset
|
57 |
if (!(spi instanceof ApacheCanonicalizer) && !isC14Nalg(spi.getAlgorithm())) { |
3810c9a2efa1
8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents:
47216
diff
changeset
|
58 |
throw new InvalidAlgorithmParameterException("Illegal CanonicalizationMethod"); |
18267 | 59 |
} |
2 | 60 |
} |
61 |
||
62 |
/** |
|
50614
3810c9a2efa1
8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents:
47216
diff
changeset
|
63 |
* Creates a {@code DOMCanonicalizationMethod} from an element. It unmarshals any |
3810c9a2efa1
8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents:
47216
diff
changeset
|
64 |
* algorithm-specific input parameters. |
2 | 65 |
* |
66 |
* @param cmElem a CanonicalizationMethod element |
|
67 |
*/ |
|
1337 | 68 |
public DOMCanonicalizationMethod(Element cmElem, XMLCryptoContext context, |
18780
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
18267
diff
changeset
|
69 |
Provider provider) |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
18267
diff
changeset
|
70 |
throws MarshalException |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
18267
diff
changeset
|
71 |
{ |
1337 | 72 |
super(cmElem, context, provider); |
50614
3810c9a2efa1
8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents:
47216
diff
changeset
|
73 |
if (!(spi instanceof ApacheCanonicalizer) && !isC14Nalg(spi.getAlgorithm())) { |
18267 | 74 |
throw new MarshalException("Illegal CanonicalizationMethod"); |
75 |
} |
|
2 | 76 |
} |
77 |
||
78 |
/** |
|
79 |
* Canonicalizes the specified data using the underlying canonicalization |
|
80 |
* algorithm. This is a convenience method that is equivalent to invoking |
|
81 |
* the {@link #transform transform} method. |
|
82 |
* |
|
83 |
* @param data the data to be canonicalized |
|
50614
3810c9a2efa1
8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents:
47216
diff
changeset
|
84 |
* @param xc the {@code XMLCryptoContext} containing |
3810c9a2efa1
8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents:
47216
diff
changeset
|
85 |
* additional context (may be {@code null} if not applicable) |
2 | 86 |
* @return the canonicalized data |
50614
3810c9a2efa1
8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents:
47216
diff
changeset
|
87 |
* @throws NullPointerException if {@code data} is {@code null} |
1337 | 88 |
* @throws TransformException if an unexpected error occurs while |
2 | 89 |
* canonicalizing the data |
90 |
*/ |
|
91 |
public Data canonicalize(Data data, XMLCryptoContext xc) |
|
18780
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
18267
diff
changeset
|
92 |
throws TransformException |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
18267
diff
changeset
|
93 |
{ |
2 | 94 |
return transform(data, xc); |
95 |
} |
|
96 |
||
97 |
public Data canonicalize(Data data, XMLCryptoContext xc, OutputStream os) |
|
18780
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
18267
diff
changeset
|
98 |
throws TransformException |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
18267
diff
changeset
|
99 |
{ |
2 | 100 |
return transform(data, xc, os); |
101 |
} |
|
102 |
||
18780
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
18267
diff
changeset
|
103 |
@Override |
2 | 104 |
public boolean equals(Object o) { |
105 |
if (this == o) { |
|
106 |
return true; |
|
107 |
} |
|
108 |
||
109 |
if (!(o instanceof CanonicalizationMethod)) { |
|
110 |
return false; |
|
111 |
} |
|
18780
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
18267
diff
changeset
|
112 |
CanonicalizationMethod ocm = (CanonicalizationMethod)o; |
2 | 113 |
|
50614
3810c9a2efa1
8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents:
47216
diff
changeset
|
114 |
return getAlgorithm().equals(ocm.getAlgorithm()) && |
3810c9a2efa1
8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents:
47216
diff
changeset
|
115 |
DOMUtils.paramsEqual(getParameterSpec(), ocm.getParameterSpec()); |
2 | 116 |
} |
18267 | 117 |
|
18780
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
18267
diff
changeset
|
118 |
@Override |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
18267
diff
changeset
|
119 |
public int hashCode() { |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
18267
diff
changeset
|
120 |
int result = 17; |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
18267
diff
changeset
|
121 |
result = 31 * result + getAlgorithm().hashCode(); |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
18267
diff
changeset
|
122 |
AlgorithmParameterSpec spec = getParameterSpec(); |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
18267
diff
changeset
|
123 |
if (spec != null) { |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
18267
diff
changeset
|
124 |
result = 31 * result + spec.hashCode(); |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
18267
diff
changeset
|
125 |
} |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
18267
diff
changeset
|
126 |
|
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
18267
diff
changeset
|
127 |
return result; |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
18267
diff
changeset
|
128 |
} |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
18267
diff
changeset
|
129 |
|
18267 | 130 |
private static boolean isC14Nalg(String alg) { |
50614
3810c9a2efa1
8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents:
47216
diff
changeset
|
131 |
return isInclusiveC14Nalg(alg) || isExclusiveC14Nalg(alg) || isC14N11alg(alg); |
3810c9a2efa1
8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents:
47216
diff
changeset
|
132 |
} |
3810c9a2efa1
8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents:
47216
diff
changeset
|
133 |
|
3810c9a2efa1
8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents:
47216
diff
changeset
|
134 |
private static boolean isInclusiveC14Nalg(String alg) { |
3810c9a2efa1
8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents:
47216
diff
changeset
|
135 |
return alg.equals(CanonicalizationMethod.INCLUSIVE) |
3810c9a2efa1
8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents:
47216
diff
changeset
|
136 |
|| alg.equals(CanonicalizationMethod.INCLUSIVE_WITH_COMMENTS); |
3810c9a2efa1
8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents:
47216
diff
changeset
|
137 |
} |
3810c9a2efa1
8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents:
47216
diff
changeset
|
138 |
|
3810c9a2efa1
8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents:
47216
diff
changeset
|
139 |
private static boolean isExclusiveC14Nalg(String alg) { |
3810c9a2efa1
8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents:
47216
diff
changeset
|
140 |
return alg.equals(CanonicalizationMethod.EXCLUSIVE) |
3810c9a2efa1
8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents:
47216
diff
changeset
|
141 |
|| alg.equals(CanonicalizationMethod.EXCLUSIVE_WITH_COMMENTS); |
3810c9a2efa1
8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents:
47216
diff
changeset
|
142 |
} |
3810c9a2efa1
8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents:
47216
diff
changeset
|
143 |
|
3810c9a2efa1
8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents:
47216
diff
changeset
|
144 |
private static boolean isC14N11alg(String alg) { |
3810c9a2efa1
8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents:
47216
diff
changeset
|
145 |
return alg.equals(DOMCanonicalXMLC14N11Method.C14N_11) |
3810c9a2efa1
8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents:
47216
diff
changeset
|
146 |
|| alg.equals(DOMCanonicalXMLC14N11Method.C14N_11_WITH_COMMENTS); |
18267 | 147 |
} |
2 | 148 |
} |