bouncycastle uses the Date class, based off of UTC. Force all GregorianCalendars to use UTC, so get and set of key dates works as expected

This commit is contained in:
Ashley Hughes 2014-01-30 11:35:55 +00:00
parent 358ab7d7e4
commit 4119757699
4 changed files with 15 additions and 27 deletions

View File

@ -29,25 +29,6 @@ import android.os.Bundle;
public class OtherHelper { public class OtherHelper {
/**
* Return the number if days between two dates
*
* @param first
* @param second
* @return number of days
*/
public static long getNumDaysBetween(GregorianCalendar first, GregorianCalendar second) {
GregorianCalendar tmp = new GregorianCalendar();
tmp.setTime(first.getTime());
long numDays = (second.getTimeInMillis() - first.getTimeInMillis()) / 1000 / 86400;
tmp.add(Calendar.DAY_OF_MONTH, (int) numDays);
while (tmp.before(second)) {
tmp.add(Calendar.DAY_OF_MONTH, 1);
++numDays;
}
return numDays;
}
/** /**
* Logs bundle content to debug for inspecting the content * Logs bundle content to debug for inspecting the content
* *

View File

@ -28,6 +28,7 @@ import java.security.SignatureException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Date; import java.util.Date;
import java.util.GregorianCalendar; import java.util.GregorianCalendar;
import java.util.TimeZone;
import org.spongycastle.bcpg.CompressionAlgorithmTags; import org.spongycastle.bcpg.CompressionAlgorithmTags;
import org.spongycastle.bcpg.HashAlgorithmTags; import org.spongycastle.bcpg.HashAlgorithmTags;
@ -323,7 +324,7 @@ public class PgpKeyOperation {
hashedPacketsGen.setPreferredCompressionAlgorithms(true, PREFERRED_COMPRESSION_ALGORITHMS); hashedPacketsGen.setPreferredCompressionAlgorithms(true, PREFERRED_COMPRESSION_ALGORITHMS);
if (keysExpiryDates.get(0) != null) { if (keysExpiryDates.get(0) != null) {
GregorianCalendar creationDate = new GregorianCalendar(); GregorianCalendar creationDate = new GregorianCalendar(TimeZone.getTimeZone("UTC"));
creationDate.setTime(masterPublicKey.getCreationTime()); creationDate.setTime(masterPublicKey.getCreationTime());
GregorianCalendar expiryDate = keysExpiryDates.get(0); GregorianCalendar expiryDate = keysExpiryDates.get(0);
//note that the below, (a/c) - (b/c) is *not* the same as (a - b) /c //note that the below, (a/c) - (b/c) is *not* the same as (a - b) /c
@ -402,7 +403,7 @@ public class PgpKeyOperation {
hashedPacketsGen.setKeyFlags(false, keyFlags); hashedPacketsGen.setKeyFlags(false, keyFlags);
if (keysExpiryDates.get(i) != null) { if (keysExpiryDates.get(i) != null) {
GregorianCalendar creationDate = new GregorianCalendar(); GregorianCalendar creationDate = new GregorianCalendar(TimeZone.getTimeZone("UTC"));
creationDate.setTime(subPublicKey.getCreationTime()); creationDate.setTime(subPublicKey.getCreationTime());
GregorianCalendar expiryDate = keysExpiryDates.get(i); GregorianCalendar expiryDate = keysExpiryDates.get(i);
//note that the below, (a/c) - (b/c) is *not* the same as (a - b) /c //note that the below, (a/c) - (b/c) is *not* the same as (a - b) /c

View File

@ -20,6 +20,7 @@ import java.text.DateFormat;
import java.util.Calendar; import java.util.Calendar;
import java.util.Date; import java.util.Date;
import java.util.GregorianCalendar; import java.util.GregorianCalendar;
import java.util.TimeZone;
import java.util.Vector; import java.util.Vector;
import org.spongycastle.openpgp.PGPPublicKey; import org.spongycastle.openpgp.PGPPublicKey;
@ -64,7 +65,8 @@ public class KeyEditor extends LinearLayout implements Editor, OnClickListener {
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) { public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
// Note: Ignore results after the first one - android sends multiples. // Note: Ignore results after the first one - android sends multiples.
if (mDatePickerResultCount++ == 0) { if (mDatePickerResultCount++ == 0) {
GregorianCalendar date = new GregorianCalendar(year, monthOfYear, dayOfMonth); GregorianCalendar date = new GregorianCalendar(TimeZone.getTimeZone("UTC"));
date.set(year, monthOfYear, dayOfMonth);
setExpiryDate(date); setExpiryDate(date);
} }
} }
@ -109,7 +111,7 @@ public class KeyEditor extends LinearLayout implements Editor, OnClickListener {
public void onClick(View v) { public void onClick(View v) {
GregorianCalendar date = mExpiryDate; GregorianCalendar date = mExpiryDate;
if (date == null) { if (date == null) {
date = new GregorianCalendar(); date = new GregorianCalendar(TimeZone.getTimeZone("UTC"));
} }
DatePickerDialog dialog = new DatePickerDialog(getContext(), DatePickerDialog dialog = new DatePickerDialog(getContext(),
@ -201,10 +203,10 @@ public class KeyEditor extends LinearLayout implements Editor, OnClickListener {
} }
} }
GregorianCalendar cal = new GregorianCalendar(); GregorianCalendar cal = new GregorianCalendar(TimeZone.getTimeZone("UTC"));
cal.setTime(PgpKeyHelper.getCreationDate(key)); cal.setTime(PgpKeyHelper.getCreationDate(key));
mCreationDate.setText(DateFormat.getDateInstance().format(cal.getTime())); mCreationDate.setText(DateFormat.getDateInstance().format(cal.getTime()));
cal = new GregorianCalendar(); cal = new GregorianCalendar(TimeZone.getTimeZone("UTC"));
Date expiryDate = PgpKeyHelper.getExpiryDate(key); Date expiryDate = PgpKeyHelper.getExpiryDate(key);
if (expiryDate == null) { if (expiryDate == null) {
setExpiryDate(null); setExpiryDate(null);

View File

@ -29,6 +29,7 @@ import java.net.UnknownHostException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.GregorianCalendar; import java.util.GregorianCalendar;
import java.util.List; import java.util.List;
import java.util.TimeZone;
import java.util.regex.Matcher; import java.util.regex.Matcher;
import java.util.regex.Pattern; import java.util.regex.Pattern;
@ -182,8 +183,11 @@ public class HkpKeyServer extends KeyServer {
info.keyId = PgpKeyHelper.convertHexToKeyId(matcher.group(3)); info.keyId = PgpKeyHelper.convertHexToKeyId(matcher.group(3));
info.fingerPrint = PgpKeyHelper.convertKeyIdToHex(info.keyId); info.fingerPrint = PgpKeyHelper.convertKeyIdToHex(info.keyId);
String chunks[] = matcher.group(4).split("-"); String chunks[] = matcher.group(4).split("-");
info.date = new GregorianCalendar(Integer.parseInt(chunks[0]),
Integer.parseInt(chunks[1]), Integer.parseInt(chunks[2])).getTime(); GregorianCalendar tmpGreg = new GregorianCalendar(TimeZone.getTimeZone("UTC"));
tmpGreg.set(Integer.parseInt(chunks[0]), Integer.parseInt(chunks[1]),
Integer.parseInt(chunks[2]));
info.date = tmpGreg.getTime();
info.userIds = new ArrayList<String>(); info.userIds = new ArrayList<String>();
if (matcher.group(5).startsWith("*** KEY")) { if (matcher.group(5).startsWith("*** KEY")) {
info.revoked = matcher.group(5); info.revoked = matcher.group(5);