| 1 | ##---------------------------------------------------------------------------- | ||
| 2 | ## WebAuthn - ~/lib/Web/Authn/CBOR.pm | ||
| 3 | ## Version v0.2.0 | ||
| 4 | ## Copyright(c) 2026 DEGUEST Pte. Ltd. | ||
| 5 | ## Author: Jacques Deguest <jack@deguest.jp> | ||
| 6 | ## Created 2026/09/09 | ||
| 7 | ## Modified 2026/09/12 | ||
| 8 | ## All rights reserved | ||
| 9 | ## | ||
| 10 | ## This program is free software; you can redistribute it and/or modify it | ||
| 11 | ## under the same terms as Perl itself. | ||
| 12 | ##---------------------------------------------------------------------------- | ||
| 13 | package Web::Authn::CBOR; | ||
| 14 | BEGIN | ||
| 15 | { | ||
| 16 | use v5.16.0; | ||
| 17 | use strict; | ||
| 18 | use warnings; | ||
| 19 | warnings::register_categories( 'Web::Authn' ); | ||
| 20 | use vars qw( $VERSION ); | ||
| 21 | use overload (); | ||
| 22 | use Scalar::Util (); | ||
| 23 | use Web::Authn::Exception; | ||
| 24 | our $VERSION = 'v0.2.0'; | ||
| 25 | }; | ||
| 26 | |||
| 27 | use strict; | ||
| 28 | use warnings; | ||
| 29 | |||
| 30 | # Minimal CTAP2-oriented CBOR codec. Maps keep integer keys as Perl integers. | ||
| 31 | # decode() returns ($value, $bytes_consumed). | ||
| 32 | |||
| 33 | sub decode | ||
| 34 | { | ||
| 35 | my $bytes = shift( @_ ); | ||
| 36 | unless( defined( $bytes ) ) | ||
| 37 | { | ||
| 38 | _err( 'empty CBOR input' ); | ||
| 39 | } | ||
| 40 | my( $val, $off ) = _decode( $bytes, 0 ); | ||
| 41 | return( wantarray ? ( $val, $off ) : $val ); | ||
| 42 | } | ||
| 43 | |||
| 44 | sub encode | ||
| 45 | { | ||
| 46 | my $val = shift( @_ ); | ||
| 47 | return( _encode( $val ) ); | ||
| 48 | } | ||
| 49 | |||
| 50 | sub encode_bstr | ||
| 51 | { | ||
| 52 | my $s = shift( @_ ); | ||
| 53 | return( _uint( 2, length( $s ) ) . $s ); | ||
| 54 | } | ||
| 55 | |||
| 56 | sub _addl | ||
| 57 | { | ||
| 58 | my( $buf, $off, $ai ) = @_; | ||
| 59 | return( $ai, $off ) if( $ai < 24 ); | ||
| 60 | if( $ai == 24 ) | ||
| 61 | { | ||
| 62 | unless( $off < length( $buf ) ) | ||
| 63 | { | ||
| 64 | _err( 'truncated' ); | ||
| 65 | } | ||
| 66 | return( unpack( 'C', substr( $buf, $off, 1 ) ), $off + 1 ); | ||
| 67 | } | ||
| 68 | elsif( $ai == 25 ) | ||
| 69 | { | ||
| 70 | unless( $off + 2 <= length( $buf ) ) | ||
| 71 | { | ||
| 72 | _err( 'truncated' ); | ||
| 73 | } | ||
| 74 | return( unpack( 'n', substr( $buf, $off, 2 ) ), $off + 2 ); | ||
| 75 | } | ||
| 76 | elsif( $ai == 26 ) | ||
| 77 | { | ||
| 78 | unless( $off + 4 <= length( $buf ) ) | ||
| 79 | { | ||
| 80 | _err( 'truncated' ); | ||
| 81 | } | ||
| 82 | return( unpack( 'N', substr( $buf, $off, 4 ) ), $off + 4 ); | ||
| 83 | } | ||
| 84 | elsif( $ai == 27 ) | ||
| 85 | { | ||
| 86 | unless( $off + 8 <= length( $buf ) ) | ||
| 87 | { | ||
| 88 | _err( 'truncated' ); | ||
| 89 | } | ||
| 90 | my( $hi, $lo ) = unpack( 'NN', substr( $buf, $off, 8 ) ); | ||
| 91 | return( $hi * 2 ** 32 + $lo, $off + 8 ); | ||
| 92 | } | ||
| 93 | _err( "indefinite or reserved additional info $ai" ); | ||
| 94 | } | ||
| 95 | |||
| 96 | sub _decode | ||
| 97 | { | ||
| 98 | my( $buf, $off ) = @_; | ||
| 99 | unless( $off < length( $buf ) ) | ||
| 100 | { | ||
| 101 | _err( 'truncated' ); | ||
| 102 | } | ||
| 103 | my $ib = unpack( 'C', substr( $buf, $off, 1 ) ); | ||
| 104 | my $major = $ib >> 5; | ||
| 105 | my $ai = $ib & 31; | ||
| 106 | $off++; | ||
| 107 | |||
| 108 | my( $n, $noff ) = _addl( $buf, $off, $ai ); | ||
| 109 | $off = $noff; | ||
| 110 | |||
| 111 | if( $major == 0 ) | ||
| 112 | { | ||
| 113 | return( $n, $off ); | ||
| 114 | } | ||
| 115 | elsif( $major == 1 ) | ||
| 116 | { | ||
| 117 | return( ( -1 - $n ), $off ); | ||
| 118 | } | ||
| 119 | elsif( $major == 2 ) | ||
| 120 | { | ||
| 121 | unless( $off + $n <= length( $buf ) ) | ||
| 122 | { | ||
| 123 | _err( 'truncated bstr' ); | ||
| 124 | } | ||
| 125 | return( substr( $buf, $off, $n ), $off + $n ); | ||
| 126 | } | ||
| 127 | elsif( $major == 3 ) | ||
| 128 | { | ||
| 129 | unless( $off + $n <= length( $buf ) ) | ||
| 130 | { | ||
| 131 | _err( 'truncated tstr' ); | ||
| 132 | } | ||
| 133 | return( substr( $buf, $off, $n ), $off + $n ); | ||
| 134 | } | ||
| 135 | elsif( $major == 4 ) | ||
| 136 | { | ||
| 137 | my @arr; | ||
| 138 | for( 1 .. $n ) | ||
| 139 | { | ||
| 140 | my( $v, $o ) = _decode( $buf, $off ); | ||
| 141 | push( @arr, $v ); | ||
| 142 | $off = $o; | ||
| 143 | } | ||
| 144 | return( \@arr, $off ); | ||
| 145 | } | ||
| 146 | elsif( $major == 5 ) | ||
| 147 | { | ||
| 148 | my %map; | ||
| 149 | for( 1 .. $n ) | ||
| 150 | { | ||
| 151 | my( $k, $o1 ) = _decode( $buf, $off ); | ||
| 152 | my( $v, $o2 ) = _decode( $buf, $o1 ); | ||
| 153 | if( exists( $map{ $k } ) ) | ||
| 154 | { | ||
| 155 | _err( 'duplicate map key' ); | ||
| 156 | } | ||
| 157 | $map{ $k } = $v; | ||
| 158 | $off = $o2; | ||
| 159 | } | ||
| 160 | return( \%map, $off ); | ||
| 161 | } | ||
| 162 | elsif( $major == 6 ) | ||
| 163 | { | ||
| 164 | # Tag: decode tagged value and ignore tag number | ||
| 165 | my( $v, $o ) = _decode( $buf, $off ); | ||
| 166 | return( $v, $o ); | ||
| 167 | } | ||
| 168 | elsif( $major == 7 ) | ||
| 169 | { | ||
| 170 | # false as 0 | ||
| 171 | if( $ai == 20 ) | ||
| 172 | { | ||
| 173 | return( 0, $off ); | ||
| 174 | } | ||
| 175 | # true as 1 | ||
| 176 | elsif( $ai == 21 ) | ||
| 177 | { | ||
| 178 | return( 1, $off ); | ||
| 179 | } | ||
| 180 | # null | ||
| 181 | elsif( $ai == 22 ) | ||
| 182 | { | ||
| 183 | return( undef, $off ); | ||
| 184 | } | ||
| 185 | # undefined | ||
| 186 | elsif( $ai == 23 ) | ||
| 187 | { | ||
| 188 | return( undef, $off ); | ||
| 189 | } | ||
| 190 | _err( "unsupported simple/float ai=$ai" ); | ||
| 191 | } | ||
| 192 | _err( "unsupported major $major" ); | ||
| 193 | } | ||
| 194 | |||
| 195 | sub _encode | ||
| 196 | { | ||
| 197 | my $v = shift( @_ ); | ||
| 198 | return( _head( 7, 22 ) ) unless( defined( $v ) ); | ||
| 199 | my $r = Scalar::Util::reftype( $v ) || ''; | ||
| 200 | if( !$r ) | ||
| 201 | { | ||
| 202 | if( $v =~ /^-?\d+\z/ ) | ||
| 203 | { | ||
| 204 | return( $v >= 0 ? _uint( 0, $v ) : _uint( 1, -1 - $v ) ); | ||
| 205 | } | ||
| 206 | # treat as text if utf8-looking printable, else bytes | ||
| 207 | if( $v =~ /[^\x00-\x7F]/ || $v =~ /^[\x20-\x7E]*\z/ ) | ||
| 208 | { | ||
| 209 | return( _uint( 3, length( $v ) ) . $v ); | ||
| 210 | } | ||
| 211 | return( _uint( 2, length( $v ) ) . $v ); | ||
| 212 | } | ||
| 213 | elsif( $r eq 'SCALAR' && !Scalar::Util::blessed( $v ) ) | ||
| 214 | { | ||
| 215 | # \bytes for explicit bstr | ||
| 216 | my $s = $$v; | ||
| 217 | return( _uint( 2, length( $s ) ) . $s ); | ||
| 218 | } | ||
| 219 | elsif( $r eq 'ARRAY' ) | ||
| 220 | { | ||
| 221 | my $out = _uint( 4, scalar( @$v ) ); | ||
| 222 | $out .= _encode( $_ ) for( @$v ); | ||
| 223 | return( $out ); | ||
| 224 | } | ||
| 225 | elsif( $r eq 'HASH' ) | ||
| 226 | { | ||
| 227 | my @keys = sort{ _key_sort( $a, $b ) } keys( %$v ); | ||
| 228 | my $out = _uint( 5, scalar( @keys ) ); | ||
| 229 | foreach my $k ( @keys ) | ||
| 230 | { | ||
| 231 | my $nk = ( $k =~ /^-?\d+\z/ ) ? 0 + $k : $k; | ||
| 232 | $out .= _encode( $nk ); | ||
| 233 | $out .= _encode( $v->{ $k } ); | ||
| 234 | } | ||
| 235 | return( $out ); | ||
| 236 | } | ||
| 237 | elsif( Scalar::Util::blessed( $v ) && overload::Method( $v, '""' ) ) | ||
| 238 | { | ||
| 239 | return( _encode( $v . '' ) ); | ||
| 240 | } | ||
| 241 | _err( "cannot encode $r" ); | ||
| 242 | } | ||
| 243 | |||
| 244 | sub _err { Web::Authn::Exception::InvalidStructure->throw( "CBOR: $_[0]" ) } | ||
| 245 | |||
| 246 | sub _head | ||
| 247 | { | ||
| 248 | my( $major, $ai ) = @_; | ||
| 249 | return( pack( 'C', ( $major << 5 ) | $ai ) ); | ||
| 250 | } | ||
| 251 | |||
| 252 | sub _key_sort | ||
| 253 | { | ||
| 254 | my( $a, $b ) = @_; | ||
| 255 | my $an = $a =~ /^-?\d+\z/; | ||
| 256 | my $bn = $b =~ /^-?\d+\z/; | ||
| 257 | return( $a <=> $b ) if( $an && $bn ); | ||
| 258 | return( $a cmp $b ); | ||
| 259 | } | ||
| 260 | |||
| 261 | sub _uint | ||
| 262 | { | ||
| 263 | my( $major, $n ) = @_; | ||
| 264 | return( _head( $major, $n ) ) if( $n < 24 ); | ||
| 265 | return( _head( $major, 24 ) ) . pack( 'C', $n ) if( $n < 256 ); | ||
| 266 | return( _head( $major, 25 ) ) . pack( 'n', $n ) if( $n < 65536 ); | ||
| 267 | return( _head( $major, 26 ) ) . pack( 'N', $n ) if( $n < 2 ** 32 ); | ||
| 268 | my $hi = int( $n / 2 ** 32 ); | ||
| 269 | my $lo = $n - $hi * 2 ** 32; | ||
| 270 | return( _head( $major, 27 ) . pack( 'NN', $hi, $lo ) ); | ||
| 271 | } | ||
| 272 | |||
| 273 | 1; | ||
| 274 | # NOTE: POD | ||
| 275 | __END__ |