Skip to content

Add a function to validate an x509 RSA key pair #552

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 3 commits into from
Jan 8, 2016

Conversation

mattbostock
Copy link
Contributor

Add a function to validate an x509 RSA certificate and key pair, as
commonly used for TLS certificates.

The rationale behind this is that we store our TLS certificates and
private keys in Hiera YAML files, and poor indentation or formatting in
the YAML file could cause a valid certificate to be considered invalid.

Will cause the Puppet run to fail if:

  • an invalid certificate is detected
  • an invalid RSA key is detected
  • the certificate does not match the key, i.e. the certificate
    has not been signed by the supplied key

The test certificates I've used in the spec tests were generated using
the Go standard library:

$ go run $GOROOT/src/crypto/tls/generate_cert.go -host localhost

Example output:

==> cache-1.router: Error: Not a valid RSA key: Neither PUB key nor PRIV key:: nested asn1 error at /var/govuk/puppet/modules/nginx/manifests/config/ssl.pp:30 on node cache-1.router.dev.gov.uk

@mattbostock mattbostock force-pushed the add_x509_rsa_key_pair branch from 96d458b to d3b5f88 Compare November 24, 2015 17:56
@mattbostock
Copy link
Contributor Author

I believe the build failed because of the ||= syntax I used to initialise the constant is not compatible with Ruby 1.8; I've amended the syntax to address that.

@mattbostock mattbostock force-pushed the add_x509_rsa_key_pair branch 2 times, most recently from 0bb61b4 to ce3b97d Compare November 24, 2015 18:27
@mattbostock
Copy link
Contributor Author

bump

@igalic
Copy link
Contributor

igalic commented Dec 4, 2015

I think it could also be useful to have a type which does this for cases where we create our own certs in the node (like we would for letsencrypt)

@mattbostock
Copy link
Contributor Author

@igalic I'm not sure what you mean, can you please elaborate?

@igalic
Copy link
Contributor

igalic commented Dec 4, 2015

a function can only run on a puppet server (or, under puppet apply) a type would run on each node.

@mattbostock
Copy link
Contributor Author

@igalic I think that's outside the scope of the stdlib and the use case that this PR is targeted at.

end

unless cert.verify(key)
raise Puppet::ParseError, "Certificate signature does not match supplied key"
Copy link
Contributor

Choose a reason for hiding this comment

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

Is there any OpenSSL error we can pass through to help debugging this?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

No. cert.verify(key) returns a boolean; either the key matches the certificate or it does not. If the key or cert are malformed or invalid, the earlier checks will catch that and report the error to the user.

@DavidS
Copy link
Contributor

DavidS commented Dec 11, 2015

@igalic has a point that similar functionality would be useful as a type. This does not preclude merging this. I've added a few comments where the code could be improved, then it's good to go!

@mattbostock
Copy link
Contributor Author

@DavidS @igalic: I understand now; I agree that adding this as a type would be useful. I'll look into that, but in the meantime I'll address the comments here so that this can be merged.

Having a stdlib function remains useful IMHO for validating data that's passed into third-party modules which don't use the type.

@mattbostock
Copy link
Contributor Author

Also apologies for the delay in getting back to you, I was away over Christmas 🎄

Add a function to validate an x509 RSA certificate and key pair, as
commonly used for TLS certificates.

The rationale behind this is that we store our TLS certificates and
private keys in Hiera YAML files, and poor indentation or formatting in
the YAML file could cause a valid certificate to be considered invalid.

Will cause the Puppet run to fail if:

- an invalid certificate is detected
- an invalid RSA key is detected
- the certificate does not match the key, i.e. the certificate
  has not been signed by the supplied key

The test certificates I've used in the spec tests were generated using
the Go standard library:

    $ go run $GOROOT/src/crypto/tls/generate_cert.go -host localhost

Example output:

    ==> cache-1.router: Error: Not a valid RSA key: Neither PUB key nor PRIV key:: nested asn1 error at /var/govuk/puppet/modules/nginx/manifests/config/ssl.pp:30 on node cache-1.router.dev.gov.uk
Test a valid certificate and valid key that have had 48 characters
removed from their middle, to simulate a malformed certificate and key.

Suggested by @DavidS in puppetlabs#552
Put the tests using a valid certificate fixture together and put tests
using a valid key fixture together.
@mattbostock mattbostock force-pushed the add_x509_rsa_key_pair branch from ce3b97d to 41f9319 Compare January 8, 2016 11:09
@mattbostock
Copy link
Contributor Author

I've just rebased to address the above comments; please let me know if there's anything else to add.

DavidS added a commit that referenced this pull request Jan 8, 2016
Add a function to validate an x509 RSA key pair
@DavidS DavidS merged commit f875770 into puppetlabs:master Jan 8, 2016
@DavidS
Copy link
Contributor

DavidS commented Jan 8, 2016

Excellent work. Love it! Thanks a lot.

@DavidS DavidS added feature and removed needs-work labels Jan 8, 2016
@mattbostock mattbostock deleted the add_x509_rsa_key_pair branch January 8, 2016 16:46
@mattbostock
Copy link
Contributor Author

@DavidS When is this likely to be released?

cegeka-jenkins pushed a commit to cegeka/puppet-stdlib that referenced this pull request Jul 17, 2019
Test a valid certificate and valid key that have had 48 characters
removed from their middle, to simulate a malformed certificate and key.

Suggested by @DavidS in puppetlabs/puppetlabs-stdlib#552
@alexjfisher
Copy link
Collaborator

@mattbostock Was this only ever intended for self-signed certs? We had a situation where we wanted to check that a key and cert matched, but as the cert was signed by an external CA, this function didn't work for us.

@mattbostock
Copy link
Contributor Author

mattbostock commented Jan 12, 2022 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants