author | mullan |
Wed, 11 Jun 2014 16:25:59 -0400 | |
changeset 27081 | 1ceee8d3844d |
parent 25859 | 3317bb8137f4 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
2 |
* reserved comment block |
|
3 |
* DO NOT REMOVE OR ALTER! |
|
4 |
*/ |
|
18780
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
1337
diff
changeset
|
5 |
/** |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
1337
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:
1337
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:
1337
diff
changeset
|
8 |
* distributed with this work for additional information |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
1337
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:
1337
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:
1337
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:
1337
diff
changeset
|
12 |
* with the License. You may obtain a copy of the License at |
2 | 13 |
* |
18780
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
1337
diff
changeset
|
14 |
* http://www.apache.org/licenses/LICENSE-2.0 |
2 | 15 |
* |
18780
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
1337
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:
1337
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:
1337
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:
1337
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:
1337
diff
changeset
|
20 |
* specific language governing permissions and limitations |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
1337
diff
changeset
|
21 |
* under the License. |
2 | 22 |
*/ |
23 |
package com.sun.org.apache.xml.internal.security.utils; |
|
24 |
||
25 |
import java.io.File; |
|
26 |
import java.io.FileInputStream; |
|
27 |
import java.io.FileNotFoundException; |
|
28 |
import java.io.FileOutputStream; |
|
29 |
import java.io.IOException; |
|
30 |
import java.io.InputStream; |
|
27081 | 31 |
import java.security.SecurityPermission; |
2 | 32 |
|
33 |
/** |
|
34 |
* A collection of different, general-purpose methods for JAVA-specific things |
|
35 |
* @author Christian Geuer-Pollmann |
|
36 |
*/ |
|
37 |
public class JavaUtils { |
|
38 |
||
18780
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
1337
diff
changeset
|
39 |
/** {@link org.apache.commons.logging} logging facility */ |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
1337
diff
changeset
|
40 |
private static java.util.logging.Logger log = |
2 | 41 |
java.util.logging.Logger.getLogger(JavaUtils.class.getName()); |
42 |
||
27081 | 43 |
private static final SecurityPermission REGISTER_PERMISSION = |
44 |
new SecurityPermission( |
|
45 |
"com.sun.org.apache.xml.internal.security.register"); |
|
46 |
||
1337 | 47 |
private JavaUtils() { |
48 |
// we don't allow instantiation |
|
49 |
} |
|
2 | 50 |
|
1337 | 51 |
/** |
52 |
* Method getBytesFromFile |
|
53 |
* |
|
54 |
* @param fileName |
|
18780
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
1337
diff
changeset
|
55 |
* @return the bytes read from the file |
1337 | 56 |
* |
57 |
* @throws FileNotFoundException |
|
58 |
* @throws IOException |
|
59 |
*/ |
|
60 |
public static byte[] getBytesFromFile(String fileName) |
|
61 |
throws FileNotFoundException, IOException { |
|
62 |
||
63 |
byte refBytes[] = null; |
|
2 | 64 |
|
18780
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
1337
diff
changeset
|
65 |
FileInputStream fisRef = null; |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
1337
diff
changeset
|
66 |
UnsyncByteArrayOutputStream baos = null; |
1337 | 67 |
try { |
18780
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
1337
diff
changeset
|
68 |
fisRef = new FileInputStream(fileName); |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
1337
diff
changeset
|
69 |
baos = new UnsyncByteArrayOutputStream(); |
1337 | 70 |
byte buf[] = new byte[1024]; |
71 |
int len; |
|
2 | 72 |
|
1337 | 73 |
while ((len = fisRef.read(buf)) > 0) { |
74 |
baos.write(buf, 0, len); |
|
75 |
} |
|
2 | 76 |
|
1337 | 77 |
refBytes = baos.toByteArray(); |
78 |
} finally { |
|
18780
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
1337
diff
changeset
|
79 |
if (baos != null) { |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
1337
diff
changeset
|
80 |
baos.close(); |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
1337
diff
changeset
|
81 |
} |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
1337
diff
changeset
|
82 |
if (fisRef != null) { |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
1337
diff
changeset
|
83 |
fisRef.close(); |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
1337
diff
changeset
|
84 |
} |
1337 | 85 |
} |
2 | 86 |
|
1337 | 87 |
return refBytes; |
88 |
} |
|
2 | 89 |
|
1337 | 90 |
/** |
91 |
* Method writeBytesToFilename |
|
92 |
* |
|
93 |
* @param filename |
|
94 |
* @param bytes |
|
95 |
*/ |
|
96 |
public static void writeBytesToFilename(String filename, byte[] bytes) { |
|
97 |
FileOutputStream fos = null; |
|
98 |
try { |
|
99 |
if (filename != null && bytes != null) { |
|
100 |
File f = new File(filename); |
|
2 | 101 |
|
1337 | 102 |
fos = new FileOutputStream(f); |
2 | 103 |
|
1337 | 104 |
fos.write(bytes); |
105 |
fos.close(); |
|
106 |
} else { |
|
18780
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
1337
diff
changeset
|
107 |
if (log.isLoggable(java.util.logging.Level.FINE)) { |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
1337
diff
changeset
|
108 |
log.log(java.util.logging.Level.FINE, "writeBytesToFilename got null byte[] pointed"); |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
1337
diff
changeset
|
109 |
} |
1337 | 110 |
} |
111 |
} catch (IOException ex) { |
|
112 |
if (fos != null) { |
|
113 |
try { |
|
114 |
fos.close(); |
|
18780
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
1337
diff
changeset
|
115 |
} catch (IOException ioe) { |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
1337
diff
changeset
|
116 |
if (log.isLoggable(java.util.logging.Level.FINE)) { |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
1337
diff
changeset
|
117 |
log.log(java.util.logging.Level.FINE, ioe.getMessage(), ioe); |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
1337
diff
changeset
|
118 |
} |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
1337
diff
changeset
|
119 |
} |
1337 | 120 |
} |
121 |
} |
|
122 |
} |
|
2 | 123 |
|
1337 | 124 |
/** |
125 |
* This method reads all bytes from the given InputStream till EOF and |
|
126 |
* returns them as a byte array. |
|
127 |
* |
|
128 |
* @param inputStream |
|
18780
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
1337
diff
changeset
|
129 |
* @return the bytes read from the stream |
1337 | 130 |
* |
131 |
* @throws FileNotFoundException |
|
132 |
* @throws IOException |
|
133 |
*/ |
|
18780
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
1337
diff
changeset
|
134 |
public static byte[] getBytesFromStream(InputStream inputStream) throws IOException { |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
1337
diff
changeset
|
135 |
UnsyncByteArrayOutputStream baos = null; |
2 | 136 |
|
18780
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
1337
diff
changeset
|
137 |
byte[] retBytes = null; |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
1337
diff
changeset
|
138 |
try { |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
1337
diff
changeset
|
139 |
baos = new UnsyncByteArrayOutputStream(); |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
1337
diff
changeset
|
140 |
byte buf[] = new byte[4 * 1024]; |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
1337
diff
changeset
|
141 |
int len; |
1337 | 142 |
|
18780
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
1337
diff
changeset
|
143 |
while ((len = inputStream.read(buf)) > 0) { |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
1337
diff
changeset
|
144 |
baos.write(buf, 0, len); |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
1337
diff
changeset
|
145 |
} |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
1337
diff
changeset
|
146 |
retBytes = baos.toByteArray(); |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
1337
diff
changeset
|
147 |
} finally { |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
1337
diff
changeset
|
148 |
baos.close(); |
1337 | 149 |
} |
2 | 150 |
|
18780
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
1337
diff
changeset
|
151 |
return retBytes; |
1337 | 152 |
} |
24251
da7dc40edb67
8038349: Signing XML with DSA throws Exception when key is larger than 1024 bits
mullan
parents:
18780
diff
changeset
|
153 |
|
da7dc40edb67
8038349: Signing XML with DSA throws Exception when key is larger than 1024 bits
mullan
parents:
18780
diff
changeset
|
154 |
/** |
27081 | 155 |
* Throws a {@code SecurityException} if a security manager is installed |
156 |
* and the caller is not allowed to register an implementation of an |
|
157 |
* algorithm, transform, or other security sensitive XML Signature function. |
|
158 |
* |
|
159 |
* @throws SecurityException if a security manager is installed and the |
|
160 |
* caller has not been granted the |
|
161 |
* {@literal "com.sun.org.apache.xml.internal.security.register"} |
|
162 |
* {@code SecurityPermission} |
|
163 |
*/ |
|
164 |
public static void checkRegisterPermission() { |
|
165 |
SecurityManager sm = System.getSecurityManager(); |
|
166 |
if (sm != null) { |
|
167 |
sm.checkPermission(REGISTER_PERMISSION); |
|
168 |
} |
|
169 |
} |
|
170 |
||
171 |
/** |
|
24251
da7dc40edb67
8038349: Signing XML with DSA throws Exception when key is larger than 1024 bits
mullan
parents:
18780
diff
changeset
|
172 |
* Converts an ASN.1 DSA value to a XML Signature DSA Value. |
da7dc40edb67
8038349: Signing XML with DSA throws Exception when key is larger than 1024 bits
mullan
parents:
18780
diff
changeset
|
173 |
* |
da7dc40edb67
8038349: Signing XML with DSA throws Exception when key is larger than 1024 bits
mullan
parents:
18780
diff
changeset
|
174 |
* The JCE DSA Signature algorithm creates ASN.1 encoded (r,s) value |
da7dc40edb67
8038349: Signing XML with DSA throws Exception when key is larger than 1024 bits
mullan
parents:
18780
diff
changeset
|
175 |
* pairs (see section 2.2.2 of RFC 3279); the XML Signature requires the |
da7dc40edb67
8038349: Signing XML with DSA throws Exception when key is larger than 1024 bits
mullan
parents:
18780
diff
changeset
|
176 |
* core BigInteger values. |
da7dc40edb67
8038349: Signing XML with DSA throws Exception when key is larger than 1024 bits
mullan
parents:
18780
diff
changeset
|
177 |
* |
da7dc40edb67
8038349: Signing XML with DSA throws Exception when key is larger than 1024 bits
mullan
parents:
18780
diff
changeset
|
178 |
* @param asn1Bytes the ASN.1 encoded bytes |
da7dc40edb67
8038349: Signing XML with DSA throws Exception when key is larger than 1024 bits
mullan
parents:
18780
diff
changeset
|
179 |
* @param size size of r and s in bytes |
da7dc40edb67
8038349: Signing XML with DSA throws Exception when key is larger than 1024 bits
mullan
parents:
18780
diff
changeset
|
180 |
* @return the XML Signature encoded bytes |
da7dc40edb67
8038349: Signing XML with DSA throws Exception when key is larger than 1024 bits
mullan
parents:
18780
diff
changeset
|
181 |
* @throws IOException if the bytes are not encoded correctly |
da7dc40edb67
8038349: Signing XML with DSA throws Exception when key is larger than 1024 bits
mullan
parents:
18780
diff
changeset
|
182 |
* @see <A HREF="http://www.w3.org/TR/xmldsig-core1/#sec-DSA">6.4.1 DSA</A> |
da7dc40edb67
8038349: Signing XML with DSA throws Exception when key is larger than 1024 bits
mullan
parents:
18780
diff
changeset
|
183 |
*/ |
da7dc40edb67
8038349: Signing XML with DSA throws Exception when key is larger than 1024 bits
mullan
parents:
18780
diff
changeset
|
184 |
public static byte[] convertDsaASN1toXMLDSIG(byte[] asn1Bytes, int size) |
da7dc40edb67
8038349: Signing XML with DSA throws Exception when key is larger than 1024 bits
mullan
parents:
18780
diff
changeset
|
185 |
throws IOException |
da7dc40edb67
8038349: Signing XML with DSA throws Exception when key is larger than 1024 bits
mullan
parents:
18780
diff
changeset
|
186 |
{ |
da7dc40edb67
8038349: Signing XML with DSA throws Exception when key is larger than 1024 bits
mullan
parents:
18780
diff
changeset
|
187 |
if (asn1Bytes[0] != 48 || asn1Bytes[1] != asn1Bytes.length - 2 |
da7dc40edb67
8038349: Signing XML with DSA throws Exception when key is larger than 1024 bits
mullan
parents:
18780
diff
changeset
|
188 |
|| asn1Bytes[2] != 2) { |
da7dc40edb67
8038349: Signing XML with DSA throws Exception when key is larger than 1024 bits
mullan
parents:
18780
diff
changeset
|
189 |
throw new IOException("Invalid ASN.1 format of DSA signature"); |
da7dc40edb67
8038349: Signing XML with DSA throws Exception when key is larger than 1024 bits
mullan
parents:
18780
diff
changeset
|
190 |
} |
da7dc40edb67
8038349: Signing XML with DSA throws Exception when key is larger than 1024 bits
mullan
parents:
18780
diff
changeset
|
191 |
|
da7dc40edb67
8038349: Signing XML with DSA throws Exception when key is larger than 1024 bits
mullan
parents:
18780
diff
changeset
|
192 |
byte rLength = asn1Bytes[3]; |
da7dc40edb67
8038349: Signing XML with DSA throws Exception when key is larger than 1024 bits
mullan
parents:
18780
diff
changeset
|
193 |
int i; |
da7dc40edb67
8038349: Signing XML with DSA throws Exception when key is larger than 1024 bits
mullan
parents:
18780
diff
changeset
|
194 |
for (i = rLength; i > 0 && asn1Bytes[4 + rLength - i] == 0; i--); |
da7dc40edb67
8038349: Signing XML with DSA throws Exception when key is larger than 1024 bits
mullan
parents:
18780
diff
changeset
|
195 |
|
da7dc40edb67
8038349: Signing XML with DSA throws Exception when key is larger than 1024 bits
mullan
parents:
18780
diff
changeset
|
196 |
byte sLength = asn1Bytes[5 + rLength]; |
da7dc40edb67
8038349: Signing XML with DSA throws Exception when key is larger than 1024 bits
mullan
parents:
18780
diff
changeset
|
197 |
int j; |
da7dc40edb67
8038349: Signing XML with DSA throws Exception when key is larger than 1024 bits
mullan
parents:
18780
diff
changeset
|
198 |
for (j = sLength; |
da7dc40edb67
8038349: Signing XML with DSA throws Exception when key is larger than 1024 bits
mullan
parents:
18780
diff
changeset
|
199 |
j > 0 && asn1Bytes[6 + rLength + sLength - j] == 0; j--); |
da7dc40edb67
8038349: Signing XML with DSA throws Exception when key is larger than 1024 bits
mullan
parents:
18780
diff
changeset
|
200 |
|
da7dc40edb67
8038349: Signing XML with DSA throws Exception when key is larger than 1024 bits
mullan
parents:
18780
diff
changeset
|
201 |
if (i > size || asn1Bytes[4 + rLength] != 2 || j > size) { |
da7dc40edb67
8038349: Signing XML with DSA throws Exception when key is larger than 1024 bits
mullan
parents:
18780
diff
changeset
|
202 |
throw new IOException("Invalid ASN.1 format of DSA signature"); |
da7dc40edb67
8038349: Signing XML with DSA throws Exception when key is larger than 1024 bits
mullan
parents:
18780
diff
changeset
|
203 |
} else { |
da7dc40edb67
8038349: Signing XML with DSA throws Exception when key is larger than 1024 bits
mullan
parents:
18780
diff
changeset
|
204 |
byte[] xmldsigBytes = new byte[size * 2]; |
da7dc40edb67
8038349: Signing XML with DSA throws Exception when key is larger than 1024 bits
mullan
parents:
18780
diff
changeset
|
205 |
System.arraycopy(asn1Bytes, 4 + rLength - i, xmldsigBytes, |
da7dc40edb67
8038349: Signing XML with DSA throws Exception when key is larger than 1024 bits
mullan
parents:
18780
diff
changeset
|
206 |
size - i, i); |
da7dc40edb67
8038349: Signing XML with DSA throws Exception when key is larger than 1024 bits
mullan
parents:
18780
diff
changeset
|
207 |
System.arraycopy(asn1Bytes, 6 + rLength + sLength - j, |
da7dc40edb67
8038349: Signing XML with DSA throws Exception when key is larger than 1024 bits
mullan
parents:
18780
diff
changeset
|
208 |
xmldsigBytes, size * 2 - j, j); |
da7dc40edb67
8038349: Signing XML with DSA throws Exception when key is larger than 1024 bits
mullan
parents:
18780
diff
changeset
|
209 |
return xmldsigBytes; |
da7dc40edb67
8038349: Signing XML with DSA throws Exception when key is larger than 1024 bits
mullan
parents:
18780
diff
changeset
|
210 |
} |
da7dc40edb67
8038349: Signing XML with DSA throws Exception when key is larger than 1024 bits
mullan
parents:
18780
diff
changeset
|
211 |
} |
da7dc40edb67
8038349: Signing XML with DSA throws Exception when key is larger than 1024 bits
mullan
parents:
18780
diff
changeset
|
212 |
|
da7dc40edb67
8038349: Signing XML with DSA throws Exception when key is larger than 1024 bits
mullan
parents:
18780
diff
changeset
|
213 |
/** |
da7dc40edb67
8038349: Signing XML with DSA throws Exception when key is larger than 1024 bits
mullan
parents:
18780
diff
changeset
|
214 |
* Converts an XML Signature DSA Value to a ASN.1 DSA value. |
da7dc40edb67
8038349: Signing XML with DSA throws Exception when key is larger than 1024 bits
mullan
parents:
18780
diff
changeset
|
215 |
* |
da7dc40edb67
8038349: Signing XML with DSA throws Exception when key is larger than 1024 bits
mullan
parents:
18780
diff
changeset
|
216 |
* The JCE DSA Signature algorithm creates ASN.1 encoded (r,s) value |
da7dc40edb67
8038349: Signing XML with DSA throws Exception when key is larger than 1024 bits
mullan
parents:
18780
diff
changeset
|
217 |
* pairs (see section 2.2.2 of RFC 3279); the XML Signature requires the |
da7dc40edb67
8038349: Signing XML with DSA throws Exception when key is larger than 1024 bits
mullan
parents:
18780
diff
changeset
|
218 |
* core BigInteger values. |
da7dc40edb67
8038349: Signing XML with DSA throws Exception when key is larger than 1024 bits
mullan
parents:
18780
diff
changeset
|
219 |
* |
da7dc40edb67
8038349: Signing XML with DSA throws Exception when key is larger than 1024 bits
mullan
parents:
18780
diff
changeset
|
220 |
* @param xmldsigBytes the XML Signature encoded bytes |
da7dc40edb67
8038349: Signing XML with DSA throws Exception when key is larger than 1024 bits
mullan
parents:
18780
diff
changeset
|
221 |
* @param size size of r and s in bytes |
da7dc40edb67
8038349: Signing XML with DSA throws Exception when key is larger than 1024 bits
mullan
parents:
18780
diff
changeset
|
222 |
* @return the ASN.1 encoded bytes |
da7dc40edb67
8038349: Signing XML with DSA throws Exception when key is larger than 1024 bits
mullan
parents:
18780
diff
changeset
|
223 |
* @throws IOException if the bytes are not encoded correctly |
da7dc40edb67
8038349: Signing XML with DSA throws Exception when key is larger than 1024 bits
mullan
parents:
18780
diff
changeset
|
224 |
* @see <A HREF="http://www.w3.org/TR/xmldsig-core1/#sec-DSA">6.4.1 DSA</A> |
da7dc40edb67
8038349: Signing XML with DSA throws Exception when key is larger than 1024 bits
mullan
parents:
18780
diff
changeset
|
225 |
*/ |
da7dc40edb67
8038349: Signing XML with DSA throws Exception when key is larger than 1024 bits
mullan
parents:
18780
diff
changeset
|
226 |
public static byte[] convertDsaXMLDSIGtoASN1(byte[] xmldsigBytes, int size) |
da7dc40edb67
8038349: Signing XML with DSA throws Exception when key is larger than 1024 bits
mullan
parents:
18780
diff
changeset
|
227 |
throws IOException |
da7dc40edb67
8038349: Signing XML with DSA throws Exception when key is larger than 1024 bits
mullan
parents:
18780
diff
changeset
|
228 |
{ |
da7dc40edb67
8038349: Signing XML with DSA throws Exception when key is larger than 1024 bits
mullan
parents:
18780
diff
changeset
|
229 |
int totalSize = size * 2; |
da7dc40edb67
8038349: Signing XML with DSA throws Exception when key is larger than 1024 bits
mullan
parents:
18780
diff
changeset
|
230 |
if (xmldsigBytes.length != totalSize) { |
da7dc40edb67
8038349: Signing XML with DSA throws Exception when key is larger than 1024 bits
mullan
parents:
18780
diff
changeset
|
231 |
throw new IOException("Invalid XMLDSIG format of DSA signature"); |
da7dc40edb67
8038349: Signing XML with DSA throws Exception when key is larger than 1024 bits
mullan
parents:
18780
diff
changeset
|
232 |
} |
da7dc40edb67
8038349: Signing XML with DSA throws Exception when key is larger than 1024 bits
mullan
parents:
18780
diff
changeset
|
233 |
|
da7dc40edb67
8038349: Signing XML with DSA throws Exception when key is larger than 1024 bits
mullan
parents:
18780
diff
changeset
|
234 |
int i; |
da7dc40edb67
8038349: Signing XML with DSA throws Exception when key is larger than 1024 bits
mullan
parents:
18780
diff
changeset
|
235 |
for (i = size; i > 0 && xmldsigBytes[size - i] == 0; i--); |
da7dc40edb67
8038349: Signing XML with DSA throws Exception when key is larger than 1024 bits
mullan
parents:
18780
diff
changeset
|
236 |
|
da7dc40edb67
8038349: Signing XML with DSA throws Exception when key is larger than 1024 bits
mullan
parents:
18780
diff
changeset
|
237 |
int j = i; |
da7dc40edb67
8038349: Signing XML with DSA throws Exception when key is larger than 1024 bits
mullan
parents:
18780
diff
changeset
|
238 |
if (xmldsigBytes[size - i] < 0) { |
da7dc40edb67
8038349: Signing XML with DSA throws Exception when key is larger than 1024 bits
mullan
parents:
18780
diff
changeset
|
239 |
j++; |
da7dc40edb67
8038349: Signing XML with DSA throws Exception when key is larger than 1024 bits
mullan
parents:
18780
diff
changeset
|
240 |
} |
da7dc40edb67
8038349: Signing XML with DSA throws Exception when key is larger than 1024 bits
mullan
parents:
18780
diff
changeset
|
241 |
|
da7dc40edb67
8038349: Signing XML with DSA throws Exception when key is larger than 1024 bits
mullan
parents:
18780
diff
changeset
|
242 |
int k; |
da7dc40edb67
8038349: Signing XML with DSA throws Exception when key is larger than 1024 bits
mullan
parents:
18780
diff
changeset
|
243 |
for (k = size; k > 0 && xmldsigBytes[totalSize - k] == 0; k--); |
da7dc40edb67
8038349: Signing XML with DSA throws Exception when key is larger than 1024 bits
mullan
parents:
18780
diff
changeset
|
244 |
|
da7dc40edb67
8038349: Signing XML with DSA throws Exception when key is larger than 1024 bits
mullan
parents:
18780
diff
changeset
|
245 |
int l = k; |
da7dc40edb67
8038349: Signing XML with DSA throws Exception when key is larger than 1024 bits
mullan
parents:
18780
diff
changeset
|
246 |
if (xmldsigBytes[totalSize - k] < 0) { |
da7dc40edb67
8038349: Signing XML with DSA throws Exception when key is larger than 1024 bits
mullan
parents:
18780
diff
changeset
|
247 |
l++; |
da7dc40edb67
8038349: Signing XML with DSA throws Exception when key is larger than 1024 bits
mullan
parents:
18780
diff
changeset
|
248 |
} |
da7dc40edb67
8038349: Signing XML with DSA throws Exception when key is larger than 1024 bits
mullan
parents:
18780
diff
changeset
|
249 |
|
da7dc40edb67
8038349: Signing XML with DSA throws Exception when key is larger than 1024 bits
mullan
parents:
18780
diff
changeset
|
250 |
byte[] asn1Bytes = new byte[6 + j + l]; |
da7dc40edb67
8038349: Signing XML with DSA throws Exception when key is larger than 1024 bits
mullan
parents:
18780
diff
changeset
|
251 |
asn1Bytes[0] = 48; |
da7dc40edb67
8038349: Signing XML with DSA throws Exception when key is larger than 1024 bits
mullan
parents:
18780
diff
changeset
|
252 |
asn1Bytes[1] = (byte)(4 + j + l); |
da7dc40edb67
8038349: Signing XML with DSA throws Exception when key is larger than 1024 bits
mullan
parents:
18780
diff
changeset
|
253 |
asn1Bytes[2] = 2; |
da7dc40edb67
8038349: Signing XML with DSA throws Exception when key is larger than 1024 bits
mullan
parents:
18780
diff
changeset
|
254 |
asn1Bytes[3] = (byte)j; |
da7dc40edb67
8038349: Signing XML with DSA throws Exception when key is larger than 1024 bits
mullan
parents:
18780
diff
changeset
|
255 |
System.arraycopy(xmldsigBytes, size - i, asn1Bytes, 4 + j - i, i); |
da7dc40edb67
8038349: Signing XML with DSA throws Exception when key is larger than 1024 bits
mullan
parents:
18780
diff
changeset
|
256 |
|
da7dc40edb67
8038349: Signing XML with DSA throws Exception when key is larger than 1024 bits
mullan
parents:
18780
diff
changeset
|
257 |
asn1Bytes[4 + j] = 2; |
da7dc40edb67
8038349: Signing XML with DSA throws Exception when key is larger than 1024 bits
mullan
parents:
18780
diff
changeset
|
258 |
asn1Bytes[5 + j] = (byte) l; |
da7dc40edb67
8038349: Signing XML with DSA throws Exception when key is larger than 1024 bits
mullan
parents:
18780
diff
changeset
|
259 |
System.arraycopy(xmldsigBytes, totalSize - k, asn1Bytes, |
da7dc40edb67
8038349: Signing XML with DSA throws Exception when key is larger than 1024 bits
mullan
parents:
18780
diff
changeset
|
260 |
6 + j + l - k, k); |
da7dc40edb67
8038349: Signing XML with DSA throws Exception when key is larger than 1024 bits
mullan
parents:
18780
diff
changeset
|
261 |
|
da7dc40edb67
8038349: Signing XML with DSA throws Exception when key is larger than 1024 bits
mullan
parents:
18780
diff
changeset
|
262 |
return asn1Bytes; |
da7dc40edb67
8038349: Signing XML with DSA throws Exception when key is larger than 1024 bits
mullan
parents:
18780
diff
changeset
|
263 |
} |
2 | 264 |
} |