public static byte[] encode(LwM2mNode node, LwM2mPath path, LwM2mModel model) {
    Validate.notNull(node);
    Validate.notNull(path);
    Validate.notNull(model);

    InternalEncoder internalEncoder = new InternalEncoder();
    internalEncoder.objectId = path.getObjectId();
    internalEncoder.model = model;
    node.accept(internalEncoder);
    return internalEncoder.out.toByteArray();
  }
  /** @param file the file path to persist the registry */
  public SecurityRegistryImpl(String file, PrivateKey serverPrivateKey, PublicKey serverPublicKey) {
    Validate.notEmpty(file);

    filename = file;
    this.serverPrivateKey = serverPrivateKey;
    this.serverPublicKey = serverPublicKey;
    loadFromFile();
  }
  /** @param file the file path to persist the registry */
  public SecurityRegistryImpl(
      String file,
      PrivateKey serverPrivateKey,
      X509Certificate[] serverX509CertChain,
      Certificate[] trustedCertificates) {
    Validate.notEmpty(file);
    Validate.notEmpty(serverX509CertChain);
    Validate.notEmpty(trustedCertificates);

    filename = file;
    this.serverPrivateKey = serverPrivateKey;
    this.serverX509CertChain = serverX509CertChain;
    // extract the raw public key from the first certificate in the chain
    this.serverPublicKey = serverX509CertChain[0].getPublicKey();
    this.trustedCertificates = trustedCertificates;
    loadFromFile();
  }
Exemple #4
0
  public RegisterRequest(
      String endpointName,
      Long lifetime,
      String lwVersion,
      BindingMode bindingMode,
      String smsNumber,
      LinkObject[] objectLinks,
      Map<String, String> additionalAttributes) {

    Validate.notNull(endpointName);
    Validate.noNullElements(objectLinks);

    this.endpointName = endpointName;
    this.lifetime = lifetime;
    this.lwVersion = lwVersion;
    this.bindingMode = bindingMode;
    this.smsNumber = smsNumber;
    this.objectLinks = objectLinks;
    if (additionalAttributes == null)
      this.additionalAttributes = Collections.unmodifiableMap(new HashMap<String, String>());
    else this.additionalAttributes = Collections.unmodifiableMap(additionalAttributes);
  }
Exemple #5
0
  /**
   * Initialize a server which will bind to the specified address and port.
   *
   * @param localAddress the address to bind the CoAP server.
   * @param localAddressSecure the address to bind the CoAP server for DTLS connection.
   * @param privateKey for RPK authentication mode
   * @param publicKey for RPK authentication mode
   */
  public LeshanServer(
      InetSocketAddress localAddress,
      InetSocketAddress localAddressSecure,
      final ClientRegistry clientRegistry,
      final SecurityRegistry securityRegistry,
      final ObservationRegistry observationRegistry,
      final LwM2mModelProvider modelProvider) {
    Validate.notNull(localAddress, "IP address cannot be null");
    Validate.notNull(localAddressSecure, "Secure IP address cannot be null");
    Validate.notNull(clientRegistry, "clientRegistry cannot be null");
    Validate.notNull(securityRegistry, "securityRegistry cannot be null");
    Validate.notNull(observationRegistry, "observationRegistry cannot be null");
    Validate.notNull(modelProvider, "modelProvider cannot be null");

    // Init registries
    this.clientRegistry = clientRegistry;
    this.securityRegistry = securityRegistry;
    this.observationRegistry = observationRegistry;

    this.modelProvider = modelProvider;

    // Cancel observations on client unregistering
    this.clientRegistry.addListener(
        new ClientRegistryListener() {

          @Override
          public void updated(final Client clientUpdated) {}

          @Override
          public void unregistered(final Client client) {
            LeshanServer.this.observationRegistry.cancelObservations(client);
          }

          @Override
          public void registered(final Client client) {}
        });

    // default endpoint
    coapServer = new CoapServer();
    final Endpoint endpoint = new CoAPEndpoint(localAddress);
    coapServer.addEndpoint(endpoint);

    // secure endpoint
    DTLSConnector connector = new DTLSConnector(localAddressSecure);
    connector
        .getConfig()
        .setPskStore(new LwM2mPskStore(this.securityRegistry, this.clientRegistry));
    PrivateKey privateKey = this.securityRegistry.getServerPrivateKey();
    PublicKey publicKey = this.securityRegistry.getServerPublicKey();
    if (privateKey != null && publicKey != null) {
      connector.getConfig().setPrivateKey(privateKey, publicKey);
      // TODO this should be automatically done by scandium
      connector.getConfig().setPreferredCipherSuite(CipherSuite.TLS_ECDHE_ECDSA_WITH_AES_128_CCM_8);
    } else {
      // TODO this should be automatically done by scandium
      connector.getConfig().setPreferredCipherSuite(CipherSuite.TLS_PSK_WITH_AES_128_CCM_8);
    }

    final Endpoint secureEndpoint = new SecureEndpoint(connector);
    coapServer.addEndpoint(secureEndpoint);

    // define /rd resource
    final RegisterResource rdResource =
        new RegisterResource(new RegistrationHandler(this.clientRegistry, this.securityRegistry));
    coapServer.add(rdResource);

    // create sender
    final Set<Endpoint> endpoints = new HashSet<>();
    endpoints.add(endpoint);
    endpoints.add(secureEndpoint);
    requestSender =
        new CaliforniumLwM2mRequestSender(
            endpoints, this.clientRegistry, this.observationRegistry, modelProvider);
  }