1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 package org.apache.james.mailet.crypto.matcher;
23
24 import java.security.Principal;
25 import java.security.cert.X509Certificate;
26 import java.util.Collection;
27 import java.util.Collections;
28 import java.util.Iterator;
29 import java.util.List;
30
31 import javax.mail.MessagingException;
32
33 import org.apache.mailet.base.GenericMatcher;
34 import org.apache.mailet.Mail;
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51 public class IsX509CertificateSubject extends GenericMatcher {
52 protected String sourceAttribute;
53 protected String check;
54
55 public void init() throws MessagingException {
56 String condition = getCondition();
57 if(condition == null || condition.indexOf(";") == -1)
58 throw new MessagingException("Invalid matcher configuration: "+condition);
59
60 int pos = condition.indexOf(";");
61 sourceAttribute = condition.substring(0,pos).trim();
62 check = condition.substring(pos+1, condition.length());
63 }
64
65 public Collection match(Mail mail) throws MessagingException {
66 List certificates;
67
68 Object obj = mail.getAttribute(sourceAttribute);
69 if (obj != null) {
70 if (obj instanceof X509Certificate) {
71 certificates = Collections.singletonList(obj);
72 } else {
73 certificates = (List) obj;
74 }
75
76 boolean valid = false;
77
78 for (Iterator iter = certificates.iterator(); iter.hasNext();) {
79 X509Certificate cert = (X509Certificate) iter.next();
80
81
82
83 Principal prin = cert.getSubjectDN();
84
85 if ((prin.toString().indexOf(check)) > 0) {
86 valid = true;
87 }
88 }
89
90 if (valid) {
91 return mail.getRecipients();
92 } else {
93 return null;
94 }
95 } else {
96 return null;
97 }
98 }
99
100 }
101