github.com/zmap/zlint@v1.1.0/lints/testingUtil.go (about)

     1  package lints
     2  
     3  /*
     4   * ZLint Copyright 2018 Regents of the University of Michigan
     5   *
     6   * Licensed under the Apache License, Version 2.0 (the "License"); you may not
     7   * use this file except in compliance with the License. You may obtain a copy
     8   * of the License at http://www.apache.org/licenses/LICENSE-2.0
     9   *
    10   * Unless required by applicable law or agreed to in writing, software
    11   * distributed under the License is distributed on an "AS IS" BASIS,
    12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
    13   * implied. See the License for the specific language governing
    14   * permissions and limitations under the License.
    15   */
    16  
    17  // Contains resources necessary to the Unit Test Cases
    18  
    19  import (
    20  	"encoding/pem"
    21  	"fmt"
    22  	"io/ioutil"
    23  	"strings"
    24  
    25  	"github.com/zmap/zcrypto/x509"
    26  )
    27  
    28  func ReadCertificate(inPath string) *x509.Certificate {
    29  	// All of this can be encapsulated in a function
    30  	data, err := ioutil.ReadFile(inPath)
    31  	if err != nil {
    32  		//read failure, die horribly here
    33  		fmt.Println(err)
    34  		panic("File read failed!")
    35  	}
    36  	var textData string = string(data)
    37  	if strings.Contains(textData, "-BEGIN CERTIFICATE-") {
    38  		block, _ := pem.Decode(data)
    39  		if block == nil {
    40  			panic("PEM decode failed!")
    41  		}
    42  		data = block.Bytes
    43  	}
    44  	theCert, err := x509.ParseCertificate(data)
    45  	if err != nil {
    46  		//die horribly here
    47  		fmt.Println(err)
    48  		return nil
    49  	}
    50  	return theCert
    51  }