Suggest different variables to use if we want to tunnel both v4 and v6
[mirror/dsa-puppet.git] / 3rdparty / modules / stdlib / lib / puppet / parser / functions / validate_x509_rsa_key_pair.rb
1 #
2 # validate_x509_rsa_key_pair.rb
3 #
4 module Puppet::Parser::Functions
5   newfunction(:validate_x509_rsa_key_pair, :doc => <<-DOC
6     Validates a PEM-formatted X.509 certificate and RSA private key using
7     OpenSSL. Verifies that the certficate's signature was created from the
8     supplied key.
9
10     Fail compilation if any value fails this check.
11
12     validate_x509_rsa_key_pair($cert, $key)
13
14     DOC
15              ) do |args|
16
17     require 'openssl'
18
19     NUM_ARGS = 2 unless defined? NUM_ARGS
20
21     unless args.length == NUM_ARGS
22       raise Puppet::ParseError,
23             "validate_x509_rsa_key_pair(): wrong number of arguments (#{args.length}; must be #{NUM_ARGS})"
24     end
25
26     args.each do |arg|
27       unless arg.is_a?(String)
28         raise Puppet::ParseError, "#{arg.inspect} is not a string."
29       end
30     end
31
32     begin
33       cert = OpenSSL::X509::Certificate.new(args[0])
34     rescue OpenSSL::X509::CertificateError => e
35       raise Puppet::ParseError, "Not a valid x509 certificate: #{e}"
36     end
37
38     begin
39       key = OpenSSL::PKey::RSA.new(args[1])
40     rescue OpenSSL::PKey::RSAError => e
41       raise Puppet::ParseError, "Not a valid RSA key: #{e}"
42     end
43
44     unless cert.verify(key)
45       raise Puppet::ParseError, 'Certificate signature does not match supplied key'
46     end
47   end
48 end