KeyStoreLoader.java 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. package at.acdp.opcur.opc;
  2. /*
  3. * Copyright (c) 2016 Kevin Herron
  4. *
  5. * All rights reserved. This program and the accompanying materials
  6. * are made available under the terms of the Eclipse Public License v1.0
  7. * and Eclipse Distribution License v1.0 which accompany this distribution.
  8. *
  9. * The Eclipse Public License is available at
  10. * http://www.eclipse.org/legal/epl-v10.html
  11. * and the Eclipse Distribution License is available at
  12. * http://www.eclipse.org/org/documents/edl-v10.html.
  13. */
  14. import java.io.File;
  15. import java.io.FileInputStream;
  16. import java.io.FileOutputStream;
  17. import java.security.Key;
  18. import java.security.KeyPair;
  19. import java.security.KeyStore;
  20. import java.security.PrivateKey;
  21. import java.security.PublicKey;
  22. import java.security.cert.X509Certificate;
  23. import java.util.Arrays;
  24. import java.util.UUID;
  25. import java.util.regex.Pattern;
  26. import org.eclipse.milo.opcua.sdk.server.util.HostnameUtil;
  27. import org.eclipse.milo.opcua.stack.core.util.SelfSignedCertificateBuilder;
  28. import org.eclipse.milo.opcua.stack.core.util.SelfSignedCertificateGenerator;
  29. import org.slf4j.Logger;
  30. import org.slf4j.LoggerFactory;
  31. class KeyStoreLoader {
  32. private static final Pattern IP_ADDR_PATTERN = Pattern.compile(
  33. "^(([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\.){3}([01]?\\d\\d?|2[0-4]\\d|25[0-5])$");
  34. private static final String SERVER_ALIAS = "server-ai";
  35. private static final char[] PASSWORD = "password".toCharArray();
  36. private final Logger logger = LoggerFactory.getLogger(getClass());
  37. private X509Certificate[] serverCertificateChain;
  38. private X509Certificate serverCertificate;
  39. private KeyPair serverKeyPair;
  40. KeyStoreLoader load(File baseDir) throws Exception {
  41. KeyStore keyStore = KeyStore.getInstance("PKCS12");
  42. File serverKeyStore = baseDir.toPath().resolve("example-server.pfx").toFile();
  43. logger.info("Loading KeyStore at {}", serverKeyStore);
  44. if (!serverKeyStore.exists()) {
  45. keyStore.load(null, PASSWORD);
  46. KeyPair keyPair = SelfSignedCertificateGenerator.generateRsaKeyPair(2048);
  47. String applicationUri = "urn:eclipse:milo:examples:server:" + UUID.randomUUID();
  48. SelfSignedCertificateBuilder builder = new SelfSignedCertificateBuilder(keyPair)
  49. .setCommonName("Eclipse Milo Example Server")
  50. .setOrganization("digitalpetri")
  51. .setOrganizationalUnit("dev")
  52. .setLocalityName("Folsom")
  53. .setStateName("CA")
  54. .setCountryCode("US")
  55. .setApplicationUri(applicationUri)
  56. .addDnsName("localhost")
  57. .addIpAddress("127.0.0.1");
  58. // Get as many hostnames and IP addresses as we can listed in the certificate.
  59. for (String hostname : HostnameUtil.getHostnames("0.0.0.0")) {
  60. if (IP_ADDR_PATTERN.matcher(hostname).matches()) {
  61. builder.addIpAddress(hostname);
  62. } else {
  63. builder.addDnsName(hostname);
  64. }
  65. }
  66. X509Certificate certificate = builder.build();
  67. keyStore.setKeyEntry(SERVER_ALIAS, keyPair.getPrivate(), PASSWORD, new X509Certificate[]{certificate});
  68. keyStore.store(new FileOutputStream(serverKeyStore), PASSWORD);
  69. } else {
  70. keyStore.load(new FileInputStream(serverKeyStore), PASSWORD);
  71. }
  72. Key serverPrivateKey = keyStore.getKey(SERVER_ALIAS, PASSWORD);
  73. if (serverPrivateKey instanceof PrivateKey) {
  74. serverCertificate = (X509Certificate) keyStore.getCertificate(SERVER_ALIAS);
  75. serverCertificateChain = Arrays.stream(keyStore.getCertificateChain(SERVER_ALIAS))
  76. .map(X509Certificate.class::cast)
  77. .toArray(X509Certificate[]::new);
  78. PublicKey serverPublicKey = serverCertificate.getPublicKey();
  79. serverKeyPair = new KeyPair(serverPublicKey, (PrivateKey) serverPrivateKey);
  80. }
  81. return this;
  82. }
  83. X509Certificate getServerCertificate() {
  84. return serverCertificate;
  85. }
  86. public X509Certificate[] getServerCertificateChain() {
  87. return serverCertificateChain;
  88. }
  89. KeyPair getServerKeyPair() {
  90. return serverKeyPair;
  91. }
  92. }