1
0
mirror of https://github.com/golang/go synced 2024-11-20 09:44:45 -07:00

encoding/pem: write Proc-Type header first.

See RFC 1421, section 4.6.1.1

R=agl, agl
CC=golang-dev
https://golang.org/cl/6814104
This commit is contained in:
Roger Peppe 2012-11-12 15:29:17 +00:00
parent 89ed40c44b
commit 1e1733a9ac
2 changed files with 100 additions and 67 deletions

View File

@ -11,6 +11,7 @@ import (
"bytes" "bytes"
"encoding/base64" "encoding/base64"
"io" "io"
"sort"
) )
// A Block represents a PEM encoded structure. // A Block represents a PEM encoded structure.
@ -209,26 +210,46 @@ func (l *lineBreaker) Close() (err error) {
return return
} }
func Encode(out io.Writer, b *Block) (err error) { func writeHeader(out io.Writer, k, v string) error {
_, err = out.Write(pemStart[1:]) _, err := out.Write([]byte(k + ": " + v + "\n"))
if err != nil { return err
return }
func Encode(out io.Writer, b *Block) error {
if _, err := out.Write(pemStart[1:]); err != nil {
return err
} }
_, err = out.Write([]byte(b.Type + "-----\n")) if _, err := out.Write([]byte(b.Type + "-----\n")); err != nil {
if err != nil { return err
return
} }
if len(b.Headers) > 0 { if len(b.Headers) > 0 {
for k, v := range b.Headers { const procType = "Proc-Type"
_, err = out.Write([]byte(k + ": " + v + "\n")) h := make([]string, 0, len(b.Headers))
if err != nil { hasProcType := false
return for k := range b.Headers {
if k == procType {
hasProcType = true
continue
}
h = append(h, k)
}
// The Proc-Type header must be written first.
// See RFC 1421, section 4.6.1.1
if hasProcType {
if err := writeHeader(out, procType, b.Headers[procType]); err != nil {
return err
} }
} }
_, err = out.Write([]byte{'\n'}) // For consistency of output, write other headers sorted by key.
if err != nil { sort.Strings(h)
return for _, k := range h {
if err := writeHeader(out, k, b.Headers[k]); err != nil {
return err
}
}
if _, err := out.Write([]byte{'\n'}); err != nil {
return err
} }
} }
@ -236,19 +257,17 @@ func Encode(out io.Writer, b *Block) (err error) {
breaker.out = out breaker.out = out
b64 := base64.NewEncoder(base64.StdEncoding, &breaker) b64 := base64.NewEncoder(base64.StdEncoding, &breaker)
_, err = b64.Write(b.Bytes) if _, err := b64.Write(b.Bytes); err != nil {
if err != nil { return err
return
} }
b64.Close() b64.Close()
breaker.Close() breaker.Close()
_, err = out.Write(pemEnd[1:]) if _, err := out.Write(pemEnd[1:]); err != nil {
if err != nil { return err
return
} }
_, err = out.Write([]byte(b.Type + "-----\n")) _, err := out.Write([]byte(b.Type + "-----\n"))
return return err
} }
func EncodeToMemory(b *Block) []byte { func EncodeToMemory(b *Block) []byte {

View File

@ -43,7 +43,7 @@ func TestDecode(t *testing.T) {
if !reflect.DeepEqual(result, privateKey) { if !reflect.DeepEqual(result, privateKey) {
t.Errorf("#1 got:%#v want:%#v", result, privateKey) t.Errorf("#1 got:%#v want:%#v", result, privateKey)
} }
result, _ = Decode([]byte(pemPrivateKey)) result, _ = Decode([]byte(pemPrivateKey2))
if !reflect.DeepEqual(result, privateKey2) { if !reflect.DeepEqual(result, privateKey2) {
t.Errorf("#2 got:%#v want:%#v", result, privateKey2) t.Errorf("#2 got:%#v want:%#v", result, privateKey2)
} }
@ -51,8 +51,8 @@ func TestDecode(t *testing.T) {
func TestEncode(t *testing.T) { func TestEncode(t *testing.T) {
r := EncodeToMemory(privateKey2) r := EncodeToMemory(privateKey2)
if string(r) != pemPrivateKey { if string(r) != pemPrivateKey2 {
t.Errorf("got:%s want:%s", r, pemPrivateKey) t.Errorf("got:%s want:%s", r, pemPrivateKey2)
} }
} }
@ -341,50 +341,64 @@ var privateKey = &Block{Type: "RSA PRIVATE KEY",
}, },
} }
var privateKey2 = &Block{Type: "RSA PRIVATE KEY", var privateKey2 = &Block{
Headers: map[string]string{}, Type: "RSA PRIVATE KEY",
Bytes: []uint8{0x30, 0x82, 0x1, 0x3a, 0x2, 0x1, 0x0, 0x2, Headers: map[string]string{
0x41, 0x0, 0xb2, 0x99, 0xf, 0x49, 0xc4, 0x7d, 0xfa, 0x8c, "Proc-Type": "4,ENCRYPTED",
0xd4, 0x0, 0xae, 0x6a, 0x4d, 0x1b, 0x8a, 0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x80, 0xef, 0x3c,
0xce, 0xcd, 0xf5, 0x90, 0x9e, 0xf9, 0xa7, 0xb2, 0xc,
0x49, 0x4, 0xf1, 0x9, 0x8f, 0xea, 0x63, 0xd2, 0x70,
0xbb, 0x86, 0xbf, 0x34, 0xab, 0xb2, 0x3, 0xb1, 0x59,
0x33, 0x16, 0x17, 0xb0, 0xdb, 0x77, 0x38, 0xf4, 0xb4,
0x94, 0xb, 0x25, 0x16, 0x7e, 0x22, 0xd4, 0xf9, 0x22,
0xb9, 0x78, 0xa3, 0x4, 0x84, 0x4, 0xd2, 0xda, 0x84,
0x2d, 0x63, 0xdd, 0xf8, 0x50, 0x6a, 0xf6, 0xe3, 0xf5,
0x65, 0x40, 0x7c, 0xa9,
}, },
} }
var pemPrivateKey = `-----BEGIN RSA PRIVATE KEY----- var pemPrivateKey2 = `-----BEGIN RSA PRIVATE KEY-----
MIIBOgIBAAJBALKZD0nEffqM1ACuak0bijtqE2QrI/KLADv7l3kK3ppMyCuLKoF0 Proc-Type: 4,ENCRYPTED
fd7Ai2KW5ToIwzFofvJcS/STa6HA5gQenRUCAwEAAQJBAIq9amn00aS0h/CrjXqu Content-Domain: RFC822
/ThglAXJmZhOMPVn4eiu7/ROixi9sex436MaVeMqSNf7Ex9a8fRNfWss7Sqd9eWu DEK-Info: AES-128-CBC,BFCD243FEDBB40A4AA6DDAA1335473A4
RTUCIQDasvGASLqmjeffBNLTXV2A5g4t+kLVCpsEIZAycV5GswIhANEPLmax0ME/
EO+ZJ79TJKN5yiGBRsv5yvx5UiHxajEXAiAhAol5N4EUyq6I9w1rYdhPMGpLfk7A qDXMK7nLIavAnXZhAPSBrWnSwEJBO+Q8r1lebSo8nKGkXmg3xIxwHKkY5sIripHc
IU2snfRJ6Nq2CQIgFrPsWRCkV+gOYcajD17rEqmuLrdIRexpg8N1DOSXoJ8CIGlS LR8IIznxS4sbL0YLsia6T0CAOcSxyzu0ZT8bsvcI0sbVqJ8jabY9+awcsxOHZAQ3
tAboUGBxTDq3ZroNism3DaMIbKPyYrAqhKov1h5V 20DIggzQ+CF83L0JBCAWsJfibVYd4+zw/OJWraQDcG1jPAG+Pig4b8Dm/YXRU6ib
y9QEsXO5czLWesYpJaXaF5N6EOhB+6UXIPhO6eOPUSATu963k64TivYJ9KZB4CtR
GjA4DbE7Z4dk9coyZ9HIpT0jcsQGr497Jqw8dZGhABPGXEnVPOeyspng1SX64hKA
N4XPksobn/NO2IDvPM7N9ZCe+aeyDEkE8QmP6mPScLuGvzSrsgOxWTMWF7Dbdzj0
tJQLJRZ+ItT5Irl4owSEBNLahC1j3fhQavbj9WVAfKk=
-----END RSA PRIVATE KEY----- -----END RSA PRIVATE KEY-----
` `