Return-Path: X-Original-To: archive-asf-public-internal@cust-asf2.ponee.io Delivered-To: archive-asf-public-internal@cust-asf2.ponee.io Received: from cust-asf.ponee.io (cust-asf.ponee.io [163.172.22.183]) by cust-asf2.ponee.io (Postfix) with ESMTP id 0502C200B29 for ; Thu, 30 Jun 2016 10:53:54 +0200 (CEST) Received: by cust-asf.ponee.io (Postfix) id 03BA3160A51; Thu, 30 Jun 2016 08:53:54 +0000 (UTC) Delivered-To: archive-asf-public@cust-asf.ponee.io Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by cust-asf.ponee.io (Postfix) with SMTP id 3403B160A8A for ; Thu, 30 Jun 2016 10:53:51 +0200 (CEST) Received: (qmail 98005 invoked by uid 500); 30 Jun 2016 08:53:49 -0000 Mailing-List: contact commits-help@directory.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@directory.apache.org Delivered-To: mailing list commits@directory.apache.org Received: (qmail 96429 invoked by uid 99); 30 Jun 2016 08:53:49 -0000 Received: from git1-us-west.apache.org (HELO git1-us-west.apache.org) (140.211.11.23) by apache.org (qpsmtpd/0.29) with ESMTP; Thu, 30 Jun 2016 08:53:49 +0000 Received: by git1-us-west.apache.org (ASF Mail Server at git1-us-west.apache.org, from userid 33) id 2B4E2E95BA; Thu, 30 Jun 2016 08:53:49 +0000 (UTC) Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: drankye@apache.org To: commits@directory.apache.org Date: Thu, 30 Jun 2016 08:54:25 -0000 Message-Id: In-Reply-To: <19c327db7623444a91b9a5859944fa8f@git.apache.org> References: <19c327db7623444a91b9a5859944fa8f@git.apache.org> X-Mailer: ASF-Git Admin Mailer Subject: [38/44] directory-kerby git commit: DIRKRB-588 - Support validation keys in different formats Note: Introducing a Commons IO dependency as part of this patch archived-at: Thu, 30 Jun 2016 08:53:54 -0000 DIRKRB-588 - Support validation keys in different formats Note: Introducing a Commons IO dependency as part of this patch Project: http://git-wip-us.apache.org/repos/asf/directory-kerby/repo Commit: http://git-wip-us.apache.org/repos/asf/directory-kerby/commit/641a3cca Tree: http://git-wip-us.apache.org/repos/asf/directory-kerby/tree/641a3cca Diff: http://git-wip-us.apache.org/repos/asf/directory-kerby/diff/641a3cca Branch: refs/heads/gssapi Commit: 641a3cca8284c7a892942bd6a5ce09b78bc4265d Parents: 8aae076 Author: Colm O hEigeartaigh Authored: Thu Jun 16 10:35:04 2016 +0100 Committer: Colm O hEigeartaigh Committed: Thu Jun 16 10:35:04 2016 +0100 ---------------------------------------------------------------------- kerby-kerb/kerb-common/pom.xml | 6 ++ .../kerberos/kerb/common/PublicKeyReader.java | 60 +++++++++----------- pom.xml | 1 + 3 files changed, 35 insertions(+), 32 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/directory-kerby/blob/641a3cca/kerby-kerb/kerb-common/pom.xml ---------------------------------------------------------------------- diff --git a/kerby-kerb/kerb-common/pom.xml b/kerby-kerb/kerb-common/pom.xml index 2272c96..779c391 100644 --- a/kerby-kerb/kerb-common/pom.xml +++ b/kerby-kerb/kerb-common/pom.xml @@ -36,5 +36,11 @@ kerb-crypto ${project.version} + + commons-io + commons-io + ${commons-io.version} + compile + http://git-wip-us.apache.org/repos/asf/directory-kerby/blob/641a3cca/kerby-kerb/kerb-common/src/main/java/org/apache/kerby/kerberos/kerb/common/PublicKeyReader.java ---------------------------------------------------------------------- diff --git a/kerby-kerb/kerb-common/src/main/java/org/apache/kerby/kerberos/kerb/common/PublicKeyReader.java b/kerby-kerb/kerb-common/src/main/java/org/apache/kerby/kerberos/kerb/common/PublicKeyReader.java index 49b2012..988d259 100644 --- a/kerby-kerb/kerb-common/src/main/java/org/apache/kerby/kerberos/kerb/common/PublicKeyReader.java +++ b/kerby-kerb/kerb-common/src/main/java/org/apache/kerby/kerberos/kerb/common/PublicKeyReader.java @@ -19,56 +19,52 @@ */ package org.apache.kerby.kerberos.kerb.common; -import org.apache.kerby.util.Base64; - -import java.io.BufferedReader; -import java.io.IOException; +import java.io.ByteArrayInputStream; import java.io.InputStream; -import java.io.InputStreamReader; -import java.nio.charset.StandardCharsets; import java.security.KeyFactory; -import java.security.NoSuchAlgorithmException; import java.security.PublicKey; +import java.security.cert.Certificate; +import java.security.cert.CertificateFactory; import java.security.spec.InvalidKeySpecException; import java.security.spec.X509EncodedKeySpec; +import org.apache.commons.io.IOUtils; +import org.apache.kerby.util.Base64; + public class PublicKeyReader { public static PublicKey loadPublicKey(InputStream in) throws Exception { + byte[] keyBytes = IOUtils.toByteArray(in); + try { - BufferedReader br = new BufferedReader(new InputStreamReader(in, StandardCharsets.UTF_8)); - String readLine = null; - StringBuilder sb = new StringBuilder(); - while ((readLine = br.readLine()) != null) { - if (readLine.charAt(0) == '-') { - continue; - } else { - sb.append(readLine); - sb.append('\r'); - } - } - return loadPublicKey(sb.toString()); - } catch (IOException e) { - throw e; - } catch (NullPointerException e) { - throw e; + return loadPublicKey(keyBytes); + } catch (InvalidKeySpecException ex) { + // It might be a Certificate and not a PublicKey... + Certificate cert = + CertificateFactory.getInstance("X.509").generateCertificate(new ByteArrayInputStream(keyBytes)); + return cert.getPublicKey(); } } - public static PublicKey loadPublicKey(String publicKeyStr) throws Exception { - try { + public static PublicKey loadPublicKey(byte[] publicKeyBytes) throws Exception { + String pubKey = new String(publicKeyBytes, "UTF-8"); + if (pubKey.startsWith("-----BEGIN PUBLIC KEY-----")) { + // PEM format + pubKey = pubKey.replace("-----BEGIN PUBLIC KEY-----", ""); + pubKey = pubKey.replace("-----END PUBLIC KEY-----", ""); + Base64 base64 = new Base64(); - byte[] buffer = base64.decode(publicKeyStr); + byte[] buffer = base64.decode(pubKey.trim()); + KeyFactory keyFactory = KeyFactory.getInstance("RSA"); X509EncodedKeySpec keySpec = new X509EncodedKeySpec(buffer); return keyFactory.generatePublic(keySpec); - } catch (NoSuchAlgorithmException e) { - throw e; - } catch (InvalidKeySpecException e) { - throw e; - } catch (NullPointerException e) { - throw e; + } else { + // DER format + KeyFactory keyFactory = KeyFactory.getInstance("RSA"); + X509EncodedKeySpec keySpec = new X509EncodedKeySpec(publicKeyBytes); + return keyFactory.generatePublic(keySpec); } } http://git-wip-us.apache.org/repos/asf/directory-kerby/blob/641a3cca/pom.xml ---------------------------------------------------------------------- diff --git a/pom.xml b/pom.xml index 7e6967f..3aeef2a 100644 --- a/pom.xml +++ b/pom.xml @@ -49,6 +49,7 @@ 2.0.0-M21 1.54 + 2.5 2.6.2 1.0.0-M33 1.2.17