org.bouncycastle.asn1.DERUTCTime Java Examples

The following examples show how to use org.bouncycastle.asn1.DERUTCTime. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: X509Ext.java    From portecle with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Get Microsoft CRL Next Publish (1.3.6.1.4.1.311.21.4) extension value as a string.
 *
 * @param bValue The octet string value
 * @return Extension value as a string
 * @throws IOException If an I/O problem occurs
 */
private String getMicrosoftCrlNextPublish(byte[] bValue)
    throws IOException
{
	DERUTCTime time = (DERUTCTime) ASN1Primitive.fromByteArray(bValue);
	String date = time.getAdjustedTime();
	try
	{
		date = DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.LONG).format(time.getAdjustedDate());
	}
	catch (ParseException e)
	{
		// Ignored
	}
	return escapeHtml(date);
}
 
Example #2
Source File: Time.java    From signer with GNU Lesser General Public License v3.0 6 votes vote down vote up
@Override
public void parse(ASN1Primitive derObject) {
    if (derObject instanceof ASN1GeneralizedTime) {
        ASN1GeneralizedTime derGeneralizedTime = (ASN1GeneralizedTime) derObject;
        try {
            this.setTime(derGeneralizedTime.getDate());
        } catch (ParseException ex) {
            this.setTime(null);
        }
    } else if (derObject instanceof DERUTCTime) {
        DERUTCTime derUTCTime = (DERUTCTime) derObject;
        try {
            this.setTime(derUTCTime.getDate());
        } catch (ParseException exception) {
            this.setTime(null);
        }
    }
}
 
Example #3
Source File: Asn1StreamParser.java    From xipki with Apache License 2.0 6 votes vote down vote up
protected static Date readTime(MyInt bytesLen, BufferedInputStream instream, String name)
    throws IOException {
  int tag = markAndReadTag(instream);
  byte[] bytes = readBlock(instream, name);
  bytesLen.set(bytes.length);
  try {
    if (tag == BERTags.UTC_TIME) {
      return DERUTCTime.getInstance(bytes).getDate();
    } else if (tag == BERTags.GENERALIZED_TIME) {
      return DERGeneralizedTime.getInstance(bytes).getDate();
    } else {
      throw new IllegalArgumentException("invalid tag for " + name + ": " + tag);
    }
  } catch (ParseException ex) {
    throw new IllegalArgumentException("error parsing time", ex);
  }
}
 
Example #4
Source File: ScepUtil.java    From xipki with Apache License 2.0 6 votes vote down vote up
public static Date getTime(Object obj) {
  if (obj instanceof byte[]) {
    byte[] encoded = (byte[]) obj;
    int tag = encoded[0] & 0xFF;;
    try {
      if (tag == BERTags.UTC_TIME) {
        return DERUTCTime.getInstance(encoded).getDate();
      } else if (tag == BERTags.GENERALIZED_TIME) {
        return DERGeneralizedTime.getInstance(encoded).getDate();
      } else {
        throw new IllegalArgumentException("invalid tag " + tag);
      }
    } catch (ParseException ex) {
      throw new IllegalArgumentException("error parsing time", ex);
    }
  } else if (obj instanceof Time) {
    return ((Time) obj).getDate();
  } else if (obj instanceof org.bouncycastle.asn1.cms.Time) {
    return ((org.bouncycastle.asn1.cms.Time) obj).getDate();
  } else {
    return Time.getInstance(obj).getDate();
  }
}
 
Example #5
Source File: RevocationRefs.java    From signer with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * 
 * 
 * @param extract
 *            CrlValidatedID from X509CRL
 * @return a CrlValidatedID
 * @throws NoSuchAlgorithmException
 * @throws CRLException
 */

private CrlValidatedID makeCrlValidatedID(X509CRL crl)
		throws NoSuchAlgorithmException, CRLException {

	Digest digest = DigestFactory.getInstance().factoryDefault();
	digest.setAlgorithm(DigestAlgorithmEnum.SHA_256);
	
	OtherHashAlgAndValue otherHashAlgAndValue = new OtherHashAlgAndValue(
				new AlgorithmIdentifier(NISTObjectIdentifiers.id_sha256), new DEROctetString(digest.digest(crl.getEncoded())));
	
	OtherHash hash = new OtherHash(otherHashAlgAndValue);

	BigInteger crlnumber;
	CrlIdentifier crlid;
	if (crl.getExtensionValue("2.5.29.20") != null) {
		ASN1Integer varASN1Integer = new ASN1Integer(crl.getExtensionValue("2.5.29.20"));
		crlnumber = varASN1Integer.getPositiveValue();

		crlid = new CrlIdentifier(new X500Name(crl.getIssuerX500Principal()
				.getName()), new DERUTCTime(crl.getThisUpdate()), crlnumber);
	} else {
		crlid = new CrlIdentifier(new X500Name(crl.getIssuerX500Principal()
				.getName()), new DERUTCTime(crl.getThisUpdate()));
	}

	CrlValidatedID crlvid = new CrlValidatedID(hash, crlid);

	return crlvid;
}
 
