Debian Codegraph @

debian/rust-vendor/openssl/src/ossl_param.rs

raw
1 //! OSSL_PARAM management for OpenSSL 3.*
2 //!
3 //! The OSSL_PARAM structure represents an array of generic
4 //! attributes that can represent various
5 //! properties in OpenSSL, including keys and operations.
6 //!
7 //! This is always represented as an array of OSSL_PARAM
8 //! structures, terminated by an entry with a NULL key.
9 //!
10 //! For convenience, the OSSL_PARAM_BLD builder can be used to
11 //! dynamically construct these structures.
12 //!
13 //! Note, that this module is available only in OpenSSL 3.* and
14 //! only internally for this crate.
15
16 use crate::error::ErrorStack;
17 use crate::util;
18 use crate::{cvt, cvt_p};
19 use foreign_types::ForeignType;
20 use libc::{c_char, c_int, c_uint, c_void};
21 use openssl_macros::corresponds;
22 use std::ffi::CStr;
23 use std::marker::PhantomData;
24 use std::ptr;
25
26 foreign_type_and_impl_send_sync! {
27 // This is the singular type, but it is always allocated
28 // and used as an array of such types.
29 type CType = ffi::OSSL_PARAM;
30 // OSSL_PARMA_free correctly frees the entire array.
31 fn drop = ffi::OSSL_PARAM_free;
32
33 /// `OsslParamArray` constructed using `OsslParamBuilder`.
34 /// Internally this is a pointer to an array of the OSSL_PARAM
35 /// structures.
36 pub struct OsslParamArray;
37 /// Reference to `OsslParamArray`.
38 pub struct OsslParamArrayRef;
39 }
40
41 impl OsslParamArray {
42 /// Locates the individual `OSSL_PARAM` element representing an
43 /// octet string identified by the key in the `OsslParamArray`
44 /// array and returns a reference to it.
45 ///
46 /// Combines OSSL_PARAM_locate and OSSL_PARAM_get_octet_string.
47 #[corresponds(OSSL_PARAM_get_octet_string)]
48 #[allow(dead_code)] // TODO: remove when when used by ML-DSA / ML-KEM
49 pub(crate) fn locate_octet_string<'a>(&'a self, key: &CStr) -> Result<&'a [u8], ErrorStack> {
50 unsafe {
51 let param = cvt_p(ffi::OSSL_PARAM_locate(self.as_ptr(), key.as_ptr()))?;
52 let mut val: *const c_void = ptr::null_mut();
53 let mut val_len: usize = 0;
54 cvt(ffi::OSSL_PARAM_get_octet_string_ptr(
55 param,
56 &mut val,
57 &mut val_len,
58 ))?;
59 Ok(util::from_raw_parts(val as *const u8, val_len))
60 }
61 }
62 }
63
64 foreign_type_and_impl_send_sync! {
65 type CType = ffi::OSSL_PARAM_BLD;
66 fn drop = ffi::OSSL_PARAM_BLD_free;
67
68 /// Builder used to construct `OsslParamArray`.
69 pub struct OsslParamBuilderInternal;
70 /// Reference to `OsslParamBuilderInternal`.
71 pub struct OsslParamBuilderRefInternal;
72 }
73
74 /// Wrapper around the internal OsslParamBuilderInternal that adds lifetime management
75 /// since the builder does not own the key and value data that is added to it.
76 pub struct OsslParamBuilder<'a> {
77 builder: OsslParamBuilderInternal,
78 _marker: PhantomData<&'a ()>,
79 }
80
81 impl<'a> OsslParamBuilder<'a> {
82 /// Returns a builder for an OsslParamArray.
83 ///
84 /// The array is initially empty.
85 #[corresponds(OSSL_PARAM_BLD_new)]
86 #[cfg_attr(any(not(ossl320), osslconf = "OPENSSL_NO_ARGON2"), allow(dead_code))]
87 pub(crate) fn new() -> Result<OsslParamBuilder<'a>, ErrorStack> {
88 unsafe {
89 ffi::init();
90
91 cvt_p(ffi::OSSL_PARAM_BLD_new()).map(|builder| OsslParamBuilder {
92 builder: OsslParamBuilderInternal(builder),
93 _marker: PhantomData,
94 })
95 }
96 }
97
98 /// Constructs the `OsslParamArray` and clears this builder.
99 #[corresponds(OSSL_PARAM_BLD_to_param)]
100 #[cfg_attr(any(not(ossl320), osslconf = "OPENSSL_NO_ARGON2"), allow(dead_code))]
101 #[allow(clippy::wrong_self_convention)]
102 pub(crate) fn to_param(&'a mut self) -> Result<OsslParamArray, ErrorStack> {
103 unsafe {
104 let params = cvt_p(ffi::OSSL_PARAM_BLD_to_param(self.as_ptr()))?;
105 Ok(OsslParamArray::from_ptr(params))
106 }
107 }
108
109 /// Adds an utf8 string to `OsslParamBuilder`.
110 #[corresponds(OSSL_PARAM_BLD_push_utf8_string)]
111 pub(crate) fn add_utf8_string(
112 &mut self,
113 key: &'a CStr,
114 buf: &'a [u8],
115 ) -> Result<(), ErrorStack> {
116 unsafe {
117 cvt(ffi::OSSL_PARAM_BLD_push_utf8_string(
118 self.as_ptr(),
119 key.as_ptr(),
120 buf.as_ptr() as *const c_char,
121 buf.len(),
122 ))
123 .map(|_| ())
124 }
125 }
126
127 /// Adds a octet string to `OsslParamBuilder`.
128 #[corresponds(OSSL_PARAM_BLD_push_octet_string)]
129 #[cfg_attr(any(not(ossl320), osslconf = "OPENSSL_NO_ARGON2"), allow(dead_code))]
130 pub(crate) fn add_octet_string(
131 &mut self,
132 key: &'a CStr,
133 buf: &'a [u8],
134 ) -> Result<(), ErrorStack> {
135 unsafe {
136 cvt(ffi::OSSL_PARAM_BLD_push_octet_string(
137 self.as_ptr(),
138 key.as_ptr(),
139 buf.as_ptr() as *const c_void,
140 buf.len(),
141 ))
142 .map(|_| ())
143 }
144 }
145
146 /// Adds a int to `OsslParamBuilder`.
147 #[corresponds(OSSL_PARAM_BLD_push_int)]
148 pub(crate) fn add_int(&mut self, key: &'a CStr, val: i32) -> Result<(), ErrorStack> {
149 unsafe {
150 cvt(ffi::OSSL_PARAM_BLD_push_int(
151 self.as_ptr(),
152 key.as_ptr(),
153 val as c_int,
154 ))
155 .map(|_| ())
156 }
157 }
158
159 /// Adds a unsigned int to `OsslParamBuilder`.
160 #[corresponds(OSSL_PARAM_BLD_push_uint)]
161 #[cfg_attr(any(not(ossl320), osslconf = "OPENSSL_NO_ARGON2"), allow(dead_code))]
162 pub(crate) fn add_uint(&mut self, key: &'a CStr, val: u32) -> Result<(), ErrorStack> {
163 unsafe {
164 cvt(ffi::OSSL_PARAM_BLD_push_uint(
165 self.as_ptr(),
166 key.as_ptr(),
167 val as c_uint,
168 ))
169 .map(|_| ())
170 }
171 }
172
173 /// Returns a raw pointer to the underlying `OSSL_PARAM_BLD` structure.
174 pub(crate) unsafe fn as_ptr(&mut self) -> *mut ffi::OSSL_PARAM_BLD {
175 self.builder.as_ptr()
176 }
177 }
178
179 #[cfg(test)]
180 mod tests {
181 use super::*;
182 #[test]
183 fn test_builder_locate_octet_string() {
184 let mut builder = OsslParamBuilder::new().unwrap();
185 builder.add_octet_string(c"key1", b"value1").unwrap();
186 let params = builder.to_param().unwrap();
187
188 assert!(params.locate_octet_string(c"invalid").is_err());
189 assert_eq!(params.locate_octet_string(c"key1").unwrap(), b"value1");
190 }
191 }