예제 #1
0
  private KeyPair getOrInitKeyPair() {
    LOG.debug(
        "Check if key pair exists {}, {}",
        clientPublicKeyFileLocation,
        clientPrivateKeyFileLocation);
    if (keyPair != null) {
      return keyPair;
    }
    if (storage.exists(clientPublicKeyFileLocation)
        && storage.exists(clientPrivateKeyFileLocation)) {
      InputStream publicKeyInput = null;
      InputStream privateKeyInput = null;
      try {
        publicKeyInput = storage.openForRead(clientPublicKeyFileLocation);
        privateKeyInput = storage.openForRead(clientPrivateKeyFileLocation);

        PublicKey publicKey = KeyUtil.getPublic(publicKeyInput);
        PrivateKey privateKey = KeyUtil.getPrivate(privateKeyInput);

        if (publicKey != null && privateKey != null) {
          keyPair = new KeyPair(publicKey, privateKey);
          if (!KeyUtil.validateKeyPair(keyPair)) {
            throw new InvalidKeyException();
          }
        }
      } catch (InvalidKeyException e) {
        keyPair = null;
        LOG.error("Unable to parse client RSA keypair. Generating new keys.. Reason {}", e);
      } catch (Exception e) {
        LOG.error("Error loading client RSA keypair. Reason {}", e);
        throw new RuntimeException(e); // NOSONAR
      } finally {
        IOUtils.closeQuietly(publicKeyInput);
        IOUtils.closeQuietly(privateKeyInput);
      }
    }
    if (keyPair == null) {
      LOG.debug("Generating Client Key pair");
      OutputStream privateKeyOutput = null;
      OutputStream publicKeyOutput = null;
      try {
        privateKeyOutput = storage.openForWrite(clientPrivateKeyFileLocation);
        publicKeyOutput = storage.openForWrite(clientPublicKeyFileLocation);
        keyPair = KeyUtil.generateKeyPair(privateKeyOutput, publicKeyOutput);
      } catch (IOException e) {
        LOG.error("Error generating Client Key pair", e);
        throw new RuntimeException(e);
      } finally {
        IOUtils.closeQuietly(privateKeyOutput);
        IOUtils.closeQuietly(publicKeyOutput);
      }
    }
    return keyPair;
  }
예제 #2
0
  public KaaClientPropertiesState(
      PersistentStorage storage, Base64 base64, KaaClientProperties properties) {
    super();
    this.storage = storage;
    this.base64 = base64;

    properties.setBase64(base64);

    stateFileLocation = properties.getStateFileFullName();

    clientPrivateKeyFileLocation = properties.getPrivateKeyFileFullName();

    clientPublicKeyFileLocation = properties.getPublicKeyFileFullName();

    LOG.info(
        "Version: '{}', commit hash: '{}'",
        properties.getBuildVersion(),
        properties.getCommitHash());

    state = new Properties();
    if (storage.exists(stateFileLocation)) {
      InputStream stream = null;
      try {
        stream = storage.openForRead(stateFileLocation);
        state.load(stream);

        if (isSDKPropertiesUpdated(properties)) {
          LOG.info("SDK properties were updated");
          setRegistered(false);
          setPropertiesHash(properties.getPropertiesHash());
          // TODO: add more intelligent check by comparing part of SDK token.
          isConfigVersionUpdated = true;
        } else {
          LOG.info("SDK properties are up to date");
        }

        parseTopics();
        parseNfSubscriptions();

        String attachedEndpointsString = state.getProperty(ATTACHED_ENDPOINTS);
        if (attachedEndpointsString != null) {
          String[] splittedEndpointsList = attachedEndpointsString.split(",");
          for (String attachedEndpoint : splittedEndpointsList) {
            if (!attachedEndpoint.isEmpty()) {
              String[] splittedValues = attachedEndpoint.split(":");
              attachedEndpoints.put(
                  new EndpointAccessToken(splittedValues[0]),
                  new EndpointKeyHash(splittedValues[1]));
            }
          }
        }

        String eventSeqNumStr = state.getProperty(EVENT_SEQ_NUM);
        if (eventSeqNumStr != null) {
          Integer eventSeqNum = 0;
          try { // NOSONAR
            eventSeqNum = Integer.parseInt(eventSeqNumStr);
          } catch (NumberFormatException e) {
            LOG.error(
                "Unexpected exception while parsing event sequence number. Can not parse String: {} to Integer",
                eventSeqNumStr);
          }
          eventSequence.set(eventSeqNum);
        }
        String topicListHashStr = state.getProperty(TOPIC_LIST_HASH);
        if (topicListHashStr != null) {
          try { // NOSONAR
            this.topicListHash = Integer.parseInt(topicListHashStr);
          } catch (NumberFormatException e) {
            LOG.error(
                "Unexpected exception while parsing topic list hash. Can not parse String: {} to Integer",
                topicListHashStr);
          }
        }
      } catch (Exception e) {
        LOG.error("Can't load state file", e);
      } finally {
        IOUtils.closeQuietly(stream);
      }
    } else {
      LOG.info("First SDK start");
      setPropertiesHash(properties.getPropertiesHash());
    }
  }