| 1 | // Modified by 42wim |
| 2 | // |
| 3 | // Copyright 2009 The Go Authors. All rights reserved. |
| 4 | // Use of this source code is governed by a BSD-style |
| 5 | // license that can be found in the LICENSE file. |
| 6 | |
| 7 | // Package pem implements the PEM data encoding, which originated in Privacy |
| 8 | // Enhanced Mail. The most common use of PEM encoding today is in TLS keys and |
| 9 | // certificates. See RFC 1421. |
| 10 | package pemscip-go gomod github.com/42wim/sshsig . `github.com/42wim/sshsig/pem`/ |
| 11 | |
| 12 | import ( |
| 13 | "bytesscip-go gomod github.com/golang/go/src go1.23.0 bytes/" |
| 14 | "encoding/base64scip-go gomod github.com/golang/go/src go1.23.0 `encoding/base64`/" |
| 15 | "errorsscip-go gomod github.com/golang/go/src go1.23.0 errors/" |
| 16 | "ioscip-go gomod github.com/golang/go/src go1.23.0 io/" |
| 17 | "sortscip-go gomod github.com/golang/go/src go1.23.0 sort/" |
| 18 | "stringsscip-go gomod github.com/golang/go/src go1.23.0 strings/" |
| 19 | ) |
| 20 | |
| 21 | // A Block represents a PEM encoded structure. |
| 22 | // |
| 23 | // The encoded form is: |
| 24 | // -----BEGIN Type----- |
| 25 | // Headers |
| 26 | // base64-encoded Bytes |
| 27 | // -----END Type----- |
| 28 | // where Headers is a possibly empty sequence of Key: Value lines. |
| 29 | type Blockscip-go gomod github.com/42wim/sshsig . `github.com/42wim/sshsig/pem`/Block# struct { |
| 30 | Typescip-go gomod github.com/42wim/sshsig . `github.com/42wim/sshsig/pem`/Block#Type. string // The type, taken from the preamble (i.e. "RSA PRIVATE KEY"). |
| 31 | Headersscip-go gomod github.com/42wim/sshsig . `github.com/42wim/sshsig/pem`/Block#Headers. map[string]string // Optional headers. |
| 32 | Bytesscip-go gomod github.com/42wim/sshsig . `github.com/42wim/sshsig/pem`/Block#Bytes. []byte // The decoded bytes of the contents. Typically a DER encoded ASN.1 structure. |
| 33 | } |
| 34 | |
| 35 | // getLine results the first \r\n or \n delineated line from the given byte |
| 36 | // array. The line does not include trailing whitespace or the trailing new |
| 37 | // line bytes. The remainder of the byte array (also not including the new line |
| 38 | // bytes) is also returned and this will always be smaller than the original |
| 39 | // argument. |
| 40 | func getLinescip-go gomod github.com/42wim/sshsig . `github.com/42wim/sshsig/pem`/getLine().(data []byte) (line, rest []byte) { |
| 41 | i := bytesscip-go gomod github.com/golang/go/src go1.23.0 bytes/.IndexBytescip-go gomod github.com/golang/go/src go1.23.0 bytes/IndexByte().(data, '\n') |
| 42 | var j int |
| 43 | if i < 0 { |
| 44 | i = len(data) |
| 45 | j = i |
| 46 | } else { |
| 47 | j = i + 1 |
| 48 | if i > 0 && data[i-1] == '\r' { |
| 49 | i-- |
| 50 | } |
| 51 | } |
| 52 | return bytesscip-go gomod github.com/golang/go/src go1.23.0 bytes/.TrimRightscip-go gomod github.com/golang/go/src go1.23.0 bytes/TrimRight().(data[0:i], " \t"), data[j:] |
| 53 | } |
| 54 | |
| 55 | // removeSpacesAndTabs returns a copy of its input with all spaces and tabs |
| 56 | // removed, if there were any. Otherwise, the input is returned unchanged. |
| 57 | // |
| 58 | // The base64 decoder already skips newline characters, so we don't need to |
| 59 | // filter them out here. |
| 60 | func removeSpacesAndTabsscip-go gomod github.com/42wim/sshsig . `github.com/42wim/sshsig/pem`/removeSpacesAndTabs().(data []byte) []byte { |
| 61 | if !bytesscip-go gomod github.com/golang/go/src go1.23.0 bytes/.ContainsAnyscip-go gomod github.com/golang/go/src go1.23.0 bytes/ContainsAny().(data, " \t") { |
| 62 | // Fast path; most base64 data within PEM contains newlines, but |
| 63 | // no spaces nor tabs. Skip the extra alloc and work. |
| 64 | return data |
| 65 | } |
| 66 | result := make([]byte, len(data)) |
| 67 | n := 0 |
| 68 | |
| 69 | for _, b := range data { |
| 70 | if b == ' ' || b == '\t' { |
| 71 | continue |
| 72 | } |
| 73 | result[n] = b |
| 74 | n++ |
| 75 | } |
| 76 | |
| 77 | return result[0:n] |
| 78 | } |
| 79 | |
| 80 | var ( |
| 81 | pemStartscip-go gomod github.com/42wim/sshsig . `github.com/42wim/sshsig/pem`/pemStart. = []byte("\n-----BEGIN ") |
| 82 | pemEndscip-go gomod github.com/42wim/sshsig . `github.com/42wim/sshsig/pem`/pemEnd. = []byte("\n-----END ") |
| 83 | pemEndOfLinescip-go gomod github.com/42wim/sshsig . `github.com/42wim/sshsig/pem`/pemEndOfLine. = []byte("-----") |
| 84 | ) |
| 85 | |
| 86 | // Decode will find the next PEM formatted block (certificate, private key |
| 87 | // etc) in the input. It returns that block and the remainder of the input. If |
| 88 | // no PEM data is found, p is nil and the whole of the input is returned in |
| 89 | // rest. |
| 90 | func Decodescip-go gomod github.com/42wim/sshsig . `github.com/42wim/sshsig/pem`/Decode().(data []byte) (p *Blockscip-go gomod github.com/42wim/sshsig . `github.com/42wim/sshsig/pem`/Block#, rest []byte) { |
| 91 | // pemStart begins with a newline. However, at the very beginning of |
| 92 | // the byte array, we'll accept the start string without it. |
| 93 | rest = data |
| 94 | if bytesscip-go gomod github.com/golang/go/src go1.23.0 bytes/.HasPrefixscip-go gomod github.com/golang/go/src go1.23.0 bytes/HasPrefix().(data, pemStartscip-go gomod github.com/42wim/sshsig . `github.com/42wim/sshsig/pem`/pemStart.[1:]) { |
| 95 | rest = rest[len(pemStartscip-go gomod github.com/42wim/sshsig . `github.com/42wim/sshsig/pem`/pemStart.)-1 : len(data)] |
| 96 | } else if i := bytesscip-go gomod github.com/golang/go/src go1.23.0 bytes/.Indexscip-go gomod github.com/golang/go/src go1.23.0 bytes/Index().(data, pemStartscip-go gomod github.com/42wim/sshsig . `github.com/42wim/sshsig/pem`/pemStart.); i >= 0 { |
| 97 | rest = rest[i+len(pemStartscip-go gomod github.com/42wim/sshsig . `github.com/42wim/sshsig/pem`/pemStart.) : len(data)] |
| 98 | } else { |
| 99 | return nil, data |
| 100 | } |
| 101 | |
| 102 | typeLine, rest := getLinescip-go gomod github.com/42wim/sshsig . `github.com/42wim/sshsig/pem`/getLine().(rest) |
| 103 | if !bytesscip-go gomod github.com/golang/go/src go1.23.0 bytes/.HasSuffixscip-go gomod github.com/golang/go/src go1.23.0 bytes/HasSuffix().(typeLine, pemEndOfLinescip-go gomod github.com/42wim/sshsig . `github.com/42wim/sshsig/pem`/pemEndOfLine.) { |
| 104 | return decodeErrorscip-go gomod github.com/42wim/sshsig . `github.com/42wim/sshsig/pem`/decodeError().(data, rest) |
| 105 | } |
| 106 | typeLine = typeLine[0 : len(typeLine)-len(pemEndOfLinescip-go gomod github.com/42wim/sshsig . `github.com/42wim/sshsig/pem`/pemEndOfLine.)] |
| 107 | |
| 108 | p = &Blockscip-go gomod github.com/42wim/sshsig . `github.com/42wim/sshsig/pem`/Block#{ |
| 109 | Headersscip-go gomod github.com/42wim/sshsig . `github.com/42wim/sshsig/pem`/Block#Headers.: make(map[string]string), |
| 110 | Typescip-go gomod github.com/42wim/sshsig . `github.com/42wim/sshsig/pem`/Block#Type.: string(typeLine), |
| 111 | } |
| 112 | |
| 113 | for { |
| 114 | // This loop terminates because getLine's second result is |
| 115 | // always smaller than its argument. |
| 116 | if len(rest) == 0 { |
| 117 | return nil, data |
| 118 | } |
| 119 | line, next := getLinescip-go gomod github.com/42wim/sshsig . `github.com/42wim/sshsig/pem`/getLine().(rest) |
| 120 | |
| 121 | i := bytesscip-go gomod github.com/golang/go/src go1.23.0 bytes/.IndexBytescip-go gomod github.com/golang/go/src go1.23.0 bytes/IndexByte().(line, ':') |
| 122 | if i == -1 { |
| 123 | break |
| 124 | } |
| 125 | |
| 126 | // TODO(agl): need to cope with values that spread across lines. |
| 127 | key, val := line[:i], line[i+1:] |
| 128 | key = bytesscip-go gomod github.com/golang/go/src go1.23.0 bytes/.TrimSpacescip-go gomod github.com/golang/go/src go1.23.0 bytes/TrimSpace().(key) |
| 129 | val = bytesscip-go gomod github.com/golang/go/src go1.23.0 bytes/.TrimSpacescip-go gomod github.com/golang/go/src go1.23.0 bytes/TrimSpace().(val) |
| 130 | p.Headersscip-go gomod github.com/42wim/sshsig . `github.com/42wim/sshsig/pem`/Block#Headers.[string(key)] = string(val) |
| 131 | rest = next |
| 132 | } |
| 133 | |
| 134 | var endIndex, endTrailerIndex int |
| 135 | |
| 136 | // If there were no headers, the END line might occur |
| 137 | // immediately, without a leading newline. |
| 138 | if len(p.Headersscip-go gomod github.com/42wim/sshsig . `github.com/42wim/sshsig/pem`/Block#Headers.) == 0 && bytesscip-go gomod github.com/golang/go/src go1.23.0 bytes/.HasPrefixscip-go gomod github.com/golang/go/src go1.23.0 bytes/HasPrefix().(rest, pemEndscip-go gomod github.com/42wim/sshsig . `github.com/42wim/sshsig/pem`/pemEnd.[1:]) { |
| 139 | endIndex = 0 |
| 140 | endTrailerIndex = len(pemEndscip-go gomod github.com/42wim/sshsig . `github.com/42wim/sshsig/pem`/pemEnd.) - 1 |
| 141 | } else { |
| 142 | endIndex = bytesscip-go gomod github.com/golang/go/src go1.23.0 bytes/.Indexscip-go gomod github.com/golang/go/src go1.23.0 bytes/Index().(rest, pemEndscip-go gomod github.com/42wim/sshsig . `github.com/42wim/sshsig/pem`/pemEnd.) |
| 143 | endTrailerIndex = endIndex + len(pemEndscip-go gomod github.com/42wim/sshsig . `github.com/42wim/sshsig/pem`/pemEnd.) |
| 144 | } |
| 145 | |
| 146 | if endIndex < 0 { |
| 147 | return decodeErrorscip-go gomod github.com/42wim/sshsig . `github.com/42wim/sshsig/pem`/decodeError().(data, rest) |
| 148 | } |
| 149 | |
| 150 | // After the "-----" of the ending line, there should be the same type |
| 151 | // and then a final five dashes. |
| 152 | endTrailer := rest[endTrailerIndex:] |
| 153 | endTrailerLen := len(typeLine) + len(pemEndOfLinescip-go gomod github.com/42wim/sshsig . `github.com/42wim/sshsig/pem`/pemEndOfLine.) |
| 154 | if len(endTrailer) < endTrailerLen { |
| 155 | return decodeErrorscip-go gomod github.com/42wim/sshsig . `github.com/42wim/sshsig/pem`/decodeError().(data, rest) |
| 156 | } |
| 157 | |
| 158 | restOfEndLine := endTrailer[endTrailerLen:] |
| 159 | endTrailer = endTrailer[:endTrailerLen] |
| 160 | if !bytesscip-go gomod github.com/golang/go/src go1.23.0 bytes/.HasPrefixscip-go gomod github.com/golang/go/src go1.23.0 bytes/HasPrefix().(endTrailer, typeLine) || |
| 161 | !bytesscip-go gomod github.com/golang/go/src go1.23.0 bytes/.HasSuffixscip-go gomod github.com/golang/go/src go1.23.0 bytes/HasSuffix().(endTrailer, pemEndOfLinescip-go gomod github.com/42wim/sshsig . `github.com/42wim/sshsig/pem`/pemEndOfLine.) { |
| 162 | return decodeErrorscip-go gomod github.com/42wim/sshsig . `github.com/42wim/sshsig/pem`/decodeError().(data, rest) |
| 163 | } |
| 164 | |
| 165 | // The line must end with only whitespace. |
| 166 | if s, _ := getLinescip-go gomod github.com/42wim/sshsig . `github.com/42wim/sshsig/pem`/getLine().(restOfEndLine); len(s) != 0 { |
| 167 | return decodeErrorscip-go gomod github.com/42wim/sshsig . `github.com/42wim/sshsig/pem`/decodeError().(data, rest) |
| 168 | } |
| 169 | |
| 170 | base64Data := removeSpacesAndTabsscip-go gomod github.com/42wim/sshsig . `github.com/42wim/sshsig/pem`/removeSpacesAndTabs().(rest[:endIndex]) |
| 171 | p.Bytesscip-go gomod github.com/42wim/sshsig . `github.com/42wim/sshsig/pem`/Block#Bytes. = make([]byte, base64scip-go gomod github.com/golang/go/src go1.23.0 `encoding/base64`/.StdEncodingscip-go gomod github.com/golang/go/src go1.23.0 `encoding/base64`/StdEncoding..DecodedLenscip-go gomod github.com/golang/go/src go1.23.0 `encoding/base64`/Encoding#DecodedLen().(len(base64Data))) |
| 172 | n, err := base64scip-go gomod github.com/golang/go/src go1.23.0 `encoding/base64`/.StdEncodingscip-go gomod github.com/golang/go/src go1.23.0 `encoding/base64`/StdEncoding..Decodescip-go gomod github.com/golang/go/src go1.23.0 `encoding/base64`/Encoding#Decode().(p.Bytesscip-go gomod github.com/42wim/sshsig . `github.com/42wim/sshsig/pem`/Block#Bytes., base64Data) |
| 173 | if err != nil { |
| 174 | return decodeErrorscip-go gomod github.com/42wim/sshsig . `github.com/42wim/sshsig/pem`/decodeError().(data, rest) |
| 175 | } |
| 176 | p.Bytesscip-go gomod github.com/42wim/sshsig . `github.com/42wim/sshsig/pem`/Block#Bytes. = p.Bytesscip-go gomod github.com/42wim/sshsig . `github.com/42wim/sshsig/pem`/Block#Bytes.[:n] |
| 177 | |
| 178 | // the -1 is because we might have only matched pemEnd without the |
| 179 | // leading newline if the PEM block was empty. |
| 180 | _, rest = getLinescip-go gomod github.com/42wim/sshsig . `github.com/42wim/sshsig/pem`/getLine().(rest[endIndex+len(pemEndscip-go gomod github.com/42wim/sshsig . `github.com/42wim/sshsig/pem`/pemEnd.)-1:]) |
| 181 | |
| 182 | return |
| 183 | } |
| 184 | |
| 185 | func decodeErrorscip-go gomod github.com/42wim/sshsig . `github.com/42wim/sshsig/pem`/decodeError().(data, rest []byte) (*Blockscip-go gomod github.com/42wim/sshsig . `github.com/42wim/sshsig/pem`/Block#, []byte) { |
| 186 | // If we get here then we have rejected a likely looking, but |
| 187 | // ultimately invalid PEM block. We need to start over from a new |
| 188 | // position. We have consumed the preamble line and will have consumed |
| 189 | // any lines which could be header lines. However, a valid preamble |
| 190 | // line is not a valid header line, therefore we cannot have consumed |
| 191 | // the preamble line for the any subsequent block. Thus, we will always |
| 192 | // find any valid block, no matter what bytes precede it. |
| 193 | // |
| 194 | // For example, if the input is |
| 195 | // |
| 196 | // -----BEGIN MALFORMED BLOCK----- |
| 197 | // junk that may look like header lines |
| 198 | // or data lines, but no END line |
| 199 | // |
| 200 | // -----BEGIN ACTUAL BLOCK----- |
| 201 | // realdata |
| 202 | // -----END ACTUAL BLOCK----- |
| 203 | // |
| 204 | // we've failed to parse using the first BEGIN line |
| 205 | // and now will try again, using the second BEGIN line. |
| 206 | p, rest := Decodescip-go gomod github.com/42wim/sshsig . `github.com/42wim/sshsig/pem`/Decode().(rest) |
| 207 | if p == nil { |
| 208 | rest = data |
| 209 | } |
| 210 | return p, rest |
| 211 | } |
| 212 | |
| 213 | // https://github.com/openssh/openssh-portable/blob/master/PROTOCOL.sshsig#L20-L21 |
| 214 | // Should be on 76 chars, but strangely enough ssh breaks on 70 |
| 215 | const pemLineLengthscip-go gomod github.com/42wim/sshsig . `github.com/42wim/sshsig/pem`/pemLineLength. = 70 |
| 216 | |
| 217 | type lineBreakerscip-go gomod github.com/42wim/sshsig . `github.com/42wim/sshsig/pem`/lineBreaker# struct { |
| 218 | linescip-go gomod github.com/42wim/sshsig . `github.com/42wim/sshsig/pem`/lineBreaker#line. [pemLineLengthscip-go gomod github.com/42wim/sshsig . `github.com/42wim/sshsig/pem`/pemLineLength.]byte |
| 219 | usedscip-go gomod github.com/42wim/sshsig . `github.com/42wim/sshsig/pem`/lineBreaker#used. int |
| 220 | outscip-go gomod github.com/42wim/sshsig . `github.com/42wim/sshsig/pem`/lineBreaker#out. ioscip-go gomod github.com/golang/go/src go1.23.0 io/.Writerscip-go gomod github.com/golang/go/src go1.23.0 io/Writer# |
| 221 | } |
| 222 | |
| 223 | var nlscip-go gomod github.com/42wim/sshsig . `github.com/42wim/sshsig/pem`/nl. = []byte{'\n'} |
| 224 | |
| 225 | func (l *lineBreakerscip-go gomod github.com/42wim/sshsig . `github.com/42wim/sshsig/pem`/lineBreaker#) Writescip-go gomod github.com/42wim/sshsig . `github.com/42wim/sshsig/pem`/lineBreaker#Write().(b []byte) (n int, err error) { |
| 226 | if l.usedscip-go gomod github.com/42wim/sshsig . `github.com/42wim/sshsig/pem`/lineBreaker#used.+len(b) < pemLineLengthscip-go gomod github.com/42wim/sshsig . `github.com/42wim/sshsig/pem`/pemLineLength. { |
| 227 | copy(l.linescip-go gomod github.com/42wim/sshsig . `github.com/42wim/sshsig/pem`/lineBreaker#line.[l.usedscip-go gomod github.com/42wim/sshsig . `github.com/42wim/sshsig/pem`/lineBreaker#used.:], b) |
| 228 | l.usedscip-go gomod github.com/42wim/sshsig . `github.com/42wim/sshsig/pem`/lineBreaker#used. += len(b) |
| 229 | return len(b), nil |
| 230 | } |
| 231 | |
| 232 | n, err = l.outscip-go gomod github.com/42wim/sshsig . `github.com/42wim/sshsig/pem`/lineBreaker#out..Writescip-go gomod github.com/golang/go/src go1.23.0 io/Writer#Write().(l.linescip-go gomod github.com/42wim/sshsig . `github.com/42wim/sshsig/pem`/lineBreaker#line.[0:l.usedscip-go gomod github.com/42wim/sshsig . `github.com/42wim/sshsig/pem`/lineBreaker#used.]) |
| 233 | if err != nil { |
| 234 | return |
| 235 | } |
| 236 | excess := pemLineLengthscip-go gomod github.com/42wim/sshsig . `github.com/42wim/sshsig/pem`/pemLineLength. - l.usedscip-go gomod github.com/42wim/sshsig . `github.com/42wim/sshsig/pem`/lineBreaker#used. |
| 237 | l.usedscip-go gomod github.com/42wim/sshsig . `github.com/42wim/sshsig/pem`/lineBreaker#used. = 0 |
| 238 | |
| 239 | n, err = l.outscip-go gomod github.com/42wim/sshsig . `github.com/42wim/sshsig/pem`/lineBreaker#out..Writescip-go gomod github.com/golang/go/src go1.23.0 io/Writer#Write().(b[0:excess]) |
| 240 | if err != nil { |
| 241 | return |
| 242 | } |
| 243 | |
| 244 | n, err = l.outscip-go gomod github.com/42wim/sshsig . `github.com/42wim/sshsig/pem`/lineBreaker#out..Writescip-go gomod github.com/golang/go/src go1.23.0 io/Writer#Write().(nlscip-go gomod github.com/42wim/sshsig . `github.com/42wim/sshsig/pem`/nl.) |
| 245 | if err != nil { |
| 246 | return |
| 247 | } |
| 248 | |
| 249 | return l.Writescip-go gomod github.com/42wim/sshsig . `github.com/42wim/sshsig/pem`/lineBreaker#Write().(b[excess:]) |
| 250 | } |
| 251 | |
| 252 | func (l *lineBreakerscip-go gomod github.com/42wim/sshsig . `github.com/42wim/sshsig/pem`/lineBreaker#) Closescip-go gomod github.com/42wim/sshsig . `github.com/42wim/sshsig/pem`/lineBreaker#Close().() (err error) { |
| 253 | if l.usedscip-go gomod github.com/42wim/sshsig . `github.com/42wim/sshsig/pem`/lineBreaker#used. > 0 { |
| 254 | _, err = l.outscip-go gomod github.com/42wim/sshsig . `github.com/42wim/sshsig/pem`/lineBreaker#out..Writescip-go gomod github.com/golang/go/src go1.23.0 io/Writer#Write().(l.linescip-go gomod github.com/42wim/sshsig . `github.com/42wim/sshsig/pem`/lineBreaker#line.[0:l.usedscip-go gomod github.com/42wim/sshsig . `github.com/42wim/sshsig/pem`/lineBreaker#used.]) |
| 255 | if err != nil { |
| 256 | return |
| 257 | } |
| 258 | _, err = l.outscip-go gomod github.com/42wim/sshsig . `github.com/42wim/sshsig/pem`/lineBreaker#out..Writescip-go gomod github.com/golang/go/src go1.23.0 io/Writer#Write().(nlscip-go gomod github.com/42wim/sshsig . `github.com/42wim/sshsig/pem`/nl.) |
| 259 | } |
| 260 | |
| 261 | return |
| 262 | } |
| 263 | |
| 264 | func writeHeaderscip-go gomod github.com/42wim/sshsig . `github.com/42wim/sshsig/pem`/writeHeader().(out ioscip-go gomod github.com/golang/go/src go1.23.0 io/.Writerscip-go gomod github.com/golang/go/src go1.23.0 io/Writer#, k, v string) error { |
| 265 | _, err := out.Writescip-go gomod github.com/golang/go/src go1.23.0 io/Writer#Write().([]byte(k + ": " + v + "\n")) |
| 266 | return err |
| 267 | } |
| 268 | |
| 269 | // Encode writes the PEM encoding of b to out. |
| 270 | func Encodescip-go gomod github.com/42wim/sshsig . `github.com/42wim/sshsig/pem`/Encode().(out ioscip-go gomod github.com/golang/go/src go1.23.0 io/.Writerscip-go gomod github.com/golang/go/src go1.23.0 io/Writer#, b *Blockscip-go gomod github.com/42wim/sshsig . `github.com/42wim/sshsig/pem`/Block#) error { |
| 271 | // Check for invalid block before writing any output. |
| 272 | for k := range b.Headersscip-go gomod github.com/42wim/sshsig . `github.com/42wim/sshsig/pem`/Block#Headers. { |
| 273 | if stringsscip-go gomod github.com/golang/go/src go1.23.0 strings/.Containsscip-go gomod github.com/golang/go/src go1.23.0 strings/Contains().(k, ":") { |
| 274 | return errorsscip-go gomod github.com/golang/go/src go1.23.0 errors/.Newscip-go gomod github.com/golang/go/src go1.23.0 errors/New().("pem: cannot encode a header key that contains a colon") |
| 275 | } |
| 276 | } |
| 277 | |
| 278 | // All errors below are relayed from underlying io.Writer, |
| 279 | // so it is now safe to write data. |
| 280 | |
| 281 | if _, err := out.Writescip-go gomod github.com/golang/go/src go1.23.0 io/Writer#Write().(pemStartscip-go gomod github.com/42wim/sshsig . `github.com/42wim/sshsig/pem`/pemStart.[1:]); err != nil { |
| 282 | return err |
| 283 | } |
| 284 | if _, err := out.Writescip-go gomod github.com/golang/go/src go1.23.0 io/Writer#Write().([]byte(b.Typescip-go gomod github.com/42wim/sshsig . `github.com/42wim/sshsig/pem`/Block#Type. + "-----\n")); err != nil { |
| 285 | return err |
| 286 | } |
| 287 | |
| 288 | if len(b.Headersscip-go gomod github.com/42wim/sshsig . `github.com/42wim/sshsig/pem`/Block#Headers.) > 0 { |
| 289 | const procType = "Proc-Type" |
| 290 | h := make([]string, 0, len(b.Headersscip-go gomod github.com/42wim/sshsig . `github.com/42wim/sshsig/pem`/Block#Headers.)) |
| 291 | hasProcType := false |
| 292 | for k := range b.Headersscip-go gomod github.com/42wim/sshsig . `github.com/42wim/sshsig/pem`/Block#Headers. { |
| 293 | if k == procType { |
| 294 | hasProcType = true |
| 295 | continue |
| 296 | } |
| 297 | h = append(h, k) |
| 298 | } |
| 299 | // The Proc-Type header must be written first. |
| 300 | // See RFC 1421, section 4.6.1.1 |
| 301 | if hasProcType { |
| 302 | if err := writeHeaderscip-go gomod github.com/42wim/sshsig . `github.com/42wim/sshsig/pem`/writeHeader().(out, procType, b.Headersscip-go gomod github.com/42wim/sshsig . `github.com/42wim/sshsig/pem`/Block#Headers.[procType]); err != nil { |
| 303 | return err |
| 304 | } |
| 305 | } |
| 306 | // For consistency of output, write other headers sorted by key. |
| 307 | sortscip-go gomod github.com/golang/go/src go1.23.0 sort/.Stringsscip-go gomod github.com/golang/go/src go1.23.0 sort/Strings().(h) |
| 308 | for _, k := range h { |
| 309 | if err := writeHeaderscip-go gomod github.com/42wim/sshsig . `github.com/42wim/sshsig/pem`/writeHeader().(out, k, b.Headersscip-go gomod github.com/42wim/sshsig . `github.com/42wim/sshsig/pem`/Block#Headers.[k]); err != nil { |
| 310 | return err |
| 311 | } |
| 312 | } |
| 313 | if _, err := out.Writescip-go gomod github.com/golang/go/src go1.23.0 io/Writer#Write().(nlscip-go gomod github.com/42wim/sshsig . `github.com/42wim/sshsig/pem`/nl.); err != nil { |
| 314 | return err |
| 315 | } |
| 316 | } |
| 317 | |
| 318 | var breaker lineBreakerscip-go gomod github.com/42wim/sshsig . `github.com/42wim/sshsig/pem`/lineBreaker# |
| 319 | breaker.outscip-go gomod github.com/42wim/sshsig . `github.com/42wim/sshsig/pem`/lineBreaker#out. = out |
| 320 | |
| 321 | b64 := base64scip-go gomod github.com/golang/go/src go1.23.0 `encoding/base64`/.NewEncoderscip-go gomod github.com/golang/go/src go1.23.0 `encoding/base64`/NewEncoder().(base64scip-go gomod github.com/golang/go/src go1.23.0 `encoding/base64`/.StdEncodingscip-go gomod github.com/golang/go/src go1.23.0 `encoding/base64`/StdEncoding., &breaker) |
| 322 | if _, err := b64.Writescip-go gomod github.com/golang/go/src go1.23.0 io/Writer#Write().(b.Bytesscip-go gomod github.com/42wim/sshsig . `github.com/42wim/sshsig/pem`/Block#Bytes.); err != nil { |
| 323 | return err |
| 324 | } |
| 325 | b64.Closescip-go gomod github.com/golang/go/src go1.23.0 io/Closer#Close().() |
| 326 | breaker.Closescip-go gomod github.com/42wim/sshsig . `github.com/42wim/sshsig/pem`/lineBreaker#Close().() |
| 327 | |
| 328 | if _, err := out.Writescip-go gomod github.com/golang/go/src go1.23.0 io/Writer#Write().(pemEndscip-go gomod github.com/42wim/sshsig . `github.com/42wim/sshsig/pem`/pemEnd.[1:]); err != nil { |
| 329 | return err |
| 330 | } |
| 331 | _, err := out.Writescip-go gomod github.com/golang/go/src go1.23.0 io/Writer#Write().([]byte(b.Typescip-go gomod github.com/42wim/sshsig . `github.com/42wim/sshsig/pem`/Block#Type. + "-----\n")) |
| 332 | return err |
| 333 | } |
| 334 | |
| 335 | // EncodeToMemory returns the PEM encoding of b. |
| 336 | // |
| 337 | // If b has invalid headers and cannot be encoded, |
| 338 | // EncodeToMemory returns nil. If it is important to |
| 339 | // report details about this error case, use Encode instead. |
| 340 | func EncodeToMemoryscip-go gomod github.com/42wim/sshsig . `github.com/42wim/sshsig/pem`/EncodeToMemory().(b *Blockscip-go gomod github.com/42wim/sshsig . `github.com/42wim/sshsig/pem`/Block#) []byte { |
| 341 | var buf bytesscip-go gomod github.com/golang/go/src go1.23.0 bytes/.Bufferscip-go gomod github.com/golang/go/src go1.23.0 bytes/Buffer# |
| 342 | if err := Encodescip-go gomod github.com/42wim/sshsig . `github.com/42wim/sshsig/pem`/Encode().(&buf, b); err != nil { |
| 343 | return nil |
| 344 | } |
| 345 | return buf.Bytesscip-go gomod github.com/golang/go/src go1.23.0 bytes/Buffer#Bytes().() |
| 346 | } |