本文整理匯總了Java中org.bouncycastle.openssl.PEMParser.readObject方法的典型用法代碼示例。如果您正苦於以下問題:Java PEMParser.readObject方法的具體用法?Java PEMParser.readObject怎麽用?Java PEMParser.readObject使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類org.bouncycastle.openssl.PEMParser
的用法示例。
在下文中一共展示了PEMParser.readObject方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: setUp
import org.bouncycastle.openssl.PEMParser; //導入方法依賴的package包/類
@Before
public void setUp() throws Exception {
// TODO should do this in @BeforeClass. Need to find out how to get to
// files from static junit method
BufferedInputStream bis = new BufferedInputStream(this.getClass().getResourceAsStream("/ca.crt"));
testCACert = cf.generateCertificate(bis);
bis.close();
crypto.addCACertificateToTrustStore(testCACert, "ca");
bis = new BufferedInputStream(this.getClass().getResourceAsStream("/keypair-signed.crt"));
Certificate cert = cf.generateCertificate(bis);
bis.close();
// TODO: get PEM file without dropping down to BouncyCastle ?
PEMParser pem = new PEMParser(new FileReader(this.getClass().getResource("/keypair-signed.key").getFile()));
PEMKeyPair bcKeyPair = (PEMKeyPair) pem.readObject();
PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(bcKeyPair.getPrivateKeyInfo().getEncoded());
PrivateKey key = kf.generatePrivate(keySpec);
Certificate[] certificates = new Certificate[] {cert, testCACert};
crypto.getTrustStore().setKeyEntry("key", key, "123456".toCharArray(), certificates);
pem.close();
}
示例2: verifyUserSig
import org.bouncycastle.openssl.PEMParser; //導入方法依賴的package包/類
@Override
public boolean verifyUserSig(String identifier, String sig)throws QCloudException {
try {
Security.addProvider(new BouncyCastleProvider());
//DeBaseUrl64 urlSig to json
Base64 decoder = new Base64();
byte [] compressBytes = Base64Url.base64DecodeUrl(sig.getBytes(Charset.forName("UTF-8")));
//Decompression
Inflater decompression = new Inflater();
decompression.setInput(compressBytes, 0, compressBytes.length);
byte [] decompressBytes = new byte [1024];
int decompressLength = decompression.inflate(decompressBytes);
decompression.end();
String jsonString = new String(Arrays.copyOfRange(decompressBytes, 0, decompressLength));
//Get TLS.Sig from json
JSONObject jsonObject= JSON.parseObject(jsonString);
String sigTLS = jsonObject.getString("TLS.sig");
//debase64 TLS.Sig to get serailString
byte[] signatureBytes = decoder.decode(sigTLS.getBytes(Charset.forName("UTF-8")));
String strSdkAppid = jsonObject.getString("TLS.sdk_appid");
String sigTime = jsonObject.getString("TLS.time");
String sigExpire = jsonObject.getString("TLS.expire_after");
if (!imConfig.getSdkAppId().equals(strSdkAppid))
{
return false;
}
if ( System.currentTimeMillis()/1000 - Long.parseLong(sigTime) > Long.parseLong(sigExpire)) {
return false;
}
//Get Serial String from json
String SerialString =
"TLS.appid_at_3rd:" + 0 + "\n" +
"TLS.account_type:" + 0 + "\n" +
"TLS.identifier:" + identifier + "\n" +
"TLS.sdk_appid:" + imConfig.getSdkAppId() + "\n" +
"TLS.time:" + sigTime + "\n" +
"TLS.expire_after:" + sigExpire + "\n";
Reader reader = new CharArrayReader(imConfig.getPublicKey().toCharArray());
PEMParser parser = new PEMParser(reader);
JcaPEMKeyConverter converter = new JcaPEMKeyConverter();
Object obj = parser.readObject();
parser.close();
PublicKey pubKeyStruct = converter.getPublicKey((SubjectPublicKeyInfo) obj);
Signature signature = Signature.getInstance("SHA256withECDSA","BC");
signature.initVerify(pubKeyStruct);
signature.update(SerialString.getBytes(Charset.forName("UTF-8")));
return signature.verify(signatureBytes);
}catch (Exception e) {
throw new QCloudException(e);
}
}
示例3: generate
import org.bouncycastle.openssl.PEMParser; //導入方法依賴的package包/類
static PrivateKey generate(Path path) throws IOException {
try (Reader in = Files.newBufferedReader(path, StandardCharsets.UTF_8)) {
PEMParser parser = new PEMParser(in);
Object keyPair = parser.readObject();
if (!(keyPair instanceof PEMKeyPair)) {
throw new IllegalStateException(String.format("%s contains an artifact that is not a key pair: %s", path, keyPair));
}
PrivateKeyInfo privateKeyInfo = ((PEMKeyPair) keyPair).getPrivateKeyInfo();
if (privateKeyInfo == null) {
throw new IllegalStateException(String.format("%s does not contain a private key", path));
}
return CONVERTER.getPrivateKey(privateKeyInfo);
}
}
示例4: generate
import org.bouncycastle.openssl.PEMParser; //導入方法依賴的package包/類
static List<X509Certificate> generate(Path path) throws IOException, CertificateException {
List<X509Certificate> certificates = new ArrayList<>();
try (Reader in = Files.newBufferedReader(path, StandardCharsets.UTF_8)) {
PEMParser parser = new PEMParser(in);
Object certificate;
while ((certificate = parser.readObject()) != null) {
if (!(certificate instanceof X509CertificateHolder)) {
throw new IllegalStateException(String.format("%s contains an artifact that is not a certificate: %s", path, certificate));
}
certificates.add(CONVERTER.getCertificate((X509CertificateHolder) certificate));
}
}
return certificates;
}
示例5: getPrivateKeyFromPEM
import org.bouncycastle.openssl.PEMParser; //導入方法依賴的package包/類
private static PrivateKey getPrivateKeyFromPEM(final Reader keyReader)
throws IOException {
final JcaPEMKeyConverter jcaPEMKeyConverter = new JcaPEMKeyConverter();
final PEMParser pem = new PEMParser(keyReader);
PrivateKey key;
Object pemContent = pem.readObject();
if(pemContent instanceof PEMKeyPair) {
PEMKeyPair pemKeyPair = (PEMKeyPair)pemContent;
KeyPair keyPair = jcaPEMKeyConverter.getKeyPair(pemKeyPair);
key = keyPair.getPrivate();
} else if (pemContent instanceof PrivateKeyInfo) {
PrivateKeyInfo privateKeyInfo = (PrivateKeyInfo) pemContent;
key = jcaPEMKeyConverter.getPrivateKey(privateKeyInfo);
} else {
throw new IllegalArgumentException("Unsupported private key format '" + pemContent.getClass().getSimpleName() + '"');
}
pem.close();
return key;
}
示例6: readVapidKeys
import org.bouncycastle.openssl.PEMParser; //導入方法依賴的package包/類
/**
* Read the VAPID key from the .pem file in the resources folder.
*
* @return
* @throws IOException
*/
public static KeyPair readVapidKeys() throws IOException {
try (InputStreamReader inputStreamReader = new InputStreamReader(TestUtils.class.getResourceAsStream("/vapid.pem"))) {
PEMParser pemParser = new PEMParser(inputStreamReader);
PEMKeyPair pemKeyPair = (PEMKeyPair) pemParser.readObject();
return new JcaPEMKeyConverter().getKeyPair(pemKeyPair);
} catch (IOException e) {
throw new IOException("The private key could not be decrypted", e);
}
}
示例7: loadKeyPair
import org.bouncycastle.openssl.PEMParser; //導入方法依賴的package包/類
private PrivateKey loadKeyPair() throws IOException {
PEMParser reader = new PEMParser(file);
Object pemObject;
JcaPEMKeyConverter converter = new JcaPEMKeyConverter().setProvider("BC");
//PEMDecryptorProvider decryptionProv = new JcePEMDecryptorProviderBuilder().build(passphrase);
while((pemObject = reader.readObject()) != null) {
logger.debug("PemObject type: " + pemObject.getClass().getName());
if(pemObject instanceof PEMKeyPair) {
logger.debug("it match");
PrivateKeyInfo pki = ((PEMKeyPair) pemObject).getPrivateKeyInfo();
logger.debug("content: " + pki.getEncoded("UTF-8"));
return converter.getPrivateKey(pki);
} else {
logger.debug("Dont match");
}
}
logger.debug("fsdfsfs");
return null;
}
示例8: providePemObjects
import org.bouncycastle.openssl.PEMParser; //導入方法依賴的package包/類
@Singleton
@Provides
@Named("pemObjects")
static ImmutableList<Object> providePemObjects(PemBytes pemBytes) {
PEMParser pemParser =
new PEMParser(new InputStreamReader(new ByteArrayInputStream(pemBytes.getBytes()), UTF_8));
ImmutableList.Builder<Object> listBuilder = new ImmutableList.Builder<>();
Object obj;
// PEMParser returns an object (private key, certificate, etc) each time readObject() is called,
// until no more object is to be read from the file.
while (true) {
try {
obj = pemParser.readObject();
if (obj == null) {
break;
} else {
listBuilder.add(obj);
}
} catch (IOException e) {
logger.severe(e, "Cannot parse PEM file correctly.");
throw new RuntimeException(e);
}
}
return listBuilder.build();
}
示例9: readKeyPairFromFile
import org.bouncycastle.openssl.PEMParser; //導入方法依賴的package包/類
/**
* Reads a keypair from a File.
*
* @throws IOException If an error occurs while reading the file.
* @throws InvalidKeySpecException If the key isn't encoded in x509.
*/
public static KeyPair readKeyPairFromFile(File file) throws IOException, InvalidKeySpecException {
PEMParser parser =
new PEMParser(new InputStreamReader(new FileInputStream(file), Constants.charset));
Object obj = parser.readObject();
parser.close();
if (obj instanceof PEMKeyPair) {
PEMKeyPair pem = (PEMKeyPair) obj;
JcaPEMKeyConverter conv = new JcaPEMKeyConverter();
return conv.getKeyPair(pem);
}
return null;
}
示例10: parsePem
import org.bouncycastle.openssl.PEMParser; //導入方法依賴的package包/類
public static X509Certificate parsePem(String pemEncodedCert) throws CertificateException {
StringReader sr = new StringReader(pemEncodedCert);
PEMParser pemReader = new PEMParser(sr);
try {
X509CertificateHolder certificateHolder = ((X509CertificateHolder) pemReader.readObject());
if (certificateHolder == null) {
return null;
}
X509Certificate cert = new JcaX509CertificateConverter().setProvider(BouncyCastleProvider.PROVIDER_NAME).getCertificate(certificateHolder);
return cert;
} catch (IOException ex) {
throw new CertificateException(ex);
} finally {
IOUtils.closeQuietly(pemReader);
}
}
示例11: readPrivateKey
import org.bouncycastle.openssl.PEMParser; //導入方法依賴的package包/類
private PrivateKey readPrivateKey(String privateKeyPath, String keyPassword) throws IOException {
FileReader fileReader = new FileReader(privateKeyPath);
PEMParser keyReader = new PEMParser(fileReader);
JcaPEMKeyConverter converter = new JcaPEMKeyConverter();
PEMDecryptorProvider decryptionProv = new JcePEMDecryptorProviderBuilder().build(keyPassword.toCharArray());
Object keyPair = keyReader.readObject();
PrivateKeyInfo keyInfo;
if (keyPair instanceof PEMEncryptedKeyPair) {
PEMKeyPair decryptedKeyPair = ((PEMEncryptedKeyPair) keyPair).decryptKeyPair(decryptionProv);
keyInfo = decryptedKeyPair.getPrivateKeyInfo();
} else {
keyInfo = ((PEMKeyPair) keyPair).getPrivateKeyInfo();
}
keyReader.close();
return converter.getPrivateKey(keyInfo);
}
示例12: loadCertificateFromPEM
import org.bouncycastle.openssl.PEMParser; //導入方法依賴的package包/類
public static X509Certificate loadCertificateFromPEM(InputStream certfile)
throws IOException, CertificateException, FileFormatException {
PEMParser parser = new PEMParser(new InputStreamReader(certfile));
try {
Object obj = parser.readObject();
if (obj instanceof X509CertificateHolder) {
return new JcaX509CertificateConverter().setProvider("BC")
.getCertificate((X509CertificateHolder) obj);
} else {
throw new FileFormatException(
"Failed to read X509Certificate from InputStream provided");
}
} finally {
IOUtils.closeQuietly(certfile);
parser.close();
}
}
示例13: doOpenSslTestFile
import org.bouncycastle.openssl.PEMParser; //導入方法依賴的package包/類
private void doOpenSslTestFile(
String fileName,
Class expectedPrivKeyClass)
throws IOException
{
JcaPEMKeyConverter converter = new JcaPEMKeyConverter().setProvider("BC");
PEMDecryptorProvider decProv = new JcePEMDecryptorProviderBuilder().setProvider("BC").build("changeit".toCharArray());
PEMParser pr = openPEMResource("data/" + fileName);
Object o = pr.readObject();
if (o == null || !((o instanceof PEMKeyPair) || (o instanceof PEMEncryptedKeyPair)))
{
fail("Didn't find OpenSSL key");
}
KeyPair kp = (o instanceof PEMEncryptedKeyPair) ?
converter.getKeyPair(((PEMEncryptedKeyPair)o).decryptKeyPair(decProv)) : converter.getKeyPair((PEMKeyPair)o);
PrivateKey privKey = kp.getPrivate();
if (!expectedPrivKeyClass.isInstance(privKey))
{
fail("Returned key not of correct type");
}
}
示例14: doDudPasswordTest
import org.bouncycastle.openssl.PEMParser; //導入方法依賴的package包/類
private void doDudPasswordTest(String password, int index, String message)
{
// illegal state exception check - in this case the wrong password will
// cause an underlying class cast exception.
try
{
PEMDecryptorProvider decProv = new JcePEMDecryptorProviderBuilder().setProvider("BC").build(password.toCharArray());
PEMParser pemRd = openPEMResource("test.pem");
Object o;
while ((o = pemRd.readObject()) != null)
{
if (o instanceof PEMEncryptedKeyPair)
{
((PEMEncryptedKeyPair)o).decryptKeyPair(decProv);
}
}
fail("issue not detected: " + index);
}
catch (IOException e)
{
// ignore
}
}
示例15: getSocketFactoryFromPEM
import org.bouncycastle.openssl.PEMParser; //導入方法依賴的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();
}