org.bouncycastle.asn1.ASN1Encoding Java Examples

The following examples show how to use org.bouncycastle.asn1.ASN1Encoding. 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: DSubjectKeyIdentifier.java    From keystore-explorer with GNU General Public License v3.0 6 votes vote down vote up
private void okPressed() {
	byte[] keyIdentifier = jkiKeyIdentifier.getKeyIdentifier();

	if (keyIdentifier == null) {
		JOptionPane.showMessageDialog(this, res.getString("DSubjectKeyIdentifier.ValueReq.message"), getTitle(),
				JOptionPane.WARNING_MESSAGE);
		return;
	}

	SubjectKeyIdentifier subjectKeyIdentifier = new SubjectKeyIdentifier(keyIdentifier);

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

	closeDialog();
}
 
Example #2
Source File: DSubjectAlternativeName.java    From keystore-explorer with GNU General Public License v3.0 6 votes vote down vote up
private void okPressed() {
	GeneralNames alternativeName = jgnAlternativeName.getGeneralNames();

	if (alternativeName.getNames().length == 0) {
		JOptionPane.showMessageDialog(this, res.getString("DSubjectAlternativeName.ValueReq.message"), getTitle(),
				JOptionPane.WARNING_MESSAGE);
		return;
	}

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

	closeDialog();
}
 
Example #3
Source File: DIssuerAlternativeName.java    From keystore-explorer with GNU General Public License v3.0 6 votes vote down vote up
private void okPressed() {
	GeneralNames issuerAlternativeName = jgnAlternativeName.getGeneralNames();

	if (issuerAlternativeName.getNames().length == 0) {
		JOptionPane.showMessageDialog(this, res.getString("DIssuerAlternativeName.ValueReq.message"), getTitle(),
				JOptionPane.WARNING_MESSAGE);
		return;
	}

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

	closeDialog();
}
 
Example #4
Source File: DNetscapeSslServerName.java    From keystore-explorer with GNU General Public License v3.0 6 votes vote down vote up
private void okPressed() {
	String netscapeSslServerNameStr = jtfNetscapeSslServerName.getText().trim();

	if (netscapeSslServerNameStr.length() == 0) {
		JOptionPane.showMessageDialog(this, res.getString("DNetscapeSslServerName.ValueReq.message"), getTitle(),
				JOptionPane.WARNING_MESSAGE);
		return;
	}

	DERIA5String netscapeSslServerName = new DERIA5String(netscapeSslServerNameStr);

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

	closeDialog();
}
 
Example #5
Source File: DNameConstraints.java    From keystore-explorer with GNU General Public License v3.0 6 votes vote down vote up
private void okPressed() {
	List<GeneralSubtree> permittedSubtrees = jgsPermittedSubtrees.getGeneralSubtrees().getGeneralSubtrees();
	List<GeneralSubtree> excludedSubtrees = jgsExcludedSubtrees.getGeneralSubtrees().getGeneralSubtrees();

	GeneralSubtree[] permittedSubtreesArray = permittedSubtrees.toArray(new GeneralSubtree[permittedSubtrees.size()]);
	GeneralSubtree[] excludedSubtreesArray = excludedSubtrees.toArray(new GeneralSubtree[excludedSubtrees.size()]);

	NameConstraints nameConstraints = new NameConstraints(permittedSubtreesArray, excludedSubtreesArray);

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

	closeDialog();
}
 
Example #6
Source File: DSubjectInformationAccess.java    From keystore-explorer with GNU General Public License v3.0 6 votes vote down vote up
private void okPressed() {
	List<AccessDescription> accessDescriptions = jadAccessDescriptions.getAccessDescriptions();

	if (accessDescriptions.size() == 0) {
		JOptionPane.showMessageDialog(this, res.getString("DSubjectInformationAccess.ValueReq.message"),
				getTitle(), JOptionPane.WARNING_MESSAGE);
		return;
	}

	SubjectInfoAccess subjectInformationAccess = new SubjectInfoAccess(accessDescriptions);

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

	closeDialog();
}
 
