summaryrefslogtreecommitdiff
path: root/src/crypto/crypto_x509.h
blob: 00b7689a194a3262ebe3faa454d54bc7dc8698a5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
#ifndef SRC_CRYPTO_CRYPTO_X509_H_
#define SRC_CRYPTO_CRYPTO_X509_H_

#if defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS

#include "base_object.h"
#include "crypto/crypto_util.h"
#include "env.h"
#include "memory_tracker.h"
#include "node_worker.h"
#include "v8.h"

namespace node {
namespace crypto {

// The ManagedX509 class is essentially a smart pointer for
// X509 objects that allows an X509Certificate instance to
// be cloned at the JS level while pointing at the same
// underlying X509 instance.
class ManagedX509 : public MemoryRetainer {
 public:
  ManagedX509() = default;
  explicit ManagedX509(X509Pointer&& cert);
  ManagedX509(const ManagedX509& that);
  ManagedX509& operator=(const ManagedX509& that);

  operator bool() const { return !!cert_; }
  X509* get() const { return cert_.get(); }

  void MemoryInfo(MemoryTracker* tracker) const override;
  SET_MEMORY_INFO_NAME(ManagedX509)
  SET_SELF_SIZE(ManagedX509)

 private:
  X509Pointer cert_;
};

class X509Certificate : public BaseObject {
 public:
  enum class GetPeerCertificateFlag {
    NONE,
    SERVER
  };

  static void Initialize(Environment* env, v8::Local<v8::Object> target);
  static void RegisterExternalReferences(ExternalReferenceRegistry* registry);
  static v8::Local<v8::FunctionTemplate> GetConstructorTemplate(
      Environment* env);
  static bool HasInstance(Environment* env, v8::Local<v8::Object> object);

  static v8::MaybeLocal<v8::Object> New(
      Environment* env,
      X509Pointer cert,
      STACK_OF(X509)* issuer_chain = nullptr);

  static v8::MaybeLocal<v8::Object> New(
      Environment* env,
      std::shared_ptr<ManagedX509> cert,
      STACK_OF(X509)* issuer_chain = nullptr);

  static v8::MaybeLocal<v8::Object> GetCert(
      Environment* env,
      const SSLPointer& ssl);

  static v8::MaybeLocal<v8::Object> GetPeerCert(
      Environment* env,
      const SSLPointer& ssl,
      GetPeerCertificateFlag flag);

  static v8::Local<v8::Object> Wrap(
      Environment* env,
      v8::Local<v8::Object> object,
      X509Pointer cert);

  static void Parse(const v8::FunctionCallbackInfo<v8::Value>& args);
  static void Subject(const v8::FunctionCallbackInfo<v8::Value>& args);
  static void SubjectAltName(const v8::FunctionCallbackInfo<v8::Value>& args);
  static void Issuer(const v8::FunctionCallbackInfo<v8::Value>& args);
  static void InfoAccess(const v8::FunctionCallbackInfo<v8::Value>& args);
  static void ValidFrom(const v8::FunctionCallbackInfo<v8::Value>& args);
  static void ValidTo(const v8::FunctionCallbackInfo<v8::Value>& args);
  static void KeyUsage(const v8::FunctionCallbackInfo<v8::Value>& args);
  static void SerialNumber(const v8::FunctionCallbackInfo<v8::Value>& args);
  static void Raw(const v8::FunctionCallbackInfo<v8::Value>& args);
  static void PublicKey(const v8::FunctionCallbackInfo<v8::Value>& args);
  static void Pem(const v8::FunctionCallbackInfo<v8::Value>& args);
  static void CheckCA(const v8::FunctionCallbackInfo<v8::Value>& args);
  static void CheckHost(const v8::FunctionCallbackInfo<v8::Value>& args);
  static void CheckEmail(const v8::FunctionCallbackInfo<v8::Value>& args);
  static void CheckIP(const v8::FunctionCallbackInfo<v8::Value>& args);
  static void CheckIssued(const v8::FunctionCallbackInfo<v8::Value>& args);
  static void CheckPrivateKey(const v8::FunctionCallbackInfo<v8::Value>& args);
  static void Verify(const v8::FunctionCallbackInfo<v8::Value>& args);
  static void ToLegacy(const v8::FunctionCallbackInfo<v8::Value>& args);
  static void GetIssuerCert(const v8::FunctionCallbackInfo<v8::Value>& args);

  X509* get() { return cert_->get(); }

  void MemoryInfo(MemoryTracker* tracker) const override;
  SET_MEMORY_INFO_NAME(X509Certificate)
  SET_SELF_SIZE(X509Certificate)

  class X509CertificateTransferData : public worker::TransferData {
   public:
    explicit X509CertificateTransferData(
        const std::shared_ptr<ManagedX509>& data)
        : data_(data) {}

    BaseObjectPtr<BaseObject> Deserialize(
        Environment* env,
        v8::Local<v8::Context> context,
        std::unique_ptr<worker::TransferData> self) override;

    SET_MEMORY_INFO_NAME(X509CertificateTransferData)
    SET_SELF_SIZE(X509CertificateTransferData)
    SET_NO_MEMORY_INFO()

   private:
    std::shared_ptr<ManagedX509> data_;
  };

  BaseObject::TransferMode GetTransferMode() const override;
  std::unique_ptr<worker::TransferData> CloneForMessaging() const override;

 private:
  X509Certificate(
      Environment* env,
      v8::Local<v8::Object> object,
      std::shared_ptr<ManagedX509> cert,
      STACK_OF(X509)* issuer_chain = nullptr);

  std::shared_ptr<ManagedX509> cert_;
  BaseObjectPtr<X509Certificate> issuer_cert_;
};

}  // namespace crypto
}  // namespace node

#endif  // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
#endif  // SRC_CRYPTO_CRYPTO_X509_H_