|
1 | 1 | package org.bouncycastle.cbor.c509.test; |
2 | 2 |
|
| 3 | +import java.io.ByteArrayOutputStream; |
3 | 4 | import java.io.IOException; |
4 | 5 | import java.io.InputStream; |
5 | 6 | import java.math.BigInteger; |
| 7 | +import java.util.Date; |
6 | 8 |
|
7 | 9 | import junit.framework.TestCase; |
8 | 10 | import org.bouncycastle.asn1.x509.Certificate; |
| 11 | +import org.bouncycastle.asn1.x509.TBSCertificate; |
9 | 12 | import org.bouncycastle.test.TestResourceFinder; |
| 13 | +import org.bouncycastle.cbor.CBORDecoder; |
| 14 | +import org.bouncycastle.cbor.CBOREncoder; |
10 | 15 | import org.bouncycastle.cbor.c509.C509AttributeType; |
11 | 16 | import org.bouncycastle.cbor.c509.C509Certificate; |
12 | 17 | import org.bouncycastle.cbor.c509.C509ConversionOptions; |
@@ -271,6 +276,108 @@ public void testNotAfterMustUseNullFor9999() |
271 | 276 | expectRejected(modified, "notAfter 99991231235959Z as uint"); |
272 | 277 | } |
273 | 278 |
|
| 279 | + public void testValidityOutsideX509TimeDomainRejected() |
| 280 | + throws Exception |
| 281 | + { |
| 282 | + byte[] good = vector("a1_c509_reencoded"); |
| 283 | + C509Certificate cert = C509Certificate.getInstance(good); |
| 284 | + |
| 285 | + // adding 2^61 seconds moves a validity value by 125 * 2^64 milliseconds, so an |
| 286 | + // accepted value would convert to the very millisecond the original did: the |
| 287 | + // reconstructed X.509 date, the signed DER TBSCertificate and the issuer |
| 288 | + // signature would all be unchanged, while a validity decision taken on the raw |
| 289 | + // seconds saw an expiry roughly 73 billion years out |
| 290 | + long wrapDelta = 1L << 61; |
| 291 | + assertEquals(0L, wrapDelta * 1000); |
| 292 | + expectRejected(withValidityField(good, 5, cert.getNotAfter() + wrapDelta), |
| 293 | + "notAfter displaced by 2^61 seconds"); |
| 294 | + expectRejected(withValidityField(good, 4, cert.getNotBefore() + wrapDelta), |
| 295 | + "notBefore displaced by 2^61 seconds"); |
| 296 | + |
| 297 | + // one second past 99991231235959Z is the first value with no X.509 meaning |
| 298 | + expectRejected(withValidityField(good, 4, C509Certificate.NO_EXPIRATION_DATE + 1), |
| 299 | + "notBefore after 99991231235959Z"); |
| 300 | + expectRejected(withValidityField(good, 5, C509Certificate.NO_EXPIRATION_DATE + 1), |
| 301 | + "notAfter after 99991231235959Z"); |
| 302 | + |
| 303 | + // the largest value CBOR can carry, and the largest a Java long can hold |
| 304 | + expectRejected(withValidityField(good, 5, Long.MAX_VALUE), "notAfter of 2^63-1"); |
| 305 | + |
| 306 | + // 99991231235959Z itself stays representable as a notBefore; as a notAfter it |
| 307 | + // must take the null encoding, which testNotAfterMustUseNullFor9999 covers. |
| 308 | + // The draft states no ordering requirement, so notBefore > notAfter is left to |
| 309 | + // the validity decision, which simply never holds. |
| 310 | + C509Certificate atLimit = C509Certificate.getInstance( |
| 311 | + withValidityField(good, 4, C509Certificate.NO_EXPIRATION_DATE)); |
| 312 | + assertEquals(C509Certificate.NO_EXPIRATION_DATE, atLimit.getNotBefore()); |
| 313 | + assertEquals(253402300799000L, C509Certificate.toDate(C509Certificate.NO_EXPIRATION_DATE).getTime()); |
| 314 | + } |
| 315 | + |
| 316 | + public void testGeneratedValidityBoundedToX509TimeDomain() |
| 317 | + throws Exception |
| 318 | + { |
| 319 | + // the generation path applies the same bound, so no caller can mint a validity |
| 320 | + // value the parse path would refuse to read back |
| 321 | + TBSCertificate tbs = Certificate.getInstance(vector("a1_x509")).getTBSCertificate(); |
| 322 | + try |
| 323 | + { |
| 324 | + C509Certificate.createTBSCertificate(C509Certificate.TYPE_REENCODED_X509, |
| 325 | + tbs.getSerialNumber().getValue(), tbs.getSignature(), tbs.getIssuer(), |
| 326 | + new Date((C509Certificate.NO_EXPIRATION_DATE + 1) * 1000L), tbs.getEndDate().getDate(), |
| 327 | + tbs.getSubject(), tbs.getSubjectPublicKeyInfo(), tbs.getExtensions(), |
| 328 | + C509ConversionOptions.DEFAULT); |
| 329 | + fail("accepted a validityNotBefore after 99991231235959Z"); |
| 330 | + } |
| 331 | + catch (IOException e) |
| 332 | + { |
| 333 | + assertEquals("validityNotBefore must not be after 9999-12-31T23:59:59Z", e.getMessage()); |
| 334 | + } |
| 335 | + |
| 336 | + try |
| 337 | + { |
| 338 | + C509Certificate.toDate(C509Certificate.NO_EXPIRATION_DATE + 1); |
| 339 | + fail("converted a validity value outside the X.509 time domain"); |
| 340 | + } |
| 341 | + catch (IllegalArgumentException e) |
| 342 | + { |
| 343 | + assertEquals("validity value outside the X.509 time domain: 253402300800", e.getMessage()); |
| 344 | + } |
| 345 | + } |
| 346 | + |
| 347 | + /** |
| 348 | + * Re-encode a C509 certificate with the CBOR item at the given index of the |
| 349 | + * certificate array replaced by an unsigned integer, leaving every other item, |
| 350 | + * including the issuer signature, exactly as it was. |
| 351 | + */ |
| 352 | + private static byte[] withValidityField(byte[] encoding, int index, long seconds) |
| 353 | + throws IOException |
| 354 | + { |
| 355 | + CBORDecoder in = new CBORDecoder(encoding); |
| 356 | + int count = in.readArrayHeader(); |
| 357 | + byte[][] items = new byte[count][]; |
| 358 | + for (int i = 0; i != count; i++) |
| 359 | + { |
| 360 | + items[i] = in.readEncodedItem(); |
| 361 | + } |
| 362 | + in.expectEnd(); |
| 363 | + |
| 364 | + ByteArrayOutputStream bOut = new ByteArrayOutputStream(); |
| 365 | + CBOREncoder out = new CBOREncoder(bOut); |
| 366 | + out.writeArrayHeader(count); |
| 367 | + for (int i = 0; i != count; i++) |
| 368 | + { |
| 369 | + if (i == index) |
| 370 | + { |
| 371 | + out.writeUnsignedInteger(seconds); |
| 372 | + } |
| 373 | + else |
| 374 | + { |
| 375 | + out.writeEncoded(items[i]); |
| 376 | + } |
| 377 | + } |
| 378 | + return bOut.toByteArray(); |
| 379 | + } |
| 380 | + |
274 | 381 | public void testCertificationRequestTemplateRoundTrip() |
275 | 382 | throws Exception |
276 | 383 | { |
|
0 commit comments