+
+ public User getActor() {
+ return actor;
+ }
+
+ public static Certificate locateCertificate(String serial, String certData) throws GigiApiException {
+ if (serial != null && !serial.isEmpty()) {
+ return getBySerial(normalizeSerial(serial));
+ }
+
+ if (certData != null && !certData.isEmpty()) {
+ final byte[] supplied;
+ final X509Certificate c0;
+ try {
+ supplied = PEM.decode("CERTIFICATE", certData);
+ c0 = (X509Certificate) CertificateFactory.getInstance("X509").generateCertificate(new ByteArrayInputStream(supplied));
+ } catch (IllegalArgumentException e1) {
+ throw new GigiApiException(NOT_PARSED);
+ } catch (CertificateException e1) {
+ throw new GigiApiException(NOT_PARSED);
+ }
+ try {
+ Certificate c = getBySerial(c0.getSerialNumber());
+ if (c == null) {
+ return null;
+ }
+ X509Certificate cert = c.cert();
+ if ( !Arrays.equals(supplied, cert.getEncoded())) {
+ return null;
+ }
+ return c;
+ } catch (IOException e) {
+ throw new GigiApiException(NOT_LOADED);
+ } catch (GeneralSecurityException e) {
+ throw new GigiApiException(NOT_LOADED);
+ }
+ }
+ throw new GigiApiException("No information to identify the correct certificate was provided.");
+ }
+
+ public static BigInteger normalizeSerial(String serial) throws GigiApiException {
+ serial = serial.replace(" ", "");
+ serial = serial.toLowerCase();
+ if (serial.matches("[0-9a-f]{2}(:[0-9a-f]{2})*")) {
+ serial = serial.replace(":", "");
+ }
+ int idx = 0;
+ while (idx < serial.length() && serial.charAt(idx) == '0') {
+ idx++;
+ }
+ serial = serial.substring(idx);
+ if ( !serial.matches("[0-9a-f]+")) {
+ throw new GigiApiException("Malformed serial");
+ }
+ return new BigInteger(serial, 16);
+ }
+
+ public String getFingerprint(String algorithm) throws IOException, GeneralSecurityException, GigiApiException {
+ X509Certificate certx = cert();
+ return getFingerprint(certx, algorithm);
+ }
+
+ private static String getFingerprint(X509Certificate cert, String algorithm) throws NoSuchAlgorithmException, CertificateEncodingException {
+ MessageDigest md = MessageDigest.getInstance(algorithm);
+ byte[] der = cert.getEncoded();
+ md.update(der);
+ byte[] digest = md.digest();
+ String digestHex = DatatypeConverter.printHexBinary(digest);
+ return digestHex.toLowerCase();
+ }