cert_v1_test.go 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. package cert
  2. import (
  3. "fmt"
  4. "net/netip"
  5. "testing"
  6. "time"
  7. "github.com/slackhq/nebula/test"
  8. "github.com/stretchr/testify/assert"
  9. "github.com/stretchr/testify/require"
  10. "google.golang.org/protobuf/proto"
  11. )
  12. func TestCertificateV1_Marshal(t *testing.T) {
  13. before := time.Now().Add(time.Second * -60).Round(time.Second)
  14. after := time.Now().Add(time.Second * 60).Round(time.Second)
  15. pubKey := []byte("1234567890abcedfghij1234567890ab")
  16. nc := certificateV1{
  17. details: detailsV1{
  18. name: "testing",
  19. networks: []netip.Prefix{
  20. mustParsePrefixUnmapped("10.1.1.1/24"),
  21. mustParsePrefixUnmapped("10.1.1.2/16"),
  22. },
  23. unsafeNetworks: []netip.Prefix{
  24. mustParsePrefixUnmapped("9.1.1.2/24"),
  25. mustParsePrefixUnmapped("9.1.1.3/16"),
  26. },
  27. groups: []string{"test-group1", "test-group2", "test-group3"},
  28. notBefore: before,
  29. notAfter: after,
  30. publicKey: pubKey,
  31. isCA: false,
  32. issuer: "1234567890abcedfghij1234567890ab",
  33. },
  34. signature: []byte("1234567890abcedfghij1234567890ab"),
  35. }
  36. b, err := nc.Marshal()
  37. require.NoError(t, err)
  38. //t.Log("Cert size:", len(b))
  39. nc2, err := unmarshalCertificateV1(b, nil)
  40. require.NoError(t, err)
  41. assert.Equal(t, Version1, nc.Version())
  42. assert.Equal(t, Curve_CURVE25519, nc.Curve())
  43. assert.Equal(t, nc.Signature(), nc2.Signature())
  44. assert.Equal(t, nc.Name(), nc2.Name())
  45. assert.Equal(t, nc.NotBefore(), nc2.NotBefore())
  46. assert.Equal(t, nc.NotAfter(), nc2.NotAfter())
  47. assert.Equal(t, nc.PublicKey(), nc2.PublicKey())
  48. assert.Equal(t, nc.IsCA(), nc2.IsCA())
  49. assert.Equal(t, nc.Networks(), nc2.Networks())
  50. assert.Equal(t, nc.UnsafeNetworks(), nc2.UnsafeNetworks())
  51. assert.Equal(t, nc.Groups(), nc2.Groups())
  52. }
  53. func TestCertificateV1_Expired(t *testing.T) {
  54. nc := certificateV1{
  55. details: detailsV1{
  56. notBefore: time.Now().Add(time.Second * -60).Round(time.Second),
  57. notAfter: time.Now().Add(time.Second * 60).Round(time.Second),
  58. },
  59. }
  60. assert.True(t, nc.Expired(time.Now().Add(time.Hour)))
  61. assert.True(t, nc.Expired(time.Now().Add(-time.Hour)))
  62. assert.False(t, nc.Expired(time.Now()))
  63. }
  64. func TestCertificateV1_MarshalJSON(t *testing.T) {
  65. time.Local = time.UTC
  66. pubKey := []byte("1234567890abcedfghij1234567890ab")
  67. nc := certificateV1{
  68. details: detailsV1{
  69. name: "testing",
  70. networks: []netip.Prefix{
  71. mustParsePrefixUnmapped("10.1.1.1/24"),
  72. mustParsePrefixUnmapped("10.1.1.2/16"),
  73. },
  74. unsafeNetworks: []netip.Prefix{
  75. mustParsePrefixUnmapped("9.1.1.2/24"),
  76. mustParsePrefixUnmapped("9.1.1.3/16"),
  77. },
  78. groups: []string{"test-group1", "test-group2", "test-group3"},
  79. notBefore: time.Date(1, 0, 0, 1, 0, 0, 0, time.UTC),
  80. notAfter: time.Date(1, 0, 0, 2, 0, 0, 0, time.UTC),
  81. publicKey: pubKey,
  82. isCA: false,
  83. issuer: "1234567890abcedfghij1234567890ab",
  84. },
  85. signature: []byte("1234567890abcedfghij1234567890ab"),
  86. }
  87. b, err := nc.MarshalJSON()
  88. require.NoError(t, err)
  89. assert.JSONEq(
  90. t,
  91. "{\"details\":{\"curve\":\"CURVE25519\",\"groups\":[\"test-group1\",\"test-group2\",\"test-group3\"],\"isCa\":false,\"issuer\":\"1234567890abcedfghij1234567890ab\",\"name\":\"testing\",\"networks\":[\"10.1.1.1/24\",\"10.1.1.2/16\"],\"notAfter\":\"0000-11-30T02:00:00Z\",\"notBefore\":\"0000-11-30T01:00:00Z\",\"publicKey\":\"313233343536373839306162636564666768696a313233343536373839306162\",\"unsafeNetworks\":[\"9.1.1.2/24\",\"9.1.1.3/16\"]},\"fingerprint\":\"3944c53d4267a229295b56cb2d27d459164c010ac97d655063ba421e0670f4ba\",\"signature\":\"313233343536373839306162636564666768696a313233343536373839306162\",\"version\":1}",
  92. string(b),
  93. )
  94. }
  95. func TestCertificateV1_VerifyPrivateKey(t *testing.T) {
  96. ca, _, caKey, _ := NewTestCaCert(Version1, Curve_CURVE25519, time.Time{}, time.Time{}, nil, nil, nil)
  97. err := ca.VerifyPrivateKey(Curve_CURVE25519, caKey)
  98. require.NoError(t, err)
  99. _, _, caKey2, _ := NewTestCaCert(Version1, Curve_CURVE25519, time.Time{}, time.Time{}, nil, nil, nil)
  100. require.NoError(t, err)
  101. err = ca.VerifyPrivateKey(Curve_CURVE25519, caKey2)
  102. require.Error(t, err)
  103. c, _, priv, _ := NewTestCert(Version1, Curve_CURVE25519, ca, caKey, "test", time.Time{}, time.Time{}, nil, nil, nil)
  104. rawPriv, b, curve, err := UnmarshalPrivateKeyFromPEM(priv)
  105. require.NoError(t, err)
  106. assert.Empty(t, b)
  107. assert.Equal(t, Curve_CURVE25519, curve)
  108. err = c.VerifyPrivateKey(Curve_CURVE25519, rawPriv)
  109. require.NoError(t, err)
  110. _, priv2 := X25519Keypair()
  111. err = c.VerifyPrivateKey(Curve_CURVE25519, priv2)
  112. require.Error(t, err)
  113. }
  114. func TestCertificateV1_VerifyPrivateKeyP256(t *testing.T) {
  115. ca, _, caKey, _ := NewTestCaCert(Version1, Curve_P256, time.Time{}, time.Time{}, nil, nil, nil)
  116. err := ca.VerifyPrivateKey(Curve_P256, caKey)
  117. require.NoError(t, err)
  118. _, _, caKey2, _ := NewTestCaCert(Version1, Curve_P256, time.Time{}, time.Time{}, nil, nil, nil)
  119. require.NoError(t, err)
  120. err = ca.VerifyPrivateKey(Curve_P256, caKey2)
  121. require.Error(t, err)
  122. c, _, priv, _ := NewTestCert(Version1, Curve_P256, ca, caKey, "test", time.Time{}, time.Time{}, nil, nil, nil)
  123. rawPriv, b, curve, err := UnmarshalPrivateKeyFromPEM(priv)
  124. require.NoError(t, err)
  125. assert.Empty(t, b)
  126. assert.Equal(t, Curve_P256, curve)
  127. err = c.VerifyPrivateKey(Curve_P256, rawPriv)
  128. require.NoError(t, err)
  129. _, priv2 := P256Keypair()
  130. err = c.VerifyPrivateKey(Curve_P256, priv2)
  131. require.Error(t, err)
  132. }
  133. // Ensure that upgrading the protobuf library does not change how certificates
  134. // are marshalled, since this would break signature verification
  135. func TestMarshalingCertificateV1Consistency(t *testing.T) {
  136. before := time.Date(1970, time.January, 1, 1, 1, 1, 1, time.UTC)
  137. after := time.Date(9999, time.January, 1, 1, 1, 1, 1, time.UTC)
  138. pubKey := []byte("1234567890abcedfghij1234567890ab")
  139. nc := certificateV1{
  140. details: detailsV1{
  141. name: "testing",
  142. networks: []netip.Prefix{
  143. mustParsePrefixUnmapped("10.1.1.2/16"),
  144. mustParsePrefixUnmapped("10.1.1.1/24"),
  145. },
  146. unsafeNetworks: []netip.Prefix{
  147. mustParsePrefixUnmapped("9.1.1.3/16"),
  148. mustParsePrefixUnmapped("9.1.1.2/24"),
  149. },
  150. groups: []string{"test-group1", "test-group2", "test-group3"},
  151. notBefore: before,
  152. notAfter: after,
  153. publicKey: pubKey,
  154. isCA: false,
  155. issuer: "1234567890abcedfghij1234567890ab",
  156. },
  157. signature: []byte("1234567890abcedfghij1234567890ab"),
  158. }
  159. b, err := nc.Marshal()
  160. require.NoError(t, err)
  161. assert.Equal(t, "0a8e010a0774657374696e671212828284508080fcff0f8182845080feffff0f1a12838284488080fcff0f8282844880feffff0f220b746573742d67726f757031220b746573742d67726f757032220b746573742d67726f75703328cd1c30cdb8ccf0af073a20313233343536373839306162636564666768696a3132333435363738393061624a081234567890abcedf1220313233343536373839306162636564666768696a313233343536373839306162", fmt.Sprintf("%x", b))
  162. b, err = proto.Marshal(nc.getRawDetails())
  163. require.NoError(t, err)
  164. assert.Equal(t, "0a0774657374696e671212828284508080fcff0f8182845080feffff0f1a12838284488080fcff0f8282844880feffff0f220b746573742d67726f757031220b746573742d67726f757032220b746573742d67726f75703328cd1c30cdb8ccf0af073a20313233343536373839306162636564666768696a3132333435363738393061624a081234567890abcedf", fmt.Sprintf("%x", b))
  165. }
  166. func TestCertificateV1_Copy(t *testing.T) {
  167. ca, _, caKey, _ := NewTestCaCert(Version1, Curve_CURVE25519, time.Now(), time.Now().Add(10*time.Minute), nil, nil, nil)
  168. c, _, _, _ := NewTestCert(Version1, Curve_CURVE25519, ca, caKey, "test", time.Now(), time.Now().Add(5*time.Minute), nil, nil, nil)
  169. cc := c.Copy()
  170. test.AssertDeepCopyEqual(t, c, cc)
  171. }
  172. func TestUnmarshalCertificateV1(t *testing.T) {
  173. // Test that we don't panic with an invalid certificate (#332)
  174. data := []byte("\x98\x00\x00")
  175. _, err := unmarshalCertificateV1(data, nil)
  176. require.EqualError(t, err, "encoded Details was nil")
  177. }
  178. func appendByteSlices(b ...[]byte) []byte {
  179. retSlice := []byte{}
  180. for _, v := range b {
  181. retSlice = append(retSlice, v...)
  182. }
  183. return retSlice
  184. }
  185. func mustParsePrefixUnmapped(s string) netip.Prefix {
  186. prefix := netip.MustParsePrefix(s)
  187. return netip.PrefixFrom(prefix.Addr().Unmap(), prefix.Bits())
  188. }