penumbra_custody/
soft_kms.rs

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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
//! A basic software key management system that stores keys in memory but
//! presents as an asynchronous signer.

use decaf377_rdsa::{Signature, SpendAuth};
use penumbra_proto::{
    core::component::{
        governance::v1::ValidatorVoteBody as ProtoValidatorVoteBody,
        stake::v1::Validator as ProtoValidator,
    },
    custody::v1::{self as pb, AuthorizeResponse},
    Message as _,
};
use penumbra_transaction::AuthorizationData;
use rand_core::OsRng;
use tonic::{async_trait, Request, Response, Status};

use crate::{
    policy::Policy, AuthorizeRequest, AuthorizeValidatorDefinitionRequest,
    AuthorizeValidatorVoteRequest,
};

mod config;

pub use config::Config;

/// A basic software key management system that stores keys in memory but
/// presents as an asynchronous signer.
pub struct SoftKms {
    config: Config,
}

impl SoftKms {
    /// Initialize with the given [`Config`].
    pub fn new(config: Config) -> Self {
        Self { config }
    }

    /// Attempt to authorize the requested [`TransactionPlan`](penumbra_transaction::TransactionPlan).
    #[tracing::instrument(skip(self, request), name = "softhsm_sign")]
    pub fn sign(&self, request: &AuthorizeRequest) -> anyhow::Result<AuthorizationData> {
        tracing::debug!(?request.plan);

        for policy in &self.config.auth_policy {
            policy.check_transaction(request)?;
        }

        Ok(request.plan.authorize(OsRng, &self.config.spend_key)?)
    }

    /// Attempt to authorize the requested validator definition.
    #[tracing::instrument(skip(self, request), name = "softhsm_sign_validator_definition")]
    pub fn sign_validator_definition(
        &self,
        request: &AuthorizeValidatorDefinitionRequest,
    ) -> anyhow::Result<Signature<SpendAuth>> {
        tracing::debug!(?request.validator_definition);

        for policy in &self.config.auth_policy {
            policy.check_validator_definition(request)?;
        }

        let protobuf_serialized: ProtoValidator = request.validator_definition.clone().into();
        let validator_definition_bytes = protobuf_serialized.encode_to_vec();

        Ok(self
            .config
            .spend_key
            .spend_auth_key()
            .sign(OsRng, &validator_definition_bytes))
    }

    /// Attempt to authorize the requested validator vote.
    #[tracing::instrument(skip(self, request), name = "softhsm_sign_validator_vote")]
    pub fn sign_validator_vote(
        &self,
        request: &AuthorizeValidatorVoteRequest,
    ) -> anyhow::Result<Signature<SpendAuth>> {
        tracing::debug!(?request.validator_vote);

        for policy in &self.config.auth_policy {
            policy.check_validator_vote(request)?;
        }

        let protobuf_serialized: ProtoValidatorVoteBody = request.validator_vote.clone().into();
        let validator_vote_bytes = protobuf_serialized.encode_to_vec();

        Ok(self
            .config
            .spend_key
            .spend_auth_key()
            .sign(OsRng, &validator_vote_bytes))
    }
}

#[async_trait]
impl pb::custody_service_server::CustodyService for SoftKms {
    async fn authorize(
        &self,
        request: Request<pb::AuthorizeRequest>,
    ) -> Result<Response<AuthorizeResponse>, Status> {
        let request = request
            .into_inner()
            .try_into()
            .map_err(|e: anyhow::Error| Status::invalid_argument(e.to_string()))?;

        let authorization_data = self
            .sign(&request)
            .map_err(|e| Status::unauthenticated(format!("{e:#}")))?;

        let authorization_response = AuthorizeResponse {
            data: Some(authorization_data.into()),
        };

        Ok(Response::new(authorization_response))
    }

    async fn authorize_validator_definition(
        &self,
        request: Request<pb::AuthorizeValidatorDefinitionRequest>,
    ) -> Result<Response<pb::AuthorizeValidatorDefinitionResponse>, Status> {
        let request = request
            .into_inner()
            .try_into()
            .map_err(|e: anyhow::Error| Status::invalid_argument(e.to_string()))?;

        let validator_definition_auth = self
            .sign_validator_definition(&request)
            .map_err(|e| Status::unauthenticated(format!("{e:#}")))?;

        let authorization_response = pb::AuthorizeValidatorDefinitionResponse {
            validator_definition_auth: Some(validator_definition_auth.into()),
        };

        Ok(Response::new(authorization_response))
    }

    async fn authorize_validator_vote(
        &self,
        request: Request<pb::AuthorizeValidatorVoteRequest>,
    ) -> Result<Response<pb::AuthorizeValidatorVoteResponse>, Status> {
        let request = request
            .into_inner()
            .try_into()
            .map_err(|e: anyhow::Error| Status::invalid_argument(e.to_string()))?;

        let validator_vote_auth = self
            .sign_validator_vote(&request)
            .map_err(|e| Status::unauthenticated(format!("{e:#}")))?;

        let authorization_response = pb::AuthorizeValidatorVoteResponse {
            validator_vote_auth: Some(validator_vote_auth.into()),
        };

        Ok(Response::new(authorization_response))
    }

    async fn export_full_viewing_key(
        &self,
        _request: Request<pb::ExportFullViewingKeyRequest>,
    ) -> Result<Response<pb::ExportFullViewingKeyResponse>, Status> {
        Ok(Response::new(pb::ExportFullViewingKeyResponse {
            full_viewing_key: Some(self.config.spend_key.full_viewing_key().clone().into()),
        }))
    }

    async fn confirm_address(
        &self,
        request: Request<pb::ConfirmAddressRequest>,
    ) -> Result<Response<pb::ConfirmAddressResponse>, Status> {
        let address_index = request
            .into_inner()
            .address_index
            .ok_or_else(|| {
                Status::invalid_argument("missing address index in confirm address request")
            })?
            .try_into()
            .map_err(|e| {
                Status::invalid_argument(format!(
                    "invalid address index in confirm address request: {e:#}"
                ))
            })?;

        let (address, _dtk) = self
            .config
            .spend_key
            .full_viewing_key()
            .payment_address(address_index);

        Ok(Response::new(pb::ConfirmAddressResponse {
            address: Some(address.into()),
        }))
    }
}