Fixes #3727 - Adding private keys allows adding certificates.

This commit is contained in:
Rolf Schmidt 2021-10-20 13:04:31 +02:00
parent cb48cd2dcd
commit 274e5a849f
4 changed files with 164 additions and 42 deletions

View file

@ -42,9 +42,7 @@ class Integration::SMIMEController < ApplicationController
string = params[:file].read.force_encoding('utf-8')
end
items = string.scan(%r{.+?-+END(?: TRUSTED)? CERTIFICATE-+}mi).each_with_object([]) do |cert, result|
result << SMIMECertificate.create!(public_key: cert)
end
items = SMIMECertificate.create_certificates(string)
render json: {
result: 'ok',
@ -73,14 +71,8 @@ class Integration::SMIMEController < ApplicationController
raise "Parameter 'data' or 'file' required." if string.blank?
private_key = OpenSSL::PKey.read(string, params[:secret])
modulus = private_key.public_key.n.to_s(16)
certificate = SMIMECertificate.find_by(modulus: modulus)
raise Exceptions::UnprocessableEntity, 'Unable for find certificate for this private key.' if !certificate
certificate.update!(private_key: string, private_key_secret: params[:secret])
SMIMECertificate.create_certificates(string)
SMIMECertificate.create_private_keys(string, params[:secret])
render json: {
result: 'ok',

View file

@ -3,6 +3,28 @@
class SMIMECertificate < ApplicationModel
validates :fingerprint, uniqueness: { case_sensitive: true }
def self.parts(raw)
raw.scan(%r{-----BEGIN[^-]+-----.+?-----END[^-]+-----}m)
end
def self.create_private_keys(raw, secret)
parts(raw).select { |part| part.include?('PRIVATE KEY') }.each do |part|
private_key = OpenSSL::PKey.read(part, secret)
modulus = private_key.public_key.n.to_s(16)
certificate = find_by(modulus: modulus)
raise Exceptions::UnprocessableEntity, 'Unable for find certificate for this private key.' if !certificate
certificate.update!(private_key: part, private_key_secret: secret)
end
end
def self.create_certificates(raw)
parts(raw).select { |part| part.include?('CERTIFICATE') }.each_with_object([]) do |part, result|
result << create!(public_key: part)
end
end
def self.parse(raw)
OpenSSL::X509::Certificate.new(raw.gsub(%r{(?:TRUSTED\s)?(CERTIFICATE---)}, '\1'))
end

87
spec/fixtures/smime/issue_3727.key vendored Normal file
View file

@ -0,0 +1,87 @@
-----BEGIN RSA P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nZoZ5X9LSfNMYH4TZG0jkH2Sm7L0OQmPOqViZ5NB3bw
-----END CERTIFICATE-----

View file

@ -23,6 +23,7 @@ RSpec.describe 'Manage > Integration > S/MIME', type: :system do
click 'label[for=setting-switch]'
end
context 'when doing basic tests' do
it 'enabling and adding of public and private key' do
# add cert
@ -36,9 +37,6 @@ RSpec.describe 'Manage > Integration > S/MIME', type: :system do
fill_in 'Enter Private Key secret', with: private_key_secret
click '.js-submit'
# wait for ajax
expect(page).to have_css('td', text: 'Including private key')
# check result
expect(Setting.get('smime_integration')).to be true
expect(SMIMECertificate.last.fingerprint).to be_present
@ -64,3 +62,26 @@ RSpec.describe 'Manage > Integration > S/MIME', type: :system do
expect(page).to have_text('RootCA')
end
end
context 'Adding private keys allows adding certificates #3727' do
let!(:private_key) do
File.read(Rails.root.join('spec/fixtures/smime/issue_3727.key'))
end
it 'does add public and private key in one file' do
# add private key
click '.js-addPrivateKey'
fill_in 'Paste Private Key', with: private_key
click '.js-submit'
# check result
expect(Setting.get('smime_integration')).to be true
expect(SMIMECertificate.last.fingerprint).to eq('db49277070afcfc657bd71d04be4dd7e28f1685a')
expect(SMIMECertificate.last.raw).to include('CERTIFICATE')
expect(SMIMECertificate.last.raw).not_to include('PRIVATE')
expect(SMIMECertificate.last.private_key).to include('PRIVATE')
expect(SMIMECertificate.last.private_key).not_to include('CERTIFICATE')
end
end
end