1 module hunt.net.secure.conscrypt.NativeRef;
2 
3 // dfmt off
4 version(WITH_HUNT_SECURITY):
5 // dfmt on
6 
7 import hunt.net.secure.conscrypt.NativeCrypto;
8 import hunt.Exceptions;
9 
10 /**
11  * Used to hold onto native OpenSSL references and run finalization on those
12  * objects. Individual types must subclass this and implement finalizer.
13  */
14 abstract class NativeRef {
15     long context;
16 
17     this(long context) {
18         if (context == 0) {
19             throw new NullPointerException("context == 0");
20         }
21 
22         this.context = context;
23     }
24 
25     override bool opEquals(Object o) {
26         NativeRef that = cast(NativeRef) o;
27         if (that is null) {
28             return false;
29         }
30 
31         return that.context == context;
32     }
33 
34     override size_t toHash() @trusted nothrow {
35         return cast(size_t)context;
36     }
37 
38     protected void finalize() {
39         try {
40             if (context != 0) {
41                 doFree(context);
42             }
43         } finally {
44             // super.finalize();
45         }
46     }
47 
48     abstract void doFree(long context);
49 
50     // static final class EC_GROUP : NativeRef {
51     //     EC_GROUP(long ctx) {
52     //         super(ctx);
53     //     }
54 
55     //     override
56     //     void doFree(long context) {
57     //         NativeCrypto.EC_GROUP_clear_free(context);
58     //     }
59     // }
60 
61     // static final class EC_POINT : NativeRef {
62     //     EC_POINT(long nativePointer) {
63     //         super(nativePointer);
64     //     }
65 
66     //     override
67     //     void doFree(long context) {
68     //         NativeCrypto.EC_POINT_clear_free(context);
69     //     }
70     // }
71 
72     // static final class EVP_CIPHER_CTX : NativeRef {
73     //     EVP_CIPHER_CTX(long nativePointer) {
74     //         super(nativePointer);
75     //     }
76 
77     //     override
78     //     void doFree(long context) {
79     //         NativeCrypto.EVP_CIPHER_CTX_free(context);
80     //     }
81     // }
82 
83     // static final class EVP_MD_CTX : NativeRef {
84     //     EVP_MD_CTX(long nativePointer) {
85     //         super(nativePointer);
86     //     }
87 
88     //     override
89     //     void doFree(long context) {
90     //         NativeCrypto.EVP_MD_CTX_destroy(context);
91     //     }
92     // }
93 
94     static final class EVP_PKEY : NativeRef {
95         this(long nativePointer) {
96             super(nativePointer);
97         }
98 
99         override
100         void doFree(long context) {
101             NativeCrypto.EVP_PKEY_free(context);
102         }
103     }
104 
105     // static final class EVP_PKEY_CTX : NativeRef {
106     //     EVP_PKEY_CTX(long nativePointer) {
107     //         super(nativePointer);
108     //     }
109 
110     //     override
111     //     void doFree(long context) {
112     //         NativeCrypto.EVP_PKEY_CTX_free(context);
113     //     }
114     // }
115 
116     // static final class HMAC_CTX : NativeRef {
117     //     HMAC_CTX(long nativePointer) {
118     //         super(nativePointer);
119     //     }
120 
121     //     override
122     //     void doFree(long context) {
123     //         NativeCrypto.HMAC_CTX_free(context);
124     //     }
125     // }
126 
127     static final class SSL_SESSION : NativeRef {
128         this(long nativePointer) {
129             super(nativePointer);
130         }
131 
132         override
133         void doFree(long context) {
134             NativeCrypto.SSL_SESSION_free(context);
135         }
136     }
137 }