Skip to content
This repository has been archived by the owner on Jan 24, 2023. It is now read-only.

Use SHA256 to compare SSH public key fingerprint #3249

Conversation

combor
Copy link

@combor combor commented Nov 28, 2018

Description

MD5 is deprecated and cf info endpoint returns now SHA256 fingerprint.
Use SHA256 to compare fingerprints instead MD5

Motivation and Context

This fixes ssh to an app instance functionality.

How Has This Been Tested?

Manually

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • Docs update
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)

Checklist:

  • My code follows the code style of this project.
  • My change requires a change to the documentation.
  • I have updated the documentation accordingly.
  • I have followed the guidelines in CONTRIBUTING.md, including the required formatting of the commit message

@cfdreddbot
Copy link

❌ Hey combor!

All pull request submitters and commit authors must have a Contributor License Agreement (CLA). Click here for details on the CLA process.

The following github user @combor is not covered by a CLA.

After the CLA process is complete, this pull request will need to be closed & reopened. DreddBot will then validate the CLA(s).

@codecov
Copy link

codecov bot commented Nov 28, 2018

Codecov Report

Merging #3249 into v2-master will decrease coverage by 0.01%.
The diff coverage is n/a.

@@              Coverage Diff              @@
##           v2-master    #3249      +/-   ##
=============================================
- Coverage       70.9%   70.89%   -0.02%     
=============================================
  Files            642      642              
  Lines          28204    28204              
  Branches        6412     6412              
=============================================
- Hits           19999    19995       -4     
- Misses          8205     8209       +4

@nwmac nwmac added in review needs attention This PR needs attention labels Dec 1, 2018
Copy link
Contributor

@nwmac nwmac left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The fingerprint check needs to cope with both MD5 and SHA256 - this change only supports SHA256, which will be break any deployments that use MD5.

SHS256 Fingerprints are prefixed with 'SHA256', so we should check which type the fingerprint is and use that when checking.

@combor
Copy link
Author

combor commented Dec 4, 2018

@nwmac SHA256 are not prefixed when returned from api /v2/info
If you check my change it does this:

if fmt.Sprintf("SHA256:%s", fingerprint) == ssh.FingerprintSHA256(key) {

It needs to add the prefix for comparison with ssh.FingerprintSHA256 output.

But I agree in general with the point of keeping this backwards compatible. I'll prepare a change.

@combor combor force-pushed the fix_ssh_key_fingerprint_comparision branch 2 times, most recently from e2b079b to 11ba12d Compare December 4, 2018 09:59
@combor
Copy link
Author

combor commented Dec 4, 2018

@nwmac please check now. It checks both sums now serially and returns on first hit.

@nwmac
Copy link
Contributor

nwmac commented Dec 4, 2018

@combor As I understand it, the fingerprint as returned by cf's /v2/info API will include the SHA256: prefix if that algorithm is being used, otherwise it is an MD5.

So, I think the code should be:

if strings.HasPrefix("SHA256")  && fingerprint == ssh.FingerprintSHA256(key) {
	return nil
}

if fingerprint == ssh.FingerprintLegacyMD5(key) {
	return nil
}

@combor
Copy link
Author

combor commented Dec 4, 2018

@nwmac well, I don't thing /v2/info returns fingerprint with a prefix, that's why I'm using this to include it:

fmt.Sprintf("SHA256:%s", fingerprint)

and later compare with

 ssh.FingerprintSHA256(key)

as this one includes SHA256 prefix.

Just to summarise and be clear:

  • variable fingerprint is without prefix
  • ssh.FingerprintSHA256(key) creates a string with the SHA256 prefix

@nwmac
Copy link
Contributor

nwmac commented Dec 12, 2018

@combor Sorry, you're right it doesn't include SHA256 - I'd found that while looking into this, but its not standard.

I've done a bit more investigation - I think we should adopt the same approach as the CLI team did - using the key length to determine the algorithm - see here: https://github.com/cloudfoundry/cli/pull/1072/files

@combor
Copy link
Author

combor commented Dec 12, 2018

Cool, the cli code looks good. I'll include it into PR.

cf api can return SHA256 now depending on configuration so we add it to
the picture and test for string length to use correct hashing function

Signed-off-by: Piotr Komborski <piotr.komborski@fil.com>
@combor combor force-pushed the fix_ssh_key_fingerprint_comparision branch from 11ba12d to b72dc68 Compare December 18, 2018 15:13
@combor
Copy link
Author

combor commented Dec 18, 2018

Hi @nwmac, I just updated this PR. Please have a look and check if that's what you meant.

@richard-cox richard-cox added ready for review and removed in review needs attention This PR needs attention labels Jan 3, 2019
Copy link
Contributor

@nwmac nwmac left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@nwmac nwmac merged commit 6df479d into cloudfoundry:v2-master Jan 7, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants