-
Notifications
You must be signed in to change notification settings - Fork 12
Implement SSH certificate authentication #10
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
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
3db8dfb
feat: Implement certificate authentication method in real
nedithgar 8187fe0
Add SSH configuration tests for certificate handling
nedithgar b9d4ac7
feat: Enhance user authentication documentation to include SSH certif…
nedithgar dbe0787
feat: Add support for custom acceptable critical options in SSH serve…
nedithgar e0502f0
fix: Update comments to clarify creation of real certificates in tests
nedithgar ab9c6b7
Merge pull request #1 from nedithgar/feat/certificate-authentication-…
nedithgar 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
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
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 | ||||
---|---|---|---|---|---|---|
|
@@ -19,13 +19,15 @@ struct UserAuthenticationStateMachine { | |||||
private var delegate: UserAuthDelegate | ||||||
private let loop: EventLoop | ||||||
private var sessionID: ByteBuffer | ||||||
private let role: SSHConnectionRole | ||||||
|
||||||
// TODO: The server SHOULD limit the number of authentication attempts the client may make. | ||||||
init(role: SSHConnectionRole, loop: EventLoop, sessionID: ByteBuffer) { | ||||||
self.state = .idle | ||||||
self.delegate = UserAuthDelegate(role: role) | ||||||
self.loop = loop | ||||||
self.sessionID = sessionID | ||||||
self.role = role | ||||||
} | ||||||
|
||||||
fileprivate static let serviceName: String = "ssh-userauth" | ||||||
|
@@ -414,8 +416,34 @@ private extension UserAuthenticationStateMachine { | |||||
return self.loop.makeSucceededFuture(.failure(.init(authentications: supportedMethods.strings, partialSuccess: false))) | ||||||
} | ||||||
|
||||||
// Check if this is a certificate and validate it | ||||||
var validatedCertificate: NIOSSHCertifiedPublicKey? = nil | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Using explicit 'nil' initialization is unnecessary in Swift. Consider using 'var validatedCertificate: NIOSSHCertifiedPublicKey?' instead.
Suggested change
Copilot uses AI. Check for mistakes. Positive FeedbackNegative Feedback |
||||||
if let certifiedKey = NIOSSHCertifiedPublicKey(key), | ||||||
case .server(let config) = self.role, | ||||||
!config.trustedUserCAKeys.isEmpty { | ||||||
// This is a certificate and we have trusted CAs configured | ||||||
do { | ||||||
let criticalOptions = try certifiedKey.validate( | ||||||
principal: request.username, | ||||||
type: .user, | ||||||
allowedAuthoritySigningKeys: config.trustedUserCAKeys, | ||||||
acceptableCriticalOptions: config.acceptableCriticalOptions | ||||||
) | ||||||
|
||||||
// Certificate is valid, store it to pass to the delegate | ||||||
validatedCertificate = certifiedKey | ||||||
} catch { | ||||||
// Certificate validation failed | ||||||
return self.loop.makeSucceededFuture(.failure(.init(authentications: supportedMethods.strings, partialSuccess: false))) | ||||||
} | ||||||
} | ||||||
|
||||||
// Signature is valid, ask if the delegate is happy. | ||||||
let request = NIOSSHUserAuthenticationRequest(username: request.username, serviceName: request.service, request: .publicKey(.init(publicKey: key))) | ||||||
let request = NIOSSHUserAuthenticationRequest( | ||||||
username: request.username, | ||||||
serviceName: request.service, | ||||||
request: .publicKey(.init(publicKey: key, certifiedKey: validatedCertificate)) | ||||||
) | ||||||
let promise = self.loop.makePromise(of: NIOSSHUserAuthenticationOutcome.self) | ||||||
delegate.requestReceived(request: request, responsePromise: promise) | ||||||
|
||||||
|
@@ -425,7 +453,26 @@ private extension UserAuthenticationStateMachine { | |||||
|
||||||
case .publicKey(.known(key: let key, signature: .none)): | ||||||
// This is a weird wrinkle in public key auth: it's a request to ask whether a given key is valid, but not to validate that key itself. | ||||||
// For now we do a shortcut: we just say that all keys are acceptable, rather than ask the delegate. | ||||||
// For certificates, we should validate them before saying they're OK | ||||||
if let certifiedKey = NIOSSHCertifiedPublicKey(key), | ||||||
case .server(let config) = self.role, | ||||||
!config.trustedUserCAKeys.isEmpty { | ||||||
// This is a certificate and we have trusted CAs configured | ||||||
do { | ||||||
_ = try certifiedKey.validate( | ||||||
principal: request.username, | ||||||
type: .user, | ||||||
allowedAuthoritySigningKeys: config.trustedUserCAKeys, | ||||||
acceptableCriticalOptions: config.acceptableCriticalOptions | ||||||
) | ||||||
// Certificate is valid | ||||||
return self.loop.makeSucceededFuture(.publicKeyOK(.init(key: key))) | ||||||
} catch { | ||||||
// Certificate validation failed, reject it | ||||||
return self.loop.makeSucceededFuture(.failure(.init(authentications: delegate.supportedAuthenticationMethods.strings, partialSuccess: false))) | ||||||
} | ||||||
} | ||||||
// For now we do a shortcut: we just say that all non-certificate keys are acceptable, rather than ask the delegate. | ||||||
return self.loop.makeSucceededFuture(.publicKeyOK(.init(key: key))) | ||||||
|
||||||
case .publicKey(.unknown): | ||||||
|
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The error message was updated but the init() method comment above still references 'PublicKeyRequest' which appears to be a copy-paste error from the original code.
Copilot uses AI. Check for mistakes.