-
Notifications
You must be signed in to change notification settings - Fork 49
Adding support for wildcard CN matching during client cert authentication #168
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
prsunny
merged 8 commits into
sonic-net:master
from
mramezani95:mramezani/cert_cn_subdomain_matching
Jan 14, 2026
+153
−12
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
72c511a
Adding support for wildcard CN matching during client cert authentica…
mramezani95 b0cb8b1
Merge branch 'master' into mramezani/cert_cn_subdomain_matching
prsunny dbdd26c
Added unit tests for client cert authentication.
mramezani95 d1ad198
Merge branch 'master' into mramezani/cert_cn_subdomain_matching
mramezani95 2cd7615
Merge branch 'mramezani/cert_cn_subdomain_matching' of github.com:mra…
mramezani95 9db551f
Added one more unit test.
mramezani95 0a943a6
Fix typo.
mramezani95 6457099
Moved the call to log.Printf so that the matching CN is also logged.
mramezani95 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,24 +1,32 @@ | ||
| package restapi | ||
|
|
||
| import ( | ||
| "log" | ||
| "net/http" | ||
| "log" | ||
| "net/http" | ||
| "strings" | ||
| ) | ||
|
|
||
| func CommonNameMatch(r *http.Request) bool { | ||
| //FIXME : in the authentication of client certificate, after the certificate chain is validated by | ||
| // TLS, here we will futher check if the common name of the end-entity certificate is in the trusted | ||
| // common name list of the server config. A more strict check may be added here later. | ||
| // During client cert authentication, after the certificate chain is validated by | ||
| // TLS, here we will further check if at least one of the common names in the end-entity certificate | ||
| // matches one of the trusted common names of the server config. | ||
| for _, peercert := range r.TLS.PeerCertificates { | ||
| commonName := peercert.Subject.CommonName | ||
| log.Printf("info: CommonName in the client cert: %s", commonName) | ||
| for _, name := range trustedCertCommonNames { | ||
| if commonName == name { | ||
| if strings.HasPrefix(name, "*") { | ||
| // wildcard common name matching | ||
| domain := name[1:] //strip "*" | ||
| if strings.HasSuffix(commonName, domain) { | ||
| log.Printf("info: CommonName %s in the client cert matches trusted wildcard common name %s", commonName, name) | ||
| return true; | ||
| } | ||
| } else if commonName == name { | ||
| return true; | ||
| } | ||
| } | ||
| log.Printf("info: CommonName in the client cert: %s", commonName) | ||
| } | ||
|
|
||
| log.Printf("error: Authentication Fail! None of the CommonNames in the client cert are found in trusted common names") | ||
| log.Printf("error: Authentication Fail! None of the CommonNames in the client cert match any of the trusted common names") | ||
| return false; | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.