BrightSide Workbench Full Report + Source Code
CertificateStore.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2011 Lluis TurrĂ³ Cutiller <http://www.turro.org/>
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU Affero General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU Affero General Public License for more details.
14  *
15  * You should have received a copy of the GNU Affero General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  */
18 package org.turro.elephant.impl.security;
19 
20 import java.io.FileInputStream;
21 import java.io.IOException;
22 import java.security.KeyStore;
23 import java.security.KeyStoreException;
24 import java.security.NoSuchAlgorithmException;
25 import java.security.PrivateKey;
26 import java.security.UnrecoverableKeyException;
27 import java.security.cert.Certificate;
28 import java.security.cert.CertificateException;
29 import java.util.logging.Level;
30 import java.util.logging.Logger;
31 import org.turro.elephant.context.ElephantContext;
32 import org.turro.elephant.impl.abstracts.AbstractImplementation;
33 
39 
40  private PrivateKey key;
41  private Certificate[] chain;
42 
43  public PrivateKey getKey() {
44  if(key == null) {
45  initCertificate();
46  }
47  return key;
48  }
49 
50  public Certificate[] getChain() {
51  if(chain == null) {
52  initCertificate();
53  }
54  return chain;
55  }
56 
57  private void initCertificate() {
58  try {
59  //String pw=constructor.encrypt((String) getAttributes().get("password"));
60  String password = ElephantContext.decrypt((String) getAttributes().get("password"));
61  KeyStore ks = KeyStore.getInstance((String) getAttributes().get("storeType"));
62  ks.load(new FileInputStream((String) getAttributes().get("storeFile")), password.toCharArray());
63  String alias = (String) ks.aliases().nextElement();
64  key = (PrivateKey) ks.getKey(alias, password.toCharArray());
65  chain = ks.getCertificateChain(alias);
66  } catch (IOException ex) {
67  Logger.getLogger(CertificateStore.class.getName()).log(Level.SEVERE, ElephantContext.logMsg(null), ex);
68  } catch (CertificateException | NoSuchAlgorithmException | UnrecoverableKeyException | KeyStoreException ex) {
69  Logger.getLogger(CertificateStore.class.getName()).log(Level.SEVERE, ElephantContext.logMsg(null), ex);
70  } catch (Exception ex) {
71  Logger.getLogger(CertificateStore.class.getName()).log(Level.SEVERE, ElephantContext.logMsg(null), ex);
72  }
73  }
74 }