@@ -59,16 +59,17 @@ impl TryFrom<Attribute> for BasicConstraints {
5959 . into ( ) ) ;
6060 }
6161 let values = value. values ;
62- if values. len ( ) != 1usize {
62+ let element = if let Some ( item) = values. get ( 0 ) {
63+ item
64+ } else {
6365 return Err ( CertificateConversionError :: InvalidInput (
6466 InvalidInput :: Length {
6567 min_length : 1 ,
6668 max_length : 1 ,
6769 actual_length : values. len ( ) . to_string ( ) ,
6870 } ,
6971 ) ) ;
70- }
71- let element = values. get ( 0 ) . expect ( "This should be infallible. Report this issue at https://github.com/polyphony-chat/polyproto" ) ;
72+ } ;
7273 if element. tag ( ) != Tag :: Sequence {
7374 return Err ( CertificateConversionError :: InvalidInput (
7475 InvalidInput :: Malformed ( format ! (
@@ -86,6 +87,7 @@ impl TryFrom<Attribute> for BasicConstraints {
8687 match value. tag ( ) {
8788 Tag :: Boolean => {
8889 // Keep track of how many Boolean tags we encounter
90+ #[ allow( clippy:: arithmetic_side_effects) ] // last i checked, 0 + 1 = 1 < 255
8991 if num_ca == 0 {
9092 num_ca += 1 ;
9193 ca = any_to_bool ( value. clone ( ) ) ?;
@@ -99,6 +101,7 @@ impl TryFrom<Attribute> for BasicConstraints {
99101 }
100102 Tag :: Integer => {
101103 // Keep track of how many Integer tags we encounter
104+ #[ allow( clippy:: arithmetic_side_effects) ]
102105 if num_path_length == 0 {
103106 num_path_length += 1 ;
104107 path_length = Some ( any_to_u64 ( value. clone ( ) ) ?) ;
@@ -218,16 +221,62 @@ impl TryFrom<Extension> for BasicConstraints {
218221 let mut path_length: Option < u64 > = None ;
219222 for item in sequence. iter ( ) {
220223 match item. tag ( ) {
224+ // TODO: lots of repetition. I do not like repetition.
221225 Tag :: Boolean => {
222- bool_encounters += 1 ;
226+ bool_encounters = match bool_encounters. checked_add ( 1 ) {
227+ Some ( new) => new,
228+ None => return Err ( CertificateConversionError :: InvalidCert (
229+ crate :: errors:: InvalidCert :: InvalidProperties (
230+ ConstraintError :: OutOfBounds
231+ {
232+ lower : 0 ,
233+ upper : 255 ,
234+ actual : "> 255" . to_owned ( ) ,
235+ reason :
236+ "Encountered a suspicious amount of tags in this certificate extension"
237+ . to_owned ( )
238+ } )
239+ )
240+ ) ,
241+ } ;
223242 ca = any_to_bool ( item. clone ( ) ) ?;
224243 }
225244 Tag :: Integer => {
226- int_encounters += 1 ;
245+ int_encounters = match int_encounters. checked_add ( 1 ) {
246+ Some ( new) => new,
247+ None => return Err ( CertificateConversionError :: InvalidCert (
248+ crate :: errors:: InvalidCert :: InvalidProperties (
249+ ConstraintError :: OutOfBounds
250+ {
251+ lower : 0 ,
252+ upper : 255 ,
253+ actual : "> 255" . to_owned ( ) ,
254+ reason :
255+ "Encountered a suspicious amount of tags in this certificate extension"
256+ . to_owned ( )
257+ } )
258+ )
259+ ) ,
260+ } ;
227261 path_length = Some ( any_to_u64 ( item. clone ( ) ) ?) ;
228262 }
229263 Tag :: Null => {
230- null_encounters += 1 ;
264+ null_encounters = match null_encounters. checked_add ( 1 ) {
265+ Some ( new) => new,
266+ None => return Err ( CertificateConversionError :: InvalidCert (
267+ crate :: errors:: InvalidCert :: InvalidProperties (
268+ ConstraintError :: OutOfBounds
269+ {
270+ lower : 0 ,
271+ upper : 255 ,
272+ actual : "> 255" . to_owned ( ) ,
273+ reason :
274+ "Encountered a suspicious amount of tags in this certificate extension"
275+ . to_owned ( )
276+ } )
277+ )
278+ ) ,
279+ } ;
231280 path_length = None ;
232281 }
233282 _ => {
@@ -291,6 +340,7 @@ fn any_to_u64(value: Any) -> Result<u64, ConstraintError> {
291340 // into a u64.
292341 let mut buf = [ 0u8 ; 8 ] ;
293342 let len = 8 . min ( value. value ( ) . len ( ) ) ;
343+ #[ allow( clippy:: indexing_slicing) ] // This is fine as long as len <= 8, which is true.
294344 buf[ ..len] . copy_from_slice ( value. value ( ) ) ;
295345 Ok ( u64:: from_be_bytes ( buf) )
296346 }
0 commit comments