Skip to content

Commit

Permalink
Initial commit.
Browse files Browse the repository at this point in the history
  • Loading branch information
grantae committed May 30, 2016
0 parents commit 3574c6e
Show file tree
Hide file tree
Showing 23 changed files with 1,329 additions and 0 deletions.
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2016 Grant Ayers

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
94 changes: 94 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
# Certificate Information for Go

A golang tool for printing x509 TLS certificates in a format similar to OpenSSL.

## Installation

``` bash
go get github.com/grantae/certinfo
```

## Usage

### Print a certificate from a website

``` go
package main

import (
"crypto/tls"
"fmt"
"github.com/grantae/certinfo"
"log"
)

func main() {
// Connect to google.com
cfg := tls.Config{}
conn, err := tls.Dial("tcp", "google.com:443", &cfg)
if err != nil {
log.Fatalln("TLS connection failed: " + err.Error())
}
// Grab the last certificate in the chain
certChain := conn.ConnectionState().PeerCertificates
cert := certChain[len(certChain)-1]

// Print the certificate
result, err := certinfo.CertificateText(cert)
if err != nil {
log.Fatal(err)
}
fmt.Print(result)
}
```

### Print a PEM-encoded certificate from a file

``` go
package main

import (
"crypto/x509"
"encoding/pem"
"fmt"
"github.com/grantae/certinfo"
"io/ioutil"
"log"
)

func main() {
// Read and parse the PEM certificate file
pemData, err := ioutil.ReadFile("cert.pem")
if err != nil {
log.Fatal(err)
}
block, rest := pem.Decode([]byte(pemData))
if block == nil || len(rest) > 0 {
log.Fatal("Certificate decoding error")
}
cert, err := x509.ParseCertificate(block.Bytes)
if err != nil {
log.Fatal(err)
}

// Print the certificate
result, err := certinfo.CertificateText(cert)
if err != nil {
log.Fatal(err)
}
fmt.Print(result)
}
```

## Testing

``` bash
go test github.com/grantae/certinfo
```

This compares several PEM-encoded certificates with their expected outputs.

## License

MIT -- see `LICENSE` for more information.

Loading

0 comments on commit 3574c6e

Please sign in to comment.