mirror of
				https://github.com/RGBCube/serenity
				synced 2025-10-31 15:12:45 +00:00 
			
		
		
		
	 1682f0b760
			
		
	
	
		1682f0b760
		
	
	
	
	
		
			
			SPDX License Identifiers are a more compact / standardized way of representing file license information. See: https://spdx.dev/resources/use/#identifiers This was done with the `ambr` search and replace tool. ambr --no-parent-ignore --key-from-file --rep-from-file key.txt rep.txt *
		
			
				
	
	
		
			52 lines
		
	
	
	
		
			1.2 KiB
		
	
	
	
		
			C++
		
	
	
	
	
	
			
		
		
	
	
			52 lines
		
	
	
	
		
			1.2 KiB
		
	
	
	
		
			C++
		
	
	
	
	
	
| /*
 | |
|  * Copyright (c) 2021, the SerenityOS developers.
 | |
|  *
 | |
|  * SPDX-License-Identifier: BSD-2-Clause
 | |
|  */
 | |
| 
 | |
| #include <AK/Base64.h>
 | |
| #include <AK/GenericLexer.h>
 | |
| #include <LibCrypto/ASN1/PEM.h>
 | |
| 
 | |
| namespace Crypto {
 | |
| 
 | |
| ByteBuffer decode_pem(ReadonlyBytes data)
 | |
| {
 | |
|     GenericLexer lexer { data };
 | |
|     ByteBuffer decoded;
 | |
| 
 | |
|     // FIXME: Parse multiple.
 | |
|     enum {
 | |
|         PreStartData,
 | |
|         Started,
 | |
|         Ended,
 | |
|     } state { PreStartData };
 | |
|     while (!lexer.is_eof()) {
 | |
|         switch (state) {
 | |
|         case PreStartData:
 | |
|             if (lexer.consume_specific("-----BEGIN"))
 | |
|                 state = Started;
 | |
|             lexer.consume_line();
 | |
|             break;
 | |
|         case Started: {
 | |
|             if (lexer.consume_specific("-----END")) {
 | |
|                 state = Ended;
 | |
|                 lexer.consume_line();
 | |
|                 break;
 | |
|             }
 | |
|             auto b64decoded = decode_base64(lexer.consume_line().trim_whitespace(TrimMode::Right));
 | |
|             decoded.append(b64decoded.data(), b64decoded.size());
 | |
|             break;
 | |
|         }
 | |
|         case Ended:
 | |
|             lexer.consume_all();
 | |
|             break;
 | |
|         default:
 | |
|             VERIFY_NOT_REACHED();
 | |
|         }
 | |
|     }
 | |
| 
 | |
|     return decoded;
 | |
| }
 | |
| 
 | |
| }
 |