Coverage Report - eu.emi.security.authn.x509.helpers.FlexiblePEMReader
 
Classes in this File Line Coverage Branch Coverage Complexity
FlexiblePEMReader
95%
40/42
77%
14/18
7
 
 1  
 /*
 2  
  * Copyright (c) 2011 ICM Uniwersytet Warszawski All rights reserved.
 3  
  * See LICENCE file for licensing information.
 4  
  */
 5  
 package eu.emi.security.authn.x509.helpers;
 6  
 
 7  
 import java.io.IOException;
 8  
 import java.io.Reader;
 9  
 import java.util.ArrayList;
 10  
 import java.util.List;
 11  
 import java.util.regex.Matcher;
 12  
 import java.util.regex.Pattern;
 13  
 
 14  
 import org.bouncycastle.openssl.PEMParser;
 15  
 import org.bouncycastle.util.encoders.Base64;
 16  
 import org.bouncycastle.util.io.pem.PemHeader;
 17  
 import org.bouncycastle.util.io.pem.PemObject;
 18  
 
 19  
 
 20  
 /**
 21  
  * Extends BC's {@link PEMReader} class so it can read correctly also 
 22  
  * PEM files with a garbage at the beginning 
 23  
  * and minor syntax violations which occur more then often in the wild. 
 24  
  *
 25  
  * TODO - probably we can remove this class - BC seems to be fixed.
 26  
  *
 27  
  * @author K. Benedyczak
 28  
  */
 29  
 public class FlexiblePEMReader extends PEMParser
 30  
 {
 31  
         /**
 32  
          * Creates a new {@link FlexiblePEMReader} object. 
 33  
          * @param reader input source
 34  
          */
 35  
         public FlexiblePEMReader(Reader reader)
 36  
         {
 37  5903
                 super(reader);
 38  5903
         }
 39  
 
 40  
         /**
 41  
          * Generate BC's PemObject
 42  
          * @return the parsed PEM object
 43  
          * @throws IOException
 44  
          */
 45  
         @Override
 46  
         public PemObject readPemObject() throws IOException
 47  
         {
 48  6578
                 Pattern starter = Pattern.compile("^---[-]+BEGIN [^-]+---[-]+$");
 49  6578
                 Pattern end = Pattern.compile("^---[-]+END [^-]+---[-]+$");
 50  
                 
 51  
                 String line;
 52  6578
                 boolean startFound = false;
 53  70289
                 while ((line=readLine()) != null)
 54  
                 {
 55  70069
                         Matcher m = starter.matcher(line);
 56  70069
                         if (m.find())
 57  
                         {
 58  6358
                                 startFound = true;
 59  6358
                                 break;
 60  
                         }
 61  63711
                 }
 62  6578
                 if (!startFound)
 63  220
                         return null;
 64  
                 
 65  6358
                 int pos = line.indexOf("BEGIN ") + 6;
 66  6358
                 int endPos = line.indexOf('-', pos);
 67  6358
                 String type = line.substring(pos, endPos);
 68  
                 
 69  6358
                 boolean endFound = false;
 70  6358
                 StringBuilder sb = new StringBuilder();
 71  6358
                 List<PemHeader> headers = new ArrayList<PemHeader>();
 72  6372
                 while ((line=readLine()) != null)
 73  
                 {
 74  6372
                         Matcher m = end.matcher(line);
 75  6372
                         if (m.find())
 76  0
                                 throw new IOException("The supplied data is not in PEM format, end line found before getting any contents.");
 77  6372
                         if (line.indexOf(":") >= 0)
 78  
                         {
 79  14
                                 int index = line.indexOf(':');
 80  14
                                 String hdr = line.substring(0, index);
 81  14
                                 String value = line.substring(index + 1).trim();
 82  14
                                 headers.add(new PemHeader(hdr, value));
 83  14
                         } else 
 84  
                         {
 85  6358
                                 sb.append(line.trim());
 86  6358
                                 break;
 87  
                         }
 88  14
                 }
 89  
                 
 90  124262
                 while ((line=readLine()) != null)
 91  
                 {
 92  124262
                         Matcher m = end.matcher(line);
 93  124262
                         if (m.find())
 94  
                         {
 95  6358
                                 endFound = true;
 96  6358
                                 break;
 97  
                         } else 
 98  117904
                                 sb.append(line.trim());
 99  117904
                 }
 100  6358
                 if (!endFound)
 101  0
                         throw new IOException("The supplied data is not in PEM format, no ending line found.");
 102  
 
 103  6358
                 return new PemObject(type, headers, Base64.decode(sb.toString()));
 104  
         }
 105  
 
 106  
 }