|
| 1 | +// Licensed to Elasticsearch B.V. under one or more contributor |
| 2 | +// license agreements. See the NOTICE file distributed with |
| 3 | +// this work for additional information regarding copyright |
| 4 | +// ownership. Elasticsearch B.V. licenses this file to you under |
| 5 | +// the Apache License, Version 2.0 (the "License"); you may |
| 6 | +// not use this file except in compliance with the License. |
| 7 | +// You may obtain a copy of the License at |
| 8 | +// |
| 9 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +// |
| 11 | +// Unless required by applicable law or agreed to in writing, |
| 12 | +// software distributed under the License is distributed on an |
| 13 | +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +// KIND, either express or implied. See the License for the |
| 15 | +// specific language governing permissions and limitations |
| 16 | +// under the License. |
| 17 | + |
| 18 | +//go:build !requirefips |
| 19 | + |
| 20 | +package tlscommon |
| 21 | + |
| 22 | +import ( |
| 23 | + "crypto/ecdsa" |
| 24 | + "crypto/rsa" |
| 25 | + "crypto/x509" |
| 26 | + "encoding/pem" |
| 27 | + "errors" |
| 28 | + "fmt" |
| 29 | + |
| 30 | + "github.com/elastic/pkcs8" |
| 31 | +) |
| 32 | + |
| 33 | +func decryptPKCS1Key(block pem.Block, passphrase []byte) (pem.Block, error) { |
| 34 | + if len(passphrase) == 0 { |
| 35 | + return block, errors.New("no passphrase available") |
| 36 | + } |
| 37 | + |
| 38 | + // Note, decrypting pem might succeed even with wrong password, but |
| 39 | + // only noise will be stored in buffer in this case. |
| 40 | + buffer, err := x509.DecryptPEMBlock(&block, passphrase) //nolint: staticcheck // deprecated, we have to get rid of it |
| 41 | + if err != nil { |
| 42 | + return block, fmt.Errorf("failed to decrypt pem: %w", err) |
| 43 | + } |
| 44 | + |
| 45 | + // DEK-Info contains encryption info. Remove header to mark block as |
| 46 | + // unencrypted. |
| 47 | + delete(block.Headers, "DEK-Info") |
| 48 | + block.Bytes = buffer |
| 49 | + |
| 50 | + return block, nil |
| 51 | +} |
| 52 | + |
| 53 | +func decryptPKCS8Key(block pem.Block, passphrase []byte) (pem.Block, error) { |
| 54 | + if len(passphrase) == 0 { |
| 55 | + return block, errors.New("no passphrase available") |
| 56 | + } |
| 57 | + |
| 58 | + key, err := pkcs8.ParsePKCS8PrivateKey(block.Bytes, passphrase) |
| 59 | + if err != nil { |
| 60 | + return block, fmt.Errorf("failed to parse key: %w", err) |
| 61 | + } |
| 62 | + |
| 63 | + switch key.(type) { |
| 64 | + case *rsa.PrivateKey: |
| 65 | + block.Type = "RSA PRIVATE KEY" |
| 66 | + case *ecdsa.PrivateKey: |
| 67 | + block.Type = "ECDSA PRIVATE KEY" |
| 68 | + default: |
| 69 | + return block, fmt.Errorf("unknown key type %T", key) |
| 70 | + } |
| 71 | + |
| 72 | + buffer, err := x509.MarshalPKCS8PrivateKey(key) |
| 73 | + if err != nil { |
| 74 | + return block, fmt.Errorf("failed to marshal decrypted private key: %w", err) |
| 75 | + } |
| 76 | + block.Bytes = buffer |
| 77 | + |
| 78 | + return block, nil |
| 79 | +} |
0 commit comments