4
0

order_test.go 78 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643
  1. package acme
  2. import (
  3. "context"
  4. "crypto"
  5. "crypto/x509"
  6. "crypto/x509/pkix"
  7. "encoding/asn1"
  8. "encoding/json"
  9. "fmt"
  10. "net"
  11. "net/url"
  12. "reflect"
  13. "testing"
  14. "time"
  15. "github.com/google/go-cmp/cmp"
  16. "github.com/pkg/errors"
  17. "github.com/smallstep/assert"
  18. "github.com/smallstep/certificates/authority"
  19. "github.com/smallstep/certificates/authority/provisioner"
  20. "github.com/smallstep/certificates/errs"
  21. "github.com/smallstep/certificates/webhook"
  22. "go.step.sm/crypto/keyutil"
  23. "go.step.sm/crypto/x509util"
  24. )
  25. func TestOrder_UpdateStatus(t *testing.T) {
  26. type test struct {
  27. o *Order
  28. err *Error
  29. db DB
  30. }
  31. tests := map[string]func(t *testing.T) test{
  32. "ok/already-invalid": func(t *testing.T) test {
  33. o := &Order{
  34. Status: StatusInvalid,
  35. }
  36. return test{
  37. o: o,
  38. }
  39. },
  40. "ok/already-valid": func(t *testing.T) test {
  41. o := &Order{
  42. Status: StatusInvalid,
  43. }
  44. return test{
  45. o: o,
  46. }
  47. },
  48. "fail/error-unexpected-status": func(t *testing.T) test {
  49. o := &Order{
  50. Status: "foo",
  51. }
  52. return test{
  53. o: o,
  54. err: NewErrorISE("unrecognized order status: %s", o.Status),
  55. }
  56. },
  57. "ok/ready-expired": func(t *testing.T) test {
  58. now := clock.Now()
  59. o := &Order{
  60. ID: "oID",
  61. AccountID: "accID",
  62. Status: StatusReady,
  63. ExpiresAt: now.Add(-5 * time.Minute),
  64. }
  65. return test{
  66. o: o,
  67. db: &MockDB{
  68. MockUpdateOrder: func(ctx context.Context, updo *Order) error {
  69. assert.Equals(t, updo.ID, o.ID)
  70. assert.Equals(t, updo.AccountID, o.AccountID)
  71. assert.Equals(t, updo.Status, StatusInvalid)
  72. assert.Equals(t, updo.ExpiresAt, o.ExpiresAt)
  73. return nil
  74. },
  75. },
  76. }
  77. },
  78. "fail/ready-expired-db.UpdateOrder-error": func(t *testing.T) test {
  79. now := clock.Now()
  80. o := &Order{
  81. ID: "oID",
  82. AccountID: "accID",
  83. Status: StatusReady,
  84. ExpiresAt: now.Add(-5 * time.Minute),
  85. }
  86. return test{
  87. o: o,
  88. db: &MockDB{
  89. MockUpdateOrder: func(ctx context.Context, updo *Order) error {
  90. assert.Equals(t, updo.ID, o.ID)
  91. assert.Equals(t, updo.AccountID, o.AccountID)
  92. assert.Equals(t, updo.Status, StatusInvalid)
  93. assert.Equals(t, updo.ExpiresAt, o.ExpiresAt)
  94. return errors.New("force")
  95. },
  96. },
  97. err: NewErrorISE("error updating order: force"),
  98. }
  99. },
  100. "ok/pending-expired": func(t *testing.T) test {
  101. now := clock.Now()
  102. o := &Order{
  103. ID: "oID",
  104. AccountID: "accID",
  105. Status: StatusPending,
  106. ExpiresAt: now.Add(-5 * time.Minute),
  107. }
  108. return test{
  109. o: o,
  110. db: &MockDB{
  111. MockUpdateOrder: func(ctx context.Context, updo *Order) error {
  112. assert.Equals(t, updo.ID, o.ID)
  113. assert.Equals(t, updo.AccountID, o.AccountID)
  114. assert.Equals(t, updo.Status, StatusInvalid)
  115. assert.Equals(t, updo.ExpiresAt, o.ExpiresAt)
  116. err := NewError(ErrorMalformedType, "order has expired")
  117. assert.HasPrefix(t, updo.Error.Err.Error(), err.Err.Error())
  118. assert.Equals(t, updo.Error.Type, err.Type)
  119. assert.Equals(t, updo.Error.Detail, err.Detail)
  120. assert.Equals(t, updo.Error.Status, err.Status)
  121. assert.Equals(t, updo.Error.Detail, err.Detail)
  122. return nil
  123. },
  124. },
  125. }
  126. },
  127. "ok/invalid": func(t *testing.T) test {
  128. now := clock.Now()
  129. o := &Order{
  130. ID: "oID",
  131. AccountID: "accID",
  132. Status: StatusPending,
  133. ExpiresAt: now.Add(5 * time.Minute),
  134. AuthorizationIDs: []string{"a", "b"},
  135. }
  136. az1 := &Authorization{
  137. ID: "a",
  138. Status: StatusValid,
  139. }
  140. az2 := &Authorization{
  141. ID: "b",
  142. Status: StatusInvalid,
  143. }
  144. return test{
  145. o: o,
  146. db: &MockDB{
  147. MockUpdateOrder: func(ctx context.Context, updo *Order) error {
  148. assert.Equals(t, updo.ID, o.ID)
  149. assert.Equals(t, updo.AccountID, o.AccountID)
  150. assert.Equals(t, updo.Status, StatusInvalid)
  151. assert.Equals(t, updo.ExpiresAt, o.ExpiresAt)
  152. return nil
  153. },
  154. MockGetAuthorization: func(ctx context.Context, id string) (*Authorization, error) {
  155. switch id {
  156. case az1.ID:
  157. return az1, nil
  158. case az2.ID:
  159. return az2, nil
  160. default:
  161. assert.FatalError(t, errors.Errorf("unexpected authz key %s", id))
  162. return nil, errors.New("force")
  163. }
  164. },
  165. },
  166. }
  167. },
  168. "ok/still-pending": func(t *testing.T) test {
  169. now := clock.Now()
  170. o := &Order{
  171. ID: "oID",
  172. AccountID: "accID",
  173. Status: StatusPending,
  174. ExpiresAt: now.Add(5 * time.Minute),
  175. AuthorizationIDs: []string{"a", "b"},
  176. }
  177. az1 := &Authorization{
  178. ID: "a",
  179. Status: StatusValid,
  180. }
  181. az2 := &Authorization{
  182. ID: "b",
  183. Status: StatusPending,
  184. }
  185. return test{
  186. o: o,
  187. db: &MockDB{
  188. MockGetAuthorization: func(ctx context.Context, id string) (*Authorization, error) {
  189. switch id {
  190. case az1.ID:
  191. return az1, nil
  192. case az2.ID:
  193. return az2, nil
  194. default:
  195. assert.FatalError(t, errors.Errorf("unexpected authz key %s", id))
  196. return nil, errors.New("force")
  197. }
  198. },
  199. },
  200. }
  201. },
  202. "ok/valid": func(t *testing.T) test {
  203. now := clock.Now()
  204. o := &Order{
  205. ID: "oID",
  206. AccountID: "accID",
  207. Status: StatusPending,
  208. ExpiresAt: now.Add(5 * time.Minute),
  209. AuthorizationIDs: []string{"a", "b"},
  210. }
  211. az1 := &Authorization{
  212. ID: "a",
  213. Status: StatusValid,
  214. }
  215. az2 := &Authorization{
  216. ID: "b",
  217. Status: StatusValid,
  218. }
  219. return test{
  220. o: o,
  221. db: &MockDB{
  222. MockUpdateOrder: func(ctx context.Context, updo *Order) error {
  223. assert.Equals(t, updo.ID, o.ID)
  224. assert.Equals(t, updo.AccountID, o.AccountID)
  225. assert.Equals(t, updo.Status, StatusReady)
  226. assert.Equals(t, updo.ExpiresAt, o.ExpiresAt)
  227. return nil
  228. },
  229. MockGetAuthorization: func(ctx context.Context, id string) (*Authorization, error) {
  230. switch id {
  231. case az1.ID:
  232. return az1, nil
  233. case az2.ID:
  234. return az2, nil
  235. default:
  236. assert.FatalError(t, errors.Errorf("unexpected authz key %s", id))
  237. return nil, errors.New("force")
  238. }
  239. },
  240. },
  241. }
  242. },
  243. }
  244. for name, run := range tests {
  245. t.Run(name, func(t *testing.T) {
  246. tc := run(t)
  247. if err := tc.o.UpdateStatus(context.Background(), tc.db); err != nil {
  248. if assert.NotNil(t, tc.err) {
  249. var k *Error
  250. if errors.As(err, &k) {
  251. assert.Equals(t, k.Type, tc.err.Type)
  252. assert.Equals(t, k.Detail, tc.err.Detail)
  253. assert.Equals(t, k.Status, tc.err.Status)
  254. assert.Equals(t, k.Err.Error(), tc.err.Err.Error())
  255. assert.Equals(t, k.Detail, tc.err.Detail)
  256. } else {
  257. assert.FatalError(t, errors.New("unexpected error type"))
  258. }
  259. }
  260. } else {
  261. assert.Nil(t, tc.err)
  262. }
  263. })
  264. }
  265. }
  266. type mockSignAuth struct {
  267. signWithContext func(ctx context.Context, csr *x509.CertificateRequest, signOpts provisioner.SignOptions, extraOpts ...provisioner.SignOption) ([]*x509.Certificate, error)
  268. areSANsAllowed func(ctx context.Context, sans []string) error
  269. loadProvisionerByName func(string) (provisioner.Interface, error)
  270. ret1, ret2 interface{}
  271. err error
  272. }
  273. func (m *mockSignAuth) SignWithContext(ctx context.Context, csr *x509.CertificateRequest, signOpts provisioner.SignOptions, extraOpts ...provisioner.SignOption) ([]*x509.Certificate, error) {
  274. if m.signWithContext != nil {
  275. return m.signWithContext(ctx, csr, signOpts, extraOpts...)
  276. } else if m.err != nil {
  277. return nil, m.err
  278. }
  279. return []*x509.Certificate{m.ret1.(*x509.Certificate), m.ret2.(*x509.Certificate)}, m.err
  280. }
  281. func (m *mockSignAuth) AreSANsAllowed(ctx context.Context, sans []string) error {
  282. if m.areSANsAllowed != nil {
  283. return m.areSANsAllowed(ctx, sans)
  284. }
  285. return m.err
  286. }
  287. func (m *mockSignAuth) LoadProvisionerByName(name string) (provisioner.Interface, error) {
  288. if m.loadProvisionerByName != nil {
  289. return m.loadProvisionerByName(name)
  290. }
  291. return m.ret1.(provisioner.Interface), m.err
  292. }
  293. func (m *mockSignAuth) IsRevoked(string) (bool, error) {
  294. return false, nil
  295. }
  296. func (m *mockSignAuth) Revoke(context.Context, *authority.RevokeOptions) error {
  297. return nil
  298. }
  299. func (m *mockSignAuth) GetBackdate() *time.Duration {
  300. return nil
  301. }
  302. func TestOrder_Finalize(t *testing.T) {
  303. mustSigner := func(kty, crv string, size int) crypto.Signer {
  304. s, err := keyutil.GenerateSigner(kty, crv, size)
  305. if err != nil {
  306. t.Fatal(err)
  307. }
  308. return s
  309. }
  310. type test struct {
  311. o *Order
  312. err *Error
  313. db DB
  314. ca CertificateAuthority
  315. csr *x509.CertificateRequest
  316. prov Provisioner
  317. }
  318. tests := map[string]func(t *testing.T) test{
  319. "fail/invalid": func(t *testing.T) test {
  320. o := &Order{
  321. ID: "oid",
  322. Status: StatusInvalid,
  323. }
  324. return test{
  325. o: o,
  326. err: NewError(ErrorOrderNotReadyType, "order %s has been abandoned", o.ID),
  327. }
  328. },
  329. "fail/pending": func(t *testing.T) test {
  330. now := clock.Now()
  331. o := &Order{
  332. ID: "oID",
  333. AccountID: "accID",
  334. Status: StatusPending,
  335. ExpiresAt: now.Add(5 * time.Minute),
  336. AuthorizationIDs: []string{"a", "b"},
  337. }
  338. az1 := &Authorization{
  339. ID: "a",
  340. Status: StatusValid,
  341. }
  342. az2 := &Authorization{
  343. ID: "b",
  344. Status: StatusPending,
  345. ExpiresAt: now.Add(5 * time.Minute),
  346. }
  347. return test{
  348. o: o,
  349. db: &MockDB{
  350. MockGetAuthorization: func(ctx context.Context, id string) (*Authorization, error) {
  351. switch id {
  352. case az1.ID:
  353. return az1, nil
  354. case az2.ID:
  355. return az2, nil
  356. default:
  357. assert.FatalError(t, errors.Errorf("unexpected authz key %s", id))
  358. return nil, errors.New("force")
  359. }
  360. },
  361. },
  362. err: NewError(ErrorOrderNotReadyType, "order %s is not ready", o.ID),
  363. }
  364. },
  365. "ok/already-valid": func(t *testing.T) test {
  366. o := &Order{
  367. ID: "oid",
  368. Status: StatusValid,
  369. }
  370. return test{
  371. o: o,
  372. }
  373. },
  374. "fail/error-unexpected-status": func(t *testing.T) test {
  375. now := clock.Now()
  376. o := &Order{
  377. ID: "oID",
  378. AccountID: "accID",
  379. Status: "foo",
  380. ExpiresAt: now.Add(5 * time.Minute),
  381. AuthorizationIDs: []string{"a", "b"},
  382. }
  383. return test{
  384. o: o,
  385. err: NewErrorISE("unrecognized order status: %s", o.Status),
  386. }
  387. },
  388. "fail/non-matching-permanent-identifier-common-name": func(t *testing.T) test {
  389. now := clock.Now()
  390. o := &Order{
  391. ID: "oID",
  392. AccountID: "accID",
  393. Status: StatusReady,
  394. ExpiresAt: now.Add(5 * time.Minute),
  395. AuthorizationIDs: []string{"a", "b"},
  396. Identifiers: []Identifier{
  397. {Type: "permanent-identifier", Value: "a-permanent-identifier"},
  398. },
  399. }
  400. signer := mustSigner("EC", "P-256", 0)
  401. fingerprint, err := keyutil.Fingerprint(signer.Public())
  402. if err != nil {
  403. t.Fatal(err)
  404. }
  405. csr := &x509.CertificateRequest{
  406. Subject: pkix.Name{
  407. CommonName: "a-different-identifier",
  408. },
  409. PublicKey: signer.Public(),
  410. ExtraExtensions: []pkix.Extension{
  411. {
  412. Id: asn1.ObjectIdentifier{1, 3, 6, 1, 5, 5, 7, 8, 3},
  413. Value: []byte("a-permanent-identifier"),
  414. },
  415. },
  416. }
  417. return test{
  418. o: o,
  419. csr: csr,
  420. db: &MockDB{
  421. MockGetAuthorization: func(ctx context.Context, id string) (*Authorization, error) {
  422. switch id {
  423. case "a":
  424. return &Authorization{
  425. ID: id,
  426. Status: StatusValid,
  427. }, nil
  428. case "b":
  429. return &Authorization{
  430. ID: id,
  431. Fingerprint: fingerprint,
  432. Status: StatusValid,
  433. }, nil
  434. default:
  435. assert.FatalError(t, errors.Errorf("unexpected authorization %s", id))
  436. return nil, errors.New("force")
  437. }
  438. },
  439. MockUpdateOrder: func(ctx context.Context, o *Order) error {
  440. return nil
  441. },
  442. },
  443. err: &Error{
  444. Type: "urn:ietf:params:acme:error:badCSR",
  445. Detail: "The CSR is unacceptable",
  446. Status: 400,
  447. Err: fmt.Errorf("CSR Subject Common Name does not match identifiers exactly: "+
  448. "CSR Subject Common Name = %s, Order Permanent Identifier = %s", csr.Subject.CommonName, "a-permanent-identifier"),
  449. },
  450. }
  451. },
  452. "fail/error-provisioner-auth": func(t *testing.T) test {
  453. now := clock.Now()
  454. o := &Order{
  455. ID: "oID",
  456. AccountID: "accID",
  457. Status: StatusReady,
  458. ExpiresAt: now.Add(5 * time.Minute),
  459. AuthorizationIDs: []string{"a", "b"},
  460. Identifiers: []Identifier{
  461. {Type: "dns", Value: "foo.internal"},
  462. {Type: "dns", Value: "bar.internal"},
  463. },
  464. }
  465. csr := &x509.CertificateRequest{
  466. Subject: pkix.Name{
  467. CommonName: "foo.internal",
  468. },
  469. DNSNames: []string{"bar.internal"},
  470. }
  471. return test{
  472. o: o,
  473. csr: csr,
  474. prov: &MockProvisioner{
  475. MauthorizeSign: func(ctx context.Context, token string) ([]provisioner.SignOption, error) {
  476. assert.Equals(t, token, "")
  477. return nil, errors.New("force")
  478. },
  479. },
  480. db: &MockDB{
  481. MockGetAuthorization: func(ctx context.Context, id string) (*Authorization, error) {
  482. return &Authorization{ID: id, Status: StatusValid}, nil
  483. },
  484. },
  485. err: NewErrorISE("error retrieving authorization options from ACME provisioner: force"),
  486. }
  487. },
  488. "fail/error-template-options": func(t *testing.T) test {
  489. now := clock.Now()
  490. o := &Order{
  491. ID: "oID",
  492. AccountID: "accID",
  493. Status: StatusReady,
  494. ExpiresAt: now.Add(5 * time.Minute),
  495. AuthorizationIDs: []string{"a", "b"},
  496. Identifiers: []Identifier{
  497. {Type: "dns", Value: "foo.internal"},
  498. {Type: "dns", Value: "bar.internal"},
  499. },
  500. }
  501. csr := &x509.CertificateRequest{
  502. Subject: pkix.Name{
  503. CommonName: "foo.internal",
  504. },
  505. DNSNames: []string{"bar.internal"},
  506. }
  507. return test{
  508. o: o,
  509. csr: csr,
  510. prov: &MockProvisioner{
  511. MauthorizeSign: func(ctx context.Context, token string) ([]provisioner.SignOption, error) {
  512. assert.Equals(t, token, "")
  513. return nil, nil
  514. },
  515. MgetOptions: func() *provisioner.Options {
  516. return &provisioner.Options{
  517. X509: &provisioner.X509Options{
  518. TemplateData: json.RawMessage([]byte("fo{o")),
  519. },
  520. }
  521. },
  522. },
  523. db: &MockDB{
  524. MockGetAuthorization: func(ctx context.Context, id string) (*Authorization, error) {
  525. return &Authorization{ID: id, Status: StatusValid}, nil
  526. },
  527. },
  528. err: NewErrorISE("error creating template options from ACME provisioner: error unmarshaling template data: invalid character 'o' in literal false (expecting 'a')"),
  529. }
  530. },
  531. "fail/error-ca-sign": func(t *testing.T) test {
  532. now := clock.Now()
  533. o := &Order{
  534. ID: "oID",
  535. AccountID: "accID",
  536. Status: StatusReady,
  537. ExpiresAt: now.Add(5 * time.Minute),
  538. AuthorizationIDs: []string{"a", "b"},
  539. Identifiers: []Identifier{
  540. {Type: "dns", Value: "foo.internal"},
  541. {Type: "dns", Value: "bar.internal"},
  542. },
  543. }
  544. csr := &x509.CertificateRequest{
  545. Subject: pkix.Name{
  546. CommonName: "foo.internal",
  547. },
  548. DNSNames: []string{"bar.internal"},
  549. }
  550. return test{
  551. o: o,
  552. csr: csr,
  553. prov: &MockProvisioner{
  554. MauthorizeSign: func(ctx context.Context, token string) ([]provisioner.SignOption, error) {
  555. assert.Equals(t, token, "")
  556. return nil, nil
  557. },
  558. MgetOptions: func() *provisioner.Options {
  559. return nil
  560. },
  561. },
  562. ca: &mockSignAuth{
  563. signWithContext: func(_ context.Context, _csr *x509.CertificateRequest, signOpts provisioner.SignOptions, extraOpts ...provisioner.SignOption) ([]*x509.Certificate, error) {
  564. assert.Equals(t, _csr, csr)
  565. return nil, errors.New("force")
  566. },
  567. },
  568. db: &MockDB{
  569. MockGetAuthorization: func(ctx context.Context, id string) (*Authorization, error) {
  570. return &Authorization{ID: id, Status: StatusValid}, nil
  571. },
  572. },
  573. err: NewErrorISE("error signing certificate for order oID: force"),
  574. }
  575. },
  576. "fail/webhook-error": func(t *testing.T) test {
  577. now := clock.Now()
  578. o := &Order{
  579. ID: "oID",
  580. AccountID: "accID",
  581. Status: StatusReady,
  582. ExpiresAt: now.Add(5 * time.Minute),
  583. AuthorizationIDs: []string{"a", "b"},
  584. Identifiers: []Identifier{
  585. {Type: "dns", Value: "foo.internal"},
  586. {Type: "dns", Value: "bar.internal"},
  587. },
  588. }
  589. csr := &x509.CertificateRequest{
  590. Subject: pkix.Name{
  591. CommonName: "foo.internal",
  592. },
  593. DNSNames: []string{"bar.internal"},
  594. }
  595. return test{
  596. o: o,
  597. csr: csr,
  598. prov: &MockProvisioner{
  599. MauthorizeSign: func(ctx context.Context, token string) ([]provisioner.SignOption, error) {
  600. assert.Equals(t, token, "")
  601. return nil, nil
  602. },
  603. MgetOptions: func() *provisioner.Options {
  604. return nil
  605. },
  606. },
  607. ca: &mockSignAuth{
  608. signWithContext: func(_ context.Context, _csr *x509.CertificateRequest, signOpts provisioner.SignOptions, extraOpts ...provisioner.SignOption) ([]*x509.Certificate, error) {
  609. assert.Equals(t, _csr, csr)
  610. return nil, errs.ForbiddenErr(&webhook.Error{Code: "theCode", Message: "The message"}, "forbidden error")
  611. },
  612. },
  613. db: &MockDB{
  614. MockGetAuthorization: func(ctx context.Context, id string) (*Authorization, error) {
  615. return &Authorization{ID: id, Status: StatusValid}, nil
  616. },
  617. },
  618. err: NewDetailedError(ErrorUnauthorizedType, "The message (theCode)").AddSubproblems(Subproblem{
  619. Type: "urn:smallstep:acme:error:theCode",
  620. Detail: "The message",
  621. }),
  622. }
  623. },
  624. "fail/error-db.CreateCertificate": func(t *testing.T) test {
  625. now := clock.Now()
  626. o := &Order{
  627. ID: "oID",
  628. AccountID: "accID",
  629. Status: StatusReady,
  630. ExpiresAt: now.Add(5 * time.Minute),
  631. AuthorizationIDs: []string{"a", "b"},
  632. Identifiers: []Identifier{
  633. {Type: "dns", Value: "foo.internal"},
  634. {Type: "dns", Value: "bar.internal"},
  635. },
  636. }
  637. csr := &x509.CertificateRequest{
  638. Subject: pkix.Name{
  639. CommonName: "foo.internal",
  640. },
  641. DNSNames: []string{"bar.internal"},
  642. }
  643. foo := &x509.Certificate{Subject: pkix.Name{CommonName: "foo"}}
  644. bar := &x509.Certificate{Subject: pkix.Name{CommonName: "bar"}}
  645. baz := &x509.Certificate{Subject: pkix.Name{CommonName: "baz"}}
  646. return test{
  647. o: o,
  648. csr: csr,
  649. prov: &MockProvisioner{
  650. MauthorizeSign: func(ctx context.Context, token string) ([]provisioner.SignOption, error) {
  651. assert.Equals(t, token, "")
  652. return nil, nil
  653. },
  654. MgetOptions: func() *provisioner.Options {
  655. return nil
  656. },
  657. },
  658. ca: &mockSignAuth{
  659. signWithContext: func(_ context.Context, _csr *x509.CertificateRequest, signOpts provisioner.SignOptions, extraOpts ...provisioner.SignOption) ([]*x509.Certificate, error) {
  660. assert.Equals(t, _csr, csr)
  661. return []*x509.Certificate{foo, bar, baz}, nil
  662. },
  663. },
  664. db: &MockDB{
  665. MockGetAuthorization: func(ctx context.Context, id string) (*Authorization, error) {
  666. return &Authorization{ID: id, Status: StatusValid}, nil
  667. },
  668. MockCreateCertificate: func(ctx context.Context, cert *Certificate) error {
  669. assert.Equals(t, cert.AccountID, o.AccountID)
  670. assert.Equals(t, cert.OrderID, o.ID)
  671. assert.Equals(t, cert.Leaf, foo)
  672. assert.Equals(t, cert.Intermediates, []*x509.Certificate{bar, baz})
  673. return errors.New("force")
  674. },
  675. },
  676. err: NewErrorISE("error creating certificate for order oID: force"),
  677. }
  678. },
  679. "fail/error-db.UpdateOrder": func(t *testing.T) test {
  680. now := clock.Now()
  681. o := &Order{
  682. ID: "oID",
  683. AccountID: "accID",
  684. Status: StatusReady,
  685. ExpiresAt: now.Add(5 * time.Minute),
  686. AuthorizationIDs: []string{"a", "b"},
  687. Identifiers: []Identifier{
  688. {Type: "dns", Value: "foo.internal"},
  689. {Type: "dns", Value: "bar.internal"},
  690. },
  691. }
  692. csr := &x509.CertificateRequest{
  693. Subject: pkix.Name{
  694. CommonName: "foo.internal",
  695. },
  696. DNSNames: []string{"bar.internal"},
  697. }
  698. foo := &x509.Certificate{Subject: pkix.Name{CommonName: "foo"}}
  699. bar := &x509.Certificate{Subject: pkix.Name{CommonName: "bar"}}
  700. baz := &x509.Certificate{Subject: pkix.Name{CommonName: "baz"}}
  701. return test{
  702. o: o,
  703. csr: csr,
  704. prov: &MockProvisioner{
  705. MauthorizeSign: func(ctx context.Context, token string) ([]provisioner.SignOption, error) {
  706. assert.Equals(t, token, "")
  707. return nil, nil
  708. },
  709. MgetOptions: func() *provisioner.Options {
  710. return nil
  711. },
  712. },
  713. ca: &mockSignAuth{
  714. signWithContext: func(_ context.Context, _csr *x509.CertificateRequest, signOpts provisioner.SignOptions, extraOpts ...provisioner.SignOption) ([]*x509.Certificate, error) {
  715. assert.Equals(t, _csr, csr)
  716. return []*x509.Certificate{foo, bar, baz}, nil
  717. },
  718. },
  719. db: &MockDB{
  720. MockGetAuthorization: func(ctx context.Context, id string) (*Authorization, error) {
  721. return &Authorization{ID: id, Status: StatusValid}, nil
  722. },
  723. MockCreateCertificate: func(ctx context.Context, cert *Certificate) error {
  724. cert.ID = "certID"
  725. assert.Equals(t, cert.AccountID, o.AccountID)
  726. assert.Equals(t, cert.OrderID, o.ID)
  727. assert.Equals(t, cert.Leaf, foo)
  728. assert.Equals(t, cert.Intermediates, []*x509.Certificate{bar, baz})
  729. return nil
  730. },
  731. MockUpdateOrder: func(ctx context.Context, updo *Order) error {
  732. assert.Equals(t, updo.CertificateID, "certID")
  733. assert.Equals(t, updo.Status, StatusValid)
  734. assert.Equals(t, updo.ID, o.ID)
  735. assert.Equals(t, updo.AccountID, o.AccountID)
  736. assert.Equals(t, updo.ExpiresAt, o.ExpiresAt)
  737. assert.Equals(t, updo.AuthorizationIDs, o.AuthorizationIDs)
  738. assert.Equals(t, updo.Identifiers, o.Identifiers)
  739. return errors.New("force")
  740. },
  741. },
  742. err: NewErrorISE("error updating order oID: force"),
  743. }
  744. },
  745. "fail/csr-fingerprint": func(t *testing.T) test {
  746. now := clock.Now()
  747. o := &Order{
  748. ID: "oID",
  749. AccountID: "accID",
  750. Status: StatusReady,
  751. ExpiresAt: now.Add(5 * time.Minute),
  752. AuthorizationIDs: []string{"a", "b"},
  753. Identifiers: []Identifier{
  754. {Type: "permanent-identifier", Value: "a-permanent-identifier"},
  755. },
  756. }
  757. signer := mustSigner("EC", "P-256", 0)
  758. csr := &x509.CertificateRequest{
  759. Subject: pkix.Name{
  760. CommonName: "a-permanent-identifier",
  761. },
  762. PublicKey: signer.Public(),
  763. ExtraExtensions: []pkix.Extension{
  764. {
  765. Id: asn1.ObjectIdentifier{1, 3, 6, 1, 5, 5, 7, 8, 3},
  766. Value: []byte("a-permanent-identifier"),
  767. },
  768. },
  769. }
  770. leaf := &x509.Certificate{
  771. Subject: pkix.Name{CommonName: "a-permanent-identifier"},
  772. PublicKey: signer.Public(),
  773. ExtraExtensions: []pkix.Extension{
  774. {
  775. Id: asn1.ObjectIdentifier{1, 3, 6, 1, 5, 5, 7, 8, 3},
  776. Value: []byte("a-permanent-identifier"),
  777. },
  778. },
  779. }
  780. inter := &x509.Certificate{Subject: pkix.Name{CommonName: "inter"}}
  781. root := &x509.Certificate{Subject: pkix.Name{CommonName: "root"}}
  782. return test{
  783. o: o,
  784. csr: csr,
  785. prov: &MockProvisioner{
  786. MauthorizeSign: func(ctx context.Context, token string) ([]provisioner.SignOption, error) {
  787. assert.Equals(t, token, "")
  788. return nil, nil
  789. },
  790. MgetOptions: func() *provisioner.Options {
  791. return nil
  792. },
  793. },
  794. ca: &mockSignAuth{
  795. signWithContext: func(_ context.Context, _csr *x509.CertificateRequest, signOpts provisioner.SignOptions, extraOpts ...provisioner.SignOption) ([]*x509.Certificate, error) {
  796. assert.Equals(t, _csr, csr)
  797. return []*x509.Certificate{leaf, inter, root}, nil
  798. },
  799. },
  800. db: &MockDB{
  801. MockGetAuthorization: func(ctx context.Context, id string) (*Authorization, error) {
  802. return &Authorization{
  803. ID: id,
  804. Fingerprint: "other-fingerprint",
  805. Status: StatusValid,
  806. }, nil
  807. },
  808. MockCreateCertificate: func(ctx context.Context, cert *Certificate) error {
  809. cert.ID = "certID"
  810. assert.Equals(t, cert.AccountID, o.AccountID)
  811. assert.Equals(t, cert.OrderID, o.ID)
  812. assert.Equals(t, cert.Leaf, leaf)
  813. assert.Equals(t, cert.Intermediates, []*x509.Certificate{inter, root})
  814. return nil
  815. },
  816. MockUpdateOrder: func(ctx context.Context, updo *Order) error {
  817. assert.Equals(t, updo.CertificateID, "certID")
  818. assert.Equals(t, updo.Status, StatusValid)
  819. assert.Equals(t, updo.ID, o.ID)
  820. assert.Equals(t, updo.AccountID, o.AccountID)
  821. assert.Equals(t, updo.ExpiresAt, o.ExpiresAt)
  822. assert.Equals(t, updo.AuthorizationIDs, o.AuthorizationIDs)
  823. assert.Equals(t, updo.Identifiers, o.Identifiers)
  824. return nil
  825. },
  826. },
  827. err: NewError(ErrorUnauthorizedType, "order oID csr does not match the attested key"),
  828. }
  829. },
  830. "ok/permanent-identifier": func(t *testing.T) test {
  831. now := clock.Now()
  832. o := &Order{
  833. ID: "oID",
  834. AccountID: "accID",
  835. Status: StatusReady,
  836. ExpiresAt: now.Add(5 * time.Minute),
  837. AuthorizationIDs: []string{"a", "b"},
  838. Identifiers: []Identifier{
  839. {Type: "permanent-identifier", Value: "a-permanent-identifier"},
  840. },
  841. }
  842. signer := mustSigner("EC", "P-256", 0)
  843. fingerprint, err := keyutil.Fingerprint(signer.Public())
  844. if err != nil {
  845. t.Fatal(err)
  846. }
  847. csr := &x509.CertificateRequest{
  848. Subject: pkix.Name{
  849. CommonName: "a-permanent-identifier",
  850. },
  851. PublicKey: signer.Public(),
  852. ExtraExtensions: []pkix.Extension{
  853. {
  854. Id: asn1.ObjectIdentifier{1, 3, 6, 1, 5, 5, 7, 8, 3},
  855. Value: []byte("a-permanent-identifier"),
  856. },
  857. },
  858. }
  859. leaf := &x509.Certificate{
  860. Subject: pkix.Name{CommonName: "a-permanent-identifier"},
  861. PublicKey: signer.Public(),
  862. ExtraExtensions: []pkix.Extension{
  863. {
  864. Id: asn1.ObjectIdentifier{1, 3, 6, 1, 5, 5, 7, 8, 3},
  865. Value: []byte("a-permanent-identifier"),
  866. },
  867. },
  868. }
  869. inter := &x509.Certificate{Subject: pkix.Name{CommonName: "inter"}}
  870. root := &x509.Certificate{Subject: pkix.Name{CommonName: "root"}}
  871. return test{
  872. o: o,
  873. csr: csr,
  874. prov: &MockProvisioner{
  875. MauthorizeSign: func(ctx context.Context, token string) ([]provisioner.SignOption, error) {
  876. assert.Equals(t, token, "")
  877. return nil, nil
  878. },
  879. MgetOptions: func() *provisioner.Options {
  880. return nil
  881. },
  882. },
  883. ca: &mockSignAuth{
  884. signWithContext: func(_ context.Context, _csr *x509.CertificateRequest, signOpts provisioner.SignOptions, extraOpts ...provisioner.SignOption) ([]*x509.Certificate, error) {
  885. assert.Equals(t, _csr, csr)
  886. return []*x509.Certificate{leaf, inter, root}, nil
  887. },
  888. },
  889. db: &MockDB{
  890. MockGetAuthorization: func(ctx context.Context, id string) (*Authorization, error) {
  891. switch id {
  892. case "a":
  893. return &Authorization{
  894. ID: id,
  895. Status: StatusValid,
  896. }, nil
  897. case "b":
  898. return &Authorization{
  899. ID: id,
  900. Fingerprint: fingerprint,
  901. Status: StatusValid,
  902. }, nil
  903. default:
  904. assert.FatalError(t, errors.Errorf("unexpected authorization %s", id))
  905. return nil, errors.New("force")
  906. }
  907. },
  908. MockCreateCertificate: func(ctx context.Context, cert *Certificate) error {
  909. cert.ID = "certID"
  910. assert.Equals(t, cert.AccountID, o.AccountID)
  911. assert.Equals(t, cert.OrderID, o.ID)
  912. assert.Equals(t, cert.Leaf, leaf)
  913. assert.Equals(t, cert.Intermediates, []*x509.Certificate{inter, root})
  914. return nil
  915. },
  916. MockUpdateOrder: func(ctx context.Context, updo *Order) error {
  917. assert.Equals(t, updo.CertificateID, "certID")
  918. assert.Equals(t, updo.Status, StatusValid)
  919. assert.Equals(t, updo.ID, o.ID)
  920. assert.Equals(t, updo.AccountID, o.AccountID)
  921. assert.Equals(t, updo.ExpiresAt, o.ExpiresAt)
  922. assert.Equals(t, updo.AuthorizationIDs, o.AuthorizationIDs)
  923. assert.Equals(t, updo.Identifiers, o.Identifiers)
  924. return nil
  925. },
  926. },
  927. }
  928. },
  929. "ok/permanent-identifier-only": func(t *testing.T) test {
  930. now := clock.Now()
  931. o := &Order{
  932. ID: "oID",
  933. AccountID: "accID",
  934. Status: StatusReady,
  935. ExpiresAt: now.Add(5 * time.Minute),
  936. AuthorizationIDs: []string{"a", "b"},
  937. Identifiers: []Identifier{
  938. {Type: "dns", Value: "foo.internal"},
  939. {Type: "permanent-identifier", Value: "a-permanent-identifier"},
  940. },
  941. }
  942. signer := mustSigner("EC", "P-256", 0)
  943. fingerprint, err := keyutil.Fingerprint(signer.Public())
  944. if err != nil {
  945. t.Fatal(err)
  946. }
  947. csr := &x509.CertificateRequest{
  948. Subject: pkix.Name{
  949. CommonName: "a-permanent-identifier",
  950. },
  951. DNSNames: []string{"foo.internal"},
  952. PublicKey: signer.Public(),
  953. ExtraExtensions: []pkix.Extension{
  954. {
  955. Id: asn1.ObjectIdentifier{1, 3, 6, 1, 5, 5, 7, 8, 3},
  956. Value: []byte("a-permanent-identifier"),
  957. },
  958. },
  959. }
  960. leaf := &x509.Certificate{
  961. Subject: pkix.Name{CommonName: "a-permanent-identifier"},
  962. PublicKey: signer.Public(),
  963. ExtraExtensions: []pkix.Extension{
  964. {
  965. Id: asn1.ObjectIdentifier{1, 3, 6, 1, 5, 5, 7, 8, 3},
  966. Value: []byte("a-permanent-identifier"),
  967. },
  968. },
  969. }
  970. inter := &x509.Certificate{Subject: pkix.Name{CommonName: "inter"}}
  971. root := &x509.Certificate{Subject: pkix.Name{CommonName: "root"}}
  972. return test{
  973. o: o,
  974. csr: csr,
  975. prov: &MockProvisioner{
  976. MauthorizeSign: func(ctx context.Context, token string) ([]provisioner.SignOption, error) {
  977. assert.Equals(t, token, "")
  978. return nil, nil
  979. },
  980. MgetOptions: func() *provisioner.Options {
  981. return nil
  982. },
  983. },
  984. // TODO(hs): we should work on making the mocks more realistic. Ideally, we should get rid of
  985. // the mock entirely, relying on an instances of provisioner, authority and DB (possibly hardest), so
  986. // that behavior of the tests is what an actual CA would do. We could gradually phase them out by
  987. // using the mocking functions as a wrapper for actual test helpers generated per test case or per
  988. // function that's tested.
  989. ca: &mockSignAuth{
  990. signWithContext: func(_ context.Context, _csr *x509.CertificateRequest, signOpts provisioner.SignOptions, extraOpts ...provisioner.SignOption) ([]*x509.Certificate, error) {
  991. assert.Equals(t, _csr, csr)
  992. return []*x509.Certificate{leaf, inter, root}, nil
  993. },
  994. },
  995. db: &MockDB{
  996. MockGetAuthorization: func(ctx context.Context, id string) (*Authorization, error) {
  997. return &Authorization{
  998. ID: id,
  999. Fingerprint: fingerprint,
  1000. Status: StatusValid,
  1001. }, nil
  1002. },
  1003. MockCreateCertificate: func(ctx context.Context, cert *Certificate) error {
  1004. cert.ID = "certID"
  1005. assert.Equals(t, cert.AccountID, o.AccountID)
  1006. assert.Equals(t, cert.OrderID, o.ID)
  1007. assert.Equals(t, cert.Leaf, leaf)
  1008. assert.Equals(t, cert.Intermediates, []*x509.Certificate{inter, root})
  1009. return nil
  1010. },
  1011. MockUpdateOrder: func(ctx context.Context, updo *Order) error {
  1012. assert.Equals(t, updo.CertificateID, "certID")
  1013. assert.Equals(t, updo.Status, StatusValid)
  1014. assert.Equals(t, updo.ID, o.ID)
  1015. assert.Equals(t, updo.AccountID, o.AccountID)
  1016. assert.Equals(t, updo.ExpiresAt, o.ExpiresAt)
  1017. assert.Equals(t, updo.AuthorizationIDs, o.AuthorizationIDs)
  1018. assert.Equals(t, updo.Identifiers, o.Identifiers)
  1019. return nil
  1020. },
  1021. },
  1022. }
  1023. },
  1024. "fail/csr-wire-id-csr-uri-missing": func(t *testing.T) test {
  1025. now := clock.Now()
  1026. o := &Order{
  1027. ID: "oID",
  1028. AccountID: "accID",
  1029. Status: StatusReady,
  1030. ExpiresAt: now.Add(5 * time.Minute),
  1031. AuthorizationIDs: []string{"a", "b"},
  1032. Identifiers: []Identifier{
  1033. {Type: "wireapp-device", Value: "{\"name\": \"device\", \"domain\": \"wire.com\", \"client-id\": \"wireapp://CzbfFjDOQrenCbDxVmgnFw!594930e9d50bb175@wire.com\", \"handle\": \"wireapp://%40alice_wire@wire.com\"}"},
  1034. },
  1035. }
  1036. signer := mustSigner("EC", "P-256", 0)
  1037. _, err := keyutil.Fingerprint(signer.Public())
  1038. if err != nil {
  1039. t.Fatal(err)
  1040. }
  1041. csr := &x509.CertificateRequest{
  1042. Subject: pkix.Name{
  1043. Names: []pkix.AttributeTypeAndValue{
  1044. {Type: asn1.ObjectIdentifier{2, 16, 840, 1, 113730, 3, 1, 241}, Value: "device"},
  1045. },
  1046. Organization: []string{"wire.com"},
  1047. },
  1048. PublicKey: signer.Public(),
  1049. ExtraExtensions: []pkix.Extension{
  1050. {
  1051. Id: asn1.ObjectIdentifier{2, 16, 840, 1, 113730, 3, 1, 241},
  1052. Value: []byte("a-wireapp-user"),
  1053. },
  1054. },
  1055. }
  1056. leaf := &x509.Certificate{
  1057. Subject: pkix.Name{CommonName: "a-wireapp-user"},
  1058. PublicKey: signer.Public(),
  1059. ExtraExtensions: []pkix.Extension{
  1060. {
  1061. Id: asn1.ObjectIdentifier{2, 16, 840, 1, 113730, 3, 1, 241},
  1062. Value: []byte("a-wireapp-user"),
  1063. },
  1064. },
  1065. }
  1066. inter := &x509.Certificate{Subject: pkix.Name{CommonName: "inter"}}
  1067. root := &x509.Certificate{Subject: pkix.Name{CommonName: "root"}}
  1068. return test{
  1069. o: o,
  1070. csr: csr,
  1071. prov: &MockProvisioner{
  1072. MauthorizeSign: func(ctx context.Context, token string) ([]provisioner.SignOption, error) {
  1073. assert.Equals(t, token, "")
  1074. return nil, nil
  1075. },
  1076. MgetOptions: func() *provisioner.Options {
  1077. return nil
  1078. },
  1079. },
  1080. ca: &mockSignAuth{
  1081. signWithContext: func(_ context.Context, _csr *x509.CertificateRequest, signOpts provisioner.SignOptions, extraOpts ...provisioner.SignOption) ([]*x509.Certificate, error) {
  1082. assert.Equals(t, _csr, csr)
  1083. return []*x509.Certificate{leaf, inter, root}, nil
  1084. },
  1085. },
  1086. db: &MockWireDB{
  1087. MockDB: MockDB{
  1088. MockGetAuthorization: func(ctx context.Context, id string) (*Authorization, error) {
  1089. return &Authorization{ID: id, Status: StatusValid}, nil
  1090. },
  1091. },
  1092. MockGetDpopToken: func(ctx context.Context, orderID string) (map[string]interface{}, error) {
  1093. assert.Equals(t, orderID, o.ID)
  1094. dpopMap := map[string]interface{}{
  1095. "dpop": "a-dpop-token",
  1096. }
  1097. return dpopMap, nil
  1098. },
  1099. MockGetOidcToken: func(ctx context.Context, orderID string) (map[string]interface{}, error) {
  1100. assert.Equals(t, orderID, o.ID)
  1101. oidcMap := map[string]interface{}{
  1102. "oidc": "a-oidc-token",
  1103. }
  1104. return oidcMap, nil
  1105. },
  1106. },
  1107. err: NewError(ErrorBadCSRType, "CSR URIs do not match identifiers exactly: CSR URIs = [], Order URIs = [wireapp://CzbfFjDOQrenCbDxVmgnFw%%21594930e9d50bb175@wire.com]"),
  1108. }
  1109. },
  1110. "fail/csr-wire-id-csr-uri-mismatch": func(t *testing.T) test {
  1111. now := clock.Now()
  1112. o := &Order{
  1113. ID: "oID",
  1114. AccountID: "accID",
  1115. Status: StatusReady,
  1116. ExpiresAt: now.Add(5 * time.Minute),
  1117. AuthorizationIDs: []string{"a", "b"},
  1118. Identifiers: []Identifier{
  1119. {Type: "wireapp-device", Value: "{\"name\": \"device\", \"domain\": \"wire.com\", \"client-id\": \"wireapp://CzbfFjDOQrenCbDxVmgnFw!594930e9d50bb175@wire.com\", \"handle\": \"wireapp://%40alice_wire@wire.com\"}"},
  1120. },
  1121. }
  1122. signer := mustSigner("EC", "P-256", 0)
  1123. _, err := keyutil.Fingerprint(signer.Public())
  1124. if err != nil {
  1125. t.Fatal(err)
  1126. }
  1127. wireURL, _ := url.Parse("someurl.com")
  1128. csr := &x509.CertificateRequest{
  1129. Subject: pkix.Name{
  1130. Names: []pkix.AttributeTypeAndValue{
  1131. {Type: asn1.ObjectIdentifier{2, 16, 840, 1, 113730, 3, 1, 241}, Value: "device"},
  1132. },
  1133. Organization: []string{"wire.com"},
  1134. },
  1135. URIs: []*url.URL{
  1136. wireURL,
  1137. },
  1138. PublicKey: signer.Public(),
  1139. ExtraExtensions: []pkix.Extension{
  1140. {
  1141. Id: asn1.ObjectIdentifier{2, 16, 840, 1, 113730, 3, 1, 241},
  1142. Value: []byte("a-wireapp-user"),
  1143. },
  1144. },
  1145. }
  1146. leaf := &x509.Certificate{
  1147. Subject: pkix.Name{CommonName: "a-wireapp-user"},
  1148. PublicKey: signer.Public(),
  1149. ExtraExtensions: []pkix.Extension{
  1150. {
  1151. Id: asn1.ObjectIdentifier{2, 16, 840, 1, 113730, 3, 1, 241},
  1152. Value: []byte("a-wireapp-user"),
  1153. },
  1154. },
  1155. }
  1156. inter := &x509.Certificate{Subject: pkix.Name{CommonName: "inter"}}
  1157. root := &x509.Certificate{Subject: pkix.Name{CommonName: "root"}}
  1158. return test{
  1159. o: o,
  1160. csr: csr,
  1161. prov: &MockProvisioner{
  1162. MauthorizeSign: func(ctx context.Context, token string) ([]provisioner.SignOption, error) {
  1163. assert.Equals(t, token, "")
  1164. return nil, nil
  1165. },
  1166. MgetOptions: func() *provisioner.Options {
  1167. return nil
  1168. },
  1169. },
  1170. ca: &mockSignAuth{
  1171. signWithContext: func(_ context.Context, _csr *x509.CertificateRequest, signOpts provisioner.SignOptions, extraOpts ...provisioner.SignOption) ([]*x509.Certificate, error) {
  1172. assert.Equals(t, _csr, csr)
  1173. return []*x509.Certificate{leaf, inter, root}, nil
  1174. },
  1175. },
  1176. db: &MockWireDB{
  1177. MockDB: MockDB{
  1178. MockGetAuthorization: func(ctx context.Context, id string) (*Authorization, error) {
  1179. return &Authorization{ID: id, Status: StatusValid}, nil
  1180. },
  1181. },
  1182. MockGetDpopToken: func(ctx context.Context, orderID string) (map[string]interface{}, error) {
  1183. assert.Equals(t, orderID, o.ID)
  1184. dpopMap := map[string]interface{}{
  1185. "dpop": "a-dpop-token",
  1186. }
  1187. return dpopMap, nil
  1188. },
  1189. MockGetOidcToken: func(ctx context.Context, orderID string) (map[string]interface{}, error) {
  1190. assert.Equals(t, orderID, o.ID)
  1191. oidcMap := map[string]interface{}{
  1192. "oidc": "a-oidc-token",
  1193. }
  1194. return oidcMap, nil
  1195. },
  1196. },
  1197. err: NewError(ErrorBadCSRType, "CSR URIs do not match identifiers exactly: CSR URIs = [someurl.com], Order URIs = [wireapp://CzbfFjDOQrenCbDxVmgnFw%%21594930e9d50bb175@wire.com]"),
  1198. }
  1199. },
  1200. "fail/other-than-wire-ids-present": func(t *testing.T) test {
  1201. now := clock.Now()
  1202. o := &Order{
  1203. ID: "oID",
  1204. AccountID: "accID",
  1205. Status: StatusReady,
  1206. ExpiresAt: now.Add(5 * time.Minute),
  1207. AuthorizationIDs: []string{"a", "b"},
  1208. Identifiers: []Identifier{
  1209. {Type: "wireapp-device", Value: "{\"name\": \"device\", \"domain\": \"wire.com\", \"client-id\": \"wireapp://CzbfFjDOQrenCbDxVmgnFw!594930e9d50bb175@wire.com\", \"handle\": \"wireapp://%40alice_wire@wire.com\"}"},
  1210. {Type: "permanent-identifier", Value: "a-permanent-identifier"},
  1211. },
  1212. }
  1213. signer := mustSigner("EC", "P-256", 0)
  1214. _, err := keyutil.Fingerprint(signer.Public())
  1215. if err != nil {
  1216. t.Fatal(err)
  1217. }
  1218. wireURL, _ := url.Parse("wireapp://CzbfFjDOQrenCbDxVmgnFw!594930e9d50bb175@wire.com")
  1219. csr := &x509.CertificateRequest{
  1220. Subject: pkix.Name{
  1221. Names: []pkix.AttributeTypeAndValue{
  1222. {Type: asn1.ObjectIdentifier{2, 16, 840, 1, 113730, 3, 1, 241}, Value: "device"},
  1223. },
  1224. Organization: []string{"wire.com"},
  1225. },
  1226. URIs: []*url.URL{
  1227. wireURL,
  1228. },
  1229. PublicKey: signer.Public(),
  1230. ExtraExtensions: []pkix.Extension{
  1231. {
  1232. Id: asn1.ObjectIdentifier{2, 16, 840, 1, 113730, 3, 1, 241},
  1233. Value: []byte("a-wireapp-user"),
  1234. },
  1235. },
  1236. }
  1237. leaf := &x509.Certificate{
  1238. Subject: pkix.Name{CommonName: "a-wireapp-user"},
  1239. PublicKey: signer.Public(),
  1240. ExtraExtensions: []pkix.Extension{
  1241. {
  1242. Id: asn1.ObjectIdentifier{2, 16, 840, 1, 113730, 3, 1, 241},
  1243. Value: []byte("a-wireapp-user"),
  1244. },
  1245. },
  1246. }
  1247. inter := &x509.Certificate{Subject: pkix.Name{CommonName: "inter"}}
  1248. root := &x509.Certificate{Subject: pkix.Name{CommonName: "root"}}
  1249. return test{
  1250. o: o,
  1251. csr: csr,
  1252. prov: &MockProvisioner{
  1253. MauthorizeSign: func(ctx context.Context, token string) ([]provisioner.SignOption, error) {
  1254. assert.Equals(t, token, "")
  1255. return nil, nil
  1256. },
  1257. MgetOptions: func() *provisioner.Options {
  1258. return nil
  1259. },
  1260. },
  1261. ca: &mockSignAuth{
  1262. signWithContext: func(_ context.Context, _csr *x509.CertificateRequest, signOpts provisioner.SignOptions, extraOpts ...provisioner.SignOption) ([]*x509.Certificate, error) {
  1263. assert.Equals(t, _csr, csr)
  1264. return []*x509.Certificate{leaf, inter, root}, nil
  1265. },
  1266. },
  1267. db: &MockWireDB{
  1268. MockDB: MockDB{
  1269. MockGetAuthorization: func(ctx context.Context, id string) (*Authorization, error) {
  1270. return &Authorization{ID: id, Status: StatusValid}, nil
  1271. },
  1272. },
  1273. },
  1274. err: NewError(ErrorServerInternalType, "order must have exactly one WireUser and WireDevice identifier"),
  1275. }
  1276. },
  1277. "fail/wire-id-org-missing": func(t *testing.T) test {
  1278. now := clock.Now()
  1279. o := &Order{
  1280. ID: "oID",
  1281. AccountID: "accID",
  1282. Status: StatusReady,
  1283. ExpiresAt: now.Add(5 * time.Minute),
  1284. AuthorizationIDs: []string{"a", "b"},
  1285. Identifiers: []Identifier{
  1286. {Type: "wireapp-user", Value: "{\"name\": \"Alice Smith\", \"domain\": \"wire.com\", \"handle\": \"wireapp://%40alice_wire@wire.com\"}"},
  1287. },
  1288. }
  1289. signer := mustSigner("EC", "P-256", 0)
  1290. _, err := keyutil.Fingerprint(signer.Public())
  1291. if err != nil {
  1292. t.Fatal(err)
  1293. }
  1294. wireURL, _ := url.Parse("wireapp://%40alice_wire@wire.com")
  1295. csr := &x509.CertificateRequest{
  1296. Subject: pkix.Name{
  1297. Names: []pkix.AttributeTypeAndValue{
  1298. {Type: asn1.ObjectIdentifier{2, 16, 840, 1, 113730, 3, 1, 241}, Value: "Alice Smith"},
  1299. },
  1300. },
  1301. URIs: []*url.URL{
  1302. wireURL,
  1303. },
  1304. PublicKey: signer.Public(),
  1305. }
  1306. leaf := &x509.Certificate{
  1307. Subject: pkix.Name{CommonName: "a-wireapp-user"},
  1308. PublicKey: signer.Public(),
  1309. ExtraExtensions: []pkix.Extension{
  1310. {
  1311. Id: asn1.ObjectIdentifier{2, 16, 840, 1, 113730, 3, 1, 241},
  1312. Value: []byte("a-wireapp-user"),
  1313. },
  1314. },
  1315. }
  1316. inter := &x509.Certificate{Subject: pkix.Name{CommonName: "inter"}}
  1317. root := &x509.Certificate{Subject: pkix.Name{CommonName: "root"}}
  1318. return test{
  1319. o: o,
  1320. csr: csr,
  1321. prov: &MockProvisioner{
  1322. MauthorizeSign: func(ctx context.Context, token string) ([]provisioner.SignOption, error) {
  1323. assert.Equals(t, token, "")
  1324. return nil, nil
  1325. },
  1326. MgetOptions: func() *provisioner.Options {
  1327. return nil
  1328. },
  1329. },
  1330. ca: &mockSignAuth{
  1331. signWithContext: func(_ context.Context, _csr *x509.CertificateRequest, signOpts provisioner.SignOptions, extraOpts ...provisioner.SignOption) ([]*x509.Certificate, error) {
  1332. assert.Equals(t, _csr, csr)
  1333. return []*x509.Certificate{leaf, inter, root}, nil
  1334. },
  1335. },
  1336. db: &MockWireDB{
  1337. MockDB: MockDB{
  1338. MockGetAuthorization: func(ctx context.Context, id string) (*Authorization, error) {
  1339. return &Authorization{ID: id, Status: StatusValid}, nil
  1340. },
  1341. },
  1342. },
  1343. err: NewError(ErrorServerInternalType, "expected Organization [wire.com], found []"),
  1344. }
  1345. },
  1346. "fail/wire-id-display-name-missing": func(t *testing.T) test {
  1347. now := clock.Now()
  1348. o := &Order{
  1349. ID: "oID",
  1350. AccountID: "accID",
  1351. Status: StatusReady,
  1352. ExpiresAt: now.Add(5 * time.Minute),
  1353. AuthorizationIDs: []string{"a", "b"},
  1354. Identifiers: []Identifier{
  1355. {Type: "wireapp-user", Value: "{\"name\": \"Alice Smith\", \"domain\": \"wire.com\", \"handle\": \"wireapp://%40alice_wire@wire.com\"}"},
  1356. },
  1357. }
  1358. signer := mustSigner("EC", "P-256", 0)
  1359. _, err := keyutil.Fingerprint(signer.Public())
  1360. if err != nil {
  1361. t.Fatal(err)
  1362. }
  1363. wireURL, _ := url.Parse("wireapp://%40alice_wire@wire.com")
  1364. csr := &x509.CertificateRequest{
  1365. Subject: pkix.Name{
  1366. Organization: []string{"wire.com"},
  1367. },
  1368. URIs: []*url.URL{
  1369. wireURL,
  1370. },
  1371. PublicKey: signer.Public(),
  1372. }
  1373. leaf := &x509.Certificate{
  1374. Subject: pkix.Name{CommonName: "a-wireapp-user"},
  1375. PublicKey: signer.Public(),
  1376. ExtraExtensions: []pkix.Extension{
  1377. {
  1378. Id: asn1.ObjectIdentifier{2, 16, 840, 1, 113730, 3, 1, 241},
  1379. Value: []byte("a-wireapp-user"),
  1380. },
  1381. },
  1382. }
  1383. inter := &x509.Certificate{Subject: pkix.Name{CommonName: "inter"}}
  1384. root := &x509.Certificate{Subject: pkix.Name{CommonName: "root"}}
  1385. return test{
  1386. o: o,
  1387. csr: csr,
  1388. prov: &MockProvisioner{
  1389. MauthorizeSign: func(ctx context.Context, token string) ([]provisioner.SignOption, error) {
  1390. assert.Equals(t, token, "")
  1391. return nil, nil
  1392. },
  1393. MgetOptions: func() *provisioner.Options {
  1394. return nil
  1395. },
  1396. },
  1397. ca: &mockSignAuth{
  1398. signWithContext: func(_ context.Context, _csr *x509.CertificateRequest, signOpts provisioner.SignOptions, extraOpts ...provisioner.SignOption) ([]*x509.Certificate, error) {
  1399. assert.Equals(t, _csr, csr)
  1400. return []*x509.Certificate{leaf, inter, root}, nil
  1401. },
  1402. },
  1403. db: &MockWireDB{
  1404. MockDB: MockDB{
  1405. MockGetAuthorization: func(ctx context.Context, id string) (*Authorization, error) {
  1406. return &Authorization{ID: id, Status: StatusValid}, nil
  1407. },
  1408. },
  1409. },
  1410. err: NewError(ErrorServerInternalType, "CSR must contain the display name in '2.16.840.1.113730.3.1.241' OID"),
  1411. }
  1412. },
  1413. "fail/wire-id-display-name-mismatch": func(t *testing.T) test {
  1414. now := clock.Now()
  1415. o := &Order{
  1416. ID: "oID",
  1417. AccountID: "accID",
  1418. Status: StatusReady,
  1419. ExpiresAt: now.Add(5 * time.Minute),
  1420. AuthorizationIDs: []string{"a", "b"},
  1421. Identifiers: []Identifier{
  1422. {Type: "wireapp-user", Value: "{\"name\": \"Alice Smith\", \"domain\": \"wire.com\", \"handle\": \"wireapp://%40alice_wire@wire.com\"}"},
  1423. },
  1424. }
  1425. signer := mustSigner("EC", "P-256", 0)
  1426. _, err := keyutil.Fingerprint(signer.Public())
  1427. if err != nil {
  1428. t.Fatal(err)
  1429. }
  1430. wireURL, _ := url.Parse("wireapp://%40alice_wire@wire.com")
  1431. csr := &x509.CertificateRequest{
  1432. Subject: pkix.Name{
  1433. Names: []pkix.AttributeTypeAndValue{
  1434. {Type: asn1.ObjectIdentifier{2, 16, 840, 1, 113730, 3, 1, 241}, Value: "Someone else"},
  1435. },
  1436. Organization: []string{"wire.com"},
  1437. },
  1438. URIs: []*url.URL{
  1439. wireURL,
  1440. },
  1441. PublicKey: signer.Public(),
  1442. }
  1443. leaf := &x509.Certificate{
  1444. Subject: pkix.Name{CommonName: "a-wireapp-user"},
  1445. PublicKey: signer.Public(),
  1446. ExtraExtensions: []pkix.Extension{
  1447. {
  1448. Id: asn1.ObjectIdentifier{2, 16, 840, 1, 113730, 3, 1, 241},
  1449. Value: []byte("a-wireapp-user"),
  1450. },
  1451. },
  1452. }
  1453. inter := &x509.Certificate{Subject: pkix.Name{CommonName: "inter"}}
  1454. root := &x509.Certificate{Subject: pkix.Name{CommonName: "root"}}
  1455. return test{
  1456. o: o,
  1457. csr: csr,
  1458. prov: &MockProvisioner{
  1459. MauthorizeSign: func(ctx context.Context, token string) ([]provisioner.SignOption, error) {
  1460. assert.Equals(t, token, "")
  1461. return nil, nil
  1462. },
  1463. MgetOptions: func() *provisioner.Options {
  1464. return nil
  1465. },
  1466. },
  1467. ca: &mockSignAuth{
  1468. signWithContext: func(_ context.Context, _csr *x509.CertificateRequest, signOpts provisioner.SignOptions, extraOpts ...provisioner.SignOption) ([]*x509.Certificate, error) {
  1469. assert.Equals(t, _csr, csr)
  1470. return []*x509.Certificate{leaf, inter, root}, nil
  1471. },
  1472. },
  1473. db: &MockWireDB{
  1474. MockDB: MockDB{
  1475. MockGetAuthorization: func(ctx context.Context, id string) (*Authorization, error) {
  1476. return &Authorization{ID: id, Status: StatusValid}, nil
  1477. },
  1478. },
  1479. },
  1480. err: NewError(ErrorServerInternalType, "expected displayName Alice Smith, found Someone else"),
  1481. }
  1482. },
  1483. "ok/wire-id-user": func(t *testing.T) test {
  1484. now := clock.Now()
  1485. o := &Order{
  1486. ID: "oID",
  1487. AccountID: "accID",
  1488. Status: StatusReady,
  1489. ExpiresAt: now.Add(5 * time.Minute),
  1490. AuthorizationIDs: []string{"a", "b"},
  1491. Identifiers: []Identifier{
  1492. {Type: "wireapp-user", Value: "{\"name\": \"Alice Smith\", \"domain\": \"wire.com\", \"handle\": \"wireapp://%40alice_wire@wire.com\"}"},
  1493. },
  1494. }
  1495. signer := mustSigner("EC", "P-256", 0)
  1496. _, err := keyutil.Fingerprint(signer.Public())
  1497. if err != nil {
  1498. t.Fatal(err)
  1499. }
  1500. wireURL, _ := url.Parse("wireapp://%40alice_wire@wire.com")
  1501. csr := &x509.CertificateRequest{
  1502. Subject: pkix.Name{
  1503. Names: []pkix.AttributeTypeAndValue{
  1504. {Type: asn1.ObjectIdentifier{2, 16, 840, 1, 113730, 3, 1, 241}, Value: "Alice Smith"},
  1505. },
  1506. Organization: []string{"wire.com"},
  1507. },
  1508. URIs: []*url.URL{
  1509. wireURL,
  1510. },
  1511. PublicKey: signer.Public(),
  1512. ExtraExtensions: []pkix.Extension{
  1513. {
  1514. Id: asn1.ObjectIdentifier{2, 16, 840, 1, 113730, 3, 1, 241},
  1515. Value: []byte("a-wireapp-user"),
  1516. },
  1517. },
  1518. }
  1519. leaf := &x509.Certificate{
  1520. Subject: pkix.Name{CommonName: "a-wireapp-user"},
  1521. PublicKey: signer.Public(),
  1522. ExtraExtensions: []pkix.Extension{
  1523. {
  1524. Id: asn1.ObjectIdentifier{2, 16, 840, 1, 113730, 3, 1, 241},
  1525. Value: []byte("a-wireapp-user"),
  1526. },
  1527. },
  1528. }
  1529. inter := &x509.Certificate{Subject: pkix.Name{CommonName: "inter"}}
  1530. root := &x509.Certificate{Subject: pkix.Name{CommonName: "root"}}
  1531. return test{
  1532. o: o,
  1533. csr: csr,
  1534. prov: &MockProvisioner{
  1535. MauthorizeSign: func(ctx context.Context, token string) ([]provisioner.SignOption, error) {
  1536. assert.Equals(t, token, "")
  1537. return nil, nil
  1538. },
  1539. MgetOptions: func() *provisioner.Options {
  1540. return nil
  1541. },
  1542. },
  1543. ca: &mockSignAuth{
  1544. signWithContext: func(_ context.Context, _csr *x509.CertificateRequest, signOpts provisioner.SignOptions, extraOpts ...provisioner.SignOption) ([]*x509.Certificate, error) {
  1545. assert.Equals(t, _csr, csr)
  1546. return []*x509.Certificate{leaf, inter, root}, nil
  1547. },
  1548. },
  1549. db: &MockWireDB{
  1550. MockDB: MockDB{
  1551. MockGetAuthorization: func(ctx context.Context, id string) (*Authorization, error) {
  1552. return &Authorization{ID: id, Status: StatusValid}, nil
  1553. },
  1554. },
  1555. MockGetDpopToken: func(ctx context.Context, orderID string) (map[string]interface{}, error) {
  1556. assert.Equals(t, orderID, o.ID)
  1557. dpopMap := map[string]interface{}{
  1558. "dpop": "a-dpop-token",
  1559. }
  1560. return dpopMap, nil
  1561. },
  1562. MockGetOidcToken: func(ctx context.Context, orderID string) (map[string]interface{}, error) {
  1563. assert.Equals(t, orderID, o.ID)
  1564. oidcMap := map[string]interface{}{
  1565. "oidc": "a-oidc-token",
  1566. }
  1567. return oidcMap, nil
  1568. },
  1569. },
  1570. }
  1571. },
  1572. "ok/wire-id-device": func(t *testing.T) test {
  1573. now := clock.Now()
  1574. o := &Order{
  1575. ID: "oID",
  1576. AccountID: "accID",
  1577. Status: StatusReady,
  1578. ExpiresAt: now.Add(5 * time.Minute),
  1579. AuthorizationIDs: []string{"a", "b"},
  1580. Identifiers: []Identifier{
  1581. {Type: "wireapp-device", Value: "{\"name\": \"device\", \"domain\": \"wire.com\", \"client-id\": \"wireapp://CzbfFjDOQrenCbDxVmgnFw!594930e9d50bb175@wire.com\", \"handle\": \"wireapp://%40alice_wire@wire.com\"}"},
  1582. },
  1583. }
  1584. signer := mustSigner("EC", "P-256", 0)
  1585. _, err := keyutil.Fingerprint(signer.Public())
  1586. if err != nil {
  1587. t.Fatal(err)
  1588. }
  1589. wireURL, _ := url.Parse("wireapp://CzbfFjDOQrenCbDxVmgnFw!594930e9d50bb175@wire.com")
  1590. csr := &x509.CertificateRequest{
  1591. Subject: pkix.Name{
  1592. Names: []pkix.AttributeTypeAndValue{
  1593. {Type: asn1.ObjectIdentifier{2, 16, 840, 1, 113730, 3, 1, 241}, Value: "device"},
  1594. },
  1595. Organization: []string{"wire.com"},
  1596. },
  1597. URIs: []*url.URL{
  1598. wireURL,
  1599. },
  1600. PublicKey: signer.Public(),
  1601. ExtraExtensions: []pkix.Extension{
  1602. {
  1603. Id: asn1.ObjectIdentifier{2, 16, 840, 1, 113730, 3, 1, 241},
  1604. Value: []byte("a-wireapp-user"),
  1605. },
  1606. },
  1607. }
  1608. leaf := &x509.Certificate{
  1609. Subject: pkix.Name{CommonName: "a-wireapp-user"},
  1610. PublicKey: signer.Public(),
  1611. ExtraExtensions: []pkix.Extension{
  1612. {
  1613. Id: asn1.ObjectIdentifier{2, 16, 840, 1, 113730, 3, 1, 241},
  1614. Value: []byte("a-wireapp-user"),
  1615. },
  1616. },
  1617. }
  1618. inter := &x509.Certificate{Subject: pkix.Name{CommonName: "inter"}}
  1619. root := &x509.Certificate{Subject: pkix.Name{CommonName: "root"}}
  1620. return test{
  1621. o: o,
  1622. csr: csr,
  1623. prov: &MockProvisioner{
  1624. MauthorizeSign: func(ctx context.Context, token string) ([]provisioner.SignOption, error) {
  1625. assert.Equals(t, token, "")
  1626. return nil, nil
  1627. },
  1628. MgetOptions: func() *provisioner.Options {
  1629. return nil
  1630. },
  1631. },
  1632. ca: &mockSignAuth{
  1633. signWithContext: func(_ context.Context, _csr *x509.CertificateRequest, signOpts provisioner.SignOptions, extraOpts ...provisioner.SignOption) ([]*x509.Certificate, error) {
  1634. assert.Equals(t, _csr, csr)
  1635. return []*x509.Certificate{leaf, inter, root}, nil
  1636. },
  1637. },
  1638. db: &MockWireDB{
  1639. MockDB: MockDB{
  1640. MockGetAuthorization: func(ctx context.Context, id string) (*Authorization, error) {
  1641. return &Authorization{ID: id, Status: StatusValid}, nil
  1642. },
  1643. },
  1644. MockGetDpopToken: func(ctx context.Context, orderID string) (map[string]interface{}, error) {
  1645. assert.Equals(t, orderID, o.ID)
  1646. dpopMap := map[string]interface{}{
  1647. "dpop": "a-dpop-token",
  1648. }
  1649. return dpopMap, nil
  1650. },
  1651. MockGetOidcToken: func(ctx context.Context, orderID string) (map[string]interface{}, error) {
  1652. assert.Equals(t, orderID, o.ID)
  1653. oidcMap := map[string]interface{}{
  1654. "oidc": "a-oidc-token",
  1655. }
  1656. return oidcMap, nil
  1657. },
  1658. },
  1659. }
  1660. },
  1661. "ok/new-cert-dns": func(t *testing.T) test {
  1662. now := clock.Now()
  1663. o := &Order{
  1664. ID: "oID",
  1665. AccountID: "accID",
  1666. Status: StatusReady,
  1667. ExpiresAt: now.Add(5 * time.Minute),
  1668. AuthorizationIDs: []string{"a", "b"},
  1669. Identifiers: []Identifier{
  1670. {Type: "dns", Value: "foo.internal"},
  1671. {Type: "dns", Value: "bar.internal"},
  1672. },
  1673. }
  1674. csr := &x509.CertificateRequest{
  1675. Subject: pkix.Name{
  1676. CommonName: "foo.internal",
  1677. },
  1678. DNSNames: []string{"bar.internal"},
  1679. }
  1680. foo := &x509.Certificate{Subject: pkix.Name{CommonName: "foo"}}
  1681. bar := &x509.Certificate{Subject: pkix.Name{CommonName: "bar"}}
  1682. baz := &x509.Certificate{Subject: pkix.Name{CommonName: "baz"}}
  1683. return test{
  1684. o: o,
  1685. csr: csr,
  1686. prov: &MockProvisioner{
  1687. MauthorizeSign: func(ctx context.Context, token string) ([]provisioner.SignOption, error) {
  1688. assert.Equals(t, token, "")
  1689. return nil, nil
  1690. },
  1691. MgetOptions: func() *provisioner.Options {
  1692. return nil
  1693. },
  1694. },
  1695. ca: &mockSignAuth{
  1696. signWithContext: func(_ context.Context, _csr *x509.CertificateRequest, signOpts provisioner.SignOptions, extraOpts ...provisioner.SignOption) ([]*x509.Certificate, error) {
  1697. assert.Equals(t, _csr, csr)
  1698. return []*x509.Certificate{foo, bar, baz}, nil
  1699. },
  1700. },
  1701. db: &MockDB{
  1702. MockGetAuthorization: func(ctx context.Context, id string) (*Authorization, error) {
  1703. return &Authorization{ID: id, Status: StatusValid}, nil
  1704. },
  1705. MockCreateCertificate: func(ctx context.Context, cert *Certificate) error {
  1706. cert.ID = "certID"
  1707. assert.Equals(t, cert.AccountID, o.AccountID)
  1708. assert.Equals(t, cert.OrderID, o.ID)
  1709. assert.Equals(t, cert.Leaf, foo)
  1710. assert.Equals(t, cert.Intermediates, []*x509.Certificate{bar, baz})
  1711. return nil
  1712. },
  1713. MockUpdateOrder: func(ctx context.Context, updo *Order) error {
  1714. assert.Equals(t, updo.CertificateID, "certID")
  1715. assert.Equals(t, updo.Status, StatusValid)
  1716. assert.Equals(t, updo.ID, o.ID)
  1717. assert.Equals(t, updo.AccountID, o.AccountID)
  1718. assert.Equals(t, updo.ExpiresAt, o.ExpiresAt)
  1719. assert.Equals(t, updo.AuthorizationIDs, o.AuthorizationIDs)
  1720. assert.Equals(t, updo.Identifiers, o.Identifiers)
  1721. return nil
  1722. },
  1723. },
  1724. }
  1725. },
  1726. "ok/new-cert-ip": func(t *testing.T) test {
  1727. now := clock.Now()
  1728. o := &Order{
  1729. ID: "oID",
  1730. AccountID: "accID",
  1731. Status: StatusReady,
  1732. ExpiresAt: now.Add(5 * time.Minute),
  1733. AuthorizationIDs: []string{"a", "b"},
  1734. Identifiers: []Identifier{
  1735. {Type: "ip", Value: "192.168.42.42"},
  1736. {Type: "ip", Value: "192.168.43.42"},
  1737. },
  1738. }
  1739. csr := &x509.CertificateRequest{
  1740. IPAddresses: []net.IP{net.ParseIP("192.168.42.42"), net.ParseIP("192.168.43.42")}, // in case of IPs, no Common Name
  1741. }
  1742. foo := &x509.Certificate{Subject: pkix.Name{CommonName: "foo"}}
  1743. bar := &x509.Certificate{Subject: pkix.Name{CommonName: "bar"}}
  1744. baz := &x509.Certificate{Subject: pkix.Name{CommonName: "baz"}}
  1745. return test{
  1746. o: o,
  1747. csr: csr,
  1748. prov: &MockProvisioner{
  1749. MauthorizeSign: func(ctx context.Context, token string) ([]provisioner.SignOption, error) {
  1750. assert.Equals(t, token, "")
  1751. return nil, nil
  1752. },
  1753. MgetOptions: func() *provisioner.Options {
  1754. return nil
  1755. },
  1756. },
  1757. ca: &mockSignAuth{
  1758. signWithContext: func(_ context.Context, _csr *x509.CertificateRequest, signOpts provisioner.SignOptions, extraOpts ...provisioner.SignOption) ([]*x509.Certificate, error) {
  1759. assert.Equals(t, _csr, csr)
  1760. return []*x509.Certificate{foo, bar, baz}, nil
  1761. },
  1762. },
  1763. db: &MockDB{
  1764. MockGetAuthorization: func(ctx context.Context, id string) (*Authorization, error) {
  1765. return &Authorization{ID: id, Status: StatusValid}, nil
  1766. },
  1767. MockCreateCertificate: func(ctx context.Context, cert *Certificate) error {
  1768. cert.ID = "certID"
  1769. assert.Equals(t, cert.AccountID, o.AccountID)
  1770. assert.Equals(t, cert.OrderID, o.ID)
  1771. assert.Equals(t, cert.Leaf, foo)
  1772. assert.Equals(t, cert.Intermediates, []*x509.Certificate{bar, baz})
  1773. return nil
  1774. },
  1775. MockUpdateOrder: func(ctx context.Context, updo *Order) error {
  1776. assert.Equals(t, updo.CertificateID, "certID")
  1777. assert.Equals(t, updo.Status, StatusValid)
  1778. assert.Equals(t, updo.ID, o.ID)
  1779. assert.Equals(t, updo.AccountID, o.AccountID)
  1780. assert.Equals(t, updo.ExpiresAt, o.ExpiresAt)
  1781. assert.Equals(t, updo.AuthorizationIDs, o.AuthorizationIDs)
  1782. assert.Equals(t, updo.Identifiers, o.Identifiers)
  1783. return nil
  1784. },
  1785. },
  1786. }
  1787. },
  1788. "ok/new-cert-dns-and-ip": func(t *testing.T) test {
  1789. now := clock.Now()
  1790. o := &Order{
  1791. ID: "oID",
  1792. AccountID: "accID",
  1793. Status: StatusReady,
  1794. ExpiresAt: now.Add(5 * time.Minute),
  1795. AuthorizationIDs: []string{"a", "b"},
  1796. Identifiers: []Identifier{
  1797. {Type: "dns", Value: "foo.internal"},
  1798. {Type: "ip", Value: "192.168.42.42"},
  1799. },
  1800. }
  1801. csr := &x509.CertificateRequest{
  1802. Subject: pkix.Name{
  1803. CommonName: "foo.internal",
  1804. },
  1805. IPAddresses: []net.IP{net.ParseIP("192.168.42.42")},
  1806. }
  1807. foo := &x509.Certificate{Subject: pkix.Name{CommonName: "foo"}}
  1808. bar := &x509.Certificate{Subject: pkix.Name{CommonName: "bar"}}
  1809. baz := &x509.Certificate{Subject: pkix.Name{CommonName: "baz"}}
  1810. return test{
  1811. o: o,
  1812. csr: csr,
  1813. prov: &MockProvisioner{
  1814. MauthorizeSign: func(ctx context.Context, token string) ([]provisioner.SignOption, error) {
  1815. assert.Equals(t, token, "")
  1816. return nil, nil
  1817. },
  1818. MgetOptions: func() *provisioner.Options {
  1819. return nil
  1820. },
  1821. },
  1822. ca: &mockSignAuth{
  1823. signWithContext: func(_ context.Context, _csr *x509.CertificateRequest, signOpts provisioner.SignOptions, extraOpts ...provisioner.SignOption) ([]*x509.Certificate, error) {
  1824. assert.Equals(t, _csr, csr)
  1825. return []*x509.Certificate{foo, bar, baz}, nil
  1826. },
  1827. },
  1828. db: &MockDB{
  1829. MockGetAuthorization: func(ctx context.Context, id string) (*Authorization, error) {
  1830. return &Authorization{ID: id, Status: StatusValid}, nil
  1831. },
  1832. MockCreateCertificate: func(ctx context.Context, cert *Certificate) error {
  1833. cert.ID = "certID"
  1834. assert.Equals(t, cert.AccountID, o.AccountID)
  1835. assert.Equals(t, cert.OrderID, o.ID)
  1836. assert.Equals(t, cert.Leaf, foo)
  1837. assert.Equals(t, cert.Intermediates, []*x509.Certificate{bar, baz})
  1838. return nil
  1839. },
  1840. MockUpdateOrder: func(ctx context.Context, updo *Order) error {
  1841. assert.Equals(t, updo.CertificateID, "certID")
  1842. assert.Equals(t, updo.Status, StatusValid)
  1843. assert.Equals(t, updo.ID, o.ID)
  1844. assert.Equals(t, updo.AccountID, o.AccountID)
  1845. assert.Equals(t, updo.ExpiresAt, o.ExpiresAt)
  1846. assert.Equals(t, updo.AuthorizationIDs, o.AuthorizationIDs)
  1847. assert.Equals(t, updo.Identifiers, o.Identifiers)
  1848. return nil
  1849. },
  1850. },
  1851. }
  1852. },
  1853. }
  1854. for name, run := range tests {
  1855. t.Run(name, func(t *testing.T) {
  1856. tc := run(t)
  1857. if err := tc.o.Finalize(context.Background(), tc.db, tc.csr, tc.ca, tc.prov); err != nil {
  1858. if assert.NotNil(t, tc.err) {
  1859. var k *Error
  1860. if errors.As(err, &k) {
  1861. assert.Equals(t, k.Type, tc.err.Type)
  1862. assert.Equals(t, k.Detail, tc.err.Detail)
  1863. assert.Equals(t, k.Status, tc.err.Status)
  1864. assert.Equals(t, k.Err.Error(), tc.err.Err.Error())
  1865. assert.Equals(t, k.Detail, tc.err.Detail)
  1866. assert.Equals(t, k.Subproblems, tc.err.Subproblems)
  1867. } else {
  1868. assert.FatalError(t, errors.New("unexpected error type"))
  1869. }
  1870. }
  1871. } else {
  1872. assert.Nil(t, tc.err)
  1873. }
  1874. })
  1875. }
  1876. }
  1877. func Test_uniqueSortedIPs(t *testing.T) {
  1878. type args struct {
  1879. ips []net.IP
  1880. }
  1881. tests := []struct {
  1882. name string
  1883. args args
  1884. want []net.IP
  1885. }{
  1886. {
  1887. name: "ok/empty",
  1888. args: args{
  1889. ips: []net.IP{},
  1890. },
  1891. want: []net.IP{},
  1892. },
  1893. {
  1894. name: "ok/single-ipv4",
  1895. args: args{
  1896. ips: []net.IP{net.ParseIP("192.168.42.42")},
  1897. },
  1898. want: []net.IP{net.ParseIP("192.168.42.42")},
  1899. },
  1900. {
  1901. name: "ok/multiple-ipv4",
  1902. args: args{
  1903. ips: []net.IP{net.ParseIP("192.168.42.42"), net.ParseIP("192.168.42.10"), net.ParseIP("192.168.42.1"), net.ParseIP("127.0.0.1")},
  1904. },
  1905. want: []net.IP{net.ParseIP("127.0.0.1"), net.ParseIP("192.168.42.1"), net.ParseIP("192.168.42.10"), net.ParseIP("192.168.42.42")},
  1906. }, {
  1907. name: "ok/multiple-ipv4-with-varying-byte-representations",
  1908. args: args{
  1909. ips: []net.IP{net.ParseIP("192.168.42.42"), net.ParseIP("192.168.42.10"), net.ParseIP("192.168.42.1"), []byte{0x7f, 0x0, 0x0, 0x1}},
  1910. },
  1911. want: []net.IP{net.ParseIP("127.0.0.1"), net.ParseIP("192.168.42.1"), net.ParseIP("192.168.42.10"), net.ParseIP("192.168.42.42")},
  1912. },
  1913. {
  1914. name: "ok/unique-ipv4",
  1915. args: args{
  1916. ips: []net.IP{net.ParseIP("192.168.42.42"), net.ParseIP("192.168.42.42")},
  1917. },
  1918. want: []net.IP{net.ParseIP("192.168.42.42")},
  1919. },
  1920. {
  1921. name: "ok/single-ipv6",
  1922. args: args{
  1923. ips: []net.IP{net.ParseIP("2001:db8::30")},
  1924. },
  1925. want: []net.IP{net.ParseIP("2001:db8::30")},
  1926. },
  1927. {
  1928. name: "ok/multiple-ipv6",
  1929. args: args{
  1930. ips: []net.IP{net.ParseIP("2001:db8::30"), net.ParseIP("2001:db8::20"), net.ParseIP("2001:db8::10")},
  1931. },
  1932. want: []net.IP{net.ParseIP("2001:db8::10"), net.ParseIP("2001:db8::20"), net.ParseIP("2001:db8::30")},
  1933. },
  1934. {
  1935. name: "ok/unique-ipv6",
  1936. args: args{
  1937. ips: []net.IP{net.ParseIP("2001:db8::1"), net.ParseIP("2001:db8::1")},
  1938. },
  1939. want: []net.IP{net.ParseIP("2001:db8::1")},
  1940. },
  1941. {
  1942. name: "ok/mixed-ipv4-and-ipv6",
  1943. args: args{
  1944. ips: []net.IP{net.ParseIP("2001:db8::1"), net.ParseIP("2001:db8::1"), net.ParseIP("192.168.42.42"), net.ParseIP("192.168.42.42")},
  1945. },
  1946. want: []net.IP{net.ParseIP("192.168.42.42"), net.ParseIP("2001:db8::1")},
  1947. },
  1948. {
  1949. name: "ok/mixed-ipv4-and-ipv6-and-varying-byte-representations",
  1950. args: args{
  1951. ips: []net.IP{net.ParseIP("2001:db8::1"), net.ParseIP("2001:db8::1"), net.ParseIP("192.168.42.42"), net.ParseIP("192.168.42.42"), []byte{0x7f, 0x0, 0x0, 0x1}},
  1952. },
  1953. want: []net.IP{net.ParseIP("127.0.0.1"), net.ParseIP("192.168.42.42"), net.ParseIP("2001:db8::1")},
  1954. },
  1955. {
  1956. name: "ok/mixed-ipv4-and-ipv6-and-more-varying-byte-representations",
  1957. args: args{
  1958. ips: []net.IP{net.ParseIP("2001:db8::1"), net.ParseIP("2001:db8::1"), net.ParseIP("192.168.42.42"), net.ParseIP("2001:db8::2"), net.ParseIP("192.168.42.42"), []byte{0x7f, 0x0, 0x0, 0x1}, []byte{0x7f, 0x0, 0x0, 0x1}, []byte{0x7f, 0x0, 0x0, 0x2}},
  1959. },
  1960. want: []net.IP{net.ParseIP("127.0.0.1"), net.ParseIP("127.0.0.2"), net.ParseIP("192.168.42.42"), net.ParseIP("2001:db8::1"), net.ParseIP("2001:db8::2")},
  1961. },
  1962. }
  1963. for _, tt := range tests {
  1964. t.Run(tt.name, func(t *testing.T) {
  1965. got := uniqueSortedIPs(tt.args.ips)
  1966. if !cmp.Equal(tt.want, got) {
  1967. t.Errorf("uniqueSortedIPs() diff =\n%s", cmp.Diff(tt.want, got))
  1968. }
  1969. })
  1970. }
  1971. }
  1972. func Test_numberOfIdentifierType(t *testing.T) {
  1973. type args struct {
  1974. typ IdentifierType
  1975. ids []Identifier
  1976. }
  1977. tests := []struct {
  1978. name string
  1979. args args
  1980. want int
  1981. }{
  1982. {
  1983. name: "ok/no-identifiers",
  1984. args: args{
  1985. typ: DNS,
  1986. ids: []Identifier{},
  1987. },
  1988. want: 0,
  1989. },
  1990. {
  1991. name: "ok/no-dns",
  1992. args: args{
  1993. typ: DNS,
  1994. ids: []Identifier{
  1995. {
  1996. Type: IP,
  1997. Value: "192.168.42.42",
  1998. },
  1999. },
  2000. },
  2001. want: 0,
  2002. },
  2003. {
  2004. name: "ok/no-ips",
  2005. args: args{
  2006. typ: IP,
  2007. ids: []Identifier{
  2008. {
  2009. Type: DNS,
  2010. Value: "example.com",
  2011. },
  2012. },
  2013. },
  2014. want: 0,
  2015. },
  2016. {
  2017. name: "ok/one-dns",
  2018. args: args{
  2019. typ: DNS,
  2020. ids: []Identifier{
  2021. {
  2022. Type: DNS,
  2023. Value: "example.com",
  2024. },
  2025. {
  2026. Type: IP,
  2027. Value: "192.168.42.42",
  2028. },
  2029. },
  2030. },
  2031. want: 1,
  2032. },
  2033. {
  2034. name: "ok/one-ip",
  2035. args: args{
  2036. typ: IP,
  2037. ids: []Identifier{
  2038. {
  2039. Type: DNS,
  2040. Value: "example.com",
  2041. },
  2042. {
  2043. Type: IP,
  2044. Value: "192.168.42.42",
  2045. },
  2046. },
  2047. },
  2048. want: 1,
  2049. },
  2050. {
  2051. name: "ok/more-dns",
  2052. args: args{
  2053. typ: DNS,
  2054. ids: []Identifier{
  2055. {
  2056. Type: DNS,
  2057. Value: "example.com",
  2058. },
  2059. {
  2060. Type: DNS,
  2061. Value: "*.example.com",
  2062. },
  2063. {
  2064. Type: IP,
  2065. Value: "192.168.42.42",
  2066. },
  2067. },
  2068. },
  2069. want: 2,
  2070. },
  2071. {
  2072. name: "ok/more-ips",
  2073. args: args{
  2074. typ: IP,
  2075. ids: []Identifier{
  2076. {
  2077. Type: DNS,
  2078. Value: "example.com",
  2079. },
  2080. {
  2081. Type: IP,
  2082. Value: "192.168.42.42",
  2083. },
  2084. {
  2085. Type: IP,
  2086. Value: "192.168.42.43",
  2087. },
  2088. },
  2089. },
  2090. want: 2,
  2091. },
  2092. }
  2093. for _, tt := range tests {
  2094. t.Run(tt.name, func(t *testing.T) {
  2095. if got := numberOfIdentifierType(tt.args.typ, tt.args.ids); got != tt.want {
  2096. t.Errorf("numberOfIdentifierType() = %v, want %v", got, tt.want)
  2097. }
  2098. })
  2099. }
  2100. }
  2101. func Test_ipsAreEqual(t *testing.T) {
  2102. type args struct {
  2103. x net.IP
  2104. y net.IP
  2105. }
  2106. tests := []struct {
  2107. name string
  2108. args args
  2109. want bool
  2110. }{
  2111. {
  2112. name: "ok/ipv4",
  2113. args: args{
  2114. x: net.ParseIP("192.168.42.42"),
  2115. y: net.ParseIP("192.168.42.42"),
  2116. },
  2117. want: true,
  2118. },
  2119. {
  2120. name: "fail/ipv4",
  2121. args: args{
  2122. x: net.ParseIP("192.168.42.42"),
  2123. y: net.ParseIP("192.168.42.43"),
  2124. },
  2125. want: false,
  2126. },
  2127. {
  2128. name: "ok/ipv6",
  2129. args: args{
  2130. x: net.ParseIP("2001:0db8:85a3:0000:0000:8a2e:0370:7334"),
  2131. y: net.ParseIP("2001:0db8:85a3:0000:0000:8a2e:0370:7334"),
  2132. },
  2133. want: true,
  2134. },
  2135. {
  2136. name: "fail/ipv6",
  2137. args: args{
  2138. x: net.ParseIP("2001:0db8:85a3:0000:0000:8a2e:0370:7334"),
  2139. y: net.ParseIP("2001:0db8:85a3:0000:0000:8a2e:0370:7335"),
  2140. },
  2141. want: false,
  2142. },
  2143. {
  2144. name: "fail/ipv4-and-ipv6",
  2145. args: args{
  2146. x: net.ParseIP("192.168.42.42"),
  2147. y: net.ParseIP("2001:0db8:85a3:0000:0000:8a2e:0370:7334"),
  2148. },
  2149. want: false,
  2150. },
  2151. {
  2152. name: "ok/ipv4-mapped-to-ipv6",
  2153. args: args{
  2154. x: net.ParseIP("192.168.42.42"),
  2155. y: net.ParseIP("::ffff:192.168.42.42"), // parsed to the same IPv4 by Go
  2156. },
  2157. want: true, // we expect this to happen; a known issue in which ipv4 mapped ipv6 addresses are considered the same as their ipv4 counterpart
  2158. },
  2159. {
  2160. name: "fail/invalid-ipv4-and-valid-ipv6",
  2161. args: args{
  2162. x: net.ParseIP("192.168.42.1000"),
  2163. y: net.ParseIP("2001:0db8:85a3:0000:0000:8a2e:0370:7334"),
  2164. },
  2165. want: false,
  2166. },
  2167. {
  2168. name: "fail/valid-ipv4-and-invalid-ipv6",
  2169. args: args{
  2170. x: net.ParseIP("192.168.42.42"),
  2171. y: net.ParseIP("2001:0db8:85a3:0000:0000:8a2e:0370:733400"),
  2172. },
  2173. want: false,
  2174. },
  2175. {
  2176. name: "fail/invalid-ipv4-and-invalid-ipv6",
  2177. args: args{
  2178. x: net.ParseIP("192.168.42.1000"),
  2179. y: net.ParseIP("2001:0db8:85a3:0000:0000:8a2e:0370:1000000"),
  2180. },
  2181. want: false,
  2182. },
  2183. }
  2184. for _, tt := range tests {
  2185. t.Run(tt.name, func(t *testing.T) {
  2186. if got := ipsAreEqual(tt.args.x, tt.args.y); got != tt.want {
  2187. t.Errorf("ipsAreEqual() = %v, want %v", got, tt.want)
  2188. }
  2189. })
  2190. }
  2191. }
  2192. func Test_canonicalize(t *testing.T) {
  2193. type args struct {
  2194. csr *x509.CertificateRequest
  2195. }
  2196. tests := []struct {
  2197. name string
  2198. args args
  2199. want *x509.CertificateRequest
  2200. }{
  2201. {
  2202. name: "ok/dns",
  2203. args: args{
  2204. csr: &x509.CertificateRequest{
  2205. DNSNames: []string{"www.example.com", "example.com"},
  2206. },
  2207. },
  2208. want: &x509.CertificateRequest{
  2209. DNSNames: []string{"example.com", "www.example.com"},
  2210. IPAddresses: []net.IP{},
  2211. },
  2212. },
  2213. {
  2214. name: "ok/common-name",
  2215. args: args{
  2216. csr: &x509.CertificateRequest{
  2217. Subject: pkix.Name{
  2218. CommonName: "example.com",
  2219. },
  2220. DNSNames: []string{"www.example.com"},
  2221. },
  2222. },
  2223. want: &x509.CertificateRequest{
  2224. Subject: pkix.Name{
  2225. CommonName: "example.com",
  2226. },
  2227. DNSNames: []string{"example.com", "www.example.com"},
  2228. IPAddresses: []net.IP{},
  2229. },
  2230. },
  2231. {
  2232. name: "ok/ipv4",
  2233. args: args{
  2234. csr: &x509.CertificateRequest{
  2235. IPAddresses: []net.IP{net.ParseIP("192.168.43.42"), net.ParseIP("192.168.42.42")},
  2236. },
  2237. },
  2238. want: &x509.CertificateRequest{
  2239. DNSNames: []string{},
  2240. IPAddresses: []net.IP{net.ParseIP("192.168.42.42"), net.ParseIP("192.168.43.42")},
  2241. },
  2242. },
  2243. {
  2244. name: "ok/mixed",
  2245. args: args{
  2246. csr: &x509.CertificateRequest{
  2247. DNSNames: []string{"www.example.com", "example.com"},
  2248. IPAddresses: []net.IP{net.ParseIP("192.168.43.42"), net.ParseIP("192.168.42.42")},
  2249. },
  2250. },
  2251. want: &x509.CertificateRequest{
  2252. DNSNames: []string{"example.com", "www.example.com"},
  2253. IPAddresses: []net.IP{net.ParseIP("192.168.42.42"), net.ParseIP("192.168.43.42")},
  2254. },
  2255. },
  2256. {
  2257. name: "ok/mixed-common-name",
  2258. args: args{
  2259. csr: &x509.CertificateRequest{
  2260. Subject: pkix.Name{
  2261. CommonName: "example.com",
  2262. },
  2263. DNSNames: []string{"www.example.com"},
  2264. IPAddresses: []net.IP{net.ParseIP("192.168.43.42"), net.ParseIP("192.168.42.42")},
  2265. },
  2266. },
  2267. want: &x509.CertificateRequest{
  2268. Subject: pkix.Name{
  2269. CommonName: "example.com",
  2270. },
  2271. DNSNames: []string{"example.com", "www.example.com"},
  2272. IPAddresses: []net.IP{net.ParseIP("192.168.42.42"), net.ParseIP("192.168.43.42")},
  2273. },
  2274. },
  2275. {
  2276. name: "ok/ip-common-name",
  2277. args: args{
  2278. csr: &x509.CertificateRequest{
  2279. Subject: pkix.Name{
  2280. CommonName: "127.0.0.1",
  2281. },
  2282. DNSNames: []string{"example.com"},
  2283. IPAddresses: []net.IP{net.ParseIP("192.168.43.42"), net.ParseIP("192.168.42.42")},
  2284. },
  2285. },
  2286. want: &x509.CertificateRequest{
  2287. Subject: pkix.Name{
  2288. CommonName: "127.0.0.1",
  2289. },
  2290. DNSNames: []string{"example.com"},
  2291. IPAddresses: []net.IP{net.ParseIP("127.0.0.1"), net.ParseIP("192.168.42.42"), net.ParseIP("192.168.43.42")},
  2292. },
  2293. },
  2294. }
  2295. for _, tt := range tests {
  2296. t.Run(tt.name, func(t *testing.T) {
  2297. got := canonicalize(tt.args.csr)
  2298. if !cmp.Equal(tt.want, got) {
  2299. t.Errorf("canonicalize() diff =\n%s", cmp.Diff(tt.want, got))
  2300. }
  2301. })
  2302. }
  2303. }
  2304. func TestOrder_sans(t *testing.T) {
  2305. type fields struct {
  2306. Identifiers []Identifier
  2307. }
  2308. tests := []struct {
  2309. name string
  2310. fields fields
  2311. csr *x509.CertificateRequest
  2312. want []x509util.SubjectAlternativeName
  2313. err *Error
  2314. }{
  2315. {
  2316. name: "ok/dns",
  2317. fields: fields{
  2318. Identifiers: []Identifier{
  2319. {Type: "dns", Value: "example.com"},
  2320. },
  2321. },
  2322. csr: &x509.CertificateRequest{
  2323. Subject: pkix.Name{
  2324. CommonName: "example.com",
  2325. },
  2326. },
  2327. want: []x509util.SubjectAlternativeName{
  2328. {Type: "dns", Value: "example.com"},
  2329. },
  2330. err: nil,
  2331. },
  2332. {
  2333. name: "fail/invalid-alternative-name-email",
  2334. fields: fields{
  2335. Identifiers: []Identifier{},
  2336. },
  2337. csr: &x509.CertificateRequest{
  2338. Subject: pkix.Name{
  2339. CommonName: "foo.internal",
  2340. },
  2341. EmailAddresses: []string{"test@example.com"},
  2342. },
  2343. want: []x509util.SubjectAlternativeName{},
  2344. err: NewError(ErrorBadCSRType, "Only DNS names and IP addresses are allowed"),
  2345. },
  2346. {
  2347. name: "fail/error-names-length-mismatch",
  2348. fields: fields{
  2349. Identifiers: []Identifier{
  2350. {Type: "dns", Value: "foo.internal"},
  2351. {Type: "dns", Value: "bar.internal"},
  2352. },
  2353. },
  2354. csr: &x509.CertificateRequest{
  2355. Subject: pkix.Name{
  2356. CommonName: "foo.internal",
  2357. },
  2358. },
  2359. want: []x509util.SubjectAlternativeName{},
  2360. err: NewError(ErrorBadCSRType, "CSR names do not match identifiers exactly: "+
  2361. "CSR names = %v, Order names = %v", []string{"foo.internal"}, []string{"bar.internal", "foo.internal"}),
  2362. },
  2363. {
  2364. name: "fail/error-names-mismatch",
  2365. fields: fields{
  2366. Identifiers: []Identifier{
  2367. {Type: "dns", Value: "foo.internal"},
  2368. {Type: "dns", Value: "bar.internal"},
  2369. },
  2370. },
  2371. csr: &x509.CertificateRequest{
  2372. Subject: pkix.Name{
  2373. CommonName: "foo.internal",
  2374. },
  2375. DNSNames: []string{"zap.internal"},
  2376. },
  2377. want: []x509util.SubjectAlternativeName{},
  2378. err: NewError(ErrorBadCSRType, "CSR names do not match identifiers exactly: "+
  2379. "CSR names = %v, Order names = %v", []string{"foo.internal", "zap.internal"}, []string{"bar.internal", "foo.internal"}),
  2380. },
  2381. {
  2382. name: "ok/ipv4",
  2383. fields: fields{
  2384. Identifiers: []Identifier{
  2385. {Type: "ip", Value: "192.168.43.42"},
  2386. {Type: "ip", Value: "192.168.42.42"},
  2387. },
  2388. },
  2389. csr: &x509.CertificateRequest{
  2390. IPAddresses: []net.IP{net.ParseIP("192.168.43.42"), net.ParseIP("192.168.42.42")},
  2391. },
  2392. want: []x509util.SubjectAlternativeName{
  2393. {Type: "ip", Value: "192.168.42.42"},
  2394. {Type: "ip", Value: "192.168.43.42"},
  2395. },
  2396. err: nil,
  2397. },
  2398. {
  2399. name: "ok/ipv6",
  2400. fields: fields{
  2401. Identifiers: []Identifier{
  2402. {Type: "ip", Value: "2001:0db8:85a3::8a2e:0370:7335"},
  2403. {Type: "ip", Value: "2001:0db8:85a3::8a2e:0370:7334"},
  2404. },
  2405. },
  2406. csr: &x509.CertificateRequest{
  2407. IPAddresses: []net.IP{net.ParseIP("2001:0db8:85a3:0000:0000:8a2e:0370:7335"), net.ParseIP("2001:0db8:85a3:0000:0000:8a2e:0370:7334")},
  2408. },
  2409. want: []x509util.SubjectAlternativeName{
  2410. {Type: "ip", Value: "2001:db8:85a3::8a2e:370:7334"},
  2411. {Type: "ip", Value: "2001:db8:85a3::8a2e:370:7335"},
  2412. },
  2413. err: nil,
  2414. },
  2415. {
  2416. name: "fail/error-ips-length-mismatch",
  2417. fields: fields{
  2418. Identifiers: []Identifier{
  2419. {Type: "ip", Value: "192.168.42.42"},
  2420. {Type: "ip", Value: "192.168.43.42"},
  2421. },
  2422. },
  2423. csr: &x509.CertificateRequest{
  2424. IPAddresses: []net.IP{net.ParseIP("192.168.42.42")},
  2425. },
  2426. want: []x509util.SubjectAlternativeName{},
  2427. err: NewError(ErrorBadCSRType, "CSR IPs do not match identifiers exactly: "+
  2428. "CSR IPs = %v, Order IPs = %v", []net.IP{net.ParseIP("192.168.42.42")}, []net.IP{net.ParseIP("192.168.42.42"), net.ParseIP("192.168.43.42")}),
  2429. },
  2430. {
  2431. name: "fail/error-ips-mismatch",
  2432. fields: fields{
  2433. Identifiers: []Identifier{
  2434. {Type: "ip", Value: "192.168.42.42"},
  2435. {Type: "ip", Value: "192.168.43.42"},
  2436. },
  2437. },
  2438. csr: &x509.CertificateRequest{
  2439. IPAddresses: []net.IP{net.ParseIP("192.168.42.42"), net.ParseIP("192.168.42.32")},
  2440. },
  2441. want: []x509util.SubjectAlternativeName{},
  2442. err: NewError(ErrorBadCSRType, "CSR IPs do not match identifiers exactly: "+
  2443. "CSR IPs = %v, Order IPs = %v", []net.IP{net.ParseIP("192.168.42.32"), net.ParseIP("192.168.42.42")}, []net.IP{net.ParseIP("192.168.42.42"), net.ParseIP("192.168.43.42")}),
  2444. },
  2445. {
  2446. name: "ok/mixed",
  2447. fields: fields{
  2448. Identifiers: []Identifier{
  2449. {Type: "dns", Value: "foo.internal"},
  2450. {Type: "dns", Value: "bar.internal"},
  2451. {Type: "ip", Value: "192.168.43.42"},
  2452. {Type: "ip", Value: "192.168.42.42"},
  2453. {Type: "ip", Value: "2001:0db8:85a3:0000:0000:8a2e:0370:7334"},
  2454. },
  2455. },
  2456. csr: &x509.CertificateRequest{
  2457. Subject: pkix.Name{
  2458. CommonName: "bar.internal",
  2459. },
  2460. DNSNames: []string{"foo.internal"},
  2461. IPAddresses: []net.IP{net.ParseIP("192.168.43.42"), net.ParseIP("192.168.42.42"), net.ParseIP("2001:0db8:85a3:0000:0000:8a2e:0370:7334")},
  2462. },
  2463. want: []x509util.SubjectAlternativeName{
  2464. {Type: "dns", Value: "bar.internal"},
  2465. {Type: "dns", Value: "foo.internal"},
  2466. {Type: "ip", Value: "192.168.42.42"},
  2467. {Type: "ip", Value: "192.168.43.42"},
  2468. {Type: "ip", Value: "2001:db8:85a3::8a2e:370:7334"},
  2469. },
  2470. err: nil,
  2471. },
  2472. {
  2473. name: "fail/unsupported-identifier-type",
  2474. fields: fields{
  2475. Identifiers: []Identifier{
  2476. {Type: "ipv4", Value: "192.168.42.42"},
  2477. },
  2478. },
  2479. csr: &x509.CertificateRequest{
  2480. IPAddresses: []net.IP{net.ParseIP("192.168.42.42")},
  2481. },
  2482. want: []x509util.SubjectAlternativeName{},
  2483. err: NewError(ErrorServerInternalType, "unsupported identifier type in order: ipv4"),
  2484. },
  2485. }
  2486. for _, tt := range tests {
  2487. t.Run(tt.name, func(t *testing.T) {
  2488. o := &Order{
  2489. Identifiers: tt.fields.Identifiers,
  2490. }
  2491. canonicalizedCSR := canonicalize(tt.csr)
  2492. got, err := o.sans(canonicalizedCSR)
  2493. if tt.err != nil {
  2494. if err == nil {
  2495. t.Errorf("Order.sans() = %v, want error; got none", got)
  2496. return
  2497. }
  2498. var k *Error
  2499. if errors.As(err, &k) {
  2500. assert.Equals(t, k.Type, tt.err.Type)
  2501. assert.Equals(t, k.Detail, tt.err.Detail)
  2502. assert.Equals(t, k.Status, tt.err.Status)
  2503. assert.Equals(t, k.Err.Error(), tt.err.Err.Error())
  2504. assert.Equals(t, k.Detail, tt.err.Detail)
  2505. } else {
  2506. assert.FatalError(t, errors.New("unexpected error type"))
  2507. }
  2508. return
  2509. }
  2510. if !reflect.DeepEqual(got, tt.want) {
  2511. t.Errorf("Order.sans() = %v, want %v", got, tt.want)
  2512. }
  2513. })
  2514. }
  2515. }
  2516. func TestOrder_getAuthorizationFingerprint(t *testing.T) {
  2517. ctx := context.Background()
  2518. type fields struct {
  2519. AuthorizationIDs []string
  2520. }
  2521. type args struct {
  2522. ctx context.Context
  2523. db DB
  2524. }
  2525. tests := []struct {
  2526. name string
  2527. fields fields
  2528. args args
  2529. want string
  2530. wantErr bool
  2531. }{
  2532. {"ok", fields{[]string{"az1", "az2"}}, args{ctx, &MockDB{
  2533. MockGetAuthorization: func(ctx context.Context, id string) (*Authorization, error) {
  2534. return &Authorization{ID: id, Status: StatusValid}, nil
  2535. },
  2536. }}, "", false},
  2537. {"ok fingerprint", fields{[]string{"az1", "az2"}}, args{ctx, &MockDB{
  2538. MockGetAuthorization: func(ctx context.Context, id string) (*Authorization, error) {
  2539. if id == "az1" {
  2540. return &Authorization{ID: id, Status: StatusValid}, nil
  2541. }
  2542. return &Authorization{ID: id, Fingerprint: "fingerprint", Status: StatusValid}, nil
  2543. },
  2544. }}, "fingerprint", false},
  2545. {"fail", fields{[]string{"az1", "az2"}}, args{ctx, &MockDB{
  2546. MockGetAuthorization: func(ctx context.Context, id string) (*Authorization, error) {
  2547. return nil, errors.New("force")
  2548. },
  2549. }}, "", true},
  2550. }
  2551. for _, tt := range tests {
  2552. t.Run(tt.name, func(t *testing.T) {
  2553. o := &Order{
  2554. AuthorizationIDs: tt.fields.AuthorizationIDs,
  2555. }
  2556. got, err := o.getAuthorizationFingerprint(tt.args.ctx, tt.args.db)
  2557. if (err != nil) != tt.wantErr {
  2558. t.Errorf("Order.getAuthorizationFingerprint() error = %v, wantErr %v", err, tt.wantErr)
  2559. return
  2560. }
  2561. if got != tt.want {
  2562. t.Errorf("Order.getAuthorizationFingerprint() = %v, want %v", got, tt.want)
  2563. }
  2564. })
  2565. }
  2566. }