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 abstract class NativeRef { 24 final long context; 25 NativeRef(long context)26 NativeRef(long context) { 27 if (context == 0) { 28 throw new NullPointerException("context == 0"); 29 } 30 31 this.context = context; 32 } 33 34 @Override equals(Object o)35 public boolean equals(Object o) { 36 if (!(o instanceof NativeRef)) { 37 return false; 38 } 39 40 return ((NativeRef) o).context == context; 41 } 42 43 @Override hashCode()44 public int hashCode() { 45 return (int) context; 46 } 47 48 @Override finalize()49 protected void finalize() throws Throwable { 50 try { 51 if (context != 0) { 52 doFree(context); 53 } 54 } finally { 55 super.finalize(); 56 } 57 } 58 doFree(long context)59 abstract void doFree(long context); 60 61 static final class EC_GROUP extends NativeRef { EC_GROUP(long ctx)62 EC_GROUP(long ctx) { 63 super(ctx); 64 } 65 66 @Override doFree(long context)67 void doFree(long context) { 68 NativeCrypto.EC_GROUP_clear_free(context); 69 } 70 } 71 72 static final class EC_POINT extends NativeRef { EC_POINT(long nativePointer)73 EC_POINT(long nativePointer) { 74 super(nativePointer); 75 } 76 77 @Override doFree(long context)78 void doFree(long context) { 79 NativeCrypto.EC_POINT_clear_free(context); 80 } 81 } 82 83 static final class EVP_CIPHER_CTX extends NativeRef { EVP_CIPHER_CTX(long nativePointer)84 EVP_CIPHER_CTX(long nativePointer) { 85 super(nativePointer); 86 } 87 88 @Override doFree(long context)89 void doFree(long context) { 90 NativeCrypto.EVP_CIPHER_CTX_free(context); 91 } 92 } 93 94 static final class EVP_MD_CTX extends NativeRef { EVP_MD_CTX(long nativePointer)95 EVP_MD_CTX(long nativePointer) { 96 super(nativePointer); 97 } 98 99 @Override doFree(long context)100 void doFree(long context) { 101 NativeCrypto.EVP_MD_CTX_destroy(context); 102 } 103 } 104 105 static final class EVP_PKEY extends NativeRef { EVP_PKEY(long nativePointer)106 EVP_PKEY(long nativePointer) { 107 super(nativePointer); 108 } 109 110 @Override doFree(long context)111 void doFree(long context) { 112 NativeCrypto.EVP_PKEY_free(context); 113 } 114 } 115 116 static final class EVP_PKEY_CTX extends NativeRef { EVP_PKEY_CTX(long nativePointer)117 EVP_PKEY_CTX(long nativePointer) { 118 super(nativePointer); 119 } 120 121 @Override doFree(long context)122 void doFree(long context) { 123 NativeCrypto.EVP_PKEY_CTX_free(context); 124 } 125 } 126 127 static final class HMAC_CTX extends NativeRef { HMAC_CTX(long nativePointer)128 HMAC_CTX(long nativePointer) { 129 super(nativePointer); 130 } 131 132 @Override doFree(long context)133 void doFree(long context) { 134 NativeCrypto.HMAC_CTX_free(context); 135 } 136 } 137 138 static final class SSL_SESSION extends NativeRef { SSL_SESSION(long nativePointer)139 SSL_SESSION(long nativePointer) { 140 super(nativePointer); 141 } 142 143 @Override doFree(long context)144 void doFree(long context) { 145 NativeCrypto.SSL_SESSION_free(context); 146 } 147 } 148 } 149