ExampleKeys.java 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. package at.acdp.opcur;
  2. /* ========================================================================
  3. * Copyright (c) 2005-2015 The OPC Foundation, Inc. All rights reserved.
  4. *
  5. * OPC Foundation MIT License 1.00
  6. *
  7. * Permission is hereby granted, free of charge, to any person
  8. * obtaining a copy of this software and associated documentation
  9. * files (the "Software"), to deal in the Software without
  10. * restriction, including without limitation the rights to use,
  11. * copy, modify, merge, publish, distribute, sublicense, and/or sell
  12. * copies of the Software, and to permit persons to whom the
  13. * Software is furnished to do so, subject to the following
  14. * conditions:
  15. *
  16. * The above copyright notice and this permission notice shall be
  17. * included in all copies or substantial portions of the Software.
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  19. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
  20. * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  21. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  22. * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  23. * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  24. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  25. * OTHER DEALINGS IN THE SOFTWARE.
  26. *
  27. * The complete license agreement can be found here:
  28. * http://opcfoundation.org/License/MIT/1.00/
  29. * ======================================================================*/
  30. import java.io.File;
  31. import java.io.IOException;
  32. import java.io.InputStream;
  33. import java.net.InetAddress;
  34. import java.security.InvalidAlgorithmParameterException;
  35. import java.security.InvalidKeyException;
  36. import java.security.Key;
  37. import java.security.KeyStore;
  38. import java.security.KeyStoreException;
  39. import java.security.NoSuchAlgorithmException;
  40. import java.security.UnrecoverableKeyException;
  41. import java.security.cert.Certificate;
  42. import java.security.cert.CertificateEncodingException;
  43. import java.security.cert.CertificateException;
  44. import java.security.cert.X509Certificate;
  45. import java.security.interfaces.RSAPrivateKey;
  46. import java.security.spec.InvalidKeySpecException;
  47. import java.security.spec.InvalidParameterSpecException;
  48. import javax.crypto.BadPaddingException;
  49. import javax.crypto.IllegalBlockSizeException;
  50. import javax.crypto.NoSuchPaddingException;
  51. import org.opcfoundation.ua.common.ServiceResultException;
  52. import org.opcfoundation.ua.transport.security.Cert;
  53. import org.opcfoundation.ua.transport.security.KeyPair;
  54. import org.opcfoundation.ua.transport.security.PrivKey;
  55. import org.opcfoundation.ua.utils.CertificateUtils;
  56. import org.opcfoundation.ua.utils.CryptoUtil;
  57. /**
  58. * Keys for examples
  59. * Keystore.p12 contains 20 RSA keypairs with the following aliases
  60. *
  61. * alias dname
  62. *
  63. * server_8192 CN=server
  64. * server_4096 CN=server
  65. * server_2048 CN=server
  66. * server_1024 CN=server
  67. * server_512 CN=server
  68. *
  69. * client_8192 CN=client
  70. * client_4096 CN=client
  71. * client_2048 CN=client
  72. * client_1024 CN=client
  73. * client_512 CN=client
  74. *
  75. * https_server_8192 CN=https_server
  76. * https_server_4096 CN=https_server
  77. * https_server_2048 CN=https_server
  78. * https_server_1024 CN=https_server
  79. * https_server_512 CN=https_server
  80. *
  81. * https_client_8192 CN=https_client
  82. * https_client_4096 CN=https_client
  83. * https_client_2048 CN=https_client
  84. * https_client_1024 CN=https_client
  85. * https_client_512 CN=https_client
  86. *
  87. * Keystore password is "password".
  88. * Private key passwords are "password".
  89. *
  90. */
  91. public class ExampleKeys {
  92. /**
  93. * Load file certificate and private key from applicationName.der & .pfx - or create ones if they do not exist
  94. * @return the KeyPair composed of the certificate and private key
  95. * @throws ServiceResultException
  96. */
  97. public static KeyPair getCert(String applicationName)
  98. throws ServiceResultException
  99. {
  100. File certFile = new File(applicationName + ".der");
  101. File privKeyFile = new File(applicationName+ ".pem");
  102. try {
  103. Cert myCertificate = Cert.load( certFile );
  104. PrivKey myPrivateKey = PrivKey.load( privKeyFile);
  105. return new KeyPair(myCertificate, myPrivateKey);
  106. } catch (CertificateException e) {
  107. throw new ServiceResultException( e );
  108. } catch (IOException e) {
  109. try {
  110. String hostName = InetAddress.getLocalHost().getHostName();
  111. String applicationUri = "urn:"+hostName+":"+applicationName;
  112. KeyPair keys = CertificateUtils.createApplicationInstanceCertificate(applicationName, null, applicationUri, 3650, hostName);
  113. keys.getCertificate().save(certFile);
  114. keys.getPrivateKey().save(privKeyFile);
  115. return keys;
  116. } catch (Exception e1) {
  117. throw new ServiceResultException( e1 );
  118. }
  119. } catch (NoSuchAlgorithmException e) {
  120. throw new ServiceResultException( e );
  121. } catch (InvalidKeySpecException e) {
  122. throw new ServiceResultException( e );
  123. }
  124. }
  125. /**
  126. * Load CA certificate and private key from SampleCA.der & .pfx - or create ones if they do not exist
  127. * @return the KeyPair composed of the certificate and private key
  128. * @throws ServiceResultException
  129. */
  130. public static KeyPair getCACert()
  131. throws ServiceResultException
  132. {
  133. File certFile = new File("SampleCA.der");
  134. File privKeyFile = new File("SampleCA.pem");
  135. try {
  136. Cert myCertificate = Cert.load( certFile );
  137. PrivKey myPrivateKey = PrivKey.load( privKeyFile);
  138. return new KeyPair(myCertificate, myPrivateKey);
  139. } catch (CertificateException e) {
  140. throw new ServiceResultException( e );
  141. } catch (IOException e) {
  142. try {
  143. KeyPair keys = CertificateUtils.createIssuerCertificate("SampleCA", 3650, null);
  144. keys.getCertificate().save(certFile);
  145. keys.getPrivateKey().save(privKeyFile);
  146. return keys;
  147. } catch (Exception e1) {
  148. throw new ServiceResultException( e1 );
  149. }
  150. } catch (NoSuchAlgorithmException e) {
  151. throw new ServiceResultException( e );
  152. } catch (InvalidKeySpecException e) {
  153. throw new ServiceResultException( e );
  154. }
  155. }
  156. /**
  157. * Load file certificate and private key from applicationName.der & .pfx - or create ones if they do not exist
  158. * @param applicationName
  159. * @return the KeyPair composed of the certificate and private key
  160. * @throws ServiceResultException
  161. */
  162. public static KeyPair getHttpsCert(String applicationName)
  163. throws ServiceResultException
  164. {
  165. File certFile = new File(applicationName + "_https.der");
  166. File privKeyFile = new File(applicationName+ "_https.pem");
  167. try {
  168. Cert myCertificate = Cert.load( certFile );
  169. PrivKey myPrivateKey = PrivKey.load( privKeyFile);
  170. return new KeyPair(myCertificate, myPrivateKey);
  171. } catch (CertificateException e) {
  172. throw new ServiceResultException( e );
  173. } catch (IOException e) {
  174. try {
  175. KeyPair caCert = getCACert();
  176. String hostName = InetAddress.getLocalHost().getHostName();
  177. String applicationUri = "urn:"+hostName+":"+applicationName;
  178. KeyPair keys = CertificateUtils.createHttpsCertificate(hostName, applicationUri, 3650, caCert);
  179. keys.getCertificate().save(certFile);
  180. keys.getPrivateKey().save(privKeyFile);
  181. return keys;
  182. } catch (Exception e1) {
  183. throw new ServiceResultException( e1 );
  184. }
  185. } catch (NoSuchAlgorithmException e) {
  186. throw new ServiceResultException( e );
  187. } catch (InvalidKeySpecException e) {
  188. throw new ServiceResultException( e );
  189. }
  190. }
  191. /**
  192. * Open keypair from keystore.p12 used in some of these examples.
  193. *
  194. * Usable aliases are : "server", "client", "https_server", "https_client"
  195. * Usable keysizes are : 8192, 4096, 2048, 1024
  196. *
  197. * @param alias
  198. * @param keysize
  199. * @return
  200. * @throws KeyStoreException
  201. * @throws IOException
  202. * @throws CertificateException
  203. * @throws NoSuchAlgorithmException
  204. * @throws UnrecoverableKeyException
  205. */
  206. // public static KeyPair getKeyPair(String alias, int keysize) throws ServiceResultException {
  207. // try {
  208. // Certificate cert = ks.getCertificate(alias+"_"+keysize);
  209. // Key key = ks.getKey(alias+"_"+keysize, "password".toCharArray());
  210. // KeyPair pair = new KeyPair( new Cert( (X509Certificate) cert ), new PrivKey( (RSAPrivateKey) key ) );
  211. // return pair;
  212. // } catch (KeyStoreException e) {
  213. // throw new ServiceResultException( e );
  214. // } catch (UnrecoverableKeyException e) {
  215. // throw new ServiceResultException( e );
  216. // } catch (NoSuchAlgorithmException e) {
  217. // throw new ServiceResultException( e );
  218. // } catch (CertificateEncodingException e) {
  219. // throw new ServiceResultException( e );
  220. // }
  221. // }
  222. //static KeyStore ks;
  223. // static {
  224. // try {
  225. // ks = KeyStore.getInstance("pkcs12");
  226. // InputStream is = ExampleKeys.class.getResourceAsStream("keystore.p12");
  227. // try {
  228. // ks.load( is, "password".toCharArray() );
  229. // } catch (NoSuchAlgorithmException e) {
  230. // throw new RuntimeException(e);
  231. // } catch (CertificateException e) {
  232. // throw new RuntimeException(e);
  233. // } catch (IOException e) {
  234. // throw new RuntimeException(e);
  235. // } finally {
  236. // try {
  237. // is.close();
  238. // } catch (IOException e) {
  239. // }
  240. // }
  241. // } catch (KeyStoreException e) {
  242. // throw new RuntimeException(e);
  243. // }
  244. // }
  245. }