|
4 | 4 |
|
5 | 5 | import { describe, expect, it } from "vitest"; |
6 | 6 |
|
7 | | -import { derToPem, getPemLabel, isPem, normalizePem, pemToDer } from "./pem"; |
| 7 | +import { derToPem, getPemLabel, isPem, normalizePem, parsePem, pemToDer } from "./pem"; |
8 | 8 |
|
9 | 9 | describe("PEM utilities", () => { |
10 | 10 | // Sample DER data (just random bytes for testing) |
@@ -164,6 +164,113 @@ describe("PEM utilities", () => { |
164 | 164 | }); |
165 | 165 | }); |
166 | 166 |
|
| 167 | + describe("parsePem", () => { |
| 168 | + it("parses a single PEM block", () => { |
| 169 | + const pem = derToPem(sampleDer, "CERTIFICATE"); |
| 170 | + const blocks = parsePem(pem); |
| 171 | + |
| 172 | + expect(blocks).toHaveLength(1); |
| 173 | + expect(blocks[0].label).toBe("CERTIFICATE"); |
| 174 | + expect(blocks[0].der).toEqual(sampleDer); |
| 175 | + }); |
| 176 | + |
| 177 | + it("parses multiple PEM blocks", () => { |
| 178 | + const der1 = new Uint8Array([0x01, 0x02, 0x03]); |
| 179 | + const der2 = new Uint8Array([0x04, 0x05, 0x06]); |
| 180 | + const der3 = new Uint8Array([0x07, 0x08, 0x09]); |
| 181 | + |
| 182 | + const pem = [ |
| 183 | + derToPem(der1, "CERTIFICATE"), |
| 184 | + derToPem(der2, "CERTIFICATE"), |
| 185 | + derToPem(der3, "PRIVATE KEY"), |
| 186 | + ].join("\n"); |
| 187 | + |
| 188 | + const blocks = parsePem(pem); |
| 189 | + |
| 190 | + expect(blocks).toHaveLength(3); |
| 191 | + expect(blocks[0]).toEqual({ label: "CERTIFICATE", der: der1 }); |
| 192 | + expect(blocks[1]).toEqual({ label: "CERTIFICATE", der: der2 }); |
| 193 | + expect(blocks[2]).toEqual({ label: "PRIVATE KEY", der: der3 }); |
| 194 | + }); |
| 195 | + |
| 196 | + it("handles different label types", () => { |
| 197 | + const labels = [ |
| 198 | + "CERTIFICATE", |
| 199 | + "PUBLIC KEY", |
| 200 | + "PRIVATE KEY", |
| 201 | + "RSA PRIVATE KEY", |
| 202 | + "EC PRIVATE KEY", |
| 203 | + "ENCRYPTED PRIVATE KEY", |
| 204 | + ]; |
| 205 | + |
| 206 | + const pem = labels.map(label => derToPem(sampleDer, label)).join("\n"); |
| 207 | + |
| 208 | + const blocks = parsePem(pem); |
| 209 | + |
| 210 | + expect(blocks).toHaveLength(labels.length); |
| 211 | + for (let i = 0; i < labels.length; i++) { |
| 212 | + expect(blocks[i].label).toBe(labels[i]); |
| 213 | + expect(blocks[i].der).toEqual(sampleDer); |
| 214 | + } |
| 215 | + }); |
| 216 | + |
| 217 | + it("returns empty array for non-PEM string", () => { |
| 218 | + expect(parsePem("not a pem")).toEqual([]); |
| 219 | + expect(parsePem("")).toEqual([]); |
| 220 | + }); |
| 221 | + |
| 222 | + it("handles PEM with extra whitespace and text between blocks", () => { |
| 223 | + const der1 = new Uint8Array([0x01, 0x02]); |
| 224 | + const der2 = new Uint8Array([0x03, 0x04]); |
| 225 | + |
| 226 | + const pem = ` |
| 227 | + Some header text |
| 228 | + ${derToPem(der1, "CERTIFICATE")} |
| 229 | + Intermediate text here |
| 230 | + ${derToPem(der2, "PRIVATE KEY")} |
| 231 | + Footer text |
| 232 | + `; |
| 233 | + |
| 234 | + const blocks = parsePem(pem); |
| 235 | + |
| 236 | + expect(blocks).toHaveLength(2); |
| 237 | + expect(blocks[0]).toEqual({ label: "CERTIFICATE", der: der1 }); |
| 238 | + expect(blocks[1]).toEqual({ label: "PRIVATE KEY", der: der2 }); |
| 239 | + }); |
| 240 | + |
| 241 | + it("throws on mismatched BEGIN/END labels", () => { |
| 242 | + const pem = `-----BEGIN CERTIFICATE----- |
| 243 | +AQID |
| 244 | +-----END PRIVATE KEY-----`; |
| 245 | + |
| 246 | + expect(() => parsePem(pem)).toThrow(/label mismatch.*BEGIN CERTIFICATE.*END PRIVATE KEY/); |
| 247 | + }); |
| 248 | + |
| 249 | + it("handles empty PEM blocks", () => { |
| 250 | + const pem = `-----BEGIN CERTIFICATE----- |
| 251 | +-----END CERTIFICATE-----`; |
| 252 | + |
| 253 | + const blocks = parsePem(pem); |
| 254 | + |
| 255 | + expect(blocks).toHaveLength(1); |
| 256 | + expect(blocks[0].label).toBe("CERTIFICATE"); |
| 257 | + expect(blocks[0].der).toEqual(new Uint8Array(0)); |
| 258 | + }); |
| 259 | + |
| 260 | + it("handles trailing text after last block", () => { |
| 261 | + const pem = `${derToPem(sampleDer, "CERTIFICATE")} |
| 262 | +Some trailing text here |
| 263 | +-----BEGIN ORPHAN----- |
| 264 | +AQID`; |
| 265 | + |
| 266 | + const blocks = parsePem(pem); |
| 267 | + |
| 268 | + // Should only parse the complete block, ignoring the incomplete one |
| 269 | + expect(blocks).toHaveLength(1); |
| 270 | + expect(blocks[0].label).toBe("CERTIFICATE"); |
| 271 | + }); |
| 272 | + }); |
| 273 | + |
167 | 274 | describe("round-trip", () => { |
168 | 275 | it("preserves data through multiple round-trips", () => { |
169 | 276 | let data: Uint8Array = sampleDer; |
|
0 commit comments