@@ -4904,20 +4904,6 @@ static jobjectArray NativeCrypto_get_X509_GENERAL_NAME_stack(JNIEnv* env, jclass
4904
4904
return joa.release();
4905
4905
}
4906
4906
4907
- /*
4908
- * Converts an ASN1_TIME to epoch time in milliseconds.
4909
- */
4910
- static jlong ASN1_TIME_convert_to_posix(JNIEnv* env, const ASN1_TIME* time) {
4911
- int64_t retval;
4912
- if (!ASN1_TIME_to_posix(time, &retval)) {
4913
- JNI_TRACE("ASN1_TIME_convert_to_posix(%p) => Invalid date value", time);
4914
- conscrypt::jniutil::throwParsingException(env, "Invalid date value");
4915
- return 0;
4916
- }
4917
- // ASN1_TIME_to_posix can only return years from 0000 to 9999, so this won't overflow.
4918
- return static_cast<jlong>(retval * 1000);
4919
- }
4920
-
4921
4907
static jlong NativeCrypto_X509_get_notBefore(JNIEnv* env, jclass, jlong x509Ref,
4922
4908
CONSCRYPT_UNUSED jobject holder) {
4923
4909
CHECK_ERROR_QUEUE_ON_RETURN;
@@ -4932,7 +4918,7 @@ static jlong NativeCrypto_X509_get_notBefore(JNIEnv* env, jclass, jlong x509Ref,
4932
4918
4933
4919
ASN1_TIME* notBefore = X509_get_notBefore(x509);
4934
4920
JNI_TRACE("X509_get_notBefore(%p) => %p", x509, notBefore);
4935
- return ASN1_TIME_convert_to_posix(env, notBefore);
4921
+ return reinterpret_cast<uintptr_t>( notBefore);
4936
4922
}
4937
4923
4938
4924
static jlong NativeCrypto_X509_get_notAfter(JNIEnv* env, jclass, jlong x509Ref,
@@ -4949,7 +4935,7 @@ static jlong NativeCrypto_X509_get_notAfter(JNIEnv* env, jclass, jlong x509Ref,
4949
4935
4950
4936
ASN1_TIME* notAfter = X509_get_notAfter(x509);
4951
4937
JNI_TRACE("X509_get_notAfter(%p) => %p", x509, notAfter);
4952
- return ASN1_TIME_convert_to_posix(env, notAfter);
4938
+ return reinterpret_cast<uintptr_t>( notAfter);
4953
4939
}
4954
4940
4955
4941
// NOLINTNEXTLINE(runtime/int)
@@ -5585,7 +5571,7 @@ static jlong NativeCrypto_get_X509_REVOKED_revocationDate(JNIEnv* env, jclass,
5585
5571
5586
5572
JNI_TRACE("get_X509_REVOKED_revocationDate(%p) => %p", revoked,
5587
5573
X509_REVOKED_get0_revocationDate(revoked));
5588
- return ASN1_TIME_convert_to_posix(env, X509_REVOKED_get0_revocationDate(revoked));
5574
+ return reinterpret_cast<uintptr_t>( X509_REVOKED_get0_revocationDate(revoked));
5589
5575
}
5590
5576
5591
5577
#ifdef __GNUC__
@@ -5679,7 +5665,7 @@ static jlong NativeCrypto_X509_CRL_get_lastUpdate(JNIEnv* env, jclass, jlong x50
5679
5665
5680
5666
ASN1_TIME* lastUpdate = X509_CRL_get_lastUpdate(crl);
5681
5667
JNI_TRACE("X509_CRL_get_lastUpdate(%p) => %p", crl, lastUpdate);
5682
- return ASN1_TIME_convert_to_posix(env, lastUpdate);
5668
+ return reinterpret_cast<uintptr_t>( lastUpdate);
5683
5669
}
5684
5670
5685
5671
static jlong NativeCrypto_X509_CRL_get_nextUpdate(JNIEnv* env, jclass, jlong x509CrlRef,
@@ -5696,7 +5682,7 @@ static jlong NativeCrypto_X509_CRL_get_nextUpdate(JNIEnv* env, jclass, jlong x50
5696
5682
5697
5683
ASN1_TIME* nextUpdate = X509_CRL_get_nextUpdate(crl);
5698
5684
JNI_TRACE("X509_CRL_get_nextUpdate(%p) => %p", crl, nextUpdate);
5699
- return ASN1_TIME_convert_to_posix(env, nextUpdate);
5685
+ return reinterpret_cast<uintptr_t>( nextUpdate);
5700
5686
}
5701
5687
5702
5688
static jbyteArray NativeCrypto_i2d_X509_REVOKED(JNIEnv* env, jclass, jlong x509RevokedRef) {
@@ -5720,6 +5706,63 @@ static jint NativeCrypto_X509_supported_extension(JNIEnv* env, jclass, jlong x50
5720
5706
return X509_supported_extension(ext);
5721
5707
}
5722
5708
5709
+ static inline bool decimal_to_integer(const char* data, size_t len, int* out) {
5710
+ int ret = 0;
5711
+ for (size_t i = 0; i < len; i++) {
5712
+ ret *= 10;
5713
+ if (data[i] < '0' || data[i] > '9') {
5714
+ return false;
5715
+ }
5716
+ ret += data[i] - '0';
5717
+ }
5718
+ *out = ret;
5719
+ return true;
5720
+ }
5721
+
5722
+ static void NativeCrypto_ASN1_TIME_to_Calendar(JNIEnv* env, jclass, jlong asn1TimeRef,
5723
+ jobject calendar) {
5724
+ CHECK_ERROR_QUEUE_ON_RETURN;
5725
+ ASN1_TIME* asn1Time = reinterpret_cast<ASN1_TIME*>(static_cast<uintptr_t>(asn1TimeRef));
5726
+ JNI_TRACE("ASN1_TIME_to_Calendar(%p, %p)", asn1Time, calendar);
5727
+
5728
+ if (asn1Time == nullptr) {
5729
+ conscrypt::jniutil::throwNullPointerException(env, "asn1Time == null");
5730
+ return;
5731
+ }
5732
+
5733
+ if (!ASN1_TIME_check(asn1Time)) {
5734
+ conscrypt::jniutil::throwParsingException(env, "Invalid date format");
5735
+ return;
5736
+ }
5737
+
5738
+ bssl::UniquePtr<ASN1_GENERALIZEDTIME> gen(ASN1_TIME_to_generalizedtime(asn1Time, nullptr));
5739
+ if (gen.get() == nullptr) {
5740
+ conscrypt::jniutil::throwParsingException(env,
5741
+ "ASN1_TIME_to_generalizedtime returned null");
5742
+ return;
5743
+ }
5744
+
5745
+ if (ASN1_STRING_length(gen.get()) < 14 || ASN1_STRING_get0_data(gen.get()) == nullptr) {
5746
+ conscrypt::jniutil::throwNullPointerException(env, "gen->length < 14 || gen->data == null");
5747
+ return;
5748
+ }
5749
+
5750
+ int year, mon, mday, hour, min, sec;
5751
+ const char* data = reinterpret_cast<const char*>(ASN1_STRING_get0_data(gen.get()));
5752
+ if (!decimal_to_integer(data, 4, &year) ||
5753
+ !decimal_to_integer(data + 4, 2, &mon) ||
5754
+ !decimal_to_integer(data + 6, 2, &mday) ||
5755
+ !decimal_to_integer(data + 8, 2, &hour) ||
5756
+ !decimal_to_integer(data + 10, 2, &min) ||
5757
+ !decimal_to_integer(data + 12, 2, &sec)) {
5758
+ conscrypt::jniutil::throwParsingException(env, "Invalid date format");
5759
+ return;
5760
+ }
5761
+
5762
+ env->CallVoidMethod(calendar, conscrypt::jniutil::calendar_setMethod, year, mon - 1, mday, hour,
5763
+ min, sec);
5764
+ }
5765
+
5723
5766
// A CbsHandle is a structure used to manage resources allocated by asn1_read-*
5724
5767
// functions so that they can be freed properly when finished. This struct owns
5725
5768
// all objects pointed to by its members.
@@ -11219,6 +11262,7 @@ static JNINativeMethod sNativeCryptoMethods[] = {
11219
11262
CONSCRYPT_NATIVE_METHOD(X509_REVOKED_dup, "(J)J"),
11220
11263
CONSCRYPT_NATIVE_METHOD(i2d_X509_REVOKED, "(J)[B"),
11221
11264
CONSCRYPT_NATIVE_METHOD(X509_supported_extension, "(J)I"),
11265
+ CONSCRYPT_NATIVE_METHOD(ASN1_TIME_to_Calendar, "(JLjava/util/Calendar;)V"),
11222
11266
CONSCRYPT_NATIVE_METHOD(asn1_read_init, "([B)J"),
11223
11267
CONSCRYPT_NATIVE_METHOD(asn1_read_sequence, "(J)J"),
11224
11268
CONSCRYPT_NATIVE_METHOD(asn1_read_next_tag_is, "(JI)Z"),
0 commit comments