• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2014 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package org.conscrypt;
18 
19 /**
20  * Used to hold onto native OpenSSL references and run finalization on those
21  * objects. Individual types must subclass this and implement finalizer.
22  *
23  * @hide
24  */
25 @Internal
26 public abstract class NativeRef {
27     final long context;
28 
NativeRef(long ctx)29     public NativeRef(long ctx) {
30         if (ctx == 0) {
31             throw new NullPointerException("ctx == 0");
32         }
33 
34         this.context = ctx;
35     }
36 
37     @Override
equals(Object o)38     public boolean equals(Object o) {
39         if (!(o instanceof NativeRef)) {
40             return false;
41         }
42 
43         return ((NativeRef) o).context == context;
44     }
45 
46     @Override
hashCode()47     public int hashCode() {
48         return (int) context;
49     }
50 
51     public static class EC_GROUP extends NativeRef {
EC_GROUP(long ctx)52         public EC_GROUP(long ctx) {
53             super(ctx);
54         }
55 
56         @Override
finalize()57         protected void finalize() throws Throwable {
58             try {
59                 NativeCrypto.EC_GROUP_clear_free(context);
60             } finally {
61                 super.finalize();
62             }
63         }
64     }
65 
66     public static class EC_POINT extends NativeRef {
EC_POINT(long ctx)67         public EC_POINT(long ctx) {
68             super(ctx);
69         }
70 
71         @Override
finalize()72         protected void finalize() throws Throwable {
73             try {
74                 NativeCrypto.EC_POINT_clear_free(context);
75             } finally {
76                 super.finalize();
77             }
78         }
79     }
80 
81     public static class EVP_CIPHER_CTX extends NativeRef {
EVP_CIPHER_CTX(long ctx)82         public EVP_CIPHER_CTX(long ctx) {
83             super(ctx);
84         }
85 
86         @Override
finalize()87         protected void finalize() throws Throwable {
88             try {
89                 NativeCrypto.EVP_CIPHER_CTX_free(context);
90             } finally {
91                 super.finalize();
92             }
93         }
94     }
95 
96     public static class EVP_MD_CTX extends NativeRef {
EVP_MD_CTX(long ctx)97         public EVP_MD_CTX(long ctx) {
98             super(ctx);
99         }
100 
101         @Override
finalize()102         protected void finalize() throws Throwable {
103             try {
104                 NativeCrypto.EVP_MD_CTX_destroy(context);
105             } finally {
106                 super.finalize();
107             }
108         }
109     }
110 
111     public static class EVP_PKEY extends NativeRef {
EVP_PKEY(long ctx)112         public EVP_PKEY(long ctx) {
113             super(ctx);
114         }
115 
116         @Override
finalize()117         protected void finalize() throws Throwable {
118             try {
119                 NativeCrypto.EVP_PKEY_free(context);
120             } finally {
121                 super.finalize();
122             }
123         }
124     }
125 
126     public static class EVP_PKEY_CTX extends NativeRef {
EVP_PKEY_CTX(long ctx)127         public EVP_PKEY_CTX(long ctx) {
128             super(ctx);
129         }
130 
131         @Override
finalize()132         protected void finalize() throws Throwable {
133             try {
134                 NativeCrypto.EVP_PKEY_CTX_free(context);
135             } finally {
136                 super.finalize();
137             }
138         }
139     }
140 
141     public static class HMAC_CTX extends NativeRef {
HMAC_CTX(long ctx)142         public HMAC_CTX(long ctx) {
143             super(ctx);
144         }
145 
146         @Override
finalize()147         protected void finalize() throws Throwable {
148             try {
149                 NativeCrypto.HMAC_CTX_free(context);
150             } finally {
151                 super.finalize();
152             }
153         }
154     }
155 }
156