本文整理汇总了Java中java.security.KeyStore.isKeyEntry方法的典型用法代码示例。如果您正苦于以下问题:Java KeyStore.isKeyEntry方法的具体用法?Java KeyStore.isKeyEntry怎么用?Java KeyStore.isKeyEntry使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类java.security.KeyStore
的用法示例。
在下文中一共展示了KeyStore.isKeyEntry方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: execute0
import java.security.KeyStore; //导入方法依赖的package包/类
@Override
protected Object execute0() throws Exception {
KeyStore ks = getKeyStore();
String keyname = null;
Enumeration<String> aliases = ks.aliases();
while (aliases.hasMoreElements()) {
String alias = aliases.nextElement();
if (ks.isKeyEntry(alias)) {
keyname = alias;
break;
}
}
if (keyname == null) {
throw new CmdFailure("could not find private key");
}
X509Certificate cert = (X509Certificate) ks.getCertificate(keyname);
saveVerbose("saved certificate to file", new File(outFile), cert.getEncoded());
return null;
}
示例2: findAlias
import java.security.KeyStore; //导入方法依赖的package包/类
private static String findAlias(KeyStore ks) throws KeyStoreException {
Enumeration<String> e = ks.aliases();
while(e.hasMoreElements()) {
String entry = e.nextElement();
if (ks.isKeyEntry(entry)) {
return entry;
}
}
throw new KeyStoreException("Cannot find a private key entry");
}
示例3: getKeyManagers
import java.security.KeyStore; //导入方法依赖的package包/类
/**
* Gets the initialized key managers.
*/
protected KeyManager[] getKeyManagers(String keystoreType,
String keystoreProvider,
String algorithm,
String keyAlias)
throws Exception {
KeyManager[] kms = null;
String keystorePass = getKeystorePassword();
KeyStore ks = getKeystore(keystoreType, keystoreProvider, keystorePass);
if (keyAlias != null && !ks.isKeyEntry(keyAlias)) {
throw new IOException(
sm.getString("jsse.alias_no_key_entry", keyAlias));
}
KeyManagerFactory kmf = KeyManagerFactory.getInstance(algorithm);
String keyPass = endpoint.getKeyPass();
if (keyPass == null) {
keyPass = keystorePass;
}
kmf.init(ks, keyPass.toCharArray());
kms = kmf.getKeyManagers();
if (keyAlias != null) {
String alias = keyAlias;
if (JSSESocketFactory.defaultKeystoreType.equals(keystoreType)) {
alias = alias.toLowerCase(Locale.ENGLISH);
}
for(int i=0; i<kms.length; i++) {
kms[i] = new JSSEKeyManager((X509KeyManager)kms[i], alias);
}
}
return kms;
}
示例4: SoftTokenMacContentSignerBuilder
import java.security.KeyStore; //导入方法依赖的package包/类
public SoftTokenMacContentSignerBuilder(String keystoreType, InputStream keystoreStream,
char[] keystorePassword, String keyname, char[] keyPassword)
throws XiSecurityException {
if (!"JCEKS".equalsIgnoreCase(keystoreType)) {
throw new IllegalArgumentException("unsupported keystore type: " + keystoreType);
}
ParamUtil.requireNonNull("keystoreStream", keystoreStream);
ParamUtil.requireNonNull("keystorePassword", keystorePassword);
ParamUtil.requireNonNull("keyPassword", keyPassword);
try {
KeyStore ks = KeyUtil.getKeyStore(keystoreType);
ks.load(keystoreStream, keystorePassword);
String tmpKeyname = keyname;
if (tmpKeyname == null) {
Enumeration<String> aliases = ks.aliases();
while (aliases.hasMoreElements()) {
String alias = aliases.nextElement();
if (ks.isKeyEntry(alias)) {
tmpKeyname = alias;
break;
}
}
} else {
if (!ks.isKeyEntry(tmpKeyname)) {
throw new XiSecurityException("unknown key named " + tmpKeyname);
}
}
this.key = (SecretKey) ks.getKey(tmpKeyname, keyPassword);
} catch (KeyStoreException | NoSuchProviderException | NoSuchAlgorithmException
| CertificateException | IOException | UnrecoverableKeyException
| ClassCastException ex) {
throw new XiSecurityException(ex.getMessage(), ex);
}
}
示例5: execute0
import java.security.KeyStore; //导入方法依赖的package包/类
@Override
protected Object execute0() throws Exception {
KeyStore ks = getKeyStore();
char[] pwd = getPassword();
X509Certificate newCert = X509Util.parseCert(certFile);
assertMatch(newCert, new String(pwd));
String keyname = null;
Enumeration<String> aliases = ks.aliases();
while (aliases.hasMoreElements()) {
String alias = aliases.nextElement();
if (ks.isKeyEntry(alias)) {
keyname = alias;
break;
}
}
if (keyname == null) {
throw new XiSecurityException("could not find private key");
}
Key key = ks.getKey(keyname, pwd);
Set<X509Certificate> caCerts = new HashSet<>();
if (isNotEmpty(caCertFiles)) {
for (String caCertFile : caCertFiles) {
caCerts.add(X509Util.parseCert(caCertFile));
}
}
X509Certificate[] certChain = X509Util.buildCertPath(newCert, caCerts);
ks.setKeyEntry(keyname, key, pwd, certChain);
try (FileOutputStream out = new FileOutputStream(p12File)) {
ks.store(out, pwd);
println("updated certificate");
return null;
}
}
示例6: execute0
import java.security.KeyStore; //导入方法依赖的package包/类
@Override
protected Object execute0() throws Exception {
File realInFile = new File(IoUtil.expandFilepath(inFile));
File realOutFile = new File(IoUtil.expandFilepath(outFile));
if (CompareUtil.equalsObject(realInFile, realOutFile)) {
throw new IllegalCmdParamException("in and out cannot be the same");
}
KeyStore inKs = KeyStore.getInstance(inType);
KeyStore outKs = KeyStore.getInstance(outType);
outKs.load(null);
char[] inPassword = readPasswordIfNotSet("password of the source keystore", inPwd);
FileInputStream inStream = new FileInputStream(realInFile);
try {
inKs.load(inStream, inPassword);
} finally {
inStream.close();
}
char[] outPassword = readPasswordIfNotSet("password of the destination keystore", outPwd);
Enumeration<String> aliases = inKs.aliases();
while (aliases.hasMoreElements()) {
String alias = aliases.nextElement();
if (inKs.isKeyEntry(alias)) {
Certificate[] certs = inKs.getCertificateChain(alias);
Key key = inKs.getKey(alias, inPassword);
outKs.setKeyEntry(alias, key, outPassword, certs);
} else {
Certificate cert = inKs.getCertificate(alias);
outKs.setCertificateEntry(alias, cert);
}
}
ByteArrayOutputStream bout = new ByteArrayOutputStream(4096);
outKs.store(bout, outPassword);
saveVerbose("saved destination keystore to file", realOutFile, bout.toByteArray());
return null;
}
示例7: getKeyManagers
import java.security.KeyStore; //导入方法依赖的package包/类
/**
* Gets the initialized key managers.
*/
protected KeyManager[] getKeyManagers(String keystoreType,
String keystoreProvider,
String algorithm,
String keyAlias)
throws Exception {
KeyManager[] kms = null;
KeyStore ks = getKeystore(keystoreType, keystoreProvider, listener.getSslKeyStorePass());
if (keyAlias != null && !ks.isKeyEntry(keyAlias)) {
throw new IOException(sm.getString("jsse.alias_no_key_entry", keyAlias));
}
KeyManagerFactory kmf = KeyManagerFactory.getInstance(algorithm);
kmf.init(ks, listener.getSslKeyStorePass().toCharArray());
kms = kmf.getKeyManagers();
if (keyAlias != null) {
if ("JKS".equals(keystoreType)) {
keyAlias = keyAlias.toLowerCase();
}
for(int i=0; i<kms.length; i++) {
kms[i] = new JSSEKeyManager((X509KeyManager)kms[i], keyAlias);
}
}
return kms;
}
示例8: sslContextFromKeystore
import java.security.KeyStore; //导入方法依赖的package包/类
static SSLContext sslContextFromKeystore(String resource, char[] secret) {
try {
KeyStore ks = KeyStore.getInstance("JKS");
ks.load(new FileInputStream(resource), secret);
final KeyManagerFactory kmf;
kmf = KeyManagerFactory.getInstance("SunX509");
kmf.init(ks, secret);
TrustManagerFactory tmf = TrustManagerFactory.getInstance("SunX509");
tmf.init(ks);
SSLContext sslContext = SSLContext.getInstance("TLSv1.2");
sslContext.init(kmf.getKeyManagers(), tmf.getTrustManagers(), null);
//print all certificates subject
Enumeration<String> aliases = ks.aliases();
while (aliases.hasMoreElements()) {
String alias = aliases.nextElement();
String certCN = ((X509Certificate) ks.getCertificate(alias)).getSubjectDN().toString();
if (ks.isKeyEntry(alias)) {
LOGGER.info("Using certificate CN: " + certCN);
} else {
LOGGER.info("Using trusted certificate CN: " + certCN);
}
}
return sslContext;
} catch (Exception e) {
throw new RuntimeException(e);
}
}
示例9: run
import java.security.KeyStore; //导入方法依赖的package包/类
private void run(KeyStore inputKeyStore, KeyStore outputKeyStore,
String inKeyPass, String outKeyPass) throws Exception {
Enumeration<String> e = inputKeyStore.aliases();
String alias;
while (e.hasMoreElements()) {
alias = e.nextElement();
Certificate[] certs = inputKeyStore.getCertificateChain(alias);
boolean isCertEntry = inputKeyStore.isCertificateEntry(alias);
// Test KeyStore only contain key pair entries.
if (isCertEntry == true) {
throw new RuntimeException(
"inputKeystore should not be certEntry because test"
+ " keystore only contain key pair entries"
+ " for alias:" + alias);
}
boolean isKeyEntry = inputKeyStore.isKeyEntry(alias);
Key key = null;
if (isKeyEntry) {
key = inputKeyStore.getKey(alias, inKeyPass.toCharArray());
} else {
throw new RuntimeException("Entry type unknown for alias:"
+ alias);
}
outputKeyStore.setKeyEntry(alias, key, outKeyPass.toCharArray(),
certs);
}
}
示例10: getKeyManagers
import java.security.KeyStore; //导入方法依赖的package包/类
/**
* Gets the initialized key managers.
*/
protected KeyManager[] getKeyManagers(String keystoreType, String keystoreProvider, String algorithm,
String keyAlias) throws Exception {
KeyManager[] kms = null;
String keystorePass = getKeystorePassword();
KeyStore ks = getKeystore(keystoreType, keystoreProvider, keystorePass);
if (keyAlias != null && !ks.isKeyEntry(keyAlias)) {
throw new IOException(sm.getString("jsse.alias_no_key_entry", keyAlias));
}
KeyManagerFactory kmf = KeyManagerFactory.getInstance(algorithm);
String keyPass = endpoint.getKeyPass();
if (keyPass == null) {
keyPass = keystorePass;
}
kmf.init(ks, keyPass.toCharArray());
kms = kmf.getKeyManagers();
if (keyAlias != null) {
String alias = keyAlias;
if (JSSESocketFactory.defaultKeystoreType.equals(keystoreType)) {
alias = alias.toLowerCase(Locale.ENGLISH);
}
for (int i = 0; i < kms.length; i++) {
kms[i] = new JSSEKeyManager((X509KeyManager) kms[i], alias);
}
}
return kms;
}
示例11: getKeyAlias
import java.security.KeyStore; //导入方法依赖的package包/类
private static String getKeyAlias(SSLConfig config,KeyStore ks)throws Exception{
String keyAlias = config.getKeyAlias();
if(keyAlias != null){
if(!ks.isKeyEntry(keyAlias)){
throw new IllegalArgumentException(keyAlias + " is not a key entry.");
}
}else{
keyAlias = getFirstKeyEntryAlias(ks);
if(keyAlias == null){
throw new IllegalArgumentException("There is no key entry in key store " + config.getKeystoreFile());
}
}
return keyAlias;
}
示例12: getFirstKeyEntryAlias
import java.security.KeyStore; //导入方法依赖的package包/类
private static String getFirstKeyEntryAlias(KeyStore ks)throws Exception{
Enumeration<String> e = ks.aliases();
while(e.hasMoreElements()){
String alias = e.nextElement();
if(ks.isKeyEntry(alias)){
return alias;
}
}
return null;
}
示例13: createKeyStoreFromPemKey
import java.security.KeyStore; //导入方法依赖的package包/类
private static void createKeyStoreFromPemKey(
String clientCert,
String clientPass,
KeyStore store ) throws Exception {
try {
// Load CA Chain file
// CertificateFactory cf = CertificateFactory.getInstance("X.509");
// X509Certificate cert = (X509Certificate) cf.generateCertificate(new FileInputStream(caCert));
store.load(null);
// Load client's public and private keys from PKCS12 certificate
KeyStore inputKeyStore = KeyStore.getInstance("PKCS12");
FileInputStream fis = new FileInputStream(clientCert);
char[] nPassword = null;
if ( (clientPass == null) || "".equals(clientPass.trim())) {
nPassword = null;
} else {
nPassword = clientPass.toCharArray();
}
inputKeyStore.load(fis, nPassword);
fis.close();
store.load(null, ( (clientPass != null)
? clientPass.toCharArray()
: null));
Enumeration<String> en = inputKeyStore.aliases();
while (en.hasMoreElements()) { // we are reading just one certificate.
String keyAlias = en.nextElement();
if (inputKeyStore.isKeyEntry(keyAlias)) {
Key key = inputKeyStore.getKey(keyAlias, nPassword);
Certificate[] certChain = inputKeyStore.getCertificateChain(keyAlias);
store.setKeyEntry("outkey",
key,
( (clientPass != null)
? clientPass.toCharArray()
: null),
certChain);
}
}
} catch (Exception e) {
throw new RuntimeException("Error creating keystore from Pem key", e);
}
}
示例14: execute0
import java.security.KeyStore; //导入方法依赖的package包/类
@Override
protected Object execute0() throws Exception {
long p11KeyType;
if ("AES".equalsIgnoreCase(keyType)) {
p11KeyType = PKCS11Constants.CKK_AES;
} else if ("DES3".equalsIgnoreCase(keyType)) {
p11KeyType = PKCS11Constants.CKK_DES3;
} else if ("GENERIC".equalsIgnoreCase(keyType)) {
p11KeyType = PKCS11Constants.CKK_GENERIC_SECRET;
} else {
throw new IllegalCmdParamException("invalid keyType " + keyType);
}
KeyStore ks = KeyStore.getInstance("JCEKS");
InputStream ksStream = new FileInputStream(IoUtil.expandFilepath(keyOutFile));
char[] pwd = getPassword();
try {
ks.load(ksStream, pwd);
} finally {
ksStream.close();
}
byte[] keyValue = null;
Enumeration<String> aliases = ks.aliases();
while (aliases.hasMoreElements()) {
String alias = aliases.nextElement();
if (!ks.isKeyEntry(alias)) {
continue;
}
Key key = ks.getKey(alias, pwd);
if (key instanceof SecretKey) {
keyValue = ((SecretKey) key).getEncoded();
break;
}
}
if (keyValue == null) {
throw new IllegalCmdParamException("keystore does not contain secret key");
}
P11Slot slot = getSlot();
P11ObjectIdentifier objId = slot.createSecretKey(p11KeyType, keyValue, label,
getControl());
finalize("Create Secret Key", objId);
return null;
}
示例15: runTest
import java.security.KeyStore; //导入方法依赖的package包/类
public void runTest(Provider p) throws Exception {
try (FileOutputStream fos = new FileOutputStream("jceks");
FileInputStream fis = new FileInputStream("jceks");) {
KeyStore ks = KeyStore.getInstance("jceks", p);
// create an empty key store
ks.load(null, null);
// store the secret keys
String aliasHead = new String("secretKey");
for (int j = 0; j < NUM_ALGOS; j++) {
ks.setKeyEntry(aliasHead + j, sks[j], PASSWDK, null);
}
// write the key store out to a file
ks.store(fos, PASSWDF);
// wipe clean the existing key store
for (int k = 0; k < NUM_ALGOS; k++) {
ks.deleteEntry(aliasHead + k);
}
if (ks.size() != 0) {
throw new RuntimeException("ERROR: re-initialization failed");
}
// reload the key store with the file
ks.load(fis, PASSWDF);
// check the integrity/validaty of the key store
Key temp = null;
String alias = null;
if (ks.size() != NUM_ALGOS) {
throw new RuntimeException("ERROR: wrong number of key"
+ " entries");
}
for (int m = 0; m < ks.size(); m++) {
alias = aliasHead + m;
temp = ks.getKey(alias, PASSWDK);
// compare the keys
if (!temp.equals(sks[m])) {
throw new RuntimeException("ERROR: key comparison (" + m
+ ") failed");
}
// check the type of key
if (ks.isCertificateEntry(alias) || !ks.isKeyEntry(alias)) {
throw new RuntimeException("ERROR: type identification ("
+ m + ") failed");
}
}
}
}