1818#include <gmssl/zuc.h>
1919#include <gmssl/mem.h>
2020#include <gmssl/rand.h>
21- #include <gmssl/aead.h>
2221#include <gmssl/x509.h>
2322#include <gmssl/error.h>
2423#include <gmssl/pbkdf2.h>
2524#include <gmssl/version.h>
2625#include "gmssljni.h"
2726
27+ #define SM2_SIGNATURE_CTX_SIZE (sizeof(SM2_SIGN_CTX) > sizeof(SM2_VERIFY_CTX) ? sizeof(SM2_SIGN_CTX) : sizeof(SM2_VERIFY_CTX))
28+
2829
2930static int check_buf (const jbyte * buf , jint bufsiz , jint offset , jint outlen )
3031{
@@ -390,7 +391,7 @@ JNIEXPORT jbyteArray JNICALL Java_org_gmssl_GmSSLJNI_sm3_1pbkdf2(
390391 goto end ;
391392 }
392393
393- if (pbkdf2_hmac_sm3_genkey (pass_str , strlen (pass_str ),
394+ if (sm3_pbkdf2 (pass_str , strlen (pass_str ),
394395 (const uint8_t * )saltbuf , saltlen , iter , keylen , keybuf ) != 1 ) {
395396 error_print ();
396397 goto end ;
@@ -1051,7 +1052,7 @@ JNIEXPORT jint JNICALL Java_org_gmssl_GmSSLJNI_sm4_1ctr_1decrypt_1init(
10511052 error_print ();
10521053 goto end ;
10531054 }
1054- if (sm4_ctr_decrypt_init ((SM4_CTR_CTX * )sm4_ctr_ctx , (uint8_t * )keybuf , (uint8_t * )ivbuf ) != 1 ) {
1055+ if (sm4_ctr_encrypt_init ((SM4_CTR_CTX * )sm4_ctr_ctx , (uint8_t * )keybuf , (uint8_t * )ivbuf ) != 1 ) {
10551056 error_print ();
10561057 goto end ;
10571058 }
@@ -1101,7 +1102,7 @@ JNIEXPORT jint JNICALL Java_org_gmssl_GmSSLJNI_sm4_1ctr_1decrypt_1update(
11011102 error_print ();
11021103 goto end ;
11031104 }
1104- if (sm4_ctr_decrypt_update ((SM4_CTR_CTX * )sm4_ctr_ctx , (uint8_t * )inbuf + in_offset , (size_t )inlen ,
1105+ if (sm4_ctr_encrypt_update ((SM4_CTR_CTX * )sm4_ctr_ctx , (uint8_t * )inbuf + in_offset , (size_t )inlen ,
11051106 (uint8_t * )outbuf + out_offset , & outlen ) != 1 ) {
11061107 error_print ();
11071108 goto end ;
@@ -1140,7 +1141,7 @@ JNIEXPORT jint JNICALL Java_org_gmssl_GmSSLJNI_sm4_1ctr_1decrypt_1finish(
11401141 error_print ();
11411142 goto end ;
11421143 }
1143- if (sm4_ctr_decrypt_finish ((SM4_CTR_CTX * )sm4_ctr_ctx ,
1144+ if (sm4_ctr_encrypt_finish ((SM4_CTR_CTX * )sm4_ctr_ctx ,
11441145 (uint8_t * )outbuf + offset , & outlen ) != 1 ) {
11451146 error_print ();
11461147 goto end ;
@@ -2188,11 +2189,11 @@ JNIEXPORT jlong JNICALL Java_org_gmssl_GmSSLJNI_sm2_1sign_1ctx_1new(
21882189{
21892190 jlong sm2_sign_ctx ;
21902191
2191- if (!(sm2_sign_ctx = (jlong )malloc (sizeof ( SM2_SIGN_CTX ) ))) {
2192+ if (!(sm2_sign_ctx = (jlong )malloc (SM2_SIGNATURE_CTX_SIZE ))) {
21922193 error_print ();
21932194 return 0 ;
21942195 }
2195- memset ((SM2_SIGN_CTX * )sm2_sign_ctx , 0 , sizeof ( SM2_SIGN_CTX ) );
2196+ memset ((void * )sm2_sign_ctx , 0 , SM2_SIGNATURE_CTX_SIZE );
21962197 return sm2_sign_ctx ;
21972198}
21982199
@@ -2206,7 +2207,7 @@ JNIEXPORT void JNICALL Java_org_gmssl_GmSSLJNI_sm2_1sign_1ctx_1free(
22062207 jlong sm2_sign_ctx )
22072208{
22082209 if (sm2_sign_ctx ) {
2209- gmssl_secure_clear ((SM2_SIGN_CTX * )sm2_sign_ctx , sizeof ( SM2_SIGN_CTX ) );
2210+ gmssl_secure_clear ((void * )sm2_sign_ctx , SM2_SIGNATURE_CTX_SIZE );
22102211 free ((SM2_SIGN_CTX * )sm2_sign_ctx );
22112212 }
22122213}
@@ -2333,7 +2334,7 @@ JNIEXPORT jint JNICALL Java_org_gmssl_GmSSLJNI_sm2_1verify_1init(
23332334 error_print ();
23342335 return -1 ;
23352336 }
2336- if (sm2_verify_init ((SM2_SIGN_CTX * )sm2_sign_ctx , (SM2_KEY * )sm2_pub , id_str , strlen (id_str )) != 1 ) {
2337+ if (sm2_verify_init ((SM2_VERIFY_CTX * )sm2_sign_ctx , (SM2_KEY * )sm2_pub , id_str , strlen (id_str )) != 1 ) {
23372338 error_print ();
23382339 goto end ;
23392340 }
@@ -2368,7 +2369,7 @@ JNIEXPORT jint JNICALL Java_org_gmssl_GmSSLJNI_sm2_1verify_1update(
23682369 error_print ();
23692370 goto end ;
23702371 }
2371- if (sm2_verify_update ((SM2_SIGN_CTX * )sm2_sign_ctx , (uint8_t * )buf + offset , (size_t )length ) != 1 ) {
2372+ if (sm2_verify_update ((SM2_VERIFY_CTX * )sm2_sign_ctx , (uint8_t * )buf + offset , (size_t )length ) != 1 ) {
23722373 error_print ();
23732374 goto end ;
23742375 }
@@ -2400,7 +2401,7 @@ JNIEXPORT jint JNICALL Java_org_gmssl_GmSSLJNI_sm2_1verify_1finish(
24002401 return -1 ;
24012402 }
24022403 siglen = (* env )-> GetArrayLength (env , sig );
2403- if ((ret = sm2_verify_finish ((SM2_SIGN_CTX * )sm2_sign_ctx , (uint8_t * )sigbuf , (size_t )siglen )) < 0 ) {
2404+ if ((ret = sm2_verify_finish ((SM2_VERIFY_CTX * )sm2_sign_ctx , (uint8_t * )sigbuf , (size_t )siglen )) < 0 ) {
24042405 error_print ();
24052406 goto end ;
24062407 }
@@ -3815,6 +3816,7 @@ JNIEXPORT jlong JNICALL Java_org_gmssl_GmSSLJNI_cert_1get_1subject_1public_1key(
38153816 jbyte * certbuf ;
38163817 jsize certlen ;
38173818 SM2_KEY * sm2_pub = NULL ;
3819+ X509_KEY x509_key ;
38183820
38193821 if (!(certbuf = (* env )-> GetByteArrayElements (env , cert , NULL ))) {
38203822 error_print ();
@@ -3826,10 +3828,18 @@ JNIEXPORT jlong JNICALL Java_org_gmssl_GmSSLJNI_cert_1get_1subject_1public_1key(
38263828 goto end ;
38273829 }
38283830 memset (sm2_pub , 0 , sizeof (SM2_KEY ));
3829- if (x509_cert_get_subject_public_key ((uint8_t * )certbuf , certlen , sm2_pub ) != 1 ) {
3831+ memset (& x509_key , 0 , sizeof (x509_key ));
3832+ if (x509_cert_get_subject_public_key ((uint8_t * )certbuf , certlen , & x509_key ) != 1 ) {
38303833 error_print ();
38313834 goto end ;
38323835 }
3836+ if (x509_key .algor != OID_ec_public_key || x509_key .algor_param != OID_sm2 ) {
3837+ error_print ();
3838+ x509_key_cleanup (& x509_key );
3839+ goto end ;
3840+ }
3841+ memcpy (sm2_pub , & x509_key .u .sm2_key , sizeof (SM2_KEY ));
3842+ x509_key_cleanup (& x509_key );
38333843 ret = (jlong )sm2_pub ;
38343844 sm2_pub = NULL ;
38353845end :
@@ -3879,4 +3889,3 @@ JNIEXPORT jint JNICALL Java_org_gmssl_GmSSLJNI_cert_1verify_1by_1ca_1cert(
38793889 if (id_str ) (* env )-> ReleaseStringUTFChars (env , ca_sm2_id , id_str );
38803890 return ret ;
38813891}
3882-
0 commit comments