Adam Langley | fd74a83 | 2009-10-21 17:53:50 -0700 | [diff] [blame] | 1 | // Copyright 2009 The Go Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style |
| 3 | // license that can be found in the LICENSE file. |
| 4 | |
| 5 | // This package implements the PEM data encoding, which originated in Privacy |
| 6 | // Enhanced Mail. The most common use of PEM encoding today is in TLS keys and |
| 7 | // certificates. See RFC 1421. |
| 8 | package pem |
| 9 | |
| 10 | import ( |
Robert Griesemer | 1c72959 | 2009-12-15 15:27:16 -0800 | [diff] [blame] | 11 | "bytes" |
| 12 | "encoding/base64" |
Adam Langley | d5841ca | 2010-02-07 15:22:36 -0500 | [diff] [blame] | 13 | "io" |
| 14 | "os" |
Adam Langley | fd74a83 | 2009-10-21 17:53:50 -0700 | [diff] [blame] | 15 | ) |
| 16 | |
| 17 | // A Block represents a PEM encoded structure. |
| 18 | // |
| 19 | // The encoded form is: |
| 20 | // -----BEGIN Type----- |
| 21 | // Headers |
| 22 | // base64-encoded Bytes |
| 23 | // -----END Type----- |
| 24 | // where Headers is a possibly empty sequence of Key: Value lines. |
| 25 | type Block struct { |
Robert Griesemer | 1c72959 | 2009-12-15 15:27:16 -0800 | [diff] [blame] | 26 | Type string // The type, taken from the preamble (i.e. "RSA PRIVATE KEY"). |
| 27 | Headers map[string]string // Optional headers. |
| 28 | Bytes []byte // The decoded bytes of the contents. Typically a DER encoded ASN.1 structure. |
Adam Langley | fd74a83 | 2009-10-21 17:53:50 -0700 | [diff] [blame] | 29 | } |
| 30 | |
| 31 | // getLine results the first \r\n or \n delineated line from the given byte |
| 32 | // array. The line does not include the \r\n or \n. The remainder of the byte |
| 33 | // array (also not including the new line bytes) is also returned and this will |
| 34 | // always be smaller than the original argument. |
| 35 | func getLine(data []byte) (line, rest []byte) { |
Robert Griesemer | 1c72959 | 2009-12-15 15:27:16 -0800 | [diff] [blame] | 36 | i := bytes.Index(data, []byte{'\n'}) |
| 37 | var j int |
Adam Langley | fd74a83 | 2009-10-21 17:53:50 -0700 | [diff] [blame] | 38 | if i < 0 { |
Robert Griesemer | 1c72959 | 2009-12-15 15:27:16 -0800 | [diff] [blame] | 39 | i = len(data) |
| 40 | j = i |
Adam Langley | fd74a83 | 2009-10-21 17:53:50 -0700 | [diff] [blame] | 41 | } else { |
Robert Griesemer | 1c72959 | 2009-12-15 15:27:16 -0800 | [diff] [blame] | 42 | j = i + 1 |
Adam Langley | fd74a83 | 2009-10-21 17:53:50 -0700 | [diff] [blame] | 43 | if i > 0 && data[i-1] == '\r' { |
Robert Griesemer | 40621d5 | 2009-11-09 12:07:39 -0800 | [diff] [blame] | 44 | i-- |
Adam Langley | fd74a83 | 2009-10-21 17:53:50 -0700 | [diff] [blame] | 45 | } |
| 46 | } |
Robert Griesemer | 1c72959 | 2009-12-15 15:27:16 -0800 | [diff] [blame] | 47 | return data[0:i], data[j:] |
Adam Langley | fd74a83 | 2009-10-21 17:53:50 -0700 | [diff] [blame] | 48 | } |
| 49 | |
| 50 | // removeWhitespace returns a copy of its input with all spaces, tab and |
| 51 | // newline characters removed. |
| 52 | func removeWhitespace(data []byte) []byte { |
Robert Griesemer | 1c72959 | 2009-12-15 15:27:16 -0800 | [diff] [blame] | 53 | result := make([]byte, len(data)) |
| 54 | n := 0 |
Adam Langley | fd74a83 | 2009-10-21 17:53:50 -0700 | [diff] [blame] | 55 | |
| 56 | for _, b := range data { |
| 57 | if b == ' ' || b == '\t' || b == '\r' || b == '\n' { |
Robert Griesemer | 40621d5 | 2009-11-09 12:07:39 -0800 | [diff] [blame] | 58 | continue |
Adam Langley | fd74a83 | 2009-10-21 17:53:50 -0700 | [diff] [blame] | 59 | } |
Robert Griesemer | 1c72959 | 2009-12-15 15:27:16 -0800 | [diff] [blame] | 60 | result[n] = b |
| 61 | n++ |
Adam Langley | fd74a83 | 2009-10-21 17:53:50 -0700 | [diff] [blame] | 62 | } |
| 63 | |
Robert Griesemer | 1c72959 | 2009-12-15 15:27:16 -0800 | [diff] [blame] | 64 | return result[0:n] |
Adam Langley | fd74a83 | 2009-10-21 17:53:50 -0700 | [diff] [blame] | 65 | } |
| 66 | |
Russ Cox | 9750adb | 2010-02-25 16:01:29 -0800 | [diff] [blame^] | 67 | var pemStart = []byte("\n-----BEGIN ") |
| 68 | var pemEnd = []byte("\n-----END ") |
| 69 | var pemEndOfLine = []byte("-----") |
Adam Langley | fd74a83 | 2009-10-21 17:53:50 -0700 | [diff] [blame] | 70 | |
| 71 | // Decode will find the next PEM formatted block (certificate, private key |
| 72 | // etc) in the input. It returns that block and the remainder of the input. If |
Adam Langley | 7d68093 | 2009-10-21 19:47:52 -0700 | [diff] [blame] | 73 | // no PEM data is found, p is nil and the whole of the input is returned in |
| 74 | // rest. |
Adam Langley | fd74a83 | 2009-10-21 17:53:50 -0700 | [diff] [blame] | 75 | func Decode(data []byte) (p *Block, rest []byte) { |
| 76 | // pemStart begins with a newline. However, at the very beginning of |
| 77 | // the byte array, we'll accept the start string without it. |
Robert Griesemer | 1c72959 | 2009-12-15 15:27:16 -0800 | [diff] [blame] | 78 | rest = data |
Russ Cox | 9ac4449 | 2009-11-20 11:45:05 -0800 | [diff] [blame] | 79 | if bytes.HasPrefix(data, pemStart[1:]) { |
Robert Griesemer | 40621d5 | 2009-11-09 12:07:39 -0800 | [diff] [blame] | 80 | rest = rest[len(pemStart)-1 : len(data)] |
Adam Langley | fd74a83 | 2009-10-21 17:53:50 -0700 | [diff] [blame] | 81 | } else if i := bytes.Index(data, pemStart); i >= 0 { |
Robert Griesemer | 40621d5 | 2009-11-09 12:07:39 -0800 | [diff] [blame] | 82 | rest = rest[i+len(pemStart) : len(data)] |
Adam Langley | fd74a83 | 2009-10-21 17:53:50 -0700 | [diff] [blame] | 83 | } else { |
Robert Griesemer | 40621d5 | 2009-11-09 12:07:39 -0800 | [diff] [blame] | 84 | return nil, data |
Adam Langley | fd74a83 | 2009-10-21 17:53:50 -0700 | [diff] [blame] | 85 | } |
| 86 | |
Robert Griesemer | 1c72959 | 2009-12-15 15:27:16 -0800 | [diff] [blame] | 87 | typeLine, rest := getLine(rest) |
Adam Langley | fd74a83 | 2009-10-21 17:53:50 -0700 | [diff] [blame] | 88 | if !bytes.HasSuffix(typeLine, pemEndOfLine) { |
Robert Griesemer | 40621d5 | 2009-11-09 12:07:39 -0800 | [diff] [blame] | 89 | goto Error |
Adam Langley | fd74a83 | 2009-10-21 17:53:50 -0700 | [diff] [blame] | 90 | } |
Robert Griesemer | 1c72959 | 2009-12-15 15:27:16 -0800 | [diff] [blame] | 91 | typeLine = typeLine[0 : len(typeLine)-len(pemEndOfLine)] |
Adam Langley | fd74a83 | 2009-10-21 17:53:50 -0700 | [diff] [blame] | 92 | |
| 93 | p = &Block{ |
| 94 | Headers: make(map[string]string), |
| 95 | Type: string(typeLine), |
Robert Griesemer | 1c72959 | 2009-12-15 15:27:16 -0800 | [diff] [blame] | 96 | } |
Adam Langley | fd74a83 | 2009-10-21 17:53:50 -0700 | [diff] [blame] | 97 | |
| 98 | for { |
| 99 | // This loop terminates because getLine's second result is |
| 100 | // always smaller than it's argument. |
| 101 | if len(rest) == 0 { |
Robert Griesemer | 40621d5 | 2009-11-09 12:07:39 -0800 | [diff] [blame] | 102 | return nil, data |
Adam Langley | fd74a83 | 2009-10-21 17:53:50 -0700 | [diff] [blame] | 103 | } |
Robert Griesemer | 1c72959 | 2009-12-15 15:27:16 -0800 | [diff] [blame] | 104 | line, next := getLine(rest) |
Adam Langley | fd74a83 | 2009-10-21 17:53:50 -0700 | [diff] [blame] | 105 | |
Robert Griesemer | 1c72959 | 2009-12-15 15:27:16 -0800 | [diff] [blame] | 106 | i := bytes.Index(line, []byte{':'}) |
Adam Langley | fd74a83 | 2009-10-21 17:53:50 -0700 | [diff] [blame] | 107 | if i == -1 { |
Robert Griesemer | 40621d5 | 2009-11-09 12:07:39 -0800 | [diff] [blame] | 108 | break |
Adam Langley | fd74a83 | 2009-10-21 17:53:50 -0700 | [diff] [blame] | 109 | } |
| 110 | |
| 111 | // TODO(agl): need to cope with values that spread across lines. |
Robert Griesemer | 1c72959 | 2009-12-15 15:27:16 -0800 | [diff] [blame] | 112 | key, val := line[0:i], line[i+1:] |
| 113 | key = bytes.TrimSpace(key) |
| 114 | val = bytes.TrimSpace(val) |
| 115 | p.Headers[string(key)] = string(val) |
| 116 | rest = next |
Adam Langley | fd74a83 | 2009-10-21 17:53:50 -0700 | [diff] [blame] | 117 | } |
| 118 | |
Robert Griesemer | 1c72959 | 2009-12-15 15:27:16 -0800 | [diff] [blame] | 119 | i := bytes.Index(rest, pemEnd) |
Adam Langley | fd74a83 | 2009-10-21 17:53:50 -0700 | [diff] [blame] | 120 | if i < 0 { |
Robert Griesemer | 40621d5 | 2009-11-09 12:07:39 -0800 | [diff] [blame] | 121 | goto Error |
Adam Langley | fd74a83 | 2009-10-21 17:53:50 -0700 | [diff] [blame] | 122 | } |
Robert Griesemer | 1c72959 | 2009-12-15 15:27:16 -0800 | [diff] [blame] | 123 | base64Data := removeWhitespace(rest[0:i]) |
Adam Langley | fd74a83 | 2009-10-21 17:53:50 -0700 | [diff] [blame] | 124 | |
Robert Griesemer | 1c72959 | 2009-12-15 15:27:16 -0800 | [diff] [blame] | 125 | p.Bytes = make([]byte, base64.StdEncoding.DecodedLen(len(base64Data))) |
| 126 | n, err := base64.StdEncoding.Decode(p.Bytes, base64Data) |
Adam Langley | fd74a83 | 2009-10-21 17:53:50 -0700 | [diff] [blame] | 127 | if err != nil { |
Robert Griesemer | 40621d5 | 2009-11-09 12:07:39 -0800 | [diff] [blame] | 128 | goto Error |
Adam Langley | fd74a83 | 2009-10-21 17:53:50 -0700 | [diff] [blame] | 129 | } |
Robert Griesemer | 1c72959 | 2009-12-15 15:27:16 -0800 | [diff] [blame] | 130 | p.Bytes = p.Bytes[0:n] |
Adam Langley | fd74a83 | 2009-10-21 17:53:50 -0700 | [diff] [blame] | 131 | |
Robert Griesemer | 1c72959 | 2009-12-15 15:27:16 -0800 | [diff] [blame] | 132 | _, rest = getLine(rest[i+len(pemEnd):]) |
Adam Langley | fd74a83 | 2009-10-21 17:53:50 -0700 | [diff] [blame] | 133 | |
Robert Griesemer | 1c72959 | 2009-12-15 15:27:16 -0800 | [diff] [blame] | 134 | return |
Adam Langley | fd74a83 | 2009-10-21 17:53:50 -0700 | [diff] [blame] | 135 | |
| 136 | Error: |
| 137 | // If we get here then we have rejected a likely looking, but |
| 138 | // ultimately invalid PEM block. We need to start over from a new |
| 139 | // position. We have consumed the preamble line and will have consumed |
| 140 | // any lines which could be header lines. However, a valid preamble |
| 141 | // line is not a valid header line, therefore we cannot have consumed |
| 142 | // the preamble line for the any subsequent block. Thus, we will always |
| 143 | // find any valid block, no matter what bytes preceed it. |
| 144 | // |
| 145 | // For example, if the input is |
| 146 | // |
| 147 | // -----BEGIN MALFORMED BLOCK----- |
| 148 | // junk that may look like header lines |
| 149 | // or data lines, but no END line |
| 150 | // |
| 151 | // -----BEGIN ACTUAL BLOCK----- |
| 152 | // realdata |
| 153 | // -----END ACTUAL BLOCK----- |
| 154 | // |
| 155 | // we've failed to parse using the first BEGIN line |
| 156 | // and now will try again, using the second BEGIN line. |
Robert Griesemer | 1c72959 | 2009-12-15 15:27:16 -0800 | [diff] [blame] | 157 | p, rest = Decode(rest) |
Adam Langley | fd74a83 | 2009-10-21 17:53:50 -0700 | [diff] [blame] | 158 | if p == nil { |
Robert Griesemer | 40621d5 | 2009-11-09 12:07:39 -0800 | [diff] [blame] | 159 | rest = data |
Adam Langley | fd74a83 | 2009-10-21 17:53:50 -0700 | [diff] [blame] | 160 | } |
Robert Griesemer | 1c72959 | 2009-12-15 15:27:16 -0800 | [diff] [blame] | 161 | return |
Adam Langley | fd74a83 | 2009-10-21 17:53:50 -0700 | [diff] [blame] | 162 | } |
Adam Langley | d5841ca | 2010-02-07 15:22:36 -0500 | [diff] [blame] | 163 | |
| 164 | const pemLineLength = 64 |
| 165 | |
| 166 | type lineBreaker struct { |
| 167 | line [pemLineLength]byte |
| 168 | used int |
| 169 | out io.Writer |
| 170 | } |
| 171 | |
| 172 | func (l *lineBreaker) Write(b []byte) (n int, err os.Error) { |
| 173 | if l.used+len(b) < pemLineLength { |
| 174 | copy(l.line[l.used:], b) |
| 175 | l.used += len(b) |
| 176 | return len(b), nil |
| 177 | } |
| 178 | |
| 179 | n, err = l.out.Write(l.line[0:l.used]) |
| 180 | if err != nil { |
| 181 | return |
| 182 | } |
| 183 | excess := pemLineLength - l.used |
| 184 | l.used = 0 |
| 185 | |
| 186 | n, err = l.out.Write(b[0:excess]) |
| 187 | if err != nil { |
| 188 | return |
| 189 | } |
| 190 | |
| 191 | n, err = l.out.Write([]byte{'\n'}) |
| 192 | if err != nil { |
| 193 | return |
| 194 | } |
| 195 | |
| 196 | return l.Write(b[excess:]) |
| 197 | } |
| 198 | |
| 199 | func (l *lineBreaker) Close() (err os.Error) { |
| 200 | if l.used > 0 { |
| 201 | _, err = l.out.Write(l.line[0:l.used]) |
| 202 | if err != nil { |
| 203 | return |
| 204 | } |
| 205 | _, err = l.out.Write([]byte{'\n'}) |
| 206 | } |
| 207 | |
| 208 | return |
| 209 | } |
| 210 | |
| 211 | func Encode(out io.Writer, b *Block) (err os.Error) { |
| 212 | _, err = out.Write(pemStart[1:]) |
| 213 | if err != nil { |
| 214 | return |
| 215 | } |
Russ Cox | 9750adb | 2010-02-25 16:01:29 -0800 | [diff] [blame^] | 216 | _, err = out.Write([]byte(b.Type + "-----\n")) |
Adam Langley | d5841ca | 2010-02-07 15:22:36 -0500 | [diff] [blame] | 217 | if err != nil { |
| 218 | return |
| 219 | } |
| 220 | |
| 221 | for k, v := range b.Headers { |
Russ Cox | 9750adb | 2010-02-25 16:01:29 -0800 | [diff] [blame^] | 222 | _, err = out.Write([]byte(k + ": " + v + "\n")) |
Adam Langley | d5841ca | 2010-02-07 15:22:36 -0500 | [diff] [blame] | 223 | if err != nil { |
| 224 | return |
| 225 | } |
| 226 | } |
| 227 | |
| 228 | if len(b.Headers) > 1 { |
| 229 | _, err = out.Write([]byte{'\n'}) |
| 230 | if err != nil { |
| 231 | return |
| 232 | } |
| 233 | } |
| 234 | |
| 235 | var breaker lineBreaker |
| 236 | breaker.out = out |
| 237 | |
| 238 | b64 := base64.NewEncoder(base64.StdEncoding, &breaker) |
| 239 | _, err = b64.Write(b.Bytes) |
| 240 | if err != nil { |
| 241 | return |
| 242 | } |
| 243 | b64.Close() |
| 244 | breaker.Close() |
| 245 | |
| 246 | _, err = out.Write(pemEnd[1:]) |
| 247 | if err != nil { |
| 248 | return |
| 249 | } |
Russ Cox | 9750adb | 2010-02-25 16:01:29 -0800 | [diff] [blame^] | 250 | _, err = out.Write([]byte(b.Type + "-----\n")) |
Adam Langley | d5841ca | 2010-02-07 15:22:36 -0500 | [diff] [blame] | 251 | return |
| 252 | } |
| 253 | |
| 254 | func EncodeToMemory(b *Block) []byte { |
| 255 | buf := bytes.NewBuffer(nil) |
| 256 | Encode(buf, b) |
| 257 | return buf.Bytes() |
| 258 | } |