本文整理汇总了Java中org.bouncycastle.util.io.pem.PemObject类的典型用法代码示例。如果您正苦于以下问题:Java PemObject类的具体用法?Java PemObject怎么用?Java PemObject使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
PemObject类属于org.bouncycastle.util.io.pem包,在下文中一共展示了PemObject类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: createPrivateKeyFromPemFile
import org.bouncycastle.util.io.pem.PemObject; //导入依赖的package包/类
private static PrivateKey createPrivateKeyFromPemFile(final String keyFileName) throws IOException, InvalidKeySpecException, NoSuchAlgorithmException
{
// Loads a privte key from the specified key file name
final PemReader pemReader = new PemReader(new FileReader(keyFileName));
final PemObject pemObject = pemReader.readPemObject();
final byte[] pemContent = pemObject.getContent();
pemReader.close();
final PKCS8EncodedKeySpec encodedKeySpec = new PKCS8EncodedKeySpec(pemContent);
final KeyFactory keyFactory = getKeyFactoryInstance();
final PrivateKey privateKey = keyFactory.generatePrivate(encodedKeySpec);
return privateKey;
}
开发者ID:PacktPublishing,项目名称:MQTT-Essentials-A-Lightweight-IoT-Protocol,代码行数:13,代码来源:SecurityHelper.java
示例2: parseObject
import org.bouncycastle.util.io.pem.PemObject; //导入依赖的package包/类
public Object parseObject(PemObject obj)
throws IOException
{
try
{
PrivateKeyInfo info = PrivateKeyInfo.getInstance(ASN1Primitive.fromByteArray(obj.getContent()));
PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(obj.getContent());
KeyFactory keyFact = KeyFactory.getInstance(info.getPrivateKeyAlgorithm().getAlgorithm().getId(), provider);
return keyFact.generatePrivate(keySpec);
}
catch (Exception e)
{
throw new PEMException("problem parsing PRIVATE KEY: " + e.toString(), e);
}
}
示例3: convert
import org.bouncycastle.util.io.pem.PemObject; //导入依赖的package包/类
public static ByteArrayOutputStream convert(final ByteArrayOutputStream baos) {
try (ByteArrayOutputStream autoCloseableBaos = baos;
ByteArrayInputStream bais = new ByteArrayInputStream(autoCloseableBaos.toByteArray());
Reader reader = new InputStreamReader(bais);
PemReader pemReader = new PemReader(reader)) {
PemObject pemObject = pemReader.readPemObject();
if (pemObject == null) {
throw new DSSException("Unable to read PEM Object");
}
byte[] binaries = pemObject.getContent();
ByteArrayOutputStream os = new ByteArrayOutputStream();
os.write(binaries, 0, binaries.length);
return os;
} catch (IOException e) {
throw new DSSException("Unable to convert the CRL to DER", e);
}
}
示例4: readObject
import org.bouncycastle.util.io.pem.PemObject; //导入依赖的package包/类
public Object readObject()
throws IOException
{
PemObject obj = readPemObject();
if (obj != null)
{
String type = obj.getType();
if (parsers.containsKey(type))
{
return ((PemObjectParser)parsers.get(type)).parseObject(obj);
}
else
{
throw new IOException("unrecognised object: " + type);
}
}
return null;
}
示例5: generate
import org.bouncycastle.util.io.pem.PemObject; //导入依赖的package包/类
public PemObject generate()
throws PemGenerationException
{
try
{
if (encryptorBuilder != null)
{
outputEncryptor = encryptorBuilder.build();
}
}
catch (OperatorCreationException e)
{
throw new PemGenerationException("unable to create operator: " + e.getMessage(), e);
}
if (outputEncryptor != null)
{
return generate(key, outputEncryptor);
}
else
{
return generate(key, null);
}
}
示例6: generateCA
import org.bouncycastle.util.io.pem.PemObject; //导入依赖的package包/类
protected void generateCA() throws NoSuchProviderException, NoSuchAlgorithmException, IOException, OperatorCreationException {
KeyPair pair = generateKey();
LocalDateTime startDate = LocalDate.now().atStartOfDay();
X509v3CertificateBuilder builder= new X509v3CertificateBuilder(
new X500Name("CN=ca"),
new BigInteger("0"),
Date.from(startDate.atZone(ZoneId.systemDefault()).toInstant()),
Date.from(startDate.plusDays(3650).atZone(ZoneId.systemDefault()).toInstant()),
new X500Name("CN=ca"),
SubjectPublicKeyInfo.getInstance(pair.getPublic().getEncoded()));
JcaContentSignerBuilder csBuilder= new JcaContentSignerBuilder("SHA512WithRSAEncryption");
ContentSigner signer = csBuilder.build(pair.getPrivate());
X509CertificateHolder holder = builder.build(signer);
try (PemWriter writer = new PemWriter(new FileWriter(pemPath.toFile()))) {
writer.writeObject(new PemObject("CERTIFICATE", holder.toASN1Structure().getEncoded()));
}
try (PemWriter writer = new PemWriter(new FileWriter(keyPath.toFile()))) {
writer.writeObject(new PemObject("PRIVATE KEY", pair.getPrivate().getEncoded()));
}
}
示例7: convertToPresentation
import org.bouncycastle.util.io.pem.PemObject; //导入依赖的package包/类
@Override
public String convertToPresentation(String value, Class<? extends String> targetType, Locale locale) throws ConversionException {
if (value == null) {
return null;
} else {
try {
final CertificateFactory certFactory = CertificateFactory.getInstance("X.509");
final InputStream in = new ByteArrayInputStream(Base64.decodeBase64(value));
final Certificate certificate = certFactory.generateCertificate(in);
final StringWriter stringWriter = new StringWriter();
final PemWriter pemWriter = new PemWriter(stringWriter);
final PemObject pemObject = new PemObject("CERTIFICATE", certificate.getEncoded());
pemWriter.writeObject(pemObject);
pemWriter.flush();
return stringWriter.toString();
} catch (final Exception e) {
throw new ConversionException("Error generating X509 certificate from database byte array.", e);
}
}
}
示例8: parseChain
import org.bouncycastle.util.io.pem.PemObject; //导入依赖的package包/类
public static List<Certificate> parseChain(final String chain) throws IOException, CertificateException {
Preconditions.checkNotNull(chain);
final List<Certificate> certs = new ArrayList<Certificate>();
try(final PemReader pemReader = new PemReader(new StringReader(chain));)
{
final PemObject pemObject = pemReader.readPemObject();
final CertificateFactory certificateFactory = CertificateFactory.getInstance("X509");
final ByteArrayInputStream bais = new ByteArrayInputStream(pemObject.getContent());
for (final Certificate cert : certificateFactory.generateCertificates(bais)) {
if (cert instanceof X509Certificate) {
certs.add(cert);
}
}
if (certs.isEmpty()) {
throw new IllegalStateException("Unable to decode certificate chain");
}
}
return certs;
}
示例9: generateCertificateUsingCsr
import org.bouncycastle.util.io.pem.PemObject; //导入依赖的package包/类
private Certificate generateCertificateUsingCsr(final String csr, final List<String> domainNames, final List<String> ipAddresses, final int validityDays) throws NoSuchAlgorithmException, InvalidKeyException, NoSuchProviderException, CertificateException, SignatureException, IOException, OperatorCreationException {
PemObject pemObject = null;
try {
final PemReader pemReader = new PemReader(new StringReader(csr));
pemObject = pemReader.readPemObject();
} catch (IOException e) {
LOG.error("Failed to read provided CSR string as a PEM object", e);
}
if (pemObject == null) {
throw new CloudRuntimeException("Unable to read/process CSR: " + csr);
}
final PKCS10CertificationRequest request = new PKCS10CertificationRequest(pemObject.getContent());
final String subject = request.getCertificationRequestInfo().getSubject().toString();
final X509Certificate clientCertificate = CertUtils.generateV3Certificate(
caCertificate, caKeyPair, request.getPublicKey(),
subject, CAManager.CertSignatureAlgorithm.value(),
validityDays, domainNames, ipAddresses);
return new Certificate(clientCertificate, null, Collections.singletonList(caCertificate));
}
示例10: parse
import org.bouncycastle.util.io.pem.PemObject; //导入依赖的package包/类
/**
* Parse a PemObject. Currently only supports <code>CERTIFICATE</code> / <code>X.509 CERTIFICATE</code> types
*
* @param obj
* a PemObject with a type and with contents
*
* @return a parsed object (or null if the input is null)
*
* @throws GeneralSecurityException
* if there is a parsing problem
* @throws IllegalArgumentException
* if the PemObject cannot be recognised
*/
public static Object parse(final PemObject obj) throws GeneralSecurityException
{
if (obj == null)
{
return null;
}
else if (obj.getType() == null)
{
throw new RuntimeException("Encountered invalid PemObject with null type: " + obj);
}
else if (obj.getType().equalsIgnoreCase("CERTIFICATE") || obj.getType().equalsIgnoreCase("X.509 CERTIFICATE"))
{
return parseX509Certificate(obj);
}
else
{
throw new IllegalArgumentException("Unknown PEM contents: encountered unsupported entry of type " +
obj.getType() +
" (expected CERTIFICATE or X.509 CERTIFICATE)");
}
}
示例11: parseObject
import org.bouncycastle.util.io.pem.PemObject; //导入依赖的package包/类
/**
* Reads in a X509Certificate.
*
* @return the X509Certificate
* @throws IOException if an I/O error occured
*/
public Object parseObject(PemObject obj)
throws IOException
{
ByteArrayInputStream bIn = new ByteArrayInputStream(obj.getContent());
try
{
CertificateFactory certFact
= CertificateFactory.getInstance("X.509", provider);
return certFact.generateCertificate(bIn);
}
catch (Exception e)
{
throw new PEMException("problem parsing cert: " + e.toString(), e);
}
}
示例12: loadPrivateKeyResource
import org.bouncycastle.util.io.pem.PemObject; //导入依赖的package包/类
static AsymmetricKeyParameter loadPrivateKeyResource(String resource)
throws IOException
{
PemObject pem = loadPemResource(resource);
if (pem.getType().endsWith("RSA PRIVATE KEY"))
{
RSAPrivateKey rsa = RSAPrivateKey.getInstance(pem.getContent());
return new RSAPrivateCrtKeyParameters(rsa.getModulus(), rsa.getPublicExponent(),
rsa.getPrivateExponent(), rsa.getPrime1(), rsa.getPrime2(), rsa.getExponent1(),
rsa.getExponent2(), rsa.getCoefficient());
}
if (pem.getType().endsWith("PRIVATE KEY"))
{
return PrivateKeyFactory.createKey(pem.getContent());
}
throw new IllegalArgumentException("'resource' doesn't specify a valid private key");
}
示例13: getSocketFactoryFromPEM
import org.bouncycastle.util.io.pem.PemObject; //导入依赖的package包/类
private SSLSocketFactory getSocketFactoryFromPEM(String filePath) throws Exception {
Security.addProvider(new BouncyCastleProvider());
PEMParser pemParser = new PEMParser(new FileReader(getConfigFilename(filePath)));
pemParser.readObject();
PemObject pemObject = pemParser.readPemObject();
pemParser.close();
X509CertificateHolder holder = new X509CertificateHolder(pemObject.getContent());
X509Certificate bc = new JcaX509CertificateConverter().setProvider("BC")
.getCertificate(holder);
KeyStore keyStore = KeyStore.getInstance(KeyStore.getDefaultType());
keyStore.load(null, null);
keyStore.setCertificateEntry("ca", bc);
TrustManager trustManager = TrustManagerUtils.getDefaultTrustManager(keyStore);
SSLContext sslContext = SSLContextUtils.createSSLContext("TLS", null, trustManager);
return sslContext.getSocketFactory();
}
示例14: parseObject
import org.bouncycastle.util.io.pem.PemObject; //导入依赖的package包/类
/**
* Reads in a X509Certificate.
*
* @return the X509Certificate
* @throws java.io.IOException if an I/O error occured
*/
public Object parseObject(PemObject obj)
throws IOException
{
try
{
return new X509CertificateHolder(obj.getContent());
}
catch (Exception e)
{
throw new PEMException("problem parsing cert: " + e.toString(), e);
}
}
示例15: generate
import org.bouncycastle.util.io.pem.PemObject; //导入依赖的package包/类
public PemObject generate()
throws PemGenerationException
{
try
{
return createPemObject(obj);
}
catch (IOException e)
{
throw new PemGenerationException("encoding exception: " + e.getMessage(), e);
}
}