parsec_interface/lib.rs
1// Copyright 2019 Contributors to the Parsec project.
2// SPDX-License-Identifier: Apache-2.0
3#![deny(
4 nonstandard_style,
5 dead_code,
6 improper_ctypes,
7 non_shorthand_field_patterns,
8 no_mangle_generic_items,
9 overflowing_literals,
10 path_statements,
11 patterns_in_fns_without_body,
12 private_in_public,
13 unconditional_recursion,
14 unused,
15 unused_allocation,
16 unused_comparisons,
17 unused_parens,
18 while_true,
19 missing_debug_implementations,
20 missing_docs,
21 trivial_casts,
22 trivial_numeric_casts,
23 unused_extern_crates,
24 unused_import_braces,
25 unused_qualifications,
26 unused_results,
27 missing_copy_implementations
28)]
29// This one is hard to avoid.
30#![allow(clippy::multiple_crate_versions)]
31// This crate declares deprecated values for legacy reasons.
32#![allow(deprecated)]
33//! # Parsec Rust Interface
34//!
35//! The Parsec Rust Interface provides methods to communicate easily with the Parsec service using
36//! the [wire protocol](https://github.com/docker/parsec/blob/master/docs/wire_protocol.md) and the
37//! [operation
38//! contracts](https://parallaxsecond.github.io/parsec-book/parsec_client/operations/index.html).
39//!
40//! ## For the Parsec service
41//!
42//! This library is used by the Parsec service to:
43//! * read from a stream a `Request` sent to the service with the `read_from_stream` method
44//! * use the `body_to_operation` method of the `Convert` trait on a converter to parse the request
45//! body into a `NativeOperation`
46//!
47//!```
48//!# use std::io::Read;
49//!#
50//!# pub struct MockRead {
51//!# pub buffer: Vec<u8>,
52//!# }
53//!#
54//!# impl Read for MockRead {
55//!# fn read(&mut self, buf: &mut [u8]) -> std::io::Result<usize> {
56//!# for val in buf.iter_mut() {
57//!# *val = self.buffer.remove(0);
58//!# }
59//!#
60//!# Ok(buf.len())
61//!# }
62//!# }
63//!#
64//!# let mut stream = MockRead {
65//!# buffer: vec![
66//!# 0x10, 0xA7, 0xC0, 0x5E, 0x1e, 0x0, 0x1, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
67//!# 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x1, 0x0, 0x0, 0x0, 0x0, 0x4, 0x0, 0x1, 0x0, 0x0,
68//!# 0x0, 0x0, 0x0, 0x0, 0x0, 0x72, 0x6F, 0x6F, 0x74
69//!# ]
70//!# };
71//!use parsec_interface::operations::{Convert, NativeOperation};
72//!use parsec_interface::requests::Request;
73//!use parsec_interface::operations_protobuf::ProtobufConverter;
74//!
75//!let converter = ProtobufConverter {};
76//!// stream is a Read object
77//!let request = Request::read_from_stream(&mut stream, 2048).unwrap();
78//!let operation: NativeOperation = converter
79//! .body_to_operation(request.body, request.header.opcode)
80//! .unwrap();
81//!```
82//!
83//! The service can now execute the operation to yield a `NativeResult` and:
84//! * use the `result_to_body` method to serialize the `NativeResult`
85//! * create a `Response` containing the result as its body and write it back to the stream with
86//! the `write_to_stream` method.
87//!
88//!```
89//!# use std::io::Write;
90//!#
91//!# pub struct MockWrite {
92//!# pub buffer: Vec<u8>,
93//!# }
94//!#
95//!# impl Write for MockWrite {
96//!# fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> {
97//!# for val in buf.iter() {
98//!# self.buffer.push(*val);
99//!# }
100//!# Ok(buf.len())
101//!# }
102//!#
103//!# fn flush(&mut self) -> std::io::Result<()> {
104//!# Ok(())
105//!# }
106//!# }
107//!# let mut stream = MockWrite { buffer: Vec::new() };
108//!use parsec_interface::operations::{Convert, NativeResult, psa_generate_key::Result};
109//!use parsec_interface::requests::{ProviderId, Opcode, BodyType, Response, ResponseStatus};
110//!use parsec_interface::requests::response::ResponseHeader;
111//!use parsec_interface::operations_protobuf::ProtobufConverter;
112//!
113//!let converter = ProtobufConverter {};
114//!let result = NativeResult::PsaGenerateKey(Result {});
115//!let result_body = converter.result_to_body(result).unwrap();
116//!let response = Response {
117//! header: ResponseHeader {
118//! provider: ProviderId::MbedCrypto,
119//! session: 0,
120//! content_type: BodyType::Protobuf,
121//! opcode: Opcode::PsaGenerateKey,
122//! status: ResponseStatus::Success,
123//! },
124//! body: result_body,
125//!};
126//!// stream is a Write object
127//!response.write_to_stream(&mut stream).unwrap();
128//!```
129//!
130//! ## For the Parsec Rust clients
131//!
132//! This library is used by the Parsec Rust clients to:
133//! * use the `operation_to_body` method to serialize the `NativeOperation` to be sent as body of a
134//! `Request`
135//! * write it to the stream with the `write_to_stream` method.
136//!
137//!```
138//!# use std::io::Write;
139//!#
140//!# pub struct MockWrite {
141//!# pub buffer: Vec<u8>,
142//!# }
143//!#
144//!# impl Write for MockWrite {
145//!# fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> {
146//!# for val in buf.iter() {
147//!# self.buffer.push(*val);
148//!# }
149//!# Ok(buf.len())
150//!# }
151
152//!# fn flush(&mut self) -> std::io::Result<()> {
153//!# Ok(())
154//!# }
155//!# }
156//!#
157//!# let mut stream = MockWrite { buffer: Vec::new() };
158//!use parsec_interface::operations::{Convert, NativeOperation};
159//!use parsec_interface::requests::{Request, ProviderId, BodyType, AuthType, Opcode};
160//!use parsec_interface::requests::request::{RequestHeader, RequestAuth};
161//!use parsec_interface::operations_protobuf::ProtobufConverter;
162//!use parsec_interface::operations::ping::Operation;
163//!
164//!let converter = ProtobufConverter {};
165//!let operation = NativeOperation::Ping(Operation {});
166//!let request = Request {
167//! header: RequestHeader {
168//! provider: ProviderId::Core,
169//! session: 0,
170//! content_type: BodyType::Protobuf,
171//! accept_type: BodyType::Protobuf,
172//! auth_type: AuthType::Direct,
173//! opcode: Opcode::Ping,
174//! },
175//! body: converter.operation_to_body(operation).unwrap(),
176//! auth: RequestAuth::new(Vec::from("root")),
177//!};
178//!// stream is a Write object
179//!request.write_to_stream(&mut stream).unwrap();
180//!```
181//!
182//! After the operation has been executed by the Parsec service:
183//! * read from a stream the `Response` from the service with the `read_from_stream` method
184//! * use the `body_to_result` method to parse the result body into a `NativeResult`
185//!
186//!```
187//!# use std::io::Read;
188//!#
189//!# pub struct MockRead {
190//!# pub buffer: Vec<u8>,
191//!# }
192//!#
193//!# impl Read for MockRead {
194//!# fn read(&mut self, buf: &mut [u8]) -> std::io::Result<usize> {
195//!# for val in buf.iter_mut() {
196//!# *val = self.buffer.remove(0);
197//!# }
198//!#
199//!# Ok(buf.len())
200//!# }
201//!# }
202//!#
203//!# let mut stream = MockRead {
204//!# buffer: vec![
205//!# 0x10, 0xA7, 0xC0, 0x5E, 0x1e, 0x0, 0x1, 0x0, 0x0, 0x0, 0x1, 0x0, 0x0, 0x0, 0x0,
206//!# 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x2, 0x0, 0x0, 0x0,
207//!# 0x0, 0x0, 0x0, 0x0
208//!# ]
209//!# };
210//!use parsec_interface::operations::{Convert, NativeResult};
211//!use parsec_interface::requests::Response;
212//!use parsec_interface::operations_protobuf::ProtobufConverter;
213//!
214//!let converter = ProtobufConverter {};
215//!// stream is a Read object
216//!let response = Response::read_from_stream(&mut stream, 2048).unwrap();
217//!let result: NativeResult = converter
218//! .body_to_result(response.body, response.header.opcode)
219//! .unwrap();
220//!```
221//!
222//! See the [Parsec Test client](https://github.com/parallaxsecond/parsec-client-test) as an example
223//! of a Rust client.
224
225pub mod operations;
226pub mod operations_protobuf;
227pub mod requests;
228
229/// Module providing access to secret-wrapping functionality.
230pub use secrecy;