1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

crypto.pem: add a static method Block.new, to replace new (#18846)

This commit is contained in:
phoebe 2023-07-18 07:02:30 +02:00 committed by GitHub
parent 39cfaafaae
commit 6dcf122172
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 64 additions and 42 deletions

View File

@ -2,14 +2,12 @@ module pem
import encoding.base64 import encoding.base64
// `decode` reads `data` and returns the first parsed PEM Block along with the rest of // decode reads `data` and returns the first parsed PEM Block along with the rest of
// the string. `none` is returned when a header is expected, but not present // the string. `none` is returned when a header is expected, but not present
// or when a start of '-----BEGIN' or end of '-----END' can't be found in `data` // or when a start of '-----BEGIN' or end of '-----END' can't be found in `data`
pub fn decode(data string) ?(Block, string) { pub fn decode(data string) ?(Block, string) {
mut rest := data[data.index(pem_begin)?..] mut rest := data[data.index(pem_begin)?..]
mut block := Block{ mut block := Block.new(rest[pem_begin.len..].all_before(pem_eol))
block_type: rest[pem_begin.len..].all_before(pem_eol)
}
block.headers, rest = parse_headers(rest[pem_begin.len..].all_after(pem_eol).trim_left(' \n\t\v\f\r'))? block.headers, rest = parse_headers(rest[pem_begin.len..].all_after(pem_eol).trim_left(' \n\t\v\f\r'))?
block_end_index := rest.index(pem_end)? block_end_index := rest.index(pem_end)?

View File

@ -3,7 +3,7 @@ module pem
import encoding.base64 import encoding.base64
import arrays import arrays
// `encode_config` encodes the given block into a // encode encodes the given block into a
// string using the EncodeConfig. It returns an error if `block_type` is undefined // string using the EncodeConfig. It returns an error if `block_type` is undefined
// or if a value in `headers` contains an invalid character ':' // or if a value in `headers` contains an invalid character ':'
// //

View File

@ -9,6 +9,13 @@ const (
colon = ':' colon = ':'
) )
// new returns a new `Block` with the specified block_type
[deprecated: 'use Block.new instead']
[inline]
pub fn new(block_type string) Block {
return Block.new(block_type)
}
[params] [params]
pub struct EncodeConfig { pub struct EncodeConfig {
pub mut: pub mut:
@ -34,7 +41,7 @@ pub enum Header {
crl crl
} }
// `str` returns the string representation of the header // str returns the string representation of the header
pub fn (header Header) str() string { pub fn (header Header) str() string {
return match header { return match header {
.proctype { 'Proc-Type' } .proctype { 'Proc-Type' }
@ -62,6 +69,14 @@ pub mut:
data []u8 data []u8
} }
// Block.new returns a new `Block` with the specified block_type
[inline]
pub fn Block.new(block_type string) Block {
return Block{
block_type: block_type
}
}
// free the resources taken by the Block `block` // free the resources taken by the Block `block`
[unsafe] [unsafe]
pub fn (mut block Block) free() { pub fn (mut block Block) free() {
@ -75,15 +90,7 @@ pub fn (mut block Block) free() {
} }
} }
// returns a new `Block` with the specified block_type // header_by_key returns the selected key using the Header enum
[inline]
pub fn new(block_type string) Block {
return Block{
block_type: block_type
}
}
// returns the selected key using the Header enum
// //
// same as `block.headers[key.str()]` // same as `block.headers[key.str()]`
[inline] [inline]

View File

@ -44,6 +44,23 @@ fn test_encode_config() {
} }
} }
fn test_decode_no_pem() {
for test in pem.test_data_no_pem {
if _, _ := decode(test) {
assert false, 'Block.decode_partial should return `none` on input without PEM data'
} else {
assert true
}
}
}
const test_data_no_pem = [
'',
'-----BEGIN',
'-----BEGIN -----',
'-----END',
]
// https://datatracker.ietf.org/doc/html/rfc7468#section-4 // https://datatracker.ietf.org/doc/html/rfc7468#section-4
const test_data_rfc1421 = [ const test_data_rfc1421 = [
'-----BEGIN PRIVACY-ENHANCED MESSAGE----- '-----BEGIN PRIVACY-ENHANCED MESSAGE-----
@ -305,8 +322,8 @@ BEGIN BEGIN BEGIN
-----END RSA PRIVATE KEY -----END RSA PRIVATE KEY
: fkalsdjflkasdjf : fkalsdjflkasdjf
private key: fsaddf", private key: fsaddf",
'Mollitia magnam ullam ipsam voluptas ipsa 'Mollitia magnam ullam ipsam voluptas ipsa
rerum debitis. Vel nulla ipsum enim perspiciatis adipisci quam. Nihil incidunt ipsum rerum debitis. Vel nulla ipsum enim perspiciatis adipisci quam. Nihil incidunt ipsum
--- --BEGIN --- --BEGIN
@ -327,46 +344,46 @@ xph0pSfsfFsTKM4RhTWD2v4fgk+xZiKd1p0+L4hTtpwnEw0uXRVd0ki6muwV5y/P
BQUAA4GBAJiDAAtY0mQQeuxWdzLRzXmjvdSuL9GoyT3BF/jSnpxz5/58dba8pWen BQUAA4GBAJiDAAtY0mQQeuxWdzLRzXmjvdSuL9GoyT3BF/jSnpxz5/58dba8pWen
v3pj4P3w5DoOso0rzkZy2jEsEitlVM2mLSbQpMM+MUVQCQoiG6W9xuCFuxSrwPIS v3pj4P3w5DoOso0rzkZy2jEsEitlVM2mLSbQpMM+MUVQCQoiG6W9xuCFuxSrwPIS
pAqEAuV4DNoxQKKWmhVv+J0ptMWD25Pnpxeq5sXzghfJnslJlQND pAqEAuV4DNoxQKKWmhVv+J0ptMWD25Pnpxeq5sXzghfJnslJlQND
-----END CERTIFICATE----- -----END CERTIFICATE-----
fdsjaf888888888888 fdsjaf888888888888
----- -----
-----END -----END
-----BEGIN', -----BEGIN',
'Lorem ipsum dolor sit amet 'Lorem ipsum dolor sit amet
, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore , consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore
et dolore magna aliqua. Massa id neque aliquam vestibulum et dolore magna aliqua. Massa id neque aliquam vestibulum
morbi blandit cursus risus. Elit at imperdiet dui accumsan sit amet nulla. Pulvinar pellentesque habitant morbi blandit cursus risus. Elit at imperdiet dui accumsan sit amet nulla. Pulvinar pellentesque habitant
morbi tristique senectus. Vulputate morbi tristique senectus. Vulputate
dignissim suspendisse in est ante in. Egestas dui id ornare arcu. Ultrices mi tempus imperdiet dignissim suspendisse in est ante in. Egestas dui id ornare arcu. Ultrices mi tempus imperdiet
nulla malesuada. Elementum nisi quis eleifend quam adipiscing. nulla malesuada. Elementum nisi quis eleifend quam adipiscing.
Mi in nulla posuere sollicitudin aliquam ultrices. Elit at imperdiet dui accumsan sit amet nulla facilisi. In hac Mi in nulla posuere sollicitudin aliquam ultrices. Elit at imperdiet dui accumsan sit amet nulla facilisi. In hac
habitasse platea dictumst quisque sagittis. Vestibulum habitasse platea dictumst quisque sagittis. Vestibulum
lectus mauris ultrices eros in cursus. Blandit volutpat maecenas volutpat blandit. Sed nisi lectus mauris ultrices eros in cursus. Blandit volutpat maecenas volutpat blandit. Sed nisi
lacus sed viverra tellus in hac habitasse platea. lacus sed viverra tellus in hac habitasse platea.
Nulla facilisi etiam dignissim diam. Donec et odio pellentesque diam volutpat Nulla facilisi etiam dignissim diam. Donec et odio pellentesque diam volutpat
commodo sed egestas. Eleifend quam adipiscing commodo sed egestas. Eleifend quam adipiscing
vitae proin sagittis nisl. vitae proin sagittis nisl.
Pharetra et ultrices neque ornare aenean euismod elementum nisi. Sit amet consectetur sed id semper risus in. Pharetra et ultrices neque ornare aenean euismod elementum nisi. Sit amet consectetur sed id semper risus in.
Eget nullam non nisi est. A diam maecenas sed enim. Enim nec dui nunc mattis. Lectus quam id leo in vitae turpis massa sed Eget nullam non nisi est. A diam maecenas sed enim. Enim nec dui nunc mattis. Lectus quam id leo in vitae turpis massa sed
. In eu mi bibendum neque egestas congue. Dui faucibus in ornare quam viverra orci j . In eu mi bibendum neque egestas congue. Dui faucibus in ornare quam viverra orci j
sagittis. Lectus sit amet est placerat in egestas erat imperdiet. sagittis. Lectus sit amet est placerat in egestas erat imperdiet.
Suspendisse potenti nullam ac tortor. Iaculis nunc sed augue lacus viverra vitae congue eu consequat. Suspendisse potenti nullam ac tortor. Iaculis nunc sed augue lacus viverra vitae congue eu consequat.
Lacus vestibulum sed arcu Lacus vestibulum sed arcu
non odio euismod. Massa sed elementum tempus egestas sed. Nulla facilisi etiam dignissim diam quis enim non odio euismod. Massa sed elementum tempus egestas sed. Nulla facilisi etiam dignissim diam quis enim
. Ac ut consequat semper viverra. Eleifend quam adipiscing vitae proin sagittis nisl rhoncus mattis rhoncus. Nunc consequat interdum varius sit amet mattis vulputate enim. . Ac ut consequat semper viverra. Eleifend quam adipiscing vitae proin sagittis nisl rhoncus mattis rhoncus. Nunc consequat interdum varius sit amet mattis vulputate enim.
Orci nulla pellentesque dignissim enim sit amet. Sed vulputate mi sit amet. Orci nulla pellentesque dignissim enim sit amet. Sed vulputate mi sit amet.
Sagittis vitae et leo duis ut diam. Orci a scelerisque purus semper eget duis Sagittis vitae et leo duis ut diam. Orci a scelerisque purus semper eget duis
at tellus at. In hac habitasse platea dictumst vestibulum rhoncus est at tellus at. In hac habitasse platea dictumst vestibulum rhoncus est
. Fames . Fames
ac turpis egestas integer. Mattis enim ut ac turpis egestas integer. Mattis enim ut
tellus elementum sagittis vitae. Pellentesque pulvinar pellentesque habitant morbi tristique senectus et netus tellus elementum sagittis vitae. Pellentesque pulvinar pellentesque habitant morbi tristique senectus et netus
et. Id semper risus in hendrerit. et. Id semper risus in hendrerit.
Et sollicitudin ac orci phasellus egestas. Sem integer vitae justo eget Et sollicitudin ac orci phasellus egestas. Sem integer vitae justo eget
magna. Et ligula ullamcorper malesuada proin libero nunc consequat.-----BEGIN CERTIFICATE----- magna. Et ligula ullamcorper malesuada proin libero nunc consequat.-----BEGIN CERTIFICATE-----
MIICMzCCAZygAwIBAgIJALiPnVsvq8dsMA0GCSqGSIb3DQEBBQUAMFMxCzAJBgNV MIICMzCCAZygAwIBAgIJALiPnVsvq8dsMA0GCSqGSIb3DQEBBQUAMFMxCzAJBgNV
BAYTAlVTMQwwCgYDVQQIEwNmb28xDDAKBgNVBAcTA2ZvbzEMMAoGA1UEChMDZm9v BAYTAlVTMQwwCgYDVQQIEwNmb28xDDAKBgNVBAcTA2ZvbzEMMAoGA1UEChMDZm9v
@ -662,7 +679,7 @@ BEGIN BEGIN BEGIN
-----END RSA PRIVATE KEY -----END RSA PRIVATE KEY
: fkalsdjflkasdjf : fkalsdjflkasdjf
private key: fsaddf', private key: fsaddf',
' '
fdsjaf888888888888 fdsjaf888888888888
----- -----