< prev index next >

src/jdk.incubator.jpackage/macosx/classes/jdk/incubator/jpackage/internal/MacCertificate.java

Print this page




   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package jdk.jpackage.internal;
  27 
  28 import java.io.BufferedOutputStream;
  29 import java.io.BufferedReader;
  30 import java.io.ByteArrayInputStream;
  31 import java.io.ByteArrayOutputStream;
  32 import java.io.File;
  33 import java.io.FileOutputStream;
  34 import java.io.IOException;
  35 import java.io.InputStreamReader;
  36 import java.io.PrintStream;


  37 import java.text.DateFormat;
  38 import java.text.ParseException;
  39 import java.text.SimpleDateFormat;
  40 import java.util.ArrayList;
  41 import java.util.Calendar;
  42 import java.util.Date;
  43 import java.util.List;
  44 import java.util.Locale;
  45 
  46 final class MacCertificate {
  47     private final String certificate;
  48     private final boolean verbose;
  49 
  50     MacCertificate(String certificate) {
  51         this.certificate = certificate;
  52         this.verbose = false;
  53     }
  54 
  55     MacCertificate(String certificate, boolean verbose) {
  56         this.certificate = certificate;
  57         this.verbose = verbose;
  58     }
  59 
  60     boolean isValid() {
  61         return verifyCertificate(this.certificate, verbose);
  62     }
  63 
  64     private static File findCertificate(String certificate, boolean verbose) {
  65         File result = null;
  66 
  67         List<String> args = new ArrayList<>();
  68         args.add("security");
  69         args.add("find-certificate");
  70         args.add("-c");
  71         args.add(certificate);
  72         args.add("-a");
  73         args.add("-p");
  74 
  75         try (ByteArrayOutputStream baos = new ByteArrayOutputStream();
  76                 PrintStream ps = new PrintStream(baos)) {
  77             ProcessBuilder security = new ProcessBuilder(args);
  78             IOUtils.exec(security, verbose, false, ps);
  79 
  80             File output = File.createTempFile("tempfile", ".tmp");
  81             PrintStream p = new PrintStream(
  82                     new BufferedOutputStream(
  83                             new FileOutputStream(output, true)));
  84             BufferedReader bfReader = new BufferedReader(
  85                     new InputStreamReader(
  86                             new ByteArrayInputStream(baos.toByteArray())));
  87             String line = null;
  88 
  89             while((line = bfReader.readLine()) != null){
  90                 p.println(line);
  91             }
  92 
  93             p.close();
  94             result = output;
  95         }
  96         catch (IOException ignored) {}
  97 
  98         return result;
  99     }
 100 
 101     private static Date findCertificateDate(String filename, boolean verbose) {
 102         Date result = null;
 103 
 104         List<String> args = new ArrayList<>();
 105         args.add("/usr/bin/openssl");
 106         args.add("x509");
 107         args.add("-noout");
 108         args.add("-enddate");
 109         args.add("-in");
 110         args.add(filename);
 111 
 112         try (ByteArrayOutputStream baos = new ByteArrayOutputStream();
 113                 PrintStream ps = new PrintStream(baos)) {
 114             ProcessBuilder security = new ProcessBuilder(args);
 115             IOUtils.exec(security, verbose, false, ps);
 116             String output = baos.toString();
 117             output = output.substring(output.indexOf("=") + 1);
 118             DateFormat df = new SimpleDateFormat(
 119                     "MMM dd kk:mm:ss yyyy z", Locale.ENGLISH);
 120             result = df.parse(output);
 121         } catch (IOException | ParseException ex) {
 122             Log.debug(ex);
 123         }
 124 
 125         return result;
 126     }
 127 
 128     private static boolean verifyCertificate(
 129             String certificate, boolean verbose) {
 130         boolean result = false;
 131 
 132         try {
 133             File file = null;
 134             Date certificateDate = null;
 135 
 136             try {
 137                 file = findCertificate(certificate, verbose);
 138 
 139                 if (file != null) {
 140                     certificateDate = findCertificateDate(
 141                             file.getCanonicalPath(), verbose);
 142                 }
 143             }
 144             finally {
 145                 if (file != null) {
 146                     file.delete();
 147                 }
 148             }
 149 
 150             if (certificateDate != null) {
 151                 Calendar c = Calendar.getInstance();
 152                 Date today = c.getTime();
 153 
 154                 if (certificateDate.after(today)) {
 155                     result = true;
 156                 }
 157             }
 158         }
 159         catch (IOException ignored) {}
 160 
 161         return result;


   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package jdk.incubator.jpackage.internal;
  27 


  28 import java.io.ByteArrayInputStream;
  29 import java.io.ByteArrayOutputStream;
  30 import java.io.File;

  31 import java.io.IOException;

  32 import java.io.PrintStream;
  33 import java.nio.file.StandardCopyOption;
  34 import java.nio.file.Files;
  35 import java.text.DateFormat;
  36 import java.text.ParseException;
  37 import java.text.SimpleDateFormat;
  38 import java.util.ArrayList;
  39 import java.util.Calendar;
  40 import java.util.Date;
  41 import java.util.List;
  42 import java.util.Locale;
  43 
  44 public final class MacCertificate {
  45     private final String certificate;

  46 
  47     public MacCertificate(String certificate) {
  48         this.certificate = certificate;

  49     }
  50 
  51     public boolean isValid() {
  52         return verifyCertificate(this.certificate);





  53     }
  54 
  55     private static File findCertificate(String certificate) {
  56         File result = null;
  57 
  58         List<String> args = new ArrayList<>();
  59         args.add("security");
  60         args.add("find-certificate");
  61         args.add("-c");
  62         args.add(certificate);
  63         args.add("-a");
  64         args.add("-p");
  65 
  66         try (ByteArrayOutputStream baos = new ByteArrayOutputStream();
  67                 PrintStream ps = new PrintStream(baos)) {
  68             ProcessBuilder security = new ProcessBuilder(args);
  69             IOUtils.exec(security, false, ps);
  70 
  71             File output = File.createTempFile("tempfile", ".tmp");







  72 
  73             Files.copy(new ByteArrayInputStream(baos.toByteArray()),
  74                     output.toPath(), StandardCopyOption.REPLACE_EXISTING);

  75 

  76             result = output;
  77         }
  78         catch (IOException ignored) {}
  79 
  80         return result;
  81     }
  82 
  83     private static Date findCertificateDate(String filename) {
  84         Date result = null;
  85 
  86         List<String> args = new ArrayList<>();
  87         args.add("/usr/bin/openssl");
  88         args.add("x509");
  89         args.add("-noout");
  90         args.add("-enddate");
  91         args.add("-in");
  92         args.add(filename);
  93 
  94         try (ByteArrayOutputStream baos = new ByteArrayOutputStream();
  95                 PrintStream ps = new PrintStream(baos)) {
  96             ProcessBuilder security = new ProcessBuilder(args);
  97             IOUtils.exec(security, false, ps);
  98             String output = baos.toString();
  99             output = output.substring(output.indexOf("=") + 1);
 100             DateFormat df = new SimpleDateFormat(
 101                     "MMM dd kk:mm:ss yyyy z", Locale.ENGLISH);
 102             result = df.parse(output);
 103         } catch (IOException | ParseException ex) {
 104             Log.verbose(ex);
 105         }
 106 
 107         return result;
 108     }
 109 
 110     private static boolean verifyCertificate(String certificate) {

 111         boolean result = false;
 112 
 113         try {
 114             File file = null;
 115             Date certificateDate = null;
 116 
 117             try {
 118                 file = findCertificate(certificate);
 119 
 120                 if (file != null) {
 121                     certificateDate = findCertificateDate(
 122                             file.getCanonicalPath());
 123                 }
 124             }
 125             finally {
 126                 if (file != null) {
 127                     file.delete();
 128                 }
 129             }
 130 
 131             if (certificateDate != null) {
 132                 Calendar c = Calendar.getInstance();
 133                 Date today = c.getTime();
 134 
 135                 if (certificateDate.after(today)) {
 136                     result = true;
 137                 }
 138             }
 139         }
 140         catch (IOException ignored) {}
 141 
 142         return result;
< prev index next >