Example #6
Source File: ExtensionSyntaxChecker.java    From xipki with Apache License 2.0 4 votes vote down vote up
private static FieldType getFieldType(ASN1Encodable obj) {
  FieldType expectedType;
  if (obj instanceof DERBitString) {
    expectedType = FieldType.BIT_STRING;
  } else if (obj instanceof DERBMPString) {
    expectedType = FieldType.BMPString;
  } else if (obj instanceof ASN1Boolean) {
    expectedType = FieldType.BOOLEAN;
  } else if (obj instanceof ASN1Enumerated) {
    expectedType = FieldType.ENUMERATED;
  } else if (obj instanceof DERGeneralizedTime) {
    expectedType = FieldType.GeneralizedTime;
  } else if (obj instanceof DERIA5String) {
    expectedType = FieldType.IA5String;
  } else if (obj instanceof ASN1Integer) {
    expectedType = FieldType.INTEGER;
  } else if (obj instanceof DERNull) {
    expectedType = FieldType.NULL;
  } else if (obj instanceof DEROctetString) {
    expectedType = FieldType.OCTET_STRING;
  } else if (obj instanceof ASN1ObjectIdentifier) {
    expectedType = FieldType.OID;
  } else if (obj instanceof DERPrintableString) {
    expectedType = FieldType.PrintableString;
  } else if (obj instanceof DERT61String) {
    expectedType = FieldType.TeletexString;
  } else if (obj instanceof DERUTCTime) {
    expectedType = FieldType.UTCTime;
  } else if (obj instanceof DERUTF8String) {
    expectedType = FieldType.UTF8String;
  } else if (obj instanceof X500Name) {
    expectedType = FieldType.Name;
  } else if (obj instanceof ASN1Sequence) {
    try {
      X500Name.getInstance(obj);
      expectedType = FieldType.Name;
    } catch (Exception ex) {
      expectedType = FieldType.SEQUENCE;
    }
  } else if (obj instanceof ASN1Set) {
    expectedType = FieldType.SET;
  } else {
    expectedType = null;
  }

  return expectedType;
}
 
Example #7
Source File: ExtensionSyntaxChecker.java    From xipki with Apache License 2.0 4 votes vote down vote up
private static ASN1Encodable getParsedImplicitValue(String name, ASN1TaggedObject taggedObject,
    FieldType fieldType) throws BadCertTemplateException {
  try {
    switch (fieldType) {
      case BIT_STRING:
        return DERBitString.getInstance(taggedObject, false);
      case BMPString:
        return DERBMPString.getInstance(taggedObject, false);
      case BOOLEAN:
        return ASN1Boolean.getInstance(taggedObject, false);
      case ENUMERATED:
        return ASN1Enumerated.getInstance(taggedObject, false);
      case GeneralizedTime:
        return DERGeneralizedTime.getInstance(taggedObject, false);
      case IA5String:
        return DERIA5String.getInstance(taggedObject, false);
      case INTEGER:
        return ASN1Integer.getInstance(taggedObject, false);
      case Name:
        return X500Name.getInstance(taggedObject, false);
      case NULL:
        if (!(taggedObject.getObject() instanceof ASN1OctetString
            && ((ASN1OctetString) taggedObject.getObject()).getOctets().length == 0)) {
          throw new BadCertTemplateException("invalid " + name);
        }
        return DERNull.INSTANCE;
      case OCTET_STRING:
        return DEROctetString.getInstance(taggedObject, false);
      case OID:
        return ASN1ObjectIdentifier.getInstance(taggedObject, false);
      case PrintableString:
        return DERPrintableString.getInstance(taggedObject, false);
      case RAW:
        return taggedObject.getObject();
      case SEQUENCE:
      case SEQUENCE_OF:
        return ASN1Sequence.getInstance(taggedObject, false);
      case SET:
      case SET_OF:
        return ASN1Set.getInstance(taggedObject, false);
      case TeletexString:
        return DERT61String.getInstance(taggedObject, false);
      case UTCTime:
        return DERUTCTime.getInstance(taggedObject, false);
      case UTF8String:
        return DERUTF8String.getInstance(taggedObject, false);
      default:
        throw new RuntimeException("Unknown FieldType " + fieldType);
    }
  } catch (IllegalArgumentException ex) {
    throw new BadCertTemplateException("invalid " + name, ex);
  }
}