Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
openanolis
dragonwell8_jdk
提交
a77b812a
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看板
提交
a77b812a
编写于
1月 23, 2013
作者:
V
vinnie
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
8005408: KeyStore API enhancements
Reviewed-by: mullan
上级
b691337a
变更
8
展开全部
显示空白变更内容
内联
并排
Showing
8 changed file
with
1502 addition
and
167 deletion
+1502
-167
src/share/classes/java/security/KeyStore.java
src/share/classes/java/security/KeyStore.java
+169
-2
src/share/classes/java/security/PKCS12Attribute.java
src/share/classes/java/security/PKCS12Attribute.java
+285
-0
src/share/classes/sun/security/pkcs12/PKCS12KeyStore.java
src/share/classes/sun/security/pkcs12/PKCS12KeyStore.java
+710
-164
src/share/classes/sun/security/x509/AlgorithmId.java
src/share/classes/sun/security/x509/AlgorithmId.java
+14
-1
test/sun/security/pkcs12/StorePasswordTest.java
test/sun/security/pkcs12/StorePasswordTest.java
+92
-0
test/sun/security/pkcs12/StoreSecretKeyTest.java
test/sun/security/pkcs12/StoreSecretKeyTest.java
+86
-0
test/sun/security/pkcs12/StoreTrustedCertTest.java
test/sun/security/pkcs12/StoreTrustedCertTest.java
+117
-0
test/sun/security/pkcs12/trusted.pem
test/sun/security/pkcs12/trusted.pem
+29
-0
未找到文件。
src/share/classes/java/security/KeyStore.java
浏览文件 @
a77b812a
...
...
@@ -26,6 +26,7 @@
package
java.security
;
import
java.io.*
;
import
java.net.URI
;
import
java.security.cert.Certificate
;
import
java.security.cert.X509Certificate
;
import
java.security.cert.CertificateException
;
...
...
@@ -405,7 +406,44 @@ public class KeyStore {
*
* @since 1.5
*/
public
static
interface
Entry
{
}
public
static
interface
Entry
{
/**
* Retrieves the attributes associated with an entry.
* <p>
* The default implementation returns an empty {@code Set}.
*
* @return an unmodifiable {@code Set} of attributes, possibly empty
*
* @since 1.8
*/
public
default
Set
<
Attribute
>
getAttributes
()
{
return
Collections
.<
Attribute
>
emptySet
();
}
/**
* An attribute associated with a keystore entry.
* It comprises a name and one or more values.
*
* @since 1.8
*/
public
interface
Attribute
{
/**
* Returns the attribute's name.
*
* @return the attribute name
*/
public
String
getName
();
/**
* Returns the attribute's value.
* Multi-valued attributes encode their values as a single string.
*
* @return the attribute value
*/
public
String
getValue
();
}
}
/**
* A <code>KeyStore</code> entry that holds a <code>PrivateKey</code>
...
...
@@ -417,6 +455,7 @@ public class KeyStore {
private
final
PrivateKey
privKey
;
private
final
Certificate
[]
chain
;
private
final
Set
<
Attribute
>
attributes
;
/**
* Constructs a <code>PrivateKeyEntry</code> with a
...
...
@@ -443,7 +482,39 @@ public class KeyStore {
* in the end entity <code>Certificate</code> (at index 0)
*/
public
PrivateKeyEntry
(
PrivateKey
privateKey
,
Certificate
[]
chain
)
{
if
(
privateKey
==
null
||
chain
==
null
)
{
this
(
privateKey
,
chain
,
Collections
.<
Attribute
>
emptySet
());
}
/**
* Constructs a {@code PrivateKeyEntry} with a {@code PrivateKey} and
* corresponding certificate chain and associated entry attributes.
*
* <p> The specified {@code chain} and {@code attributes} are cloned
* before they are stored in the new {@code PrivateKeyEntry} object.
*
* @param privateKey the {@code PrivateKey}
* @param chain an array of {@code Certificate}s
* representing the certificate chain.
* The chain must be ordered and contain a
* {@code Certificate} at index 0
* corresponding to the private key.
* @param attributes the attributes
*
* @exception NullPointerException if {@code privateKey}, {@code chain}
* or {@code attributes} is {@code null}
* @exception IllegalArgumentException if the specified chain has a
* length of 0, if the specified chain does not contain
* {@code Certificate}s of the same type,
* or if the {@code PrivateKey} algorithm
* does not match the algorithm of the {@code PublicKey}
* in the end entity {@code Certificate} (at index 0)
*
* @since 1.8
*/
public
PrivateKeyEntry
(
PrivateKey
privateKey
,
Certificate
[]
chain
,
Set
<
Attribute
>
attributes
)
{
if
(
privateKey
==
null
||
chain
==
null
||
attributes
==
null
)
{
throw
new
NullPointerException
(
"invalid null input"
);
}
if
(
chain
.
length
==
0
)
{
...
...
@@ -478,6 +549,9 @@ public class KeyStore {
}
else
{
this
.
chain
=
clonedChain
;
}
this
.
attributes
=
Collections
.
unmodifiableSet
(
new
HashSet
<>(
attributes
));
}
/**
...
...
@@ -518,6 +592,19 @@ public class KeyStore {
return
chain
[
0
];
}
/**
* Retrieves the attributes associated with an entry.
* <p>
*
* @return an unmodifiable {@code Set} of attributes, possibly empty
*
* @since 1.8
*/
@Override
public
Set
<
Attribute
>
getAttributes
()
{
return
attributes
;
}
/**
* Returns a string representation of this PrivateKeyEntry.
* @return a string representation of this PrivateKeyEntry.
...
...
@@ -543,6 +630,7 @@ public class KeyStore {
public
static
final
class
SecretKeyEntry
implements
Entry
{
private
final
SecretKey
sKey
;
private
final
Set
<
Attribute
>
attributes
;
/**
* Constructs a <code>SecretKeyEntry</code> with a
...
...
@@ -558,6 +646,32 @@ public class KeyStore {
throw
new
NullPointerException
(
"invalid null input"
);
}
this
.
sKey
=
secretKey
;
this
.
attributes
=
Collections
.<
Attribute
>
emptySet
();
}
/**
* Constructs a {@code SecretKeyEntry} with a {@code SecretKey} and
* associated entry attributes.
*
* <p> The specified {@code attributes} is cloned before it is stored
* in the new {@code SecretKeyEntry} object.
*
* @param secretKey the {@code SecretKey}
* @param attributes the attributes
*
* @exception NullPointerException if {@code secretKey} or
* {@code attributes} is {@code null}
*
* @since 1.8
*/
public
SecretKeyEntry
(
SecretKey
secretKey
,
Set
<
Attribute
>
attributes
)
{
if
(
secretKey
==
null
||
attributes
==
null
)
{
throw
new
NullPointerException
(
"invalid null input"
);
}
this
.
sKey
=
secretKey
;
this
.
attributes
=
Collections
.
unmodifiableSet
(
new
HashSet
<>(
attributes
));
}
/**
...
...
@@ -569,6 +683,19 @@ public class KeyStore {
return
sKey
;
}
/**
* Retrieves the attributes associated with an entry.
* <p>
*
* @return an unmodifiable {@code Set} of attributes, possibly empty
*
* @since 1.8
*/
@Override
public
Set
<
Attribute
>
getAttributes
()
{
return
attributes
;
}
/**
* Returns a string representation of this SecretKeyEntry.
* @return a string representation of this SecretKeyEntry.
...
...
@@ -587,6 +714,7 @@ public class KeyStore {
public
static
final
class
TrustedCertificateEntry
implements
Entry
{
private
final
Certificate
cert
;
private
final
Set
<
Attribute
>
attributes
;
/**
* Constructs a <code>TrustedCertificateEntry</code> with a
...
...
@@ -602,6 +730,32 @@ public class KeyStore {
throw
new
NullPointerException
(
"invalid null input"
);
}
this
.
cert
=
trustedCert
;
this
.
attributes
=
Collections
.<
Attribute
>
emptySet
();
}
/**
* Constructs a {@code TrustedCertificateEntry} with a
* trusted {@code Certificate} and associated entry attributes.
*
* <p> The specified {@code attributes} is cloned before it is stored
* in the new {@code TrustedCertificateEntry} object.
*
* @param trustedCert the trusted {@code Certificate}
* @param attributes the attributes
*
* @exception NullPointerException if {@code trustedCert} or
* {@code attributes} is {@code null}
*
* @since 1.8
*/
public
TrustedCertificateEntry
(
Certificate
trustedCert
,
Set
<
Attribute
>
attributes
)
{
if
(
trustedCert
==
null
||
attributes
==
null
)
{
throw
new
NullPointerException
(
"invalid null input"
);
}
this
.
cert
=
trustedCert
;
this
.
attributes
=
Collections
.
unmodifiableSet
(
new
HashSet
<>(
attributes
));
}
/**
...
...
@@ -613,6 +767,19 @@ public class KeyStore {
return
cert
;
}
/**
* Retrieves the attributes associated with an entry.
* <p>
*
* @return an unmodifiable {@code Set} of attributes, possibly empty
*
* @since 1.8
*/
@Override
public
Set
<
Attribute
>
getAttributes
()
{
return
attributes
;
}
/**
* Returns a string representation of this TrustedCertificateEntry.
* @return a string representation of this TrustedCertificateEntry.
...
...
src/share/classes/java/security/PKCS12Attribute.java
0 → 100644
浏览文件 @
a77b812a
/*
* Copyright (c) 2013, Oracle and/or its affiliates. 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. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package
java.security
;
import
java.io.IOException
;
import
java.math.BigInteger
;
import
java.util.Arrays
;
import
java.util.regex.Pattern
;
import
sun.security.util.*
;
/**
* An attribute associated with a PKCS12 keystore entry.
* The attribute name is an ASN.1 Object Identifier and the attribute
* value is a set of ASN.1 types.
*
* @since 1.8
*/
public
final
class
PKCS12Attribute
implements
KeyStore
.
Entry
.
Attribute
{
private
static
final
Pattern
COLON_SEPARATED_HEX_PAIRS
=
Pattern
.
compile
(
"^[0-9a-fA-F]{2}(:[0-9a-fA-F]{2})+$"
);
private
String
name
;
private
String
value
;
private
byte
[]
encoded
;
private
int
hashValue
=
-
1
;
/**
* Constructs a PKCS12 attribute from its name and value.
* The name is an ASN.1 Object Identifier represented as a list of
* dot-separated integers.
* A string value is represented as the string itself.
* A binary value is represented as a string of colon-separated
* pairs of hexadecimal digits.
* Multi-valued attributes are represented as a comma-separated
* list of values, enclosed in square brackets. See
* {@link Arrays.toString}.
* <p>
* A string value will be DER-encoded as an ASN.1 UTF8String and a
* binary value will be DER-encoded as an ASN.1 Octet String.
*
* @param name the attribute's identifier
* @param value the attribute's value
*
* @exception NullPointerException if {@code name} or {@code value}
* is {@code null}
* @exception IllegalArgumentException if {@code name} or
* {@code value} is incorrectly formatted
*/
public
PKCS12Attribute
(
String
name
,
String
value
)
{
if
(
name
==
null
||
value
==
null
)
{
throw
new
NullPointerException
();
}
// Validate name
ObjectIdentifier
type
;
try
{
type
=
new
ObjectIdentifier
(
name
);
}
catch
(
IOException
e
)
{
throw
new
IllegalArgumentException
(
"Incorrect format: name"
,
e
);
}
this
.
name
=
name
;
// Validate value
int
length
=
value
.
length
();
String
[]
values
;
if
(
value
.
charAt
(
0
)
==
'['
&&
value
.
charAt
(
length
-
1
)
==
']'
)
{
values
=
value
.
substring
(
1
,
length
-
1
).
split
(
", "
);
}
else
{
values
=
new
String
[]{
value
};
}
this
.
value
=
value
;
try
{
this
.
encoded
=
encode
(
type
,
values
);
}
catch
(
IOException
e
)
{
throw
new
IllegalArgumentException
(
"Incorrect format: value"
,
e
);
}
}
/**
* Constructs a PKCS12 attribute from its ASN.1 DER encoding.
* The DER encoding is specified by the following ASN.1 definition:
* <pre>
*
* Attribute ::= SEQUENCE {
* type AttributeType,
* values SET OF AttributeValue
* }
* AttributeType ::= OBJECT IDENTIFIER
* AttributeValue ::= ANY defined by type
*
* </pre>
*
* @param encoded the attribute's ASN.1 DER encoding. It is cloned
* to prevent subsequent modificaion.
*
* @exception NullPointerException if {@code encoded} is
* {@code null}
* @exception IllegalArgumentException if {@code encoded} is
* incorrectly formatted
*/
public
PKCS12Attribute
(
byte
[]
encoded
)
{
if
(
encoded
==
null
)
{
throw
new
NullPointerException
();
}
this
.
encoded
=
encoded
.
clone
();
try
{
parse
(
encoded
);
}
catch
(
IOException
e
)
{
throw
new
IllegalArgumentException
(
"Incorrect format: encoded"
,
e
);
}
}
/**
* Returns the attribute's ASN.1 Object Identifier represented as a
* list of dot-separated integers.
*
* @return the attribute's identifier
*/
@Override
public
String
getName
()
{
return
name
;
}
/**
* Returns the attribute's ASN.1 DER-encoded value as a string.
* An ASN.1 DER-encoded value is returned in one of the following
* {@code String} formats:
* <ul>
* <li> the DER encoding of a basic ASN.1 type that has a natural
* string representation is returned as the string itself.
* Such types are currently limited to BOOLEAN, INTEGER,
* OBJECT IDENTIFIER, UTCTime, GeneralizedTime and the
* following six ASN.1 string types: UTF8String,
* PrintableString, T61String, IA5String, BMPString and
* GeneralString.
* <li> the DER encoding of any other ASN.1 type is not decoded but
* returned as a binary string of colon-separated pairs of
* hexadecimal digits.
* </ul>
* Multi-valued attributes are represented as a comma-separated
* list of values, enclosed in square brackets. See
* {@link Arrays.toString}.
*
* @return the attribute value's string encoding
*/
@Override
public
String
getValue
()
{
return
value
;
}
/**
* Returns the attribute's ASN.1 DER encoding.
*
* @return a clone of the attribute's DER encoding
*/
public
byte
[]
getEncoded
()
{
return
encoded
.
clone
();
}
/**
* Compares this {@code PKCS12Attribute} and a specified object for
* equality.
*
* @param obj the comparison object
*
* @return true if {@code obj} is a {@code PKCS12Attribute} and
* their DER encodings are equal.
*/
@Override
public
boolean
equals
(
Object
obj
)
{
if
(
this
==
obj
)
{
return
true
;
}
if
(!(
obj
instanceof
PKCS12Attribute
))
{
return
false
;
}
return
Arrays
.
equals
(
encoded
,
((
PKCS12Attribute
)
obj
).
getEncoded
());
}
/**
* Returns the hashcode for this {@code PKCS12Attribute}.
* The hash code is computed from its DER encoding.
*
* @return the hash code
*/
@Override
public
int
hashCode
()
{
if
(
hashValue
==
-
1
)
{
Arrays
.
hashCode
(
encoded
);
}
return
hashValue
;
}
/**
* Returns a string representation of this {@code PKCS12Attribute}.
*
* @return a name/value pair separated by an 'equals' symbol
*/
@Override
public
String
toString
()
{
return
(
name
+
"="
+
value
);
}
private
byte
[]
encode
(
ObjectIdentifier
type
,
String
[]
values
)
throws
IOException
{
DerOutputStream
attribute
=
new
DerOutputStream
();
attribute
.
putOID
(
type
);
DerOutputStream
attrContent
=
new
DerOutputStream
();
for
(
String
value
:
values
)
{
if
(
COLON_SEPARATED_HEX_PAIRS
.
matcher
(
value
).
matches
())
{
byte
[]
bytes
=
new
BigInteger
(
value
.
replace
(
":"
,
""
),
16
).
toByteArray
();
if
(
bytes
[
0
]
==
0
)
{
bytes
=
Arrays
.
copyOfRange
(
bytes
,
1
,
bytes
.
length
);
}
attrContent
.
putOctetString
(
bytes
);
}
else
{
attrContent
.
putUTF8String
(
value
);
}
}
attribute
.
write
(
DerValue
.
tag_Set
,
attrContent
);
DerOutputStream
attributeValue
=
new
DerOutputStream
();
attributeValue
.
write
(
DerValue
.
tag_Sequence
,
attribute
);
return
attributeValue
.
toByteArray
();
}
private
void
parse
(
byte
[]
encoded
)
throws
IOException
{
DerInputStream
attributeValue
=
new
DerInputStream
(
encoded
);
DerValue
[]
attrSeq
=
attributeValue
.
getSequence
(
2
);
ObjectIdentifier
type
=
attrSeq
[
0
].
getOID
();
DerInputStream
attrContent
=
new
DerInputStream
(
attrSeq
[
1
].
toByteArray
());
DerValue
[]
attrValueSet
=
attrContent
.
getSet
(
1
);
String
[]
values
=
new
String
[
attrValueSet
.
length
];
String
printableString
;
for
(
int
i
=
0
;
i
<
attrValueSet
.
length
;
i
++)
{
if
(
attrValueSet
[
i
].
tag
==
DerValue
.
tag_OctetString
)
{
values
[
i
]
=
Debug
.
toString
(
attrValueSet
[
i
].
getOctetString
());
}
else
if
((
printableString
=
attrValueSet
[
i
].
getAsString
())
!=
null
)
{
values
[
i
]
=
printableString
;
}
else
if
(
attrValueSet
[
i
].
tag
==
DerValue
.
tag_ObjectId
)
{
values
[
i
]
=
attrValueSet
[
i
].
getOID
().
toString
();
}
else
if
(
attrValueSet
[
i
].
tag
==
DerValue
.
tag_GeneralizedTime
)
{
values
[
i
]
=
attrValueSet
[
i
].
getGeneralizedTime
().
toString
();
}
else
if
(
attrValueSet
[
i
].
tag
==
DerValue
.
tag_UtcTime
)
{
values
[
i
]
=
attrValueSet
[
i
].
getUTCTime
().
toString
();
}
else
if
(
attrValueSet
[
i
].
tag
==
DerValue
.
tag_Integer
)
{
values
[
i
]
=
attrValueSet
[
i
].
getBigInteger
().
toString
();
}
else
if
(
attrValueSet
[
i
].
tag
==
DerValue
.
tag_Boolean
)
{
values
[
i
]
=
String
.
valueOf
(
attrValueSet
[
i
].
getBoolean
());
}
else
{
values
[
i
]
=
Debug
.
toString
(
attrValueSet
[
i
].
getDataBytes
());
}
}
this
.
name
=
type
.
toString
();
this
.
value
=
values
.
length
==
1
?
values
[
0
]
:
Arrays
.
toString
(
values
);
}
}
src/share/classes/sun/security/pkcs12/PKCS12KeyStore.java
浏览文件 @
a77b812a
此差异已折叠。
点击以展开。
src/share/classes/sun/security/x509/AlgorithmId.java
浏览文件 @
a77b812a
/*
* Copyright (c) 1996, 201
2
, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1996, 201
3
, Oracle and/or its affiliates. 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
...
...
@@ -502,6 +502,11 @@ public class AlgorithmId implements Serializable, DerEncoder {
return
AlgorithmId
.
ECDH_oid
;
}
// Secret key algorithms
if
(
name
.
equalsIgnoreCase
(
"AES"
))
{
return
AlgorithmId
.
AES_oid
;
}
// Common signature types
if
(
name
.
equalsIgnoreCase
(
"MD5withRSA"
)
||
name
.
equalsIgnoreCase
(
"MD5/RSA"
))
{
...
...
@@ -660,6 +665,12 @@ public class AlgorithmId implements Serializable, DerEncoder {
public
static
final
ObjectIdentifier
RSA_oid
;
public
static
final
ObjectIdentifier
RSAEncryption_oid
;
/*
* COMMON SECRET KEY TYPES
*/
public
static
final
ObjectIdentifier
AES_oid
=
oid
(
2
,
16
,
840
,
1
,
101
,
3
,
4
,
1
);
/*
* COMMON SIGNATURE ALGORITHMS
*/
...
...
@@ -893,6 +904,8 @@ public class AlgorithmId implements Serializable, DerEncoder {
nameTable
.
put
(
EC_oid
,
"EC"
);
nameTable
.
put
(
ECDH_oid
,
"ECDH"
);
nameTable
.
put
(
AES_oid
,
"AES"
);
nameTable
.
put
(
sha1WithECDSA_oid
,
"SHA1withECDSA"
);
nameTable
.
put
(
sha224WithECDSA_oid
,
"SHA224withECDSA"
);
nameTable
.
put
(
sha256WithECDSA_oid
,
"SHA256withECDSA"
);
...
...
test/sun/security/pkcs12/StorePasswordTest.java
0 → 100644
浏览文件 @
a77b812a
/*
* Copyright (c) 2013, Oracle and/or its affiliates. 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/*
* @test
* @bug 8005408
* @summary KeyStore API enhancements
*/
import
java.io.*
;
import
java.security.*
;
import
java.util.*
;
import
javax.crypto.*
;
import
javax.crypto.spec.*
;
import
java.security.spec.InvalidKeySpecException
;
// Store a password in a keystore and retrieve it again.
public
class
StorePasswordTest
{
private
final
static
String
DIR
=
System
.
getProperty
(
"test.src"
,
"."
);
private
static
final
char
[]
PASSWORD
=
"passphrase"
.
toCharArray
();
private
static
final
String
KEYSTORE
=
"pwdstore.p12"
;
private
static
final
String
ALIAS
=
"my password"
;
private
static
final
String
USER_PASSWORD
=
"hello1"
;
public
static
void
main
(
String
[]
args
)
throws
Exception
{
new
File
(
KEYSTORE
).
delete
();
try
{
KeyStore
keystore
=
KeyStore
.
getInstance
(
"PKCS12"
);
keystore
.
load
(
null
,
null
);
// Set entry
keystore
.
setEntry
(
ALIAS
,
new
KeyStore
.
SecretKeyEntry
(
convertPassword
(
USER_PASSWORD
)),
new
KeyStore
.
PasswordProtection
(
PASSWORD
));
System
.
out
.
println
(
"Storing keystore to: "
+
KEYSTORE
);
keystore
.
store
(
new
FileOutputStream
(
KEYSTORE
),
PASSWORD
);
System
.
out
.
println
(
"Loading keystore from: "
+
KEYSTORE
);
keystore
.
load
(
new
FileInputStream
(
KEYSTORE
),
PASSWORD
);
System
.
out
.
println
(
"Loaded keystore with "
+
keystore
.
size
()
+
" entries"
);
KeyStore
.
Entry
entry
=
keystore
.
getEntry
(
ALIAS
,
new
KeyStore
.
PasswordProtection
(
PASSWORD
));
System
.
out
.
println
(
"Retrieved entry: "
+
entry
);
SecretKey
key
=
(
SecretKey
)
keystore
.
getKey
(
ALIAS
,
PASSWORD
);
SecretKeyFactory
factory
=
SecretKeyFactory
.
getInstance
(
key
.
getAlgorithm
());
PBEKeySpec
keySpec
=
(
PBEKeySpec
)
factory
.
getKeySpec
(
key
,
PBEKeySpec
.
class
);
char
[]
pwd
=
keySpec
.
getPassword
();
System
.
out
.
println
(
"Recovered credential: "
+
new
String
(
pwd
));
if
(!
Arrays
.
equals
(
USER_PASSWORD
.
toCharArray
(),
pwd
))
{
throw
new
Exception
(
"Failed to recover the stored password"
);
}
}
finally
{
new
File
(
KEYSTORE
).
delete
();
}
}
private
static
SecretKey
convertPassword
(
String
password
)
throws
NoSuchAlgorithmException
,
InvalidKeySpecException
{
SecretKeyFactory
factory
=
SecretKeyFactory
.
getInstance
(
"PBE"
);
return
factory
.
generateSecret
(
new
PBEKeySpec
(
password
.
toCharArray
()));
}
}
test/sun/security/pkcs12/StoreSecretKeyTest.java
0 → 100644
浏览文件 @
a77b812a
/*
* Copyright (c) 2013, Oracle and/or its affiliates. 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/*
* @test
* @bug 8005408
* @summary KeyStore API enhancements
*/
import
java.io.*
;
import
java.security.*
;
import
java.util.*
;
import
javax.crypto.*
;
import
javax.crypto.spec.*
;
// Store a secret key in a keystore and retrieve it again.
public
class
StoreSecretKeyTest
{
private
final
static
String
DIR
=
System
.
getProperty
(
"test.src"
,
"."
);
private
static
final
char
[]
PASSWORD
=
"passphrase"
.
toCharArray
();
private
static
final
String
KEYSTORE
=
"keystore.p12"
;
private
static
final
String
ALIAS
=
"my secret key"
;
public
static
void
main
(
String
[]
args
)
throws
Exception
{
new
File
(
KEYSTORE
).
delete
();
try
{
KeyStore
keystore
=
KeyStore
.
getInstance
(
"PKCS12"
);
keystore
.
load
(
null
,
null
);
// Set entry
keystore
.
setEntry
(
ALIAS
,
new
KeyStore
.
SecretKeyEntry
(
generateSecretKey
(
"AES"
,
128
)),
new
KeyStore
.
PasswordProtection
(
PASSWORD
));
System
.
out
.
println
(
"Storing keystore to: "
+
KEYSTORE
);
keystore
.
store
(
new
FileOutputStream
(
KEYSTORE
),
PASSWORD
);
System
.
out
.
println
(
"Loading keystore from: "
+
KEYSTORE
);
keystore
.
load
(
new
FileInputStream
(
KEYSTORE
),
PASSWORD
);
System
.
out
.
println
(
"Loaded keystore with "
+
keystore
.
size
()
+
" entries"
);
KeyStore
.
Entry
entry
=
keystore
.
getEntry
(
ALIAS
,
new
KeyStore
.
PasswordProtection
(
PASSWORD
));
System
.
out
.
println
(
"Retrieved entry: "
+
entry
);
if
(
entry
instanceof
KeyStore
.
SecretKeyEntry
)
{
System
.
out
.
println
(
"Retrieved secret key entry: "
+
entry
);
}
else
{
throw
new
Exception
(
"Not a secret key entry"
);
}
}
finally
{
new
File
(
KEYSTORE
).
delete
();
}
}
private
static
SecretKey
generateSecretKey
(
String
algorithm
,
int
size
)
throws
NoSuchAlgorithmException
{
KeyGenerator
generator
=
KeyGenerator
.
getInstance
(
algorithm
);
generator
.
init
(
size
);
return
generator
.
generateKey
();
}
}
test/sun/security/pkcs12/StoreTrustedCertTest.java
0 → 100644
浏览文件 @
a77b812a
/*
* Copyright (c) 2013, Oracle and/or its affiliates. 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/*
* @test
* @bug 8005408
* @summary KeyStore API enhancements
*/
import
java.io.*
;
import
java.security.*
;
import
java.security.cert.*
;
import
java.util.*
;
import
java.security.cert.Certificate
;
import
javax.crypto.*
;
import
javax.crypto.spec.*
;
// Store a trusted certificate in a keystore and retrieve it again.
public
class
StoreTrustedCertTest
{
private
final
static
String
DIR
=
System
.
getProperty
(
"test.src"
,
"."
);
private
static
final
char
[]
PASSWORD
=
"passphrase"
.
toCharArray
();
private
static
final
String
KEYSTORE
=
"truststore.p12"
;
private
static
final
String
CERT
=
DIR
+
"/trusted.pem"
;
private
static
final
String
ALIAS
=
"my trustedcert"
;
private
static
final
String
ALIAS2
=
"my trustedcert with attributes"
;
public
static
void
main
(
String
[]
args
)
throws
Exception
{
new
File
(
KEYSTORE
).
delete
();
try
{
KeyStore
keystore
=
KeyStore
.
getInstance
(
"PKCS12"
);
keystore
.
load
(
null
,
null
);
Certificate
cert
=
loadCertificate
(
CERT
);
Set
<
KeyStore
.
Entry
.
Attribute
>
attributes
=
new
HashSet
<>();
attributes
.
add
(
new
PKCS12Attribute
(
"1.3.5.7.9"
,
"that's odd"
));
attributes
.
add
(
new
PKCS12Attribute
(
"2.4.6.8.10"
,
"that's even"
));
// Set trusted certificate entry
keystore
.
setEntry
(
ALIAS
,
new
KeyStore
.
TrustedCertificateEntry
(
cert
),
null
);
// Set trusted certificate entry with attributes
keystore
.
setEntry
(
ALIAS2
,
new
KeyStore
.
TrustedCertificateEntry
(
cert
,
attributes
),
null
);
System
.
out
.
println
(
"Storing keystore to: "
+
KEYSTORE
);
keystore
.
store
(
new
FileOutputStream
(
KEYSTORE
),
PASSWORD
);
System
.
out
.
println
(
"Loading keystore from: "
+
KEYSTORE
);
keystore
.
load
(
new
FileInputStream
(
KEYSTORE
),
PASSWORD
);
System
.
out
.
println
(
"Loaded keystore with "
+
keystore
.
size
()
+
" entries"
);
KeyStore
.
Entry
entry
=
keystore
.
getEntry
(
ALIAS
,
null
);
if
(
entry
instanceof
KeyStore
.
TrustedCertificateEntry
)
{
System
.
out
.
println
(
"Retrieved trusted certificate entry: "
+
entry
);
}
else
{
throw
new
Exception
(
"Not a trusted certificate entry"
);
}
System
.
out
.
println
();
entry
=
keystore
.
getEntry
(
ALIAS2
,
null
);
if
(
entry
instanceof
KeyStore
.
TrustedCertificateEntry
)
{
KeyStore
.
TrustedCertificateEntry
trustedEntry
=
(
KeyStore
.
TrustedCertificateEntry
)
entry
;
Set
<
KeyStore
.
Entry
.
Attribute
>
entryAttributes
=
trustedEntry
.
getAttributes
();
if
(
entryAttributes
.
containsAll
(
attributes
))
{
System
.
out
.
println
(
"Retrieved trusted certificate entry "
+
"with attributes: "
+
entry
);
}
else
{
throw
new
Exception
(
"Failed to retrieve entry attributes"
);
}
}
else
{
throw
new
Exception
(
"Not a trusted certificate entry"
);
}
}
finally
{
new
File
(
KEYSTORE
).
delete
();
}
}
private
static
Certificate
loadCertificate
(
String
certFile
)
throws
Exception
{
X509Certificate
cert
=
null
;
try
(
FileInputStream
certStream
=
new
FileInputStream
(
certFile
))
{
CertificateFactory
factory
=
CertificateFactory
.
getInstance
(
"X.509"
);
return
factory
.
generateCertificate
(
certStream
);
}
}
}
test/sun/security/pkcs12/trusted.pem
0 → 100644
浏览文件 @
a77b812a
-----BEGIN CERTIFICATE-----
MIIF5DCCBMygAwIBAgIQGVCD3zqdD1ZMZZ/zLAPnQzANBgkqhkiG9w0BAQUFADCBvDELMAkGA1UE
BhMCVVMxFzAVBgNVBAoTDlZlcmlTaWduLCBJbmMuMR8wHQYDVQQLExZWZXJpU2lnbiBUcnVzdCBO
ZXR3b3JrMTswOQYDVQQLEzJUZXJtcyBvZiB1c2UgYXQgaHR0cHM6Ly93d3cudmVyaXNpZ24uY29t
L3JwYSAoYykxMDE2MDQGA1UEAxMtVmVyaVNpZ24gQ2xhc3MgMyBJbnRlcm5hdGlvbmFsIFNlcnZl
ciBDQSAtIEczMB4XDTEyMDcxMDAwMDAwMFoXDTEzMDczMTIzNTk1OVowgbgxCzAJBgNVBAYTAlVT
MRMwEQYDVQQIEwpDYWxpZm9ybmlhMRcwFQYDVQQHFA5SZWR3b29kIFNob3JlczEbMBkGA1UEChQS
T3JhY2xlIENvcnBvcmF0aW9uMRIwEAYDVQQLFAlHbG9iYWwgSVQxMzAxBgNVBAsUKlRlcm1zIG9m
IHVzZSBhdCB3d3cudmVyaXNpZ24uY29tL3JwYSAoYykwNTEVMBMGA1UEAxQMKi5vcmFjbGUuY29t
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAz/dOCGrWzPj62q0ZkF59Oj9Fli4wHAuX
U4/S0yBXF8j6K7TKWFTQkGZt3+08KUhmLm1CE1DbbyRJT292YNXYXunNaKdABob8kaBO/NESUOEJ
0SZh7fd0xCSJAAPiwOMrM5jLeb/dEpU6nP74Afrhu5ffvKdcvTRGguj9H2oVsisTK8Z1HsiiwcJG
JXcrjvdCZoPU4FHvK03XZPAqPHKNSaJOrux6kRIWYjQMlmL+qDOb0nNHa6gBdi+VqqJHJHeAM677
dcUd0jn2m2OWtUnrM3MJZQof7/z27RTdX5J8np0ChkUgm63biDgRZO7uZP0DARQ0I6lZMlrarT8/
sct3twIDAQABo4IB4jCCAd4wFwYDVR0RBBAwDoIMKi5vcmFjbGUuY29tMAkGA1UdEwQCMAAwCwYD
VR0PBAQDAgWgMEQGA1UdIAQ9MDswOQYLYIZIAYb4RQEHFwMwKjAoBggrBgEFBQcCARYcaHR0cHM6
Ly93d3cudmVyaXNpZ24uY29tL3JwYTAdBgNVHSUEFjAUBggrBgEFBQcDAQYIKwYBBQUHAwIwbgYI
KwYBBQUHAQwEYjBgoV6gXDBaMFgwVhYJaW1hZ2UvZ2lmMCEwHzAHBgUrDgMCGgQUS2u5KJYGDLvQ
UjibKaxLB4shBRgwJhYkaHR0cDovL2xvZ28udmVyaXNpZ24uY29tL3ZzbG9nbzEuZ2lmMHIGCCsG
AQUFBwEBBGYwZDAkBggrBgEFBQcwAYYYaHR0cDovL29jc3AudmVyaXNpZ24uY29tMDwGCCsGAQUF
BzAChjBodHRwOi8vc3ZyaW50bC1nMy1haWEudmVyaXNpZ24uY29tL1NWUkludGxHMy5jZXIwQQYD
VR0fBDowODA2oDSgMoYwaHR0cDovL3N2cmludGwtZzMtY3JsLnZlcmlzaWduLmNvbS9TVlJJbnRs
RzMuY3JsMB8GA1UdIwQYMBaAFNebfNgioBX33a1fzimbWMO8RgC1MA0GCSqGSIb3DQEBBQUAA4IB
AQAITRBlEo+qXLwCL53Db2BGnhDgnSomjne8aCmU7Yt4Kp91tzJdhNuaC/wwDuzD2dPJqzemae3s
wKiOXrmDQZDj9NNTdkrXHnCvDR4TpOynWe3zBa0bwKnV2cIRKcv482yV53u0kALyFZbagYPwOOz3
YJA/2SqdcDn9Ztc/ABQ1SkyXyA5j4LJdf2g7BtYrFxjy0RG6We2iM781WSB/9MCNKyHgiwd3KpLf
urdSKLzy1elNAyt1P3UHwBIIvZ6sJIr/eeELc54Lxt6PtQCXx8qwxYTYXWPXbLgKBHdebgrmAbPK
TfD69wysvjk6vwSHjmvaqB4R4WRcgkuT+1gxx+ve
-----END CERTIFICATE-----
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录