Exemplo n.º 1
0
 /**
  * Load file certificate and private key from applicationName.der & .pfx - or create ones if they
  * do not exist
  *
  * @return the KeyPair composed of the certificate and private key
  * @throws ServiceResultException
  */
 private static KeyPair getOPCCert(String applicationName) throws ServiceResultException {
   File certFile = new File(applicationName + ".der");
   File privKeyFile = new File(applicationName + ".pem");
   try {
     Cert myServerCertificate = Cert.load(certFile);
     PrivKey myServerPrivateKey = PrivKey.load(privKeyFile, PRIVKEY_PASSWORD);
     return new KeyPair(myServerCertificate, myServerPrivateKey);
   } catch (CertificateException e) {
     throw new ServiceResultException(e);
   } catch (IOException e) {
     try {
       String hostName = InetAddress.getLocalHost().getHostName();
       String applicationUri = "urn:" + hostName + ":" + applicationName;
       KeyPair keys =
           CertificateUtils.createApplicationInstanceCertificate(
               applicationName, null, applicationUri, 3650, hostName);
       keys.getCertificate().save(certFile);
       PrivKey privKeySecure = keys.getPrivateKey();
       privKeySecure.save(privKeyFile, PRIVKEY_PASSWORD);
       return keys;
     } catch (Exception e1) {
       throw new ServiceResultException(e1);
     }
   } catch (NoSuchAlgorithmException e) {
     throw new ServiceResultException(e);
   } catch (Exception e) {
     throw new ServiceResultException(e);
   }
 }