@@ -4861,6 +4861,20 @@ static jobjectArray NativeCrypto_get_X509_GENERAL_NAME_stack(JNIEnv* env, jclass
48614861 return joa.release ();
48624862}
48634863
4864+ /*
4865+ * Converts an ASN1_TIME to epoch time in milliseconds.
4866+ */
4867+ static jlong ASN1_TIME_convert_to_posix (JNIEnv* env, const ASN1_TIME* time) {
4868+ int64_t retval;
4869+ if (!ASN1_TIME_to_posix (time, &retval)) {
4870+ JNI_TRACE (" ASN1_TIME_convert_to_posix(%p) => Invalid date value" , time);
4871+ conscrypt::jniutil::throwParsingException (env, " Invalid date value" );
4872+ return 0 ;
4873+ }
4874+ // ASN1_TIME_to_posix can only return years from 0000 to 9999, so this won't overflow.
4875+ return static_cast <jlong>(retval * 1000 );
4876+ }
4877+
48644878static jlong NativeCrypto_X509_get_notBefore (JNIEnv* env, jclass, jlong x509Ref,
48654879 CONSCRYPT_UNUSED jobject holder) {
48664880 CHECK_ERROR_QUEUE_ON_RETURN;
@@ -4875,7 +4889,7 @@ static jlong NativeCrypto_X509_get_notBefore(JNIEnv* env, jclass, jlong x509Ref,
48754889
48764890 ASN1_TIME* notBefore = X509_get_notBefore (x509);
48774891 JNI_TRACE (" X509_get_notBefore(%p) => %p" , x509, notBefore);
4878- return reinterpret_cast < uintptr_t >( notBefore);
4892+ return ASN1_TIME_convert_to_posix (env, notBefore);
48794893}
48804894
48814895static jlong NativeCrypto_X509_get_notAfter (JNIEnv* env, jclass, jlong x509Ref,
@@ -4892,7 +4906,7 @@ static jlong NativeCrypto_X509_get_notAfter(JNIEnv* env, jclass, jlong x509Ref,
48924906
48934907 ASN1_TIME* notAfter = X509_get_notAfter (x509);
48944908 JNI_TRACE (" X509_get_notAfter(%p) => %p" , x509, notAfter);
4895- return reinterpret_cast < uintptr_t >( notAfter);
4909+ return ASN1_TIME_convert_to_posix (env, notAfter);
48964910}
48974911
48984912// NOLINTNEXTLINE(runtime/int)
@@ -5528,7 +5542,7 @@ static jlong NativeCrypto_get_X509_REVOKED_revocationDate(JNIEnv* env, jclass,
55285542
55295543 JNI_TRACE (" get_X509_REVOKED_revocationDate(%p) => %p" , revoked,
55305544 X509_REVOKED_get0_revocationDate (revoked));
5531- return reinterpret_cast < uintptr_t >( X509_REVOKED_get0_revocationDate (revoked));
5545+ return ASN1_TIME_convert_to_posix (env, X509_REVOKED_get0_revocationDate (revoked));
55325546}
55335547
55345548#ifdef __GNUC__
@@ -5622,7 +5636,7 @@ static jlong NativeCrypto_X509_CRL_get_lastUpdate(JNIEnv* env, jclass, jlong x50
56225636
56235637 ASN1_TIME* lastUpdate = X509_CRL_get_lastUpdate (crl);
56245638 JNI_TRACE (" X509_CRL_get_lastUpdate(%p) => %p" , crl, lastUpdate);
5625- return reinterpret_cast < uintptr_t >( lastUpdate);
5639+ return ASN1_TIME_convert_to_posix (env, lastUpdate);
56265640}
56275641
56285642static jlong NativeCrypto_X509_CRL_get_nextUpdate (JNIEnv* env, jclass, jlong x509CrlRef,
@@ -5639,7 +5653,7 @@ static jlong NativeCrypto_X509_CRL_get_nextUpdate(JNIEnv* env, jclass, jlong x50
56395653
56405654 ASN1_TIME* nextUpdate = X509_CRL_get_nextUpdate (crl);
56415655 JNI_TRACE (" X509_CRL_get_nextUpdate(%p) => %p" , crl, nextUpdate);
5642- return reinterpret_cast < uintptr_t >( nextUpdate);
5656+ return ASN1_TIME_convert_to_posix (env, nextUpdate);
56435657}
56445658
56455659static jbyteArray NativeCrypto_i2d_X509_REVOKED (JNIEnv* env, jclass, jlong x509RevokedRef) {
@@ -5663,63 +5677,6 @@ static jint NativeCrypto_X509_supported_extension(JNIEnv* env, jclass, jlong x50
56635677 return X509_supported_extension (ext);
56645678}
56655679
5666- static inline bool decimal_to_integer (const char * data, size_t len, int * out) {
5667- int ret = 0 ;
5668- for (size_t i = 0 ; i < len; i++) {
5669- ret *= 10 ;
5670- if (data[i] < ' 0' || data[i] > ' 9' ) {
5671- return false ;
5672- }
5673- ret += data[i] - ' 0' ;
5674- }
5675- *out = ret;
5676- return true ;
5677- }
5678-
5679- static void NativeCrypto_ASN1_TIME_to_Calendar (JNIEnv* env, jclass, jlong asn1TimeRef,
5680- jobject calendar) {
5681- CHECK_ERROR_QUEUE_ON_RETURN;
5682- ASN1_TIME* asn1Time = reinterpret_cast <ASN1_TIME*>(static_cast <uintptr_t >(asn1TimeRef));
5683- JNI_TRACE (" ASN1_TIME_to_Calendar(%p, %p)" , asn1Time, calendar);
5684-
5685- if (asn1Time == nullptr ) {
5686- conscrypt::jniutil::throwNullPointerException (env, " asn1Time == null" );
5687- return ;
5688- }
5689-
5690- if (!ASN1_TIME_check (asn1Time)) {
5691- conscrypt::jniutil::throwParsingException (env, " Invalid date format" );
5692- return ;
5693- }
5694-
5695- bssl::UniquePtr<ASN1_GENERALIZEDTIME> gen (ASN1_TIME_to_generalizedtime (asn1Time, nullptr ));
5696- if (gen.get () == nullptr ) {
5697- conscrypt::jniutil::throwParsingException (env,
5698- " ASN1_TIME_to_generalizedtime returned null" );
5699- return ;
5700- }
5701-
5702- if (ASN1_STRING_length (gen.get ()) < 14 || ASN1_STRING_get0_data (gen.get ()) == nullptr ) {
5703- conscrypt::jniutil::throwNullPointerException (env, " gen->length < 14 || gen->data == null" );
5704- return ;
5705- }
5706-
5707- int year, mon, mday, hour, min, sec;
5708- const char * data = reinterpret_cast <const char *>(ASN1_STRING_get0_data (gen.get ()));
5709- if (!decimal_to_integer (data, 4 , &year) ||
5710- !decimal_to_integer (data + 4 , 2 , &mon) ||
5711- !decimal_to_integer (data + 6 , 2 , &mday) ||
5712- !decimal_to_integer (data + 8 , 2 , &hour) ||
5713- !decimal_to_integer (data + 10 , 2 , &min) ||
5714- !decimal_to_integer (data + 12 , 2 , &sec)) {
5715- conscrypt::jniutil::throwParsingException (env, " Invalid date format" );
5716- return ;
5717- }
5718-
5719- env->CallVoidMethod (calendar, conscrypt::jniutil::calendar_setMethod, year, mon - 1 , mday, hour,
5720- min, sec);
5721- }
5722-
57235680// A CbsHandle is a structure used to manage resources allocated by asn1_read-*
57245681// functions so that they can be freed properly when finished. This struct owns
57255682// all objects pointed to by its members.
@@ -11218,7 +11175,6 @@ static JNINativeMethod sNativeCryptoMethods[] = {
1121811175 CONSCRYPT_NATIVE_METHOD(X509_REVOKED_dup, " (J)J" ),
1121911176 CONSCRYPT_NATIVE_METHOD(i2d_X509_REVOKED, " (J)[B" ),
1122011177 CONSCRYPT_NATIVE_METHOD(X509_supported_extension, " (J)I" ),
11221- CONSCRYPT_NATIVE_METHOD(ASN1_TIME_to_Calendar, " (JLjava/util/Calendar;)V" ),
1122211178 CONSCRYPT_NATIVE_METHOD(asn1_read_init, " ([B)J" ),
1122311179 CONSCRYPT_NATIVE_METHOD(asn1_read_sequence, " (J)J" ),
1122411180 CONSCRYPT_NATIVE_METHOD(asn1_read_next_tag_is, " (JI)Z" ),
0 commit comments