org.bouncycastle.asn1.DERGeneralizedTime Java Examples

The following examples show how to use org.bouncycastle.asn1.DERGeneralizedTime. 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: 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 #2
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 #3
Source File: DPrivateKeyUsagePeriod.java    From keystore-explorer with GNU General Public License v3.0 5 votes vote down vote up
private void okPressed() {

		Date notBefore = jdtNotBefore.getDateTime();
		Date notAfter = jdtNotAfter.getDateTime();

		if ((notBefore == null) && (notAfter == null)) {
			JOptionPane.showMessageDialog(this, res.getString("DPrivateKeyUsagePeriod.ValueReq.message"), getTitle(),
					JOptionPane.WARNING_MESSAGE);
			return;
		}

		// BC forgot the value constructor for PrivateKeyUsagePeriod...
		ASN1EncodableVector v = new ASN1EncodableVector();
		if (notBefore != null) {
			DERGeneralizedTime notBeforeGenTime = new DERGeneralizedTime(notBefore);
			v.add(new DERTaggedObject(false, 0, notBeforeGenTime));
		}
		if (notAfter != null) {
			DERGeneralizedTime notAfterGenTime = new DERGeneralizedTime(notAfter);
			v.add(new DERTaggedObject(false, 1, notAfterGenTime));
		}

		PrivateKeyUsagePeriod privateKeyUsagePeriod = PrivateKeyUsagePeriod.getInstance(new DERSequence(v));

		try {
			value = privateKeyUsagePeriod.getEncoded(ASN1Encoding.DER);
		} catch (IOException e) {
			DError.displayError(this, e);
			return;
		}

		closeDialog();
	}
 
Example #4
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 #5
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);
  }
}