private void addCertTLV(Integer type, X509Certificate cert) { byte[] certDer = null; try { certDer = cert.getEncoded(); } catch (CertificateEncodingException e) { log.error("Failed to add DER TLV type "+type); e.printStackTrace(); } this.addTLV(new TLV (type, certDer, certDer.length)); }
public String certificateString() { if (this.chain == null) return null; StringBuffer sb = new StringBuffer(); for (X509Certificate cert : this.chain) { try { sb.append(CERT_BEGIN); sb.append(NEW_LINE); byte[] bytes = cert.getEncoded(); sb.append(Base64.encodeLines64(bytes)); sb.append(CERT_END); sb.append(NEW_LINE); } catch (CertificateEncodingException e) { e.printStackTrace(); throw new RuntimeException("Cannot encode X509Certificate to byte[].",e); } } sb.deleteCharAt(sb.length()-1); // remove the last new line return sb.toString(); }
public HashMap<String,String> getCertificateInfo(X509Certificate cert){ HashMap<String,String> certInfo = new HashMap<String,String>(); X500Name x500name; try { x500name = new JcaX509CertificateHolder(cert).getSubject(); } catch (CertificateEncodingException e) { e.printStackTrace(); return certInfo; } certInfo.put(CertManagerConstants.SUBJECT_NAME,cert.getSubjectDN().getName()); certInfo.put(CertManagerConstants.ISSUER_NAME,cert.getIssuerDN().getName()); certInfo.put(CertManagerConstants.SN, cert.getSerialNumber().toString()); RDN cn = x500name.getRDNs(BCStyle.CN)[0]; certInfo.put(CertManagerConstants.CN,cn.toString()); RDN ou = x500name.getRDNs(BCStyle.OU)[0]; certInfo.put(CertManagerConstants.OU,ou.toString()); certInfo.put(CertManagerConstants.START_DATE,cert.getNotBefore().toString()); certInfo.put(CertManagerConstants.EXPIRY_DATE,cert.getNotAfter().toString()); return certInfo; }
/** * 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; } }
} catch (CertificateEncodingException e) { setStatus("Problem with certificate encoding. (" + e.getMessage() + ")"); e.printStackTrace(); } catch (InvalidKeyException e) { setStatus("Invalid Key.");
@Override public ASN1Set getValue() { SigningCertificate attribute = (SigningCertificate) super.getAttribute(); X509Certificate cert = attribute.getValue(); Digest digest = DigestFactory.getInstance().factoryDefault(); digest.setAlgorithm(DigestAlgorithmEnum.SHA_1); byte[] certHash = null; try { certHash = digest.digest(cert.getEncoded()); } catch (CertificateEncodingException ex) { ex.printStackTrace(); } X509Name dirName = new X509Name(cert.getSubjectDN().getName()); GeneralName name = new GeneralName(dirName); GeneralNames issuer = new GeneralNames(name); DERInteger serialNumber = new DERInteger(cert.getSerialNumber()); IssuerSerial issuerSerial = new IssuerSerial(issuer, serialNumber); ESSCertID essCertId = new ESSCertID(certHash, issuerSerial); return new DERSet(new DERSequence(new ASN1Encodable[]{new DERSequence(essCertId), new DERSequence(new DERNull())})); } }
@Override public ASN1Set getValue() { SigningCertificateV2 attribute = (SigningCertificateV2) super.getAttribute(); X509Certificate cert = attribute.getValue(); Digest digest = DigestFactory.getInstance().factoryDefault(); digest.setAlgorithm(DigestAlgorithmEnum.SHA_256); byte[] certHash = null; try { certHash = digest.digest(cert.getEncoded()); } catch (CertificateEncodingException ex) { ex.printStackTrace(); } X509Name dirName = new X509Name(cert.getSubjectDN().getName()); GeneralName name = new GeneralName(dirName); GeneralNames issuer = new GeneralNames(name); DERInteger serial = new DERInteger(cert.getSerialNumber()); IssuerSerial issuerSerial = new IssuerSerial(issuer, serial); String algorithmHashOID = SignerAlgorithmEnum.getSignerAlgorithmEnum(attribute.getAlgorithmHash()).getOIDAlgorithmHash(); AlgorithmIdentifier algorithmId = new AlgorithmIdentifier(algorithmHashOID); ESSCertIDv2 essCertIDv2 = new ESSCertIDv2(algorithmId, certHash, issuerSerial); return new DERSet(new DERSequence(new ASN1Encodable[]{new DERSequence(essCertIDv2), new DERSequence(new DERNull())})); } }
} catch (CertificateEncodingException e) { setStatus("Problem with certificate encoding. (" + e.getMessage() + ")"); e.printStackTrace(); } catch (InvalidKeyException e) { setStatus("Invalid Key. (" + e.getMessage() + ")");