本文整理汇总了Java中org.bouncycastle.pkcs.PKCSException类的典型用法代码示例。如果您正苦于以下问题:Java PKCSException类的具体用法?Java PKCSException怎么用?Java PKCSException使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
PKCSException类属于org.bouncycastle.pkcs包,在下文中一共展示了PKCSException类的11个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: loadCSR
import org.bouncycastle.pkcs.PKCSException; //导入依赖的package包/类
/**
* Load a CSR from the specified URL.
*
* @param url The URL to load CSR from
* @return The CSR
* @throws CryptoException Problem encountered while loading the CSR
* @throws FileNotFoundException If the CSR file does not exist, is a directory rather than a regular file, or for
* some other reason cannot be opened for reading
* @throws IOException An I/O error occurred
*/
public static PKCS10CertificationRequest loadCSR(URL url)
throws CryptoException, IOException
{
// TODO: handle DER encoded requests too?
try (PEMParser pr = new PEMParser(new InputStreamReader(NetUtil.openGetStream(url))))
{
PKCS10CertificationRequest csr = (PKCS10CertificationRequest) pr.readObject();
ContentVerifierProvider prov = new JcaContentVerifierProviderBuilder().build(csr.getSubjectPublicKeyInfo());
if (!csr.isSignatureValid(prov))
{
throw new CryptoException(RB.getString("NoVerifyCsr.exception.message"));
}
return csr;
}
catch (ClassCastException | OperatorCreationException | PKCSException ex)
{
throw new CryptoException(RB.getString("NoLoadCsr.exception.message"), ex);
}
}
示例2: getPublicKey
import org.bouncycastle.pkcs.PKCSException; //导入依赖的package包/类
public AsymmetricKeyParameter getPublicKey()
throws PKCSException
{
try
{
return PublicKeyFactory.createKey(this.getSubjectPublicKeyInfo());
}
catch (IOException e)
{
throw new PKCSException("error extracting key encoding: " + e.getMessage(), e);
}
}
示例3: generatePKCS10CSR
import org.bouncycastle.pkcs.PKCSException; //导入依赖的package包/类
/**
* Create a PKCS #10 certification request (CSR) using the supplied certificate and private key.
*
* @param cert The certificate
* @param privateKey The private key
* @throws CryptoException If there was a problem generating the CSR
* @return The CSR
*/
public static PKCS10CertificationRequest generatePKCS10CSR(X509Certificate cert, PrivateKey privateKey)
throws CryptoException
{
X500Name subject = new X500Name(cert.getSubjectDN().toString());
JcaPKCS10CertificationRequestBuilder csrBuilder =
new JcaPKCS10CertificationRequestBuilder(subject, cert.getPublicKey());
JcaContentSignerBuilder signerBuilder = new JcaContentSignerBuilder(cert.getSigAlgName());
try
{
ContentVerifierProvider prov = new JcaContentVerifierProviderBuilder().build(cert);
PKCS10CertificationRequest csr = csrBuilder.build(signerBuilder.build(privateKey));
if (!csr.isSignatureValid(prov))
{
throw new CryptoException(RB.getString("NoVerifyGenCsr.exception.message"));
}
return csr;
}
catch (OperatorCreationException | PKCSException ex)
{
throw new CryptoException(RB.getString("NoGenerateCsr.exception.message"), ex);
}
}
示例4: writeBinary
import org.bouncycastle.pkcs.PKCSException; //导入依赖的package包/类
@Override
public void writeBinary(IOResource<OutputStream> out, CertObjectStore certObjects)
throws IOException, UnsupportedOperationException {
try {
List<PKCS12SafeBagBuilder> safeBagBuilders = new ArrayList<>(certObjects.size());
for (CertObjectStore.Entry certObject : certObjects) {
switch (certObject.type()) {
case CRT:
safeBagBuilders.add(createCRTSafeBagBuilder(certObject.alias(), certObject.getCRT(),
safeBagBuilders.isEmpty()));
break;
case KEY:
safeBagBuilders.add(createKeySafeBagBuilder(certObject.alias(), certObject.getKey()));
break;
case CSR:
break;
case CRL:
break;
}
}
PKCS12PfxPduBuilder pkcs12Builder = new PKCS12PfxPduBuilder();
for (PKCS12SafeBagBuilder safeBagBuilder : safeBagBuilders) {
pkcs12Builder.addData(safeBagBuilder.build());
}
PKCS12PfxPdu pkcs12 = pkcs12Builder.build(null, null);
out.io().write(pkcs12.getEncoded());
} catch (GeneralSecurityException | PKCSException e) {
throw new CertProviderException(e);
}
}
示例5: buildInputDecryptorProvider
import org.bouncycastle.pkcs.PKCSException; //导入依赖的package包/类
private static InputDecryptorProvider buildInputDecryptorProvider(String resource, PasswordCallback password,
@Nullable PKCSException decryptException) throws IOException {
char[] passwordChars = (decryptException != null ? password.requeryPassword(resource, decryptException)
: password.queryPassword(resource));
if (passwordChars == null) {
throw new PasswordRequiredException(resource, decryptException);
}
return PKCS12_DECRYPTOR_PROVIDER_BUILDER.build(passwordChars);
}
示例6: convertPrivateKey
import org.bouncycastle.pkcs.PKCSException; //导入依赖的package包/类
private static PrivateKey convertPrivateKey(PKCS8EncryptedPrivateKeyInfo safeBagValue, String resource,
PasswordCallback password) throws IOException {
PrivateKeyInfo decryptedSafeBagValue = null;
PKCSException decryptException = null;
while (decryptedSafeBagValue == null) {
try {
decryptedSafeBagValue = safeBagValue
.decryptPrivateKeyInfo(buildInputDecryptorProvider(resource, password, decryptException));
} catch (PKCSException e) {
decryptException = e;
}
}
return convertPrivateKey(decryptedSafeBagValue);
}
示例7: writeEncryptedBinary
import org.bouncycastle.pkcs.PKCSException; //导入依赖的package包/类
@Override
public void writeEncryptedBinary(IOResource<OutputStream> out, CertObjectStore certObjects,
PasswordCallback newPassword) throws IOException {
char[] passwordChars = newPassword.queryPassword(out.resource());
if (passwordChars == null) {
throw new PasswordRequiredException(out.resource());
}
try {
List<PKCS12SafeBagBuilder> safeBagBuilders = new ArrayList<>(certObjects.size());
for (CertObjectStore.Entry certObject : certObjects) {
switch (certObject.type()) {
case CRT:
safeBagBuilders.add(createCRTSafeBagBuilder(certObject.alias(), certObject.getCRT(),
safeBagBuilders.isEmpty()));
break;
case KEY:
safeBagBuilders
.add(createKeySafeBagBuilder(certObject.alias(), certObject.getKey(), passwordChars));
break;
case CSR:
break;
case CRL:
break;
}
}
PKCS12PfxPduBuilder pkcs12Builder = new PKCS12PfxPduBuilder();
for (PKCS12SafeBagBuilder safeBagBuilder : safeBagBuilders) {
pkcs12Builder.addData(safeBagBuilder.build());
}
PKCS12PfxPdu pkcs12 = pkcs12Builder.build(new BcPKCS12MacCalculatorBuilder(), passwordChars);
out.io().write(pkcs12.getEncoded());
} catch (GeneralSecurityException | PKCSException e) {
throw new CertProviderException(e);
}
}
示例8: CryptoException
import org.bouncycastle.pkcs.PKCSException; //导入依赖的package包/类
public CryptoException(PKCSException e) {
super(e);
}
示例9: parsePrivateKey
import org.bouncycastle.pkcs.PKCSException; //导入依赖的package包/类
/**
* Parses a PrivateKey instance from a PEM representation.
*
* When the provided key is encrypted, the provided pass phrase is applied.
*
* @param pemRepresentation a PEM representation of a private key (cannot be null or empty)
* @param passPhrase optional pass phrase (must be present if the private key is encrypted).
* @return a PrivateKey instance (never null)
*/
public static PrivateKey parsePrivateKey(InputStream pemRepresentation, String passPhrase) throws IOException {
if ( passPhrase == null ) {
passPhrase = "";
}
try (Reader reader = new InputStreamReader(pemRepresentation); //
PEMParser pemParser = new PEMParser(reader)) {
final Object object = pemParser.readObject();
final JcaPEMKeyConverter converter = new JcaPEMKeyConverter().setProvider( "BC" );
final KeyPair kp;
if ( object instanceof PEMEncryptedKeyPair )
{
// Encrypted key - we will use provided password
final PEMDecryptorProvider decProv = new JcePEMDecryptorProviderBuilder().build( passPhrase.toCharArray() );
kp = converter.getKeyPair( ( (PEMEncryptedKeyPair) object ).decryptKeyPair( decProv ) );
}
else if ( object instanceof PKCS8EncryptedPrivateKeyInfo )
{
// Encrypted key - we will use provided password
try
{
final PKCS8EncryptedPrivateKeyInfo encryptedInfo = (PKCS8EncryptedPrivateKeyInfo) object;
final InputDecryptorProvider provider = new JceOpenSSLPKCS8DecryptorProviderBuilder().build( passPhrase.toCharArray() );
final PrivateKeyInfo privateKeyInfo = encryptedInfo.decryptPrivateKeyInfo( provider );
return converter.getPrivateKey( privateKeyInfo );
}
catch ( PKCSException | OperatorCreationException e )
{
throw new IOException( "Unable to decrypt private key.", e );
}
}
else if ( object instanceof PrivateKeyInfo )
{
return converter.getPrivateKey( (PrivateKeyInfo) object );
}
else
{
// Unencrypted key - no password needed
kp = converter.getKeyPair( (PEMKeyPair) object );
}
return kp.getPrivate();
}
}
示例10: getPkcs10_Pkcs8_AsPemStrings
import org.bouncycastle.pkcs.PKCSException; //导入依赖的package包/类
/**
* Get the PKCS#10 PEM string and encrypted PKCS#8 PEM string.
* @param subject
* @param email Added as a Subject Alt Name extension if not null
* @param pw
* @return First element contains the PKCS#10 PEM, second element contains the private key.
* @throws IOException
* @throws NoSuchAlgorithmException
* @throws NoSuchProviderException
* @throws OperatorCreationException
* @throws PKCSException
*/
public String[] getPkcs10_Pkcs8_AsPemStrings(X500Name subject, String email, String pw)
throws IOException, NoSuchAlgorithmException,
NoSuchProviderException, OperatorCreationException, PKCSException {
// Create a PKCS10 cert signing request
KeyPairGenerator kpg = KeyPairGenerator.getInstance("RSA", "BC");
kpg.initialize(2048);
KeyPair kp = kpg.genKeyPair();
PrivateKey priKey = kp.getPrivate();
// X500NameBuilder x500NameBld = new X500NameBuilder(BCStyle.INSTANCE);
// x500NameBld.addRDN(BCStyle.C, csrRequestValidationConfigParams.getCountryOID());
// x500NameBld.addRDN(BCStyle.O, csrRequestValidationConfigParams.getOrgNameOID());
// x500NameBld.addRDN(BCStyle.OU, ou);
// x500NameBld.addRDN(BCStyle.L, loc);
// x500NameBld.addRDN(BCStyle.CN, cn);
// X500Name subject = x500NameBld.build();
PKCS10CertificationRequestBuilder requestBuilder
= new JcaPKCS10CertificationRequestBuilder(subject, kp.getPublic());
ExtensionsGenerator extGen = new ExtensionsGenerator();
if(email != null){
extGen.addExtension(Extension.subjectAlternativeName, false,
new GeneralNames(new GeneralName(GeneralName.rfc822Name, email)));
}
requestBuilder.addAttribute(
PKCSObjectIdentifiers.pkcs_9_at_extensionRequest, extGen.generate());
String sigName = "SHA1withRSA";
PKCS10CertificationRequest req1 = requestBuilder.build(
new JcaContentSignerBuilder(sigName).setProvider("BC").build(kp.getPrivate()));
if (req1.isSignatureValid(new JcaContentVerifierProviderBuilder().setProvider("BC").build(kp.getPublic()))) {
//log.info(sigName + ": PKCS#10 request verified.");
} else {
//log.error(sigName + ": Failed verify check.");
throw new RuntimeException(sigName + ": Failed verify check.");
}
StringWriter writer = new StringWriter();
PEMWriter pemWrite = new PEMWriter(writer);
pemWrite.writeObject(req1);
pemWrite.close();
String csr = writer.toString();
JceOpenSSLPKCS8EncryptorBuilder encryptorBuilder
= new JceOpenSSLPKCS8EncryptorBuilder(PKCS8Generator.PBE_SHA1_3DES);
SecureRandom random = new SecureRandom();
encryptorBuilder.setRandom(random);
encryptorBuilder.setPasssword(pw.toCharArray());
OutputEncryptor oe = encryptorBuilder.build();
JcaPKCS8Generator pkcs8GeneratorEnc = new JcaPKCS8Generator(priKey, oe);
// Output encrypted private key pkcs8 PEM string (todo use later api)
PemObject pkcs8PemEnc = pkcs8GeneratorEnc.generate();
StringWriter writer2 = new StringWriter();
PEMWriter pemWrite2 = new PEMWriter(writer2);
pemWrite2.writeObject(pkcs8PemEnc);
pemWrite2.close();
String pkcs8StrEnc = writer2.toString();
String[] pems = new String[2];
pems[0] = csr;
pems[1] = pkcs8StrEnc;
return pems;
}
示例11: createPfx
import org.bouncycastle.pkcs.PKCSException; //导入依赖的package包/类
private PKCS12PfxPdu createPfx(PrivateKey privKey, PublicKey pubKey, X509Certificate[] chain)
throws NoSuchAlgorithmException, IOException, PKCSException
{
JcaX509ExtensionUtils extUtils = new JcaX509ExtensionUtils();
PKCS12SafeBagBuilder taCertBagBuilder = new JcaPKCS12SafeBagBuilder(chain[2]);
taCertBagBuilder.addBagAttribute(PKCSObjectIdentifiers.pkcs_9_at_friendlyName, new DERBMPString("Bouncy Primary Certificate"));
PKCS12SafeBagBuilder caCertBagBuilder = new JcaPKCS12SafeBagBuilder(chain[1]);
caCertBagBuilder.addBagAttribute(PKCSObjectIdentifiers.pkcs_9_at_friendlyName, new DERBMPString("Bouncy Intermediate Certificate"));
PKCS12SafeBagBuilder eeCertBagBuilder = new JcaPKCS12SafeBagBuilder(chain[0]);
eeCertBagBuilder.addBagAttribute(PKCSObjectIdentifiers.pkcs_9_at_friendlyName, new DERBMPString("Eric's Key"));
eeCertBagBuilder.addBagAttribute(PKCSObjectIdentifiers.pkcs_9_at_localKeyId, extUtils.createSubjectKeyIdentifier(pubKey));
PKCS12SafeBagBuilder keyBagBuilder = new JcaPKCS12SafeBagBuilder(privKey, new BcPKCS12PBEOutputEncryptorBuilder(PKCSObjectIdentifiers.pbeWithSHAAnd3_KeyTripleDES_CBC, new CBCBlockCipher(new DESedeEngine())).build(passwd));
keyBagBuilder.addBagAttribute(PKCSObjectIdentifiers.pkcs_9_at_friendlyName, new DERBMPString("Eric's Key"));
keyBagBuilder.addBagAttribute(PKCSObjectIdentifiers.pkcs_9_at_localKeyId, extUtils.createSubjectKeyIdentifier(pubKey));
//
// construct the actual key store
//
PKCS12PfxPduBuilder pfxPduBuilder = new PKCS12PfxPduBuilder();
PKCS12SafeBag[] certs = new PKCS12SafeBag[3];
certs[0] = eeCertBagBuilder.build();
certs[1] = caCertBagBuilder.build();
certs[2] = taCertBagBuilder.build();
pfxPduBuilder.addEncryptedData(new BcPKCS12PBEOutputEncryptorBuilder(PKCSObjectIdentifiers.pbeWithSHAAnd40BitRC2_CBC, new CBCBlockCipher(new RC2Engine())).build(passwd), certs);
pfxPduBuilder.addData(keyBagBuilder.build());
return pfxPduBuilder.build(new BcPKCS12MacCalculatorBuilder(), passwd);
}