| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
package x509andr |
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
import ( |
| 12 |
"crypto/aes" |
| 13 |
"crypto/cipher" |
| 14 |
"crypto/des" |
| 15 |
"crypto/md5" |
| 16 |
"encoding/hex" |
| 17 |
"encoding/pem" |
| 18 |
"errors" |
| 19 |
"io" |
| 20 |
"strings" |
| 21 |
) |
| 22 |
|
| 23 |
type PEMCipher int |
| 24 |
|
| 25 |
|
| 26 |
const ( |
| 27 |
_ PEMCipher = iota |
| 28 |
PEMCipherDES |
| 29 |
PEMCipher3DES |
| 30 |
PEMCipherAES128 |
| 31 |
PEMCipherAES192 |
| 32 |
PEMCipherAES256 |
| 33 |
) |
| 34 |
|
| 35 |
|
| 36 |
type rfc1423Algo struct { |
| 37 |
cipher PEMCipher |
| 38 |
name string |
| 39 |
cipherFunc func(key []byte) (cipher.Block, error) |
| 40 |
keySize int |
| 41 |
blockSize int |
| 42 |
} |
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
var rfc1423Algos = []rfc1423Algo{{ |
| 47 |
cipher: PEMCipherDES, |
| 48 |
name: "DES-CBC", |
| 49 |
cipherFunc: des.NewCipher, |
| 50 |
keySize: 8, |
| 51 |
blockSize: des.BlockSize, |
| 52 |
}, { |
| 53 |
cipher: PEMCipher3DES, |
| 54 |
name: "DES-EDE3-CBC", |
| 55 |
cipherFunc: des.NewTripleDESCipher, |
| 56 |
keySize: 24, |
| 57 |
blockSize: des.BlockSize, |
| 58 |
}, { |
| 59 |
cipher: PEMCipherAES128, |
| 60 |
name: "AES-128-CBC", |
| 61 |
cipherFunc: aes.NewCipher, |
| 62 |
keySize: 16, |
| 63 |
blockSize: aes.BlockSize, |
| 64 |
}, { |
| 65 |
cipher: PEMCipherAES192, |
| 66 |
name: "AES-192-CBC", |
| 67 |
cipherFunc: aes.NewCipher, |
| 68 |
keySize: 24, |
| 69 |
blockSize: aes.BlockSize, |
| 70 |
}, { |
| 71 |
cipher: PEMCipherAES256, |
| 72 |
name: "AES-256-CBC", |
| 73 |
cipherFunc: aes.NewCipher, |
| 74 |
keySize: 32, |
| 75 |
blockSize: aes.BlockSize, |
| 76 |
}, |
| 77 |
} |
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
func (c rfc1423Algo) deriveKey(password, salt []byte) []byte { |
| 83 |
hash := md5.New() |
| 84 |
out := make([]byte, c.keySize) |
| 85 |
var digest []byte |
| 86 |
|
| 87 |
for i := 0; i < len(out); i += len(digest) { |
| 88 |
hash.Reset() |
| 89 |
hash.Write(digest) |
| 90 |
hash.Write(password) |
| 91 |
hash.Write(salt) |
| 92 |
digest = hash.Sum(digest[:0]) |
| 93 |
copy(out[i:], digest) |
| 94 |
} |
| 95 |
return out |
| 96 |
} |
| 97 |
|
| 98 |
|
| 99 |
func IsEncryptedPEMBlock(b *pem.Block) bool { |
| 100 |
_, ok := b.Headers["DEK-Info"] |
| 101 |
return ok |
| 102 |
} |
| 103 |
|
| 104 |
|
| 105 |
var IncorrectPasswordError = errors.New("x509: decryption password incorrect") |
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
func DecryptPEMBlock(b *pem.Block, password []byte) ([]byte, error) { |
| 116 |
dek, ok := b.Headers["DEK-Info"] |
| 117 |
if !ok { |
| 118 |
return nil, errors.New("x509: no DEK-Info header in block") |
| 119 |
} |
| 120 |
|
| 121 |
idx := strings.Index(dek, ",") |
| 122 |
if idx == -1 { |
| 123 |
return nil, errors.New("x509: malformed DEK-Info header") |
| 124 |
} |
| 125 |
|
| 126 |
mode, hexIV := dek[:idx], dek[idx+1:] |
| 127 |
ciph := cipherByName(mode) |
| 128 |
if ciph == nil { |
| 129 |
return nil, errors.New("x509: unknown encryption mode") |
| 130 |
} |
| 131 |
iv, err := hex.DecodeString(hexIV) |
| 132 |
if err != nil { |
| 133 |
return nil, err |
| 134 |
} |
| 135 |
if len(iv) != ciph.blockSize { |
| 136 |
return nil, errors.New("x509: incorrect IV size") |
| 137 |
} |
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
key := ciph.deriveKey(password, iv[:8]) |
| 142 |
block, err := ciph.cipherFunc(key) |
| 143 |
if err != nil { |
| 144 |
return nil, err |
| 145 |
} |
| 146 |
|
| 147 |
if len(b.Bytes)%block.BlockSize() != 0 { |
| 148 |
return nil, errors.New("x509: encrypted PEM data is not a multiple of the block size") |
| 149 |
} |
| 150 |
|
| 151 |
data := make([]byte, len(b.Bytes)) |
| 152 |
dec := cipher.NewCBCDecrypter(block, iv) |
| 153 |
dec.CryptBlocks(data, b.Bytes) |
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
dlen := len(data) |
| 162 |
if dlen == 0 || dlen%ciph.blockSize != 0 { |
| 163 |
return nil, errors.New("x509: invalid padding") |
| 164 |
} |
| 165 |
last := int(data[dlen-1]) |
| 166 |
if dlen < last { |
| 167 |
return nil, IncorrectPasswordError |
| 168 |
} |
| 169 |
if last == 0 || last > ciph.blockSize { |
| 170 |
return nil, IncorrectPasswordError |
| 171 |
} |
| 172 |
for _, val := range data[dlen-last:] { |
| 173 |
if int(val) != last { |
| 174 |
return nil, IncorrectPasswordError |
| 175 |
} |
| 176 |
} |
| 177 |
return data[:dlen-last], nil |
| 178 |
} |
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
func EncryptPEMBlock(rand io.Reader, blockType string, data, password []byte, alg PEMCipher) (*pem.Block, error) { |
| 184 |
ciph := cipherByKey(alg) |
| 185 |
if ciph == nil { |
| 186 |
return nil, errors.New("x509: unknown encryption mode") |
| 187 |
} |
| 188 |
iv := make([]byte, ciph.blockSize) |
| 189 |
if _, err := io.ReadFull(rand, iv); err != nil { |
| 190 |
return nil, errors.New("x509: cannot generate IV: " + err.Error()) |
| 191 |
} |
| 192 |
|
| 193 |
|
| 194 |
key := ciph.deriveKey(password, iv[:8]) |
| 195 |
block, err := ciph.cipherFunc(key) |
| 196 |
if err != nil { |
| 197 |
return nil, err |
| 198 |
} |
| 199 |
enc := cipher.NewCBCEncrypter(block, iv) |
| 200 |
pad := ciph.blockSize - len(data)%ciph.blockSize |
| 201 |
encrypted := make([]byte, len(data), len(data)+pad) |
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
copy(encrypted, data) |
| 206 |
|
| 207 |
for i := 0; i < pad; i++ { |
| 208 |
encrypted = append(encrypted, byte(pad)) |
| 209 |
} |
| 210 |
enc.CryptBlocks(encrypted, encrypted) |
| 211 |
|
| 212 |
return &pem.Block{ |
| 213 |
Type: blockType, |
| 214 |
Headers: map[string]string{ |
| 215 |
"Proc-Type": "4,ENCRYPTED", |
| 216 |
"DEK-Info": ciph.name + "," + hex.EncodeToString(iv), |
| 217 |
}, |
| 218 |
Bytes: encrypted, |
| 219 |
}, nil |
| 220 |
} |
| 221 |
|
| 222 |
func cipherByName(name string) *rfc1423Algo { |
| 223 |
for i := range rfc1423Algos { |
| 224 |
alg := &rfc1423Algos[i] |
| 225 |
if alg.name == name { |
| 226 |
return alg |
| 227 |
} |
| 228 |
} |
| 229 |
return nil |
| 230 |
} |
| 231 |
|
| 232 |
func cipherByKey(key PEMCipher) *rfc1423Algo { |
| 233 |
for i := range rfc1423Algos { |
| 234 |
alg := &rfc1423Algos[i] |
| 235 |
if alg.cipher == key { |
| 236 |
return alg |
| 237 |
} |
| 238 |
} |
| 239 |
return nil |
| 240 |
} |