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