sign_options_test.go 26 KB

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