本文整理匯總了Java中java.security.cert.CertificateEncodingException類的典型用法代碼示例。如果您正苦於以下問題:Java CertificateEncodingException類的具體用法?Java CertificateEncodingException怎麽用?Java CertificateEncodingException使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
CertificateEncodingException類屬於java.security.cert包,在下文中一共展示了CertificateEncodingException類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: generateSignatureBlock
import java.security.cert.CertificateEncodingException; //導入依賴的package包/類
private static byte[] generateSignatureBlock(
SignerConfig signerConfig, byte[] signatureFileBytes)
throws InvalidKeyException, CertificateEncodingException, SignatureException {
JcaCertStore certs = new JcaCertStore(signerConfig.certificates);
X509Certificate signerCert = signerConfig.certificates.get(0);
String jcaSignatureAlgorithm =
getJcaSignatureAlgorithm(
signerCert.getPublicKey(), signerConfig.signatureDigestAlgorithm);
try {
ContentSigner signer =
new JcaContentSignerBuilder(jcaSignatureAlgorithm)
.build(signerConfig.privateKey);
CMSSignedDataGenerator gen = new CMSSignedDataGenerator();
gen.addSignerInfoGenerator(
new SignerInfoGeneratorBuilder(
new JcaDigestCalculatorProviderBuilder().build(),
SignerInfoSignatureAlgorithmFinder.INSTANCE)
.setDirectSignature(true)
.build(signer, new JcaX509CertificateHolder(signerCert)));
gen.addCertificates(certs);
CMSSignedData sigData =
gen.generate(new CMSProcessableByteArray(signatureFileBytes), false);
ByteArrayOutputStream out = new ByteArrayOutputStream();
try (ASN1InputStream asn1 = new ASN1InputStream(sigData.getEncoded())) {
DEROutputStream dos = new DEROutputStream(out);
dos.writeObject(asn1.readObject());
}
return out.toByteArray();
} catch (OperatorCreationException | CMSException | IOException e) {
throw new SignatureException("Failed to generate signature", e);
}
}
示例2: getIssuerX509Principal
import java.security.cert.CertificateEncodingException; //導入依賴的package包/類
/**
* return the issuer of the given cert as an X509PrincipalObject.
*/
public static X509Principal getIssuerX509Principal(
X509Certificate cert)
throws CertificateEncodingException
{
try
{
TBSCertificateStructure tbsCert = TBSCertificateStructure.getInstance(
ASN1Primitive.fromByteArray(cert.getTBSCertificate()));
return new X509Principal(X509Name.getInstance(tbsCert.getIssuer()));
}
catch (IOException e)
{
throw new CertificateEncodingException(e.toString());
}
}
示例3: getSha1Fingerprint
import java.security.cert.CertificateEncodingException; //導入依賴的package包/類
@Override
public String getSha1Fingerprint(X509Certificate cert) throws NoSuchAlgorithmException, CertificateEncodingException, IllegalArgumentException {
if(cert == null) {
throw new IllegalArgumentException("Provided certificate is empty");
}
MessageDigest md = MessageDigest.getInstance("SHA-1");
byte[] der = cert.getEncoded();
md.update(der);
byte[] digest = md.digest();
return DatatypeConverter.printHexBinary(digest);
}
示例4: writeCertArray
import java.security.cert.CertificateEncodingException; //導入依賴的package包/類
private void writeCertArray(Writer writer, Certificate[] certificates) throws IOException {
if (certificates == null) {
writer.write("-1\n");
return;
}
try {
writer.write(Integer.toString(certificates.length) + '\n');
for (Certificate certificate : certificates) {
byte[] bytes = certificate.getEncoded();
String line = Base64.encode(bytes);
writer.write(line + '\n');
}
} catch (CertificateEncodingException e) {
throw new IOException(e.getMessage());
}
}
示例5: processEntityCertificateChain
import java.security.cert.CertificateEncodingException; //導入依賴的package包/類
/** Process the value of {@link X509Credential#getEntityCertificateChain()}.
*
* @param keyInfo the KeyInfo that is being built
* @param x509Data the X509Data that is being built
* @param credential the Credential that is being processed
* @throws SecurityException thrown if the certificate data can not be encoded from the Java certificate object
*/
protected void processEntityCertificateChain(KeyInfo keyInfo, X509Data x509Data, X509Credential credential)
throws SecurityException {
if (options.emitEntityCertificateChain && credential.getEntityCertificateChain() != null) {
for (java.security.cert.X509Certificate javaCert : credential.getEntityCertificateChain()) {
try {
X509Certificate xmlCert = KeyInfoHelper.buildX509Certificate(javaCert);
x509Data.getX509Certificates().add(xmlCert);
} catch (CertificateEncodingException e) {
throw new SecurityException("Error generating X509Certificate element "
+ "from a certificate in credential's certificate chain", e);
}
}
}
}
示例6: onHandleIntent
import java.security.cert.CertificateEncodingException; //導入依賴的package包/類
@Override
protected void onHandleIntent(Intent intent) {
android.os.Process.setThreadPriority(android.os.Process.THREAD_PRIORITY_LOWEST);
if (intent == null || !ACTION_PARSE_APP.equals(intent.getAction())) {
Utils.debugLog(TAG, "received bad Intent: " + intent);
return;
}
try {
PackageManager pm = getPackageManager();
String packageName = intent.getData().getSchemeSpecificPart();
App app = new App(this, pm, packageName);
SwapService.putAppInCache(packageName, app);
} catch (CertificateEncodingException | IOException | PackageManager.NameNotFoundException e) {
e.printStackTrace();
}
}
示例7: getSigningKey
import java.security.cert.CertificateEncodingException; //導入依賴的package包/類
/**
* Returns the private key signature on JBMR2+ or else null.
*/
public static String getSigningKey(String alias) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) {
Certificate cert = getPrivateKeyEntry(alias).getCertificate();
if (cert == null) {
return null;
}
try {
return Base64.encodeToString(cert.getEncoded(), Base64.NO_WRAP);
} catch (CertificateEncodingException e) {
e.printStackTrace();
return null;
}
} else {
return null;
}
}
示例8: getSubjectX509Principal
import java.security.cert.CertificateEncodingException; //導入依賴的package包/類
/**
* return the subject of the given cert as an X509PrincipalObject.
*/
public static X509Principal getSubjectX509Principal(
X509Certificate cert)
throws CertificateEncodingException
{
try
{
ByteArrayInputStream bIn = new ByteArrayInputStream(
cert.getTBSCertificate());
ASN1InputStream aIn = new ASN1InputStream(bIn);
TBSCertificateStructure tbsCert = new TBSCertificateStructure(
(ASN1Sequence)aIn.readObject());
return new X509Principal(tbsCert.getSubject());
}
catch (IOException e)
{
throw new CertificateEncodingException(e.toString());
}
}
示例9: calculateHashCode
import java.security.cert.CertificateEncodingException; //導入依賴的package包/類
private int calculateHashCode()
{
try
{
int hashCode = 0;
byte[] certData = this.getEncoded();
for (int i = 1; i < certData.length; i++)
{
hashCode += certData[i] * i;
}
return hashCode;
}
catch (CertificateEncodingException e)
{
return 0;
}
}
示例10: extractFromCertificate
import java.security.cert.CertificateEncodingException; //導入依賴的package包/類
/**
* Extract the private key a client certificate from a X509 certificate and write them to disk.
*
* @param certificatCredential Jenkins certificateCredential
* @param clientCrtFile path where to write of the certificate
* @param clientKeyFile path where to write of the private key
* @throws IOException lol
* @throws InterruptedException on file operation
*/
public static void extractFromCertificate(StandardCertificateCredentials certificatCredential,
FilePath clientCrtFile,
FilePath clientKeyFile) throws IOException, InterruptedException {
try {
KeyStore keyStore = certificatCredential.getKeyStore();
String alias = keyStore.aliases().nextElement();
X509Certificate certificate = (X509Certificate) keyStore.getCertificate(alias);
// Get private key using passphrase
Key key = keyStore.getKey(alias, Secret.toString(certificatCredential.getPassword()).toCharArray());
// Write certificate
String encodedClientCrt = wrapCertificate(Base64.encodeBase64String(certificate.getEncoded()));
clientCrtFile.write(encodedClientCrt, null);
// Write private key
String encodedClientKey = wrapPrivateKey(Base64.encodeBase64String(key.getEncoded()));
clientKeyFile.write(encodedClientKey, null);
} catch (KeyStoreException | UnrecoverableKeyException | NoSuchAlgorithmException | CertificateEncodingException e) {
throw new AbortException(e.getMessage());
}
}
示例11: encodeCertificate
import java.security.cert.CertificateEncodingException; //導入依賴的package包/類
private void encodeCertificate(
Certificate cert,
DataOutputStream dOut)
throws IOException
{
try
{
byte[] cEnc = cert.getEncoded();
dOut.writeUTF(cert.getType());
dOut.writeInt(cEnc.length);
dOut.write(cEnc);
}
catch (CertificateEncodingException ex)
{
throw new IOException(ex.toString());
}
}
示例12: getCertificateFingerPrint
import java.security.cert.CertificateEncodingException; //導入依賴的package包/類
/**
* Gets the requested finger print of the certificate.
*/
private static String getCertificateFingerPrint(String mdAlg,
X509Certificate cert) {
String fingerPrint = "";
try {
byte[] encCertInfo = cert.getEncoded();
MessageDigest md = MessageDigest.getInstance(mdAlg);
byte[] digest = md.digest(encCertInfo);
StringBuffer buf = new StringBuffer();
for (int i = 0; i < digest.length; i++) {
byte2hex(digest[i], buf);
}
fingerPrint = buf.toString();
} catch (NoSuchAlgorithmException | CertificateEncodingException e) {
// ignored
}
return fingerPrint;
}
示例13: test
import java.security.cert.CertificateEncodingException; //導入依賴的package包/類
@Test
public void test() {
byte[] encoded = MathUtils.toByteArray(1, 100);
String base64Pin = BaseEncodingUtils.encodeBase64ToString(digester.digest(encoded));
MockX509Certificate cert = new MockX509Certificate(encoded);
CertificatePin pin = new CertificatePin(base64Pin, digester);
try {
boolean result = pin.verify(cert);
Assert.assertTrue(result);
} catch (CertificateEncodingException ex) {
Assert.fail(ex.getMessage());
}
}
示例14: generateCertificate
import java.security.cert.CertificateEncodingException; //導入依賴的package包/類
/**
* Create a self-signed X.509 Certificate.
*
* @param dn the X.509 Distinguished Name, eg "CN=Test, L=London, C=GB"
* @param pair the KeyPair
* @param days how many days from now the Certificate is valid for
* @param algorithm the signing algorithm, eg "SHA1withRSA"
* @return the self-signed certificate
*/
public static X509Certificate generateCertificate(String dn, KeyPair pair, int days, String algorithm)
throws CertificateEncodingException, InvalidKeyException, IllegalStateException,
NoSuchProviderException, NoSuchAlgorithmException, SignatureException {
Date from = new Date();
Date to = new Date(from.getTime() + days * 86400000l);
BigInteger sn = new BigInteger(64, new SecureRandom());
KeyPair keyPair = pair;
X509V1CertificateGenerator certGen = new X509V1CertificateGenerator();
X500Principal dnName = new X500Principal(dn);
certGen.setSerialNumber(sn);
certGen.setIssuerDN(dnName);
certGen.setNotBefore(from);
certGen.setNotAfter(to);
certGen.setSubjectDN(dnName);
certGen.setPublicKey(keyPair.getPublic());
certGen.setSignatureAlgorithm(algorithm);
X509Certificate cert = certGen.generate(pair.getPrivate());
return cert;
}
示例15: sign
import java.security.cert.CertificateEncodingException; //導入依賴的package包/類
/**
* Signs the provided APK using JAR signing (aka v1 signature scheme) and returns the list of
* JAR entries which need to be added to the APK as part of the signature.
*
* @param signerConfigs signer configurations, one for each signer. At least one signer config
* must be provided.
*
* @throws InvalidKeyException if a signing key is not suitable for this signature scheme or
* cannot be used in general
* @throws SignatureException if an error occurs when computing digests of generating
* signatures
*/
public static List<Pair<String, byte[]>> sign(
List<SignerConfig> signerConfigs,
DigestAlgorithm jarEntryDigestAlgorithm,
Map<String, byte[]> jarEntryDigests,
List<Integer> apkSigningSchemeIds,
byte[] sourceManifestBytes)
throws InvalidKeyException, CertificateEncodingException, SignatureException {
if (signerConfigs.isEmpty()) {
throw new IllegalArgumentException("At least one signer config must be provided");
}
OutputManifestFile manifest =
generateManifestFile(jarEntryDigestAlgorithm, jarEntryDigests, sourceManifestBytes);
return signManifest(signerConfigs, jarEntryDigestAlgorithm, apkSigningSchemeIds, manifest);
}