src/java.smartcardio/unix/native/libj2pcsc/pcsc_md.c
author mli
Thu, 17 Oct 2019 13:34:06 +0800
changeset 58661 d2108d9fc4f5
parent 47216 71c04702a3d5
permissions -rw-r--r--
8134599: TEST_BUG: java/rmi/transport/closeServerSocket/CloseServerSocket.java fails intermittently with Address already in use Reviewed-by: weijun, darcy, rriggs, coffeys
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
23060
61ffe815de7c 8033571: [parfait] warning from b128 for security/smartcardio/pcsc_md.c: JNI exception pending
valeriep
parents: 23010
diff changeset
     2
 * Copyright (c) 2005, 2014, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
#include <stdio.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
#include <stdlib.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
#include <string.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
#include <assert.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
#include <dlfcn.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
#include <winscard.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
#include "sun_security_smartcardio_PlatformPCSC.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
#include "pcsc_md.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
void *hModule;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
FPTR_SCardEstablishContext scardEstablishContext;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
FPTR_SCardConnect scardConnect;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
FPTR_SCardDisconnect scardDisconnect;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
FPTR_SCardStatus scardStatus;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
FPTR_SCardGetStatusChange scardGetStatusChange;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
FPTR_SCardTransmit scardTransmit;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
FPTR_SCardListReaders scardListReaders;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
FPTR_SCardBeginTransaction scardBeginTransaction;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
FPTR_SCardEndTransaction scardEndTransaction;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
FPTR_SCardControl scardControl;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
10798
413b731e1818 7103549: Remove dependencies on libjava and libjvm from security libraries
chegar
parents: 5506
diff changeset
    51
/*
413b731e1818 7103549: Remove dependencies on libjava and libjvm from security libraries
chegar
parents: 5506
diff changeset
    52
 * Throws a Java Exception by name
413b731e1818 7103549: Remove dependencies on libjava and libjvm from security libraries
chegar
parents: 5506
diff changeset
    53
 */
413b731e1818 7103549: Remove dependencies on libjava and libjvm from security libraries
chegar
parents: 5506
diff changeset
    54
void throwByName(JNIEnv *env, const char *name, const char *msg)
413b731e1818 7103549: Remove dependencies on libjava and libjvm from security libraries
chegar
parents: 5506
diff changeset
    55
{
413b731e1818 7103549: Remove dependencies on libjava and libjvm from security libraries
chegar
parents: 5506
diff changeset
    56
    jclass cls = (*env)->FindClass(env, name);
413b731e1818 7103549: Remove dependencies on libjava and libjvm from security libraries
chegar
parents: 5506
diff changeset
    57
413b731e1818 7103549: Remove dependencies on libjava and libjvm from security libraries
chegar
parents: 5506
diff changeset
    58
    if (cls != 0) /* Otherwise an exception has already been thrown */
413b731e1818 7103549: Remove dependencies on libjava and libjvm from security libraries
chegar
parents: 5506
diff changeset
    59
        (*env)->ThrowNew(env, cls, msg);
413b731e1818 7103549: Remove dependencies on libjava and libjvm from security libraries
chegar
parents: 5506
diff changeset
    60
}
413b731e1818 7103549: Remove dependencies on libjava and libjvm from security libraries
chegar
parents: 5506
diff changeset
    61
413b731e1818 7103549: Remove dependencies on libjava and libjvm from security libraries
chegar
parents: 5506
diff changeset
    62
/*
413b731e1818 7103549: Remove dependencies on libjava and libjvm from security libraries
chegar
parents: 5506
diff changeset
    63
 * Throws java.lang.NullPointerException
413b731e1818 7103549: Remove dependencies on libjava and libjvm from security libraries
chegar
parents: 5506
diff changeset
    64
 */
413b731e1818 7103549: Remove dependencies on libjava and libjvm from security libraries
chegar
parents: 5506
diff changeset
    65
void throwNullPointerException(JNIEnv *env, const char *msg)
413b731e1818 7103549: Remove dependencies on libjava and libjvm from security libraries
chegar
parents: 5506
diff changeset
    66
{
413b731e1818 7103549: Remove dependencies on libjava and libjvm from security libraries
chegar
parents: 5506
diff changeset
    67
    throwByName(env, "java/lang/NullPointerException", msg);
413b731e1818 7103549: Remove dependencies on libjava and libjvm from security libraries
chegar
parents: 5506
diff changeset
    68
}
413b731e1818 7103549: Remove dependencies on libjava and libjvm from security libraries
chegar
parents: 5506
diff changeset
    69
413b731e1818 7103549: Remove dependencies on libjava and libjvm from security libraries
chegar
parents: 5506
diff changeset
    70
/*
413b731e1818 7103549: Remove dependencies on libjava and libjvm from security libraries
chegar
parents: 5506
diff changeset
    71
 * Throws java.io.IOException
413b731e1818 7103549: Remove dependencies on libjava and libjvm from security libraries
chegar
parents: 5506
diff changeset
    72
 */
413b731e1818 7103549: Remove dependencies on libjava and libjvm from security libraries
chegar
parents: 5506
diff changeset
    73
void throwIOException(JNIEnv *env, const char *msg)
413b731e1818 7103549: Remove dependencies on libjava and libjvm from security libraries
chegar
parents: 5506
diff changeset
    74
{
413b731e1818 7103549: Remove dependencies on libjava and libjvm from security libraries
chegar
parents: 5506
diff changeset
    75
    throwByName(env, "java/io/IOException", msg);
413b731e1818 7103549: Remove dependencies on libjava and libjvm from security libraries
chegar
parents: 5506
diff changeset
    76
}
413b731e1818 7103549: Remove dependencies on libjava and libjvm from security libraries
chegar
parents: 5506
diff changeset
    77
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
void *findFunction(JNIEnv *env, void *hModule, char *functionName) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    void *fAddress = dlsym(hModule, functionName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    if (fAddress == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        char errorMessage[256];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        snprintf(errorMessage, sizeof(errorMessage), "Symbol not found: %s", functionName);
10798
413b731e1818 7103549: Remove dependencies on libjava and libjvm from security libraries
chegar
parents: 5506
diff changeset
    83
        throwNullPointerException(env, errorMessage);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        return NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    return fAddress;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
JNIEXPORT void JNICALL Java_sun_security_smartcardio_PlatformPCSC_initialize
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        (JNIEnv *env, jclass thisClass, jstring jLibName) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    const char *libName = (*env)->GetStringUTFChars(env, jLibName, NULL);
23060
61ffe815de7c 8033571: [parfait] warning from b128 for security/smartcardio/pcsc_md.c: JNI exception pending
valeriep
parents: 23010
diff changeset
    92
    if (libName == NULL) {
61ffe815de7c 8033571: [parfait] warning from b128 for security/smartcardio/pcsc_md.c: JNI exception pending
valeriep
parents: 23010
diff changeset
    93
        throwNullPointerException(env, "PCSC library name is null");
61ffe815de7c 8033571: [parfait] warning from b128 for security/smartcardio/pcsc_md.c: JNI exception pending
valeriep
parents: 23010
diff changeset
    94
        return;
61ffe815de7c 8033571: [parfait] warning from b128 for security/smartcardio/pcsc_md.c: JNI exception pending
valeriep
parents: 23010
diff changeset
    95
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    hModule = dlopen(libName, RTLD_LAZY);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    (*env)->ReleaseStringUTFChars(env, jLibName, libName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    if (hModule == NULL) {
10798
413b731e1818 7103549: Remove dependencies on libjava and libjvm from security libraries
chegar
parents: 5506
diff changeset
   100
        throwIOException(env, dlerror());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    scardEstablishContext = (FPTR_SCardEstablishContext)findFunction(env, hModule, "SCardEstablishContext");
23060
61ffe815de7c 8033571: [parfait] warning from b128 for security/smartcardio/pcsc_md.c: JNI exception pending
valeriep
parents: 23010
diff changeset
   104
    if ((*env)->ExceptionCheck(env)) {
61ffe815de7c 8033571: [parfait] warning from b128 for security/smartcardio/pcsc_md.c: JNI exception pending
valeriep
parents: 23010
diff changeset
   105
         return;
61ffe815de7c 8033571: [parfait] warning from b128 for security/smartcardio/pcsc_md.c: JNI exception pending
valeriep
parents: 23010
diff changeset
   106
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    scardConnect          = (FPTR_SCardConnect)         findFunction(env, hModule, "SCardConnect");
23060
61ffe815de7c 8033571: [parfait] warning from b128 for security/smartcardio/pcsc_md.c: JNI exception pending
valeriep
parents: 23010
diff changeset
   108
    if ((*env)->ExceptionCheck(env)) {
61ffe815de7c 8033571: [parfait] warning from b128 for security/smartcardio/pcsc_md.c: JNI exception pending
valeriep
parents: 23010
diff changeset
   109
         return;
61ffe815de7c 8033571: [parfait] warning from b128 for security/smartcardio/pcsc_md.c: JNI exception pending
valeriep
parents: 23010
diff changeset
   110
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    scardDisconnect       = (FPTR_SCardDisconnect)      findFunction(env, hModule, "SCardDisconnect");
23060
61ffe815de7c 8033571: [parfait] warning from b128 for security/smartcardio/pcsc_md.c: JNI exception pending
valeriep
parents: 23010
diff changeset
   112
    if ((*env)->ExceptionCheck(env)) {
61ffe815de7c 8033571: [parfait] warning from b128 for security/smartcardio/pcsc_md.c: JNI exception pending
valeriep
parents: 23010
diff changeset
   113
         return;
61ffe815de7c 8033571: [parfait] warning from b128 for security/smartcardio/pcsc_md.c: JNI exception pending
valeriep
parents: 23010
diff changeset
   114
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    scardStatus           = (FPTR_SCardStatus)          findFunction(env, hModule, "SCardStatus");
23060
61ffe815de7c 8033571: [parfait] warning from b128 for security/smartcardio/pcsc_md.c: JNI exception pending
valeriep
parents: 23010
diff changeset
   116
    if ((*env)->ExceptionCheck(env)) {
61ffe815de7c 8033571: [parfait] warning from b128 for security/smartcardio/pcsc_md.c: JNI exception pending
valeriep
parents: 23010
diff changeset
   117
         return;
61ffe815de7c 8033571: [parfait] warning from b128 for security/smartcardio/pcsc_md.c: JNI exception pending
valeriep
parents: 23010
diff changeset
   118
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    scardGetStatusChange  = (FPTR_SCardGetStatusChange) findFunction(env, hModule, "SCardGetStatusChange");
23060
61ffe815de7c 8033571: [parfait] warning from b128 for security/smartcardio/pcsc_md.c: JNI exception pending
valeriep
parents: 23010
diff changeset
   120
    if ((*env)->ExceptionCheck(env)) {
61ffe815de7c 8033571: [parfait] warning from b128 for security/smartcardio/pcsc_md.c: JNI exception pending
valeriep
parents: 23010
diff changeset
   121
         return;
61ffe815de7c 8033571: [parfait] warning from b128 for security/smartcardio/pcsc_md.c: JNI exception pending
valeriep
parents: 23010
diff changeset
   122
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    scardTransmit         = (FPTR_SCardTransmit)        findFunction(env, hModule, "SCardTransmit");
23060
61ffe815de7c 8033571: [parfait] warning from b128 for security/smartcardio/pcsc_md.c: JNI exception pending
valeriep
parents: 23010
diff changeset
   124
    if ((*env)->ExceptionCheck(env)) {
61ffe815de7c 8033571: [parfait] warning from b128 for security/smartcardio/pcsc_md.c: JNI exception pending
valeriep
parents: 23010
diff changeset
   125
         return;
61ffe815de7c 8033571: [parfait] warning from b128 for security/smartcardio/pcsc_md.c: JNI exception pending
valeriep
parents: 23010
diff changeset
   126
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    scardListReaders      = (FPTR_SCardListReaders)     findFunction(env, hModule, "SCardListReaders");
23060
61ffe815de7c 8033571: [parfait] warning from b128 for security/smartcardio/pcsc_md.c: JNI exception pending
valeriep
parents: 23010
diff changeset
   128
    if ((*env)->ExceptionCheck(env)) {
61ffe815de7c 8033571: [parfait] warning from b128 for security/smartcardio/pcsc_md.c: JNI exception pending
valeriep
parents: 23010
diff changeset
   129
         return;
61ffe815de7c 8033571: [parfait] warning from b128 for security/smartcardio/pcsc_md.c: JNI exception pending
valeriep
parents: 23010
diff changeset
   130
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
    scardBeginTransaction = (FPTR_SCardBeginTransaction)findFunction(env, hModule, "SCardBeginTransaction");
23060
61ffe815de7c 8033571: [parfait] warning from b128 for security/smartcardio/pcsc_md.c: JNI exception pending
valeriep
parents: 23010
diff changeset
   132
    if ((*env)->ExceptionCheck(env)) {
61ffe815de7c 8033571: [parfait] warning from b128 for security/smartcardio/pcsc_md.c: JNI exception pending
valeriep
parents: 23010
diff changeset
   133
         return;
61ffe815de7c 8033571: [parfait] warning from b128 for security/smartcardio/pcsc_md.c: JNI exception pending
valeriep
parents: 23010
diff changeset
   134
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
    scardEndTransaction   = (FPTR_SCardEndTransaction)  findFunction(env, hModule, "SCardEndTransaction");
23060
61ffe815de7c 8033571: [parfait] warning from b128 for security/smartcardio/pcsc_md.c: JNI exception pending
valeriep
parents: 23010
diff changeset
   136
    if ((*env)->ExceptionCheck(env)) {
61ffe815de7c 8033571: [parfait] warning from b128 for security/smartcardio/pcsc_md.c: JNI exception pending
valeriep
parents: 23010
diff changeset
   137
         return;
61ffe815de7c 8033571: [parfait] warning from b128 for security/smartcardio/pcsc_md.c: JNI exception pending
valeriep
parents: 23010
diff changeset
   138
    }
24622
0b2ad6558dc6 8039319: (smartcardio) Card.transmitControlCommand() does not work on Mac OS X
igerasim
parents: 23060
diff changeset
   139
#ifndef __APPLE__
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
    scardControl          = (FPTR_SCardControl)         findFunction(env, hModule, "SCardControl");
24622
0b2ad6558dc6 8039319: (smartcardio) Card.transmitControlCommand() does not work on Mac OS X
igerasim
parents: 23060
diff changeset
   141
#else
0b2ad6558dc6 8039319: (smartcardio) Card.transmitControlCommand() does not work on Mac OS X
igerasim
parents: 23060
diff changeset
   142
    scardControl          = (FPTR_SCardControl)         findFunction(env, hModule, "SCardControl132");
0b2ad6558dc6 8039319: (smartcardio) Card.transmitControlCommand() does not work on Mac OS X
igerasim
parents: 23060
diff changeset
   143
#endif // __APPLE__
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
}