| /* |
| * Licensed to the Apache Software Foundation (ASF) under one or more |
| * contributor license agreements. See the NOTICE file distributed with |
| * this work for additional information regarding copyright ownership. |
| * The ASF licenses this file to You under the Apache License, Version 2.0 |
| * (the "License"); you may not use this file except in compliance with |
| * the License. You may obtain a copy of the License at |
| * |
| * http://www.apache.org/licenses/LICENSE-2.0 |
| * |
| * Unless required by applicable law or agreed to in writing, software |
| * distributed under the License is distributed on an "AS IS" BASIS, |
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| * See the License for the specific language governing permissions and |
| * limitations under the License. |
| */ |
| |
| package tls |
| |
| import ( |
| "crypto" |
| "crypto/ecdsa" |
| "crypto/ed25519" |
| "crypto/rsa" |
| "crypto/x509" |
| "errors" |
| ) |
| |
| // ParsePrivateKey parses an ASN.1 DER-encoded private key. This is |
| // basically what tls.X509KeyPair does internally. |
| func ParsePrivateKey(data []byte) (crypto.PrivateKey, error) { |
| if key, err := x509.ParsePKCS1PrivateKey(data); err == nil { |
| return key, nil |
| } |
| |
| if key, err := x509.ParseECPrivateKey(data); err == nil { |
| return key, nil |
| } |
| |
| if key, err := x509.ParsePKCS8PrivateKey(data); err == nil { |
| switch key := key.(type) { |
| case *rsa.PrivateKey, *ecdsa.PrivateKey, ed25519.PrivateKey: |
| return key, nil |
| } |
| } |
| |
| return nil, errors.New("failed to parse private key") |
| } |