sign_options_test.go 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800
  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_ipAddressesValidator_Valid(t *testing.T) {
  178. ip1 := net.IPv4(10, 3, 2, 1)
  179. ip2 := net.IPv4(10, 3, 2, 2)
  180. ip3 := net.IPv4(10, 3, 2, 3)
  181. type args struct {
  182. req *x509.CertificateRequest
  183. }
  184. tests := []struct {
  185. name string
  186. v ipAddressesValidator
  187. args args
  188. wantErr bool
  189. }{
  190. {"ok0", []net.IP{}, args{&x509.CertificateRequest{IPAddresses: []net.IP{}}}, false},
  191. {"ok1", []net.IP{ip1}, args{&x509.CertificateRequest{IPAddresses: []net.IP{ip1}}}, false},
  192. {"ok2", []net.IP{ip1, ip2}, args{&x509.CertificateRequest{IPAddresses: []net.IP{ip1, ip2}}}, false},
  193. {"ok3", []net.IP{ip1, ip2}, args{&x509.CertificateRequest{IPAddresses: []net.IP{ip2, ip1}}}, false},
  194. {"ok4", []net.IP{ip1, ip2}, args{&x509.CertificateRequest{}}, false},
  195. {"fail1", []net.IP{ip1}, args{&x509.CertificateRequest{IPAddresses: []net.IP{ip2}}}, true},
  196. {"fail2", []net.IP{ip1}, args{&x509.CertificateRequest{IPAddresses: []net.IP{ip2, ip1}}}, true},
  197. {"fail3", []net.IP{ip1, ip2}, args{&x509.CertificateRequest{IPAddresses: []net.IP{ip1, ip3}}}, true},
  198. }
  199. for _, tt := range tests {
  200. t.Run(tt.name, func(t *testing.T) {
  201. if err := tt.v.Valid(tt.args.req); (err != nil) != tt.wantErr {
  202. t.Errorf("ipAddressesValidator.Valid() error = %v, wantErr %v", err, tt.wantErr)
  203. }
  204. })
  205. }
  206. }
  207. func Test_urisValidator_Valid(t *testing.T) {
  208. u1, err := url.Parse("https://ca.smallstep.com")
  209. assert.FatalError(t, err)
  210. u2, err := url.Parse("https://google.com/index.html")
  211. assert.FatalError(t, err)
  212. u3, err := url.Parse("urn:uuid:ddfe62ba-7e99-4bc1-83b3-8f57fe3e9959")
  213. assert.FatalError(t, err)
  214. fu, err := url.Parse("https://unexpected.com")
  215. assert.FatalError(t, err)
  216. signContext := NewContextWithMethod(context.Background(), SignMethod)
  217. signIdentityContext := NewContextWithMethod(context.Background(), SignIdentityMethod)
  218. type args struct {
  219. req *x509.CertificateRequest
  220. }
  221. tests := []struct {
  222. name string
  223. v *urisValidator
  224. args args
  225. wantErr bool
  226. }{
  227. {"ok0", newURIsValidator(signContext, []*url.URL{}), args{&x509.CertificateRequest{URIs: []*url.URL{}}}, false},
  228. {"ok1", newURIsValidator(signContext, []*url.URL{u1}), args{&x509.CertificateRequest{URIs: []*url.URL{u1}}}, false},
  229. {"ok2", newURIsValidator(signContext, []*url.URL{u1, u2}), args{&x509.CertificateRequest{URIs: []*url.URL{u2, u1}}}, false},
  230. {"ok3", newURIsValidator(signContext, []*url.URL{u2, u1, u3}), args{&x509.CertificateRequest{URIs: []*url.URL{u3, u2, u1}}}, false},
  231. {"ok4", newURIsValidator(signIdentityContext, []*url.URL{u1, u2}), args{&x509.CertificateRequest{URIs: []*url.URL{u1, fu}}}, false},
  232. {"fail1", newURIsValidator(signContext, []*url.URL{u1}), args{&x509.CertificateRequest{URIs: []*url.URL{u2}}}, true},
  233. {"fail2", newURIsValidator(signContext, []*url.URL{u1}), args{&x509.CertificateRequest{URIs: []*url.URL{u2, u1}}}, true},
  234. {"fail3", newURIsValidator(signContext, []*url.URL{u1, u2}), args{&x509.CertificateRequest{URIs: []*url.URL{u1, fu}}}, true},
  235. }
  236. for _, tt := range tests {
  237. t.Run(tt.name, func(t *testing.T) {
  238. if err := tt.v.Valid(tt.args.req); (err != nil) != tt.wantErr {
  239. t.Errorf("urisValidator.Valid() error = %v, wantErr %v", err, tt.wantErr)
  240. }
  241. })
  242. }
  243. }
  244. func Test_defaultSANsValidator_Valid(t *testing.T) {
  245. type test struct {
  246. csr *x509.CertificateRequest
  247. ctx context.Context
  248. expectedSANs []string
  249. err error
  250. }
  251. signContext := NewContextWithMethod(context.Background(), SignMethod)
  252. signIdentityContext := NewContextWithMethod(context.Background(), SignIdentityMethod)
  253. tests := map[string]func() test{
  254. "fail/dnsNamesValidator": func() test {
  255. return test{
  256. csr: &x509.CertificateRequest{DNSNames: []string{"foo", "bar"}},
  257. ctx: signContext,
  258. expectedSANs: []string{"foo"},
  259. err: errors.New("certificate request does not contain the valid DNS names"),
  260. }
  261. },
  262. "fail/emailAddressesValidator": func() test {
  263. return test{
  264. csr: &x509.CertificateRequest{EmailAddresses: []string{"max@fx.com", "mariano@fx.com"}},
  265. ctx: signContext,
  266. expectedSANs: []string{"dcow@fx.com"},
  267. err: errors.New("certificate request does not contain the valid email addresses"),
  268. }
  269. },
  270. "fail/ipAddressesValidator": func() test {
  271. return test{
  272. csr: &x509.CertificateRequest{IPAddresses: []net.IP{net.ParseIP("1.1.1.1"), net.ParseIP("127.0.0.1")}},
  273. ctx: signContext,
  274. expectedSANs: []string{"127.0.0.1"},
  275. err: errors.New("certificate request does not contain the valid IP addresses"),
  276. }
  277. },
  278. "fail/urisValidator": func() test {
  279. u1, err := url.Parse("https://google.com")
  280. assert.FatalError(t, err)
  281. u2, err := url.Parse("urn:uuid:ddfe62ba-7e99-4bc1-83b3-8f57fe3e9959")
  282. assert.FatalError(t, err)
  283. return test{
  284. csr: &x509.CertificateRequest{URIs: []*url.URL{u1, u2}},
  285. ctx: signContext,
  286. expectedSANs: []string{"urn:uuid:ddfe62ba-7e99-4bc1-83b3-8f57fe3e9959"},
  287. err: errors.New("certificate request does not contain the valid URIs"),
  288. }
  289. },
  290. "ok/urisBadValidator-SignIdentity": func() test {
  291. u1, err := url.Parse("https://google.com")
  292. assert.FatalError(t, err)
  293. u2, err := url.Parse("urn:uuid:ddfe62ba-7e99-4bc1-83b3-8f57fe3e9959")
  294. assert.FatalError(t, err)
  295. return test{
  296. csr: &x509.CertificateRequest{URIs: []*url.URL{u1, u2}},
  297. ctx: signIdentityContext,
  298. expectedSANs: []string{"urn:uuid:ddfe62ba-7e99-4bc1-83b3-8f57fe3e9959"},
  299. }
  300. },
  301. "ok": func() test {
  302. u1, err := url.Parse("https://google.com")
  303. assert.FatalError(t, err)
  304. u2, err := url.Parse("urn:uuid:ddfe62ba-7e99-4bc1-83b3-8f57fe3e9959")
  305. assert.FatalError(t, err)
  306. return test{
  307. ctx: signContext,
  308. csr: &x509.CertificateRequest{
  309. DNSNames: []string{"foo", "bar"},
  310. EmailAddresses: []string{"max@fx.com", "mariano@fx.com"},
  311. IPAddresses: []net.IP{net.ParseIP("1.1.1.1"), net.ParseIP("127.0.0.1")},
  312. URIs: []*url.URL{u1, u2},
  313. },
  314. 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"},
  315. }
  316. },
  317. }
  318. for name, run := range tests {
  319. t.Run(name, func(t *testing.T) {
  320. tt := run()
  321. if err := newDefaultSANsValidator(tt.ctx, tt.expectedSANs).Valid(tt.csr); err != nil {
  322. if assert.NotNil(t, tt.err, fmt.Sprintf("expected no error, but got err = %s", err.Error())) {
  323. assert.True(t, strings.Contains(err.Error(), tt.err.Error()),
  324. fmt.Sprintf("want err = %s, but got err = %s", tt.err.Error(), err.Error()))
  325. }
  326. } else {
  327. assert.Nil(t, tt.err, fmt.Sprintf("expected err = %s, but not <nil>", tt.err))
  328. }
  329. })
  330. }
  331. }
  332. func Test_validityValidator_Valid(t *testing.T) {
  333. type test struct {
  334. cert *x509.Certificate
  335. opts SignOptions
  336. vv *validityValidator
  337. err error
  338. }
  339. tests := map[string]func() test{
  340. "fail/notAfter-past": func() test {
  341. return test{
  342. vv: &validityValidator{5 * time.Minute, 24 * time.Hour},
  343. cert: &x509.Certificate{NotAfter: time.Now().Add(-5 * time.Minute)},
  344. opts: SignOptions{},
  345. err: errors.New("notAfter cannot be in the past"),
  346. }
  347. },
  348. "fail/notBefore-after-notAfter": func() test {
  349. return test{
  350. vv: &validityValidator{5 * time.Minute, 24 * time.Hour},
  351. cert: &x509.Certificate{NotBefore: time.Now().Add(10 * time.Minute),
  352. NotAfter: time.Now().Add(5 * time.Minute)},
  353. opts: SignOptions{},
  354. err: errors.New("notAfter cannot be before notBefore"),
  355. }
  356. },
  357. "fail/duration-too-short": func() test {
  358. n := now()
  359. return test{
  360. vv: &validityValidator{5 * time.Minute, 24 * time.Hour},
  361. cert: &x509.Certificate{NotBefore: n,
  362. NotAfter: n.Add(3 * time.Minute)},
  363. opts: SignOptions{},
  364. err: errors.New("is less than the authorized minimum certificate duration of "),
  365. }
  366. },
  367. "ok/duration-exactly-min": func() test {
  368. n := now()
  369. return test{
  370. vv: &validityValidator{5 * time.Minute, 24 * time.Hour},
  371. cert: &x509.Certificate{NotBefore: n,
  372. NotAfter: n.Add(5 * time.Minute)},
  373. opts: SignOptions{},
  374. }
  375. },
  376. "fail/duration-too-great": func() test {
  377. n := now()
  378. return test{
  379. vv: &validityValidator{5 * time.Minute, 24 * time.Hour},
  380. cert: &x509.Certificate{NotBefore: n,
  381. NotAfter: n.Add(24*time.Hour + time.Second)},
  382. err: errors.New("is more than the authorized maximum certificate duration of "),
  383. }
  384. },
  385. "ok/duration-exactly-max": func() test {
  386. n := time.Now()
  387. return test{
  388. vv: &validityValidator{5 * time.Minute, 24 * time.Hour},
  389. cert: &x509.Certificate{NotBefore: n,
  390. NotAfter: n.Add(24 * time.Hour)},
  391. }
  392. },
  393. "ok/duration-exact-min-with-backdate": func() test {
  394. now := time.Now()
  395. cert := &x509.Certificate{NotBefore: now, NotAfter: now.Add(5 * time.Minute)}
  396. time.Sleep(time.Second)
  397. return test{
  398. vv: &validityValidator{5 * time.Minute, 24 * time.Hour},
  399. cert: cert,
  400. opts: SignOptions{Backdate: time.Second},
  401. }
  402. },
  403. "ok/duration-exact-max-with-backdate": func() test {
  404. backdate := time.Second
  405. now := time.Now()
  406. cert := &x509.Certificate{NotBefore: now, NotAfter: now.Add(24*time.Hour + backdate)}
  407. time.Sleep(backdate)
  408. return test{
  409. vv: &validityValidator{5 * time.Minute, 24 * time.Hour},
  410. cert: cert,
  411. opts: SignOptions{Backdate: backdate},
  412. }
  413. },
  414. }
  415. for name, run := range tests {
  416. t.Run(name, func(t *testing.T) {
  417. tt := run()
  418. if err := tt.vv.Valid(tt.cert, tt.opts); err != nil {
  419. if assert.NotNil(t, tt.err, fmt.Sprintf("expected no error, but got err = %s", err.Error())) {
  420. assert.True(t, strings.Contains(err.Error(), tt.err.Error()),
  421. fmt.Sprintf("want err = %s, but got err = %s", tt.err.Error(), err.Error()))
  422. }
  423. } else {
  424. assert.Nil(t, tt.err, fmt.Sprintf("expected err = %s, but not <nil>", tt.err))
  425. }
  426. })
  427. }
  428. }
  429. func Test_forceCN_Option(t *testing.T) {
  430. type test struct {
  431. so SignOptions
  432. fcn forceCNOption
  433. cert *x509.Certificate
  434. valid func(*x509.Certificate)
  435. err error
  436. }
  437. tests := map[string]func() test{
  438. "ok/CN-not-forced": func() test {
  439. return test{
  440. fcn: forceCNOption{false},
  441. so: SignOptions{},
  442. cert: &x509.Certificate{
  443. Subject: pkix.Name{},
  444. DNSNames: []string{"acme.example.com", "step.example.com"},
  445. },
  446. valid: func(cert *x509.Certificate) {
  447. assert.Equals(t, cert.Subject.CommonName, "")
  448. },
  449. }
  450. },
  451. "ok/CN-forced-and-set": func() test {
  452. return test{
  453. fcn: forceCNOption{true},
  454. so: SignOptions{},
  455. cert: &x509.Certificate{
  456. Subject: pkix.Name{
  457. CommonName: "Some Common Name",
  458. },
  459. DNSNames: []string{"acme.example.com", "step.example.com"},
  460. },
  461. valid: func(cert *x509.Certificate) {
  462. assert.Equals(t, cert.Subject.CommonName, "Some Common Name")
  463. },
  464. }
  465. },
  466. "ok/CN-forced-and-not-set": func() test {
  467. return test{
  468. fcn: forceCNOption{true},
  469. so: SignOptions{},
  470. cert: &x509.Certificate{
  471. Subject: pkix.Name{},
  472. DNSNames: []string{"acme.example.com", "step.example.com"},
  473. },
  474. valid: func(cert *x509.Certificate) {
  475. assert.Equals(t, cert.Subject.CommonName, "acme.example.com")
  476. },
  477. }
  478. },
  479. "fail/CN-forced-and-empty-DNSNames": func() test {
  480. return test{
  481. fcn: forceCNOption{true},
  482. so: SignOptions{},
  483. cert: &x509.Certificate{
  484. Subject: pkix.Name{},
  485. DNSNames: []string{},
  486. },
  487. err: errors.New("cannot force common name, DNS names is empty"),
  488. }
  489. },
  490. }
  491. for name, run := range tests {
  492. t.Run(name, func(t *testing.T) {
  493. tt := run()
  494. if err := tt.fcn.Modify(tt.cert, tt.so); err != nil {
  495. if assert.NotNil(t, tt.err) {
  496. assert.HasPrefix(t, err.Error(), tt.err.Error())
  497. }
  498. } else {
  499. if assert.Nil(t, tt.err) {
  500. tt.valid(tt.cert)
  501. }
  502. }
  503. })
  504. }
  505. }
  506. func Test_profileDefaultDuration_Option(t *testing.T) {
  507. type test struct {
  508. so SignOptions
  509. pdd profileDefaultDuration
  510. cert *x509.Certificate
  511. valid func(*x509.Certificate)
  512. }
  513. tests := map[string]func() test{
  514. "ok/notBefore-notAfter-duration-empty": func() test {
  515. return test{
  516. pdd: profileDefaultDuration(0),
  517. so: SignOptions{},
  518. cert: new(x509.Certificate),
  519. valid: func(cert *x509.Certificate) {
  520. n := now()
  521. assert.True(t, n.After(cert.NotBefore.Add(-time.Second)))
  522. assert.True(t, n.Add(-1*time.Minute).Before(cert.NotBefore))
  523. assert.True(t, n.Add(24*time.Hour).After(cert.NotAfter.Add(-time.Second)))
  524. assert.True(t, n.Add(24*time.Hour).Add(-1*time.Minute).Before(cert.NotAfter))
  525. },
  526. }
  527. },
  528. "ok/notBefore-set": func() test {
  529. nb := time.Now().Add(5 * time.Minute).UTC()
  530. return test{
  531. pdd: profileDefaultDuration(0),
  532. so: SignOptions{NotBefore: NewTimeDuration(nb)},
  533. cert: new(x509.Certificate),
  534. valid: func(cert *x509.Certificate) {
  535. assert.Equals(t, cert.NotBefore, nb)
  536. assert.Equals(t, cert.NotAfter, nb.Add(24*time.Hour))
  537. },
  538. }
  539. },
  540. "ok/duration-set": func() test {
  541. d := 4 * time.Hour
  542. return test{
  543. pdd: profileDefaultDuration(d),
  544. so: SignOptions{Backdate: time.Second},
  545. cert: new(x509.Certificate),
  546. valid: func(cert *x509.Certificate) {
  547. n := now()
  548. assert.True(t, n.After(cert.NotBefore), fmt.Sprintf("expected now = %s to be after cert.NotBefore = %s", n, cert.NotBefore))
  549. assert.True(t, n.Add(-1*time.Minute).Before(cert.NotBefore))
  550. assert.True(t, n.Add(d).After(cert.NotAfter))
  551. assert.True(t, n.Add(d).Add(-1*time.Minute).Before(cert.NotAfter))
  552. },
  553. }
  554. },
  555. "ok/notAfter-set": func() test {
  556. na := now().Add(10 * time.Minute).UTC()
  557. return test{
  558. pdd: profileDefaultDuration(0),
  559. so: SignOptions{NotAfter: NewTimeDuration(na)},
  560. cert: new(x509.Certificate),
  561. valid: func(cert *x509.Certificate) {
  562. n := now()
  563. 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))
  564. assert.True(t, n.Add(-1*time.Minute).Before(cert.NotBefore))
  565. assert.Equals(t, cert.NotAfter, na)
  566. },
  567. }
  568. },
  569. "ok/notBefore-and-notAfter-set": func() test {
  570. nb := time.Now().Add(5 * time.Minute).UTC()
  571. na := time.Now().Add(10 * time.Minute).UTC()
  572. d := 4 * time.Hour
  573. return test{
  574. pdd: profileDefaultDuration(d),
  575. so: SignOptions{NotBefore: NewTimeDuration(nb), NotAfter: NewTimeDuration(na)},
  576. cert: new(x509.Certificate),
  577. valid: func(cert *x509.Certificate) {
  578. assert.Equals(t, cert.NotBefore, nb)
  579. assert.Equals(t, cert.NotAfter, na)
  580. },
  581. }
  582. },
  583. }
  584. for name, run := range tests {
  585. t.Run(name, func(t *testing.T) {
  586. tt := run()
  587. assert.FatalError(t, tt.pdd.Modify(tt.cert, tt.so), "unexpected error")
  588. time.Sleep(100 * time.Millisecond)
  589. tt.valid(tt.cert)
  590. })
  591. }
  592. }
  593. func Test_newProvisionerExtension_Option(t *testing.T) {
  594. expectedValue, err := asn1.Marshal(extensionASN1{
  595. Type: int(TypeJWK),
  596. Name: []byte("name"),
  597. CredentialID: []byte("credentialId"),
  598. KeyValuePairs: []string{"key", "value"},
  599. })
  600. if err != nil {
  601. t.Fatal(err)
  602. }
  603. // Claims with smallstep extensions disabled.
  604. claimer, err := NewClaimer(&Claims{
  605. DisableSmallstepExtensions: &trueValue,
  606. }, globalProvisionerClaims)
  607. if err != nil {
  608. t.Fatal(err)
  609. }
  610. type test struct {
  611. modifier *provisionerExtensionOption
  612. cert *x509.Certificate
  613. valid func(*x509.Certificate)
  614. }
  615. tests := map[string]func() test{
  616. "ok/one-element": func() test {
  617. return test{
  618. modifier: newProvisionerExtensionOption(TypeJWK, "name", "credentialId", "key", "value"),
  619. cert: new(x509.Certificate),
  620. valid: func(cert *x509.Certificate) {
  621. if assert.Len(t, 1, cert.ExtraExtensions) {
  622. ext := cert.ExtraExtensions[0]
  623. assert.Equals(t, StepOIDProvisioner, ext.Id)
  624. assert.Equals(t, expectedValue, ext.Value)
  625. assert.False(t, ext.Critical)
  626. }
  627. },
  628. }
  629. },
  630. "ok/replace": func() test {
  631. return test{
  632. modifier: newProvisionerExtensionOption(TypeJWK, "name", "credentialId", "key", "value"),
  633. cert: &x509.Certificate{ExtraExtensions: []pkix.Extension{{Id: StepOIDProvisioner, Critical: true}, {Id: []int{1, 2, 3}}}},
  634. valid: func(cert *x509.Certificate) {
  635. if assert.Len(t, 2, cert.ExtraExtensions) {
  636. ext := cert.ExtraExtensions[0]
  637. assert.Equals(t, StepOIDProvisioner, ext.Id)
  638. assert.Equals(t, expectedValue, ext.Value)
  639. assert.False(t, ext.Critical)
  640. }
  641. },
  642. }
  643. },
  644. "ok/disabled": func() test {
  645. return test{
  646. modifier: newProvisionerExtensionOption(TypeJWK, "name", "credentialId", "key", "value").WithControllerOptions(&Controller{
  647. Claimer: claimer,
  648. }),
  649. cert: new(x509.Certificate),
  650. valid: func(cert *x509.Certificate) {
  651. assert.Len(t, 0, cert.ExtraExtensions)
  652. },
  653. }
  654. },
  655. }
  656. for name, run := range tests {
  657. t.Run(name, func(t *testing.T) {
  658. tt := run()
  659. assert.FatalError(t, tt.modifier.Modify(tt.cert, SignOptions{}))
  660. tt.valid(tt.cert)
  661. })
  662. }
  663. }
  664. func Test_profileLimitDuration_Option(t *testing.T) {
  665. n, fn := mockNow()
  666. defer fn()
  667. type test struct {
  668. pld profileLimitDuration
  669. so SignOptions
  670. cert *x509.Certificate
  671. valid func(*x509.Certificate)
  672. err error
  673. }
  674. tests := map[string]func() test{
  675. "fail/notBefore-before-active-window": func() test {
  676. d, err := ParseTimeDuration("6h")
  677. assert.FatalError(t, err)
  678. return test{
  679. pld: profileLimitDuration{def: 4 * time.Hour, notBefore: n.Add(8 * time.Hour)},
  680. so: SignOptions{NotBefore: d},
  681. cert: new(x509.Certificate),
  682. err: errors.New("requested certificate notBefore ("),
  683. }
  684. },
  685. "fail/requested-notAfter-after-limit": func() test {
  686. d, err := ParseTimeDuration("4h")
  687. assert.FatalError(t, err)
  688. return test{
  689. pld: profileLimitDuration{def: 4 * time.Hour, notAfter: n.Add(6 * time.Hour)},
  690. so: SignOptions{NotBefore: NewTimeDuration(n.Add(3 * time.Hour)), NotAfter: d},
  691. cert: new(x509.Certificate),
  692. err: errors.New("requested certificate notAfter ("),
  693. }
  694. },
  695. "ok/valid-notAfter-requested": func() test {
  696. d, err := ParseTimeDuration("2h")
  697. assert.FatalError(t, err)
  698. return test{
  699. pld: profileLimitDuration{def: 4 * time.Hour, notAfter: n.Add(6 * time.Hour)},
  700. so: SignOptions{NotBefore: NewTimeDuration(n.Add(3 * time.Hour)), NotAfter: d, Backdate: 1 * time.Minute},
  701. cert: new(x509.Certificate),
  702. valid: func(cert *x509.Certificate) {
  703. assert.Equals(t, cert.NotBefore, n.Add(3*time.Hour))
  704. assert.Equals(t, cert.NotAfter, n.Add(5*time.Hour))
  705. },
  706. }
  707. },
  708. "ok/valid-notAfter-nil-limit-over-default": func() test {
  709. return test{
  710. pld: profileLimitDuration{def: 1 * time.Hour, notAfter: n.Add(6 * time.Hour)},
  711. so: SignOptions{NotBefore: NewTimeDuration(n.Add(3 * time.Hour)), Backdate: 1 * time.Minute},
  712. cert: new(x509.Certificate),
  713. valid: func(cert *x509.Certificate) {
  714. assert.Equals(t, cert.NotBefore, n.Add(3*time.Hour))
  715. assert.Equals(t, cert.NotAfter, n.Add(4*time.Hour))
  716. },
  717. }
  718. },
  719. "ok/valid-notAfter-nil-limit-under-default": func() test {
  720. return test{
  721. pld: profileLimitDuration{def: 4 * time.Hour, notAfter: n.Add(6 * time.Hour)},
  722. so: SignOptions{NotBefore: NewTimeDuration(n.Add(3 * time.Hour)), Backdate: 1 * time.Minute},
  723. cert: new(x509.Certificate),
  724. valid: func(cert *x509.Certificate) {
  725. assert.Equals(t, cert.NotBefore, n.Add(3*time.Hour))
  726. assert.Equals(t, cert.NotAfter, n.Add(6*time.Hour))
  727. },
  728. }
  729. },
  730. "ok/over-limit-with-backdate": func() test {
  731. return test{
  732. pld: profileLimitDuration{def: 24 * time.Hour, notAfter: n.Add(6 * time.Hour)},
  733. so: SignOptions{Backdate: 1 * time.Minute},
  734. cert: new(x509.Certificate),
  735. valid: func(cert *x509.Certificate) {
  736. assert.Equals(t, cert.NotBefore, n.Add(-time.Minute))
  737. assert.Equals(t, cert.NotAfter, n.Add(6*time.Hour))
  738. },
  739. }
  740. },
  741. "ok/under-limit-with-backdate": func() test {
  742. return test{
  743. pld: profileLimitDuration{def: 24 * time.Hour, notAfter: n.Add(30 * time.Hour)},
  744. so: SignOptions{Backdate: 1 * time.Minute},
  745. cert: new(x509.Certificate),
  746. valid: func(cert *x509.Certificate) {
  747. assert.Equals(t, cert.NotBefore, n.Add(-time.Minute))
  748. assert.Equals(t, cert.NotAfter, n.Add(24*time.Hour))
  749. },
  750. }
  751. },
  752. }
  753. for name, run := range tests {
  754. t.Run(name, func(t *testing.T) {
  755. tt := run()
  756. if err := tt.pld.Modify(tt.cert, tt.so); err != nil {
  757. if assert.NotNil(t, tt.err) {
  758. assert.HasPrefix(t, err.Error(), tt.err.Error())
  759. }
  760. } else {
  761. if assert.Nil(t, tt.err) {
  762. tt.valid(tt.cert)
  763. }
  764. }
  765. })
  766. }
  767. }