Debian Codegraph @

third_party/rust/cose/examples/sign_verify/util_test.rs

raw
1 /// We don't need COSE signing at the moment. But we need to generate test files.
2 /// This module implements basic COSE signing.
3 use nss;
4 use {CoseError, Signature, SignatureAlgorithm, SignatureParameters};
5 use std::collections::BTreeMap;
6 use cbor::CborType;
7 use cose::util::get_sig_struct_bytes;
8 use cose::decoder::decode_signature;
9 use cose::decoder::{COSE_TYPE_ES256, COSE_TYPE_ES384, COSE_TYPE_ES512, COSE_TYPE_PS256};
10
11 /// Converts a `SignatureAlgorithm` to its corresponding `CborType`.
12 /// See RFC 8152 section 8.1 and RFC 8230 section 5.1.
13 pub fn signature_type_to_cbor_value(signature_type: &SignatureAlgorithm) -> CborType {
14 CborType::SignedInteger(match signature_type {
15 &SignatureAlgorithm::ES256 => COSE_TYPE_ES256,
16 &SignatureAlgorithm::ES384 => COSE_TYPE_ES384,
17 &SignatureAlgorithm::ES512 => COSE_TYPE_ES512,
18 &SignatureAlgorithm::PS256 => COSE_TYPE_PS256,
19 })
20 }
21
22 pub fn build_protected_sig_header(ee_cert: &[u8], alg: &SignatureAlgorithm) -> CborType {
23 // Protected signature header
24 let mut header_map: BTreeMap<CborType, CborType> = BTreeMap::new();
25
26 // Signature type.
27 let signature_type_value = signature_type_to_cbor_value(alg);
28 header_map.insert(CborType::Integer(1), signature_type_value);
29
30 // Signer certificate.
31 header_map.insert(CborType::Integer(4), CborType::Bytes(ee_cert.to_vec()));
32
33 let header_map = CborType::Map(header_map).serialize();
34 CborType::Bytes(header_map)
35 }
36
37 pub fn build_protected_header(cert_chain: &[&[u8]]) -> CborType {
38 let mut cert_array: Vec<CborType> = Vec::new();
39 for cert in cert_chain {
40 cert_array.push(CborType::Bytes(cert.to_vec()));
41 }
42 let mut protected_body_header: BTreeMap<CborType, CborType> = BTreeMap::new();
43 protected_body_header.insert(CborType::Integer(4), CborType::Array(cert_array));
44 let protected_body_header = CborType::Map(protected_body_header).serialize();
45
46 CborType::Bytes(protected_body_header)
47 }
48
49 pub fn build_sig_struct(ee_cert: &[u8], alg: &SignatureAlgorithm, sig_bytes: &Vec<u8>) -> CborType {
50 // Build the signature item.
51 let mut signature_item: Vec<CborType> = Vec::new();
52
53 // Protected signature header
54 signature_item.push(build_protected_sig_header(ee_cert, alg));
55
56 // The unprotected signature header is empty.
57 let empty_map: BTreeMap<CborType, CborType> = BTreeMap::new();
58 signature_item.push(CborType::Map(empty_map));
59
60 // And finally the signature bytes.
61 signature_item.push(CborType::Bytes(sig_bytes.clone()));
62 CborType::Array(signature_item)
63 }
64
65 // 98(
66 // [
67 // / protected / h'..', / {
68 // \ kid \ 4:'..' \ Array of DER encoded intermediate certificates \
69 // } / ,
70 // / unprotected / {},
71 // / payload / nil, / The payload is the contents of the manifest file /
72 // / signatures / [
73 // [
74 // / protected / h'a2012604..' / {
75 // \ alg \ 1:-7, \ ECDSA with SHA-256 \
76 // \ kid \ 4:'..' \ DER encoded signing certificate \
77 // } / ,
78 // / unprotected / {},
79 // / signature / h'e2ae..'
80 // ],
81 // [
82 // / protected / h'a201382404..' / {
83 // \ alg \ 1:-37, \ RSASSA-PSS with SHA-256 \
84 // \ kid \ 4:'..' \ DER encoded signing certificate \
85 // } / ,
86 // / unprotected / {},
87 // / signature / h'00a2..'
88 // ]
89 // ]
90 // ]
91 pub fn build_cose_signature(cert_chain: &[&[u8]], signature_vec: &Vec<Signature>) -> Vec<u8> {
92 // Building the COSE signature content.
93 let mut cose_signature: Vec<CborType> = Vec::new();
94
95 // add cert chain as protected header
96 cose_signature.push(build_protected_header(cert_chain));
97
98 // Empty map (unprotected header)
99 let empty_map: BTreeMap<CborType, CborType> = BTreeMap::new();
100 cose_signature.push(CborType::Map(empty_map));
101
102 // No payload (nil).
103 cose_signature.push(CborType::Null);
104
105 // Create signature items.
106 let mut signatures: Vec<CborType> = Vec::new();
107 for signature in signature_vec {
108 let signature_item = build_sig_struct(
109 signature.parameter.certificate,
110 &signature.parameter.algorithm,
111 &signature.signature_bytes,
112 );
113 signatures.push(signature_item);
114 }
115
116 // Pack the signature item and add everything to the cose signature object.
117 cose_signature.push(CborType::Array(signatures));
118
119 // A COSE signature is a tagged array (98).
120 let signature_struct = CborType::Tag(98, Box::new(CborType::Array(cose_signature).clone()));
121
122 return signature_struct.serialize();
123 }
124
125 pub fn sign(
126 payload: &[u8],
127 cert_chain: &[&[u8]],
128 parameters: &Vec<SignatureParameters>,
129 ) -> Result<Vec<u8>, CoseError> {
130 assert!(parameters.len() > 0);
131 if parameters.len() < 1 {
132 return Err(CoseError::InvalidArgument);
133 }
134
135 let mut signatures: Vec<Signature> = Vec::new();
136 for param in parameters {
137 // Build the signature structure containing the protected headers and the
138 // payload to generate the payload that is actually signed.
139 let protected_sig_header_serialized =
140 build_protected_sig_header(param.certificate, &param.algorithm);
141 let protected_header_serialized = build_protected_header(cert_chain);
142 let payload = get_sig_struct_bytes(
143 protected_header_serialized,
144 protected_sig_header_serialized,
145 payload,
146 );
147
148 let signature_bytes = match nss::sign(&param.algorithm, &param.pkcs8, &payload) {
149 Err(_) => return Err(CoseError::SigningFailed),
150 Ok(signature) => signature,
151 };
152 let signature = Signature {
153 parameter: param,
154 signature_bytes: signature_bytes,
155 };
156 signatures.push(signature);
157 }
158
159 assert!(signatures.len() > 0);
160 if signatures.len() < 1 {
161 return Err(CoseError::MalformedInput);
162 }
163
164 let cose_signature = build_cose_signature(cert_chain, &signatures);
165 Ok(cose_signature)
166 }
167
168 /// Verify a COSE signature.
169 pub fn verify_signature(payload: &[u8], cose_signature: Vec<u8>) -> Result<(), CoseError> {
170 // Parse COSE signature.
171 let cose_signatures = decode_signature(&cose_signature, payload)?;
172 if cose_signatures.len() < 1 {
173 return Err(CoseError::MalformedInput);
174 }
175
176 for signature in cose_signatures {
177 let signature_algorithm = &signature.signature_type;
178 let signature_bytes = &signature.signature;
179 let real_payload = &signature.to_verify;
180
181 // Verify the parsed signatures.
182 // We ignore the certs field here because we don't verify the certificate.
183 let verify_result = nss::verify_signature(
184 &signature_algorithm,
185 &signature.signer_cert,
186 real_payload,
187 signature_bytes,
188 );
189 if !verify_result.is_ok() {
190 return Err(CoseError::VerificationFailed);
191 }
192 }
193 Ok(())
194 }