Example #7
Source File: DAuthorityInformationAccess.java    From keystore-explorer with GNU General Public License v3.0 6 votes vote down vote up
private void okPressed() {
	List<AccessDescription> accessDescriptions = jadAccessDescriptions.getAccessDescriptions();

	if (accessDescriptions.isEmpty()) {
		JOptionPane.showMessageDialog(this, res.getString("DAuthorityInformationAccess.ValueReq.message"),
				getTitle(), JOptionPane.WARNING_MESSAGE);
		return;
	}

	ASN1EncodableVector vec = new ASN1EncodableVector();
	for (AccessDescription accessDescription : accessDescriptions) {
		vec.add(accessDescription);
	}
	AuthorityInformationAccess authorityInformationAccess =
			AuthorityInformationAccess.getInstance(new DERSequence(vec));

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

	closeDialog();
}
 
Example #8
Source File: DNetscapeBaseUrl.java    From keystore-explorer with GNU General Public License v3.0 6 votes vote down vote up
private void okPressed() {
	String netscapeBaseUrlStr = jtfNetscapeBaseUrl.getText().trim();

	if (netscapeBaseUrlStr.length() == 0) {
		JOptionPane.showMessageDialog(this, res.getString("DNetscapeBaseUrl.ValueReq.message"), getTitle(),
				JOptionPane.WARNING_MESSAGE);
		return;
	}

	DERIA5String netscapeBaseUrl = new DERIA5String(netscapeBaseUrlStr);

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

	closeDialog();
}
 
Example #9
Source File: DCertificatePolicies.java    From keystore-explorer with GNU General Public License v3.0 6 votes vote down vote up
private void okPressed() {
	List<PolicyInformation> policyInformation = jpiCertificatePolicies.getPolicyInformation();

	if (policyInformation.isEmpty()) {
		JOptionPane.showMessageDialog(this, res.getString("DCertificatePolicies.ValueReq.message"), getTitle(),
				JOptionPane.WARNING_MESSAGE);
		return;
	}



	CertificatePolicies certificatePolicies = new CertificatePolicies(policyInformation.toArray(
			new PolicyInformation[policyInformation.size()]));

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

	closeDialog();
}
 
Example #10
Source File: DPolicyMappings.java    From keystore-explorer with GNU General Public License v3.0 6 votes vote down vote up
private void okPressed() {
	PolicyMappings policyMappings = jpmPolicyMappings.getPolicyMappings();
	ASN1Sequence policyMappingsSeq = (ASN1Sequence) policyMappings.toASN1Primitive();

	if (policyMappingsSeq.size() == 0) {
		JOptionPane.showMessageDialog(this, res.getString("DPolicyMappings.ValueReq.message"), getTitle(),
				JOptionPane.WARNING_MESSAGE);
		return;
	}

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

	closeDialog();
}
 
Example #11
Source File: DNetscapeComment.java    From keystore-explorer with GNU General Public License v3.0 6 votes vote down vote up
private void okPressed() {
	String netscapeCommentStr = jtaNetscapeComment.getText().trim();

	if (netscapeCommentStr.length() == 0) {
		JOptionPane.showMessageDialog(this, res.getString("DNetscapeComment.ValueReq.message"), getTitle(),
				JOptionPane.WARNING_MESSAGE);
		return;
	}

	DERIA5String netscapeComment = new DERIA5String(netscapeCommentStr);

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

	closeDialog();
}
 
Example #12
Source File: DNetscapeRevocationUrl.java    From keystore-explorer with GNU General Public License v3.0 6 votes vote down vote up
private void okPressed() {
	String netscapeRevocationUrlStr = jtfNetscapeRevocationUrl.getText().trim();

	if (netscapeRevocationUrlStr.length() == 0) {
		JOptionPane.showMessageDialog(this, res.getString("DNetscapeRevocationUrl.ValueReq.message"), getTitle(),
				JOptionPane.WARNING_MESSAGE);
		return;
	}

	DERIA5String netscapeRevocationUrl = new DERIA5String(netscapeRevocationUrlStr);

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

	closeDialog();
}
 
