1/*
2 * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.  Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26package com.oracle.security.ucrypto;
27
28import java.nio.ByteBuffer;
29import java.util.Set;
30import java.util.Arrays;
31import java.util.concurrent.ConcurrentSkipListSet;
32import java.lang.ref.*;
33
34import java.security.*;
35import java.security.spec.*;
36import javax.crypto.*;
37
38import javax.crypto.spec.SecretKeySpec;
39import javax.crypto.spec.IvParameterSpec;
40
41/**
42 * Internal class for context resource clean up.
43 *
44 * @since 9
45 */
46final class CipherContextRef extends PhantomReference<NativeCipher>
47    implements Comparable<CipherContextRef> {
48
49    private static ReferenceQueue<NativeCipher> refQueue =
50        new ReferenceQueue<NativeCipher>();
51
52    // Needed to keep these references from being GC'ed until when their
53    // referents are GC'ed so we can do post-mortem processing
54    private static Set<CipherContextRef> refList =
55        new ConcurrentSkipListSet<CipherContextRef>();
56
57    final long id;
58    final boolean encrypt;
59
60    private static void drainRefQueueBounded() {
61        while (true) {
62            CipherContextRef next = (CipherContextRef) refQueue.poll();
63            if (next == null) break;
64            next.dispose(true);
65        }
66    }
67
68    CipherContextRef(NativeCipher nc, long id, boolean encrypt) {
69        super(nc, refQueue);
70        this.id = id;
71        this.encrypt = encrypt;
72        refList.add(this);
73        UcryptoProvider.debug("Resource: trace CipherCtxt " + this.id);
74        drainRefQueueBounded();
75    }
76
77    public int compareTo(CipherContextRef other) {
78        if (this.id == other.id) {
79            return 0;
80        } else {
81            return (this.id < other.id) ? -1 : 1;
82        }
83    }
84
85    void dispose(boolean doCancel) {
86        refList.remove(this);
87        try {
88            if (doCancel) {
89                UcryptoProvider.debug("Resource: cancel CipherCtxt " + id);
90                int k = NativeCipher.nativeFinal(id, encrypt, null, 0);
91                if (k < 0) {
92                    UcryptoProvider.debug
93                        ("Resource: error cancelling CipherCtxt " + id +
94                        " " + new UcryptoException(-k).getMessage());
95                }
96            } else {
97                UcryptoProvider.debug("Resource: untrace CipherCtxt " + id);
98            }
99        } finally {
100            this.clear();
101        }
102    }
103}
104