Yifan Hong | ffdaf95 | 2021-09-17 18:08:38 -0700 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright (C) 2021 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 | #include "RpcAuthTesting.h" |
| 17 | #include "../Utils.h" // for TEST_AND_RETURN |
| 18 | |
| 19 | namespace android { |
| 20 | |
| 21 | bssl::UniquePtr<EVP_PKEY> makeKeyPairForSelfSignedCert() { |
| 22 | bssl::UniquePtr<EC_KEY> ec_key(EC_KEY_new_by_curve_name(NID_X9_62_prime256v1)); |
| 23 | if (ec_key == nullptr || !EC_KEY_generate_key(ec_key.get())) { |
| 24 | ALOGE("Failed to generate key pair."); |
| 25 | return nullptr; |
| 26 | } |
| 27 | bssl::UniquePtr<EVP_PKEY> pkey(EVP_PKEY_new()); |
| 28 | // Use set1 instead of assign to avoid leaking ec_key when assign fails. set1 increments |
| 29 | // the refcount of the ec_key, so it is okay to release it at the end of this function. |
| 30 | if (pkey == nullptr || !EVP_PKEY_set1_EC_KEY(pkey.get(), ec_key.get())) { |
| 31 | ALOGE("Failed to assign key pair."); |
| 32 | return nullptr; |
| 33 | } |
| 34 | return pkey; |
| 35 | } |
| 36 | |
| 37 | bssl::UniquePtr<X509> makeSelfSignedCert(EVP_PKEY* pkey, const uint32_t validSeconds) { |
| 38 | bssl::UniquePtr<X509> x509(X509_new()); |
| 39 | bssl::UniquePtr<BIGNUM> serial(BN_new()); |
| 40 | bssl::UniquePtr<BIGNUM> serialLimit(BN_new()); |
| 41 | TEST_AND_RETURN(nullptr, BN_lshift(serialLimit.get(), BN_value_one(), 128)); |
| 42 | TEST_AND_RETURN(nullptr, BN_rand_range(serial.get(), serialLimit.get())); |
| 43 | TEST_AND_RETURN(nullptr, BN_to_ASN1_INTEGER(serial.get(), X509_get_serialNumber(x509.get()))); |
| 44 | TEST_AND_RETURN(nullptr, X509_gmtime_adj(X509_getm_notBefore(x509.get()), 0)); |
| 45 | TEST_AND_RETURN(nullptr, X509_gmtime_adj(X509_getm_notAfter(x509.get()), validSeconds)); |
| 46 | |
| 47 | X509_NAME* subject = X509_get_subject_name(x509.get()); |
| 48 | TEST_AND_RETURN(nullptr, |
| 49 | X509_NAME_add_entry_by_txt(subject, "O", MBSTRING_ASC, |
| 50 | reinterpret_cast<const uint8_t*>("Android"), -1, -1, |
| 51 | 0)); |
| 52 | TEST_AND_RETURN(nullptr, |
| 53 | X509_NAME_add_entry_by_txt(subject, "CN", MBSTRING_ASC, |
| 54 | reinterpret_cast<const uint8_t*>("BinderRPC"), -1, |
| 55 | -1, 0)); |
| 56 | TEST_AND_RETURN(nullptr, X509_set_issuer_name(x509.get(), subject)); |
| 57 | |
| 58 | TEST_AND_RETURN(nullptr, X509_set_pubkey(x509.get(), pkey)); |
| 59 | TEST_AND_RETURN(nullptr, X509_sign(x509.get(), pkey, EVP_sha256())); |
| 60 | return x509; |
| 61 | } |
| 62 | |
| 63 | status_t RpcAuthSelfSigned::configure(SSL_CTX* ctx) { |
| 64 | auto pkey = makeKeyPairForSelfSignedCert(); |
| 65 | TEST_AND_RETURN(UNKNOWN_ERROR, pkey != nullptr); |
| 66 | auto cert = makeSelfSignedCert(pkey.get(), mValidSeconds); |
| 67 | TEST_AND_RETURN(UNKNOWN_ERROR, cert != nullptr); |
| 68 | TEST_AND_RETURN(INVALID_OPERATION, SSL_CTX_use_PrivateKey(ctx, pkey.get())); |
| 69 | TEST_AND_RETURN(INVALID_OPERATION, SSL_CTX_use_certificate(ctx, cert.get())); |
| 70 | return OK; |
| 71 | } |
| 72 | |
| 73 | } // namespace android |