Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
openanolis
dragonwell8_jdk
提交
2ba224d3
D
dragonwell8_jdk
项目概览
openanolis
/
dragonwell8_jdk
通知
4
Star
2
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
D
dragonwell8_jdk
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
2ba224d3
编写于
6月 23, 2009
作者:
M
mullan
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
6824440: XML Signature HMAC issue
Reviewed-by: asaha
上级
e62aae22
变更
9
隐藏空白更改
内联
并排
Showing
9 changed file
with
311 addition
and
57 deletion
+311
-57
src/share/classes/com/sun/org/apache/xml/internal/security/algorithms/implementations/IntegrityHmac.java
...al/security/algorithms/implementations/IntegrityHmac.java
+55
-21
src/share/classes/org/jcp/xml/dsig/internal/dom/DOMHMACSignatureMethod.java
...org/jcp/xml/dsig/internal/dom/DOMHMACSignatureMethod.java
+29
-24
test/com/sun/org/apache/xml/internal/security/TruncateHMAC.java
...om/sun/org/apache/xml/internal/security/TruncateHMAC.java
+118
-0
test/com/sun/org/apache/xml/internal/security/signature-enveloping-hmac-sha1-trunclen-0-attack.xml
...rity/signature-enveloping-hmac-sha1-trunclen-0-attack.xml
+16
-0
test/com/sun/org/apache/xml/internal/security/signature-enveloping-hmac-sha1-trunclen-8-attack.xml
...rity/signature-enveloping-hmac-sha1-trunclen-8-attack.xml
+17
-0
test/javax/xml/crypto/dsig/GenerationTests.java
test/javax/xml/crypto/dsig/GenerationTests.java
+10
-6
test/javax/xml/crypto/dsig/ValidationTests.java
test/javax/xml/crypto/dsig/ValidationTests.java
+33
-6
test/javax/xml/crypto/dsig/data/signature-enveloping-hmac-sha1-trunclen-0-attack.xml
...data/signature-enveloping-hmac-sha1-trunclen-0-attack.xml
+16
-0
test/javax/xml/crypto/dsig/data/signature-enveloping-hmac-sha1-trunclen-8-attack.xml
...data/signature-enveloping-hmac-sha1-trunclen-8-attack.xml
+17
-0
未找到文件。
src/share/classes/com/sun/org/apache/xml/internal/security/algorithms/implementations/IntegrityHmac.java
浏览文件 @
2ba224d3
...
@@ -60,8 +60,14 @@ public abstract class IntegrityHmac extends SignatureAlgorithmSpi {
...
@@ -60,8 +60,14 @@ public abstract class IntegrityHmac extends SignatureAlgorithmSpi {
*/
*/
public
abstract
String
engineGetURI
();
public
abstract
String
engineGetURI
();
/**
* Returns the output length of the hash/digest.
*/
abstract
int
getDigestLength
();
/** Field _macAlgorithm */
/** Field _macAlgorithm */
private
Mac
_macAlgorithm
=
null
;
private
Mac
_macAlgorithm
=
null
;
private
boolean
_HMACOutputLengthSet
=
false
;
/** Field _HMACOutputLength */
/** Field _HMACOutputLength */
int
_HMACOutputLength
=
0
;
int
_HMACOutputLength
=
0
;
...
@@ -115,14 +121,16 @@ public abstract class IntegrityHmac extends SignatureAlgorithmSpi {
...
@@ -115,14 +121,16 @@ public abstract class IntegrityHmac extends SignatureAlgorithmSpi {
throws
XMLSignatureException
{
throws
XMLSignatureException
{
try
{
try
{
byte
[]
completeResult
=
this
.
_macAlgorithm
.
doFinal
();
if
(
this
.
_HMACOutputLengthSet
&&
this
.
_HMACOutputLength
<
getDigestLength
())
{
if
(
log
.
isLoggable
(
java
.
util
.
logging
.
Level
.
FINE
))
{
if
((
this
.
_HMACOutputLength
==
0
)
||
(
this
.
_HMACOutputLength
>=
160
))
{
log
.
log
(
java
.
util
.
logging
.
Level
.
FINE
,
"HMACOutputLength must not be less than "
+
getDigestLength
());
}
throw
new
XMLSignatureException
(
"errorMessages.XMLSignatureException"
);
}
else
{
byte
[]
completeResult
=
this
.
_macAlgorithm
.
doFinal
();
return
MessageDigestAlgorithm
.
isEqual
(
completeResult
,
signature
);
return
MessageDigestAlgorithm
.
isEqual
(
completeResult
,
signature
);
}
}
byte
[]
stripped
=
IntegrityHmac
.
reduceBitLength
(
completeResult
,
this
.
_HMACOutputLength
);
return
MessageDigestAlgorithm
.
isEqual
(
stripped
,
signature
);
}
catch
(
IllegalStateException
ex
)
{
}
catch
(
IllegalStateException
ex
)
{
throw
new
XMLSignatureException
(
"empty"
,
ex
);
throw
new
XMLSignatureException
(
"empty"
,
ex
);
}
}
...
@@ -176,14 +184,15 @@ public abstract class IntegrityHmac extends SignatureAlgorithmSpi {
...
@@ -176,14 +184,15 @@ public abstract class IntegrityHmac extends SignatureAlgorithmSpi {
protected
byte
[]
engineSign
()
throws
XMLSignatureException
{
protected
byte
[]
engineSign
()
throws
XMLSignatureException
{
try
{
try
{
byte
[]
completeResult
=
this
.
_macAlgorithm
.
doFinal
();
if
(
this
.
_HMACOutputLengthSet
&&
this
.
_HMACOutputLength
<
getDigestLength
())
{
if
(
log
.
isLoggable
(
java
.
util
.
logging
.
Level
.
FINE
))
{
if
((
this
.
_HMACOutputLength
==
0
)
||
(
this
.
_HMACOutputLength
>=
160
))
{
log
.
log
(
java
.
util
.
logging
.
Level
.
FINE
,
return
completeResult
;
"HMACOutputLength must not be less than "
+
getDigestLength
());
}
throw
new
XMLSignatureException
(
"errorMessages.XMLSignatureException"
);
}
else
{
return
this
.
_macAlgorithm
.
doFinal
();
}
}
return
IntegrityHmac
.
reduceBitLength
(
completeResult
,
this
.
_HMACOutputLength
);
}
catch
(
IllegalStateException
ex
)
{
}
catch
(
IllegalStateException
ex
)
{
throw
new
XMLSignatureException
(
"empty"
,
ex
);
throw
new
XMLSignatureException
(
"empty"
,
ex
);
}
}
...
@@ -361,6 +370,7 @@ public abstract class IntegrityHmac extends SignatureAlgorithmSpi {
...
@@ -361,6 +370,7 @@ public abstract class IntegrityHmac extends SignatureAlgorithmSpi {
*/
*/
protected
void
engineSetHMACOutputLength
(
int
HMACOutputLength
)
{
protected
void
engineSetHMACOutputLength
(
int
HMACOutputLength
)
{
this
.
_HMACOutputLength
=
HMACOutputLength
;
this
.
_HMACOutputLength
=
HMACOutputLength
;
this
.
_HMACOutputLengthSet
=
true
;
}
}
/**
/**
...
@@ -376,12 +386,13 @@ public abstract class IntegrityHmac extends SignatureAlgorithmSpi {
...
@@ -376,12 +386,13 @@ public abstract class IntegrityHmac extends SignatureAlgorithmSpi {
throw
new
IllegalArgumentException
(
"element null"
);
throw
new
IllegalArgumentException
(
"element null"
);
}
}
Text
hmaclength
=
XMLUtils
.
selectDsNodeText
(
element
.
getFirstChild
(),
Text
hmaclength
=
XMLUtils
.
selectDsNodeText
(
element
.
getFirstChild
(),
Constants
.
_TAG_HMACOUTPUTLENGTH
,
0
);
Constants
.
_TAG_HMACOUTPUTLENGTH
,
0
);
if
(
hmaclength
!=
null
)
{
if
(
hmaclength
!=
null
)
{
this
.
_HMACOutputLength
=
Integer
.
parseInt
(
hmaclength
.
getData
());
this
.
_HMACOutputLength
=
Integer
.
parseInt
(
hmaclength
.
getData
());
}
this
.
_HMACOutputLengthSet
=
true
;
}
}
}
...
@@ -390,14 +401,13 @@ public abstract class IntegrityHmac extends SignatureAlgorithmSpi {
...
@@ -390,14 +401,13 @@ public abstract class IntegrityHmac extends SignatureAlgorithmSpi {
*
*
* @param element
* @param element
*/
*/
public
void
engineAddContextToElement
(
Element
element
)
public
void
engineAddContextToElement
(
Element
element
)
{
{
if
(
element
==
null
)
{
if
(
element
==
null
)
{
throw
new
IllegalArgumentException
(
"null element"
);
throw
new
IllegalArgumentException
(
"null element"
);
}
}
if
(
this
.
_HMACOutputLength
!=
0
)
{
if
(
this
.
_HMACOutputLength
Set
)
{
Document
doc
=
element
.
getOwnerDocument
();
Document
doc
=
element
.
getOwnerDocument
();
Element
HMElem
=
XMLUtils
.
createElementInSignatureSpace
(
doc
,
Element
HMElem
=
XMLUtils
.
createElementInSignatureSpace
(
doc
,
Constants
.
_TAG_HMACOUTPUTLENGTH
);
Constants
.
_TAG_HMACOUTPUTLENGTH
);
...
@@ -436,6 +446,10 @@ public abstract class IntegrityHmac extends SignatureAlgorithmSpi {
...
@@ -436,6 +446,10 @@ public abstract class IntegrityHmac extends SignatureAlgorithmSpi {
public
String
engineGetURI
()
{
public
String
engineGetURI
()
{
return
XMLSignature
.
ALGO_ID_MAC_HMAC_SHA1
;
return
XMLSignature
.
ALGO_ID_MAC_HMAC_SHA1
;
}
}
int
getDigestLength
()
{
return
160
;
}
}
}
/**
/**
...
@@ -463,6 +477,10 @@ public abstract class IntegrityHmac extends SignatureAlgorithmSpi {
...
@@ -463,6 +477,10 @@ public abstract class IntegrityHmac extends SignatureAlgorithmSpi {
public
String
engineGetURI
()
{
public
String
engineGetURI
()
{
return
XMLSignature
.
ALGO_ID_MAC_HMAC_SHA256
;
return
XMLSignature
.
ALGO_ID_MAC_HMAC_SHA256
;
}
}
int
getDigestLength
()
{
return
256
;
}
}
}
/**
/**
...
@@ -490,6 +508,10 @@ public abstract class IntegrityHmac extends SignatureAlgorithmSpi {
...
@@ -490,6 +508,10 @@ public abstract class IntegrityHmac extends SignatureAlgorithmSpi {
public
String
engineGetURI
()
{
public
String
engineGetURI
()
{
return
XMLSignature
.
ALGO_ID_MAC_HMAC_SHA384
;
return
XMLSignature
.
ALGO_ID_MAC_HMAC_SHA384
;
}
}
int
getDigestLength
()
{
return
384
;
}
}
}
/**
/**
...
@@ -517,6 +539,10 @@ public abstract class IntegrityHmac extends SignatureAlgorithmSpi {
...
@@ -517,6 +539,10 @@ public abstract class IntegrityHmac extends SignatureAlgorithmSpi {
public
String
engineGetURI
()
{
public
String
engineGetURI
()
{
return
XMLSignature
.
ALGO_ID_MAC_HMAC_SHA512
;
return
XMLSignature
.
ALGO_ID_MAC_HMAC_SHA512
;
}
}
int
getDigestLength
()
{
return
512
;
}
}
}
/**
/**
...
@@ -544,6 +570,10 @@ public abstract class IntegrityHmac extends SignatureAlgorithmSpi {
...
@@ -544,6 +570,10 @@ public abstract class IntegrityHmac extends SignatureAlgorithmSpi {
public
String
engineGetURI
()
{
public
String
engineGetURI
()
{
return
XMLSignature
.
ALGO_ID_MAC_HMAC_RIPEMD160
;
return
XMLSignature
.
ALGO_ID_MAC_HMAC_RIPEMD160
;
}
}
int
getDigestLength
()
{
return
160
;
}
}
}
/**
/**
...
@@ -571,5 +601,9 @@ public abstract class IntegrityHmac extends SignatureAlgorithmSpi {
...
@@ -571,5 +601,9 @@ public abstract class IntegrityHmac extends SignatureAlgorithmSpi {
public
String
engineGetURI
()
{
public
String
engineGetURI
()
{
return
XMLSignature
.
ALGO_ID_MAC_HMAC_NOT_RECOMMENDED_MD5
;
return
XMLSignature
.
ALGO_ID_MAC_HMAC_NOT_RECOMMENDED_MD5
;
}
}
int
getDigestLength
()
{
return
128
;
}
}
}
}
}
src/share/classes/org/jcp/xml/dsig/internal/dom/DOMHMACSignatureMethod.java
浏览文件 @
2ba224d3
...
@@ -19,7 +19,7 @@
...
@@ -19,7 +19,7 @@
*
*
*/
*/
/*
/*
* Copyright 2005-200
8
Sun Microsystems, Inc. All rights reserved.
* Copyright 2005-200
9
Sun Microsystems, Inc. All rights reserved.
*/
*/
/*
/*
* $Id: DOMHMACSignatureMethod.java,v 1.2 2008/07/24 15:20:32 mullan Exp $
* $Id: DOMHMACSignatureMethod.java,v 1.2 2008/07/24 15:20:32 mullan Exp $
...
@@ -58,6 +58,7 @@ public abstract class DOMHMACSignatureMethod extends DOMSignatureMethod {
...
@@ -58,6 +58,7 @@ public abstract class DOMHMACSignatureMethod extends DOMSignatureMethod {
Logger
.
getLogger
(
"org.jcp.xml.dsig.internal.dom"
);
Logger
.
getLogger
(
"org.jcp.xml.dsig.internal.dom"
);
private
Mac
hmac
;
private
Mac
hmac
;
private
int
outputLength
;
private
int
outputLength
;
private
boolean
outputLengthSet
;
/**
/**
* Creates a <code>DOMHMACSignatureMethod</code> with the specified params
* Creates a <code>DOMHMACSignatureMethod</code> with the specified params
...
@@ -87,6 +88,7 @@ public abstract class DOMHMACSignatureMethod extends DOMSignatureMethod {
...
@@ -87,6 +88,7 @@ public abstract class DOMHMACSignatureMethod extends DOMSignatureMethod {
(
"params must be of type HMACParameterSpec"
);
(
"params must be of type HMACParameterSpec"
);
}
}
outputLength
=
((
HMACParameterSpec
)
params
).
getOutputLength
();
outputLength
=
((
HMACParameterSpec
)
params
).
getOutputLength
();
outputLengthSet
=
true
;
if
(
log
.
isLoggable
(
Level
.
FINE
))
{
if
(
log
.
isLoggable
(
Level
.
FINE
))
{
log
.
log
(
Level
.
FINE
,
log
.
log
(
Level
.
FINE
,
"Setting outputLength from HMACParameterSpec to: "
"Setting outputLength from HMACParameterSpec to: "
...
@@ -101,6 +103,7 @@ public abstract class DOMHMACSignatureMethod extends DOMSignatureMethod {
...
@@ -101,6 +103,7 @@ public abstract class DOMHMACSignatureMethod extends DOMSignatureMethod {
throws
MarshalException
{
throws
MarshalException
{
outputLength
=
new
Integer
outputLength
=
new
Integer
(
paramsElem
.
getFirstChild
().
getNodeValue
()).
intValue
();
(
paramsElem
.
getFirstChild
().
getNodeValue
()).
intValue
();
outputLengthSet
=
true
;
if
(
log
.
isLoggable
(
Level
.
FINE
))
{
if
(
log
.
isLoggable
(
Level
.
FINE
))
{
log
.
log
(
Level
.
FINE
,
"unmarshalled outputLength: "
+
outputLength
);
log
.
log
(
Level
.
FINE
,
"unmarshalled outputLength: "
+
outputLength
);
}
}
...
@@ -135,23 +138,13 @@ public abstract class DOMHMACSignatureMethod extends DOMSignatureMethod {
...
@@ -135,23 +138,13 @@ public abstract class DOMHMACSignatureMethod extends DOMSignatureMethod {
throw
new
XMLSignatureException
(
nsae
);
throw
new
XMLSignatureException
(
nsae
);
}
}
}
}
if
(
log
.
isLoggable
(
Level
.
FINE
))
{
if
(
outputLengthSet
&&
outputLength
<
getDigestLength
())
{
log
.
log
(
Level
.
FINE
,
"outputLength = "
+
outputLength
);
throw
new
XMLSignatureException
(
"HMACOutputLength must not be less than "
+
getDigestLength
());
}
}
hmac
.
init
((
SecretKey
)
key
);
hmac
.
init
((
SecretKey
)
key
);
si
.
canonicalize
(
context
,
new
MacOutputStream
(
hmac
));
si
.
canonicalize
(
context
,
new
MacOutputStream
(
hmac
));
byte
[]
result
=
hmac
.
doFinal
();
byte
[]
result
=
hmac
.
doFinal
();
if
(
log
.
isLoggable
(
Level
.
FINE
))
{
log
.
log
(
Level
.
FINE
,
"resultLength = "
+
result
.
length
);
}
if
(
outputLength
!=
-
1
)
{
int
byteLength
=
outputLength
/
8
;
if
(
result
.
length
>
byteLength
)
{
byte
[]
truncated
=
new
byte
[
byteLength
];
System
.
arraycopy
(
result
,
0
,
truncated
,
0
,
byteLength
);
result
=
truncated
;
}
}
return
MessageDigest
.
isEqual
(
sig
,
result
);
return
MessageDigest
.
isEqual
(
sig
,
result
);
}
}
...
@@ -171,18 +164,13 @@ public abstract class DOMHMACSignatureMethod extends DOMSignatureMethod {
...
@@ -171,18 +164,13 @@ public abstract class DOMHMACSignatureMethod extends DOMSignatureMethod {
throw
new
XMLSignatureException
(
nsae
);
throw
new
XMLSignatureException
(
nsae
);
}
}
}
}
if
(
outputLengthSet
&&
outputLength
<
getDigestLength
())
{
throw
new
XMLSignatureException
(
"HMACOutputLength must not be less than "
+
getDigestLength
());
}
hmac
.
init
((
SecretKey
)
key
);
hmac
.
init
((
SecretKey
)
key
);
si
.
canonicalize
(
context
,
new
MacOutputStream
(
hmac
));
si
.
canonicalize
(
context
,
new
MacOutputStream
(
hmac
));
byte
[]
result
=
hmac
.
doFinal
();
return
hmac
.
doFinal
();
if
(
outputLength
!=
-
1
)
{
int
byteLength
=
outputLength
/
8
;
if
(
result
.
length
>
byteLength
)
{
byte
[]
truncated
=
new
byte
[
byteLength
];
System
.
arraycopy
(
result
,
0
,
truncated
,
0
,
byteLength
);
result
=
truncated
;
}
}
return
result
;
}
}
boolean
paramsEqual
(
AlgorithmParameterSpec
spec
)
{
boolean
paramsEqual
(
AlgorithmParameterSpec
spec
)
{
...
@@ -197,6 +185,11 @@ public abstract class DOMHMACSignatureMethod extends DOMSignatureMethod {
...
@@ -197,6 +185,11 @@ public abstract class DOMHMACSignatureMethod extends DOMSignatureMethod {
return
(
outputLength
==
ospec
.
getOutputLength
());
return
(
outputLength
==
ospec
.
getOutputLength
());
}
}
/**
* Returns the output length of the hash/digest.
*/
abstract
int
getDigestLength
();
static
final
class
SHA1
extends
DOMHMACSignatureMethod
{
static
final
class
SHA1
extends
DOMHMACSignatureMethod
{
SHA1
(
AlgorithmParameterSpec
params
)
SHA1
(
AlgorithmParameterSpec
params
)
throws
InvalidAlgorithmParameterException
{
throws
InvalidAlgorithmParameterException
{
...
@@ -211,6 +204,9 @@ public abstract class DOMHMACSignatureMethod extends DOMSignatureMethod {
...
@@ -211,6 +204,9 @@ public abstract class DOMHMACSignatureMethod extends DOMSignatureMethod {
String
getSignatureAlgorithm
()
{
String
getSignatureAlgorithm
()
{
return
"HmacSHA1"
;
return
"HmacSHA1"
;
}
}
int
getDigestLength
()
{
return
160
;
}
}
}
static
final
class
SHA256
extends
DOMHMACSignatureMethod
{
static
final
class
SHA256
extends
DOMHMACSignatureMethod
{
...
@@ -227,6 +223,9 @@ public abstract class DOMHMACSignatureMethod extends DOMSignatureMethod {
...
@@ -227,6 +223,9 @@ public abstract class DOMHMACSignatureMethod extends DOMSignatureMethod {
String
getSignatureAlgorithm
()
{
String
getSignatureAlgorithm
()
{
return
"HmacSHA256"
;
return
"HmacSHA256"
;
}
}
int
getDigestLength
()
{
return
256
;
}
}
}
static
final
class
SHA384
extends
DOMHMACSignatureMethod
{
static
final
class
SHA384
extends
DOMHMACSignatureMethod
{
...
@@ -243,6 +242,9 @@ public abstract class DOMHMACSignatureMethod extends DOMSignatureMethod {
...
@@ -243,6 +242,9 @@ public abstract class DOMHMACSignatureMethod extends DOMSignatureMethod {
String
getSignatureAlgorithm
()
{
String
getSignatureAlgorithm
()
{
return
"HmacSHA384"
;
return
"HmacSHA384"
;
}
}
int
getDigestLength
()
{
return
384
;
}
}
}
static
final
class
SHA512
extends
DOMHMACSignatureMethod
{
static
final
class
SHA512
extends
DOMHMACSignatureMethod
{
...
@@ -259,5 +261,8 @@ public abstract class DOMHMACSignatureMethod extends DOMSignatureMethod {
...
@@ -259,5 +261,8 @@ public abstract class DOMHMACSignatureMethod extends DOMSignatureMethod {
String
getSignatureAlgorithm
()
{
String
getSignatureAlgorithm
()
{
return
"HmacSHA512"
;
return
"HmacSHA512"
;
}
}
int
getDigestLength
()
{
return
512
;
}
}
}
}
}
test/com/sun/org/apache/xml/internal/security/TruncateHMAC.java
0 → 100644
浏览文件 @
2ba224d3
/*
* Copyright 2009 Sun Microsystems, Inc. All Rights Reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
* CA 95054 USA or visit www.sun.com if you need additional information or
* have any questions.
*/
/**
* @test %I% %E%
* @bug 6824440
* @summary Check that Apache XMLSec APIs will not accept HMAC truncation
* lengths less than minimum bound
* @compile -XDignore.symbol.file TruncateHMAC.java
* @run main TruncateHMAC
*/
import
java.io.File
;
import
javax.crypto.SecretKey
;
import
javax.xml.parsers.DocumentBuilderFactory
;
import
org.w3c.dom.Document
;
import
org.w3c.dom.Element
;
import
org.w3c.dom.NodeList
;
import
com.sun.org.apache.xml.internal.security.Init
;
import
com.sun.org.apache.xml.internal.security.c14n.Canonicalizer
;
import
com.sun.org.apache.xml.internal.security.signature.XMLSignature
;
import
com.sun.org.apache.xml.internal.security.signature.XMLSignatureException
;
import
com.sun.org.apache.xml.internal.security.utils.Constants
;
public
class
TruncateHMAC
{
private
final
static
String
DIR
=
System
.
getProperty
(
"test.src"
,
"."
);
private
static
DocumentBuilderFactory
dbf
=
null
;
private
static
boolean
atLeastOneFailed
=
false
;
public
static
void
main
(
String
[]
args
)
throws
Exception
{
Init
.
init
();
dbf
=
DocumentBuilderFactory
.
newInstance
();
dbf
.
setNamespaceAware
(
true
);
dbf
.
setValidating
(
false
);
validate
(
"signature-enveloping-hmac-sha1-trunclen-0-attack.xml"
);
validate
(
"signature-enveloping-hmac-sha1-trunclen-8-attack.xml"
);
generate_hmac_sha1_40
();
if
(
atLeastOneFailed
)
{
throw
new
Exception
(
"At least one signature did not validate as expected"
);
}
}
private
static
void
validate
(
String
data
)
throws
Exception
{
System
.
out
.
println
(
"Validating "
+
data
);
File
file
=
new
File
(
DIR
,
data
);
Document
doc
=
dbf
.
newDocumentBuilder
().
parse
(
file
);
NodeList
nl
=
doc
.
getElementsByTagNameNS
(
Constants
.
SignatureSpecNS
,
"Signature"
);
if
(
nl
.
getLength
()
==
0
)
{
throw
new
Exception
(
"Couldn't find signature Element"
);
}
Element
sigElement
=
(
Element
)
nl
.
item
(
0
);
XMLSignature
signature
=
new
XMLSignature
(
sigElement
,
file
.
toURI
().
toString
());
SecretKey
sk
=
signature
.
createSecretKey
(
"secret"
.
getBytes
(
"ASCII"
));
try
{
System
.
out
.
println
(
"Validation status: "
+
signature
.
checkSignatureValue
(
sk
));
System
.
out
.
println
(
"FAILED"
);
atLeastOneFailed
=
true
;
}
catch
(
XMLSignatureException
xse
)
{
System
.
out
.
println
(
xse
.
getMessage
());
System
.
out
.
println
(
"PASSED"
);
}
}
private
static
void
generate_hmac_sha1_40
()
throws
Exception
{
System
.
out
.
println
(
"Generating "
);
Document
doc
=
dbf
.
newDocumentBuilder
().
newDocument
();
XMLSignature
sig
=
new
XMLSignature
(
doc
,
null
,
XMLSignature
.
ALGO_ID_MAC_HMAC_SHA1
,
40
,
Canonicalizer
.
ALGO_ID_C14N_OMIT_COMMENTS
);
try
{
sig
.
sign
(
getSecretKey
(
"secret"
.
getBytes
(
"ASCII"
)));
System
.
out
.
println
(
"FAILED"
);
atLeastOneFailed
=
true
;
}
catch
(
XMLSignatureException
xse
)
{
System
.
out
.
println
(
xse
.
getMessage
());
System
.
out
.
println
(
"PASSED"
);
}
}
private
static
SecretKey
getSecretKey
(
final
byte
[]
secret
)
{
return
new
SecretKey
()
{
public
String
getFormat
()
{
return
"RAW"
;
}
public
byte
[]
getEncoded
()
{
return
secret
;
}
public
String
getAlgorithm
(){
return
"SECRET"
;
}
};
}
}
test/com/sun/org/apache/xml/internal/security/signature-enveloping-hmac-sha1-trunclen-0-attack.xml
0 → 100644
浏览文件 @
2ba224d3
<?xml version="1.0" encoding="UTF-8"?>
<Signature
xmlns=
"http://www.w3.org/2000/09/xmldsig#"
>
<SignedInfo>
<CanonicalizationMethod
Algorithm=
"http://www.w3.org/TR/2001/REC-xml-c14n-20010315"
/>
<SignatureMethod
Algorithm=
"http://www.w3.org/2000/09/xmldsig#hmac-sha1"
>
<HMACOutputLength>
0
</HMACOutputLength>
</SignatureMethod>
<Reference
URI=
"#object"
>
<DigestMethod
Algorithm=
"http://www.w3.org/2000/09/xmldsig#sha1"
/>
<DigestValue>
nz4GS0NbH2SrWlD/4fX313CoTzc=
</DigestValue>
</Reference>
</SignedInfo>
<SignatureValue>
</SignatureValue>
<Object
Id=
"object"
>
some other text
</Object>
</Signature>
test/com/sun/org/apache/xml/internal/security/signature-enveloping-hmac-sha1-trunclen-8-attack.xml
0 → 100644
浏览文件 @
2ba224d3
<?xml version="1.0" encoding="UTF-8"?>
<Signature
xmlns=
"http://www.w3.org/2000/09/xmldsig#"
>
<SignedInfo>
<CanonicalizationMethod
Algorithm=
"http://www.w3.org/TR/2001/REC-xml-c14n-20010315"
/>
<SignatureMethod
Algorithm=
"http://www.w3.org/2000/09/xmldsig#hmac-sha1"
>
<HMACOutputLength>
8
</HMACOutputLength>
</SignatureMethod>
<Reference
URI=
"#object"
>
<DigestMethod
Algorithm=
"http://www.w3.org/2000/09/xmldsig#sha1"
/>
<DigestValue>
nz4GS0NbH2SrWlD/4fX313CoTzc=
</DigestValue>
</Reference>
</SignedInfo>
<SignatureValue>
Qw==
</SignatureValue>
<Object
Id=
"object"
>
some other text
</Object>
</Signature>
test/javax/xml/crypto/dsig/GenerationTests.java
浏览文件 @
2ba224d3
/*
/*
* Copyright 2005-200
8
Sun Microsystems, Inc. All Rights Reserved.
* Copyright 2005-200
9
Sun Microsystems, Inc. All Rights Reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
*
* This code is free software; you can redistribute it and/or modify it
* This code is free software; you can redistribute it and/or modify it
...
@@ -23,9 +23,7 @@
...
@@ -23,9 +23,7 @@
/**
/**
* @test
* @test
* @bug 4635230
* @bug 4635230 6283345 6303830 6824440
* @bug 6283345
* @bug 6303830
* @summary Basic unit tests for generating XML Signatures with JSR 105
* @summary Basic unit tests for generating XML Signatures with JSR 105
* @compile -XDignore.symbol.file KeySelectors.java SignatureValidator.java
* @compile -XDignore.symbol.file KeySelectors.java SignatureValidator.java
* X509KeySelector.java GenerationTests.java
* X509KeySelector.java GenerationTests.java
...
@@ -248,8 +246,14 @@ public class GenerationTests {
...
@@ -248,8 +246,14 @@ public class GenerationTests {
System
.
out
.
println
(
"* Generating signature-enveloping-hmac-sha1-40.xml"
);
System
.
out
.
println
(
"* Generating signature-enveloping-hmac-sha1-40.xml"
);
SignatureMethod
hmacSha1
=
fac
.
newSignatureMethod
SignatureMethod
hmacSha1
=
fac
.
newSignatureMethod
(
SignatureMethod
.
HMAC_SHA1
,
new
HMACParameterSpec
(
40
));
(
SignatureMethod
.
HMAC_SHA1
,
new
HMACParameterSpec
(
40
));
test_create_signature_enveloping
(
sha1
,
hmacSha1
,
null
,
try
{
getSecretKey
(
"secret"
.
getBytes
(
"ASCII"
)),
sks
,
false
);
test_create_signature_enveloping
(
sha1
,
hmacSha1
,
null
,
getSecretKey
(
"secret"
.
getBytes
(
"ASCII"
)),
sks
,
false
);
}
catch
(
Exception
e
)
{
if
(!(
e
instanceof
XMLSignatureException
))
{
throw
e
;
}
}
System
.
out
.
println
();
System
.
out
.
println
();
}
}
...
...
test/javax/xml/crypto/dsig/ValidationTests.java
浏览文件 @
2ba224d3
/*
/*
* Copyright 2005-200
7
Sun Microsystems, Inc. All Rights Reserved.
* Copyright 2005-200
9
Sun Microsystems, Inc. All Rights Reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
*
* This code is free software; you can redistribute it and/or modify it
* This code is free software; you can redistribute it and/or modify it
...
@@ -23,9 +23,7 @@
...
@@ -23,9 +23,7 @@
/**
/**
* @test
* @test
* @bug 4635230
* @bug 4635230 6365103 6366054 6824440
* @bug 6365103
* @bug 6366054
* @summary Basic unit tests for validating XML Signatures with JSR 105
* @summary Basic unit tests for validating XML Signatures with JSR 105
* @compile -XDignore.symbol.file KeySelectors.java SignatureValidator.java
* @compile -XDignore.symbol.file KeySelectors.java SignatureValidator.java
* X509KeySelector.java ValidationTests.java
* X509KeySelector.java ValidationTests.java
...
@@ -42,6 +40,7 @@ import javax.xml.crypto.URIDereferencer;
...
@@ -42,6 +40,7 @@ import javax.xml.crypto.URIDereferencer;
import
javax.xml.crypto.URIReference
;
import
javax.xml.crypto.URIReference
;
import
javax.xml.crypto.URIReferenceException
;
import
javax.xml.crypto.URIReferenceException
;
import
javax.xml.crypto.XMLCryptoContext
;
import
javax.xml.crypto.XMLCryptoContext
;
import
javax.xml.crypto.dsig.XMLSignatureException
;
import
javax.xml.crypto.dsig.XMLSignatureFactory
;
import
javax.xml.crypto.dsig.XMLSignatureFactory
;
/**
/**
...
@@ -68,7 +67,6 @@ public class ValidationTests {
...
@@ -68,7 +67,6 @@ public class ValidationTests {
"signature-enveloping-dsa.xml"
,
"signature-enveloping-dsa.xml"
,
"signature-enveloping-rsa.xml"
,
"signature-enveloping-rsa.xml"
,
"signature-enveloping-hmac-sha1.xml"
,
"signature-enveloping-hmac-sha1.xml"
,
"signature-enveloping-hmac-sha1-40.xml"
,
"signature-external-dsa.xml"
,
"signature-external-dsa.xml"
,
"signature-external-b64-dsa.xml"
,
"signature-external-b64-dsa.xml"
,
"signature-retrievalmethod-rawx509crt.xml"
,
"signature-retrievalmethod-rawx509crt.xml"
,
...
@@ -106,7 +104,6 @@ public class ValidationTests {
...
@@ -106,7 +104,6 @@ public class ValidationTests {
KVKS
,
KVKS
,
KVKS
,
KVKS
,
SKKS
,
SKKS
,
SKKS
,
KVKS
,
KVKS
,
KVKS
,
KVKS
,
CKS
,
CKS
,
...
@@ -146,6 +143,36 @@ public class ValidationTests {
...
@@ -146,6 +143,36 @@ public class ValidationTests {
atLeastOneFailed
=
true
;
atLeastOneFailed
=
true
;
}
}
System
.
out
.
println
(
"Validating signature-enveloping-hmac-sha1-40.xml"
);
try
{
test_signature
(
"signature-enveloping-hmac-sha1-40.xml"
,
SKKS
,
false
);
System
.
out
.
println
(
"FAILED"
);
atLeastOneFailed
=
true
;
}
catch
(
XMLSignatureException
xse
)
{
System
.
out
.
println
(
xse
.
getMessage
());
System
.
out
.
println
(
"PASSED"
);
}
System
.
out
.
println
(
"Validating signature-enveloping-hmac-sha1-trunclen-0-attack.xml"
);
try
{
test_signature
(
"signature-enveloping-hmac-sha1-trunclen-0-attack.xml"
,
SKKS
,
false
);
System
.
out
.
println
(
"FAILED"
);
atLeastOneFailed
=
true
;
}
catch
(
XMLSignatureException
xse
)
{
System
.
out
.
println
(
xse
.
getMessage
());
System
.
out
.
println
(
"PASSED"
);
}
System
.
out
.
println
(
"Validating signature-enveloping-hmac-sha1-trunclen-8-attack.xml"
);
try
{
test_signature
(
"signature-enveloping-hmac-sha1-trunclen-8-attack.xml"
,
SKKS
,
false
);
System
.
out
.
println
(
"FAILED"
);
atLeastOneFailed
=
true
;
}
catch
(
XMLSignatureException
xse
)
{
System
.
out
.
println
(
xse
.
getMessage
());
System
.
out
.
println
(
"PASSED"
);
}
if
(
atLeastOneFailed
)
{
if
(
atLeastOneFailed
)
{
throw
new
Exception
throw
new
Exception
(
"At least one signature did not validate as expected"
);
(
"At least one signature did not validate as expected"
);
...
...
test/javax/xml/crypto/dsig/data/signature-enveloping-hmac-sha1-trunclen-0-attack.xml
0 → 100644
浏览文件 @
2ba224d3
<?xml version="1.0" encoding="UTF-8"?>
<Signature
xmlns=
"http://www.w3.org/2000/09/xmldsig#"
>
<SignedInfo>
<CanonicalizationMethod
Algorithm=
"http://www.w3.org/TR/2001/REC-xml-c14n-20010315"
/>
<SignatureMethod
Algorithm=
"http://www.w3.org/2000/09/xmldsig#hmac-sha1"
>
<HMACOutputLength>
0
</HMACOutputLength>
</SignatureMethod>
<Reference
URI=
"#object"
>
<DigestMethod
Algorithm=
"http://www.w3.org/2000/09/xmldsig#sha1"
/>
<DigestValue>
nz4GS0NbH2SrWlD/4fX313CoTzc=
</DigestValue>
</Reference>
</SignedInfo>
<SignatureValue>
</SignatureValue>
<Object
Id=
"object"
>
some other text
</Object>
</Signature>
test/javax/xml/crypto/dsig/data/signature-enveloping-hmac-sha1-trunclen-8-attack.xml
0 → 100644
浏览文件 @
2ba224d3
<?xml version="1.0" encoding="UTF-8"?>
<Signature
xmlns=
"http://www.w3.org/2000/09/xmldsig#"
>
<SignedInfo>
<CanonicalizationMethod
Algorithm=
"http://www.w3.org/TR/2001/REC-xml-c14n-20010315"
/>
<SignatureMethod
Algorithm=
"http://www.w3.org/2000/09/xmldsig#hmac-sha1"
>
<HMACOutputLength>
8
</HMACOutputLength>
</SignatureMethod>
<Reference
URI=
"#object"
>
<DigestMethod
Algorithm=
"http://www.w3.org/2000/09/xmldsig#sha1"
/>
<DigestValue>
nz4GS0NbH2SrWlD/4fX313CoTzc=
</DigestValue>
</Reference>
</SignedInfo>
<SignatureValue>
Qw==
</SignatureValue>
<Object
Id=
"object"
>
some other text
</Object>
</Signature>
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录