Example #13
Source File: DNetscapeCaRevocationUrl.java    From keystore-explorer with GNU General Public License v3.0 6 votes vote down vote up
private void okPressed() {
	String netscapeCaRevocationUrlStr = jtfNetscapeCaRevocationUrl.getText().trim();

	if (netscapeCaRevocationUrlStr.length() == 0) {
		JOptionPane.showMessageDialog(this, res.getString("DNetscapeCaRevocationUrl.ValueReq.message"), getTitle(),
				JOptionPane.WARNING_MESSAGE);
		return;
	}

	DERIA5String netscapeCaRevocationUrl = new DERIA5String(netscapeCaRevocationUrlStr);

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

	closeDialog();
}
 
Example #14
Source File: DNetscapeCertificateRenewalUrl.java    From keystore-explorer with GNU General Public License v3.0 6 votes vote down vote up
private void okPressed() {
	String netscapeCertificateRenewalUrlStr = jtfNetscapeCertificateRenewalUrl.getText().trim();

	if (netscapeCertificateRenewalUrlStr.length() == 0) {
		JOptionPane.showMessageDialog(this, res.getString("DNetscapeCertificateRenewalUrl.ValueReq.message"),
				getTitle(), JOptionPane.WARNING_MESSAGE);
		return;
	}

	DERIA5String netscapeCertificateRenewalUrl = new DERIA5String(netscapeCertificateRenewalUrlStr);

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

	closeDialog();
}
 
Example #15
Source File: SM2PfxMakerTest.java    From gmhelper with Apache License 2.0 6 votes vote down vote up
@Test
public void testMakePfx() {
    try {
        KeyPair subKP = SM2Util.generateKeyPair();
        X500Name subDN = SM2X509CertMakerTest.buildSubjectDN();
        SM2PublicKey sm2SubPub = new SM2PublicKey(subKP.getPublic().getAlgorithm(),
            (BCECPublicKey) subKP.getPublic());
        byte[] csr = CommonUtil.createCSR(subDN, sm2SubPub, subKP.getPrivate(),
            SM2X509CertMaker.SIGN_ALGO_SM3WITHSM2).getEncoded();
        SM2X509CertMaker certMaker = SM2X509CertMakerTest.buildCertMaker();
        X509Certificate cert = certMaker.makeSSLEndEntityCert(csr);

        SM2PfxMaker pfxMaker = new SM2PfxMaker();
        PKCS10CertificationRequest request = new PKCS10CertificationRequest(csr);
        PublicKey subPub = BCECUtil.createPublicKeyFromSubjectPublicKeyInfo(request.getSubjectPublicKeyInfo());
        PKCS12PfxPdu pfx = pfxMaker.makePfx(subKP.getPrivate(), subPub, cert, TEST_PFX_PASSWD);
        byte[] pfxDER = pfx.getEncoded(ASN1Encoding.DER);
        FileUtil.writeFile(TEST_PFX_FILENAME, pfxDER);
    } catch (Exception ex) {
        ex.printStackTrace();
        Assert.fail();
    }
}
 
Example #16
Source File: DNetscapeCaPolicyUrl.java    From keystore-explorer with GNU General Public License v3.0 6 votes vote down vote up
private void okPressed() {
	String netscapeCaPolicyUrlStr = jtfNetscapeCaPolicyUrl.getText().trim();

	if (netscapeCaPolicyUrlStr.length() == 0) {
		JOptionPane.showMessageDialog(this, res.getString("DNetscapeCaPolicyUrl.ValueReq.message"), getTitle(),
				JOptionPane.WARNING_MESSAGE);
		return;
	}

	DERIA5String netscapeCaPolicyUrl = new DERIA5String(netscapeCaPolicyUrlStr);

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

	closeDialog();
}
 
