Skip to main content

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