Class: R509::CertificateAuthority::Signer
- Inherits:
-
Object
- Object
- R509::CertificateAuthority::Signer
- Defined in:
- lib/r509/certificateauthority.rb
Overview
Contains the certification authority signing operation methods
Instance Method Summary (collapse)
-
- (Signer) initialize(config = nil)
constructor
A new instance of Signer.
-
- (R509::Cert) selfsign(options)
Self-signs a CSR.
-
- (R509::Cert) sign(options)
Signs a CSR.
Constructor Details
- (Signer) initialize(config = nil)
A new instance of Signer
11 12 13 14 15 16 17 18 19 20 |
# File 'lib/r509/certificateauthority.rb', line 11 def initialize(config=nil) @config = config if not @config.nil? and not @config.kind_of?(R509::Config::CaConfig) raise R509::R509Error, "config must be a kind of R509::Config::CaConfig or nil (for self-sign only)" end if not @config.nil? and not @config.ca_cert.has_private_key? raise R509::R509Error, "You must have a private key associated with your CA certificate to issue" end end |
Instance Method Details
- (R509::Cert) selfsign(options)
Self-signs a CSR
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 |
# File 'lib/r509/certificateauthority.rb', line 121 def selfsign() if not .kind_of?(Hash) raise ArgumentError, "You must pass a hash of options consisting of at minimum :csr" end csr = [:csr] if csr.key.nil? raise ArgumentError, 'CSR must also have a private key to self sign' end cert = build_cert( :subject => csr.subject.name, :issuer => csr.subject.name, :not_before => [:not_before], :not_after => [:not_after], :public_key => csr.public_key, :serial => [:serial] ) if .has_key?(:san_names) san_names = [:san_names] else san_names = csr.san_names end build_extensions( :subject_certificate => cert, :issuer_certificate => cert, :basic_constraints => "CA:TRUE", :san_names => san_names ) if .has_key?(:message_digest) = R509::MessageDigest.new([:message_digest]) else = R509::MessageDigest.new('sha1') end # Csr#key returns R509::PrivateKey and #key on that returns OpenSSL object we need cert.sign( csr.key.key, .digest ) R509::Cert.new(:cert => cert) end |
- (R509::Cert) sign(options)
Signs a CSR
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 |
# File 'lib/r509/certificateauthority.rb', line 32 def sign() if @config.nil? raise R509::R509Error, "When instantiating the signer without a config you can only call #selfsign" elsif @config.num_profiles == 0 raise R509::R509Error, "You must have at least one CaProfile on your CaConfig to issue" end if .has_key?(:csr) and .has_key?(:spki) raise ArgumentError, "You can't pass both :csr and :spki" elsif not .has_key?(:csr) and not .has_key?(:spki) raise ArgumentError, "You must supply either :csr or :spki" elsif .has_key?(:csr) if not [:csr].kind_of?(R509::Csr) raise ArgumentError, "You must pass an R509::Csr object for :csr" else signable_object = [:csr] end elsif not .has_key?(:csr) and .has_key?(:spki) if not [:spki].kind_of?(R509::Spki) raise ArgumentError, "You must pass an R509::Spki object for :spki" else signable_object = [:spki] end end if .has_key?(:data_hash) san_names = [:data_hash][:san_names] subject = [:data_hash][:subject] else san_names = signable_object.to_hash[:san_names] subject = signable_object.to_hash[:subject] end if .has_key?(:csr) and not [:csr].verify_signature raise R509::R509Error, "Certificate request signature is invalid." end #handle DSA here if .has_key?(:message_digest) = R509::MessageDigest.new([:message_digest]) else = R509::MessageDigest.new(@config.) end profile = @config.profile([:profile_name]) validated_subject = validate_subject(subject,profile) cert = build_cert( :subject => validated_subject.name, :issuer => @config.ca_cert.subject, :not_before => [:not_before], :not_after => [:not_after], :public_key => signable_object.public_key, :serial => [:serial] ) basic_constraints = profile.basic_constraints key_usage = profile.key_usage extended_key_usage = profile.extended_key_usage certificate_policies = profile.certificate_policies build_extensions( :subject_certificate => cert, :issuer_certificate => @config.ca_cert.cert, :basic_constraints => basic_constraints, :key_usage => key_usage, :extended_key_usage => extended_key_usage, :certificate_policies => certificate_policies, :san_names => san_names ) #@config.ca_cert.key.key ... ugly. ca_cert returns R509::Cert # #key returns R509::PrivateKey and #key on that returns OpenSSL object we need cert.sign( @config.ca_cert.key.key, .digest ) R509::Cert.new(:cert => cert) end |