sign_options_test.go 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973
  1. package provisioner
  2. import (
  3. "context"
  4. "crypto/x509"
  5. "crypto/x509/pkix"
  6. "encoding/asn1"
  7. "fmt"
  8. "net"
  9. "net/url"
  10. "strings"
  11. "testing"
  12. "time"
  13. "github.com/pkg/errors"
  14. "github.com/smallstep/assert"
  15. "go.step.sm/crypto/pemutil"
  16. )
  17. func Test_defaultPublicKeyValidator_Valid(t *testing.T) {
  18. _shortRSA, err := pemutil.Read("./testdata/certs/short-rsa.csr")
  19. assert.FatalError(t, err)
  20. shortRSA, ok := _shortRSA.(*x509.CertificateRequest)
  21. assert.Fatal(t, ok)
  22. _rsa, err := pemutil.Read("./testdata/certs/rsa.csr")
  23. assert.FatalError(t, err)
  24. rsaCSR, ok := _rsa.(*x509.CertificateRequest)
  25. assert.Fatal(t, ok)
  26. _ecdsa, err := pemutil.Read("./testdata/certs/ecdsa.csr")
  27. assert.FatalError(t, err)
  28. ecdsaCSR, ok := _ecdsa.(*x509.CertificateRequest)
  29. assert.Fatal(t, ok)
  30. _ed25519, err := pemutil.Read("./testdata/certs/ed25519.csr")
  31. assert.FatalError(t, err)
  32. ed25519CSR, ok := _ed25519.(*x509.CertificateRequest)
  33. assert.Fatal(t, ok)
  34. v := defaultPublicKeyValidator{}
  35. tests := []struct {
  36. name string
  37. csr *x509.CertificateRequest
  38. err error
  39. }{
  40. {
  41. "fail/unrecognized-key-type",
  42. &x509.CertificateRequest{PublicKey: "foo"},
  43. errors.New("certificate request key of type 'string' is not supported"),
  44. },
  45. {
  46. "fail/rsa/too-short",
  47. shortRSA,
  48. errors.New("certificate request RSA key must be at least 2048 bits (256 bytes)"),
  49. },
  50. {
  51. "ok/rsa",
  52. rsaCSR,
  53. nil,
  54. },
  55. {
  56. "ok/ecdsa",
  57. ecdsaCSR,
  58. nil,
  59. },
  60. {
  61. "ok/ed25519",
  62. ed25519CSR,
  63. nil,
  64. },
  65. }
  66. for _, tt := range tests {
  67. t.Run(tt.name, func(t *testing.T) {
  68. if err := v.Valid(tt.csr); err != nil {
  69. if assert.NotNil(t, tt.err) {
  70. assert.HasPrefix(t, err.Error(), tt.err.Error())
  71. }
  72. } else {
  73. assert.Nil(t, tt.err)
  74. }
  75. })
  76. }
  77. }
  78. func Test_commonNameValidator_Valid(t *testing.T) {
  79. type args struct {
  80. req *x509.CertificateRequest
  81. }
  82. tests := []struct {
  83. name string
  84. v commonNameValidator
  85. args args
  86. wantErr bool
  87. }{
  88. {"ok", "foo.bar.zar", args{&x509.CertificateRequest{Subject: pkix.Name{CommonName: "foo.bar.zar"}}}, false},
  89. {"empty", "", args{&x509.CertificateRequest{Subject: pkix.Name{CommonName: ""}}}, false},
  90. {"wrong", "foo.bar.zar", args{&x509.CertificateRequest{Subject: pkix.Name{CommonName: "example.com"}}}, true},
  91. }
  92. for _, tt := range tests {
  93. t.Run(tt.name, func(t *testing.T) {
  94. if err := tt.v.Valid(tt.args.req); (err != nil) != tt.wantErr {
  95. t.Errorf("commonNameValidator.Valid() error = %v, wantErr %v", err, tt.wantErr)
  96. }
  97. })
  98. }
  99. }
  100. func Test_commonNameSliceValidator_Valid(t *testing.T) {
  101. type args struct {
  102. req *x509.CertificateRequest
  103. }
  104. tests := []struct {
  105. name string
  106. v commonNameSliceValidator
  107. args args
  108. wantErr bool
  109. }{
  110. {"ok", []string{"foo.bar.zar"}, args{&x509.CertificateRequest{Subject: pkix.Name{CommonName: "foo.bar.zar"}}}, false},
  111. {"ok", []string{"example.com", "foo.bar.zar"}, args{&x509.CertificateRequest{Subject: pkix.Name{CommonName: "foo.bar.zar"}}}, false},
  112. {"empty", []string{""}, args{&x509.CertificateRequest{Subject: pkix.Name{CommonName: ""}}}, false},
  113. {"wrong", []string{"foo.bar.zar"}, args{&x509.CertificateRequest{Subject: pkix.Name{CommonName: "example.com"}}}, true},
  114. }
  115. for _, tt := range tests {
  116. t.Run(tt.name, func(t *testing.T) {
  117. if err := tt.v.Valid(tt.args.req); (err != nil) != tt.wantErr {
  118. t.Errorf("commonNameSliceValidator.Valid() error = %v, wantErr %v", err, tt.wantErr)
  119. }
  120. })
  121. }
  122. }
  123. func Test_emailAddressesValidator_Valid(t *testing.T) {
  124. type args struct {
  125. req *x509.CertificateRequest
  126. }
  127. tests := []struct {
  128. name string
  129. v emailAddressesValidator
  130. args args
  131. wantErr bool
  132. }{
  133. {"ok0", []string{}, args{&x509.CertificateRequest{EmailAddresses: []string{}}}, false},
  134. {"ok1", []string{"max@smallstep.com"}, args{&x509.CertificateRequest{EmailAddresses: []string{"max@smallstep.com"}}}, false},
  135. {"ok2", []string{"max@step.com", "mike@step.com"}, args{&x509.CertificateRequest{EmailAddresses: []string{"max@step.com", "mike@step.com"}}}, false},
  136. {"ok3", []string{"max@step.com", "mike@step.com"}, args{&x509.CertificateRequest{EmailAddresses: []string{"mike@step.com", "max@step.com"}}}, false},
  137. {"ok3", []string{"max@step.com", "mike@step.com"}, args{&x509.CertificateRequest{}}, false},
  138. {"fail1", []string{"max@step.com"}, args{&x509.CertificateRequest{EmailAddresses: []string{"mike@step.com"}}}, true},
  139. {"fail2", []string{"mike@step.com"}, args{&x509.CertificateRequest{EmailAddresses: []string{"max@step.com", "mike@step.com"}}}, true},
  140. {"fail3", []string{"mike@step.com", "max@step.com"}, args{&x509.CertificateRequest{EmailAddresses: []string{"mike@step.com", "mex@step.com"}}}, true},
  141. }
  142. for _, tt := range tests {
  143. t.Run(tt.name, func(t *testing.T) {
  144. if err := tt.v.Valid(tt.args.req); (err != nil) != tt.wantErr {
  145. t.Errorf("emailAddressesValidator.Valid() error = %v, wantErr %v", err, tt.wantErr)
  146. }
  147. })
  148. }
  149. }
  150. func Test_dnsNamesValidator_Valid(t *testing.T) {
  151. type args struct {
  152. req *x509.CertificateRequest
  153. }
  154. tests := []struct {
  155. name string
  156. v dnsNamesValidator
  157. args args
  158. wantErr bool
  159. }{
  160. {"ok0", []string{}, args{&x509.CertificateRequest{DNSNames: []string{}}}, false},
  161. {"ok1", []string{"foo.bar.zar"}, args{&x509.CertificateRequest{DNSNames: []string{"foo.bar.zar"}}}, false},
  162. {"ok2", []string{"foo.bar.zar", "bar.zar"}, args{&x509.CertificateRequest{DNSNames: []string{"foo.bar.zar", "bar.zar"}}}, false},
  163. {"ok3", []string{"foo.bar.zar", "bar.zar"}, args{&x509.CertificateRequest{DNSNames: []string{"bar.zar", "foo.bar.zar"}}}, false},
  164. {"ok4", []string{"foo.bar.zar", "bar.zar"}, args{&x509.CertificateRequest{}}, false},
  165. {"fail1", []string{"foo.bar.zar"}, args{&x509.CertificateRequest{DNSNames: []string{"bar.zar"}}}, true},
  166. {"fail2", []string{"foo.bar.zar"}, args{&x509.CertificateRequest{DNSNames: []string{"bar.zar", "foo.bar.zar"}}}, true},
  167. {"fail3", []string{"foo.bar.zar", "bar.zar"}, args{&x509.CertificateRequest{DNSNames: []string{"foo.bar.zar", "zar.bar"}}}, true},
  168. }
  169. for _, tt := range tests {
  170. t.Run(tt.name, func(t *testing.T) {
  171. if err := tt.v.Valid(tt.args.req); (err != nil) != tt.wantErr {
  172. t.Errorf("dnsNamesValidator.Valid() error = %v, wantErr %v", err, tt.wantErr)
  173. }
  174. })
  175. }
  176. }
  177. func Test_dnsNamesSubsetValidator_Valid(t *testing.T) {
  178. type args struct {
  179. req *x509.CertificateRequest
  180. }
  181. tests := []struct {
  182. name string
  183. v dnsNamesSubsetValidator
  184. args args
  185. wantErr bool
  186. }{
  187. {"ok0", []string{}, args{&x509.CertificateRequest{DNSNames: []string{}}}, false},
  188. {"ok1", []string{"foo.bar.zar"}, args{&x509.CertificateRequest{DNSNames: []string{"foo.bar.zar"}}}, false},
  189. {"ok2", []string{"foo.bar.zar", "bar.zar"}, args{&x509.CertificateRequest{DNSNames: []string{"foo.bar.zar", "bar.zar"}}}, false},
  190. {"ok3", []string{"foo.bar.zar", "bar.zar"}, args{&x509.CertificateRequest{DNSNames: []string{"bar.zar", "foo.bar.zar"}}}, false},
  191. {"ok4", []string{"foo.bar.zar", "bar.zar"}, args{&x509.CertificateRequest{}}, false},
  192. {"ok5", []string{"foo.bar.zar", "bar.zar"}, args{&x509.CertificateRequest{DNSNames: []string{"bar.zar"}}}, false},
  193. {"ok6", []string{"foo", "bar", "baz", "zar", "zap"}, args{&x509.CertificateRequest{DNSNames: []string{"zap", "baz", "foo"}}}, false},
  194. {"fail1", []string{"foo.bar.zar"}, args{&x509.CertificateRequest{DNSNames: []string{"bar.zar"}}}, true},
  195. {"fail2", []string{"foo.bar.zar"}, args{&x509.CertificateRequest{DNSNames: []string{"bar.zar", "foo.bar.zar"}}}, true},
  196. {"fail3", []string{"foo.bar.zar", "bar.zar"}, args{&x509.CertificateRequest{DNSNames: []string{"foo.bar.zar", "zar.bar"}}}, true},
  197. {"fail4", []string{"foo", "bar", "baz", "zar", "zap"}, args{&x509.CertificateRequest{DNSNames: []string{"zap", "baz", "foO"}}}, true},
  198. {"fail5", []string{"foo", "bar", "baz", "zar", "zap"}, args{&x509.CertificateRequest{DNSNames: []string{"zap", "baz", "fax", "foo"}}}, true},
  199. {"fail6", []string{}, args{&x509.CertificateRequest{DNSNames: []string{"zap", "baz", "fax", "foo"}}}, true},
  200. }
  201. for _, tt := range tests {
  202. t.Run(tt.name, func(t *testing.T) {
  203. if err := tt.v.Valid(tt.args.req); (err != nil) != tt.wantErr {
  204. t.Errorf("dnsNamesSubsetValidator.Valid() error = %v, wantErr %v", err, tt.wantErr)
  205. }
  206. })
  207. }
  208. }
  209. func Test_ipAddressesValidator_Valid(t *testing.T) {
  210. ip1 := net.IPv4(10, 3, 2, 1)
  211. ip2 := net.IPv4(10, 3, 2, 2)
  212. ip3 := net.IPv4(10, 3, 2, 3)
  213. type args struct {
  214. req *x509.CertificateRequest
  215. }
  216. tests := []struct {
  217. name string
  218. v ipAddressesValidator
  219. args args
  220. wantErr bool
  221. }{
  222. {"ok0", []net.IP{}, args{&x509.CertificateRequest{IPAddresses: []net.IP{}}}, false},
  223. {"ok1", []net.IP{ip1}, args{&x509.CertificateRequest{IPAddresses: []net.IP{ip1}}}, false},
  224. {"ok2", []net.IP{ip1, ip2}, args{&x509.CertificateRequest{IPAddresses: []net.IP{ip1, ip2}}}, false},
  225. {"ok3", []net.IP{ip1, ip2}, args{&x509.CertificateRequest{IPAddresses: []net.IP{ip2, ip1}}}, false},
  226. {"ok4", []net.IP{ip1, ip2}, args{&x509.CertificateRequest{}}, false},
  227. {"fail1", []net.IP{ip1}, args{&x509.CertificateRequest{IPAddresses: []net.IP{ip2}}}, true},
  228. {"fail2", []net.IP{ip1}, args{&x509.CertificateRequest{IPAddresses: []net.IP{ip2, ip1}}}, true},
  229. {"fail3", []net.IP{ip1, ip2}, args{&x509.CertificateRequest{IPAddresses: []net.IP{ip1, ip3}}}, true},
  230. }
  231. for _, tt := range tests {
  232. t.Run(tt.name, func(t *testing.T) {
  233. if err := tt.v.Valid(tt.args.req); (err != nil) != tt.wantErr {
  234. t.Errorf("ipAddressesValidator.Valid() error = %v, wantErr %v", err, tt.wantErr)
  235. }
  236. })
  237. }
  238. }
  239. func Test_urisValidator_Valid(t *testing.T) {
  240. u1, err := url.Parse("https://ca.smallstep.com")
  241. assert.FatalError(t, err)
  242. u2, err := url.Parse("https://google.com/index.html")
  243. assert.FatalError(t, err)
  244. u3, err := url.Parse("urn:uuid:ddfe62ba-7e99-4bc1-83b3-8f57fe3e9959")
  245. assert.FatalError(t, err)
  246. fu, err := url.Parse("https://unexpected.com")
  247. assert.FatalError(t, err)
  248. signContext := NewContextWithMethod(context.Background(), SignMethod)
  249. signIdentityContext := NewContextWithMethod(context.Background(), SignIdentityMethod)
  250. type args struct {
  251. req *x509.CertificateRequest
  252. }
  253. tests := []struct {
  254. name string
  255. v *urisValidator
  256. args args
  257. wantErr bool
  258. }{
  259. {"ok0", newURIsValidator(signContext, []*url.URL{}), args{&x509.CertificateRequest{URIs: []*url.URL{}}}, false},
  260. {"ok1", newURIsValidator(signContext, []*url.URL{u1}), args{&x509.CertificateRequest{URIs: []*url.URL{u1}}}, false},
  261. {"ok2", newURIsValidator(signContext, []*url.URL{u1, u2}), args{&x509.CertificateRequest{URIs: []*url.URL{u2, u1}}}, false},
  262. {"ok3", newURIsValidator(signContext, []*url.URL{u2, u1, u3}), args{&x509.CertificateRequest{URIs: []*url.URL{u3, u2, u1}}}, false},
  263. {"ok4", newURIsValidator(signIdentityContext, []*url.URL{u1, u2}), args{&x509.CertificateRequest{URIs: []*url.URL{u1, fu}}}, false},
  264. {"fail1", newURIsValidator(signContext, []*url.URL{u1}), args{&x509.CertificateRequest{URIs: []*url.URL{u2}}}, true},
  265. {"fail2", newURIsValidator(signContext, []*url.URL{u1}), args{&x509.CertificateRequest{URIs: []*url.URL{u2, u1}}}, true},
  266. {"fail3", newURIsValidator(signContext, []*url.URL{u1, u2}), args{&x509.CertificateRequest{URIs: []*url.URL{u1, fu}}}, true},
  267. }
  268. for _, tt := range tests {
  269. t.Run(tt.name, func(t *testing.T) {
  270. if err := tt.v.Valid(tt.args.req); (err != nil) != tt.wantErr {
  271. t.Errorf("urisValidator.Valid() error = %v, wantErr %v", err, tt.wantErr)
  272. }
  273. })
  274. }
  275. }
  276. func Test_defaultSANsValidator_Valid(t *testing.T) {
  277. type test struct {
  278. csr *x509.CertificateRequest
  279. ctx context.Context
  280. expectedSANs []string
  281. err error
  282. }
  283. signContext := NewContextWithMethod(context.Background(), SignMethod)
  284. signIdentityContext := NewContextWithMethod(context.Background(), SignIdentityMethod)
  285. tests := map[string]func() test{
  286. "fail/dnsNamesValidator": func() test {
  287. return test{
  288. csr: &x509.CertificateRequest{DNSNames: []string{"foo", "bar"}},
  289. ctx: signContext,
  290. expectedSANs: []string{"foo"},
  291. err: errors.New("certificate request does not contain the valid DNS names"),
  292. }
  293. },
  294. "fail/emailAddressesValidator": func() test {
  295. return test{
  296. csr: &x509.CertificateRequest{EmailAddresses: []string{"max@fx.com", "mariano@fx.com"}},
  297. ctx: signContext,
  298. expectedSANs: []string{"dcow@fx.com"},
  299. err: errors.New("certificate request does not contain the valid email addresses"),
  300. }
  301. },
  302. "fail/ipAddressesValidator": func() test {
  303. return test{
  304. csr: &x509.CertificateRequest{IPAddresses: []net.IP{net.ParseIP("1.1.1.1"), net.ParseIP("127.0.0.1")}},
  305. ctx: signContext,
  306. expectedSANs: []string{"127.0.0.1"},
  307. err: errors.New("certificate request does not contain the valid IP addresses"),
  308. }
  309. },
  310. "fail/urisValidator": func() test {
  311. u1, err := url.Parse("https://google.com")
  312. assert.FatalError(t, err)
  313. u2, err := url.Parse("urn:uuid:ddfe62ba-7e99-4bc1-83b3-8f57fe3e9959")
  314. assert.FatalError(t, err)
  315. return test{
  316. csr: &x509.CertificateRequest{URIs: []*url.URL{u1, u2}},
  317. ctx: signContext,
  318. expectedSANs: []string{"urn:uuid:ddfe62ba-7e99-4bc1-83b3-8f57fe3e9959"},
  319. err: errors.New("certificate request does not contain the valid URIs"),
  320. }
  321. },
  322. "ok/urisBadValidator-SignIdentity": func() test {
  323. u1, err := url.Parse("https://google.com")
  324. assert.FatalError(t, err)
  325. u2, err := url.Parse("urn:uuid:ddfe62ba-7e99-4bc1-83b3-8f57fe3e9959")
  326. assert.FatalError(t, err)
  327. return test{
  328. csr: &x509.CertificateRequest{URIs: []*url.URL{u1, u2}},
  329. ctx: signIdentityContext,
  330. expectedSANs: []string{"urn:uuid:ddfe62ba-7e99-4bc1-83b3-8f57fe3e9959"},
  331. }
  332. },
  333. "ok": func() test {
  334. u1, err := url.Parse("https://google.com")
  335. assert.FatalError(t, err)
  336. u2, err := url.Parse("urn:uuid:ddfe62ba-7e99-4bc1-83b3-8f57fe3e9959")
  337. assert.FatalError(t, err)
  338. return test{
  339. ctx: signContext,
  340. csr: &x509.CertificateRequest{
  341. DNSNames: []string{"foo", "bar"},
  342. EmailAddresses: []string{"max@fx.com", "mariano@fx.com"},
  343. IPAddresses: []net.IP{net.ParseIP("1.1.1.1"), net.ParseIP("127.0.0.1")},
  344. URIs: []*url.URL{u1, u2},
  345. },
  346. expectedSANs: []string{"foo", "127.0.0.1", "max@fx.com", "mariano@fx.com", "https://google.com", "1.1.1.1", "bar", "urn:uuid:ddfe62ba-7e99-4bc1-83b3-8f57fe3e9959"},
  347. }
  348. },
  349. }
  350. for name, run := range tests {
  351. t.Run(name, func(t *testing.T) {
  352. tt := run()
  353. if err := newDefaultSANsValidator(tt.ctx, tt.expectedSANs).Valid(tt.csr); err != nil {
  354. if assert.NotNil(t, tt.err, fmt.Sprintf("expected no error, but got err = %s", err.Error())) {
  355. assert.True(t, strings.Contains(err.Error(), tt.err.Error()),
  356. fmt.Sprintf("want err = %s, but got err = %s", tt.err.Error(), err.Error()))
  357. }
  358. } else {
  359. assert.Nil(t, tt.err, fmt.Sprintf("expected err = %s, but not <nil>", tt.err))
  360. }
  361. })
  362. }
  363. }
  364. func Test_validityValidator_Valid(t *testing.T) {
  365. type test struct {
  366. cert *x509.Certificate
  367. opts SignOptions
  368. vv *validityValidator
  369. err error
  370. }
  371. tests := map[string]func() test{
  372. "fail/notAfter-past": func() test {
  373. return test{
  374. vv: &validityValidator{5 * time.Minute, 24 * time.Hour},
  375. cert: &x509.Certificate{NotAfter: time.Now().Add(-5 * time.Minute)},
  376. opts: SignOptions{},
  377. err: errors.New("notAfter cannot be in the past"),
  378. }
  379. },
  380. "fail/notBefore-after-notAfter": func() test {
  381. return test{
  382. vv: &validityValidator{5 * time.Minute, 24 * time.Hour},
  383. cert: &x509.Certificate{NotBefore: time.Now().Add(10 * time.Minute),
  384. NotAfter: time.Now().Add(5 * time.Minute)},
  385. opts: SignOptions{},
  386. err: errors.New("notAfter cannot be before notBefore"),
  387. }
  388. },
  389. "fail/duration-too-short": func() test {
  390. n := now()
  391. return test{
  392. vv: &validityValidator{5 * time.Minute, 24 * time.Hour},
  393. cert: &x509.Certificate{NotBefore: n,
  394. NotAfter: n.Add(3 * time.Minute)},
  395. opts: SignOptions{},
  396. err: errors.New("is less than the authorized minimum certificate duration of "),
  397. }
  398. },
  399. "ok/duration-exactly-min": func() test {
  400. n := now()
  401. return test{
  402. vv: &validityValidator{5 * time.Minute, 24 * time.Hour},
  403. cert: &x509.Certificate{NotBefore: n,
  404. NotAfter: n.Add(5 * time.Minute)},
  405. opts: SignOptions{},
  406. }
  407. },
  408. "fail/duration-too-great": func() test {
  409. n := now()
  410. return test{
  411. vv: &validityValidator{5 * time.Minute, 24 * time.Hour},
  412. cert: &x509.Certificate{NotBefore: n,
  413. NotAfter: n.Add(24*time.Hour + time.Second)},
  414. err: errors.New("is more than the authorized maximum certificate duration of "),
  415. }
  416. },
  417. "ok/duration-exactly-max": func() test {
  418. n := time.Now()
  419. return test{
  420. vv: &validityValidator{5 * time.Minute, 24 * time.Hour},
  421. cert: &x509.Certificate{NotBefore: n,
  422. NotAfter: n.Add(24 * time.Hour)},
  423. }
  424. },
  425. "ok/duration-exact-min-with-backdate": func() test {
  426. now := time.Now()
  427. cert := &x509.Certificate{NotBefore: now, NotAfter: now.Add(5 * time.Minute)}
  428. time.Sleep(time.Second)
  429. return test{
  430. vv: &validityValidator{5 * time.Minute, 24 * time.Hour},
  431. cert: cert,
  432. opts: SignOptions{Backdate: time.Second},
  433. }
  434. },
  435. "ok/duration-exact-max-with-backdate": func() test {
  436. backdate := time.Second
  437. now := time.Now()
  438. cert := &x509.Certificate{NotBefore: now, NotAfter: now.Add(24*time.Hour + backdate)}
  439. time.Sleep(backdate)
  440. return test{
  441. vv: &validityValidator{5 * time.Minute, 24 * time.Hour},
  442. cert: cert,
  443. opts: SignOptions{Backdate: backdate},
  444. }
  445. },
  446. }
  447. for name, run := range tests {
  448. t.Run(name, func(t *testing.T) {
  449. tt := run()
  450. if err := tt.vv.Valid(tt.cert, tt.opts); err != nil {
  451. if assert.NotNil(t, tt.err, fmt.Sprintf("expected no error, but got err = %s", err.Error())) {
  452. assert.True(t, strings.Contains(err.Error(), tt.err.Error()),
  453. fmt.Sprintf("want err = %s, but got err = %s", tt.err.Error(), err.Error()))
  454. }
  455. } else {
  456. assert.Nil(t, tt.err, fmt.Sprintf("expected err = %s, but not <nil>", tt.err))
  457. }
  458. })
  459. }
  460. }
  461. func Test_forceCN_Option(t *testing.T) {
  462. type test struct {
  463. so SignOptions
  464. fcn forceCNOption
  465. cert *x509.Certificate
  466. valid func(*x509.Certificate)
  467. err error
  468. }
  469. tests := map[string]func() test{
  470. "ok/CN-not-forced": func() test {
  471. return test{
  472. fcn: forceCNOption{false},
  473. so: SignOptions{},
  474. cert: &x509.Certificate{
  475. Subject: pkix.Name{},
  476. DNSNames: []string{"acme.example.com", "step.example.com"},
  477. },
  478. valid: func(cert *x509.Certificate) {
  479. assert.Equals(t, cert.Subject.CommonName, "")
  480. },
  481. }
  482. },
  483. "ok/CN-forced-and-set": func() test {
  484. return test{
  485. fcn: forceCNOption{true},
  486. so: SignOptions{},
  487. cert: &x509.Certificate{
  488. Subject: pkix.Name{
  489. CommonName: "Some Common Name",
  490. },
  491. DNSNames: []string{"acme.example.com", "step.example.com"},
  492. },
  493. valid: func(cert *x509.Certificate) {
  494. assert.Equals(t, cert.Subject.CommonName, "Some Common Name")
  495. },
  496. }
  497. },
  498. "ok/CN-forced-and-not-set": func() test {
  499. return test{
  500. fcn: forceCNOption{true},
  501. so: SignOptions{},
  502. cert: &x509.Certificate{
  503. Subject: pkix.Name{},
  504. DNSNames: []string{"acme.example.com", "step.example.com"},
  505. },
  506. valid: func(cert *x509.Certificate) {
  507. assert.Equals(t, cert.Subject.CommonName, "acme.example.com")
  508. },
  509. }
  510. },
  511. "fail/CN-forced-and-empty-DNSNames": func() test {
  512. return test{
  513. fcn: forceCNOption{true},
  514. so: SignOptions{},
  515. cert: &x509.Certificate{
  516. Subject: pkix.Name{},
  517. DNSNames: []string{},
  518. },
  519. err: errors.New("cannot force common name, DNS names is empty"),
  520. }
  521. },
  522. }
  523. for name, run := range tests {
  524. t.Run(name, func(t *testing.T) {
  525. tt := run()
  526. if err := tt.fcn.Modify(tt.cert, tt.so); err != nil {
  527. if assert.NotNil(t, tt.err) {
  528. assert.HasPrefix(t, err.Error(), tt.err.Error())
  529. }
  530. } else {
  531. if assert.Nil(t, tt.err) {
  532. tt.valid(tt.cert)
  533. }
  534. }
  535. })
  536. }
  537. }
  538. func Test_profileDefaultDuration_Option(t *testing.T) {
  539. type test struct {
  540. so SignOptions
  541. pdd profileDefaultDuration
  542. cert *x509.Certificate
  543. valid func(*x509.Certificate)
  544. }
  545. tests := map[string]func() test{
  546. "ok/notBefore-notAfter-duration-empty": func() test {
  547. return test{
  548. pdd: profileDefaultDuration(0),
  549. so: SignOptions{},
  550. cert: new(x509.Certificate),
  551. valid: func(cert *x509.Certificate) {
  552. n := now()
  553. assert.True(t, n.After(cert.NotBefore.Add(-time.Second)))
  554. assert.True(t, n.Add(-1*time.Minute).Before(cert.NotBefore))
  555. assert.True(t, n.Add(24*time.Hour).After(cert.NotAfter.Add(-time.Second)))
  556. assert.True(t, n.Add(24*time.Hour).Add(-1*time.Minute).Before(cert.NotAfter))
  557. },
  558. }
  559. },
  560. "ok/notBefore-set": func() test {
  561. nb := time.Now().Add(5 * time.Minute).UTC()
  562. return test{
  563. pdd: profileDefaultDuration(0),
  564. so: SignOptions{NotBefore: NewTimeDuration(nb)},
  565. cert: new(x509.Certificate),
  566. valid: func(cert *x509.Certificate) {
  567. assert.Equals(t, cert.NotBefore, nb)
  568. assert.Equals(t, cert.NotAfter, nb.Add(24*time.Hour))
  569. },
  570. }
  571. },
  572. "ok/duration-set": func() test {
  573. d := 4 * time.Hour
  574. return test{
  575. pdd: profileDefaultDuration(d),
  576. so: SignOptions{Backdate: time.Second},
  577. cert: new(x509.Certificate),
  578. valid: func(cert *x509.Certificate) {
  579. n := now()
  580. assert.True(t, n.After(cert.NotBefore), fmt.Sprintf("expected now = %s to be after cert.NotBefore = %s", n, cert.NotBefore))
  581. assert.True(t, n.Add(-1*time.Minute).Before(cert.NotBefore))
  582. assert.True(t, n.Add(d).After(cert.NotAfter))
  583. assert.True(t, n.Add(d).Add(-1*time.Minute).Before(cert.NotAfter))
  584. },
  585. }
  586. },
  587. "ok/notAfter-set": func() test {
  588. na := now().Add(10 * time.Minute).UTC()
  589. return test{
  590. pdd: profileDefaultDuration(0),
  591. so: SignOptions{NotAfter: NewTimeDuration(na)},
  592. cert: new(x509.Certificate),
  593. valid: func(cert *x509.Certificate) {
  594. n := now()
  595. assert.True(t, n.Add(3*time.Second).After(cert.NotBefore), fmt.Sprintf("expected now = %s to be after cert.NotBefore = %s", n.Add(3*time.Second), cert.NotBefore))
  596. assert.True(t, n.Add(-1*time.Minute).Before(cert.NotBefore))
  597. assert.Equals(t, cert.NotAfter, na)
  598. },
  599. }
  600. },
  601. "ok/notBefore-and-notAfter-set": func() test {
  602. nb := time.Now().Add(5 * time.Minute).UTC()
  603. na := time.Now().Add(10 * time.Minute).UTC()
  604. d := 4 * time.Hour
  605. return test{
  606. pdd: profileDefaultDuration(d),
  607. so: SignOptions{NotBefore: NewTimeDuration(nb), NotAfter: NewTimeDuration(na)},
  608. cert: &x509.Certificate{
  609. NotBefore: time.Now(),
  610. NotAfter: time.Now().Add(time.Hour),
  611. },
  612. valid: func(cert *x509.Certificate) {
  613. assert.Equals(t, nb, cert.NotBefore)
  614. assert.Equals(t, na, cert.NotAfter)
  615. },
  616. }
  617. },
  618. "ok/cert-with-validity": func() test {
  619. nb := time.Now().Add(5 * time.Minute).UTC()
  620. na := time.Now().Add(10 * time.Minute).UTC()
  621. d := 4 * time.Hour
  622. return test{
  623. pdd: profileDefaultDuration(d),
  624. so: SignOptions{},
  625. cert: &x509.Certificate{
  626. NotBefore: nb,
  627. NotAfter: na,
  628. },
  629. valid: func(cert *x509.Certificate) {
  630. assert.Equals(t, nb, cert.NotBefore)
  631. assert.Equals(t, na, cert.NotAfter)
  632. },
  633. }
  634. },
  635. "ok/cert-notBefore-option-notafter": func() test {
  636. nb := time.Now().Add(5 * time.Minute).UTC()
  637. na := time.Now().Add(10 * time.Minute).UTC()
  638. d := 4 * time.Hour
  639. return test{
  640. pdd: profileDefaultDuration(d),
  641. so: SignOptions{NotAfter: NewTimeDuration(na)},
  642. cert: &x509.Certificate{
  643. NotBefore: nb,
  644. },
  645. valid: func(cert *x509.Certificate) {
  646. assert.Equals(t, nb, cert.NotBefore)
  647. assert.Equals(t, na, cert.NotAfter)
  648. },
  649. }
  650. },
  651. "ok/cert-notAfter-option-notBefore": func() test {
  652. nb := time.Now().Add(5 * time.Minute).UTC()
  653. na := time.Now().Add(10 * time.Minute).UTC()
  654. d := 4 * time.Hour
  655. return test{
  656. pdd: profileDefaultDuration(d),
  657. so: SignOptions{NotBefore: NewTimeDuration(nb)},
  658. cert: &x509.Certificate{
  659. NotAfter: na,
  660. },
  661. valid: func(cert *x509.Certificate) {
  662. assert.Equals(t, cert.NotBefore, nb)
  663. assert.Equals(t, cert.NotAfter, na)
  664. },
  665. }
  666. },
  667. }
  668. for name, run := range tests {
  669. t.Run(name, func(t *testing.T) {
  670. tt := run()
  671. assert.FatalError(t, tt.pdd.Modify(tt.cert, tt.so), "unexpected error")
  672. time.Sleep(100 * time.Millisecond)
  673. tt.valid(tt.cert)
  674. })
  675. }
  676. }
  677. func Test_newProvisionerExtension_Option(t *testing.T) {
  678. expectedValue, err := asn1.Marshal(extensionASN1{
  679. Type: int(TypeJWK),
  680. Name: []byte("name"),
  681. CredentialID: []byte("credentialId"),
  682. KeyValuePairs: []string{"key", "value"},
  683. })
  684. if err != nil {
  685. t.Fatal(err)
  686. }
  687. // Claims with smallstep extensions disabled.
  688. claimer, err := NewClaimer(&Claims{
  689. DisableSmallstepExtensions: &trueValue,
  690. }, globalProvisionerClaims)
  691. if err != nil {
  692. t.Fatal(err)
  693. }
  694. type test struct {
  695. modifier *provisionerExtensionOption
  696. cert *x509.Certificate
  697. valid func(*x509.Certificate)
  698. }
  699. tests := map[string]func() test{
  700. "ok/one-element": func() test {
  701. return test{
  702. modifier: newProvisionerExtensionOption(TypeJWK, "name", "credentialId", "key", "value"),
  703. cert: new(x509.Certificate),
  704. valid: func(cert *x509.Certificate) {
  705. if assert.Len(t, 1, cert.ExtraExtensions) {
  706. ext := cert.ExtraExtensions[0]
  707. assert.Equals(t, StepOIDProvisioner, ext.Id)
  708. assert.Equals(t, expectedValue, ext.Value)
  709. assert.False(t, ext.Critical)
  710. }
  711. },
  712. }
  713. },
  714. "ok/replace": func() test {
  715. return test{
  716. modifier: newProvisionerExtensionOption(TypeJWK, "name", "credentialId", "key", "value"),
  717. cert: &x509.Certificate{ExtraExtensions: []pkix.Extension{{Id: StepOIDProvisioner, Critical: true}, {Id: []int{1, 2, 3}}}},
  718. valid: func(cert *x509.Certificate) {
  719. if assert.Len(t, 2, cert.ExtraExtensions) {
  720. ext := cert.ExtraExtensions[0]
  721. assert.Equals(t, StepOIDProvisioner, ext.Id)
  722. assert.Equals(t, expectedValue, ext.Value)
  723. assert.False(t, ext.Critical)
  724. }
  725. },
  726. }
  727. },
  728. "ok/disabled": func() test {
  729. return test{
  730. modifier: newProvisionerExtensionOption(TypeJWK, "name", "credentialId", "key", "value").WithControllerOptions(&Controller{
  731. Claimer: claimer,
  732. }),
  733. cert: new(x509.Certificate),
  734. valid: func(cert *x509.Certificate) {
  735. assert.Len(t, 0, cert.ExtraExtensions)
  736. },
  737. }
  738. },
  739. }
  740. for name, run := range tests {
  741. t.Run(name, func(t *testing.T) {
  742. tt := run()
  743. assert.FatalError(t, tt.modifier.Modify(tt.cert, SignOptions{}))
  744. tt.valid(tt.cert)
  745. })
  746. }
  747. }
  748. func Test_profileLimitDuration_Option(t *testing.T) {
  749. n, fn := mockNow()
  750. defer fn()
  751. type test struct {
  752. pld profileLimitDuration
  753. so SignOptions
  754. cert *x509.Certificate
  755. valid func(*x509.Certificate)
  756. err error
  757. }
  758. tests := map[string]func() test{
  759. "fail/notBefore-before-active-window": func() test {
  760. d, err := ParseTimeDuration("6h")
  761. assert.FatalError(t, err)
  762. return test{
  763. pld: profileLimitDuration{def: 4 * time.Hour, notBefore: n.Add(8 * time.Hour)},
  764. so: SignOptions{NotBefore: d},
  765. cert: new(x509.Certificate),
  766. err: errors.New("requested certificate notBefore ("),
  767. }
  768. },
  769. "fail/requested-notAfter-after-limit": func() test {
  770. d, err := ParseTimeDuration("4h")
  771. assert.FatalError(t, err)
  772. return test{
  773. pld: profileLimitDuration{def: 4 * time.Hour, notAfter: n.Add(6 * time.Hour)},
  774. so: SignOptions{NotBefore: NewTimeDuration(n.Add(3 * time.Hour)), NotAfter: d},
  775. cert: new(x509.Certificate),
  776. err: errors.New("requested certificate notAfter ("),
  777. }
  778. },
  779. "fail/cert-validity-notBefore": func() test {
  780. return test{
  781. pld: profileLimitDuration{def: 4 * time.Hour, notBefore: n, notAfter: n.Add(6 * time.Hour)},
  782. so: SignOptions{},
  783. cert: &x509.Certificate{
  784. NotBefore: n.Add(-time.Second),
  785. NotAfter: n.Add(5 * time.Hour),
  786. },
  787. err: errors.New("requested certificate notBefore ("),
  788. }
  789. },
  790. "fail/cert-validity-notAfter": func() test {
  791. return test{
  792. pld: profileLimitDuration{def: 4 * time.Hour, notBefore: n, notAfter: n.Add(6 * time.Hour)},
  793. so: SignOptions{},
  794. cert: &x509.Certificate{
  795. NotBefore: n,
  796. NotAfter: n.Add(6*time.Hour + time.Second),
  797. },
  798. err: errors.New("requested certificate notAfter ("),
  799. }
  800. },
  801. "ok/valid-notAfter-requested": func() test {
  802. d, err := ParseTimeDuration("2h")
  803. assert.FatalError(t, err)
  804. return test{
  805. pld: profileLimitDuration{def: 4 * time.Hour, notAfter: n.Add(6 * time.Hour)},
  806. so: SignOptions{NotBefore: NewTimeDuration(n.Add(3 * time.Hour)), NotAfter: d, Backdate: 1 * time.Minute},
  807. cert: new(x509.Certificate),
  808. valid: func(cert *x509.Certificate) {
  809. assert.Equals(t, cert.NotBefore, n.Add(3*time.Hour))
  810. assert.Equals(t, cert.NotAfter, n.Add(5*time.Hour))
  811. },
  812. }
  813. },
  814. "ok/valid-notAfter-nil-limit-over-default": func() test {
  815. return test{
  816. pld: profileLimitDuration{def: 1 * time.Hour, notAfter: n.Add(6 * time.Hour)},
  817. so: SignOptions{NotBefore: NewTimeDuration(n.Add(3 * time.Hour)), Backdate: 1 * time.Minute},
  818. cert: new(x509.Certificate),
  819. valid: func(cert *x509.Certificate) {
  820. assert.Equals(t, cert.NotBefore, n.Add(3*time.Hour))
  821. assert.Equals(t, cert.NotAfter, n.Add(4*time.Hour))
  822. },
  823. }
  824. },
  825. "ok/valid-notAfter-nil-limit-under-default": func() test {
  826. return test{
  827. pld: profileLimitDuration{def: 4 * time.Hour, notAfter: n.Add(6 * time.Hour)},
  828. so: SignOptions{NotBefore: NewTimeDuration(n.Add(3 * time.Hour)), Backdate: 1 * time.Minute},
  829. cert: new(x509.Certificate),
  830. valid: func(cert *x509.Certificate) {
  831. assert.Equals(t, cert.NotBefore, n.Add(3*time.Hour))
  832. assert.Equals(t, cert.NotAfter, n.Add(6*time.Hour))
  833. },
  834. }
  835. },
  836. "ok/over-limit-with-backdate": func() test {
  837. return test{
  838. pld: profileLimitDuration{def: 24 * time.Hour, notAfter: n.Add(6 * time.Hour)},
  839. so: SignOptions{Backdate: 1 * time.Minute},
  840. cert: new(x509.Certificate),
  841. valid: func(cert *x509.Certificate) {
  842. assert.Equals(t, cert.NotBefore, n.Add(-time.Minute))
  843. assert.Equals(t, cert.NotAfter, n.Add(6*time.Hour))
  844. },
  845. }
  846. },
  847. "ok/under-limit-with-backdate": func() test {
  848. return test{
  849. pld: profileLimitDuration{def: 24 * time.Hour, notAfter: n.Add(30 * time.Hour)},
  850. so: SignOptions{Backdate: 1 * time.Minute},
  851. cert: new(x509.Certificate),
  852. valid: func(cert *x509.Certificate) {
  853. assert.Equals(t, cert.NotBefore, n.Add(-time.Minute))
  854. assert.Equals(t, cert.NotAfter, n.Add(24*time.Hour))
  855. },
  856. }
  857. },
  858. "ok/cert-validity": func() test {
  859. return test{
  860. pld: profileLimitDuration{def: 4 * time.Hour, notBefore: n, notAfter: n.Add(6 * time.Hour)},
  861. so: SignOptions{},
  862. cert: &x509.Certificate{
  863. NotBefore: n,
  864. NotAfter: n.Add(5 * time.Hour),
  865. },
  866. valid: func(cert *x509.Certificate) {
  867. assert.Equals(t, n, cert.NotBefore)
  868. assert.Equals(t, n.Add(5*time.Hour), cert.NotAfter)
  869. },
  870. }
  871. },
  872. "ok/cert-notBefore-default": func() test {
  873. return test{
  874. pld: profileLimitDuration{def: 4 * time.Hour, notBefore: n, notAfter: n.Add(6 * time.Hour)},
  875. so: SignOptions{},
  876. cert: &x509.Certificate{
  877. NotBefore: n,
  878. },
  879. valid: func(cert *x509.Certificate) {
  880. assert.Equals(t, n, cert.NotBefore)
  881. assert.Equals(t, n.Add(4*time.Hour), cert.NotAfter)
  882. },
  883. }
  884. },
  885. "ok/cert-notAfter-default": func() test {
  886. return test{
  887. pld: profileLimitDuration{def: 4 * time.Hour, notBefore: n, notAfter: n.Add(6 * time.Hour)},
  888. so: SignOptions{},
  889. cert: &x509.Certificate{
  890. NotAfter: n.Add(5 * time.Hour),
  891. },
  892. valid: func(cert *x509.Certificate) {
  893. assert.Equals(t, n, cert.NotBefore)
  894. assert.Equals(t, n.Add(5*time.Hour), cert.NotAfter)
  895. },
  896. }
  897. },
  898. "ok/cert-notBefore-option": func() test {
  899. return test{
  900. pld: profileLimitDuration{def: 4 * time.Hour, notBefore: n, notAfter: n.Add(6 * time.Hour)},
  901. so: SignOptions{NotAfter: NewTimeDuration(n.Add(5 * time.Hour))},
  902. cert: &x509.Certificate{
  903. NotBefore: n,
  904. },
  905. valid: func(cert *x509.Certificate) {
  906. assert.Equals(t, n, cert.NotBefore)
  907. assert.Equals(t, n.Add(5*time.Hour), cert.NotAfter)
  908. },
  909. }
  910. },
  911. "ok/cert-notAfter-option": func() test {
  912. return test{
  913. pld: profileLimitDuration{def: 4 * time.Hour, notBefore: n, notAfter: n.Add(6 * time.Hour)},
  914. so: SignOptions{NotBefore: NewTimeDuration(n.Add(4 * time.Hour))},
  915. cert: &x509.Certificate{
  916. NotAfter: n.Add(5 * time.Hour),
  917. },
  918. valid: func(cert *x509.Certificate) {
  919. assert.Equals(t, n.Add(4*time.Hour), cert.NotBefore)
  920. assert.Equals(t, n.Add(5*time.Hour), cert.NotAfter)
  921. },
  922. }
  923. },
  924. }
  925. for name, run := range tests {
  926. t.Run(name, func(t *testing.T) {
  927. tt := run()
  928. if err := tt.pld.Modify(tt.cert, tt.so); err != nil {
  929. if assert.NotNil(t, tt.err) {
  930. assert.HasPrefix(t, err.Error(), tt.err.Error())
  931. }
  932. } else {
  933. if assert.Nil(t, tt.err) {
  934. tt.valid(tt.cert)
  935. }
  936. }
  937. })
  938. }
  939. }