blob: d9a4939a7e4526a9b5b2922169ecee40ab1eae73 [file] [log] [blame]
Taylor Brandstetter165c6182020-12-10 16:23:03 -08001/*
2 * Copyright 2020 The WebRTC Project Authors. All rights reserved.
3 *
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
10
11#ifndef RTC_BASE_OPENSSL_KEY_PAIR_H_
12#define RTC_BASE_OPENSSL_KEY_PAIR_H_
13
14#include <openssl/ossl_typ.h>
15
16#include <memory>
17#include <string>
18
19#include "rtc_base/checks.h"
Taylor Brandstetter165c6182020-12-10 16:23:03 -080020#include "rtc_base/ssl_identity.h"
21
22namespace rtc {
23
24// OpenSSLKeyPair encapsulates an OpenSSL EVP_PKEY* keypair object,
25// which is reference counted inside the OpenSSL library.
26class OpenSSLKeyPair final {
27 public:
28 // Takes ownership of the key.
29 explicit OpenSSLKeyPair(EVP_PKEY* pkey) : pkey_(pkey) {
30 RTC_DCHECK(pkey_ != nullptr);
31 }
32
33 static std::unique_ptr<OpenSSLKeyPair> Generate(const KeyParams& key_params);
34 // Constructs a key pair from the private key PEM string. This must not result
35 // in missing public key parameters. Returns null on error.
36 static std::unique_ptr<OpenSSLKeyPair> FromPrivateKeyPEMString(
37 const std::string& pem_string);
38
39 ~OpenSSLKeyPair();
40
Byoungchan Lee14af7622022-01-12 05:24:58 +090041 OpenSSLKeyPair(const OpenSSLKeyPair&) = delete;
42 OpenSSLKeyPair& operator=(const OpenSSLKeyPair&) = delete;
43
Taylor Brandstetter165c6182020-12-10 16:23:03 -080044 std::unique_ptr<OpenSSLKeyPair> Clone();
45
46 EVP_PKEY* pkey() const { return pkey_; }
47 std::string PrivateKeyToPEMString() const;
48 std::string PublicKeyToPEMString() const;
49 bool operator==(const OpenSSLKeyPair& other) const;
50 bool operator!=(const OpenSSLKeyPair& other) const;
51
52 private:
53 void AddReference();
54
55 EVP_PKEY* pkey_;
Taylor Brandstetter165c6182020-12-10 16:23:03 -080056};
57
58} // namespace rtc
59
60#endif // RTC_BASE_OPENSSL_KEY_PAIR_H_