From 85830aa66a67eb6f8bccf6bc60a0a64345f81352 Mon Sep 17 00:00:00 2001 From: Alex Kotov Date: Sat, 14 Sep 2019 04:20:47 +0500 Subject: [PATCH] Add model EcurveKey --- app/models/ecurve_key.rb | 11 +++++++++++ app/models/rsa_key.rb | 2 ++ factories/ecurve_keys.rb | 27 +++++++++++++++++++++++++++ factories/rsa_keys.rb | 3 ++- spec/models/ecurve_key_spec.rb | 20 ++++++++++++++++++++ spec/models/rsa_key_spec.rb | 4 ++++ 6 files changed, 66 insertions(+), 1 deletion(-) create mode 100644 app/models/ecurve_key.rb create mode 100644 factories/ecurve_keys.rb create mode 100644 spec/models/ecurve_key_spec.rb diff --git a/app/models/ecurve_key.rb b/app/models/ecurve_key.rb new file mode 100644 index 0000000..39769fb --- /dev/null +++ b/app/models/ecurve_key.rb @@ -0,0 +1,11 @@ +# frozen_string_literal: true + +class EcurveKey < AsymmetricKey + ############### + # Validations # + ############### + + validates :curve, inclusion: { in: %w[prime256v1 secp384r1] } + + validates :bits, absence: true +end diff --git a/app/models/rsa_key.rb b/app/models/rsa_key.rb index 275eebc..af956c0 100644 --- a/app/models/rsa_key.rb +++ b/app/models/rsa_key.rb @@ -6,4 +6,6 @@ class RSAKey < AsymmetricKey ############### validates :bits, inclusion: { in: [2048, 4096] } + + validates :curve, absence: true end diff --git a/factories/ecurve_keys.rb b/factories/ecurve_keys.rb new file mode 100644 index 0000000..2d8b8e1 --- /dev/null +++ b/factories/ecurve_keys.rb @@ -0,0 +1,27 @@ +# frozen_string_literal: true + +FactoryBot.define do + factory :ecurve_key do + association :account, factory: :usual_account + + public_key_pem do + point = OpenSSL::PKey::EC.generate(curve).public_key + pkey = OpenSSL::PKey::EC.new point.group + pkey.public_key = point + pkey.to_pem + end + + public_key_der do + point = OpenSSL::PKey::EC.generate(curve).public_key + pkey = OpenSSL::PKey::EC.new point.group + pkey.public_key = point + pkey.to_der + end + + has_password { [false, true].sample } + sha1 { Digest::SHA1.hexdigest SecureRandom.hex } + sha256 { Digest::SHA256.hexdigest SecureRandom.hex } + + curve { %w[prime256v1 secp384r1].sample } + end +end diff --git a/factories/rsa_keys.rb b/factories/rsa_keys.rb index 7fe53ab..69d7277 100644 --- a/factories/rsa_keys.rb +++ b/factories/rsa_keys.rb @@ -8,8 +8,9 @@ FactoryBot.define do public_key_der { OpenSSL::PKey::RSA.new(bits).public_key.to_der } has_password { [false, true].sample } - bits { [2048, 4096].sample } sha1 { Digest::SHA1.hexdigest SecureRandom.hex } sha256 { Digest::SHA256.hexdigest SecureRandom.hex } + + bits { [2048, 4096].sample } end end diff --git a/spec/models/ecurve_key_spec.rb b/spec/models/ecurve_key_spec.rb new file mode 100644 index 0000000..769a00d --- /dev/null +++ b/spec/models/ecurve_key_spec.rb @@ -0,0 +1,20 @@ +# frozen_string_literal: true + +require 'rails_helper' + +RSpec.describe EcurveKey do + subject { create :ecurve_key } + + it_behaves_like 'asymmetric_key' + + describe '#curve' do + it do + is_expected.to \ + validate_inclusion_of(:curve).in_array(%w[prime256v1 secp384r1]) + end + end + + describe '#bits' do + it { is_expected.to validate_absence_of :bits } + end +end diff --git a/spec/models/rsa_key_spec.rb b/spec/models/rsa_key_spec.rb index 8f32563..f902d1a 100644 --- a/spec/models/rsa_key_spec.rb +++ b/spec/models/rsa_key_spec.rb @@ -11,6 +11,10 @@ RSpec.describe RSAKey do it { is_expected.to validate_inclusion_of(:bits).in_array([2048, 4096]) } end + describe '#curve' do + it { is_expected.to validate_absence_of :curve } + end + describe '#encrypt_private_key_pem' do subject { create :rsa_key, private_key_pem: cleartext }