aboutsummaryrefslogtreecommitdiff
path: root/private_key.go
blob: 07ec3f86b19e616b9171da5821767bed5fa13ca3 (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
package main

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

const (
  TypeLabelRSA   = "RSA PRIVATE KEY"
  TypeLabelECDSA = "EC PRIVATE KEY"
)

var (
  ErrNoPKFound = errors.New("no private key found")
)

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