1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
use crate::proto;
use prost::Message;
use tink_core::{utils::wrap_err, TinkError};
#[derive(Debug, Default)]
pub struct KeysetServerImpl;
#[tonic::async_trait]
impl proto::keyset_server::Keyset for KeysetServerImpl {
async fn generate(
&self,
request: tonic::Request<proto::KeysetGenerateRequest>,
) -> Result<tonic::Response<proto::KeysetGenerateResponse>, tonic::Status> {
let req = request.into_inner();
let closure = move || -> Result<_, TinkError> {
let template = tink_proto::KeyTemplate::decode(req.template.as_ref())
.map_err(|e| wrap_err("decode failed", e))?;
let handle = tink_core::keyset::Handle::new(&template)?;
let mut buf = Vec::new();
{
let mut writer = tink_core::keyset::BinaryWriter::new(&mut buf);
tink_core::keyset::insecure::write(&handle, &mut writer)
.map_err(|e| wrap_err("write failed", e))?;
}
Ok(buf)
};
Ok(tonic::Response::new(proto::KeysetGenerateResponse {
result: Some(match closure() {
Ok(buf) => proto::keyset_generate_response::Result::Keyset(buf),
Err(e) => proto::keyset_generate_response::Result::Err(format!("{:?}", e)),
}),
}))
}
async fn public(
&self,
request: tonic::Request<proto::KeysetPublicRequest>,
) -> Result<tonic::Response<proto::KeysetPublicResponse>, tonic::Status> {
let req = request.into_inner();
let closure = move || -> Result<_, TinkError> {
let cursor = std::io::Cursor::new(req.private_keyset);
let mut reader = tink_core::keyset::BinaryReader::new(cursor);
let private_handle = tink_core::keyset::insecure::read(&mut reader)
.map_err(|e| wrap_err("read failed", e))?;
let public_handle = private_handle.public()?;
let mut buf = Vec::new();
{
let mut writer = tink_core::keyset::BinaryWriter::new(&mut buf);
tink_core::keyset::insecure::write(&public_handle, &mut writer)
.map_err(|e| wrap_err("write failed", e))?;
}
Ok(buf)
};
Ok(tonic::Response::new(proto::KeysetPublicResponse {
result: Some(match closure() {
Ok(buf) => proto::keyset_public_response::Result::PublicKeyset(buf),
Err(e) => proto::keyset_public_response::Result::Err(format!("{:?}", e)),
}),
}))
}
async fn to_json(
&self,
request: tonic::Request<proto::KeysetToJsonRequest>,
) -> Result<tonic::Response<proto::KeysetToJsonResponse>, tonic::Status> {
let req = request.into_inner();
let closure = move || -> Result<_, TinkError> {
let cursor = std::io::Cursor::new(req.keyset);
let mut reader = tink_core::keyset::BinaryReader::new(cursor);
let handle = tink_core::keyset::insecure::read(&mut reader)
.map_err(|e| wrap_err("read failed", e))?;
let mut buf = Vec::new();
{
let mut writer = tink_core::keyset::JsonWriter::new(&mut buf);
tink_core::keyset::insecure::write(&handle, &mut writer)
.map_err(|e| wrap_err("write failed", e))?;
}
let json = std::str::from_utf8(&buf).map_err(|e| wrap_err("utf8 failed", e))?;
Ok(json.to_string())
};
Ok(tonic::Response::new(proto::KeysetToJsonResponse {
result: Some(match closure() {
Ok(json) => proto::keyset_to_json_response::Result::JsonKeyset(json),
Err(e) => proto::keyset_to_json_response::Result::Err(format!("{:?}", e)),
}),
}))
}
async fn from_json(
&self,
request: tonic::Request<proto::KeysetFromJsonRequest>,
) -> Result<tonic::Response<proto::KeysetFromJsonResponse>, tonic::Status> {
let req = request.into_inner();
let closure = move || -> Result<_, TinkError> {
let cursor = std::io::Cursor::new(req.json_keyset.as_bytes());
let mut reader = tink_core::keyset::JsonReader::new(cursor);
let handle = tink_core::keyset::insecure::read(&mut reader)
.map_err(|e| wrap_err("read failed", e))?;
let mut buf = Vec::new();
{
let mut writer = tink_core::keyset::BinaryWriter::new(&mut buf);
tink_core::keyset::insecure::write(&handle, &mut writer)
.map_err(|e| wrap_err("write failed", e))?;
}
Ok(buf)
};
Ok(tonic::Response::new(proto::KeysetFromJsonResponse {
result: Some(match closure() {
Ok(buf) => proto::keyset_from_json_response::Result::Keyset(buf),
Err(e) => proto::keyset_from_json_response::Result::Err(format!("{:?}", e)),
}),
}))
}
}