ExampleKeys.java 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  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. private static final String PRIVKEY_PASSWORD = "Opc.Ua";
  93. /**
  94. * Load file certificate and private key from applicationName.der & .pfx - or create ones if they do not exist
  95. * @return the KeyPair composed of the certificate and private key
  96. * @throws ServiceResultException
  97. */
  98. public static KeyPair getCert(String applicationName)
  99. throws ServiceResultException
  100. {
  101. File certFile = new File(applicationName + ".der");
  102. File privKeyFile = new File(applicationName+ ".pem");
  103. try {
  104. Cert myCertificate = Cert.load( certFile );
  105. PrivKey myPrivateKey = PrivKey.load( privKeyFile, PRIVKEY_PASSWORD );
  106. return new KeyPair(myCertificate, myPrivateKey);
  107. } catch (CertificateException e) {
  108. throw new ServiceResultException( e );
  109. } catch (IOException e) {
  110. try {
  111. String hostName = InetAddress.getLocalHost().getHostName();
  112. String applicationUri = "urn:"+hostName+":"+applicationName;
  113. KeyPair keys = CertificateUtils.createApplicationInstanceCertificate(applicationName, null, applicationUri, 3650, hostName);
  114. keys.getCertificate().save(certFile);
  115. keys.getPrivateKey().save(privKeyFile);
  116. return keys;
  117. } catch (Exception e1) {
  118. throw new ServiceResultException( e1 );
  119. }
  120. } catch (NoSuchAlgorithmException e) {
  121. throw new ServiceResultException( e );
  122. } catch (InvalidKeyException e) {
  123. throw new ServiceResultException( e );
  124. } catch (InvalidKeySpecException e) {
  125. throw new ServiceResultException( e );
  126. } catch (NoSuchPaddingException e) {
  127. throw new ServiceResultException( e );
  128. } catch (InvalidAlgorithmParameterException e) {
  129. throw new ServiceResultException( e );
  130. } catch (IllegalBlockSizeException e) {
  131. throw new ServiceResultException( e );
  132. } catch (BadPaddingException e) {
  133. throw new ServiceResultException( e );
  134. } catch (InvalidParameterSpecException e) {
  135. throw new ServiceResultException( e );
  136. }
  137. }
  138. /**
  139. * Load CA certificate and private key from SampleCA.der & .pfx - or create ones if they do not exist
  140. * @return the KeyPair composed of the certificate and private key
  141. * @throws ServiceResultException
  142. */
  143. public static KeyPair getCACert()
  144. throws ServiceResultException
  145. {
  146. File certFile = new File("SampleCA.der");
  147. File privKeyFile = new File("SampleCA.pem");
  148. try {
  149. Cert myCertificate = Cert.load( certFile );
  150. PrivKey myPrivateKey = PrivKey.load( privKeyFile, PRIVKEY_PASSWORD );
  151. return new KeyPair(myCertificate, myPrivateKey);
  152. } catch (CertificateException e) {
  153. throw new ServiceResultException( e );
  154. } catch (IOException e) {
  155. try {
  156. KeyPair keys = CertificateUtils.createIssuerCertificate("SampleCA", 3650, null);
  157. keys.getCertificate().save(certFile);
  158. keys.getPrivateKey().save(privKeyFile, PRIVKEY_PASSWORD);
  159. return keys;
  160. } catch (Exception e1) {
  161. throw new ServiceResultException( e1 );
  162. }
  163. } catch (NoSuchAlgorithmException e) {
  164. throw new ServiceResultException( e );
  165. } catch (InvalidKeyException e) {
  166. throw new ServiceResultException( e );
  167. } catch (InvalidKeySpecException e) {
  168. throw new ServiceResultException( e );
  169. } catch (NoSuchPaddingException e) {
  170. throw new ServiceResultException( e );
  171. } catch (InvalidAlgorithmParameterException e) {
  172. throw new ServiceResultException( e );
  173. } catch (IllegalBlockSizeException e) {
  174. throw new ServiceResultException( e );
  175. } catch (BadPaddingException e) {
  176. throw new ServiceResultException( e );
  177. } catch (InvalidParameterSpecException e) {
  178. throw new ServiceResultException( e );
  179. }
  180. }
  181. /**
  182. * Load file certificate and private key from applicationName.der & .pfx - or create ones if they do not exist
  183. * @param applicationName
  184. * @return the KeyPair composed of the certificate and private key
  185. * @throws ServiceResultException
  186. */
  187. public static KeyPair getHttpsCert(String applicationName)
  188. throws ServiceResultException
  189. {
  190. File certFile = new File(applicationName + "_https.der");
  191. File privKeyFile = new File(applicationName+ "_https.pem");
  192. try {
  193. Cert myCertificate = Cert.load( certFile );
  194. PrivKey myPrivateKey = PrivKey.load( privKeyFile, PRIVKEY_PASSWORD );
  195. return new KeyPair(myCertificate, myPrivateKey);
  196. } catch (CertificateException e) {
  197. throw new ServiceResultException( e );
  198. } catch (IOException e) {
  199. try {
  200. KeyPair caCert = getCACert();
  201. String hostName = InetAddress.getLocalHost().getHostName();
  202. String applicationUri = "urn:"+hostName+":"+applicationName;
  203. KeyPair keys = CertificateUtils.createHttpsCertificate(hostName, applicationUri, 3650, caCert);
  204. keys.getCertificate().save(certFile);
  205. keys.getPrivateKey().save(privKeyFile, PRIVKEY_PASSWORD);
  206. return keys;
  207. } catch (Exception e1) {
  208. throw new ServiceResultException( e1 );
  209. }
  210. } catch (NoSuchAlgorithmException e) {
  211. throw new ServiceResultException( e );
  212. } catch (InvalidKeyException e) {
  213. throw new ServiceResultException( e );
  214. } catch (InvalidKeySpecException e) {
  215. throw new ServiceResultException( e );
  216. } catch (NoSuchPaddingException e) {
  217. throw new ServiceResultException( e );
  218. } catch (InvalidAlgorithmParameterException e) {
  219. throw new ServiceResultException( e );
  220. } catch (IllegalBlockSizeException e) {
  221. throw new ServiceResultException( e );
  222. } catch (BadPaddingException e) {
  223. throw new ServiceResultException( e );
  224. } catch (InvalidParameterSpecException e) {
  225. throw new ServiceResultException( e );
  226. }
  227. }
  228. /**
  229. * Open keypair from keystore.p12 used in some of these examples.
  230. *
  231. * Usable aliases are : "server", "client", "https_server", "https_client"
  232. * Usable keysizes are : 8192, 4096, 2048, 1024
  233. *
  234. * @param alias
  235. * @param keysize
  236. * @return
  237. * @throws KeyStoreException
  238. * @throws IOException
  239. * @throws CertificateException
  240. * @throws NoSuchAlgorithmException
  241. * @throws UnrecoverableKeyException
  242. */
  243. public static KeyPair getKeyPair(String alias, int keysize) throws ServiceResultException {
  244. try {
  245. Certificate cert = ks.getCertificate(alias+"_"+keysize);
  246. Key key = ks.getKey(alias+"_"+keysize, "password".toCharArray());
  247. KeyPair pair = new KeyPair( new Cert( (X509Certificate) cert ), new PrivKey( (RSAPrivateKey) key ) );
  248. return pair;
  249. } catch (KeyStoreException e) {
  250. throw new ServiceResultException( e );
  251. } catch (UnrecoverableKeyException e) {
  252. throw new ServiceResultException( e );
  253. } catch (NoSuchAlgorithmException e) {
  254. throw new ServiceResultException( e );
  255. } catch (CertificateEncodingException e) {
  256. throw new ServiceResultException( e );
  257. }
  258. }
  259. static KeyStore ks;
  260. static {
  261. try {
  262. ks = KeyStore.getInstance("pkcs12");
  263. InputStream is = ExampleKeys.class.getResourceAsStream("keystore.p12");
  264. try {
  265. ks.load( is, "password".toCharArray() );
  266. } catch (NoSuchAlgorithmException e) {
  267. throw new RuntimeException(e);
  268. } catch (CertificateException e) {
  269. throw new RuntimeException(e);
  270. } catch (IOException e) {
  271. throw new RuntimeException(e);
  272. } finally {
  273. try {
  274. is.close();
  275. } catch (IOException e) {
  276. }
  277. }
  278. } catch (KeyStoreException e) {
  279. throw new RuntimeException(e);
  280. }
  281. }
  282. }