bmp-string.go 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. // Copyright 2015 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package pkcs12
  5. import (
  6. "errors"
  7. "unicode/utf16"
  8. )
  9. // bmpString returns s encoded in UCS-2 with a zero terminator.
  10. func bmpString(s string) ([]byte, error) {
  11. // References:
  12. // https://tools.ietf.org/html/rfc7292#appendix-B.1
  13. // https://en.wikipedia.org/wiki/Plane_(Unicode)#Basic_Multilingual_Plane
  14. // - non-BMP characters are encoded in UTF 16 by using a surrogate pair of 16-bit codes
  15. // EncodeRune returns 0xfffd if the rune does not need special encoding
  16. // - the above RFC provides the info that BMPStrings are NULL terminated.
  17. ret := make([]byte, 0, 2*len(s)+2)
  18. for _, r := range s {
  19. if t, _ := utf16.EncodeRune(r); t != 0xfffd {
  20. return nil, errors.New("pkcs12: string contains characters that cannot be encoded in UCS-2")
  21. }
  22. ret = append(ret, byte(r/256), byte(r%256))
  23. }
  24. return append(ret, 0, 0), nil
  25. }
  26. func decodeBMPString(bmpString []byte) (string, error) {
  27. if len(bmpString)%2 != 0 {
  28. return "", errors.New("pkcs12: odd-length BMP string")
  29. }
  30. // strip terminator if present
  31. if l := len(bmpString); l >= 2 && bmpString[l-1] == 0 && bmpString[l-2] == 0 {
  32. bmpString = bmpString[:l-2]
  33. }
  34. s := make([]uint16, 0, len(bmpString)/2)
  35. for len(bmpString) > 0 {
  36. s = append(s, uint16(bmpString[0])<<8+uint16(bmpString[1]))
  37. bmpString = bmpString[2:]
  38. }
  39. return string(utf16.Decode(s)), nil
  40. }