Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 53f217f5fe972516793f8acecb72dfc73e8bf7e3 (plain) (blame)
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
/*******************************************************************************
 * Copyright (c) 2008, 2017  Jay Rosenthal and others.
 * 
 *
 * This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License 2.0
 * which accompanies this distribution, and is available at
 * https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 * 
 * Contributors:
 *     Jay Rosenthal - initial API and implementation
 *******************************************************************************/

package org.eclipse.equinox.internal.provisional.security.ui;

import java.util.*;
import javax.security.auth.x500.X500Principal;

/**
 * X500PrincipalHelper
 * <p>
 * Helper class to extract pieces (attributes) of an X500Principal object for display
 * in the UI.
 * <p>
 * This helper uses the X500Principal.RFC2253 format of X500Principal.getname() to parse an X500Principal name 
 *   into it's component parts. 
 *  <p> 
 * In principals which contain multiple occurrences of the same attribute,the default for all the methods 
 *  is to return the most significant (first) attribute found. 
 *    
 */

public class X500PrincipalHelper {
	public static int LEASTSIGNIFICANT = 0;
	public static int MOSTSIGNIFICANT = 1;

	public final static String attrCN = "CN"; //$NON-NLS-1$
	public final static String attrOU = "OU"; //$NON-NLS-1$
	public final static String attrO = "O"; //$NON-NLS-1$
	public final static String attrC = "C"; //$NON-NLS-1$
	public final static String attrL = "L"; //$NON-NLS-1$
	public final static String attrST = "ST"; //$NON-NLS-1$
	public final static String attrSTREET = "STREET";//$NON-NLS-1$
	public final static String attrEMAIL = "EMAILADDRESS"; //$NON-NLS-1$
	public final static String attrUID = "UID"; //$NON-NLS-1$

	ArrayList<List<String>> rdnNameArray = new ArrayList<>();

	private final static String attrTerminator = "="; //$NON-NLS-1$

	public X500PrincipalHelper() {
		// Do nothing constructor..
		// Wont be useful unless setPrincipal is called...
	}

	public X500PrincipalHelper(X500Principal principal) {
		parseDN(principal.getName(X500Principal.RFC2253));
	}

	/**
	 *  Set the X500Principal name object to be parsed.
	 *  <p>
	 * @param principal - X500Principal
	 */
	public void setPrincipal(X500Principal principal) {
		parseDN(principal.getName(X500Principal.RFC2253));
	}

	/**
	 *   Gets the most significant common name (CN) attribute from the given
	 *      X500Principal object.
	 *   For names that contains multiple attributes of this type.  The first
	 *      (most significant) one will be returned   
	 * <p> 
	 * @return the Most significant common name attribute.
	 * 
	 */
	public String getCN() {
		return findPart(attrCN);
	}

	/**
	 *   Gets the most significant Organizational Unit (OU) attribute from the given
	 *      X500Principal object.
	 *   For names that contains multiple attributes of this type.  The first
	 *      (most significant) one will be returned       
	 * <p> 
	 * 
	 * @return the Most significant OU attribute.
	 * 
	 */

	public String getOU() {
		return findPart(attrOU);

	}

	/**
	 *   Gets the most significant Organization (O) attribute from the given
	 *      X500Principal object.
	 *   For names that contains multiple attributes of this type.  The first
	 *      (most significant) one will be returned       
	 * <p> 
	 * 
	 * @return the Most significant O attribute.
	 * 
	 */

	public String getO() {
		return findPart(attrO);

	}

	/**
	 *   Gets the Country (C) attribute from the given
	 *      X500Principal object.      
	 * <p> 
	 * 
	 * @return the C attribute.
	 * 
	 */
	public String getC() {
		return findPart(attrC);
	}

	/**
	 *   Gets the Locale (L) attribute from the given
	 *      X500Principal object.      
	 * <p> 
	 * 
	 * @return the L attribute.
	 * 
	 */
	public String getL() {
		return findPart(attrL);
	}

