aboutsummaryrefslogtreecommitdiffstats
path: root/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/linked/RawLinkedIdentity.java
blob: 931f2ec6bfca20e479a55e9c1f7f41d637c03ac7 (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
package org.sufficientlysecure.keychain.pgp.linked;

import org.spongycastle.bcpg.UserAttributeSubpacket;
import org.spongycastle.util.Strings;
import org.sufficientlysecure.keychain.Constants;
import org.sufficientlysecure.keychain.pgp.WrappedUserAttribute;
import org.sufficientlysecure.keychain.util.Log;

import java.net.URI;
import java.nio.ByteBuffer;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map.Entry;
import java.util.Set;

/** The RawLinkedIdentity contains raw parsed data from a Linked Identity subpacket. */
public class RawLinkedIdentity {

    public final int mNonce;
    public final URI mUri;

    protected RawLinkedIdentity(int nonce, URI uri) {
        mNonce = nonce;
        mUri = uri;
    }

    public byte[] getEncoded() {
        byte[] uriData = Strings.toUTF8ByteArray(mUri.toASCIIString());

        ByteBuffer buf = ByteBuffer.allocate(4 + uriData.length);

        buf.putInt(mNonce);
        buf.put(uriData);

        return buf.array();
    }

    /** This method parses a linked id from a UserAttributeSubpacket, or returns null if the
     * subpacket can not be parsed as a valid linked id.
     */
    static RawLinkedIdentity fromAttributeSubpacket(UserAttributeSubpacket subpacket) {
        if (subpacket.getType() != 100) {
            return null;
        }

        byte[] data = subpacket.getData();

        return fromSubpacketData(data);

    }

    public static RawLinkedIdentity fromSubpacketData(byte[] data) {

        try {
            int nonce = ByteBuffer.wrap(data).getInt();
            String uri = Strings.fromUTF8ByteArray(Arrays.copyOfRange(data, 4, data.length));

            return new RawLinkedIdentity(nonce, URI.create(uri));

        } catch (IllegalArgumentException e) {
            Log.e(Constants.TAG, "error parsing uri in (suspected) linked id packet");
            return null;
        }
    }

    public static RawLinkedIdentity fromResource (LinkedCookieResource res, int nonce) {
        return new RawLinkedIdentity(nonce, res.toUri());
    }

    public WrappedUserAttribute toUserAttribute () {
        return WrappedUserAttribute.fromSubpacket(WrappedUserAttribute.UAT_LINKED_ID, getEncoded());
    }

    public static String generateNonce() {
        // TODO make this actually random
        // byte[] data = new byte[4];
        // new SecureRandom().nextBytes(data);
        // return Hex.toHexString(data);

        // debug for now
        return "01234567";
    }

}