• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #ifndef SRC_CRYPTO_CRYPTO_X509_H_
2 #define SRC_CRYPTO_CRYPTO_X509_H_
3 
4 #if defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
5 
6 #include "base_object.h"
7 #include "crypto/crypto_util.h"
8 #include "env.h"
9 #include "memory_tracker.h"
10 #include "node_worker.h"
11 #include "v8.h"
12 
13 namespace node {
14 namespace crypto {
15 
16 // The ManagedX509 class is essentially a smart pointer for
17 // X509 objects that allows an X509Certificate instance to
18 // be cloned at the JS level while pointing at the same
19 // underlying X509 instance.
20 class ManagedX509 : public MemoryRetainer {
21  public:
22   ManagedX509() = default;
23   explicit ManagedX509(X509Pointer&& cert);
24   ManagedX509(const ManagedX509& that);
25   ManagedX509& operator=(const ManagedX509& that);
26 
27   operator bool() const { return !!cert_; }
get()28   X509* get() const { return cert_.get(); }
29 
30   void MemoryInfo(MemoryTracker* tracker) const override;
31   SET_MEMORY_INFO_NAME(ManagedX509)
32   SET_SELF_SIZE(ManagedX509)
33 
34  private:
35   X509Pointer cert_;
36 };
37 
38 class X509Certificate : public BaseObject {
39  public:
40   enum class GetPeerCertificateFlag {
41     NONE,
42     SERVER
43   };
44 
45   static void Initialize(Environment* env, v8::Local<v8::Object> target);
46   static void RegisterExternalReferences(ExternalReferenceRegistry* registry);
47   static v8::Local<v8::FunctionTemplate> GetConstructorTemplate(
48       Environment* env);
49   static bool HasInstance(Environment* env, v8::Local<v8::Object> object);
50 
51   static v8::MaybeLocal<v8::Object> New(
52       Environment* env,
53       X509Pointer cert,
54       STACK_OF(X509)* issuer_chain = nullptr);
55 
56   static v8::MaybeLocal<v8::Object> New(
57       Environment* env,
58       std::shared_ptr<ManagedX509> cert,
59       STACK_OF(X509)* issuer_chain = nullptr);
60 
61   static v8::MaybeLocal<v8::Object> GetCert(
62       Environment* env,
63       const SSLPointer& ssl);
64 
65   static v8::MaybeLocal<v8::Object> GetPeerCert(
66       Environment* env,
67       const SSLPointer& ssl,
68       GetPeerCertificateFlag flag);
69 
70   static v8::Local<v8::Object> Wrap(
71       Environment* env,
72       v8::Local<v8::Object> object,
73       X509Pointer cert);
74 
75   static void Parse(const v8::FunctionCallbackInfo<v8::Value>& args);
76   static void Subject(const v8::FunctionCallbackInfo<v8::Value>& args);
77   static void SubjectAltName(const v8::FunctionCallbackInfo<v8::Value>& args);
78   static void Issuer(const v8::FunctionCallbackInfo<v8::Value>& args);
79   static void InfoAccess(const v8::FunctionCallbackInfo<v8::Value>& args);
80   static void ValidFrom(const v8::FunctionCallbackInfo<v8::Value>& args);
81   static void ValidTo(const v8::FunctionCallbackInfo<v8::Value>& args);
82   static void KeyUsage(const v8::FunctionCallbackInfo<v8::Value>& args);
83   static void SerialNumber(const v8::FunctionCallbackInfo<v8::Value>& args);
84   static void Raw(const v8::FunctionCallbackInfo<v8::Value>& args);
85   static void PublicKey(const v8::FunctionCallbackInfo<v8::Value>& args);
86   static void Pem(const v8::FunctionCallbackInfo<v8::Value>& args);
87   static void CheckCA(const v8::FunctionCallbackInfo<v8::Value>& args);
88   static void CheckHost(const v8::FunctionCallbackInfo<v8::Value>& args);
89   static void CheckEmail(const v8::FunctionCallbackInfo<v8::Value>& args);
90   static void CheckIP(const v8::FunctionCallbackInfo<v8::Value>& args);
91   static void CheckIssued(const v8::FunctionCallbackInfo<v8::Value>& args);
92   static void CheckPrivateKey(const v8::FunctionCallbackInfo<v8::Value>& args);
93   static void Verify(const v8::FunctionCallbackInfo<v8::Value>& args);
94   static void ToLegacy(const v8::FunctionCallbackInfo<v8::Value>& args);
95   static void GetIssuerCert(const v8::FunctionCallbackInfo<v8::Value>& args);
96 
get()97   X509* get() { return cert_->get(); }
98 
99   void MemoryInfo(MemoryTracker* tracker) const override;
100   SET_MEMORY_INFO_NAME(X509Certificate)
SET_SELF_SIZE(X509Certificate)101   SET_SELF_SIZE(X509Certificate)
102 
103   class X509CertificateTransferData : public worker::TransferData {
104    public:
105     explicit X509CertificateTransferData(
106         const std::shared_ptr<ManagedX509>& data)
107         : data_(data) {}
108 
109     BaseObjectPtr<BaseObject> Deserialize(
110         Environment* env,
111         v8::Local<v8::Context> context,
112         std::unique_ptr<worker::TransferData> self) override;
113 
114     SET_MEMORY_INFO_NAME(X509CertificateTransferData)
115     SET_SELF_SIZE(X509CertificateTransferData)
116     SET_NO_MEMORY_INFO()
117 
118    private:
119     std::shared_ptr<ManagedX509> data_;
120   };
121 
122   BaseObject::TransferMode GetTransferMode() const override;
123   std::unique_ptr<worker::TransferData> CloneForMessaging() const override;
124 
125  private:
126   X509Certificate(
127       Environment* env,
128       v8::Local<v8::Object> object,
129       std::shared_ptr<ManagedX509> cert,
130       STACK_OF(X509)* issuer_chain = nullptr);
131 
132   std::shared_ptr<ManagedX509> cert_;
133   BaseObjectPtr<X509Certificate> issuer_cert_;
134 };
135 
136 }  // namespace crypto
137 }  // namespace node
138 
139 #endif  // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
140 #endif  // SRC_CRYPTO_CRYPTO_X509_H_
141