Commit dfbfc02
committed
pkey: track whether pkey is private key or not
There are multiple places where it's necessary to know whether a pkey
is a private key, a public key, or just key parameters. This is due to
two reasons:
1. It's currently a responsibility of the caller to give a properly
populated pkey instance to _some_ OpenSSL functions. For example,
calling EVP_PKEY_sign() with an RSA pkey instance without the
necessary components is known to cause a segfault.
2. OpenSSL::PKey::{RSA,DSA,EC}#to_der behaves differently depending on
it: they use the X.509 SubjectPublicKeyInfo structure instead of
private key structures if the receiver pkey is a public key.
Currently, whenever this is necessary, we check the backing object, such
as RSA, and see if the fields are set or not. This approach won't always
work on OpenSSL 3.0 because of the architecture change. Unfortunately,
OpenSSL doesn't expose an API for this purpose (even though it has one
for its internal use).
As a workaround, let's manually track this information in an instance
variable. This has been partly done for ENGINE-backed pkeys. Now all
pkeys get this flag. This is straightforward on OpenSSL 3.0 as pkeys
are immutable once instantiated. On OpenSSL 1.1 or before (and current
versions of LibreSSL), it must be updated whenever a modification is
made to the object.
This commit comes with a slight behavior change. Pkeys returned by
following method will be explicitly marked as "public", even if it
happens to point at an EVP_PKEY struct containing private key
components. I expect the effect is minimum since these methods
explicitly say "public key".
- OpenSSL::X509::Certificate#public_key
- OpenSSL::X509::Request#public_key
- OpenSSL::Netscape::SPKI#public_key1 parent 0ab4e67 commit dfbfc02
File tree
13 files changed
+288
-241
lines changed- ext/openssl
13 files changed
+288
-241
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
140 | 140 | | |
141 | 141 | | |
142 | 142 | | |
| 143 | + | |
143 | 144 | | |
144 | 145 | | |
145 | 146 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
373 | 373 | | |
374 | 374 | | |
375 | 375 | | |
376 | | - | |
377 | | - | |
| 376 | + | |
378 | 377 | | |
379 | 378 | | |
380 | 379 | | |
| |||
403 | 402 | | |
404 | 403 | | |
405 | 404 | | |
406 | | - | |
| 405 | + | |
407 | 406 | | |
408 | 407 | | |
409 | 408 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
190 | 190 | | |
191 | 191 | | |
192 | 192 | | |
193 | | - | |
| 193 | + | |
194 | 194 | | |
195 | 195 | | |
196 | 196 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
152 | 152 | | |
153 | 153 | | |
154 | 154 | | |
155 | | - | |
| 155 | + | |
156 | 156 | | |
157 | 157 | | |
158 | 158 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
19 | 19 | | |
20 | 20 | | |
21 | 21 | | |
22 | | - | |
| 22 | + | |
23 | 23 | | |
24 | 24 | | |
25 | 25 | | |
| |||
65 | 65 | | |
66 | 66 | | |
67 | 67 | | |
68 | | - | |
| 68 | + | |
69 | 69 | | |
70 | 70 | | |
71 | 71 | | |
| |||
75 | 75 | | |
76 | 76 | | |
77 | 77 | | |
| 78 | + | |
78 | 79 | | |
79 | 80 | | |
80 | 81 | | |
| |||
83 | 84 | | |
84 | 85 | | |
85 | 86 | | |
86 | | - | |
| 87 | + | |
87 | 88 | | |
88 | 89 | | |
89 | 90 | | |
90 | 91 | | |
91 | 92 | | |
| 93 | + | |
92 | 94 | | |
93 | 95 | | |
94 | 96 | | |
95 | 97 | | |
96 | 98 | | |
97 | 99 | | |
98 | 100 | | |
99 | | - | |
100 | | - | |
101 | | - | |
102 | | - | |
103 | | - | |
104 | | - | |
105 | | - | |
106 | | - | |
107 | 101 | | |
108 | | - | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
109 | 109 | | |
110 | 110 | | |
111 | 111 | | |
| |||
125 | 125 | | |
126 | 126 | | |
127 | 127 | | |
128 | | - | |
129 | | - | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
130 | 152 | | |
131 | 153 | | |
132 | | - | |
133 | | - | |
134 | | - | |
135 | | - | |
136 | | - | |
137 | | - | |
138 | | - | |
| 154 | + | |
139 | 155 | | |
140 | 156 | | |
141 | | - | |
142 | | - | |
143 | | - | |
144 | | - | |
145 | | - | |
146 | | - | |
147 | | - | |
148 | | - | |
149 | | - | |
150 | | - | |
151 | | - | |
152 | | - | |
| 157 | + | |
| 158 | + | |
| 159 | + | |
| 160 | + | |
| 161 | + | |
| 162 | + | |
| 163 | + | |
| 164 | + | |
| 165 | + | |
| 166 | + | |
| 167 | + | |
| 168 | + | |
| 169 | + | |
| 170 | + | |
| 171 | + | |
| 172 | + | |
| 173 | + | |
| 174 | + | |
153 | 175 | | |
154 | 176 | | |
155 | 177 | | |
156 | 178 | | |
| 179 | + | |
| 180 | + | |
| 181 | + | |
| 182 | + | |
157 | 183 | | |
158 | 184 | | |
159 | 185 | | |
160 | 186 | | |
161 | | - | |
| 187 | + | |
162 | 188 | | |
163 | 189 | | |
164 | 190 | | |
165 | 191 | | |
| 192 | + | |
166 | 193 | | |
167 | 194 | | |
168 | 195 | | |
169 | 196 | | |
170 | 197 | | |
| 198 | + | |
| 199 | + | |
171 | 200 | | |
172 | 201 | | |
173 | 202 | | |
174 | | - | |
| 203 | + | |
175 | 204 | | |
| 205 | + | |
| 206 | + | |
176 | 207 | | |
177 | 208 | | |
| 209 | + | |
| 210 | + | |
178 | 211 | | |
179 | 212 | | |
180 | 213 | | |
| 214 | + | |
| 215 | + | |
181 | 216 | | |
182 | 217 | | |
183 | 218 | | |
| |||
234 | 269 | | |
235 | 270 | | |
236 | 271 | | |
| 272 | + | |
237 | 273 | | |
238 | 274 | | |
239 | 275 | | |
240 | | - | |
| 276 | + | |
241 | 277 | | |
242 | 278 | | |
243 | 279 | | |
244 | | - | |
| 280 | + | |
245 | 281 | | |
246 | 282 | | |
247 | 283 | | |
| |||
445 | 481 | | |
446 | 482 | | |
447 | 483 | | |
448 | | - | |
| 484 | + | |
449 | 485 | | |
450 | 486 | | |
451 | 487 | | |
| |||
567 | 603 | | |
568 | 604 | | |
569 | 605 | | |
570 | | - | |
571 | | - | |
572 | | - | |
573 | | - | |
574 | | - | |
575 | | - | |
576 | | - | |
577 | | - | |
578 | | - | |
579 | | - | |
580 | | - | |
581 | | - | |
| 606 | + | |
| 607 | + | |
| 608 | + | |
582 | 609 | | |
583 | 610 | | |
584 | 611 | | |
| |||
654 | 681 | | |
655 | 682 | | |
656 | 683 | | |
| 684 | + | |
| 685 | + | |
| 686 | + | |
| 687 | + | |
| 688 | + | |
| 689 | + | |
| 690 | + | |
| 691 | + | |
| 692 | + | |
| 693 | + | |
| 694 | + | |
| 695 | + | |
| 696 | + | |
| 697 | + | |
| 698 | + | |
| 699 | + | |
| 700 | + | |
| 701 | + | |
| 702 | + | |
| 703 | + | |
| 704 | + | |
| 705 | + | |
| 706 | + | |
| 707 | + | |
| 708 | + | |
| 709 | + | |
| 710 | + | |
657 | 711 | | |
658 | 712 | | |
659 | 713 | | |
| |||
1620 | 1674 | | |
1621 | 1675 | | |
1622 | 1676 | | |
| 1677 | + | |
| 1678 | + | |
1623 | 1679 | | |
1624 | 1680 | | |
1625 | 1681 | | |
| |||
1637 | 1693 | | |
1638 | 1694 | | |
1639 | 1695 | | |
1640 | | - | |
| 1696 | + | |
1641 | 1697 | | |
1642 | 1698 | | |
1643 | 1699 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
14 | 14 | | |
15 | 15 | | |
16 | 16 | | |
17 | | - | |
18 | | - | |
19 | | - | |
20 | | - | |
| 17 | + | |
21 | 18 | | |
22 | 19 | | |
23 | 20 | | |
| |||
26 | 23 | | |
27 | 24 | | |
28 | 25 | | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
29 | 49 | | |
30 | | - | |
| 50 | + | |
31 | 51 | | |
32 | | - | |
| 52 | + | |
| 53 | + | |
33 | 54 | | |
34 | 55 | | |
35 | 56 | | |
| |||
145 | 166 | | |
146 | 167 | | |
147 | 168 | | |
| 169 | + | |
148 | 170 | | |
149 | 171 | | |
150 | 172 | | |
| |||
172 | 194 | | |
173 | 195 | | |
174 | 196 | | |
| 197 | + | |
175 | 198 | | |
176 | 199 | | |
177 | 200 | | |
| |||
0 commit comments