	/**
	 *   Gets the State (ST) attribute from the given
	 *      X500Principal object.      
	 * <p> 
	 * 
	 * @return the ST attribute.
	 * 
	 */
	public String getST() {
		return findPart(attrST);
	}

	/**
	 *   Gets the Street (STREET) attribute from the given
	 *      X500Principal object.      
	 * <p> 
	 * 
	 * @return the STREET attribute.
	 * 
	 */
	public String getSTREET() {
		return findPart(attrSTREET);
	}

	/**
	 *   Gets the Email Address (EMAILADDRESS) attribute from the given
	 *      X500Principal object.      
	 * <p> 
	 * 
	 * @return the EMAILADDRESS attribute.
	 * 
	 */
	public String getEMAILDDRESS() {
		return findPart(attrEMAIL);
	}

	public String getUID() {
		return findPart(attrUID);
	}

	/**
	 * Derived From: org.eclipse.osgi.internal.verifier - DNChainMatching.java
	 * 
	 * Takes a distinguished name in canonical form and fills in the rdnArray
	 * with the extracted RDNs.
	 * 
	 * @param dn the distinguished name in canonical form.
	 * @throws IllegalArgumentException if a formatting error is found.
	 */
	private void parseDN(String dn) throws IllegalArgumentException {
		int startIndex = 0;
		char c = '\0';
		ArrayList<String> nameValues = new ArrayList<>();

		// Clear the existing array, in case this instance is being re-used
		rdnNameArray.clear();

		while (startIndex < dn.length()) {
			int endIndex;
			StringBuilder value = new StringBuilder();
			for (endIndex = startIndex; endIndex < dn.length(); endIndex++) {
				c = dn.charAt(endIndex);
				if (c == ',' || c == '+')
					break;
				if (c == '\\') {
					endIndex++; // skip the escaped char
				} else {
					value.append(c);
				}
			}

			if (endIndex > dn.length())
				throw new IllegalArgumentException("unterminated escape " + dn); //$NON-NLS-1$

			nameValues.add(value.toString());

			if (c != '+') {
				rdnNameArray.add(nameValues);
				if (endIndex != dn.length())
					nameValues = new ArrayList<>();
				else
					nameValues = null;
			}
			startIndex = endIndex + 1;
		}
		if (nameValues != null) {
			throw new IllegalArgumentException("improperly terminated DN " + dn); //$NON-NLS-1$
		}
	}

	/**
	 * Returns an ArrayList containing all the values for the given attribute identifier.
	 * <p>
	 * @param  attributeID  String containing the X500 name attribute whose values are to be 
	 *      returned
	 * @return ArrayList containing the string values of the requested attribute. Values are in   
	 *      the order they occur.  May be empty.
	 * 
	 */
	public ArrayList<String> getAllValues(String attributeID) {
		ArrayList<String> retList = new ArrayList<>();
		String searchPart = attributeID + attrTerminator;

		for (List<String> nameList : rdnNameArray) {
			String namePart = nameList.get(0);

			if (namePart.startsWith(searchPart)) {
				// Return the string starting after the ID string and the = sign that follows it.
				retList.add(namePart.toString().substring(searchPart.length()));
			}
		}

		return retList;

	}

	private String findPart(String attributeID) {
		return findSignificantPart(attributeID, MOSTSIGNIFICANT);
	}

	private String findSignificantPart(String attributeID, int significance) {
		String retNamePart = null;
		String searchPart = attributeID + attrTerminator;

		for (List<String> nameList : rdnNameArray) {
			String namePart = nameList.get(0);

			if (namePart.startsWith(searchPart)) {
				// Return the string starting after the ID string and the = sign that follows it.
				retNamePart = namePart.toString().substring(searchPart.length());
				//  By definition the first one is most significant
				if (significance == MOSTSIGNIFICANT)
					break;
			}
		}

		return retNamePart;
	}

}

Back to the top