Example #17
Source File: RsaCertificateAuthorityClient.java    From protect with MIT License 6 votes vote down vote up
/*** Static Methods ***/

	private static BigInteger EMSA_PKCS1_V1_5_ENCODE(byte[] input, final BigInteger modulus)
			throws NoSuchAlgorithmException, IOException {

		// Digest the input
		final MessageDigest md = MessageDigest.getInstance(HASH_ALGORITHM);
		final byte[] digest = md.digest(input);

		// Create a digest info consisting of the algorithm id and the hash
		final AlgorithmIdentifier algId = new AlgorithmIdentifier(NISTObjectIdentifiers.id_sha512, DERNull.INSTANCE);
		final DigestInfo digestInfo = new DigestInfo(algId, digest);
		final byte[] message = digestInfo.getEncoded(ASN1Encoding.DER);

		// Do PKCS1 padding
		final byte[] block = new byte[((modulus.bitLength() + 7) / 8) - 1];
		System.arraycopy(message, 0, block, block.length - message.length, message.length);
		block[0] = 0x01; // type code 1
		for (int i = 1; i != block.length - message.length - 1; i++) {
			block[i] = (byte) 0xFF;
		}

		return new BigInteger(1, block);
	}
 
Example #18
Source File: RsaSigningClient.java    From protect with MIT License 6 votes vote down vote up
public static BigInteger EMSA_PKCS1_V1_5_ENCODE(byte[] input, final BigInteger modulus)
		throws NoSuchAlgorithmException, IOException {

	// Digest the input
	final MessageDigest md = MessageDigest.getInstance(HASH_ALGORITHM);
	final byte[] digest = md.digest(input);

	// Create a digest info consisting of the algorithm id and the hash
	final AlgorithmIdentifier algId = new AlgorithmIdentifier(NISTObjectIdentifiers.id_sha512, DERNull.INSTANCE);
	final DigestInfo digestInfo = new DigestInfo(algId, digest);
	final byte[] message = digestInfo.getEncoded(ASN1Encoding.DER);

	// Do PKCS1 padding
	final byte[] block = new byte[(modulus.bitLength() / 8) - 1];
	System.arraycopy(message, 0, block, block.length - message.length, message.length);
	block[0] = 0x01; // type code 1
	for (int i = 1; i != block.length - message.length - 1; i++) {
		block[i] = (byte) 0xFF;
	}

	return new BigInteger(1, block);
}
 
Example #19
Source File: DSSASN1Utils.java    From dss with GNU Lesser General Public License v2.1 5 votes vote down vote up
/**
 * Transforms an object of class {@code IssuerSerial} into instance of
 * {@code CertificateIdentifier}
 * 
 * @param issuerAndSerial {@link IssuerSerial} to transform
 * @return {@link CertificateIdentifier}
 */
public static CertificateIdentifier toCertificateIdentifier(IssuerSerial issuerAndSerial) {
	if (issuerAndSerial == null) {
		return null;
	}
	try {
		CertificateIdentifier certificateIdentifier = new CertificateIdentifier();
		GeneralNames gnames = issuerAndSerial.getIssuer();
		if (gnames != null) {
			GeneralName[] names = gnames.getNames();
			if (names.length == 1) {
				certificateIdentifier.setIssuerName(new X500Principal(names[0].getName().toASN1Primitive().getEncoded(ASN1Encoding.DER)));
			} else {
				LOG.warn("More than one GeneralName");
			}
		}

		ASN1Integer serialNumber = issuerAndSerial.getSerial();
		if (serialNumber != null) {
			certificateIdentifier.setSerialNumber(serialNumber.getValue());
		}

		return certificateIdentifier;
	} catch (Exception e) {
		LOG.error("Unable to read the IssuerSerial object", e);
		return null;
	}
}
 
Example #20
Source File: P11RSADigestSignatureSpi.java    From xipki with Apache License 2.0 5 votes vote down vote up
private byte[] derEncode(byte[] hash) throws IOException {
  if (digestAlgId == null) {
    // For raw RSA, the DigestInfo must be prepared externally
    return hash;
  }

  DigestInfo digestInfo = new DigestInfo(digestAlgId, hash);
  return digestInfo.getEncoded(ASN1Encoding.DER);
}
 
