openssl-additions 0.6.0 → 0.7.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. checksums.yaml +4 -4
  2. data/lib/openssl/pkey/rsa.rb +59 -0
  3. metadata +2 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 3af2a1bdc82b061267777ed93961fa411eaea3ddeeb4912a0be3772cadfacd48
4
- data.tar.gz: '0860f6fa16eb240a6d3408f91a3637f48900dc06d4f2fbe550684df60bccd3f8'
3
+ metadata.gz: c0523bd578bb44447a923fd4091dfcaa6d6bade8a5edc24e3207100bfee5022f
4
+ data.tar.gz: 9eff31167c2af32b1bd992c98a9e1f509d328b84b07c6365fb19509049792f7e
5
5
  SHA512:
6
- metadata.gz: d27ca892b2505c0fb88095936dd44e880f340d112b2df50e48deb2fc66c96aa8bf36a0ea3d4c599c621e36efa020045709783fb4c239e26a143bd3a08d8fc265
7
- data.tar.gz: 7bb76838e1bc1eee2af1b813e3406827dc0fe08ce32defe633be1da3f7037f7f9485a0e534e2fef0d16d99f10a8f4e317711639ab6363fd8796a8e0b6d1eb130
6
+ metadata.gz: 945ef4ee75d39142e7976cca06087ac4e34add2243eeb6c5ec57f00f1287cf5514dbc88b0313c0e811a5c8d2b3113069f73cdf7bf06f58c093a9be4b746369b3
7
+ data.tar.gz: 82ae3be58dbf8d88aee6e5480f93d50d8e659bd330aec73fe4256ad0f100cb41f566be1d9569927e645c3a4b520e166909565dee97492c9e6e91f11c19a6dadc
@@ -15,6 +15,65 @@ class OpenSSL::PKey::RSA
15
15
  OpenSSL::X509::SPKI.new(self.public_key.to_der)
16
16
  end
17
17
 
18
+ # Give our best guess as to whether the given RSA private key is valid.
19
+ #
20
+ # Applies a set of heuristics to the (private) key, with a view to deciding
21
+ # whether it is correctly formed.
22
+ #
23
+ # Based on the RSA_check_key OpenSSL function.
24
+ #
25
+ # @param extended [Boolean] specify whether to only check problems which
26
+ # cannot be corrected by re-calculating from the fundamental parameters of
27
+ # the key (the private factors `p` and `q`, and the public exponent `e`).
28
+ # The default is to consider any deviation from a completely correct key
29
+ # to render the key invalid.
30
+ #
31
+ # @return [Boolean]
32
+ #
33
+ def valid?(extended = true)
34
+ # Must have factors and public exponent
35
+ return false if p.nil? || q.nil? || e.nil?
36
+
37
+ # Public exponent must be odd and greater than one
38
+ return false if e == 1
39
+
40
+ return false if e % 2 == 0
41
+
42
+ # Factors must be prime
43
+ return false unless p.prime?
44
+ return false unless q.prime?
45
+
46
+ # All the remaining checks are things that could be fixed with some
47
+ # arithmetic
48
+ return true if !extended
49
+
50
+ # Must have private exponent and a modulus
51
+ return false if d.nil? || n.nil?
52
+
53
+ # Public modulus must be the product of the two prime factors
54
+ return false unless n == p * q
55
+
56
+ # d * e must equal 1 mod (lcm(p-1,q-1))
57
+ return false unless e * d % (p.to_i-1).lcm(q.to_i-1) == 1
58
+
59
+ # CRT parameters are optional, but if present must be correct
60
+ unless dmp1.nil?
61
+ return false unless dmp1 == d % (p-1)
62
+ end
63
+
64
+ unless dmq1.nil?
65
+ return false unless dmq1 == d % (q-1)
66
+ end
67
+
68
+ unless iqmp.nil?
69
+ t, _ = self.class.egcd(q.to_i, p.to_i)
70
+ t %= p if t < 0
71
+ return false unless iqmp == t
72
+ end
73
+
74
+ return true
75
+ end
76
+
18
77
  # Construct a fully-featured RSA private key from fundamental values.
19
78
  #
20
79
  # Many parts of an RSA key are, in fact, derived from the basic numbers that
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: openssl-additions
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.6.0
4
+ version: 0.7.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Matt Palmer
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-05-07 00:00:00.000000000 Z
11
+ date: 2020-05-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler