< prev index next >

src/jdk.crypto.mscapi/windows/native/libsunmscapi/security.cpp

Print this page




  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 
  26 //=--------------------------------------------------------------------------=
  27 // security.cpp    by Stanley Man-Kit Ho
  28 //=--------------------------------------------------------------------------=
  29 //
  30 
  31 #include <jni.h>
  32 #include "jni_util.h"
  33 #include <stdlib.h>
  34 #include <string.h>
  35 #include <windows.h>
  36 #include <BaseTsd.h>
  37 #include <wincrypt.h>
  38 #include <stdio.h>
  39 #include <memory>
  40 






  41 
  42 #define OID_EKU_ANY         "2.5.29.37.0"
  43 
  44 #define CERTIFICATE_PARSING_EXCEPTION \
  45                             "java/security/cert/CertificateParsingException"
  46 #define INVALID_KEY_EXCEPTION \
  47                             "java/security/InvalidKeyException"
  48 #define KEY_EXCEPTION       "java/security/KeyException"
  49 #define KEYSTORE_EXCEPTION  "java/security/KeyStoreException"
  50 #define PROVIDER_EXCEPTION  "java/security/ProviderException"
  51 #define SIGNATURE_EXCEPTION "java/security/SignatureException"
  52 #define OUT_OF_MEMORY_ERROR "java/lang/OutOfMemoryError"
  53 
  54 extern "C" {
  55 
  56 /*
  57  * Declare library specific JNI_Onload entry if static build
  58  */
  59 DEF_STATIC_JNI_OnLoad
  60 


1296         if (pbCertEncoding)
1297             delete [] pbCertEncoding;
1298 
1299         if (pszNameString)
1300             delete [] pszNameString;
1301 
1302         if (pCertContext)
1303             ::CertFreeCertificateContext(pCertContext);
1304 
1305         if (bDeleteAttempted && pTBDCertContext)
1306             ::CertFreeCertificateContext(pTBDCertContext);
1307     }
1308 }
1309 
1310 /*
1311  * Class:     sun_security_mscapi_KeyStore
1312  * Method:    destroyKeyContainer
1313  * Signature: (Ljava/lang/String;)V
1314  */
1315 JNIEXPORT void JNICALL Java_sun_security_mscapi_KeyStore_destroyKeyContainer
1316   (JNIEnv *env, jclass clazz, jstring keyContainerName)
1317 {
1318     HCRYPTPROV hCryptProv = NULL;
1319     const char* pszKeyContainerName = NULL;
1320 
1321     __try
1322     {
1323         if ((pszKeyContainerName =
1324             env->GetStringUTFChars(keyContainerName, NULL)) == NULL) {
1325             __leave;
1326         }
1327 
1328         // Destroying the default key container is not permitted
1329         // (because it may contain more one keypair).
1330         if (pszKeyContainerName == NULL) {
1331 
1332             ThrowException(env, KEYSTORE_EXCEPTION, NTE_BAD_KEYSET_PARAM);
1333             __leave;
1334         }
1335 
1336         // Acquire a CSP context (to the key container).


1418         result = env->NewByteArray(dwBufLen);
1419 
1420         // Copy data from native buffer to Java buffer
1421         env->SetByteArrayRegion(result, 0, dwBufLen, (jbyte*) pData);
1422     }
1423     __finally
1424     {
1425         if (pData)
1426             delete [] pData;
1427     }
1428 
1429     return result;
1430 }
1431 
1432 /*
1433  * Class:     sun_security_mscapi_RSAPublicKey
1434  * Method:    getPublicKeyBlob
1435  * Signature: (J)[B
1436  */
1437 JNIEXPORT jbyteArray JNICALL Java_sun_security_mscapi_RSAPublicKey_getPublicKeyBlob
1438     (JNIEnv *env, jclass clazz, jlong hCryptKey) {
1439 
1440     jbyteArray blob = NULL;
1441     DWORD dwBlobLen;
1442     BYTE* pbKeyBlob = NULL;
1443 
1444     __try
1445     {
1446 
1447         // Determine the size of the blob
1448         if (! ::CryptExportKey((HCRYPTKEY) hCryptKey, 0, PUBLICKEYBLOB, 0, NULL,
1449             &dwBlobLen)) {
1450 
1451             ThrowException(env, KEY_EXCEPTION, GetLastError());
1452             __leave;
1453         }
1454 
1455         pbKeyBlob = new (env) BYTE[dwBlobLen];
1456         if (pbKeyBlob == NULL) {
1457             __leave;
1458         }


1469         blob = env->NewByteArray(dwBlobLen);
1470 
1471         // Copy data from native buffer to Java buffer
1472         env->SetByteArrayRegion(blob, 0, dwBlobLen, (jbyte*) pbKeyBlob);
1473     }
1474     __finally
1475     {
1476         if (pbKeyBlob)
1477             delete [] pbKeyBlob;
1478     }
1479 
1480     return blob;
1481 }
1482 
1483 /*
1484  * Class:     sun_security_mscapi_RSAPublicKey
1485  * Method:    getExponent
1486  * Signature: ([B)[B
1487  */
1488 JNIEXPORT jbyteArray JNICALL Java_sun_security_mscapi_RSAPublicKey_getExponent
1489     (JNIEnv *env, jclass clazz, jbyteArray jKeyBlob) {
1490 
1491     jbyteArray exponent = NULL;
1492     jbyte*     exponentBytes = NULL;
1493     jbyte*     keyBlob = NULL;
1494 
1495     __try {
1496 
1497         jsize length = env->GetArrayLength(jKeyBlob);
1498         if ((keyBlob = env->GetByteArrayElements(jKeyBlob, 0)) == NULL) {
1499             __leave;
1500         }
1501 
1502         PUBLICKEYSTRUC* pPublicKeyStruc = (PUBLICKEYSTRUC *) keyBlob;
1503 
1504         // Check BLOB type
1505         if (pPublicKeyStruc->bType != PUBLICKEYBLOB) {
1506             ThrowException(env, KEY_EXCEPTION, NTE_BAD_TYPE);
1507             __leave;
1508         }
1509 


1525         env->SetByteArrayRegion(exponent, 0, len, exponentBytes);
1526     }
1527     __finally
1528     {
1529         if (keyBlob)
1530             env->ReleaseByteArrayElements(jKeyBlob, keyBlob, JNI_ABORT);
1531 
1532         if (exponentBytes)
1533             delete [] exponentBytes;
1534     }
1535 
1536     return exponent;
1537 }
1538 
1539 /*
1540  * Class:     sun_security_mscapi_RSAPublicKey
1541  * Method:    getModulus
1542  * Signature: ([B)[B
1543  */
1544 JNIEXPORT jbyteArray JNICALL Java_sun_security_mscapi_RSAPublicKey_getModulus
1545     (JNIEnv *env, jclass clazz, jbyteArray jKeyBlob) {
1546 
1547     jbyteArray modulus = NULL;
1548     jbyte*     modulusBytes = NULL;
1549     jbyte*     keyBlob = NULL;
1550 
1551     __try {
1552 
1553         jsize length = env->GetArrayLength(jKeyBlob);
1554         if ((keyBlob = env->GetByteArrayElements(jKeyBlob, 0)) == NULL) {
1555             __leave;
1556         }
1557 
1558         PUBLICKEYSTRUC* pPublicKeyStruc = (PUBLICKEYSTRUC *) keyBlob;
1559 
1560         // Check BLOB type
1561         if (pPublicKeyStruc->bType != PUBLICKEYBLOB) {
1562             ThrowException(env, KEY_EXCEPTION, NTE_BAD_TYPE);
1563             __leave;
1564         }
1565 


1798 
1799         jBlob = env->NewByteArray(jBlobLength);
1800         env->SetByteArrayRegion(jBlob, 0, jBlobLength, jBlobBytes);
1801 
1802     }
1803     __finally
1804     {
1805         if (jBlobBytes)
1806             delete [] jBlobBytes;
1807     }
1808 
1809     return jBlob;
1810 }
1811 
1812 /*
1813  * Class:     sun_security_mscapi_KeyStore
1814  * Method:    generatePrivateKeyBlob
1815  * Signature: (I[B[B[B[B[B[B[B[B)[B
1816  */
1817 JNIEXPORT jbyteArray JNICALL Java_sun_security_mscapi_KeyStore_generatePrivateKeyBlob
1818     (JNIEnv *env, jclass clazz,
1819         jint jKeyBitLength,
1820         jbyteArray jModulus,
1821         jbyteArray jPublicExponent,
1822         jbyteArray jPrivateExponent,
1823         jbyteArray jPrimeP,
1824         jbyteArray jPrimeQ,
1825         jbyteArray jExponentP,
1826         jbyteArray jExponentQ,
1827         jbyteArray jCrtCoefficient)
1828 {
1829     return generateKeyBlob(env, jKeyBitLength, jModulus, jPublicExponent,
1830         jPrivateExponent, jPrimeP, jPrimeQ, jExponentP, jExponentQ,
1831         jCrtCoefficient);
1832 }
1833 
1834 /*
1835  * Class:     sun_security_mscapi_RSASignature
1836  * Method:    generatePublicKeyBlob
1837  * Signature: (I[B[B)[B
1838  */
1839 JNIEXPORT jbyteArray JNICALL Java_sun_security_mscapi_RSASignature_generatePublicKeyBlob
1840     (JNIEnv *env, jclass clazz,
1841         jint jKeyBitLength,
1842         jbyteArray jModulus,
1843         jbyteArray jPublicExponent)
1844 {
1845     return generateKeyBlob(env, jKeyBitLength, jModulus, jPublicExponent,
1846         NULL, NULL, NULL, NULL, NULL, NULL);
1847 }
1848 
1849 /*
1850  * Class:     sun_security_mscapi_KeyStore
1851  * Method:    storePrivateKey
1852  * Signature: ([BLjava/lang/String;I)Lsun/security/mscapi/RSAPrivateKey;
1853  */
1854 JNIEXPORT jobject JNICALL Java_sun_security_mscapi_KeyStore_storePrivateKey
1855     (JNIEnv *env, jclass clazz, jbyteArray keyBlob, jstring keyContainerName,
1856      jint keySize)
1857 {
1858     HCRYPTPROV hCryptProv = NULL;
1859     HCRYPTKEY hKey = NULL;
1860     DWORD dwBlobLen;
1861     BYTE * pbKeyBlob = NULL;
1862     const char* pszKeyContainerName = NULL; // UUID
1863     jobject privateKey = NULL;
1864 
1865     __try
1866     {
1867         if ((pszKeyContainerName =
1868             env->GetStringUTFChars(keyContainerName, NULL)) == NULL) {
1869             __leave;
1870         }
1871         dwBlobLen = env->GetArrayLength(keyBlob);
1872         if ((pbKeyBlob = (BYTE *) env->GetByteArrayElements(keyBlob, 0))
1873             == NULL) {
1874             __leave;
1875         }




  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 
  26 //=--------------------------------------------------------------------------=
  27 // security.cpp    by Stanley Man-Kit Ho
  28 //=--------------------------------------------------------------------------=
  29 //
  30 
  31 #include <jni.h>
  32 #include "jni_util.h"
  33 #include <stdlib.h>
  34 #include <string.h>
  35 #include <windows.h>
  36 #include <BaseTsd.h>
  37 #include <wincrypt.h>
  38 #include <stdio.h>
  39 #include <memory>
  40 #include "sun_security_mscapi_Key.h"
  41 #include "sun_security_mscapi_KeyStore.h"
  42 #include "sun_security_mscapi_PRNG.h"
  43 #include "sun_security_mscapi_RSACipher.h"
  44 #include "sun_security_mscapi_RSAKeyPairGenerator.h"
  45 #include "sun_security_mscapi_RSAPublicKey.h"
  46 #include "sun_security_mscapi_RSASignature.h"
  47 
  48 #define OID_EKU_ANY         "2.5.29.37.0"
  49 
  50 #define CERTIFICATE_PARSING_EXCEPTION \
  51                             "java/security/cert/CertificateParsingException"
  52 #define INVALID_KEY_EXCEPTION \
  53                             "java/security/InvalidKeyException"
  54 #define KEY_EXCEPTION       "java/security/KeyException"
  55 #define KEYSTORE_EXCEPTION  "java/security/KeyStoreException"
  56 #define PROVIDER_EXCEPTION  "java/security/ProviderException"
  57 #define SIGNATURE_EXCEPTION "java/security/SignatureException"
  58 #define OUT_OF_MEMORY_ERROR "java/lang/OutOfMemoryError"
  59 
  60 extern "C" {
  61 
  62 /*
  63  * Declare library specific JNI_Onload entry if static build
  64  */
  65 DEF_STATIC_JNI_OnLoad
  66 


1302         if (pbCertEncoding)
1303             delete [] pbCertEncoding;
1304 
1305         if (pszNameString)
1306             delete [] pszNameString;
1307 
1308         if (pCertContext)
1309             ::CertFreeCertificateContext(pCertContext);
1310 
1311         if (bDeleteAttempted && pTBDCertContext)
1312             ::CertFreeCertificateContext(pTBDCertContext);
1313     }
1314 }
1315 
1316 /*
1317  * Class:     sun_security_mscapi_KeyStore
1318  * Method:    destroyKeyContainer
1319  * Signature: (Ljava/lang/String;)V
1320  */
1321 JNIEXPORT void JNICALL Java_sun_security_mscapi_KeyStore_destroyKeyContainer
1322   (JNIEnv *env, jobject clazz, jstring keyContainerName)
1323 {
1324     HCRYPTPROV hCryptProv = NULL;
1325     const char* pszKeyContainerName = NULL;
1326 
1327     __try
1328     {
1329         if ((pszKeyContainerName =
1330             env->GetStringUTFChars(keyContainerName, NULL)) == NULL) {
1331             __leave;
1332         }
1333 
1334         // Destroying the default key container is not permitted
1335         // (because it may contain more one keypair).
1336         if (pszKeyContainerName == NULL) {
1337 
1338             ThrowException(env, KEYSTORE_EXCEPTION, NTE_BAD_KEYSET_PARAM);
1339             __leave;
1340         }
1341 
1342         // Acquire a CSP context (to the key container).


1424         result = env->NewByteArray(dwBufLen);
1425 
1426         // Copy data from native buffer to Java buffer
1427         env->SetByteArrayRegion(result, 0, dwBufLen, (jbyte*) pData);
1428     }
1429     __finally
1430     {
1431         if (pData)
1432             delete [] pData;
1433     }
1434 
1435     return result;
1436 }
1437 
1438 /*
1439  * Class:     sun_security_mscapi_RSAPublicKey
1440  * Method:    getPublicKeyBlob
1441  * Signature: (J)[B
1442  */
1443 JNIEXPORT jbyteArray JNICALL Java_sun_security_mscapi_RSAPublicKey_getPublicKeyBlob
1444     (JNIEnv *env, jobject clazz, jlong hCryptKey) {
1445 
1446     jbyteArray blob = NULL;
1447     DWORD dwBlobLen;
1448     BYTE* pbKeyBlob = NULL;
1449 
1450     __try
1451     {
1452 
1453         // Determine the size of the blob
1454         if (! ::CryptExportKey((HCRYPTKEY) hCryptKey, 0, PUBLICKEYBLOB, 0, NULL,
1455             &dwBlobLen)) {
1456 
1457             ThrowException(env, KEY_EXCEPTION, GetLastError());
1458             __leave;
1459         }
1460 
1461         pbKeyBlob = new (env) BYTE[dwBlobLen];
1462         if (pbKeyBlob == NULL) {
1463             __leave;
1464         }


1475         blob = env->NewByteArray(dwBlobLen);
1476 
1477         // Copy data from native buffer to Java buffer
1478         env->SetByteArrayRegion(blob, 0, dwBlobLen, (jbyte*) pbKeyBlob);
1479     }
1480     __finally
1481     {
1482         if (pbKeyBlob)
1483             delete [] pbKeyBlob;
1484     }
1485 
1486     return blob;
1487 }
1488 
1489 /*
1490  * Class:     sun_security_mscapi_RSAPublicKey
1491  * Method:    getExponent
1492  * Signature: ([B)[B
1493  */
1494 JNIEXPORT jbyteArray JNICALL Java_sun_security_mscapi_RSAPublicKey_getExponent
1495     (JNIEnv *env, jobject clazz, jbyteArray jKeyBlob) {
1496 
1497     jbyteArray exponent = NULL;
1498     jbyte*     exponentBytes = NULL;
1499     jbyte*     keyBlob = NULL;
1500 
1501     __try {
1502 
1503         jsize length = env->GetArrayLength(jKeyBlob);
1504         if ((keyBlob = env->GetByteArrayElements(jKeyBlob, 0)) == NULL) {
1505             __leave;
1506         }
1507 
1508         PUBLICKEYSTRUC* pPublicKeyStruc = (PUBLICKEYSTRUC *) keyBlob;
1509 
1510         // Check BLOB type
1511         if (pPublicKeyStruc->bType != PUBLICKEYBLOB) {
1512             ThrowException(env, KEY_EXCEPTION, NTE_BAD_TYPE);
1513             __leave;
1514         }
1515 


1531         env->SetByteArrayRegion(exponent, 0, len, exponentBytes);
1532     }
1533     __finally
1534     {
1535         if (keyBlob)
1536             env->ReleaseByteArrayElements(jKeyBlob, keyBlob, JNI_ABORT);
1537 
1538         if (exponentBytes)
1539             delete [] exponentBytes;
1540     }
1541 
1542     return exponent;
1543 }
1544 
1545 /*
1546  * Class:     sun_security_mscapi_RSAPublicKey
1547  * Method:    getModulus
1548  * Signature: ([B)[B
1549  */
1550 JNIEXPORT jbyteArray JNICALL Java_sun_security_mscapi_RSAPublicKey_getModulus
1551     (JNIEnv *env, jobject clazz, jbyteArray jKeyBlob) {
1552 
1553     jbyteArray modulus = NULL;
1554     jbyte*     modulusBytes = NULL;
1555     jbyte*     keyBlob = NULL;
1556 
1557     __try {
1558 
1559         jsize length = env->GetArrayLength(jKeyBlob);
1560         if ((keyBlob = env->GetByteArrayElements(jKeyBlob, 0)) == NULL) {
1561             __leave;
1562         }
1563 
1564         PUBLICKEYSTRUC* pPublicKeyStruc = (PUBLICKEYSTRUC *) keyBlob;
1565 
1566         // Check BLOB type
1567         if (pPublicKeyStruc->bType != PUBLICKEYBLOB) {
1568             ThrowException(env, KEY_EXCEPTION, NTE_BAD_TYPE);
1569             __leave;
1570         }
1571 


1804 
1805         jBlob = env->NewByteArray(jBlobLength);
1806         env->SetByteArrayRegion(jBlob, 0, jBlobLength, jBlobBytes);
1807 
1808     }
1809     __finally
1810     {
1811         if (jBlobBytes)
1812             delete [] jBlobBytes;
1813     }
1814 
1815     return jBlob;
1816 }
1817 
1818 /*
1819  * Class:     sun_security_mscapi_KeyStore
1820  * Method:    generatePrivateKeyBlob
1821  * Signature: (I[B[B[B[B[B[B[B[B)[B
1822  */
1823 JNIEXPORT jbyteArray JNICALL Java_sun_security_mscapi_KeyStore_generatePrivateKeyBlob
1824     (JNIEnv *env, jobject clazz,
1825         jint jKeyBitLength,
1826         jbyteArray jModulus,
1827         jbyteArray jPublicExponent,
1828         jbyteArray jPrivateExponent,
1829         jbyteArray jPrimeP,
1830         jbyteArray jPrimeQ,
1831         jbyteArray jExponentP,
1832         jbyteArray jExponentQ,
1833         jbyteArray jCrtCoefficient)
1834 {
1835     return generateKeyBlob(env, jKeyBitLength, jModulus, jPublicExponent,
1836         jPrivateExponent, jPrimeP, jPrimeQ, jExponentP, jExponentQ,
1837         jCrtCoefficient);
1838 }
1839 
1840 /*
1841  * Class:     sun_security_mscapi_RSASignature
1842  * Method:    generatePublicKeyBlob
1843  * Signature: (I[B[B)[B
1844  */
1845 JNIEXPORT jbyteArray JNICALL Java_sun_security_mscapi_RSASignature_generatePublicKeyBlob
1846     (JNIEnv *env, jclass clazz,
1847         jint jKeyBitLength,
1848         jbyteArray jModulus,
1849         jbyteArray jPublicExponent)
1850 {
1851     return generateKeyBlob(env, jKeyBitLength, jModulus, jPublicExponent,
1852         NULL, NULL, NULL, NULL, NULL, NULL);
1853 }
1854 
1855 /*
1856  * Class:     sun_security_mscapi_KeyStore
1857  * Method:    storePrivateKey
1858  * Signature: ([BLjava/lang/String;I)Lsun/security/mscapi/RSAPrivateKey;
1859  */
1860 JNIEXPORT jobject JNICALL Java_sun_security_mscapi_KeyStore_storePrivateKey
1861     (JNIEnv *env, jobject clazz, jbyteArray keyBlob, jstring keyContainerName,
1862      jint keySize)
1863 {
1864     HCRYPTPROV hCryptProv = NULL;
1865     HCRYPTKEY hKey = NULL;
1866     DWORD dwBlobLen;
1867     BYTE * pbKeyBlob = NULL;
1868     const char* pszKeyContainerName = NULL; // UUID
1869     jobject privateKey = NULL;
1870 
1871     __try
1872     {
1873         if ((pszKeyContainerName =
1874             env->GetStringUTFChars(keyContainerName, NULL)) == NULL) {
1875             __leave;
1876         }
1877         dwBlobLen = env->GetArrayLength(keyBlob);
1878         if ((pbKeyBlob = (BYTE *) env->GetByteArrayElements(keyBlob, 0))
1879             == NULL) {
1880             __leave;
1881         }


< prev index next >