Update IoTivity-lite library

parent 99c65d38
iotivity-lite @ b6f46e32
Subproject commit 66a2d08784903110b7dead5b9a518933a355e1e6
Subproject commit b6f46e322c9a389c86dfcaa26fb2772fcf264a4e
......@@ -24,7 +24,6 @@ import io.reactivex.Single;
import org.apache.log4j.Logger;
import org.iotivity.*;
import org.openconnectivity.otgc.domain.model.resource.secure.acl.*;
import org.openconnectivity.otgc.utils.constant.OcfResourceUri;
import org.openconnectivity.otgc.utils.constant.OcfWildcard;
import javax.inject.Inject;
......@@ -41,64 +40,33 @@ public class AmsRepository {
@Inject
public AmsRepository(){}
public Single<OcAcl> getAcl(String endpoint, String deviceId) {
public Single<OcAcl> getAcl(String deviceId) {
return Single.create(emitter -> {
OCEndpoint ep = OCEndpointUtil.stringToEndpoint(endpoint, new String[1]);
OCUuid uuid = OCUuidUtil.stringToUuid(deviceId);
OCEndpointUtil.setDi(ep, uuid);
OCResponseHandler handler = (OCClientResponse response) -> {
if (response.getCode().equals(OCStatus.OC_STATUS_OK)) {
OcAcl acl = new OcAcl();
acl.parseOCRepresentation(response.getPayload());
emitter.onSuccess(acl);
} else {
LOG.debug("GET ACL error - Status code: " + response.getCode());
emitter.onError(new Exception("GET ACL error - Status code: " + response.getCode()));
}
};
if (!OCMain.doGet(OcfResourceUri.ACL2_URI, ep, null, handler, OCQos.HIGH_QOS)) {
emitter.onError(new Exception("GET ACL error"));
}
OCEndpointUtil.freeEndpoint(ep);
});
}
OCObtAclHandler handler = (OCSecurityAcl acl) -> {
if (acl != null) {
OcAcl aclRet = new OcAcl();
aclRet.parseOCRepresentation(acl);
emitter.onSuccess(aclRet);
private Completable provisionAcl(String endpoint, String deviceId, OcAcl acl) {
return Completable.create(emitter -> {
OCEndpoint ep = OCEndpointUtil.stringToEndpoint(endpoint, new String[1]);
OCUuid uuid = OCUuidUtil.stringToUuid(deviceId);
OCEndpointUtil.setDi(ep, uuid);
OCResponseHandler handler = (OCClientResponse response) -> {
OCStatus code = response.getCode();
if (code.equals(OCStatus.OC_STATUS_OK) || code.equals(OCStatus.OC_STATUS_CHANGED)) {
LOG.debug("Provision ACL succeeded");
emitter.onComplete();
/* Freeing the ACL structure */
OCObt.freeAcl(acl);
} else {
emitter.onError(new IOException("Provision ACE error"));
String error = "GET ACL error";
LOG.debug(error);
emitter.onError(new Exception(error));
}
};
if (OCMain.initPost(OcfResourceUri.ACL2_URI, ep, null, handler, OCQos.LOW_QOS)) {
CborEncoder root = acl.parseToCbor();
if (OCMain.doPost()) {
LOG.debug("Sent POST request to /oic/sec/acl2");
} else {
String error = "Could not send POST request to /oic/sec/acl2";
LOG.error(error);
emitter.onError(new Exception(error));
}
int ret = OCObt.retrieveAcl(uuid, handler);
if (ret >= 0) {
LOG.debug("Successfully issued request to RETRIEVE /oic/sec/acl2");
} else {
String error = "Could not init POST request to /oic/sec/acl2";
String error = "ERROR issuing request to RETRIEVE /oic/sec/acl2";
LOG.error(error);
emitter.onError(new Exception(error));
}
OCEndpointUtil.freeEndpoint(ep);
});
}
......@@ -138,22 +106,6 @@ public class AmsRepository {
});
}
public Completable provisionUuidAcl(String endpoint, String deviceId, String subjectId, List<String> verticalResources, long permission) {
OcAceSubject subject = new OcAceSubject();
subject.setType(OcAceSubjectType.UUID_TYPE);
subject.setUuid(subjectId);
OcAce ace = new OcAce();
ace.setSubject(subject);
ace.setPermission(permission);
ace.setResources(getResources(verticalResources));
List<OcAce> aceList = new ArrayList<>();
aceList.add(ace);
OcAcl acl = new OcAcl();
acl.setAceList(aceList);
return provisionAcl(endpoint, deviceId, acl);
}
public Completable provisionUuidAce(String deviceId, String subjectId, List<String> verticalResources, long permission) {
OCUuid di = OCUuidUtil.stringToUuid(subjectId);
......@@ -167,23 +119,6 @@ public class AmsRepository {
return provisionAce(deviceId, ace, verticalResources, permission);
}
public Completable provisionRoleAcl(String endpoint, String deviceId, String roleId, String roleAuthority, List<String> verticalResources, long permission) {
OcAceSubject subject = new OcAceSubject();
subject.setType(OcAceSubjectType.ROLE_TYPE);
subject.setRoleId(roleId);
subject.setAuthority(roleAuthority);
OcAce ace = new OcAce();
ace.setSubject(subject);
ace.setPermission(permission);
ace.setResources(getResources(verticalResources));
List<OcAce> aceList = new ArrayList<>();
aceList.add(ace);
OcAcl acl = new OcAcl();
acl.setAceList(aceList);
return provisionAcl(endpoint, deviceId, acl);
}
public Completable provisionRoleAce(String deviceId, String roleId, String roleAuthority, List<String> verticalResources, long permission) {
OCSecurityAce ace = OCObt.newAceForRole(roleId, roleAuthority);
if (ace == null) {
......@@ -195,22 +130,6 @@ public class AmsRepository {
return provisionAce(deviceId, ace, verticalResources, permission);
}
public Completable provisionConntypeAcl(String endpoint, String deviceId, boolean isAuthCrypt, List<String> verticalResources, long permission) {
OcAceSubject subject = new OcAceSubject();
subject.setType(OcAceSubjectType.CONN_TYPE);
subject.setConnType(isAuthCrypt ? "auth-crypt" : "anon-clear");
OcAce ace = new OcAce();
ace.setSubject(subject);
ace.setPermission(permission);
ace.setResources(getResources(verticalResources));
List<OcAce> aceList = new ArrayList<>();
aceList.add(ace);
OcAcl acl = new OcAcl();
acl.setAceList(aceList);
return provisionAcl(endpoint, deviceId, acl);
}
public Completable provisionConntypeAce(String deviceId, boolean isAuthCrypt, List<String> verticalResources, long permission) {
OCSecurityAce ace = OCObt.newAceForConnection(isAuthCrypt ? OCAceConnectionType.OC_CONN_AUTH_CRYPT : OCAceConnectionType.OC_CONN_ANON_CLEAR);
if (ace == null) {
......@@ -274,29 +193,27 @@ public class AmsRepository {
});
}
public Completable deleteAcl(String endpoint, String deviceId, long aceId) {
public Completable deleteAcl(String deviceId, long aceId) {
return Completable.create(emitter -> {
OCEndpoint ep = OCEndpointUtil.stringToEndpoint(endpoint, new String[1]);
OCUuid uuid = OCUuidUtil.stringToUuid(deviceId);
OCEndpointUtil.setDi(ep, uuid);
OCResponseHandler handler = (OCClientResponse response) -> {
OCStatus code = response.getCode();
if (code.equals(OCStatus.OC_STATUS_OK) || code.equals(OCStatus.OC_STATUS_DELETED)) {
OCObtStatusHandler handler = (int status) -> {
if (status >= 0) {
LOG.debug("Delete ACE succeeded");
emitter.onComplete();
} else {
emitter.onError(new IOException("Delete ACE error"));
emitter.onError(new Exception("Delete ACE error"));
}
};
if (!OCMain.doDelete(OcfResourceUri.ACL2_URI, ep, OcfResourceUri.DELETE_ACE_QUERY + aceId, handler, OCQos.HIGH_QOS)) {
int ret = OCObt.deleteAceByAceId(uuid, (int)aceId, handler);
if (ret >= 0) {
LOG.debug("Successfully issued request to DELETE /oic/sec/acl2 with aceid=" + aceId);
} else {
String error = "Could not send DELETE request to /oic/sec/acl2 with aceid=" + aceId;
LOG.error(error);
emitter.onError(new Exception(error));
}
OCEndpointUtil.freeEndpoint(ep);
});
}
......
......@@ -25,16 +25,10 @@ import org.apache.log4j.Logger;
import org.iotivity.*;
import org.openconnectivity.otgc.domain.model.resource.secure.cred.*;
import org.openconnectivity.otgc.domain.model.resource.secure.csr.OcCsr;
import org.openconnectivity.otgc.utils.constant.OcfCredType;
import org.openconnectivity.otgc.utils.constant.OcfCredUsage;
import org.openconnectivity.otgc.utils.constant.OcfEncoding;
import org.openconnectivity.otgc.utils.constant.OcfResourceUri;
import javax.inject.Inject;
import javax.inject.Singleton;
import java.io.*;
import java.util.ArrayList;
import java.util.List;
@Singleton
public class CmsRepository {
......@@ -43,30 +37,33 @@ public class CmsRepository {
@Inject
public CmsRepository() {}
public Single<OcCredentials> getCredentials(String endpoint, String deviceId) {
public Single<OcCredentials> getCredentials(String deviceId) {
return Single.create(emitter -> {
OCEndpoint ep = OCEndpointUtil.stringToEndpoint(endpoint, new String[1]);
OCUuid uuid = OCUuidUtil.stringToUuid(deviceId);
OCEndpointUtil.setDi(ep, uuid);
OCResponseHandler handler = (OCClientResponse response) -> {
OCStatus code = response.getCode();
if (code.equals(OCStatus.OC_STATUS_OK)) {
OCObtCredsHandler handler = (OCCreds credentials) -> {
if (credentials != null) {
OcCredentials creds = new OcCredentials();
creds.parseOCRepresentation(response.getPayload());
creds.parseOCRepresentation(credentials);
emitter.onSuccess(creds);
/* Freeing the credential structure */
OCObt.freeCreds(credentials);
} else {
String error = "GET credentials error";
LOG.error(error);
emitter.onError(new Exception(error));
}
};
if (!OCMain.doGet(OcfResourceUri.CRED_URI, ep, null, handler, OCQos.HIGH_QOS)) {
int ret = OCObt.retrieveCreds(uuid, handler);
if (ret >= 0) {
System.out.println("Successfully issued request to RETRIEVE /oic/sec/cred");
LOG.debug("Successfully issued request to retrieve the credentials");
} else {
String error = "GET credentials error";
LOG.error(error);
emitter.onError(new Exception(error));
}
OCEndpointUtil.freeEndpoint(ep);
});
}
......@@ -95,108 +92,6 @@ public class CmsRepository {
});
}
public Completable provisionTrustAnchor(String endpoint, String deviceId, String rootCert) {
return Completable.create(emitter -> {
OCEndpoint ep = OCEndpointUtil.stringToEndpoint(endpoint, new String[1]);
OCUuid di = OCUuidUtil.stringToUuid(deviceId);
OCEndpointUtil.setDi(ep, di);
OCResponseHandler handler = (OCClientResponse response) -> {
OCStatus code = response.getCode();
if (code.equals(OCStatus.OC_STATUS_OK) || code.equals(OCStatus.OC_STATUS_CHANGED)) {
LOG.debug("Provision identity certificate succeeded");
emitter.onComplete();
} else {
emitter.onError(new IOException("Provision identity certificate error"));
}
};
if (OCMain.initPost(OcfResourceUri.CRED_URI, ep, null, handler, OCQos.HIGH_QOS)) {
OcCredPublicData publicData = new OcCredPublicData();
publicData.setPemData(rootCert);
publicData.setEncoding(OcfEncoding.OC_ENCODING_PEM);
OcCredential cred = new OcCredential();
cred.setSubjectuuid("*");
cred.setCredtype(OcfCredType.OC_CREDTYPE_CERT);
cred.setCredusage(OcfCredUsage.OC_CREDUSAGE_TRUSTCA);
cred.setPublicData(publicData);
List<OcCredential> credList = new ArrayList<>();
credList.add(cred);
OcCredentials creds = new OcCredentials();
creds.setCredList(credList);
CborEncoder root = creds.parseToCbor();
if (OCMain.doPost()) {
LOG.debug("Sent POST request to /oic/sec/cred");
} else {
String error = "Could not send POST request to /oic/sec/cred";
LOG.error(error);
emitter.onError(new Exception(error));
}
} else {
String error = "Could not init POST request to /oic/sec/cred";
LOG.error(error);
emitter.onError(new Exception(error));
}
OCEndpointUtil.freeEndpoint(ep);
});
}
public Completable provisionIdentityCertificate(String endpoint, String deviceId, String rootCert, String identityCert) {
return provisionTrustAnchor(endpoint, deviceId, rootCert)
.andThen(
Completable.create(emitter -> {
OCEndpoint ep = OCEndpointUtil.stringToEndpoint(endpoint, new String[1]);
OCUuid di = OCUuidUtil.stringToUuid(deviceId);
OCEndpointUtil.setDi(ep, di);
OCResponseHandler handler = (OCClientResponse response) -> {
OCStatus code = response.getCode();
if (code.equals(OCStatus.OC_STATUS_OK) || code.equals(OCStatus.OC_STATUS_CHANGED)) {
LOG.debug("Provision identity certificate succeeded");
emitter.onComplete();
} else {
emitter.onError(new IOException("Provision identity certificate error"));
}
};
if (OCMain.initPost(OcfResourceUri.CRED_URI, ep, null, handler, OCQos.HIGH_QOS)) {
OcCredPublicData publicData = new OcCredPublicData();
publicData.setPemData(identityCert);
publicData.setEncoding(OcfEncoding.OC_ENCODING_PEM);
OcCredential cred = new OcCredential();
cred.setSubjectuuid(deviceId);
cred.setCredtype(OcfCredType.OC_CREDTYPE_CERT);
cred.setCredusage(OcfCredUsage.OC_CREDUSAGE_CERT);
cred.setPublicData(publicData);
List<OcCredential> credList = new ArrayList<>();
credList.add(cred);
OcCredentials creds = new OcCredentials();
creds.setCredList(credList);
CborEncoder root = creds.parseToCbor();
if (OCMain.doPost()) {
LOG.debug("Sent POST request to /oic/sec/cred");
} else {
String error = "Could not send POST request to /oic/sec/cred";
LOG.error(error);
emitter.onError(new Exception(error));
}
} else {
String error = "Could not init POST request to /oic/sec/cred";
LOG.error(error);
emitter.onError(new Exception(error));
}
OCEndpointUtil.freeEndpoint(ep);
}));
}
public Completable provisionIdentityCertificate(String deviceId) {
return Completable.create(emitter -> {
OCUuid di = OCUuidUtil.stringToUuid(deviceId);
......@@ -206,69 +101,14 @@ public class CmsRepository {
LOG.debug("Provision identity certificate succeeded");
emitter.onComplete();
} else {
emitter.onError(new IOException("Provision identity certificate error"));
emitter.onError(new Exception("Provision identity certificate error"));
}
};
int ret = OCObt.provisionIdentityCertificate(di, handler);
if (ret < 0) {
emitter.onError(new IOException("Provision identity certificate error"));
}
});
}
public Completable provisionRoleCertificate(String endpoint, String deviceId, String roleCert, String roleId, String roleAuthority) {
return Completable.create(emitter -> {
OCEndpoint ep = OCEndpointUtil.stringToEndpoint(endpoint, new String[1]);
OCUuid di = OCUuidUtil.stringToUuid(deviceId);
OCEndpointUtil.setDi(ep, di);
OCResponseHandler handler = (OCClientResponse response) -> {
OCStatus code = response.getCode();
if (code.equals(OCStatus.OC_STATUS_OK) || code.equals(OCStatus.OC_STATUS_CHANGED)) {
LOG.debug("Provision role certificate succeeded");
emitter.onComplete();
} else {
emitter.onError(new IOException("Provision role certificate error"));
}
};
if (OCMain.initPost(OcfResourceUri.CRED_URI, ep, null, handler, OCQos.HIGH_QOS)) {
OcCredPublicData publicData = new OcCredPublicData();
publicData.setPemData(roleCert);
publicData.setEncoding(OcfEncoding.OC_ENCODING_PEM);
OcCredRole role = new OcCredRole();
role.setRole(roleId);
role.setAuthority(roleAuthority);
OcCredential cred = new OcCredential();
cred.setSubjectuuid(deviceId);
cred.setCredtype(OcfCredType.OC_CREDTYPE_CERT);
cred.setCredusage(OcfCredUsage.OC_CREDUSAGE_ROLECERT);
cred.setPublicData(publicData);
cred.setRoleid(role);
List<OcCredential> credList = new ArrayList<>();
credList.add(cred);
OcCredentials creds = new OcCredentials();
creds.setCredList(credList);
CborEncoder root = creds.parseToCbor();
if (OCMain.doPost()) {
LOG.debug("Sent POST request to /oic/sec/cred");
} else {
String error = "Could not send POST request to /oic/sec/cred";
LOG.error(error);
emitter.onError(new Exception(error));
}
} else {
String error = "Could not init POST request to /oic/sec/cred";
LOG.error(error);
emitter.onError(new Exception(error));
emitter.onError(new Exception("Provision identity certificate error"));
}
OCEndpointUtil.freeEndpoint(ep);
});
}
......@@ -283,67 +123,18 @@ public class CmsRepository {
LOG.debug("Provision role certificate succeeded");
emitter.onComplete();
} else {
emitter.onError(new IOException("Provision role certificate error"));
emitter.onError(new Exception("Provision role certificate error"));
}
};
int ret = OCObt.provisionRoleCertificate(roles, di, handler);
if (ret < 0) {
emitter.onError(new IOException("Provision role certificate error"));
emitter.onError(new Exception("Provision role certificate error"));
OCObt.freeRoleId(roles);
}
});
}
public Completable createPskCredential(String endpoint, String deviceId, String targetUuid, byte[] symmetricKey) {
return Completable.create(emitter -> {
OCEndpoint ep = OCEndpointUtil.stringToEndpoint(endpoint, new String[1]);
OCUuid di = OCUuidUtil.stringToUuid(deviceId);
OCEndpointUtil.setDi(ep, di);
OCResponseHandler handler = (OCClientResponse response) -> {
OCStatus code = response.getCode();
if (code.equals(OCStatus.OC_STATUS_OK) || code.equals(OCStatus.OC_STATUS_CHANGED)) {
LOG.debug("Provision identity certificate succeeded");
emitter.onComplete();
} else {
emitter.onError(new IOException("Provision identity certificate error"));
}
};
if (OCMain.initPost(OcfResourceUri.CRED_URI, ep, null, handler, OCQos.HIGH_QOS)) {
OcCredPrivateData privateData = new OcCredPrivateData();
privateData.setDataDer(symmetricKey);
privateData.setEncoding(OcfEncoding.OC_ENCODING_RAW);
OcCredential cred = new OcCredential();
cred.setSubjectuuid(targetUuid);
cred.setCredtype(OcfCredType.OC_CREDTYPE_PSK);
cred.setPrivateData(privateData);
List<OcCredential> credList = new ArrayList<>();
credList.add(cred);
OcCredentials creds = new OcCredentials();
creds.setCredList(credList);
CborEncoder root = creds.parseToCbor();
if (OCMain.doPost()) {
LOG.debug("Sent POST request to /oic/sec/cred");
} else {
String error = "Could not send POST request to /oic/sec/cred";
LOG.error(error);
emitter.onError(new Exception(error));
}
} else {
String error = "Could not init POST request to /oic/sec/cred";
LOG.error(error);
emitter.onError(new Exception(error));
}
OCEndpointUtil.freeEndpoint(ep);
});
}
public Completable provisionPairwiseCredential(String clientId, String serverId) {
return Completable.create(emitter -> {
OCUuid cliendDi = OCUuidUtil.stringToUuid(clientId);
......@@ -371,16 +162,12 @@ public class CmsRepository {
});
}
public Completable deleteCredential(String endpoint, String deviceId, long credId) {
public Completable deleteCredential(String deviceId, long credId) {
return Completable.create(emitter -> {
OCEndpoint ep = OCEndpointUtil.stringToEndpoint(endpoint, new String[1]);
OCUuid uuid = OCUuidUtil.stringToUuid(deviceId);
OCEndpointUtil.setDi(ep, uuid);
OCResponseHandler handler = (OCClientResponse response) -> {
OCStatus code = response.getCode();
if (code == OCStatus.OC_STATUS_OK
|| code == OCStatus.OC_STATUS_DELETED) {
OCObtStatusHandler handler = (int status) -> {
if (status >= 0) {
LOG.debug("Delete credential success");
emitter.onComplete();
} else {
......@@ -390,13 +177,22 @@ public class CmsRepository {
}
};
if (!OCMain.doDelete(OcfResourceUri.CRED_URI, ep, OcfResourceUri.DELETE_CRED_QUERY + credId, handler, OCQos.HIGH_QOS)) {
int ret = OCObt.deleteCredByCredId(uuid, (int)credId, handler);
if (ret >= 0) {
LOG.debug("Successfully issued request to DELETE /oic/sec/cred");
} else {
String error = "DELETE request to /oic/sec/cred error";
LOG.debug(error);
emitter.onError(new Exception(error));
}
});
}
OCEndpointUtil.freeEndpoint(ep);
public Single<OcCredentials> retrieveOwnCredentials() {
return Single.create(emitter -> {
OcCredentials creds = new OcCredentials();
creds.parseOCRepresentation(OCObt.retrieveOwnCreds());
emitter.onSuccess(creds);
});
}
......@@ -414,10 +210,17 @@ public class CmsRepository {
});
}
public Completable removeTrustAnchor(long device, long credid) {
public Completable removeTrustAnchor(long credid) {
return Completable.create(emitter -> {
OCPki.removeCredentialByCredid(device, (int)credid);
emitter.onComplete();
int ret = OCObt.deleteOwnCredByCredId((int)credid);
if (ret >= 0) {
LOG.debug("Successfully DELETED cred");
emitter.onComplete();
} else {
String error = "ERROR DELETING cred";
LOG.error(error);
emitter.onError(new Exception(error));
}
});
}
}
......@@ -20,7 +20,6 @@
package org.openconnectivity.otgc.data.repository;
import io.reactivex.Completable;
import io.reactivex.Maybe;
import io.reactivex.Observable;
import io.reactivex.Single;