confium 0.3.3 → 0.4.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.
data/README.adoc CHANGED
@@ -10,7 +10,7 @@ Confium supports three deployment modes:
10
10
  * **Mode 2 — TC PKI replacement**: drop-in for existing PKI consumers (PKCS#11 server, OpenSSL 3.0 provider, JCE)
11
11
  * **Mode 3 — TC Certificate PKI**: institutional deployments with custom certificate formats (OIML CNML, BIPM, pharma, accreditation)
12
12
 
13
- This gem wraps the high-value Confium subsystems via a Rust native extension. Pre-compiled platform gems are published for Linux and macOS, so `gem install` needs no Rust toolchain there; other platforms build from source at install time with `rb_sys` + `magnus`. No separate C ABI library to install; everything is statically linked into the extension.
13
+ This gem wraps the high-value Confium subsystems via a pure-Rust native extension (`rb_sys` + `magnus`, no C dependencies). Pre-compiled platform gems are published for Linux (glibc + musl), macOS, and Windows, so `gem install` needs no Rust toolchain there; other platforms build from source at install time.
14
14
 
15
15
  == Installation
16
16
 
@@ -32,17 +32,13 @@ $ gem install confium
32
32
 
33
33
  * Ruby ≥ 3.1
34
34
  * Nothing else on the pre-compiled platforms: `x86_64-linux`,
35
- `aarch64-linux`, `x86_64-darwin`, `arm64-darwin` (each gem carries
36
- one extension per Ruby C-ABI window)
35
+ `aarch64-linux`, `x86_64-linux-musl`, `aarch64-linux-musl`,
36
+ `x86_64-darwin`, `arm64-darwin`, `x64-mingw-ucrt` (each gem
37
+ carries one extension per Ruby C-ABI window)
37
38
 
38
39
  Source builds (other platforms, or installing from the repo) also
39
- need:
40
-
41
- * Rust stable toolchain (`rustup default stable`)
42
- * C toolchain (clang/gcc/Xcode CLT)
43
-
44
- There is no separate `libconfium` to install — the extension
45
- statically links everything.
40
+ need the Rust stable toolchain (`rustup default stable`) — and
41
+ nothing else: the extension has no C dependencies to satisfy.
46
42
 
47
43
  == Quick start
48
44
 
@@ -55,9 +55,5 @@ chrono = "0.4"
55
55
  hex = "0.4"
56
56
  serde_json = "1"
57
57
 
58
- # RNP (OpenPGP, RFC 9580) — hard-bundled with vendored feature so
59
- # librnp is compiled from source via rnp-src. No system librnp needed.
60
- rnp = { package = "rnp-rs", version = "0.1.10", features = ["vendored"] }
61
-
62
58
  [profile.release]
63
59
  lto = "off"
@@ -12,7 +12,7 @@
12
12
 
13
13
  use confium_attributes::{evaluate, parse as dsl_parse, Predicate, SignerAttributes};
14
14
  use magnus::{
15
- exception, function, method, typed_data::Obj, DataTypeFunctions, Error, Module,
15
+ function, method, typed_data::Obj, DataTypeFunctions, Error, Module,
16
16
  Object, Ruby, TryConvert, TypedData, Value,
17
17
  };
18
18
 
@@ -26,8 +26,8 @@ impl PredicateWrap {
26
26
  fn satisfied_by(&self, signers_value: Value) -> Result<bool, Error> {
27
27
  let arr = magnus::RArray::try_convert(signers_value)?;
28
28
  let mut owned: Vec<SignerAttributes> = Vec::with_capacity(arr.len());
29
- for v in arr.each() {
30
- let signer_wrap = Obj::<SignerWrap>::try_convert(v?)?;
29
+ for v in arr.into_iter() {
30
+ let signer_wrap = Obj::<SignerWrap>::try_convert(v)?;
31
31
  owned.push(signer_wrap.inner.borrow().clone());
32
32
  }
33
33
  let refs: Vec<&SignerAttributes> = owned.iter().collect();
@@ -64,7 +64,7 @@ impl SignerWrap {
64
64
  fn parse(expr: String) -> Result<Obj<PredicateWrap>, Error> {
65
65
  let predicate = dsl_parse(&expr)
66
66
  .map_err(|e| crate::util::parse_error(e.to_string(), "Attributes.parse", Some("dsl"), None))?;
67
- let ruby = Ruby::get().map_err(|e| Error::new(exception::runtime_error(), e.to_string()))?;
67
+ let ruby = Ruby::get().map_err(|e| crate::util::runtime(e.to_string()))?;
68
68
  Ok(ruby.obj_wrap(PredicateWrap { inner: predicate }))
69
69
  }
70
70
 
@@ -11,7 +11,7 @@
11
11
  //! sink raises, the calling operation still succeeds. Audit
12
12
  //! failures must never break a signing ceremony.
13
13
 
14
- use magnus::{exception, function, prelude::*, Error, Module, Ruby, Value};
14
+ use magnus::{function, prelude::*, Error, Module, Ruby, Value};
15
15
  use sha2::{Digest, Sha256};
16
16
 
17
17
  const SINK_IVAR: &str = "@sink";
@@ -94,12 +94,12 @@ fn record(
94
94
  algorithm: Option<String>,
95
95
  error: Option<String>,
96
96
  ) -> Result<(), Error> {
97
- let ruby = Ruby::get().map_err(|e| Error::new(exception::runtime_error(), e.to_string()))?;
97
+ let ruby = Ruby::get().map_err(|e| crate::util::runtime(e.to_string()))?;
98
98
  let audit_mod: magnus::RModule = ruby
99
99
  .class_object()
100
100
  .const_get("Confium")
101
101
  .and_then(|m: magnus::RModule| m.const_get("Audit"))
102
- .map_err(|e| Error::new(exception::runtime_error(), e.to_string()))?;
102
+ .map_err(|e| crate::util::runtime(e.to_string()))?;
103
103
 
104
104
  let sink: Value = audit_mod.ivar_get(SINK_IVAR)?;
105
105
  if sink.is_nil() {
@@ -123,31 +123,31 @@ fn record(
123
123
 
124
124
  let _: Value = sink
125
125
  .funcall("call", (h,))
126
- .map_err(|e| Error::new(exception::runtime_error(), e.to_string()))?;
126
+ .map_err(|e| crate::util::runtime(e.to_string()))?;
127
127
  Ok(())
128
128
  }
129
129
 
130
130
  /// Set the global audit sink. `callback` is a Proc that receives a Hash
131
131
  /// or nil to disable auditing.
132
132
  fn set_sink(callback: Value) -> Result<(), Error> {
133
- let ruby = Ruby::get().map_err(|e| Error::new(exception::runtime_error(), e.to_string()))?;
133
+ let ruby = Ruby::get().map_err(|e| crate::util::runtime(e.to_string()))?;
134
134
  let audit_mod: magnus::RModule = ruby
135
135
  .class_object()
136
136
  .const_get("Confium")
137
137
  .and_then(|m: magnus::RModule| m.const_get("Audit"))
138
- .map_err(|e| Error::new(exception::runtime_error(), e.to_string()))?;
138
+ .map_err(|e| crate::util::runtime(e.to_string()))?;
139
139
  audit_mod.ivar_set(SINK_IVAR, callback)?;
140
140
  Ok(())
141
141
  }
142
142
 
143
143
  /// Get the current audit sink (or nil if not set).
144
144
  fn get_sink() -> Result<Value, Error> {
145
- let ruby = Ruby::get().map_err(|e| Error::new(exception::runtime_error(), e.to_string()))?;
145
+ let ruby = Ruby::get().map_err(|e| crate::util::runtime(e.to_string()))?;
146
146
  let audit_mod: magnus::RModule = ruby
147
147
  .class_object()
148
148
  .const_get("Confium")
149
149
  .and_then(|m: magnus::RModule| m.const_get("Audit"))
150
- .map_err(|e| Error::new(exception::runtime_error(), e.to_string()))?;
150
+ .map_err(|e| crate::util::runtime(e.to_string()))?;
151
151
  audit_mod.ivar_get(SINK_IVAR)
152
152
  }
153
153
 
@@ -14,7 +14,7 @@
14
14
  use confium_composite::{CompositeSignature, ComponentSignature, VerificationResult};
15
15
  use ed25519_dalek::SigningKey;
16
16
  use magnus::{
17
- exception, function, method, prelude::*, scan_args,
17
+ function, method, prelude::*, scan_args,
18
18
  typed_data::Obj, DataTypeFunctions, Error, IntoValue,
19
19
  Module, Object, RHash, Ruby, TryConvert, TypedData, Value,
20
20
  };
@@ -79,7 +79,7 @@ impl CompositeSig {
79
79
  let details = new_details(&ruby);
80
80
  confium_error(e.to_string(), "VerificationError", details)
81
81
  })?;
82
- let ruby = Ruby::get().map_err(|e| Error::new(exception::runtime_error(), e.to_string()))?;
82
+ let ruby = Ruby::get().map_err(|e| crate::util::runtime(e.to_string()))?;
83
83
  Ok(ruby.obj_wrap(VerificationResultWrap { inner: result }))
84
84
  }
85
85
  }
@@ -133,7 +133,7 @@ impl VerificationResultWrap {
133
133
  }
134
134
 
135
135
  fn per_component(&self) -> Result<Value, Error> {
136
- let ruby = Ruby::get().map_err(|e| Error::new(exception::runtime_error(), e.to_string()))?;
136
+ let ruby = Ruby::get().map_err(|e| crate::util::runtime(e.to_string()))?;
137
137
  let result = ruby.hash_new();
138
138
  for c in &self.inner.per_component {
139
139
  let entry = ruby.hash_new();
@@ -156,9 +156,7 @@ fn sign_ed25519(ruby: &Ruby, private_key: Value, message: Value) -> Result<RHash
156
156
  let pk_bytes = bytes_from_value(private_key)?;
157
157
  let msg = bytes_from_value(message)?;
158
158
  if pk_bytes.len() != 32 {
159
- return Err(Error::new(
160
- exception::arg_error(),
161
- format!("Ed25519 private key must be 32 bytes, got {}", pk_bytes.len()),
159
+ return Err(crate::util::arg_error(format!("Ed25519 private key must be 32 bytes, got {}", pk_bytes.len()),
162
160
  ));
163
161
  }
164
162
  let mut pk_arr = [0u8; 32];
@@ -174,7 +172,7 @@ fn sign_ed25519(ruby: &Ruby, private_key: Value, message: Value) -> Result<RHash
174
172
  Some(&msg),
175
173
  Some(&e.to_string()),
176
174
  );
177
- return Err(Error::new(exception::runtime_error(), e.to_string()));
175
+ return Err(crate::util::crypto_error(e.to_string(), "Composite.sign_ed25519", "ed25519"));
178
176
  }
179
177
  };
180
178
 
@@ -204,16 +202,14 @@ fn sign_p256(ruby: &Ruby, private_key: Value, message: Value) -> Result<RHash, E
204
202
  let pk_bytes = bytes_from_value(private_key)?;
205
203
  let msg = bytes_from_value(message)?;
206
204
  if pk_bytes.len() != 32 {
207
- return Err(Error::new(
208
- exception::arg_error(),
209
- format!("P-256 private key must be 32 bytes, got {}", pk_bytes.len()),
205
+ return Err(crate::util::arg_error(format!("P-256 private key must be 32 bytes, got {}", pk_bytes.len()),
210
206
  ));
211
207
  }
212
208
  let mut arr = [0u8; 32];
213
209
  arr.copy_from_slice(&pk_bytes);
214
210
  use p256::ecdsa::{Signature, SigningKey, signature::Signer};
215
211
  let signing = SigningKey::from_bytes(&arr.into())
216
- .map_err(|e| Error::new(exception::arg_error(), format!("invalid P-256 private key: {e}")))?;
212
+ .map_err(|e| crate::util::arg_error(format!("invalid P-256 private key: {e}")))?;
217
213
  let sig: Signature = match signing.try_sign(msg.as_slice()) {
218
214
  Ok(s) => s,
219
215
  Err(e) => {
@@ -224,7 +220,7 @@ fn sign_p256(ruby: &Ruby, private_key: Value, message: Value) -> Result<RHash, E
224
220
  Some(&msg),
225
221
  Some(&format!("sign error: {e}")),
226
222
  );
227
- return Err(Error::new(exception::runtime_error(), format!("sign error: {e}")));
223
+ return Err(crate::util::crypto_error(format!("sign error: {e}"), "Composite.sign_p256", "ecdsa-p256"));
228
224
  }
229
225
  };
230
226
  let verifying = signing.verifying_key();
@@ -259,8 +255,8 @@ fn generate_ed25519_keypair(ruby: &Ruby) -> Result<RHash, Error> {
259
255
  fn parse_components(value: Value) -> Result<Vec<ComponentSignature>, Error> {
260
256
  let arr = magnus::RArray::try_convert(value)?;
261
257
  let mut out = Vec::with_capacity(arr.len());
262
- for v in arr.each() {
263
- let h: RHash = RHash::try_convert(v?)?;
258
+ for v in arr.into_iter() {
259
+ let h: RHash = RHash::try_convert(v)?;
264
260
  let algorithm: String = h.fetch::<_, String>("algorithm")?;
265
261
  let public_key: Value = h.fetch::<_, Value>("public_key")?;
266
262
  let signature: Value = h.fetch::<_, Value>("signature")?;
@@ -12,7 +12,7 @@ use confium_deployment::{
12
12
  validate::validate_manifest,
13
13
  };
14
14
  use crate::util::{enforce_size, parse_error};
15
- use magnus::{exception, function, method, typed_data::Obj, DataTypeFunctions, Error, Module, Object, Ruby, TypedData};
15
+ use magnus::{function, method, typed_data::Obj, DataTypeFunctions, Error, Module, Object, Ruby, TypedData};
16
16
 
17
17
  #[derive(TypedData, DataTypeFunctions)]
18
18
  #[magnus(class = "Confium::Identity::Actor", size)]
@@ -25,7 +25,7 @@ impl Actor {
25
25
  enforce_size(json.len())?;
26
26
  let actor: ActorIdentity = serde_json::from_str(&json)
27
27
  .map_err(|e| parse_error(e.to_string(), "Actor.from_json", Some("json"), None))?;
28
- let ruby = Ruby::get().map_err(|e| Error::new(exception::runtime_error(), e.to_string()))?;
28
+ let ruby = Ruby::get().map_err(|e| crate::util::runtime(e.to_string()))?;
29
29
  Ok(ruby.obj_wrap(Self {
30
30
  inner: std::cell::RefCell::new(actor),
31
31
  }))
@@ -33,7 +33,7 @@ impl Actor {
33
33
 
34
34
  fn to_json(&self) -> Result<String, Error> {
35
35
  serde_json::to_string(&*self.inner.borrow())
36
- .map_err(|e| Error::new(exception::runtime_error(), e.to_string()))
36
+ .map_err(|e| crate::util::runtime(e.to_string()))
37
37
  }
38
38
 
39
39
  fn actor_id(&self) -> String {
@@ -94,7 +94,7 @@ impl Manifest {
94
94
  enforce_size(toml_str.len())?;
95
95
  let manifest = parse_manifest(&toml_str)
96
96
  .map_err(|e| parse_error(e.to_string(), "Manifest.from_toml", Some("toml"), None))?;
97
- let ruby = Ruby::get().map_err(|e| Error::new(exception::runtime_error(), e.to_string()))?;
97
+ let ruby = Ruby::get().map_err(|e| crate::util::runtime(e.to_string()))?;
98
98
  Ok(ruby.obj_wrap(Self {
99
99
  inner: std::cell::RefCell::new(manifest),
100
100
  }))
@@ -123,9 +123,10 @@ impl Manifest {
123
123
  .get(index)
124
124
  .map(|t| t.name.clone())
125
125
  .ok_or_else(|| {
126
- Error::new(
127
- exception::index_error(),
126
+ crate::util::index_error(
128
127
  format!("tier index {index} out of range"),
128
+ "Manifest.tier_name_at",
129
+ Some(index as u64),
129
130
  )
130
131
  })
131
132
  }
@@ -5,7 +5,7 @@ use confium_transparency::ers::{
5
5
  EvidenceRecord, HashAlgorithm,
6
6
  };
7
7
  use magnus::{
8
- exception, function, method, typed_data::Obj,
8
+ function, method, typed_data::Obj,
9
9
  DataTypeFunctions, Error, Module, Object, Ruby, TryConvert, TypedData, Value,
10
10
  };
11
11
 
@@ -20,13 +20,11 @@ pub struct ErsRecord {
20
20
  impl ErsRecord {
21
21
  fn build_initial(args: &[Value]) -> Result<Obj<Self>, Error> {
22
22
  let first = args.first().ok_or_else(|| {
23
- Error::new(exception::arg_error(), "data_hash required")
23
+ crate::util::arg_error("data_hash required")
24
24
  })?;
25
25
  let data_hash = bytes_from_value(*first)?;
26
26
  if data_hash.len() != 32 {
27
- return Err(Error::new(
28
- exception::arg_error(),
29
- format!("data_hash must be 32 bytes, got {}", data_hash.len()),
27
+ return Err(crate::util::arg_error(format!("data_hash must be 32 bytes, got {}", data_hash.len()),
30
28
  ));
31
29
  }
32
30
  let mut hash = [0u8; 32];
@@ -42,7 +40,7 @@ impl ErsRecord {
42
40
  let record =
43
41
  build_initial_evidence_record(hash, HashAlgorithm::Sha256, tsa_id, token_bytes);
44
42
  let ruby = Ruby::get()
45
- .map_err(|e| Error::new(exception::runtime_error(), e.to_string()))?;
43
+ .map_err(|e| crate::util::runtime(e.to_string()))?;
46
44
  Ok(ruby.obj_wrap(Self {
47
45
  inner: std::cell::RefCell::new(record),
48
46
  }))
@@ -50,13 +48,11 @@ impl ErsRecord {
50
48
 
51
49
  fn renew(&self, args: &[Value]) -> Result<Obj<Self>, Error> {
52
50
  let first = args.first().ok_or_else(|| {
53
- Error::new(exception::arg_error(), "new_hash required")
51
+ crate::util::arg_error("new_hash required")
54
52
  })?;
55
53
  let new_hash_bytes = bytes_from_value(*first)?;
56
54
  if new_hash_bytes.len() != 32 {
57
- return Err(Error::new(
58
- exception::arg_error(),
59
- format!("new_hash must be 32 bytes, got {}", new_hash_bytes.len()),
55
+ return Err(crate::util::arg_error(format!("new_hash must be 32 bytes, got {}", new_hash_bytes.len()),
60
56
  ));
61
57
  }
62
58
  let mut hash = [0u8; 32];
@@ -72,7 +68,7 @@ impl ErsRecord {
72
68
  let mut cloned = self.inner.borrow().clone();
73
69
  renew_evidence_record(&mut cloned, HashAlgorithm::Sha256, hash, tsa_id, token_bytes);
74
70
  let ruby = Ruby::get()
75
- .map_err(|e| Error::new(exception::runtime_error(), e.to_string()))?;
71
+ .map_err(|e| crate::util::runtime(e.to_string()))?;
76
72
  Ok(ruby.obj_wrap(Self {
77
73
  inner: std::cell::RefCell::new(cloned),
78
74
  }))
@@ -9,7 +9,6 @@ mod attributes;
9
9
  mod composite;
10
10
  mod deployment;
11
11
  mod ers;
12
- mod openpgp;
13
12
  mod path;
14
13
  mod pki;
15
14
  mod tc;
@@ -51,6 +50,5 @@ fn init(ruby: &Ruby) -> Result<(), Error> {
51
50
  tc::init(ruby, confium)?;
52
51
  audit::init(ruby, confium)?;
53
52
  ers::init(ruby, confium)?;
54
- openpgp::init(ruby, confium)?;
55
53
  Ok(())
56
54
  }
@@ -10,7 +10,7 @@ use confium_pki::{
10
10
  path::{validate_path, CertPath},
11
11
  result::VerificationResult as PathVerificationResult,
12
12
  };
13
- use magnus::{exception, function, method, prelude::*, DataTypeFunctions, Error, Module, Ruby, TypedData, Value};
13
+ use magnus::{function, method, prelude::*, DataTypeFunctions, Error, Module, Ruby, TypedData, Value};
14
14
 
15
15
  /// Wraps a confium_pki::path::VerificationResult.
16
16
  #[derive(TypedData, DataTypeFunctions)]
@@ -63,17 +63,17 @@ fn validate(args: &[Value]) -> Result<magnus::typed_data::Obj<PathResult>, Error
63
63
  Vec::new()
64
64
  } else {
65
65
  let arr = magnus::RArray::try_convert(intermediates_value)
66
- .map_err(|e| Error::new(exception::arg_error(), e.to_string()))?;
66
+ .map_err(|e| crate::util::arg_error(e.to_string()))?;
67
67
  let mut out = Vec::with_capacity(arr.len());
68
- for v in arr.each() {
69
- out.push(extract_cert(v?, "intermediate")?);
68
+ for v in arr.into_iter() {
69
+ out.push(extract_cert(v, "intermediate")?);
70
70
  }
71
71
  out
72
72
  };
73
73
 
74
74
  let now = match now_iso8601 {
75
75
  Some(s) => chrono::DateTime::parse_from_rfc3339(&s)
76
- .map_err(|e| Error::new(exception::arg_error(), format!("invalid time: {e}")))?
76
+ .map_err(|e| crate::util::arg_error(format!("invalid time: {e}")))?
77
77
  .with_timezone(&Utc),
78
78
  None => Utc::now(),
79
79
  };
@@ -86,7 +86,7 @@ fn validate(args: &[Value]) -> Result<magnus::typed_data::Obj<PathResult>, Error
86
86
  let result = validate_path(&path, now);
87
87
 
88
88
  let ruby = Ruby::get()
89
- .map_err(|e| Error::new(exception::runtime_error(), e.to_string()))?;
89
+ .map_err(|e| crate::util::runtime(e.to_string()))?;
90
90
  Ok(ruby.obj_wrap(PathResult { inner: result }))
91
91
  }
92
92
 
@@ -98,10 +98,10 @@ fn extract_cert(value: Value, label: &str) -> Result<RustCert, Error> {
98
98
  // We re-parse those bytes into a fresh RustCert.
99
99
  let der_value: Value = value
100
100
  .funcall("to_der", ())
101
- .map_err(|e| Error::new(exception::runtime_error(), format!("{}: cannot get DER: {e}", label)))?;
101
+ .map_err(|e| crate::util::parse_error(format!("{label}: cannot get DER: {e}"), "PathValidator.validate", Some("der"), None))?;
102
102
  let der = crate::util::bytes_from_value(der_value)?;
103
103
  RustCert::from_der(&der)
104
- .map_err(|e| Error::new(exception::runtime_error(), format!("{}: invalid DER: {e}", label)))
104
+ .map_err(|e| crate::util::parse_error(format!("{label}: invalid DER: {e}"), "PathValidator.validate", Some("der"), None))
105
105
  }
106
106
 
107
107
  pub fn init(ruby: &Ruby, parent: magnus::RModule) -> Result<(), Error> {
@@ -20,7 +20,7 @@ use confium_pki::{
20
20
  xmldsig::{canonicalize, canonicalize_exclusive},
21
21
  };
22
22
  use magnus::{
23
- exception, function, method, prelude::*, typed_data::Obj, DataTypeFunctions, Error, Module,
23
+ function, method, prelude::*, typed_data::Obj, DataTypeFunctions, Error, Module,
24
24
  Object, RHash, RString, Ruby, TryConvert, TypedData, Value,
25
25
  };
26
26
 
@@ -35,7 +35,7 @@ impl Certificate {
35
35
  let der = bytes_from_value(bytes)?;
36
36
  let cert = RustCert::from_der(&der)
37
37
  .map_err(|e| parse_error(e.to_string(), "Certificate.from_der", Some("der"), None))?;
38
- let ruby = Ruby::get().map_err(|e| Error::new(exception::runtime_error(), e.to_string()))?;
38
+ let ruby = Ruby::get().map_err(|e| crate::util::runtime(e.to_string()))?;
39
39
  Ok(ruby.obj_wrap(Self { inner: cert }))
40
40
  }
41
41
 
@@ -43,12 +43,12 @@ impl Certificate {
43
43
  enforce_size(pem.len())?;
44
44
  let cert = RustCert::from_pem(&pem)
45
45
  .map_err(|e| parse_error(e.to_string(), "Certificate.from_pem", Some("pem"), None))?;
46
- let ruby = Ruby::get().map_err(|e| Error::new(exception::runtime_error(), e.to_string()))?;
46
+ let ruby = Ruby::get().map_err(|e| crate::util::runtime(e.to_string()))?;
47
47
  Ok(ruby.obj_wrap(Self { inner: cert }))
48
48
  }
49
49
 
50
50
  fn to_der(&self) -> Result<RString, Error> {
51
- let ruby = Ruby::get().map_err(|e| Error::new(exception::runtime_error(), e.to_string()))?;
51
+ let ruby = Ruby::get().map_err(|e| crate::util::runtime(e.to_string()))?;
52
52
  Ok(bytes_to_rstring(&ruby, &self.inner.to_der()))
53
53
  }
54
54
 
@@ -81,13 +81,13 @@ impl Certificate {
81
81
 
82
82
  fn valid_at(&self, iso8601: String) -> Result<bool, Error> {
83
83
  let now = DateTime::parse_from_rfc3339(&iso8601)
84
- .map_err(|e| Error::new(exception::arg_error(), format!("invalid ISO8601 time: {e}")))?
84
+ .map_err(|e| crate::util::arg_error(format!("invalid ISO8601 time: {e}")))?
85
85
  .with_timezone(&Utc);
86
86
  Ok(self.inner.is_within_validity(now))
87
87
  }
88
88
 
89
89
  fn public_key_bytes(&self) -> Result<RString, Error> {
90
- let ruby = Ruby::get().map_err(|e| Error::new(exception::runtime_error(), e.to_string()))?;
90
+ let ruby = Ruby::get().map_err(|e| crate::util::runtime(e.to_string()))?;
91
91
  Ok(bytes_to_rstring(&ruby, self.inner.public_key_bytes()))
92
92
  }
93
93
  }
@@ -103,7 +103,7 @@ impl Csr {
103
103
  let der = bytes_from_value(bytes)?;
104
104
  let csr = RustCsr::from_der(&der)
105
105
  .map_err(|e| parse_error(e.to_string(), "Csr.from_der", Some("der"), None))?;
106
- let ruby = Ruby::get().map_err(|e| Error::new(exception::runtime_error(), e.to_string()))?;
106
+ let ruby = Ruby::get().map_err(|e| crate::util::runtime(e.to_string()))?;
107
107
  Ok(ruby.obj_wrap(Self { inner: csr }))
108
108
  }
109
109
 
@@ -111,12 +111,12 @@ impl Csr {
111
111
  enforce_size(pem.len())?;
112
112
  let csr = RustCsr::from_pem(&pem)
113
113
  .map_err(|e| parse_error(e.to_string(), "Csr.from_pem", Some("pem"), None))?;
114
- let ruby = Ruby::get().map_err(|e| Error::new(exception::runtime_error(), e.to_string()))?;
114
+ let ruby = Ruby::get().map_err(|e| crate::util::runtime(e.to_string()))?;
115
115
  Ok(ruby.obj_wrap(Self { inner: csr }))
116
116
  }
117
117
 
118
118
  fn to_der(&self) -> Result<RString, Error> {
119
- let ruby = Ruby::get().map_err(|e| Error::new(exception::runtime_error(), e.to_string()))?;
119
+ let ruby = Ruby::get().map_err(|e| crate::util::runtime(e.to_string()))?;
120
120
  Ok(bytes_to_rstring(&ruby, &self.inner.to_der()))
121
121
  }
122
122
 
@@ -136,7 +136,7 @@ impl SignedData {
136
136
  enforce_size(json.len())?;
137
137
  let sd: RustSignedData = serde_json::from_str(&json)
138
138
  .map_err(|e| parse_error(e.to_string(), "SignedData.from_json", Some("json"), None))?;
139
- let ruby = Ruby::get().map_err(|e| Error::new(exception::runtime_error(), e.to_string()))?;
139
+ let ruby = Ruby::get().map_err(|e| crate::util::runtime(e.to_string()))?;
140
140
  Ok(ruby.obj_wrap(Self {
141
141
  inner: std::cell::RefCell::new(sd),
142
142
  }))
@@ -162,15 +162,11 @@ impl SignedData {
162
162
  certificates: Value,
163
163
  ) -> Result<Obj<Self>, Error> {
164
164
  if signature.is_nil() {
165
- return Err(Error::new(
166
- exception::arg_error(),
167
- "signature is required",
165
+ return Err(crate::util::arg_error("signature is required",
168
166
  ));
169
167
  }
170
168
  if certificates.is_nil() {
171
- return Err(Error::new(
172
- exception::arg_error(),
173
- "certificates is required",
169
+ return Err(crate::util::arg_error("certificates is required",
174
170
  ));
175
171
  }
176
172
 
@@ -179,16 +175,15 @@ impl SignedData {
179
175
 
180
176
  let certs_array: magnus::RArray = magnus::RArray::try_convert(certificates)?;
181
177
  let mut cert_ders = Vec::with_capacity(certs_array.len());
182
- for item in certs_array.each() {
183
- let v = item?;
178
+ for v in certs_array.into_iter() {
184
179
  let der = bytes_from_value(v)?;
185
180
  enforce_size(der.len())?;
186
181
  cert_ders.push(der);
187
182
  }
188
183
 
189
- let ruby = Ruby::get().map_err(|e| Error::new(exception::runtime_error(), e.to_string()))?;
184
+ let ruby = Ruby::get().map_err(|e| crate::util::runtime(e.to_string()))?;
190
185
  let sd = build_detached_signature(Vec::new(), algorithm, sig_bytes, cert_ders)
191
- .map_err(|e| Error::new(exception::runtime_error(), e.to_string()))?;
186
+ .map_err(|e| crate::util::runtime(e.to_string()))?;
192
187
  Ok(ruby.obj_wrap(Self {
193
188
  inner: std::cell::RefCell::new(sd),
194
189
  }))
@@ -196,7 +191,7 @@ impl SignedData {
196
191
 
197
192
  fn to_json(&self) -> Result<String, Error> {
198
193
  serde_json::to_string(&*self.inner.borrow())
199
- .map_err(|e| Error::new(exception::runtime_error(), e.to_string()))
194
+ .map_err(|e| crate::util::runtime(e.to_string()))
200
195
  }
201
196
 
202
197
  /// Encode this SignedData as DER bytes (RFC 5652 ContentInfo).
@@ -204,9 +199,9 @@ impl SignedData {
204
199
  /// The output is parseable by `openssl cms` / `openssl pkcs7` and
205
200
  /// any standards-compliant CMS consumer.
206
201
  fn to_der(&self) -> Result<RString, Error> {
207
- let ruby = Ruby::get().map_err(|e| Error::new(exception::runtime_error(), e.to_string()))?;
202
+ let ruby = Ruby::get().map_err(|e| crate::util::runtime(e.to_string()))?;
208
203
  let der = encode_signed_data_der(&self.inner.borrow())
209
- .map_err(|e| Error::new(exception::runtime_error(), e.to_string()))?;
204
+ .map_err(|e| crate::util::runtime(e.to_string()))?;
210
205
  Ok(bytes_to_rstring(&ruby, &der))
211
206
  }
212
207
 
@@ -225,7 +220,7 @@ impl SignedData {
225
220
  fn content(&self) -> Result<Option<Obj<CertWrapper>>, Error> {
226
221
  // Wrap the optional content bytes in a small value object so Ruby
227
222
  // can ask `.present?` / `.bytes` without juggling nil-vs-string.
228
- let ruby = Ruby::get().map_err(|e| Error::new(exception::runtime_error(), e.to_string()))?;
223
+ let ruby = Ruby::get().map_err(|e| crate::util::runtime(e.to_string()))?;
229
224
  match &self.inner.borrow().encap_content_info.content {
230
225
  Some(bytes) => {
231
226
  let wrapper = CertWrapper {
@@ -242,16 +237,17 @@ impl SignedData {
242
237
  }
243
238
 
244
239
  fn certificate_at(&self, index: usize) -> Result<RString, Error> {
245
- let ruby = Ruby::get().map_err(|e| Error::new(exception::runtime_error(), e.to_string()))?;
240
+ let ruby = Ruby::get().map_err(|e| crate::util::runtime(e.to_string()))?;
246
241
  self.inner
247
242
  .borrow()
248
243
  .certificates
249
244
  .get(index)
250
245
  .map(|c| bytes_to_rstring(&ruby, c))
251
246
  .ok_or_else(|| {
252
- Error::new(
253
- exception::index_error(),
247
+ crate::util::index_error(
254
248
  format!("certificate index {index} out of range"),
249
+ "SignedData.certificate_at",
250
+ Some(index as u64),
255
251
  )
256
252
  })
257
253
  }
@@ -294,8 +290,8 @@ impl SignedData {
294
290
  } else {
295
291
  Err(format!("unsupported signature algorithm OID: {oid}"))
296
292
  }
297
- }).map_err(|e| Error::new(exception::runtime_error(), e.to_string()))?;
298
- let ruby = Ruby::get().map_err(|e| Error::new(exception::runtime_error(), e.to_string()))?;
293
+ }).map_err(|e| crate::util::runtime(e.to_string()))?;
294
+ let ruby = Ruby::get().map_err(|e| crate::util::runtime(e.to_string()))?;
299
295
  Ok(ruby.obj_wrap(CmsVerificationResult { inner: result }))
300
296
  }
301
297
  }
@@ -349,7 +345,7 @@ pub struct CertWrapper {
349
345
 
350
346
  impl CertWrapper {
351
347
  fn bytes(&self) -> Result<RString, Error> {
352
- let ruby = Ruby::get().map_err(|e| Error::new(exception::runtime_error(), e.to_string()))?;
348
+ let ruby = Ruby::get().map_err(|e| crate::util::runtime(e.to_string()))?;
353
349
  Ok(bytes_to_rstring(&ruby, &self.bytes))
354
350
  }
355
351