Example #21
Source File: X509Util.java    From xipki with Apache License 2.0 5 votes vote down vote up
public static String rdnValueToString(ASN1Encodable value) {
  Args.notNull(value, "value");
  if (value instanceof ASN1String && !(value instanceof DERUniversalString)) {
    return ((ASN1String) value).getString();
  } else {
    try {
      return "#" + Hex.encode(
          value.toASN1Primitive().getEncoded(ASN1Encoding.DER));
    } catch (IOException ex) {
      throw new IllegalArgumentException("other value has no encoded form");
    }
  }
}
 
Example #22
Source File: DKeyUsage.java    From keystore-explorer with GNU General Public License v3.0 5 votes vote down vote up
private void okPressed() {
	if (!jcbDigitalSignature.isSelected() && !jcbNonRepudiation.isSelected() && !jcbKeyEncipherment.isSelected()
			&& !jcbDataEncipherment.isSelected() && !jcbKeyAgreement.isSelected()
			&& !jcbCertificateSigning.isSelected() && !jcbCrlSign.isSelected() && !jcbEncipherOnly.isSelected()
			&& !jcbDecipherOnly.isSelected()) {
		JOptionPane.showMessageDialog(this, res.getString("DKeyUsage.ValueReq.message"), getTitle(),
				JOptionPane.WARNING_MESSAGE);
		return;
	}

	int keyUsageIntValue = 0;
	keyUsageIntValue |= jcbDigitalSignature.isSelected() ? KeyUsage.digitalSignature : 0;
	keyUsageIntValue |= jcbNonRepudiation.isSelected() ? KeyUsage.nonRepudiation : 0;
	keyUsageIntValue |= jcbKeyEncipherment.isSelected() ? KeyUsage.keyEncipherment : 0;
	keyUsageIntValue |= jcbDataEncipherment.isSelected() ? KeyUsage.dataEncipherment : 0;
	keyUsageIntValue |= jcbKeyAgreement.isSelected() ? KeyUsage.keyAgreement : 0;
	keyUsageIntValue |= jcbCertificateSigning.isSelected() ? KeyUsage.keyCertSign : 0;
	keyUsageIntValue |= jcbCrlSign.isSelected() ? KeyUsage.cRLSign : 0;
	keyUsageIntValue |= jcbEncipherOnly.isSelected() ? KeyUsage.encipherOnly : 0;
	keyUsageIntValue |= jcbDecipherOnly.isSelected() ? KeyUsage.decipherOnly : 0;

	KeyUsage keyUsage = new KeyUsage(keyUsageIntValue);

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

	closeDialog();
}
 
Example #23
Source File: SAML2SPKeystoreTest.java    From syncope with Apache License 2.0 5 votes vote down vote up
private static Certificate createSelfSignedCert(final KeyPair keyPair) throws Exception {
    final X500Name dn = new X500Name("cn=Unknown");
    final V3TBSCertificateGenerator certGen = new V3TBSCertificateGenerator();

    certGen.setSerialNumber(new ASN1Integer(BigInteger.valueOf(1)));
    certGen.setIssuer(dn);
    certGen.setSubject(dn);
    certGen.setStartDate(new Time(new Date(System.currentTimeMillis() - 1000L)));

    final Date expiration = new Date(System.currentTimeMillis() + 100000);
    certGen.setEndDate(new Time(expiration));

    final AlgorithmIdentifier sigAlgID = new AlgorithmIdentifier(PKCSObjectIdentifiers.sha1WithRSAEncryption, DERNull.INSTANCE);
    certGen.setSignature(sigAlgID);
    certGen.setSubjectPublicKeyInfo(SubjectPublicKeyInfo.getInstance(keyPair.getPublic().getEncoded()));

    final Signature sig = Signature.getInstance("SHA1WithRSA");
    sig.initSign(keyPair.getPrivate());
    sig.update(certGen.generateTBSCertificate().getEncoded(ASN1Encoding.DER));

    final TBSCertificate tbsCert = certGen.generateTBSCertificate();
    final ASN1EncodableVector v = new ASN1EncodableVector();

    v.add(tbsCert);
    v.add(sigAlgID);
    v.add(new DERBitString(sig.sign()));

    final Certificate cert = CertificateFactory.getInstance("X.509")
        .generateCertificate(new ByteArrayInputStream(new DERSequence(v).getEncoded(ASN1Encoding.DER)));
    cert.verify(keyPair.getPublic());
    return cert;
}
 
