Home | History | Annotate | Download | only in jsse
      1 /*
      2  * Copyright (C) 2012 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.apache.harmony.xnet.provider.jsse;
     18 
     19 class OpenSSLKey {
     20     private final int ctx;
     21 
     22     private final OpenSSLEngine engine;
     23 
     24     OpenSSLKey(int ctx) {
     25         this.ctx = ctx;
     26         engine = null;
     27     }
     28 
     29     OpenSSLKey(int ctx, OpenSSLEngine engine) {
     30         this.ctx = ctx;
     31         this.engine = engine;
     32     }
     33 
     34     int getPkeyContext() {
     35         return ctx;
     36     }
     37 
     38     OpenSSLEngine getEngine() {
     39         return engine;
     40     }
     41 
     42     boolean isEngineBased() {
     43         return engine != null;
     44     }
     45 
     46     @Override
     47     protected void finalize() throws Throwable {
     48         try {
     49             if (ctx != 0) {
     50                 NativeCrypto.EVP_PKEY_free(ctx);
     51             }
     52         } finally {
     53             super.finalize();
     54         }
     55     }
     56 
     57     @Override
     58     public boolean equals(Object o) {
     59         if (o == this) {
     60             return true;
     61         }
     62 
     63         if (!(o instanceof OpenSSLKey)) {
     64             return false;
     65         }
     66 
     67         OpenSSLKey other = (OpenSSLKey) o;
     68         if (ctx != other.getPkeyContext()) {
     69             return false;
     70         }
     71 
     72         if (engine == null) {
     73             return other.getEngine() == null;
     74         } else {
     75             return engine.equals(other.getEngine());
     76         }
     77     }
     78 
     79     @Override
     80     public int hashCode() {
     81         int hash = 1;
     82         hash = hash * 17 + ctx;
     83         hash = hash * 31 + (engine == null ? 0 : engine.getEngineContext());
     84         return hash;
     85     }
     86 }
     87