Skip to content

Commit d20cdb8

Browse files
committed
refactored out common code from LDAP classes
1 parent e380617 commit d20cdb8

File tree

3 files changed

+154
-158
lines changed

3 files changed

+154
-158
lines changed

prov/src/main/java/org/bouncycastle/jce/provider/X509LDAPCertStoreSpi.java

Lines changed: 5 additions & 96 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
import org.bouncycastle.asn1.ASN1InputStream;
3535
import org.bouncycastle.asn1.x509.CertificatePair;
3636
import org.bouncycastle.jce.X509LDAPCertStoreParameters;
37+
import org.bouncycastle.ldap.LDAPUtils;
3738
import org.bouncycastle.util.Strings;
3839

3940
/**
@@ -50,26 +51,6 @@
5051
public class X509LDAPCertStoreSpi
5152
extends CertStoreSpi
5253
{
53-
private static String[] FILTER_ESCAPE_TABLE = new String['\\' + 1];
54-
55-
static
56-
{
57-
// Filter encoding table -------------------------------------
58-
59-
// fill with char itself
60-
for (char c = 0; c < FILTER_ESCAPE_TABLE.length; c++)
61-
{
62-
FILTER_ESCAPE_TABLE[c] = String.valueOf(c);
63-
}
64-
65-
// escapes (RFC2254)
66-
FILTER_ESCAPE_TABLE['*'] = "\\2a";
67-
FILTER_ESCAPE_TABLE['('] = "\\28";
68-
FILTER_ESCAPE_TABLE[')'] = "\\29";
69-
FILTER_ESCAPE_TABLE['\\'] = "\\5c";
70-
FILTER_ESCAPE_TABLE[0] = "\\00";
71-
}
72-
7354
/**
7455
* Initial Context Factory.
7556
*/
@@ -124,42 +105,6 @@ private DirContext connectLDAP()
124105
return ctx;
125106
}
126107

