You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Implement OpenSSL 3.0 OSSL_PARAM API binding for RSA key parameters (#377)
* Implement OSSL_PARAM API binding for RSA key parameters
- Enhanced param.c to support RSA key parameters (n, e, d, p, q, exponents, coefficients)
- Updated RSA parse function to use OSSL_PARAM API in OpenSSL 3.0+
- Added fallback to legacy RSA_get0_* functions for backward compatibility
- Exported RSA parameter definitions in param module
- Created comprehensive test suite for OSSL_PARAM functionality
- All tests passing (7/7)
Co-authored-by: zhaozg <[email protected]>
* Add OSSL_PARAM API usage documentation
---------
Co-authored-by: copilot-swe-agent[bot] <[email protected]>
Co-authored-by: zhaozg <[email protected]>
Starting with OpenSSL 3.0, the OSSL_PARAM API is the modern way to access cryptographic key parameters. lua-openssl now supports this API while maintaining backward compatibility with OpenSSL 1.x.
6
+
7
+
## What is OSSL_PARAM?
8
+
9
+
OSSL_PARAM is OpenSSL 3.0's unified parameter system that replaces many legacy key access functions. It provides a consistent interface for accessing parameters across different key types (RSA, EC, DH, etc.).
10
+
11
+
## Supported Parameters
12
+
13
+
### RSA Key Parameters
14
+
15
+
The following RSA parameters are accessible via the param module:
16
+
17
+
```lua
18
+
localparam=require("openssl").param
19
+
20
+
-- Available RSA parameters:
21
+
-- param.rsa.n - Modulus (public)
22
+
-- param.rsa.e - Public exponent
23
+
-- param.rsa.d - Private exponent
24
+
-- param.rsa["rsa-factor1"] - Prime p
25
+
-- param.rsa["rsa-factor2"] - Prime q
26
+
-- param.rsa["rsa-exponent1"] - dmp1 (d mod (p-1))
27
+
-- param.rsa["rsa-exponent2"] - dmq1 (d mod (q-1))
28
+
-- param.rsa["rsa-coefficient1"] - iqmp (q^-1 mod p)
29
+
```
30
+
31
+
### KDF Parameters
32
+
33
+
KDF parameters continue to work as before. See `test/2.kdf.lua` for examples.
34
+
35
+
## Usage Examples
36
+
37
+
### Parsing RSA Key Parameters
38
+
39
+
```lua
40
+
localopenssl=require("openssl")
41
+
localrsa=openssl.rsa
42
+
43
+
-- Generate an RSA key
44
+
localkey=rsa.generate_key(2048)
45
+
46
+
-- Parse the key to access parameters
47
+
localparams=key:parse()
48
+
49
+
-- Access parameters
50
+
print("Key size:", params.size, "bytes")
51
+
print("Key bits:", params.bits)
52
+
53
+
-- Access public parameters (always available)
54
+
print("Modulus n:", params.n) -- BIGNUM object
55
+
print("Exponent e:", params.e) -- BIGNUM object
56
+
57
+
-- Access private parameters (only for private keys)
print(string.format(" Number type: %s", info.number_type))
78
+
end
79
+
end
80
+
```
81
+
82
+
## Implementation Details
83
+
84
+
### OpenSSL 3.0+ Path
85
+
86
+
When running on OpenSSL 3.0 or later, `rsa:parse()` uses the modern `EVP_PKEY_get_bn_param()` API to extract parameters. This is the recommended approach for new code.
87
+
88
+
### OpenSSL 1.x Path
89
+
90
+
On OpenSSL 1.x, or when the PARAM API fails (e.g., for legacy keys), the implementation automatically falls back to the traditional `RSA_get0_key()`, `RSA_get0_factors()`, and `RSA_get0_crt_params()` functions.
91
+
92
+
### Compatibility
93
+
94
+
The dual-path implementation ensures:
95
+
- ✅ Works with OpenSSL 1.x (using legacy API)
96
+
- ✅ Works with OpenSSL 3.0+ (using PARAM API)
97
+
- ✅ Handles keys created with either API version
98
+
- ✅ No changes required to existing code
99
+
- ✅ Same behavior across versions
100
+
101
+
## Testing
102
+
103
+
The implementation includes comprehensive tests in `test/2.param.lua`:
104
+
105
+
```bash
106
+
# Run param tests
107
+
cdtest
108
+
lua 2.param.lua
109
+
```
110
+
111
+
## Future Enhancements
112
+
113
+
The current implementation focuses on RSA parameters. Future updates may include:
114
+
- EC (Elliptic Curve) key parameters
115
+
- DH (Diffie-Hellman) parameters
116
+
- DSA parameters
117
+
- General-purpose OSSL_PARAM array creation from Lua
0 commit comments