Example #24
Source File: BaseSyncopeWASAML2ClientTest.java    From syncope with Apache License 2.0 5 votes vote down vote up
protected static Certificate createSelfSignedCert(final KeyPair keyPair) throws Exception {
    final X500Name dn = new X500Name("cn=Unknown");
    final V3TBSCertificateGenerator certGen = new V3TBSCertificateGenerator();

    certGen.setSerialNumber(new ASN1Integer(BigInteger.valueOf(1)));
    certGen.setIssuer(dn);
    certGen.setSubject(dn);
    certGen.setStartDate(new Time(new Date(System.currentTimeMillis() - 1000L)));

    final Date expiration = new Date(System.currentTimeMillis() + 100000);
    certGen.setEndDate(new Time(expiration));

    final AlgorithmIdentifier sigAlgID = new AlgorithmIdentifier(PKCSObjectIdentifiers.sha1WithRSAEncryption, DERNull.INSTANCE);
    certGen.setSignature(sigAlgID);
    certGen.setSubjectPublicKeyInfo(SubjectPublicKeyInfo.getInstance(keyPair.getPublic().getEncoded()));

    final Signature sig = Signature.getInstance("SHA1WithRSA");
    sig.initSign(keyPair.getPrivate());
    sig.update(certGen.generateTBSCertificate().getEncoded(ASN1Encoding.DER));

    final TBSCertificate tbsCert = certGen.generateTBSCertificate();
    final ASN1EncodableVector v = new ASN1EncodableVector();

    v.add(tbsCert);
    v.add(sigAlgID);
    v.add(new DERBitString(sig.sign()));

    final Certificate cert = CertificateFactory.getInstance("X.509")
        .generateCertificate(new ByteArrayInputStream(new DERSequence(v).getEncoded(ASN1Encoding.DER)));
    cert.verify(keyPair.getPublic());
    return cert;
}
 
Example #25
Source File: DSSASN1Utils.java    From dss with GNU Lesser General Public License v2.1 5 votes vote down vote up
/**
 * Returns the ASN.1 encoded representation of {@code CMSSignedData}.
 *
 * @param data
 *             the CMSSignedData to be encoded
 * @return the DER encoded CMSSignedData
 */
public static byte[] getDEREncoded(final CMSSignedData data) {
	try (ByteArrayOutputStream baos = new ByteArrayOutputStream()) {
		final ASN1OutputStream asn1OutputStream = ASN1OutputStream.create(baos, ASN1Encoding.DER);
		asn1OutputStream.writeObject(data.toASN1Structure());
		asn1OutputStream.close();
		return baos.toByteArray();
	} catch (IOException e) {
		throw new DSSException("Unable to encode to DER", e);
	}
}
 
Example #26
Source File: CMSSignedDocument.java    From dss with GNU Lesser General Public License v2.1 5 votes vote down vote up
@Override
public void writeTo(OutputStream stream) throws IOException {
	final byte[] encoded = signedData.getEncoded();
	final ASN1Primitive asn1Primitive = DSSASN1Utils.toASN1Primitive(encoded);
	final ASN1OutputStream asn1OutputStream = ASN1OutputStream.create(stream, ASN1Encoding.DER);
	asn1OutputStream.writeObject(asn1Primitive);
}
 
