2 # validate_x509_rsa_key_pair.rb
4 module Puppet::Parser::Functions
5 newfunction(:validate_x509_rsa_key_pair, :doc => <<-DOC
7 Validates a PEM-formatted X.509 certificate and RSA private key using
8 OpenSSL. Verifies that the certficate's signature was created from the
12 Fail compilation if any value fails this check.
14 ```validate_x509_rsa_key_pair($cert, $key)```
21 NUM_ARGS = 2 unless defined? NUM_ARGS
23 unless args.length == NUM_ARGS
24 raise Puppet::ParseError,
25 "validate_x509_rsa_key_pair(): wrong number of arguments (#{args.length}; must be #{NUM_ARGS})"
29 unless arg.is_a?(String)
30 raise Puppet::ParseError, "#{arg.inspect} is not a string."
35 cert = OpenSSL::X509::Certificate.new(args[0])
36 rescue OpenSSL::X509::CertificateError => e
37 raise Puppet::ParseError, "Not a valid x509 certificate: #{e}"
41 key = OpenSSL::PKey::RSA.new(args[1])
42 rescue OpenSSL::PKey::RSAError => e
43 raise Puppet::ParseError, "Not a valid RSA key: #{e}"
46 unless cert.verify(key)
47 raise Puppet::ParseError, 'Certificate signature does not match supplied key'