tink_proto/
lib.rs

1// Copyright 2020 The Tink-Rust Authors
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//      http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14//
15////////////////////////////////////////////////////////////////////////////////
16
17//! Protocol buffer message definitions for Tink.
18//!
19//! Almost all of the code in this crate is auto-generated (using [prost](https://docs.rs/prost)) from the protocol
20//! buffer message definitions in the `proto/` subdirectory.  These `.proto` files are copies from
21//! the upstream [Tink project](https://github.com/google/tink/tree/master/proto).
22
23#![cfg_attr(docsrs, feature(doc_cfg))]
24#![deny(broken_intra_doc_links)]
25#![allow(clippy::derive_partial_eq_without_eq)]
26#![allow(clippy::too_long_first_doc_paragraph)]
27
28/// Re-export to ensure that users of this crate can access the same version.
29pub use prost;
30
31#[cfg(not(feature = "json"))]
32include!("codegen/google.crypto.tink.rs");
33#[cfg(feature = "json")]
34include!("codegen/serde/google.crypto.tink.rs");
35
36// Manual keyset serialization implementations that map enums onto strings rather than
37// the `i32` values used by [prost](https://docs.rs/prost).
38#[cfg(feature = "json")]
39#[cfg_attr(docsrs, doc(cfg(feature = "json")))]
40pub mod json {
41    pub mod key_status_type {
42        //! Manual JSON serialization for [`KeyStatusType`](crate::KeyStatusType) enums.
43        use serde::Deserialize;
44        use std::convert::TryFrom;
45        pub fn serialize<S: serde::Serializer>(
46            val: &i32,
47            serializer: S,
48        ) -> Result<S::Ok, S::Error> {
49            serializer.serialize_str(match crate::KeyStatusType::try_from(*val) {
50                Ok(crate::KeyStatusType::Enabled) => "ENABLED",
51                Ok(crate::KeyStatusType::Disabled) => "DISABLED",
52                Ok(crate::KeyStatusType::Destroyed) => "DESTROYED",
53                _ => "UNKNOWN",
54            })
55        }
56        pub fn deserialize<'de, D: serde::Deserializer<'de>>(
57            deserializer: D,
58        ) -> Result<i32, D::Error> {
59            let s = String::deserialize(deserializer)?;
60            Ok(match s.as_ref() {
61                "ENABLED" => crate::KeyStatusType::Enabled as i32,
62                "DISABLED" => crate::KeyStatusType::Disabled as i32,
63                "DESTROYED" => crate::KeyStatusType::Destroyed as i32,
64                _ => crate::KeyStatusType::UnknownStatus as i32,
65            })
66        }
67    }
68    pub mod output_prefix_type {
69        //! Manual JSON serialization for [`OutputPrefixType`](crate::OutputPrefixType) enums.
70        use serde::Deserialize;
71        use std::convert::TryFrom;
72        pub fn serialize<S: serde::Serializer>(
73            val: &i32,
74            serializer: S,
75        ) -> Result<S::Ok, S::Error> {
76            serializer.serialize_str(match crate::OutputPrefixType::try_from(*val) {
77                Ok(crate::OutputPrefixType::Tink) => "TINK",
78                Ok(crate::OutputPrefixType::Legacy) => "LEGACY",
79                Ok(crate::OutputPrefixType::Raw) => "RAW",
80                Ok(crate::OutputPrefixType::Crunchy) => "CRUNCHY",
81                _ => "UNKNOWN",
82            })
83        }
84        pub fn deserialize<'de, D: serde::Deserializer<'de>>(
85            deserializer: D,
86        ) -> Result<i32, D::Error> {
87            let s = String::deserialize(deserializer)?;
88            Ok(match s.as_ref() {
89                "TINK" => crate::OutputPrefixType::Tink as i32,
90                "LEGACY" => crate::OutputPrefixType::Legacy as i32,
91                "RAW" => crate::OutputPrefixType::Raw as i32,
92                "CRUNCHY" => crate::OutputPrefixType::Crunchy as i32,
93                _ => crate::OutputPrefixType::UnknownPrefix as i32,
94            })
95        }
96    }
97    pub mod key_material_type {
98        //! Manual JSON serialization for [`KeyMaterialType`](crate::key_data::KeyMaterialType)
99        //! enums.
100        use serde::Deserialize;
101        use std::convert::TryFrom;
102        pub fn serialize<S: serde::Serializer>(
103            val: &i32,
104            serializer: S,
105        ) -> Result<S::Ok, S::Error> {
106            serializer.serialize_str(match crate::key_data::KeyMaterialType::try_from(*val) {
107                Ok(crate::key_data::KeyMaterialType::Symmetric) => "SYMMETRIC",
108                Ok(crate::key_data::KeyMaterialType::AsymmetricPrivate) => "ASYMMETRIC_PRIVATE",
109                Ok(crate::key_data::KeyMaterialType::AsymmetricPublic) => "ASYMMETRIC_PUBLIC",
110                Ok(crate::key_data::KeyMaterialType::Remote) => "REMOTE",
111                _ => "UNKNOWN",
112            })
113        }
114        pub fn deserialize<'de, D: serde::Deserializer<'de>>(
115            deserializer: D,
116        ) -> Result<i32, D::Error> {
117            let s = String::deserialize(deserializer)?;
118            Ok(match s.as_ref() {
119                "SYMMETRIC" => crate::key_data::KeyMaterialType::Symmetric as i32,
120                "ASYMMETRIC_PRIVATE" => crate::key_data::KeyMaterialType::AsymmetricPrivate as i32,
121                "ASYMMETRIC_PUBLIC" => crate::key_data::KeyMaterialType::AsymmetricPublic as i32,
122                "REMOTE" => crate::key_data::KeyMaterialType::Remote as i32,
123                _ => crate::key_data::KeyMaterialType::UnknownKeymaterial as i32,
124            })
125        }
126    }
127    pub mod b64 {
128        //! Manual serialization implementations for base64-encoded binary data.
129        use base64::Engine;
130        use serde::Deserialize;
131        pub fn serialize<S: serde::Serializer>(
132            val: &[u8],
133            serializer: S,
134        ) -> Result<S::Ok, S::Error> {
135            serializer.serialize_str(&base64::engine::general_purpose::STANDARD.encode(val))
136        }
137        pub fn deserialize<'de, D: serde::Deserializer<'de>>(
138            deserializer: D,
139        ) -> Result<Vec<u8>, D::Error> {
140            let s = String::deserialize(deserializer)?;
141            base64::engine::general_purpose::STANDARD
142                .decode(&s)
143                .map_err(|_e| {
144                    serde::de::Error::invalid_value(
145                        serde::de::Unexpected::Str(&s),
146                        &"base64 data expected",
147                    )
148                })
149        }
150    }
151}