order_test.go 77 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591
  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 any
  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]any, error) {
  1093. assert.Equals(t, orderID, o.ID)
  1094. dpopMap := map[string]any{
  1095. "dpop": "a-dpop-token",
  1096. }
  1097. return dpopMap, nil
  1098. },
  1099. MockGetOidcToken: func(ctx context.Context, orderID string) (map[string]any, error) {
  1100. assert.Equals(t, orderID, o.ID)
  1101. oidcMap := map[string]any{
  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]any, error) {
  1183. assert.Equals(t, orderID, o.ID)
  1184. dpopMap := map[string]any{
  1185. "dpop": "a-dpop-token",
  1186. }
  1187. return dpopMap, nil
  1188. },
  1189. MockGetOidcToken: func(ctx context.Context, orderID string) (map[string]any, error) {
  1190. assert.Equals(t, orderID, o.ID)
  1191. oidcMap := map[string]any{
  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]any, error) {
  1556. assert.Equals(t, orderID, o.ID)
  1557. dpopMap := map[string]any{
  1558. "dpop": "a-dpop-token",
  1559. }
  1560. return dpopMap, nil
  1561. },
  1562. MockGetOidcToken: func(ctx context.Context, orderID string) (map[string]any, error) {
  1563. assert.Equals(t, orderID, o.ID)
  1564. oidcMap := map[string]any{
  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]any, error) {
  1645. assert.Equals(t, orderID, o.ID)
  1646. dpopMap := map[string]any{
  1647. "dpop": "a-dpop-token",
  1648. }
  1649. return dpopMap, nil
  1650. },
  1651. MockGetOidcToken: func(ctx context.Context, orderID string) (map[string]any, error) {
  1652. assert.Equals(t, orderID, o.ID)
  1653. oidcMap := map[string]any{
  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. }