summaryrefslogtreecommitdiffstats
blob: 0bc6a8dfaeb83187898b08ef19f7c693f8984f75 (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
135
136
137
138
139
140
141
142
143
144
145
146
/*
 * Copyright (c) 2010-2013 Isode Limited.
 * All rights reserved.
 * See the COPYING file for more information.
 */
/*
 * Copyright (c) 2015 Tarun Gupta.
 * Licensed under the simplified BSD license.
 * See Documentation/Licenses/BSD-simplified.txt for more information.
 */

package com.isode.stroke.sasl;

import com.isode.stroke.sasl.ClientAuthenticator;
import com.isode.stroke.base.SafeByteArray;
import com.isode.stroke.base.ByteArray;
import java.util.TreeMap;
import java.util.Map;
import java.util.Vector;

public class DIGESTMD5Properties {

	// Swiften uses std::multimap, but this is not required, so a TreeMap is used here.
	private TreeMap<String, ByteArray> properties = new TreeMap<String, ByteArray>();

	static boolean insideQuotes(final ByteArray v) {
		if (v.isEmpty()) {
			return false;
		}
		else if (v.getSize() == 1) {
			return v.getData()[0] == '"';
		}
		else if (v.getData()[0] == '"') {
			return v.getData()[v.getSize() - 1] != '"';
		}
		else {
			return false;
		}
	}

	static ByteArray stripQuotes(final ByteArray v) {
		String s = new String(v.getData()); // possibly with a charset
		int size = v.getSize();		
		int i = 0;
		if(s.charAt(0) == '"') {
			i++;
			size--;
		}
		if(s.charAt(v.getSize() - 1) == '"') {
			size--;
		}
		String data = s.substring(i, size+1);
		return new ByteArray(data);
	}

	public DIGESTMD5Properties() {

	}

	public String getValue(final String key) {
		if (properties.containsKey(key)) {
			return properties.get(key).toString();
		}
		else {
			return null;
		}
	}

	public void setValue(final String key, final String value) {
		if(!(properties.containsKey(key))) {
			properties.put(key, new ByteArray(value));
		}	
	}

	public ByteArray serialize() {
		ByteArray result = new ByteArray();
		for(Map.Entry<String, ByteArray> entry : properties.entrySet()) {
			if(entry.getKey() != properties.firstKey()) {
				result.append((byte)(','));
			}
			result.append(new ByteArray(entry.getKey()));
			result.append((byte)('='));
			if (isQuoted(entry.getKey())) {
				result.append(new ByteArray("\""));
				result.append(entry.getValue()); //It does not iterate over all values possible for this key. So no need for MultiMap. Also serialize test also indicates the same.
				result.append(new ByteArray("\""));
			}
			else {
				result.append(entry.getValue());
			}			
		}
		return result;
	}

	public static DIGESTMD5Properties parse(final ByteArray data) {
		DIGESTMD5Properties result = new DIGESTMD5Properties();
		boolean inKey = true;
		ByteArray currentKey = new ByteArray();
		ByteArray currentValue = new ByteArray();
		byte[] byteArrayData = data.getData();
		for (int i = 0; i < data.getSize(); ++i) {
			char c = (char)(byteArrayData[i]);
			if (inKey) {
				if (c == '=') {
					inKey = false;
				}
				else {
					currentKey.append((byte)(c));
				}
			}
			else {
				if (c == ',' && !insideQuotes(currentValue)) {
					String key = currentKey.toString();
					if (isQuoted(key)) {
						result.setValue(key, stripQuotes(currentValue).toString());
					}
					else {
						result.setValue(key, currentValue.toString());
					}
					inKey = true;
					currentKey = new ByteArray();
					currentValue = new ByteArray();
				}
				else {
					currentValue.append((byte)(c));
				}
			}
		}

		if (!currentKey.isEmpty()) {
			String key = currentKey.toString();
			if (isQuoted(key)) {
				result.setValue(key, stripQuotes(currentValue).toString());
			}
			else {
				result.setValue(key, currentValue.toString());
			}
		}

		return result;
	}

	private static boolean isQuoted(final String p) {
		return p.equals("authzid") || p.equals("cnonce") || p.equals("digest-uri") || p.equals("nonce") || p.equals("realm") || p.equals("username");
	}
}