encoding.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. /*-
  2. * Copyright 2014 Square Inc.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package jose
  17. import (
  18. "bytes"
  19. "compress/flate"
  20. "encoding/base64"
  21. "encoding/binary"
  22. "io"
  23. "math/big"
  24. "strings"
  25. "unicode"
  26. "gopkg.in/square/go-jose.v2/json"
  27. )
  28. // Helper function to serialize known-good objects.
  29. // Precondition: value is not a nil pointer.
  30. func mustSerializeJSON(value interface{}) []byte {
  31. out, err := json.Marshal(value)
  32. if err != nil {
  33. panic(err)
  34. }
  35. // We never want to serialize the top-level value "null," since it's not a
  36. // valid JOSE message. But if a caller passes in a nil pointer to this method,
  37. // MarshalJSON will happily serialize it as the top-level value "null". If
  38. // that value is then embedded in another operation, for instance by being
  39. // base64-encoded and fed as input to a signing algorithm
  40. // (https://github.com/square/go-jose/issues/22), the result will be
  41. // incorrect. Because this method is intended for known-good objects, and a nil
  42. // pointer is not a known-good object, we are free to panic in this case.
  43. // Note: It's not possible to directly check whether the data pointed at by an
  44. // interface is a nil pointer, so we do this hacky workaround.
  45. // https://groups.google.com/forum/#!topic/golang-nuts/wnH302gBa4I
  46. if string(out) == "null" {
  47. panic("Tried to serialize a nil pointer.")
  48. }
  49. return out
  50. }
  51. // Strip all newlines and whitespace
  52. func stripWhitespace(data string) string {
  53. buf := strings.Builder{}
  54. buf.Grow(len(data))
  55. for _, r := range data {
  56. if !unicode.IsSpace(r) {
  57. buf.WriteRune(r)
  58. }
  59. }
  60. return buf.String()
  61. }
  62. // Perform compression based on algorithm
  63. func compress(algorithm CompressionAlgorithm, input []byte) ([]byte, error) {
  64. switch algorithm {
  65. case DEFLATE:
  66. return deflate(input)
  67. default:
  68. return nil, ErrUnsupportedAlgorithm
  69. }
  70. }
  71. // Perform decompression based on algorithm
  72. func decompress(algorithm CompressionAlgorithm, input []byte) ([]byte, error) {
  73. switch algorithm {
  74. case DEFLATE:
  75. return inflate(input)
  76. default:
  77. return nil, ErrUnsupportedAlgorithm
  78. }
  79. }
  80. // Compress with DEFLATE
  81. func deflate(input []byte) ([]byte, error) {
  82. output := new(bytes.Buffer)
  83. // Writing to byte buffer, err is always nil
  84. writer, _ := flate.NewWriter(output, 1)
  85. _, _ = io.Copy(writer, bytes.NewBuffer(input))
  86. err := writer.Close()
  87. return output.Bytes(), err
  88. }
  89. // Decompress with DEFLATE
  90. func inflate(input []byte) ([]byte, error) {
  91. output := new(bytes.Buffer)
  92. reader := flate.NewReader(bytes.NewBuffer(input))
  93. _, err := io.Copy(output, reader)
  94. if err != nil {
  95. return nil, err
  96. }
  97. err = reader.Close()
  98. return output.Bytes(), err
  99. }
  100. // byteBuffer represents a slice of bytes that can be serialized to url-safe base64.
  101. type byteBuffer struct {
  102. data []byte
  103. }
  104. func newBuffer(data []byte) *byteBuffer {
  105. if data == nil {
  106. return nil
  107. }
  108. return &byteBuffer{
  109. data: data,
  110. }
  111. }
  112. func newFixedSizeBuffer(data []byte, length int) *byteBuffer {
  113. if len(data) > length {
  114. panic("square/go-jose: invalid call to newFixedSizeBuffer (len(data) > length)")
  115. }
  116. pad := make([]byte, length-len(data))
  117. return newBuffer(append(pad, data...))
  118. }
  119. func newBufferFromInt(num uint64) *byteBuffer {
  120. data := make([]byte, 8)
  121. binary.BigEndian.PutUint64(data, num)
  122. return newBuffer(bytes.TrimLeft(data, "\x00"))
  123. }
  124. func (b *byteBuffer) MarshalJSON() ([]byte, error) {
  125. return json.Marshal(b.base64())
  126. }
  127. func (b *byteBuffer) UnmarshalJSON(data []byte) error {
  128. var encoded string
  129. err := json.Unmarshal(data, &encoded)
  130. if err != nil {
  131. return err
  132. }
  133. if encoded == "" {
  134. return nil
  135. }
  136. decoded, err := base64.RawURLEncoding.DecodeString(encoded)
  137. if err != nil {
  138. return err
  139. }
  140. *b = *newBuffer(decoded)
  141. return nil
  142. }
  143. func (b *byteBuffer) base64() string {
  144. return base64.RawURLEncoding.EncodeToString(b.data)
  145. }
  146. func (b *byteBuffer) bytes() []byte {
  147. // Handling nil here allows us to transparently handle nil slices when serializing.
  148. if b == nil {
  149. return nil
  150. }
  151. return b.data
  152. }
  153. func (b byteBuffer) bigInt() *big.Int {
  154. return new(big.Int).SetBytes(b.data)
  155. }
  156. func (b byteBuffer) toInt() int {
  157. return int(b.bigInt().Int64())
  158. }