Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adicionando segurança ao projeto #1

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .idea/gradle.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 0 additions & 10 deletions .idea/runConfigurations.xml

This file was deleted.

82 changes: 81 additions & 1 deletion app/src/main/java/br/ufma/lsdi/cddlbaseproject/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ public class MainActivity extends AppCompatActivity {
private String host;
private CDDL cddl;
private ConnectionImpl connection;
private String CLIENT_ID = "app";

@Override
protected void onCreate(Bundle savedInstanceState) {
Expand Down Expand Up @@ -87,7 +88,7 @@ private void initCDDL() {

//host = CDDL.startMicroBroker();
connection = ConnectionFactory.createConnection();
connection.setClientId("app");
connection.setClientId(this.CLIENT_ID);
connection.setHost(host);
connection.addConnectionListener(connectionListener);
connection.setEnableIntermediateBuffer(true);
Expand All @@ -99,6 +100,85 @@ private void initCDDL() {
cddl.startCommunicationTechnology(CDDL.INTERNAL_TECHNOLOGY_ID);
}

private void secureInitCDDL() {
/*
Ip do Broker MQTT
Para você utilizar um Broker externo ou na sua máquina, você deve
configurar o proxy do emulador manualmente e colocar o ip referente
ao Broker.
Este método utiliza o cddl no modo seguro.
*/
host = "192.168.18.12";

// host = CDDL.startSecureMicroBroker(getApplicationContext(), true );

connection = ConnectionFactory.createConnection();
connection.setClientId(this.CLIENT_ID);
connection.setHost(host);
connection.addConnectionListener(connectionListener);
connection.setEnableIntermediateBuffer(true);
connection.secureConnect(getApplicationContext());
cddl = CDDL.getInstance();
cddl.setConnection(connection);
cddl.setContext(this);
cddl.startService();
cddl.startCommunicationTechnology(CDDL.INTERNAL_TECHNOLOGY_ID);
}



/*
Método para gerar a requisição de certificado digital;
*/
public void generateCSR(String nomeComum,
String unidadeOrganizacional,
String organizacao,
String cidade,
String estado,
String pais){
SecurityService securityService = CDDL
.getSecurityServiceInstance(getApplicationContext());

securityService
.generateCSR(
nomeComum,
unidadeOrganizacional,
organizacao,
cidade,
estado,
pais);

}

/*
Método para importar o certificado assinado da autoridade certificadora e certificado do cliente
*/
public void importClientAndCaCertificate(String caCertFileName, String clientCertFileName ){
try {
securityService.setCaCertificate(caCertFileName);
securityService.setCertificate(clientCertFileName);

} catch (FileNotFoundException e) {
e.printStackTrace();
}
}

/*
Método para adicionar as regras de acesso a todos os tópicos ao microbroker para um cliente específico
*/
public void importClientAndCaCertificate(String clientID, String clientCertFileName ){
try {


securityService.grantReadPermissionByCDDLTopic(clientID, SecurityService.ALL_TOPICS);
securityService.grantWritePermissionByCDDLTopic(clientID,SecurityService.ALL_TOPICS);

} catch (FileNotFoundException e) {
e.printStackTrace();
}
}


@Override
protected void onDestroy() {
cddl.stopAllCommunicationTechnologies();;
Expand Down