encoding.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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. "encoding/json"
  23. "io"
  24. "math/big"
  25. "regexp"
  26. )
  27. var stripWhitespaceRegex = regexp.MustCompile("\\s")
  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. return stripWhitespaceRegex.ReplaceAllString(data, "")
  54. }
  55. // Perform compression based on algorithm
  56. func compress(algorithm CompressionAlgorithm, input []byte) ([]byte, error) {
  57. switch algorithm {
  58. case DEFLATE:
  59. return deflate(input)
  60. default:
  61. return nil, ErrUnsupportedAlgorithm
  62. }
  63. }
  64. // Perform decompression based on algorithm
  65. func decompress(algorithm CompressionAlgorithm, input []byte) ([]byte, error) {
  66. switch algorithm {
  67. case DEFLATE:
  68. return inflate(input)
  69. default:
  70. return nil, ErrUnsupportedAlgorithm
  71. }
  72. }
  73. // Compress with DEFLATE
  74. func deflate(input []byte) ([]byte, error) {
  75. output := new(bytes.Buffer)
  76. // Writing to byte buffer, err is always nil
  77. writer, _ := flate.NewWriter(output, 1)
  78. _, _ = io.Copy(writer, bytes.NewBuffer(input))
  79. err := writer.Close()
  80. return output.Bytes(), err
  81. }
  82. // Decompress with DEFLATE
  83. func inflate(input []byte) ([]byte, error) {
  84. output := new(bytes.Buffer)
  85. reader := flate.NewReader(bytes.NewBuffer(input))
  86. _, err := io.Copy(output, reader)
  87. if err != nil {
  88. return nil, err
  89. }
  90. err = reader.Close()
  91. return output.Bytes(), err
  92. }
  93. // byteBuffer represents a slice of bytes that can be serialized to url-safe base64.
  94. type byteBuffer struct {
  95. data []byte
  96. }
  97. func newBuffer(data []byte) *byteBuffer {
  98. if data == nil {
  99. return nil
  100. }
  101. return &byteBuffer{
  102. data: data,
  103. }
  104. }
  105. func newFixedSizeBuffer(data []byte, length int) *byteBuffer {
  106. if len(data) > length {
  107. panic("square/go-jose: invalid call to newFixedSizeBuffer (len(data) > length)")
  108. }
  109. pad := make([]byte, length-len(data))
  110. return newBuffer(append(pad, data...))
  111. }
  112. func newBufferFromInt(num uint64) *byteBuffer {
  113. data := make([]byte, 8)
  114. binary.BigEndian.PutUint64(data, num)
  115. return newBuffer(bytes.TrimLeft(data, "\x00"))
  116. }
  117. func (b *byteBuffer) MarshalJSON() ([]byte, error) {
  118. return json.Marshal(b.base64())
  119. }
  120. func (b *byteBuffer) UnmarshalJSON(data []byte) error {
  121. var encoded string
  122. err := json.Unmarshal(data, &encoded)
  123. if err != nil {
  124. return err
  125. }
  126. if encoded == "" {
  127. return nil
  128. }
  129. decoded, err := base64.RawURLEncoding.DecodeString(encoded)
  130. if err != nil {
  131. return err
  132. }
  133. *b = *newBuffer(decoded)
  134. return nil
  135. }
  136. func (b *byteBuffer) base64() string {
  137. return base64.RawURLEncoding.EncodeToString(b.data)
  138. }
  139. func (b *byteBuffer) bytes() []byte {
  140. // Handling nil here allows us to transparently handle nil slices when serializing.
  141. if b == nil {
  142. return nil
  143. }
  144. return b.data
  145. }
  146. func (b byteBuffer) bigInt() *big.Int {
  147. return new(big.Int).SetBytes(b.data)
  148. }
  149. func (b byteBuffer) toInt() int {
  150. return int(b.bigInt().Int64())
  151. }