LCOV - code coverage report
Current view: top level - include/hpactor/net - tls_context.hpp (source / functions) Coverage Total Hit
Test: HPActor Coverage Lines: 100.0 % 6 6
Test Date: 2026-05-20 02:24:49 Functions: 100.0 % 3 3
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: - 0 0

             Branch data     Line data    Source code
       1                 :             : // Copyright 2026 HPActor Contributors
       2                 :             : //
       3                 :             : // Licensed under the Apache License, Version 2.0 (the "License");
       4                 :             : // you may not use this file except in compliance with the License.
       5                 :             : // You may obtain a copy of the License at
       6                 :             : //
       7                 :             : //     http://www.apache.org/licenses/LICENSE-2.0
       8                 :             : //
       9                 :             : // Unless required by applicable law or agreed to in writing, software
      10                 :             : // distributed under the License is distributed on an "AS IS" BASIS,
      11                 :             : // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
      12                 :             : // See the License for the specific language governing permissions and
      13                 :             : // limitations under the License.
      14                 :             : 
      15                 :             : #pragma once
      16                 :             : 
      17                 :             : #include <hpactor/types/types.hpp>
      18                 :             : 
      19                 :             : #include <memory>
      20                 :             : #include <string>
      21                 :             : #include <vector>
      22                 :             : 
      23                 :             : namespace hpactor {
      24                 :             : 
      25                 :             : namespace net {
      26                 :             : 
      27                 :             : // Forward declarations
      28                 :             : struct TlsConfig;
      29                 :             : class TlsContext;
      30                 :             : 
      31                 :             : // -----------------------------------------------------------------------------
      32                 :             : // TlsContext - TLS configuration and crypto operations
      33                 :             : // -----------------------------------------------------------------------------
      34                 :             : // Manages certificates, private keys, and provides crypto operations for
      35                 :             : // the TLS handshake. Supports both filesystem-based and in-memory
      36                 :             : // configuration.
      37                 :             : // -----------------------------------------------------------------------------
      38                 :             : class TlsContext {
      39                 :             :   public:
      40                 :             :     // Certificate verification result
      41                 :             :     enum class CertVerifyResult {
      42                 :             :         Ok,
      43                 :             :         Invalid,
      44                 :             :         Untrusted,
      45                 :             :         Expired,
      46                 :             :         UnknownError,
      47                 :             :     };
      48                 :             : 
      49                 :             :     ~TlsContext();
      50                 :             : 
      51                 :             :     // Non-copyable
      52                 :             :     TlsContext(const TlsContext&) = delete;
      53                 :             :     TlsContext& operator=(const TlsContext&) = delete;
      54                 :             : 
      55                 :             :     // Moveable
      56                 :             :     TlsContext(TlsContext&&) noexcept;
      57                 :             :     TlsContext& operator=(TlsContext&&) noexcept;
      58                 :             : 
      59                 :             :     // Create from filesystem paths
      60                 :             :     // Expected structure:
      61                 :             :     //   cert_dir/
      62                 :             :     //     node_<node_id>.pem       - own certificate
      63                 :             :     //     node_<node_id>_key.pem   - own private key
      64                 :             :     //     ca.pem                   - trusted CA certificate
      65                 :             :     //     remote/                  - trusted peer certificates
      66                 :             :     static TlsContext
      67                 :             :     from_filesystem(EndPoint endpoint, const std::string& cert_dir);
      68                 :             : 
      69                 :             :     // Create from in-memory configuration
      70                 :             :     static TlsContext from_config(const TlsConfig& config);
      71                 :             : 
      72                 :             :     // Verify peer certificate against trusted CAs
      73                 :             :     CertVerifyResult verify_certificate(const StreamBuffer& cert_der) const;
      74                 :             : 
      75                 :             :     // Sign data with own private key (for CertificateVerify)
      76                 :             :     StreamBuffer sign_data(const StreamBuffer& data) const;
      77                 :             : 
      78                 :             :     // Decrypt pre_master_secret using own private key (RSA decryption)
      79                 :             :     // Returns true on success, false on failure
      80                 :             :     bool decrypt_pre_master_secret(const StreamBuffer& encrypted,
      81                 :             :                                    StreamBuffer& pre_master_secret) const;
      82                 :             : 
      83                 :             :     // Get public key bytes from own certificate (for sending to peer)
      84                 :           9 :     const StreamBuffer& public_key() const {
      85                 :           9 :         return public_key_;
      86                 :             :     }
      87                 :             : 
      88                 :             :     // Get own node ID
      89                 :           8 :     EndPoint endpoint() const {
      90                 :           8 :         return endpoint_;
      91                 :             :     }
      92                 :             : 
      93                 :             :     // Get own certificate in DER format
      94                 :           1 :     const StreamBuffer& certificate() const {
      95                 :           1 :         return certificate_;
      96                 :             :     }
      97                 :             : 
      98                 :             :   private:
      99                 :             :     TlsContext();
     100                 :             : 
     101                 :             :     EndPoint endpoint_;
     102                 :             :     StreamBuffer certificate_;
     103                 :             :     StreamBuffer public_key_;
     104                 :             :     StreamBuffer private_key_;
     105                 :             : 
     106                 :             :     // RSA key handle (OpenSSL)
     107                 :             :     struct RSAKey;
     108                 :             :     std::unique_ptr<RSAKey> rsa_key_;
     109                 :             : 
     110                 :             :     // Trusted CA certificates (for verification)
     111                 :             :     std::vector<StreamBuffer> ca_certs_;
     112                 :             : 
     113                 :             :     // Peer certificates (for verification)
     114                 :             :     std::vector<StreamBuffer> peer_certs_;
     115                 :             : };
     116                 :             : 
     117                 :             : // Configuration for in-memory TLS setup
     118                 :             : struct TlsConfig {
     119                 :             :     StreamBuffer own_cert_der;
     120                 :             :     StreamBuffer own_key_der;
     121                 :             :     std::vector<StreamBuffer> ca_certs_der;
     122                 :             :     EndPoint endpoint; // local endpoint for this node
     123                 :             :     bool verify_peer = true;
     124                 :             : };
     125                 :             : 
     126                 :             : } // namespace net
     127                 :             : } // namespace hpactor
        

Generated by: LCOV version 2.0-1