pub struct SignerBuilder<'a> { /* private fields */ }Expand description
Builder type to construct an entity that will sign some data.
Instances will be attached to SignedDataBuilder instances where they
will sign data using configured settings.
Implementations§
source§impl<'a> SignerBuilder<'a>
impl<'a> SignerBuilder<'a>
sourcepub fn new(
signing_key: &'a dyn KeyInfoSigner,
signing_certificate: CapturedX509Certificate
) -> Self
pub fn new(
signing_key: &'a dyn KeyInfoSigner,
signing_certificate: CapturedX509Certificate
) -> Self
Construct a new entity that will sign content.
An entity is constructed from a signing key, which is mandatory.
sourcepub fn signature_algorithm(&self) -> Result<SignatureAlgorithm, CmsError>
pub fn signature_algorithm(&self) -> Result<SignatureAlgorithm, CmsError>
Obtain the signature algorithm used by the signing key.
Examples found in repository?
257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449
pub fn build_der(&self) -> Result<Vec<u8>, CmsError> {
let mut signer_infos = SignerInfos::default();
let mut seen_digest_algorithms = HashSet::new();
let mut seen_certificates = self.certificates.clone();
for signer in &self.signers {
seen_digest_algorithms.insert(signer.digest_algorithm);
if !seen_certificates
.iter()
.any(|x| x == &signer.signing_certificate)
{
seen_certificates.push(signer.signing_certificate.clone());
}
let version = CmsVersion::V1;
let digest_algorithm = DigestAlgorithmIdentifier {
algorithm: signer.digest_algorithm.into(),
parameters: None,
};
let sid = SignerIdentifier::IssuerAndSerialNumber(IssuerAndSerialNumber {
issuer: signer.signing_certificate.issuer_name().clone(),
serial_number: signer.signing_certificate.serial_number_asn1().clone(),
});
// The message digest attribute is mandatory.
//
// Message digest is computed from override content on the signer
// or the encapsulated content if present. The "empty" hash is a
// valid value if no content (only signed attributes) are being signed.
let mut hasher = signer.digest_algorithm.digester();
if let Some(content) = &signer.message_id_content {
hasher.update(content);
} else {
match &self.signed_content {
SignedContent::None => {}
SignedContent::Inline(content) | SignedContent::External(content) => {
hasher.update(content)
}
}
}
let digest = hasher.finish();
let mut signed_attributes = SignedAttributes::default();
// The content-type field is mandatory.
signed_attributes.push(Attribute {
typ: Oid(Bytes::copy_from_slice(OID_CONTENT_TYPE.as_ref())),
values: vec![AttributeValue::new(Captured::from_values(
Mode::Der,
signer.content_type.encode_ref(),
))],
});
// Set `messageDigest` field
signed_attributes.push(Attribute {
typ: Oid(Bytes::copy_from_slice(OID_MESSAGE_DIGEST.as_ref())),
values: vec![AttributeValue::new(Captured::from_values(
Mode::Der,
digest.as_ref().encode(),
))],
});
// Add signing time because it is common to include.
signed_attributes.push(Attribute {
typ: Oid(Bytes::copy_from_slice(OID_SIGNING_TIME.as_ref())),
values: vec![AttributeValue::new(Captured::from_values(
Mode::Der,
UtcTime::now().encode(),
))],
});
signed_attributes.extend(signer.extra_signed_attributes.iter().cloned());
// According to RFC 5652, signed attributes are DER encoded. This means a SET
// (which SignedAttributes is) should be sorted. But bcder doesn't appear to do
// this. So we manually sort here.
let signed_attributes = signed_attributes.as_sorted()?;
let signed_attributes = Some(signed_attributes);
let signature_algorithm = signer.signature_algorithm()?.into();
// The function for computing the signed attributes digested content
// is on SignerInfo. So construct an instance so we can compute the
// signature.
let mut signer_info = SignerInfo {
version,
sid,
digest_algorithm,
signed_attributes,
signature_algorithm,
signature: SignatureValue::new(Bytes::copy_from_slice(&[])),
unsigned_attributes: None,
signed_attributes_data: None,
};
// The content being signed is the DER encoded signed attributes, if present, or the
// encapsulated content. Since we always create signed attributes above, it *must* be
// the DER encoded signed attributes.
let signed_content = signer_info
.signed_attributes_digested_content()?
.expect("presence of signed attributes should ensure this is Some(T)");
let signature = signer.signing_key.try_sign(&signed_content)?;
let signature_algorithm = signer.signing_key.signature_algorithm()?;
signer_info.signature = SignatureValue::new(Bytes::from(signature.clone()));
signer_info.signature_algorithm = signature_algorithm.into();
if let Some(url) = &signer.time_stamp_url {
// The message sent to the TSA (via a digest) is the signature of the signed data.
let res = time_stamp_message_http(
url.clone(),
signature.as_ref(),
signer.digest_algorithm,
)?;
if !res.is_success() {
return Err(TimeStampError::Unsuccessful(res.clone()).into());
}
let signed_data = res
.signed_data()?
.ok_or(CmsError::TimeStampProtocol(TimeStampError::BadResponse))?;
let mut unsigned_attributes = UnsignedAttributes::default();
unsigned_attributes.push(Attribute {
typ: Oid(Bytes::copy_from_slice(OID_TIME_STAMP_TOKEN.as_ref())),
values: vec![AttributeValue::new(Captured::from_values(
Mode::Der,
signed_data.encode_ref(),
))],
});
signer_info.unsigned_attributes = Some(unsigned_attributes);
}
signer_infos.push(signer_info);
}
let mut digest_algorithms = DigestAlgorithmIdentifiers::default();
digest_algorithms.extend(seen_digest_algorithms.into_iter().map(|alg| {
DigestAlgorithmIdentifier {
algorithm: alg.into(),
parameters: None,
}
}));
// Many consumers prefer the issuing certificate to come before the issued
// certificate. So we explicitly sort all the seen certificates in this order,
// attempting for all issuing certificates to come before the issued.
seen_certificates.sort_by(|a, b| a.compare_issuer(b));
let mut certificates = CertificateSet::default();
certificates.extend(
seen_certificates
.into_iter()
.map(|cert| CertificateChoices::Certificate(Box::new(cert.into()))),
);
// The certificates could have been encountered in any order. For best results,
// we want issuer certificates before their "children." So we apply sorting here.
let signed_data = SignedData {
version: CmsVersion::V1,
digest_algorithms,
content_info: EncapsulatedContentInfo {
content_type: self.content_type.clone(),
content: match &self.signed_content {
SignedContent::None | SignedContent::External(_) => None,
SignedContent::Inline(content) => {
Some(OctetString::new(Bytes::copy_from_slice(content)))
}
},
},
certificates: if certificates.is_empty() {
None
} else {
Some(certificates)
},
crls: None,
signer_infos,
};
let mut ber = Vec::new();
signed_data
.encode_ref()
.write_encoded(Mode::Der, &mut ber)?;
Ok(ber)
}sourcepub fn message_id_content(self, data: Vec<u8>) -> Self
pub fn message_id_content(self, data: Vec<u8>) -> Self
Define the content to use to calculate the message-id attribute.
In most cases, this is never called and the encapsulated content
embedded within the generated message is used. However, some users
omit storing the data inline and instead use a message-id digest
calculated from a different source. This defines that different source.
sourcepub fn content_type(self, oid: Oid) -> Self
pub fn content_type(self, oid: Oid) -> Self
Define the content type of the signed content.
sourcepub fn signed_attribute(self, typ: Oid, values: Vec<AttributeValue>) -> Self
pub fn signed_attribute(self, typ: Oid, values: Vec<AttributeValue>) -> Self
Add an additional attribute to sign.
sourcepub fn signed_attribute_octet_string(self, typ: Oid, data: &[u8]) -> Self
pub fn signed_attribute_octet_string(self, typ: Oid, data: &[u8]) -> Self
Add an additional OctetString signed attribute.
This is a helper for converting a byte slice to an OctetString and AttributeValue without having to go through low-level ASN.1 code.
sourcepub fn time_stamp_url(self, url: impl IntoUrl) -> Result<Self, Error>
pub fn time_stamp_url(self, url: impl IntoUrl) -> Result<Self, Error>
Obtain a time-stamp token from a server.
If this is called, the URL must be a server implementing the Time-Stamp Protocol (TSP) as defined by RFC 3161. At signature generation time, the server will be contacted and the time stamp token response will be added as an unsigned attribute on the SignedData instance.
Trait Implementations§
source§impl<'a> Clone for SignerBuilder<'a>
impl<'a> Clone for SignerBuilder<'a>
source§fn clone(&self) -> SignerBuilder<'a>
fn clone(&self) -> SignerBuilder<'a>
1.0.0 · source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more