127-
private String parseDN(String subject, String subjectAttributeName)
128-
{
129-
String temp = subject;
130-
int begin = Strings.toLowerCase(temp).indexOf(Strings.toLowerCase(subjectAttributeName));
131-
temp = temp.substring(begin + subjectAttributeName.length());
132-
int end = temp.indexOf(',');
133-
if (end == -1)
134-
{
135-
end = temp.length();
136-
}
137-
while (temp.charAt(end - 1) == '\\')
138-
{
139-
end = temp.indexOf(',', end + 1);
140-
if (end == -1)
141-
{
142-
end = temp.length();
143-
}
144-
}
145-
temp = temp.substring(0, end);
146-
begin = temp.indexOf('=');
147-
temp = temp.substring(begin + 1);
148-
if (temp.charAt(0) == ' ')
149-
{
150-
temp = temp.substring(1);
151-
}
152-
if (temp.startsWith("\""))
153-
{
154-
temp = temp.substring(1);
155-
}
156-
if (temp.endsWith("\""))
157-
{
158-
temp = temp.substring(0, temp.length() - 1);
159-
}
160-
return filterEncode(temp);
161-
}
162-
163108
public Collection engineGetCertificates(CertSelector selector)
164109
throws CertStoreException
165110
{
@@ -277,7 +222,7 @@ private Set certSubjectSerialSearch(X509CertSelector xselector,
277222
subject = xselector.getSubjectAsString();
278223
}
279224
}
280-
String attrValue = parseDN(subject, subjectAttributeName);
225+
String attrValue = LDAPUtils.parseDN(subject, subjectAttributeName);
281226
set.addAll(search(attrName, "*" + attrValue + "*", attrs));
282227
if (serial != null
283228
&& params.getSearchForSerialNumberIn() != null)
@@ -374,13 +319,13 @@ public Collection engineGetCRLs(CRLSelector selector)
374319
{
375320
String issuerAttributeName = params
376321
.getCertificateRevocationListIssuerAttributeName();
377-
attrValue = parseDN((String)o, issuerAttributeName);
322+
attrValue = LDAPUtils.parseDN((String)o, issuerAttributeName);
378323
}
379324
else
380325
{
381326
String issuerAttributeName = params
382327
.getCertificateRevocationListIssuerAttributeName();
383-
attrValue = parseDN(new X500Principal((byte[])o)
328+
attrValue = LDAPUtils.parseDN(new X500Principal((byte[])o)
384329
.getName("RFC1779"), issuerAttributeName);
385330
}
386331
set.addAll(search(attrName, "*" + attrValue + "*", attrs));
@@ -415,43 +360,7 @@ public Collection engineGetCRLs(CRLSelector selector)
415360

416361
return crlSet;
417362
}
418-
419-
/**
420-
* Escape a value for use in a filter.
421-
*
422-
* @param value the value to escape.
423-
* @return a properly escaped representation of the supplied value.
424-
*/
425-
private String filterEncode(String value)
426-
{
427-
if (value == null)
428-
{
429-
return null;
430-
}
431-
432-
// make buffer roomy
433-
StringBuilder encodedValue = new StringBuilder(value.length() * 2);
434-
435-
int length = value.length();
436-
437-
for (int i = 0; i < length; i++)
438-
{
439-
char c = value.charAt(i);
440-
441-
if (c < FILTER_ESCAPE_TABLE.length)
442-
{
443-
encodedValue.append(FILTER_ESCAPE_TABLE[c]);
444-
}
445-
else
446-
{
447-
// default: add the char
448-
encodedValue.append(c);
449-
}
450-
}
451-
452-
return encodedValue.toString();
453-
}
454-
363+
455364
/**
456365
* Returns a Set of byte arrays with the certificate or CRL encodings.
457366
*
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
package org.bouncycastle.ldap;
2+
3+
import org.bouncycastle.util.Strings;
4+
5+
/**
6+
* General utility methods for assisting with preparation of LDAP queries.
7+
*/
8+
public class LDAPUtils
9+
{
10+
private static String[] FILTER_ESCAPE_TABLE = new String['\\' + 1];
11+
12+
static
13+
{
14+
// Filter encoding table -------------------------------------
15+
16+
// fill with char itself
17+
for (char c = 0; c < FILTER_ESCAPE_TABLE.length; c++)
18+
{
19+
FILTER_ESCAPE_TABLE[c] = String.valueOf(c);
20+
}
21+
22+
// escapes (RFC2254)
23+
FILTER_ESCAPE_TABLE['*'] = "\\2a";
24+
FILTER_ESCAPE_TABLE['('] = "\\28";
25+
FILTER_ESCAPE_TABLE[')'] = "\\29";
26+
FILTER_ESCAPE_TABLE['\\'] = "\\5c";
27+
FILTER_ESCAPE_TABLE[0] = "\\00";
28+
}
29+
30+
/**
31+
* Parse out the contents of a particular subject attribute name from the string form of an X.500 DN.
32+
*
33+
* @param subject string form of an X.500 DN.
34+
* @param subjectAttributeName the RDN attribute name of interest.
35+
* @return an escaped string suitable for use in an LDAP query.
36+
*/
37+
public static String parseDN(String subject, String subjectAttributeName)
38+
{
39+
String temp = subject;
40+
int begin = Strings.toLowerCase(temp).indexOf(Strings.toLowerCase(subjectAttributeName));
41+
if (begin == -1)
42+
{
43+
return "";
44+
}
45+
temp = temp.substring(begin + subjectAttributeName.length());
46+
int end = temp.indexOf(',');
47+
if (end == -1)
48+
{
49+
end = temp.length();
50+
}
51+
while (temp.charAt(end - 1) == '\\')
52+
{
53+
end = temp.indexOf(',', end + 1);
54+
if (end == -1)
55+
{
56+
end = temp.length();
57+
}
58+
}
59+
temp = temp.substring(0, end);
60+
begin = temp.indexOf('=');
61+
temp = temp.substring(begin + 1);
62+
if (temp.charAt(0) == ' ')
63+
{
64+
temp = temp.substring(1);
65+
}
66+
if (temp.startsWith("\""))
67+
{
68+
temp = temp.substring(1);
69+
}
70+
if (temp.endsWith("\""))
71+
{
72+
temp = temp.substring(0, temp.length() - 1);
73+
}
74+
return filterEncode(temp);
75+
}
76+
77+
/**
78+
* Escape a value for use in a filter.
79+
*
80+
* @param value the value to escape.
81+
* @return a properly escaped representation of the supplied value.
82+
*/
83+
private static String filterEncode(String value)
84+
{
85+
if (value == null)
86+
{
87+
return null;
88+
}
89+
90+
// make buffer roomy
91+
StringBuilder encodedValue = new StringBuilder(value.length() * 2);
92+
93+
int length = value.length();
94+
95+
for (int i = 0; i < length; i++)
96+
{
97+
char c = value.charAt(i);
98+
99+
if (c < FILTER_ESCAPE_TABLE.length)
100+
{
101+
encodedValue.append(FILTER_ESCAPE_TABLE[c]);
102+
}
103+
else
104+
{
105+
// default: add the char
106+
encodedValue.append(c);
107+
}
108+
}
109+
110+
return encodedValue.toString();
111+
}
112+
}

0 commit comments

Comments
 (0)