Skip to content

Commit b67f5e9

Browse files
committed
Add Signing Key Handle to Oak Containers C++ SDK
Bug: 345761932 Change-Id: I3e32af292440b385cbd323a696624fd7f52bd318
1 parent d5c8076 commit b67f5e9

File tree

7 files changed

+178
-4
lines changed

7 files changed

+178
-4
lines changed

cc/containers/sdk/BUILD

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,3 +67,16 @@ cc_library(
6767
"@com_google_absl//absl/strings",
6868
],
6969
)
70+
71+
cc_library(
72+
name = "signing_key_handle",
73+
srcs = ["signing_key_handle.cc"],
74+
hdrs = ["signing_key_handle.h"],
75+
deps = [
76+
":orchestrator_crypto_client",
77+
"//cc/crypto:signing_key",
78+
"//proto/crypto:crypto_cc_proto",
79+
"@com_google_absl//absl/status:statusor",
80+
"@com_google_absl//absl/strings",
81+
],
82+
)

cc/containers/sdk/orchestrator_crypto_client.cc

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
#include "absl/status/status.h"
2323
#include "absl/status/statusor.h"
24+
#include "absl/strings/str_cat.h"
2425
#include "absl/strings/string_view.h"
2526
#include "cc/containers/sdk/common.h"
2627
#include "grpcpp/client_context.h"
@@ -31,11 +32,14 @@
3132
namespace oak::containers::sdk {
3233

3334
namespace {
34-
using ::grpc::ClientContext;
35+
using grpc::ClientContext;
3536
using ::oak::containers::v1::DeriveSessionKeysRequest;
3637
using ::oak::containers::v1::DeriveSessionKeysResponse;
3738
using ::oak::containers::v1::KeyOrigin;
39+
using ::oak::containers::v1::SignRequest;
40+
using ::oak::containers::v1::SignResponse;
3841
using ::oak::crypto::v1::SessionKeys;
42+
using ::oak::crypto::v1::Signature;
3943
} // namespace
4044

4145
absl::StatusOr<SessionKeys> OrchestratorCryptoClient::DeriveSessionKeys(
@@ -49,13 +53,33 @@ absl::StatusOr<SessionKeys> OrchestratorCryptoClient::DeriveSessionKeys(
4953
serialized_encapsulated_public_key);
5054
DeriveSessionKeysResponse response;
5155

52-
::grpc::Status status =
53-
stub_->DeriveSessionKeys(&context, request, &response);
56+
grpc::Status status = stub_->DeriveSessionKeys(&context, request, &response);
5457
if (!status.ok()) {
55-
return absl::InternalError("couldn't derive session keys");
58+
return absl::InternalError(
59+
absl::StrCat("couldn't derive session keys: code=", status.error_code(),
60+
", message=", status.error_message()));
5661
}
5762

5863
return response.session_keys();
5964
}
6065

66+
absl::StatusOr<Signature> OrchestratorCryptoClient::Sign(
67+
KeyOrigin key_origin, absl::string_view message) const {
68+
ClientContext context;
69+
context.set_authority(kContextAuthority);
70+
SignRequest request;
71+
request.set_key_origin(key_origin);
72+
request.set_message(message);
73+
SignResponse response;
74+
75+
grpc::Status status = stub_->Sign(&context, request, &response);
76+
if (!status.ok()) {
77+
return absl::InternalError(
78+
absl::StrCat("couldn't sign message: code=", status.error_code(),
79+
", message=", status.error_message()));
80+
}
81+
82+
return response.signature();
83+
}
84+
6185
} // namespace oak::containers::sdk

cc/containers/sdk/orchestrator_crypto_client.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,10 @@ class OrchestratorCryptoClient {
4040
::oak::containers::v1::KeyOrigin key_origin,
4141
absl::string_view serialized_encapsulated_public_key) const;
4242

