|
| 1 | +/* |
| 2 | + * Copyright 2002-2025 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.springframework.security.web.authentication.preauth.x509; |
| 18 | + |
| 19 | +import java.security.cert.X509Certificate; |
| 20 | +import java.util.regex.Matcher; |
| 21 | +import java.util.regex.Pattern; |
| 22 | + |
| 23 | +import javax.security.auth.x500.X500Principal; |
| 24 | + |
| 25 | +import org.apache.commons.logging.Log; |
| 26 | +import org.apache.commons.logging.LogFactory; |
| 27 | + |
| 28 | +import org.springframework.context.MessageSource; |
| 29 | +import org.springframework.context.MessageSourceAware; |
| 30 | +import org.springframework.context.support.MessageSourceAccessor; |
| 31 | +import org.springframework.core.log.LogMessage; |
| 32 | +import org.springframework.security.authentication.BadCredentialsException; |
| 33 | +import org.springframework.security.core.SpringSecurityMessageSource; |
| 34 | +import org.springframework.util.Assert; |
| 35 | + |
| 36 | +/** |
| 37 | + * Obtains the principal from a certificate using RFC2253 and RFC1779 formats. By default, |
| 38 | + * RFC2253 is used: DN is extracted from CN. If extractPrincipalNameFromEmail is true then |
| 39 | + * format RFC1779 will be used: DN is extracted from EMAIlADDRESS. |
| 40 | + * |
| 41 | + * @author Max Batischev |
| 42 | + * @since 7.0 |
| 43 | + */ |
| 44 | +public final class SubjectX500PrincipalExtractor implements X509PrincipalExtractor, MessageSourceAware { |
| 45 | + |
| 46 | + private final Log logger = LogFactory.getLog(getClass()); |
| 47 | + |
| 48 | + private MessageSourceAccessor messages = SpringSecurityMessageSource.getAccessor(); |
| 49 | + |
| 50 | + private boolean extractPrincipalNameFromEmail = false; |
| 51 | + |
| 52 | + private final Pattern cnSubjectDnPattern = Pattern.compile("CN=(.*?)(?:,|$)", Pattern.CASE_INSENSITIVE); |
| 53 | + |
| 54 | + private final Pattern emailSubjectDnPattern = Pattern.compile("OID.1.2.840.113549.1.9.1=(.*?)(?:,|$)", |
| 55 | + Pattern.CASE_INSENSITIVE); |
| 56 | + |
| 57 | + @Override |
| 58 | + public Object extractPrincipal(X509Certificate clientCert) { |
| 59 | + Assert.notNull(clientCert, "clientCert cannot be null"); |
| 60 | + X500Principal principal = clientCert.getSubjectX500Principal(); |
| 61 | + String subjectDN = this.extractPrincipalNameFromEmail ? principal.getName("RFC1779") : principal.getName(); |
| 62 | + this.logger.debug(LogMessage.format("Subject DN is '%s'", subjectDN)); |
| 63 | + Matcher matcher = this.extractPrincipalNameFromEmail ? this.emailSubjectDnPattern.matcher(subjectDN) |
| 64 | + : this.cnSubjectDnPattern.matcher(subjectDN); |
| 65 | + if (!matcher.find()) { |
| 66 | + throw new BadCredentialsException(this.messages.getMessage("SubjectX500PrincipalExtractor.noMatching", |
| 67 | + new Object[] { subjectDN }, "No matching pattern was found in subject DN: {0}")); |
| 68 | + } |
| 69 | + String principalName = matcher.group(1); |
| 70 | + this.logger.debug(LogMessage.format("Extracted Principal name is '%s'", principalName)); |
| 71 | + return principalName; |
| 72 | + } |
| 73 | + |
| 74 | + @Override |
| 75 | + public void setMessageSource(MessageSource messageSource) { |
| 76 | + Assert.notNull(messageSource, "messageSource cannot be null"); |
| 77 | + this.messages = new MessageSourceAccessor(messageSource); |
| 78 | + } |
| 79 | + |
| 80 | + /** |
| 81 | + * If true then DN will be extracted from EMAIlADDRESS, defaults to {@code false} |
| 82 | + * @param extractPrincipalNameFromEmail whether to extract DN from EMAIlADDRESS |
| 83 | + */ |
| 84 | + public void setExtractPrincipalNameFromEmail(boolean extractPrincipalNameFromEmail) { |
| 85 | + this.extractPrincipalNameFromEmail = extractPrincipalNameFromEmail; |
| 86 | + } |
| 87 | + |
| 88 | +} |
0 commit comments