asn1.go 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. package cert
  2. import (
  3. "golang.org/x/crypto/cryptobyte"
  4. "golang.org/x/crypto/cryptobyte/asn1"
  5. )
  6. // readOptionalASN1Boolean reads an asn.1 boolean with a specific tag instead of a asn.1 tag wrapping a boolean with a value
  7. // https://github.com/golang/go/issues/64811#issuecomment-1944446920
  8. func readOptionalASN1Boolean(b *cryptobyte.String, out *bool, tag asn1.Tag, defaultValue bool) bool {
  9. var present bool
  10. var child cryptobyte.String
  11. if !b.ReadOptionalASN1(&child, &present, tag) {
  12. return false
  13. }
  14. if !present {
  15. *out = defaultValue
  16. return true
  17. }
  18. // Ensure we have 1 byte
  19. if len(child) == 1 {
  20. *out = child[0] > 0
  21. return true
  22. }
  23. return false
  24. }
  25. // readOptionalASN1Byte reads an asn.1 uint8 with a specific tag instead of a asn.1 tag wrapping a uint8 with a value
  26. // Similar issue as with readOptionalASN1Boolean
  27. func readOptionalASN1Byte(b *cryptobyte.String, out *byte, tag asn1.Tag, defaultValue byte) bool {
  28. var present bool
  29. var child cryptobyte.String
  30. if !b.ReadOptionalASN1(&child, &present, tag) {
  31. return false
  32. }
  33. if !present {
  34. *out = defaultValue
  35. return true
  36. }
  37. // Ensure we have 1 byte
  38. if len(child) == 1 {
  39. *out = child[0]
  40. return true
  41. }
  42. return false
  43. }