43+
absl::StatusOr<::oak::crypto::v1::Signature> Sign(
44+
::oak::containers::v1::KeyOrigin key_origin,
45+
absl::string_view message) const;
46+
4347
private:
4448
explicit OrchestratorCryptoClient(
4549
const std::shared_ptr<grpc::Channel>& channel)
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*
2+
* Copyright 2024 The Project Oak Authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
#include "cc/containers/sdk/signing_key_handle.h"
18+
19+
#include "absl/status/statusor.h"
20+
#include "absl/strings/string_view.h"
21+
22+
namespace oak::containers::sdk {
23+
24+
namespace {
25+
using ::oak::containers::v1::KeyOrigin;
26+
using ::oak::crypto::v1::Signature;
27+
} // namespace
28+
29+
absl::StatusOr<Signature> InstanceSigningKeyHandle::Sign(
30+
absl::string_view message) {
31+
return orchestrator_crypto_client_.Sign(KeyOrigin::INSTANCE, message);
32+
}
33+
34+
absl::StatusOr<Signature> GroupSigningKeyHandle::Sign(
35+
absl::string_view message) {
36+
return orchestrator_crypto_client_.Sign(KeyOrigin::GROUP, message);
37+
}
38+
39+
} // namespace oak::containers::sdk
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
* Copyright 2024 The Project Oak Authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
#ifndef CC_CONTAINERS_SDK_SIGNING_KEY_HANDLE_H_
18+
#define CC_CONTAINERS_SDK_SIGNING_KEY_HANDLE_H_
19+
20+
#include "absl/status/statusor.h"
21+
#include "absl/strings/string_view.h"
22+
#include "cc/containers/sdk/orchestrator_crypto_client.h"
23+
#include "cc/crypto/signing_key.h"
24+
25+
namespace oak::containers::sdk {
26+
27+
class InstanceSigningKeyHandle : public ::oak::crypto::SigningKeyHandle {
28+
public:
29+
absl::StatusOr<::oak::crypto::v1::Signature> Sign(
30+
absl::string_view message) override;
31+
32+
private:
33+
OrchestratorCryptoClient orchestrator_crypto_client_;
34+
};
35+
36+
class GroupSigningKeyHandle : public ::oak::crypto::SigningKeyHandle {
37+
public:
38+
absl::StatusOr<::oak::crypto::v1::Signature> Sign(
39+
absl::string_view message) override;
40+
41+
private:
42+
OrchestratorCryptoClient orchestrator_crypto_client_;
43+
};
44+
45+
} // namespace oak::containers::sdk
46+
47+
#endif // THIRD_PARTY_OAK_CC_CONTAINERS_SIGNING_KEY_HANDLE_H_

cc/crypto/BUILD

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,17 @@ cc_library(
7070
],
7171
)
7272

73+
cc_library(
74+
name = "signing_key",
75+
hdrs = ["signing_key.h"],
76+
deps = [
77+
":common",
78+
"//proto/crypto:crypto_cc_proto",
79+
"@com_google_absl//absl/status:statusor",
80+
"@com_google_absl//absl/strings",
81+
],
82+
)
83+
7384
cc_test(
7485
name = "encryptor_test",
7586
size = "small",

cc/crypto/signing_key.h

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
* Copyright 2024 The Project Oak Authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
#ifndef CC_CRYPTO_SIGNING_KEY_H_
18+
#define CC_CRYPTO_SIGNING_KEY_H_
19+
20+
#include "absl/status/statusor.h"
21+
#include "absl/strings/string_view.h"
22+
#include "proto/crypto/crypto.pb.h"
23+
24+
namespace oak::crypto {
25+
26+
class SigningKeyHandle {
27+
public:
28+
virtual absl::StatusOr<::oak::crypto::v1::Signature> Sign(
29+
absl::string_view message) = 0;
30+
31+
virtual ~SigningKeyHandle() = default;
32+
};
33+
34+
} // namespace oak::crypto
35+
36+
#endif // CC_CRYPTO_SIGNING_KEY_H_

0 commit comments

Comments
 (0)