aboutsummaryrefslogtreecommitdiffstats
path: root/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/linked/LinkedCookieResource.java
blob: 84b79920ad36447ad20ad66d1f241378d8b67547 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
package org.sufficientlysecure.keychain.pgp.linked;

import android.content.Context;

import org.sufficientlysecure.keychain.Constants;
import org.sufficientlysecure.keychain.operations.results.LinkedVerifyResult;
import org.sufficientlysecure.keychain.operations.results.OperationResult.LogType;
import org.sufficientlysecure.keychain.operations.results.OperationResult.OperationLog;
import org.sufficientlysecure.keychain.ui.util.KeyFormattingUtils;
import org.sufficientlysecure.keychain.util.Log;

import java.net.URI;
import java.util.HashMap;
import java.util.Map.Entry;
import java.util.Set;
import java.util.regex.Matcher;

public abstract class LinkedCookieResource extends LinkedResource {

    protected LinkedCookieResource(Set<String> flags, HashMap<String, String> params, URI uri) {
        super(flags, params, uri);
    }

    public URI toUri () {

        StringBuilder b = new StringBuilder();
        b.append("pgpid+cookie:");

        // add flags
        if (mFlags != null) {
            boolean first = true;
            for (String flag : mFlags) {
                if (!first) {
                    b.append(";");
                }
                first = false;
                b.append(flag);
            }
        }

        // add parameters
        if (mParams != null) {
            boolean first = true;
            for (Entry<String, String> stringStringEntry : mParams.entrySet()) {
                if (!first) {
                    b.append(";");
                }
                first = false;
                b.append(stringStringEntry.getKey()).append("=").append(stringStringEntry.getValue());
            }
        }

        b.append("@");
        b.append(mSubUri);

        return URI.create(b.toString());

    }

    public URI getSubUri () {
        return mSubUri;
    }

    public static String generate (Context context, byte[] fingerprint, int nonce) {
        return String.format("[Verifying my PGP key: openpgp4fpr:%s#%08x]",
                KeyFormattingUtils.convertFingerprintToHex(fingerprint), nonce);
    }

    public static String generatePreview () {
        return "[Verifying my PGP key: openpgp4fpr:0x…]";
    }

    public LinkedVerifyResult verify(byte[] fingerprint, int nonce) {

        OperationLog log = new OperationLog();
        log.add(LogType.MSG_LV, 0);

        // Try to fetch resource. Logs for itself
        String res = fetchResource(log, 1);
        if (res == null) {
            // if this is null, an error was recorded in fetchResource above
            return new LinkedVerifyResult(LinkedVerifyResult.RESULT_ERROR, log);
        }

        Log.d(Constants.TAG, "Resource data: '" + res + "'");

        return verifyString(log, 1, res, nonce, fingerprint);

    }

    protected abstract String fetchResource (OperationLog log, int indent);

    protected Matcher matchResource (OperationLog log, int indent, String res) {
        return magicPattern.matcher(res);
    }

    protected LinkedVerifyResult verifyString (OperationLog log, int indent,
                                               String res,
                                               int nonce, byte[] fingerprint) {

        log.add(LogType.MSG_LV_MATCH, indent);
        Matcher match = matchResource(log, indent+1, res);
        if (!match.find()) {
            log.add(LogType.MSG_LV_MATCH_ERROR, 2);
            return new LinkedVerifyResult(LinkedVerifyResult.RESULT_ERROR, log);
        }

        String candidateFp = match.group(1).toLowerCase();
        try {
            int nonceCandidate = Integer.parseInt(match.group(2).toLowerCase(), 16);

            if (nonce != nonceCandidate) {
                log.add(LogType.MSG_LV_NONCE_ERROR, indent);
                return new LinkedVerifyResult(LinkedVerifyResult.RESULT_ERROR, log);
            }
        } catch (NumberFormatException e) {
            log.add(LogType.MSG_LV_NONCE_ERROR, indent);
            return new LinkedVerifyResult(LinkedVerifyResult.RESULT_ERROR, log);
        }

        String fp = KeyFormattingUtils.convertFingerprintToHex(fingerprint);

        if (!fp.equals(candidateFp)) {
            log.add(LogType.MSG_LV_FP_ERROR, indent);
            return new LinkedVerifyResult(LinkedVerifyResult.RESULT_ERROR, log);
        }
        log.add(LogType.MSG_LV_FP_OK, indent);

        log.add(LogType.MSG_LV_NONCE_OK, indent);
        return new LinkedVerifyResult(LinkedVerifyResult.RESULT_OK, log);

    }

}