go.uber.org/cadence@v1.2.9/internal/common/util/rsa.go (about)

     1  // Copyright (c) 2021 Uber Technologies Inc.
     2  // Portions of the Software are attributed to Copyright (c) 2020 Temporal Technologies Inc.
     3  //
     4  // Permission is hereby granted, free of charge, to any person obtaining a copy
     5  // of this software and associated documentation files (the "Software"), to deal
     6  // in the Software without restriction, including without limitation the rights
     7  // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
     8  // copies of the Software, and to permit persons to whom the Software is
     9  // furnished to do so, subject to the following conditions:
    10  //
    11  // The above copyright notice and this permission notice shall be included in
    12  // all copies or substantial portions of the Software.
    13  //
    14  // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    15  // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    16  // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    17  // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    18  // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    19  // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
    20  // THE SOFTWARE.
    21  
    22  package util
    23  
    24  import (
    25  	"crypto/rsa"
    26  	"crypto/x509"
    27  	"encoding/pem"
    28  	"fmt"
    29  	"strings"
    30  )
    31  
    32  type KeyType string
    33  
    34  const (
    35  	KeyTypePrivate KeyType = "private key"
    36  
    37  	KeyTypePublic KeyType = "public key"
    38  )
    39  
    40  func loadRSAKey(keyString []byte, keyType KeyType) (interface{}, error) {
    41  	block, _ := pem.Decode(keyString)
    42  	if block == nil || strings.ToLower(block.Type) != strings.ToLower(string(keyType)) {
    43  		return nil, fmt.Errorf("failed to parse PEM block containing the %s", keyType)
    44  	}
    45  
    46  	switch keyType {
    47  	case KeyTypePrivate:
    48  		key, err := x509.ParsePKCS8PrivateKey(block.Bytes)
    49  		if err != nil {
    50  			return nil, fmt.Errorf("failed to parse DER encoded %s: %s", keyType, err.Error())
    51  		}
    52  		return key, nil
    53  	case KeyTypePublic:
    54  		key, err := x509.ParsePKIXPublicKey(block.Bytes)
    55  		if err != nil {
    56  			return nil, fmt.Errorf("failed to parse DER encoded %s: %s", keyType, err.Error())
    57  		}
    58  		return key, nil
    59  	default:
    60  		return nil, fmt.Errorf("invalid Key Type")
    61  	}
    62  }
    63  
    64  func LoadRSAPublicKey(key []byte) (*rsa.PublicKey, error) {
    65  	rsaKey, err := loadRSAKey(key, KeyTypePublic)
    66  	if err != nil {
    67  		return nil, err
    68  	}
    69  	return rsaKey.(*rsa.PublicKey), err
    70  }
    71  
    72  func LoadRSAPrivateKey(key []byte) (*rsa.PrivateKey, error) {
    73  	rsaKey, err := loadRSAKey(key, KeyTypePrivate)
    74  	if err != nil {
    75  		return nil, err
    76  	}
    77  	return rsaKey.(*rsa.PrivateKey), err
    78  }