aboutsummaryrefslogtreecommitdiff
path: root/private_key.go
blob: 0591e1874f6cb215ea9f0c8738301d0b67dbb1b2 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
package main

import (
  "errors"
  "github.com/gibheer/pki"
)

var (
  ErrNoPKFound = errors.New("no private key found")
  ErrNoPUFound = errors.New("no public key found")
  ErrUnknownFormat = errors.New("key is an unknown format")
)

// Read the private key from the path and try to figure out which type of key it
// might be.
func ReadPrivateKeyFile(path string) (pki.PrivateKey, error) {
  raw_pk, err := readSectionFromFile(path, pki.PemLabelEcdsa)
  if err == nil {
    pk, err := pki.LoadPrivateKeyEcdsa(raw_pk)
    if err != nil { return nil, err }
    return pk, nil
  }
  raw_pk, err = readSectionFromFile(path, pki.PemLabelRsa)
  if err == nil {
    pk, err := pki.LoadPrivateKeyRsa(raw_pk)
    if err != nil { return nil, err }
    return pk, nil
  }
  return nil, ErrNoPKFound
}

// read the public key and try to figure out what kind of key it might be
func ReadPublicKeyFile(path string) (pki.PublicKey, error) {
  raw_pu, err := readSectionFromFile(path, pki.PemLabelPublic)
  if err != nil { return nil, ErrNoPUFound }

  var public pki.PublicKey
  public, err = pki.LoadPublicKeyEcdsa(raw_pu)
  if err == nil { return public, nil }
  public, err = pki.LoadPublicKeyRsa(raw_pu)
  if err == nil { return public, nil }
  return nil, ErrUnknownFormat
}