|
| 1 | +// Copyright 2021 Contributors to the Parsec project. |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | +use super::generated_ops::attest_key::{ |
| 4 | + attestation_mechanism_params, attestation_output, AttestationMechanismParams, |
| 5 | + AttestationOutput, Operation as OperationProto, Result as ResultProto, |
| 6 | +}; |
| 7 | +use crate::operations::attest_key::{Operation, Result}; |
| 8 | +use crate::requests::ResponseStatus; |
| 9 | +use log::error; |
| 10 | +use std::convert::TryFrom; |
| 11 | + |
| 12 | +impl TryFrom<Operation> for OperationProto { |
| 13 | + type Error = ResponseStatus; |
| 14 | + |
| 15 | + fn try_from(op: Operation) -> std::result::Result<Self, Self::Error> { |
| 16 | + match op { |
| 17 | + Operation::ActivateCredential { |
| 18 | + attested_key_name, |
| 19 | + attesting_key_name, |
| 20 | + credential_blob, |
| 21 | + secret, |
| 22 | + } => Ok(OperationProto { |
| 23 | + attested_key_name, |
| 24 | + attesting_key_name: attesting_key_name.unwrap_or_default(), |
| 25 | + parameters: Some(AttestationMechanismParams { |
| 26 | + mechanism: Some(attestation_mechanism_params::Mechanism::ActivateCredential( |
| 27 | + attestation_mechanism_params::ActivateCredential { |
| 28 | + credential_blob: credential_blob.to_vec(), |
| 29 | + secret: secret.to_vec(), |
| 30 | + }, |
| 31 | + )), |
| 32 | + }), |
| 33 | + }), |
| 34 | + } |
| 35 | + } |
| 36 | +} |
| 37 | + |
| 38 | +impl TryFrom<OperationProto> for Operation { |
| 39 | + type Error = ResponseStatus; |
| 40 | + |
| 41 | + fn try_from(proto_op: OperationProto) -> std::result::Result<Self, Self::Error> { |
| 42 | + match proto_op.parameters { |
| 43 | + Some(AttestationMechanismParams { |
| 44 | + mechanism: |
| 45 | + Some(attestation_mechanism_params::Mechanism::ActivateCredential( |
| 46 | + attestation_mechanism_params::ActivateCredential { |
| 47 | + credential_blob, |
| 48 | + secret, |
| 49 | + }, |
| 50 | + )), |
| 51 | + }) => Ok(Operation::ActivateCredential { |
| 52 | + attested_key_name: proto_op.attested_key_name, |
| 53 | + attesting_key_name: if proto_op.attesting_key_name.is_empty() { |
| 54 | + None |
| 55 | + } else { |
| 56 | + Some(proto_op.attesting_key_name) |
| 57 | + }, |
| 58 | + credential_blob: credential_blob.into(), |
| 59 | + secret: secret.into(), |
| 60 | + }), |
| 61 | + _ => { |
| 62 | + error!("The encoding of the operation does not follow the expected pattern"); |
| 63 | + Err(ResponseStatus::InvalidEncoding) |
| 64 | + } |
| 65 | + } |
| 66 | + } |
| 67 | +} |
| 68 | + |
| 69 | +impl TryFrom<Result> for ResultProto { |
| 70 | + type Error = ResponseStatus; |
| 71 | + |
| 72 | + fn try_from(op: Result) -> std::result::Result<Self, Self::Error> { |
| 73 | + match op { |
| 74 | + Result::ActivateCredential { credential } => Ok(ResultProto { |
| 75 | + output: Some(AttestationOutput { |
| 76 | + mechanism: Some(attestation_output::Mechanism::ActivateCredential( |
| 77 | + attestation_output::ActivateCredential { |
| 78 | + credential: credential.to_vec(), |
| 79 | + }, |
| 80 | + )), |
| 81 | + }), |
| 82 | + }), |
| 83 | + } |
| 84 | + } |
| 85 | +} |
| 86 | + |
| 87 | +impl TryFrom<ResultProto> for Result { |
| 88 | + type Error = ResponseStatus; |
| 89 | + |
| 90 | + fn try_from(proto_op: ResultProto) -> std::result::Result<Self, Self::Error> { |
| 91 | + match proto_op { |
| 92 | + ResultProto { |
| 93 | + output: |
| 94 | + Some(AttestationOutput { |
| 95 | + mechanism: |
| 96 | + Some(attestation_output::Mechanism::ActivateCredential( |
| 97 | + attestation_output::ActivateCredential { credential }, |
| 98 | + )), |
| 99 | + }), |
| 100 | + } => Ok(Result::ActivateCredential { |
| 101 | + credential: credential.into(), |
| 102 | + }), |
| 103 | + _ => { |
| 104 | + error!("The encoding of the result does not follow the expected pattern"); |
| 105 | + Err(ResponseStatus::InvalidEncoding) |
| 106 | + } |
| 107 | + } |
| 108 | + } |
| 109 | +} |
| 110 | + |
| 111 | +#[cfg(test)] |
| 112 | +mod test { |
| 113 | + use super::super::generated_ops::attest_key::{ |
| 114 | + attestation_mechanism_params, attestation_output, AttestationMechanismParams, |
| 115 | + AttestationOutput, Operation as OperationProto, Result as ResultProto, |
| 116 | + }; |
| 117 | + use super::super::{Convert, ProtobufConverter}; |
| 118 | + use crate::operations::{ |
| 119 | + attest_key::Operation, attest_key::Result, NativeOperation, NativeResult, |
| 120 | + }; |
| 121 | + use crate::requests::Opcode; |
| 122 | + use std::convert::TryInto; |
| 123 | + |
| 124 | + static CONVERTER: ProtobufConverter = ProtobufConverter {}; |
| 125 | + |
| 126 | + #[test] |
| 127 | + fn attest_key_op_from_proto() { |
| 128 | + let op_attested_key_name = String::from("attested key name"); |
| 129 | + let op_attesting_key_name = String::from("attesting key name"); |
| 130 | + let op_credential_blob = vec![0xaa; 32]; |
| 131 | + let op_secret = vec![0x11; 16]; |
| 132 | + |
| 133 | + let proto = OperationProto { |
| 134 | + attested_key_name: op_attested_key_name.clone(), |
| 135 | + attesting_key_name: op_attesting_key_name.clone(), |
| 136 | + parameters: Some(AttestationMechanismParams { |
| 137 | + mechanism: Some(attestation_mechanism_params::Mechanism::ActivateCredential( |
| 138 | + attestation_mechanism_params::ActivateCredential { |
| 139 | + credential_blob: op_credential_blob.clone(), |
| 140 | + secret: op_secret.clone(), |
| 141 | + }, |
| 142 | + )), |
| 143 | + }), |
| 144 | + }; |
| 145 | + |
| 146 | + let op: Operation = proto.try_into().expect("Failed conversion"); |
| 147 | + let Operation::ActivateCredential { |
| 148 | + attested_key_name, |
| 149 | + attesting_key_name, |
| 150 | + credential_blob, |
| 151 | + secret, |
| 152 | + } = op; |
| 153 | + assert_eq!(attested_key_name, op_attested_key_name); |
| 154 | + assert_eq!( |
| 155 | + attesting_key_name.unwrap_or_default(), |
| 156 | + op_attesting_key_name |
| 157 | + ); |
| 158 | + assert_eq!(credential_blob.to_vec(), op_credential_blob); |
| 159 | + assert_eq!(secret.to_vec(), op_secret); |
| 160 | + } |
| 161 | + |
| 162 | + #[test] |
| 163 | + fn attest_key_proto_no_attesting() { |
| 164 | + let op_attested_key_name = String::from("attested key name"); |
| 165 | + let op_credential_blob = vec![0xaa; 32]; |
| 166 | + let op_secret = vec![0x11; 16]; |
| 167 | + |
| 168 | + let proto = OperationProto { |
| 169 | + attested_key_name: op_attested_key_name, |
| 170 | + attesting_key_name: String::new(), |
| 171 | + parameters: Some(AttestationMechanismParams { |
| 172 | + mechanism: Some(attestation_mechanism_params::Mechanism::ActivateCredential( |
| 173 | + attestation_mechanism_params::ActivateCredential { |
| 174 | + credential_blob: op_credential_blob, |
| 175 | + secret: op_secret, |
| 176 | + }, |
| 177 | + )), |
| 178 | + }), |
| 179 | + }; |
| 180 | + |
| 181 | + let op: Operation = proto.try_into().expect("Failed conversion"); |
| 182 | + let Operation::ActivateCredential { |
| 183 | + attesting_key_name, .. |
| 184 | + } = op; |
| 185 | + assert!(attesting_key_name.is_none()); |
| 186 | + } |
| 187 | + |
| 188 | + #[test] |
| 189 | + fn attest_key_proto_from_op() { |
| 190 | + let op_attested_key_name = String::from("attested key name"); |
| 191 | + let op_attesting_key_name = String::from("attesting key name"); |
| 192 | + let op_credential_blob = vec![0xaa; 32]; |
| 193 | + let op_secret = vec![0x11; 16]; |
| 194 | + |
| 195 | + let op = Operation::ActivateCredential { |
| 196 | + attested_key_name: op_attested_key_name.clone(), |
| 197 | + attesting_key_name: Some(op_attesting_key_name.clone()), |
| 198 | + credential_blob: op_credential_blob.clone().into(), |
| 199 | + secret: op_secret.clone().into(), |
| 200 | + }; |
| 201 | + |
| 202 | + let proto: OperationProto = op.try_into().expect("Failed conversion"); |
| 203 | + if let OperationProto { |
| 204 | + attested_key_name, |
| 205 | + attesting_key_name, |
| 206 | + parameters: |
| 207 | + Some(AttestationMechanismParams { |
| 208 | + mechanism: |
| 209 | + Some(attestation_mechanism_params::Mechanism::ActivateCredential( |
| 210 | + attestation_mechanism_params::ActivateCredential { |
| 211 | + credential_blob, |
| 212 | + secret, |
| 213 | + }, |
| 214 | + )), |
| 215 | + }), |
| 216 | + } = proto |
| 217 | + { |
| 218 | + assert_eq!(attested_key_name, op_attested_key_name); |
| 219 | + assert_eq!(attesting_key_name, op_attesting_key_name); |
| 220 | + assert_eq!(credential_blob, op_credential_blob); |
| 221 | + assert_eq!(secret, op_secret); |
| 222 | + } |
| 223 | + } |
| 224 | + |
| 225 | + #[test] |
| 226 | + fn attest_key_op_no_attesting() { |
| 227 | + let op_attested_key_name = String::from("attested key name"); |
| 228 | + let op_credential_blob = vec![0xaa; 32]; |
| 229 | + let op_secret = vec![0x11; 16]; |
| 230 | + |
| 231 | + let op = Operation::ActivateCredential { |
| 232 | + attested_key_name: op_attested_key_name, |
| 233 | + attesting_key_name: None, |
| 234 | + credential_blob: op_credential_blob.into(), |
| 235 | + secret: op_secret.into(), |
| 236 | + }; |
| 237 | + |
| 238 | + let proto: OperationProto = op.try_into().expect("Failed conversion"); |
| 239 | + assert_eq!(proto.attesting_key_name, String::new()); |
| 240 | + } |
| 241 | + |
| 242 | + #[test] |
| 243 | + fn attest_key_op_e2e() { |
| 244 | + let op_attested_key_name = String::from("attested key name"); |
| 245 | + let op_attesting_key_name = String::from("attesting key name"); |
| 246 | + let op_credential_blob = vec![0xaa; 32]; |
| 247 | + let op_secret = vec![0x11; 16]; |
| 248 | + |
| 249 | + let op = Operation::ActivateCredential { |
| 250 | + attested_key_name: op_attested_key_name, |
| 251 | + attesting_key_name: Some(op_attesting_key_name), |
| 252 | + credential_blob: op_credential_blob.into(), |
| 253 | + secret: op_secret.into(), |
| 254 | + }; |
| 255 | + |
| 256 | + let body = CONVERTER |
| 257 | + .operation_to_body(NativeOperation::AttestKey(op)) |
| 258 | + .expect("Failed to convert to body"); |
| 259 | + |
| 260 | + let _ = CONVERTER |
| 261 | + .body_to_operation(body, Opcode::AttestKey) |
| 262 | + .expect("Failed to convert to operation"); |
| 263 | + } |
| 264 | + |
| 265 | + #[test] |
| 266 | + fn attest_key_resp_from_proto() { |
| 267 | + let resp_credential = vec![0xbb; 32]; |
| 268 | + |
| 269 | + let proto = ResultProto { |
| 270 | + output: Some(AttestationOutput { |
| 271 | + mechanism: Some(attestation_output::Mechanism::ActivateCredential( |
| 272 | + attestation_output::ActivateCredential { |
| 273 | + credential: resp_credential.clone(), |
| 274 | + }, |
| 275 | + )), |
| 276 | + }), |
| 277 | + }; |
| 278 | + |
| 279 | + let resp: Result = proto.try_into().expect("Failed conversion"); |
| 280 | + |
| 281 | + let Result::ActivateCredential { credential } = resp; |
| 282 | + |
| 283 | + assert_eq!(credential.to_vec(), resp_credential); |
| 284 | + } |
| 285 | + |
| 286 | + #[test] |
| 287 | + fn attest_key_resp_to_proto() { |
| 288 | + let resp_credential = vec![0xbb; 32]; |
| 289 | + |
| 290 | + let resp = Result::ActivateCredential { |
| 291 | + credential: resp_credential.clone().into(), |
| 292 | + }; |
| 293 | + |
| 294 | + let proto: ResultProto = resp.try_into().expect("Failed conversion"); |
| 295 | + |
| 296 | + if let ResultProto { |
| 297 | + output: |
| 298 | + Some(AttestationOutput { |
| 299 | + mechanism: |
| 300 | + Some(attestation_output::Mechanism::ActivateCredential( |
| 301 | + attestation_output::ActivateCredential { credential }, |
| 302 | + )), |
| 303 | + }), |
| 304 | + } = proto |
| 305 | + { |
| 306 | + assert_eq!(credential.to_vec(), resp_credential); |
| 307 | + } |
| 308 | + } |
| 309 | + |
| 310 | + #[test] |
| 311 | + fn attest_key_resp_e2e() { |
| 312 | + let resp_credential = vec![0xbb; 32]; |
| 313 | + |
| 314 | + let resp = Result::ActivateCredential { |
| 315 | + credential: resp_credential.into(), |
| 316 | + }; |
| 317 | + |
| 318 | + let body = CONVERTER |
| 319 | + .result_to_body(NativeResult::AttestKey(resp)) |
| 320 | + .expect("Failed to convert to body"); |
| 321 | + |
| 322 | + let _ = CONVERTER |
| 323 | + .body_to_result(body, Opcode::AttestKey) |
| 324 | + .expect("Failed to convert to operation"); |
| 325 | + } |
| 326 | +} |
0 commit comments