1 /*
2  * Copyright (C) 2019 The Android Open Source Project
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 "adb/crypto/rsa_2048_key.h"
18 
19 #include <android-base/logging.h>
20 #include <crypto_utils/android_pubkey.h>
21 #include <openssl/bn.h>
22 #include <openssl/rsa.h>
23 
24 namespace adb {
25 namespace crypto {
26 
27 namespace {
get_user_info()28 std::string get_user_info() {
29     std::string hostname;
30     if (getenv("HOSTNAME")) hostname = getenv("HOSTNAME");
31 #if !defined(_WIN32)
32     char buf[64];
33     if (hostname.empty() && gethostname(buf, sizeof(buf)) != -1) hostname = buf;
34 #endif
35     if (hostname.empty()) hostname = "unknown";
36 
37     std::string username;
38     if (getenv("LOGNAME")) username = getenv("LOGNAME");
39 #if !defined(_WIN32)
40     if (username.empty() && getlogin()) username = getlogin();
41 #endif
42     if (username.empty()) hostname = "unknown";
43 
44     return " " + username + "@" + hostname;
45 }
46 
47 }  // namespace
48 
CalculatePublicKey(std::string * out,RSA * private_key)49 bool CalculatePublicKey(std::string* out, RSA* private_key) {
50     uint8_t binary_key_data[ANDROID_PUBKEY_ENCODED_SIZE];
51     if (!android_pubkey_encode(private_key, binary_key_data, sizeof(binary_key_data))) {
52         LOG(ERROR) << "Failed to convert to public key";
53         return false;
54     }
55 
56     size_t expected_length;
57     if (!EVP_EncodedLength(&expected_length, sizeof(binary_key_data))) {
58         LOG(ERROR) << "Public key too large to base64 encode";
59         return false;
60     }
61 
62     out->resize(expected_length);
63     size_t actual_length = EVP_EncodeBlock(reinterpret_cast<uint8_t*>(out->data()), binary_key_data,
64                                            sizeof(binary_key_data));
65     out->resize(actual_length);
66     out->append(get_user_info());
67     return true;
68 }
69 
CreateRSA2048Key()70 std::optional<Key> CreateRSA2048Key() {
71     bssl::UniquePtr<EVP_PKEY> pkey(EVP_PKEY_new());
72     bssl::UniquePtr<BIGNUM> exponent(BN_new());
73     bssl::UniquePtr<RSA> rsa(RSA_new());
74     if (!pkey || !exponent || !rsa) {
75         LOG(ERROR) << "Failed to allocate key";
76         return std::nullopt;
77     }
78 
79     BN_set_word(exponent.get(), RSA_F4);
80     RSA_generate_key_ex(rsa.get(), 2048, exponent.get(), nullptr);
81     EVP_PKEY_set1_RSA(pkey.get(), rsa.get());
82 
83     return std::optional<Key>{Key(std::move(pkey), adb::proto::KeyType::RSA_2048)};
84 }
85 
86 }  // namespace crypto
87 }  // namespace adb
88