Example #27
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 #28
Source File: DInhibitAnyPolicy.java    From keystore-explorer with GNU General Public License v3.0 5 votes vote down vote up
private void okPressed() {
	int skipCertificates = -1;

	String skipCertificatesStr = jtfSkipCertificates.getText().trim();

	if (skipCertificatesStr.length() == 0) {
		JOptionPane.showMessageDialog(this, res.getString("DInhibitAnyPolicy.ValueReq.message"), getTitle(),
				JOptionPane.WARNING_MESSAGE);
		return;
	}

	try {
		skipCertificates = Integer.parseInt(skipCertificatesStr);
	} catch (NumberFormatException ex) {
		JOptionPane.showMessageDialog(this, res.getString("DInhibitAnyPolicy.InvalidLengthValue.message"),
				getTitle(), JOptionPane.WARNING_MESSAGE);
		return;
	}

	if (skipCertificates < 0) {
		JOptionPane.showMessageDialog(this, res.getString("DInhibitAnyPolicy.InvalidLengthValue.message"),
				getTitle(), JOptionPane.WARNING_MESSAGE);
		return;
	}

	InhibitAnyPolicy inhibitAnyPolicy = new InhibitAnyPolicy(skipCertificates);

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

	closeDialog();
}
 
Example #29
Source File: ExtensionsConfCreatorDemo.java    From xipki with Apache License 2.0 5 votes vote down vote up
private static void check(Path path) throws Exception {
  byte[] bytes = IoUtil.read(path.toFile());
  ExtensionsType extraExtensions = JSON.parseObject(bytes, ExtensionsType.class);
  extraExtensions.validate();

  List<X509ExtensionType> extnConfs = extraExtensions.getExtensions();
  if (CollectionUtil.isNotEmpty(extnConfs)) {
    for (X509ExtensionType m : extnConfs) {
      byte[] encodedExtnValue =
          m.getConstant().toASN1Encodable().toASN1Primitive().getEncoded(ASN1Encoding.DER);
      new Extension(new ASN1ObjectIdentifier(m.getType().getOid()), false, encodedExtnValue);
    }
  }
}
 
Example #30
Source File: DNetscapeCertificateType.java    From keystore-explorer with GNU General Public License v3.0 5 votes vote down vote up
private void okPressed() {
	if (!jcbSslClient.isSelected() && !jcbSslServer.isSelected() && !jcbSmime.isSelected()
			&& !jcbObjectSigning.isSelected() && !jcbReserved.isSelected() && !jcbSslCa.isSelected()
			&& !jcbSmimeCa.isSelected() && !jcbObjectSigningCa.isSelected()) {
		JOptionPane.showMessageDialog(this, res.getString("DNetscapeCertificateType.ValueReq.message"), getTitle(),
				JOptionPane.WARNING_MESSAGE);
		return;
	}

	int netscapeCertTypeIntValue = 0;
	netscapeCertTypeIntValue |= jcbSslClient.isSelected() ? NetscapeCertType.sslClient : 0;
	netscapeCertTypeIntValue |= jcbSslServer.isSelected() ? NetscapeCertType.sslServer : 0;
	netscapeCertTypeIntValue |= jcbSmime.isSelected() ? NetscapeCertType.smime : 0;
	netscapeCertTypeIntValue |= jcbObjectSigning.isSelected() ? NetscapeCertType.objectSigning : 0;
	netscapeCertTypeIntValue |= jcbReserved.isSelected() ? NetscapeCertType.reserved : 0;
	netscapeCertTypeIntValue |= jcbSslCa.isSelected() ? NetscapeCertType.sslCA : 0;
	netscapeCertTypeIntValue |= jcbSmimeCa.isSelected() ? NetscapeCertType.smimeCA : 0;
	netscapeCertTypeIntValue |= jcbObjectSigningCa.isSelected() ? NetscapeCertType.objectSigningCA : 0;

	NetscapeCertType netscapeCertType = new NetscapeCertType(netscapeCertTypeIntValue);

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

	closeDialog();
}