Search in sources :

Example 1 with Request

use of co.krypt.krypton.protocol.Request in project krypton-android by kryptco.

the class Silo method onMessageJob.

private void onMessageJob(UUID pairingUUID, byte[] incoming, String communicationMedium) {
    try {
        NetworkMessage message = NetworkMessage.parse(incoming);
        Pairing pairing;
        synchronized (pairingsLock) {
            pairing = activePairingsByUUID.get(pairingUUID);
        }
        if (pairing == null) {
            Log.e(TAG, "not valid pairing: " + pairingUUID);
            return;
        }
        switch(message.header) {
            case CIPHERTEXT:
                byte[] json = pairing.unseal(message.message);
                Request request = JSON.fromJson(json, Request.class);
                handle(pairing, request, communicationMedium);
                break;
            case WRAPPED_KEY:
                break;
            case WRAPPED_PUBLIC_KEY:
                break;
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}
Also used : UnpairRequest(co.krypt.krypton.protocol.UnpairRequest) SignRequest(co.krypt.krypton.protocol.SignRequest) Request(co.krypt.krypton.protocol.Request) LogDecryptionRequest(co.krypt.krypton.protocol.LogDecryptionRequest) TeamOperationRequest(co.krypt.krypton.protocol.TeamOperationRequest) ReadTeamRequest(co.krypt.krypton.protocol.ReadTeamRequest) MeRequest(co.krypt.krypton.protocol.MeRequest) HostsRequest(co.krypt.krypton.protocol.HostsRequest) GitSignRequest(co.krypt.krypton.protocol.GitSignRequest) NetworkMessage(co.krypt.krypton.protocol.NetworkMessage) Pairing(co.krypt.krypton.pairing.Pairing) CryptoException(co.krypt.krypton.exception.CryptoException) TransportException(co.krypt.krypton.exception.TransportException) ProtocolException(co.krypt.krypton.exception.ProtocolException) SQLException(java.sql.SQLException) MismatchedHostKeyException(co.krypt.krypton.exception.MismatchedHostKeyException) IOException(java.io.IOException)

Example 2 with Request

use of co.krypt.krypton.protocol.Request in project krypton-android by kryptco.

the class Policy method onAction.

public static void onAction(final Context context, final String requestID, final String action) {
    Log.i(TAG, action + " requestID " + requestID);
    final Pair<Pairing, Request> pairingAndRequest;
    // Lock manually to prevent deadlock from Silo
    synchronized (Policy.class) {
        pairingAndRequest = pendingRequestCache.remove(requestID);
    }
    if (pairingAndRequest == null) {
        Log.e(TAG, "requestID " + requestID + " not pending");
        return;
    }
    Silo silo = Silo.shared(context);
    OpenDatabaseHelper db = silo.pairings().dbHelper;
    Notifications.clearRequest(context, pairingAndRequest.second);
    switch(action) {
        case APPROVE_ONCE:
            try {
                silo.respondToRequest(pairingAndRequest.first, pairingAndRequest.second, true);
                new Analytics(context).postEvent(pairingAndRequest.second.analyticsCategory(), "background approve", "once", null, false);
            } catch (Exception e) {
                e.printStackTrace();
            }
            break;
        case APPROVE_ALL_TEMPORARILY:
            try {
                pairingAndRequest.second.body.visit(new RequestBody.Visitor<Void, Unrecoverable>() {

                    @Override
                    public Void visit(MeRequest meRequest) throws Unrecoverable {
                        return null;
                    }

                    @Override
                    public Void visit(SignRequest signRequest) throws Unrecoverable {
                        try {
                            Approval.approveSSHAnyHost(db, pairingAndRequest.first.uuid);
                        } catch (IOException | SQLException e) {
                            throw new Unrecoverable(e);
                        }
                        return null;
                    }

                    @Override
                    public Void visit(GitSignRequest gitSignRequest) throws Unrecoverable {
                        gitSignRequest.body.visit(new GitSignRequestBody.Visitor<Void, Unrecoverable>() {

                            @Override
                            public Void visit(CommitInfo commit) throws Unrecoverable {
                                try {
                                    Approval.approveGitCommitSignatures(db, pairingAndRequest.first.uuid);
                                } catch (IOException | SQLException e) {
                                    throw new Unrecoverable(e);
                                }
                                return null;
                            }

                            @Override
                            public Void visit(TagInfo tag) throws Unrecoverable {
                                try {
                                    Approval.approveGitTagSignatures(db, pairingAndRequest.first.uuid);
                                } catch (IOException | SQLException e) {
                                    throw new Unrecoverable(e);
                                }
                                return null;
                            }
                        });
                        return null;
                    }

                    @Override
                    public Void visit(UnpairRequest unpairRequest) throws Unrecoverable {
                        return null;
                    }

                    @Override
                    public Void visit(HostsRequest hostsRequest) throws Unrecoverable {
                        return null;
                    }

                    @Override
                    public Void visit(ReadTeamRequest readTeamRequest) throws Unrecoverable {
                        try {
                            Approval.approveReadTeamData(db, pairingAndRequest.first.uuid);
                        } catch (IOException | SQLException e) {
                            throw new Unrecoverable(e);
                        }
                        return null;
                    }

                    @Override
                    public Void visit(LogDecryptionRequest logDecryptionRequest) throws Unrecoverable {
                        try {
                            Approval.approveReadTeamData(db, pairingAndRequest.first.uuid);
                        } catch (IOException | SQLException e) {
                            throw new Unrecoverable(e);
                        }
                        return null;
                    }

                    @Override
                    public Void visit(TeamOperationRequest teamOperationRequest) throws Unrecoverable {
                        return null;
                    }
                });
                silo.respondToRequest(pairingAndRequest.first, pairingAndRequest.second, true);
                new Analytics(context).postEvent(pairingAndRequest.second.analyticsCategory(), "background approve", "time", (int) temporaryApprovalSeconds(context, pairingAndRequest.second), false);
            } catch (Exception e) {
                e.printStackTrace();
            }
            break;
        case APPROVE_THIS_TEMPORARILY:
            try {
                pairingAndRequest.second.body.visit(new RequestBody.Visitor<Void, Unrecoverable>() {

                    @Override
                    public Void visit(MeRequest meRequest) throws Unrecoverable {
                        return null;
                    }

                    @Override
                    public Void visit(SignRequest signRequest) throws Unrecoverable {
                        String user = signRequest.user();
                        if (signRequest.hostNameVerified && signRequest.hostAuth.hostNames.length > 0) {
                            try {
                                Approval.approveSSHUserHost(db, pairingAndRequest.first.uuid, user, signRequest.hostAuth.hostNames[0]);
                            } catch (IOException | SQLException e) {
                                throw new Unrecoverable(e);
                            }
                        }
                        return null;
                    }

                    @Override
                    public Void visit(GitSignRequest gitSignRequest) throws Unrecoverable {
                        return null;
                    }

                    @Override
                    public Void visit(UnpairRequest unpairRequest) throws Unrecoverable {
                        return null;
                    }

                    @Override
                    public Void visit(HostsRequest hostsRequest) throws Unrecoverable {
                        return null;
                    }

                    @Override
                    public Void visit(ReadTeamRequest readTeamRequest) throws Unrecoverable {
                        return null;
                    }

                    @Override
                    public Void visit(LogDecryptionRequest logDecryptionRequest) throws Unrecoverable {
                        return null;
                    }

                    @Override
                    public Void visit(TeamOperationRequest teamOperationRequest) throws Unrecoverable {
                        return null;
                    }
                });
                silo.respondToRequest(pairingAndRequest.first, pairingAndRequest.second, true);
                new Analytics(context).postEvent(pairingAndRequest.second.analyticsCategory(), "background approve this", "time", (int) temporaryApprovalSeconds(context, pairingAndRequest.second), false);
            } catch (Exception e) {
                e.printStackTrace();
            }
        case REJECT:
            try {
                silo.respondToRequest(pairingAndRequest.first, pairingAndRequest.second, false);
                new Analytics(context).postEvent(pairingAndRequest.second.analyticsCategory(), "background reject", null, null, false);
            } catch (Unrecoverable e) {
                e.printStackTrace();
            }
            break;
    }
}
Also used : MeRequest(co.krypt.krypton.protocol.MeRequest) SQLException(java.sql.SQLException) Unrecoverable(co.krypt.krypton.exception.Unrecoverable) TeamOperationRequest(co.krypt.krypton.protocol.TeamOperationRequest) OpenDatabaseHelper(co.krypt.krypton.db.OpenDatabaseHelper) GitSignRequest(co.krypt.krypton.protocol.GitSignRequest) TagInfo(co.krypt.krypton.git.TagInfo) CommitInfo(co.krypt.krypton.git.CommitInfo) Pairing(co.krypt.krypton.pairing.Pairing) Silo(co.krypt.krypton.silo.Silo) RequestBody(co.krypt.krypton.protocol.RequestBody) GitSignRequestBody(co.krypt.krypton.protocol.GitSignRequestBody) SignRequest(co.krypt.krypton.protocol.SignRequest) GitSignRequest(co.krypt.krypton.protocol.GitSignRequest) ReadTeamRequest(co.krypt.krypton.protocol.ReadTeamRequest) UnpairRequest(co.krypt.krypton.protocol.UnpairRequest) SignRequest(co.krypt.krypton.protocol.SignRequest) MeRequest(co.krypt.krypton.protocol.MeRequest) Request(co.krypt.krypton.protocol.Request) LogDecryptionRequest(co.krypt.krypton.protocol.LogDecryptionRequest) HostsRequest(co.krypt.krypton.protocol.HostsRequest) GitSignRequest(co.krypt.krypton.protocol.GitSignRequest) TeamOperationRequest(co.krypt.krypton.protocol.TeamOperationRequest) HostsRequest(co.krypt.krypton.protocol.HostsRequest) IOException(java.io.IOException) UnpairRequest(co.krypt.krypton.protocol.UnpairRequest) Analytics(co.krypt.krypton.analytics.Analytics) SQLException(java.sql.SQLException) IOException(java.io.IOException) ReadTeamRequest(co.krypt.krypton.protocol.ReadTeamRequest) LogDecryptionRequest(co.krypt.krypton.protocol.LogDecryptionRequest)

Example 3 with Request

use of co.krypt.krypton.protocol.Request in project krypton-android by kryptco.

the class JSONUnitTest method gitSignRequestDeserialization_works.

@Test
public void gitSignRequestDeserialization_works() throws Exception {
    String json = "{\"request_id\":\"132\", \"unix_seconds\":0, \"v\": \"1.0.0\", \"git_sign_request\":{\"user_id\": \"kevin\", " + "\"commit\": {\"tree\":\"7fe58682fc6e3cb5e90f77f74ae479eb41d2a13a\", \"author\": \"John Doe <jd@example.com>\", \"committer\": \"John Doe <jd@example.com>\", \"message\": \"bWVzc2FnZQo=\"}}}";
    Request request = JSON.fromJson(json, Request.class);
    Assert.assertTrue(request.body instanceof GitSignRequest);
    Assert.assertTrue(((GitSignRequest) request.body).body instanceof CommitInfo);
}
Also used : GitSignRequest(co.krypt.krypton.protocol.GitSignRequest) Request(co.krypt.krypton.protocol.Request) GitSignRequest(co.krypt.krypton.protocol.GitSignRequest) SignRequest(co.krypt.krypton.protocol.SignRequest) CommitInfo(co.krypt.krypton.git.CommitInfo) Test(org.junit.Test)

Example 4 with Request

use of co.krypt.krypton.protocol.Request in project krypton-android by kryptco.

the class JSONUnitTest method nestedRequestDeserialization_works.

@Test
public void nestedRequestDeserialization_works() throws Exception {
    String json = "{\"request_id\":\"132\", \"unix_seconds\":0, \"sign_request\":{\"public_key_fingerprint\":\"aGkK\", \"data\": \"aGkK\"}, \"v\": \"1.0.0\"}";
    Request request = JSON.fromJson(json, Request.class);
    if (!(request.body instanceof SignRequest)) {
        throw new JsonParseException("expected sign request");
    }
}
Also used : GitSignRequest(co.krypt.krypton.protocol.GitSignRequest) SignRequest(co.krypt.krypton.protocol.SignRequest) Request(co.krypt.krypton.protocol.Request) GitSignRequest(co.krypt.krypton.protocol.GitSignRequest) SignRequest(co.krypt.krypton.protocol.SignRequest) JsonParseException(com.google.gson.JsonParseException) Test(org.junit.Test)

Example 5 with Request

use of co.krypt.krypton.protocol.Request in project krypton-android by kryptco.

the class ApprovalDialog method showApprovalDialog.

public static void showApprovalDialog(final Activity activity, final String requestID) {
    Pair<Pairing, Request> pendingRequestAndPairing = Policy.getPendingRequestAndPairing(requestID);
    if (pendingRequestAndPairing == null) {
        Log.e(TAG, "user clicked notification for unknown request");
        return;
    }
    Pairing pairing = pendingRequestAndPairing.first;
    Request request = pendingRequestAndPairing.second;
    AlertDialog.Builder builder = new AlertDialog.Builder(activity);
    builder.setIcon(R.mipmap.ic_launcher);
    // setPositiveButton: right button
    // setNeutralButton: left button
    // setNegativeButton: middle button
    long temporaryApprovalSeconds = Policy.temporaryApprovalSeconds(activity, request);
    boolean temporaryApprovalEnabled = temporaryApprovalSeconds > 0;
    String temporaryApprovalDuration = Policy.temporaryApprovalDuration(activity, request);
    request.body.visit(new RequestBody.Visitor<Void, RuntimeException>() {

        @Override
        public Void visit(MeRequest meRequest) throws RuntimeException {
            return null;
        }

        @Override
        public Void visit(SignRequest signRequest) throws RuntimeException {
            builder.setPositiveButton("Once", (dialog, id) -> Policy.onAction(activity.getApplicationContext(), requestID, Policy.APPROVE_ONCE));
            if (temporaryApprovalEnabled) {
                builder.setNeutralButton("All for " + temporaryApprovalDuration, (dialog, id) -> Policy.onAction(activity.getApplicationContext(), requestID, Policy.APPROVE_ALL_TEMPORARILY));
                if (signRequest.hostNameVerified) {
                    builder.setNegativeButton("This host for " + temporaryApprovalDuration, (dialog, id) -> Policy.onAction(activity.getApplicationContext(), requestID, Policy.APPROVE_THIS_TEMPORARILY));
                }
            }
            return null;
        }

        @Override
        public Void visit(GitSignRequest gitSignRequest) throws RuntimeException {
            builder.setPositiveButton("Once", (dialog, id) -> Policy.onAction(activity.getApplicationContext(), requestID, Policy.APPROVE_ONCE));
            if (temporaryApprovalEnabled) {
                builder.setNeutralButton("All for " + temporaryApprovalDuration, (dialog, id) -> Policy.onAction(activity.getApplicationContext(), requestID, Policy.APPROVE_ALL_TEMPORARILY));
            }
            return null;
        }

        @Override
        public Void visit(UnpairRequest unpairRequest) throws RuntimeException {
            return null;
        }

        @Override
        public Void visit(HostsRequest hostsRequest) throws RuntimeException {
            builder.setPositiveButton("Allow", (dialog, id) -> Policy.onAction(activity.getApplicationContext(), requestID, Policy.APPROVE_ONCE));
            if (temporaryApprovalEnabled) {
                builder.setNeutralButton("All for " + temporaryApprovalDuration, (dialog, id) -> Policy.onAction(activity.getApplicationContext(), requestID, Policy.APPROVE_ALL_TEMPORARILY));
            }
            return null;
        }

        @Override
        public Void visit(ReadTeamRequest readTeamRequest) throws RuntimeException {
            builder.setPositiveButton("Allow for " + temporaryApprovalDuration, (dialog, id) -> Policy.onAction(activity.getApplicationContext(), requestID, Policy.APPROVE_ALL_TEMPORARILY));
            return null;
        }

        @Override
        public Void visit(LogDecryptionRequest logDecryptionRequest) throws RuntimeException {
            builder.setPositiveButton("Allow for " + temporaryApprovalDuration, (dialog, id) -> Policy.onAction(activity.getApplicationContext(), requestID, Policy.APPROVE_ALL_TEMPORARILY));
            return null;
        }

        @Override
        public Void visit(TeamOperationRequest teamOperationRequest) throws RuntimeException {
            builder.setPositiveButton("Allow", (dialog, id) -> Policy.onAction(activity.getApplicationContext(), requestID, Policy.APPROVE_ONCE));
            return null;
        }
    });
    builder.setOnDismissListener(dialogInterface -> {
        Policy.onAction(activity.getApplicationContext(), requestID, Policy.REJECT);
    });
    View requestView = activity.getLayoutInflater().inflate(R.layout.request, null);
    TextView workstationNameText = (TextView) requestView.findViewById(R.id.workstationName);
    workstationNameText.setText(pairing.workstationName);
    ConstraintLayout content = (ConstraintLayout) requestView.findViewById(R.id.content);
    request.fillView(content);
    builder.setView(requestView);
    builder.create().show();
}
Also used : AlertDialog(android.support.v7.app.AlertDialog) ReadTeamRequest(co.krypt.krypton.protocol.ReadTeamRequest) UnpairRequest(co.krypt.krypton.protocol.UnpairRequest) HostsRequest(co.krypt.krypton.protocol.HostsRequest) SignRequest(co.krypt.krypton.protocol.SignRequest) Pairing(co.krypt.krypton.pairing.Pairing) MeRequest(co.krypt.krypton.protocol.MeRequest) Request(co.krypt.krypton.protocol.Request) RequestBody(co.krypt.krypton.protocol.RequestBody) AlertDialog(android.support.v7.app.AlertDialog) TextView(android.widget.TextView) Pair(android.support.v4.util.Pair) GitSignRequest(co.krypt.krypton.protocol.GitSignRequest) View(android.view.View) LogDecryptionRequest(co.krypt.krypton.protocol.LogDecryptionRequest) ConstraintLayout(android.support.constraint.ConstraintLayout) Activity(android.app.Activity) R(co.krypt.krypton.R) Policy(co.krypt.krypton.policy.Policy) TeamOperationRequest(co.krypt.krypton.protocol.TeamOperationRequest) Log(android.util.Log) MeRequest(co.krypt.krypton.protocol.MeRequest) TeamOperationRequest(co.krypt.krypton.protocol.TeamOperationRequest) ConstraintLayout(android.support.constraint.ConstraintLayout) GitSignRequest(co.krypt.krypton.protocol.GitSignRequest) TextView(android.widget.TextView) Pairing(co.krypt.krypton.pairing.Pairing) RequestBody(co.krypt.krypton.protocol.RequestBody) SignRequest(co.krypt.krypton.protocol.SignRequest) GitSignRequest(co.krypt.krypton.protocol.GitSignRequest) ReadTeamRequest(co.krypt.krypton.protocol.ReadTeamRequest) UnpairRequest(co.krypt.krypton.protocol.UnpairRequest) HostsRequest(co.krypt.krypton.protocol.HostsRequest) SignRequest(co.krypt.krypton.protocol.SignRequest) MeRequest(co.krypt.krypton.protocol.MeRequest) Request(co.krypt.krypton.protocol.Request) GitSignRequest(co.krypt.krypton.protocol.GitSignRequest) LogDecryptionRequest(co.krypt.krypton.protocol.LogDecryptionRequest) TeamOperationRequest(co.krypt.krypton.protocol.TeamOperationRequest) HostsRequest(co.krypt.krypton.protocol.HostsRequest) UnpairRequest(co.krypt.krypton.protocol.UnpairRequest) TextView(android.widget.TextView) View(android.view.View) ReadTeamRequest(co.krypt.krypton.protocol.ReadTeamRequest) LogDecryptionRequest(co.krypt.krypton.protocol.LogDecryptionRequest)

Aggregations

GitSignRequest (co.krypt.krypton.protocol.GitSignRequest)5 Request (co.krypt.krypton.protocol.Request)5 SignRequest (co.krypt.krypton.protocol.SignRequest)5 Pairing (co.krypt.krypton.pairing.Pairing)3 HostsRequest (co.krypt.krypton.protocol.HostsRequest)3 LogDecryptionRequest (co.krypt.krypton.protocol.LogDecryptionRequest)3 MeRequest (co.krypt.krypton.protocol.MeRequest)3 ReadTeamRequest (co.krypt.krypton.protocol.ReadTeamRequest)3 TeamOperationRequest (co.krypt.krypton.protocol.TeamOperationRequest)3 UnpairRequest (co.krypt.krypton.protocol.UnpairRequest)3 CommitInfo (co.krypt.krypton.git.CommitInfo)2 RequestBody (co.krypt.krypton.protocol.RequestBody)2 IOException (java.io.IOException)2 SQLException (java.sql.SQLException)2 Test (org.junit.Test)2 Activity (android.app.Activity)1 ConstraintLayout (android.support.constraint.ConstraintLayout)1 Pair (android.support.v4.util.Pair)1 AlertDialog (android.support.v7.app.AlertDialog)1 Log (android.util.Log)1