-
Notifications
You must be signed in to change notification settings - Fork 27
/
piv-certs.c
3585 lines (3023 loc) · 78.1 KB
/
piv-certs.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*
* Copyright 2022 The University of Queensland
* Author: Alex Wilson <alex@uq.edu.au>
*/
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <stdint.h>
#include <string.h>
#include <strings.h>
#include <assert.h>
#include <unistd.h>
#include <stdint.h>
#include <stddef.h>
#include <errno.h>
#include <strings.h>
#include <limits.h>
#include <err.h>
#include <ctype.h>
#include <inttypes.h>
#include "debug.h"
#if defined(__APPLE__)
#include <PCSC/wintypes.h>
#include <PCSC/winscard.h>
#else
#include <wintypes.h>
#include <winscard.h>
#endif
#include <sys/types.h>
#include <sys/errno.h>
#if defined(__sun)
#include <sys/fork.h>
#endif
#include <sys/wait.h>
#include <sys/stat.h>
#include "openssh/config.h"
#include "openssh/sshkey.h"
#include "openssh/sshbuf.h"
#include "openssh/digest.h"
#include "openssh/ssherr.h"
#include "openssh/authfd.h"
#include <openssl/err.h>
#include <openssl/x509.h>
#include <openssl/x509v3.h>
#include <openssl/pem.h>
#include <openssl/evp.h>
#include "utils.h"
#include "tlv.h"
#include "piv.h"
#include "bunyan.h"
#include "utils.h"
#include "pkinit_asn1.h"
#include "piv-ca.h"
#include "piv-internal.h"
enum req_flags {
RQF_CERT = (1<<0),
RQF_CERT_REQ = (1<<1)
};
struct param {
const char *cp_name;
uint cp_flags;
const char *cp_help;
};
struct cert_tpl {
const char *ct_name;
const char *ct_help;
struct param ct_params[16];
errf_t *(*ct_populate)(struct cert_var_scope *, X509 *);
errf_t *(*ct_populate_req)(struct cert_var_scope *, X509_REQ *);
};
static struct cert_var *get_or_define_empty_var(struct cert_var_scope *,
const char *, const char *, uint);
static errf_t *cert_var_eval_into(struct cert_var *, struct sshbuf *);
static struct cert_var *add_undefined_deps(struct cert_var *, struct cert_var *);
static struct cert_var *cert_var_clone(struct cert_var *);
static struct cert_var *find_var(struct cert_var *, const char *);
static errf_t *load_ossl_config(const char *section,
struct cert_var_scope *cs, CONF **out);
errf_t *read_text_file(const char *path, char **out, size_t *outlen);
errf_t *validate_cstring(const char *buf, size_t len, size_t maxlen);
#define PARAM_DN { "dn", RQF_CERT | RQF_CERT_REQ, \
"Distinguished name (e.g. 'cn=foo, o=company, c=AU')" }
#define PARAM_LIFETIME { "lifetime", RQF_CERT, \
"Certificate lifetime in seconds (or use unit suffixes 'h', 'd', 'w', 'y')" }
#define PARAM_AD_UPN { "ad_upn", 0, \
"Active Directory UPN (e.g. 'foobar@domain.com')" }
#define PARAM_KRB5_PN { "krb5_principal", 0, \
"Kerberos V principal name (e.g. 'user/admin@REALM')" }
#define PARAM_AD_SID { "ad_sid", 0, "MS AD SID (see KB5014754). Accepts " \
"string format (S-1-5-...) or base64-encoded " \
"(AQUAAAA...) SIDs." }
static errf_t *populate_user_auth(struct cert_var_scope *, X509 *);
static errf_t *populate_user_key_mgmt(struct cert_var_scope *, X509 *);
static errf_t *populate_user_email(struct cert_var_scope *, X509 *);
static errf_t *populate_computer_auth(struct cert_var_scope *, X509 *);
static errf_t *populate_code_signing(struct cert_var_scope *, X509 *);
static errf_t *populate_ca(struct cert_var_scope *, X509 *);
static errf_t *populate_dn_only(struct cert_var_scope *, X509 *);
static errf_t *rpopulate_user_auth(struct cert_var_scope *, X509_REQ *);
static errf_t *rpopulate_user_key_mgmt(struct cert_var_scope *, X509_REQ *);
static errf_t *rpopulate_user_email(struct cert_var_scope *, X509_REQ *);
static errf_t *rpopulate_computer_auth(struct cert_var_scope *, X509_REQ *);
static errf_t *rpopulate_code_signing(struct cert_var_scope *, X509_REQ *);
static errf_t *rpopulate_ca(struct cert_var_scope *, X509_REQ *);
static errf_t *rpopulate_dn_only(struct cert_var_scope *, X509_REQ *);
struct cert_tpl cert_templates[] = {
{
.ct_name = "user-auth",
.ct_help = "User auth certificate",
.ct_params = {
PARAM_DN,
PARAM_LIFETIME,
PARAM_AD_UPN,
PARAM_KRB5_PN,
PARAM_AD_SID,
{ "is_ad_user", 0, "Set to 'yes' or 'true' to force "
"including the AD Smartcard Logon EKU without "
"ad_upn" },
{ NULL }
},
.ct_populate = populate_user_auth,
.ct_populate_req = rpopulate_user_auth
},
{
.ct_name = "user-key-mgmt",
.ct_help = "User certificate for key management (e.g. "
"deriving symmetric keys). If ad_upn is given, "
"also useable for Microsoft EFS",
.ct_params = {
PARAM_DN,
PARAM_LIFETIME,
PARAM_AD_UPN,
PARAM_AD_SID,
{ NULL }
},
.ct_populate = populate_user_key_mgmt,
.ct_populate_req = rpopulate_user_key_mgmt
},
{
.ct_name = "user-email",
.ct_help = "User certificate for encrypting/signing email "
"using S/MIME",
.ct_params = {
PARAM_DN,
PARAM_LIFETIME,
{ "email", RQF_CERT | RQF_CERT_REQ, "E-mail address" },
{ NULL }
},
.ct_populate = populate_user_email,
.ct_populate_req = rpopulate_user_email
},
{
.ct_name = "computer-auth",
.ct_help = "Computer auth certificate, for a TLS server or "
"if ad_upn or krb5_principal are set, a KDC or MS AD DC",
.ct_params = {
PARAM_DN,
PARAM_LIFETIME,
{ "dns_name", RQF_CERT | RQF_CERT_REQ, "DNS domain name" },
PARAM_AD_UPN,
PARAM_KRB5_PN,
{ "is_ad_dc", 0, "Generate a KDC cert for an AD DC if 'yes'" },
{ "ad_repl_guid", 0, "Hex GUID for AD email replication cert" },
PARAM_AD_SID,
{ "is_ike_server", 0, "Add IKE EKU for VPN servers if 'yes'" },
{ NULL }
},
.ct_populate = populate_computer_auth,
.ct_populate_req = rpopulate_computer_auth
},
{
.ct_name = "code-signing",
.ct_help = "Code signing certificate",
.ct_params = {
PARAM_DN,
PARAM_LIFETIME,
PARAM_AD_UPN,
PARAM_KRB5_PN,
{ "email", 0, "E-mail address" },
{ NULL }
},
.ct_populate = populate_code_signing,
.ct_populate_req = rpopulate_code_signing
},
{
.ct_name = "ca",
.ct_help = "Certification authority",
.ct_params = {
PARAM_DN,
PARAM_LIFETIME,
{ "ext_key_usage", 0, "Extended key usage constraint" },
{ "path_len", 0, "Maximum CA path length" },
{ "name_constraints", 0,
"Name constraints (OpenSSL config expression)" },
{ "openssl_config_file", 0, "Path to openssl.cnf" },
{ NULL }
},
.ct_populate = populate_ca,
.ct_populate_req = rpopulate_ca
},
{
.ct_name = "dn-only",
.ct_help = "Sets DN, expiry and no extensions or other fields",
.ct_params = {
PARAM_DN,
PARAM_LIFETIME,
{ NULL }
},
.ct_populate = populate_dn_only,
.ct_populate_req = rpopulate_dn_only
},
{
.ct_name = NULL
}
};
const struct cert_tpl *
cert_tpl_find(const char *name)
{
const struct cert_tpl *tpl = cert_templates;
for (; tpl->ct_name != NULL; ++tpl) {
if (strcmp(tpl->ct_name, name) == 0)
return (tpl);
}
return (NULL);
}
const struct cert_tpl *
cert_tpl_first(void)
{
return (&cert_templates[0]);
}
const struct cert_tpl *
cert_tpl_next(const struct cert_tpl *tpl)
{
++tpl;
if (tpl->ct_name == NULL)
return (NULL);
return (tpl);
}
const char *
cert_tpl_name(const struct cert_tpl *tpl)
{
return (tpl->ct_name);
}
const char *
cert_tpl_help(const struct cert_tpl *tpl)
{
return (tpl->ct_help);
}
struct cert_var *
cert_var_next(struct cert_var *cv)
{
return (cv->cv_next);
}
const char *
cert_var_name(const struct cert_var *cv)
{
return (cv->cv_name);
}
const char *
cert_var_help(const struct cert_var *cv)
{
return (cv->cv_help);
}
char *
cert_var_raw_value(const struct cert_var *cv)
{
if (cv->cv_value == NULL)
return (NULL);
return (varval_unparse(cv->cv_value));
}
char *
cert_var_value(const struct cert_var *cv)
{
while (cv->cv_value == NULL && cv->cv_parent != NULL)
cv = cv->cv_parent;
if (cv->cv_value == NULL)
return (NULL);
return (varval_unparse(cv->cv_value));
}
int
cert_var_required(const struct cert_var *cv, enum requirement_type rt)
{
switch (rt) {
case REQUIRED_FOR_CERT:
return ((cv->cv_flags & RQF_CERT) != 0);
case REQUIRED_FOR_CERT_REQUEST:
return ((cv->cv_flags & RQF_CERT_REQ) != 0);
default:
return (0);
}
}
void
cert_var_set_required(struct cert_var *cv0, enum requirement_type rt)
{
uint nflags = cv0->cv_flags;
struct cert_var *cv;
struct varval *vv;
VERIFY(cv0->cv_scope != NULL);
switch (rt) {
case REQUIRED_FOR_CERT:
nflags |= RQF_CERT;
break;
case REQUIRED_FOR_CERT_REQUEST:
nflags |= RQF_CERT_REQ;
break;
default:
return;
}
if (cv0->cv_flags == nflags)
return;
for (cv = cv0; cv != NULL; cv = cv->cv_parent)
cv->cv_flags |= nflags;
cv = cv0;
while (cv->cv_value == NULL && cv->cv_parent != NULL)
cv = cv->cv_parent;
for (vv = cv->cv_value; vv != NULL; vv = vv->vv_next) {
if (vv->vv_type == VV_VAR) {
cert_var_set_required(vv->vv_var, rt);
}
}
}
void *
cert_var_alloc_private(struct cert_var *cv, size_t sz)
{
VERIFY(cv->cv_priv == NULL);
cv->cv_priv = calloc(1, sz);
return (cv->cv_priv);
}
void *
cert_var_private(struct cert_var *cv)
{
return (cv->cv_priv);
}
void
cert_var_free_private(struct cert_var *cv)
{
free(cv->cv_priv);
cv->cv_priv = NULL;
}
static void
cert_var_free(struct cert_var *cv)
{
VERIFY(cv->cv_priv == NULL);
free(cv->cv_name);
free(cv->cv_help);
varval_free(cv->cv_value);
free(cv);
}
static void
cert_var_free_chain(struct cert_var *cv)
{
struct cert_var *next;
for (; cv != NULL; cv = next) {
next = cv->cv_next;
cert_var_free(cv);
}
}
void
cert_var_free_all(struct cert_var *cv)
{
VERIFY(cv->cv_scope == NULL);
cert_var_free_chain(cv);
}
struct cert_var *
cert_tpl_vars(const struct cert_tpl *tpl)
{
struct cert_var *cv, *cv0 = NULL, *cvlast = NULL;
const struct param *p;
for (p = &tpl->ct_params[0]; p->cp_name != NULL; ++p) {
cv = calloc(1, sizeof(struct cert_var));
cv->cv_name = strdup(p->cp_name);
if (p->cp_help != NULL)
cv->cv_help = strdup(p->cp_help);
cv->cv_flags = p->cp_flags;
if (cvlast != NULL)
cvlast->cv_next = cv;
cvlast = cv;
if (cv0 == NULL)
cv0 = cv;
}
return (cv0);
}
struct cert_var_scope *
scope_parent(struct cert_var_scope *scope)
{
return (scope->cvs_parent);
}
struct cert_var_scope *
scope_new_root(void)
{
return (scope_new_empty(NULL));
}
struct cert_var_scope *
scope_new_empty(struct cert_var_scope *parent)
{
struct cert_var_scope *cvs;
cvs = calloc(1, sizeof (struct cert_var_scope));
VERIFY(cvs != NULL);
cvs->cvs_parent = parent;
if (parent != NULL) {
cvs->cvs_next = parent->cvs_children;
parent->cvs_children = cvs;
}
return (cvs);
}
struct cert_var_scope *
scope_new_for_tpl(struct cert_var_scope *parent, const struct cert_tpl *tpl)
{
struct cert_var_scope *cvs;
const struct param *p;
cvs = scope_new_empty(parent);
for (p = &tpl->ct_params[0]; p->cp_name != NULL; ++p) {
(void) get_or_define_empty_var(cvs, p->cp_name, p->cp_help,
p->cp_flags);
}
return (cvs);
}
struct cert_var *
scope_lookup(struct cert_var_scope *cvs, const char *name, int undef)
{
struct cert_var *cv;
VERIFY(cvs != NULL);
VERIFY(name != NULL);
VERIFY(undef == 1 || undef == 0);
cv = get_or_define_empty_var(cvs, name, NULL, 0);
if (undef)
return (cv);
if (cv->cv_value == NULL)
return (NULL);
return (cv);
}
void
cert_var_set_help(struct cert_var *cv, const char *help)
{
VERIFY(cv != NULL);
VERIFY(cv->cv_scope != NULL);
free(cv->cv_help);
cv->cv_help = strdup(help);
}
errf_t *
cert_var_set(struct cert_var *var, const char *value)
{
struct varval *vv;
struct cert_var *cv;
VERIFY(var != NULL);
VERIFY(var->cv_scope != NULL);
vv = varval_parse(value);
if (vv == NULL) {
return (errf("SyntaxError", NULL, "Failed to parse variable "
"value for %s: '%s'", var->cv_name, value));
}
varval_free(var->cv_value);
var->cv_value = vv;
for (vv = var->cv_value; vv != NULL; vv = vv->vv_next) {
if (vv->vv_type == VV_VAR) {
cv = get_or_define_empty_var(var->cv_scope,
vv->vv_string, NULL, var->cv_flags);
free(vv->vv_string);
vv->vv_var = cv;
}
}
return (ERRF_OK);
}
errf_t *
scope_set(struct cert_var_scope *cvs, const char *name, const char *value)
{
struct cert_var *cv, *var = NULL;
struct varval *vv;
VERIFY(cvs != NULL);
vv = varval_parse(value);
if (vv == NULL) {
return (errf("SyntaxError", NULL, "Failed to parse variable "
"value for %s: '%s'", name, value));
}
var = get_or_define_empty_var(cvs, name, NULL, 0);
varval_free(var->cv_value);
var->cv_value = vv;
for (vv = var->cv_value; vv != NULL; vv = vv->vv_next) {
if (vv->vv_type == VV_VAR) {
cv = get_or_define_empty_var(cvs, vv->vv_string,
NULL, var->cv_flags);
free(vv->vv_string);
vv->vv_var = cv;
}
}
return (ERRF_OK);
}
errf_t *
scope_eval(struct cert_var_scope *cvs, const char *name, char **out)
{
struct cert_var *cv;
cv = scope_lookup(cvs, name, 1);
return (cert_var_eval(cv, out));
}
void
scope_free_root(struct cert_var_scope *rcvs)
{
struct cert_var_scope *cvs, *ncvs;
if (rcvs == NULL)
return;
VERIFY(rcvs->cvs_parent == NULL);
cert_var_free_chain(rcvs->cvs_vars);
for (cvs = rcvs->cvs_children; cvs != NULL; cvs = ncvs) {
ncvs = cvs->cvs_next;
cert_var_free_chain(cvs->cvs_vars);
free(cvs);
}
free(rcvs);
}
errf_t *
cert_var_eval(struct cert_var *var, char **out)
{
struct sshbuf *buf;
errf_t *err;
VERIFY(var != NULL);
VERIFY(var->cv_scope != NULL);
VERIFY(out != NULL);
buf = sshbuf_new();
VERIFY(buf != NULL);
err = cert_var_eval_into(var, buf);
if (err != ERRF_OK) {
sshbuf_free(buf);
return (err);
}
*out = sshbuf_dup_string(buf);
sshbuf_free(buf);
return (ERRF_OK);
}
errf_t *
cert_tpl_populate(const struct cert_tpl *tpl, struct cert_var_scope *cvs,
X509 *cert)
{
return (tpl->ct_populate(cvs, cert));
}
errf_t *
cert_tpl_populate_req(const struct cert_tpl *tpl, struct cert_var_scope *cvs,
X509_REQ *req)
{
return (tpl->ct_populate_req(cvs, req));
}
struct cert_var *
scope_all_vars(struct cert_var_scope *cvs)
{
return (cvs->cvs_vars);
}
struct cert_var *
scope_undef_vars(struct cert_var_scope *cvs)
{
struct cert_var *cv, *rcv = NULL;
for (cv = cvs->cvs_vars; cv != NULL; cv = cv->cv_next)
rcv = add_undefined_deps(cv, rcv);
return (rcv);
}
static struct cert_var *
cert_var_clone(struct cert_var *cv)
{
struct cert_var *rcv;
rcv = calloc(1, sizeof (struct cert_var));
VERIFY(rcv != NULL);
rcv->cv_name = strdup(cv->cv_name);
if (cv->cv_help != NULL)
rcv->cv_help = strdup(cv->cv_help);
rcv->cv_flags = cv->cv_flags;
return (rcv);
}
static struct cert_var *
add_undefined_deps(struct cert_var *cv, struct cert_var *rcv0)
{
struct varval *vv;
struct cert_var *rcv;
while (cv->cv_value == NULL && cv->cv_parent != NULL)
cv = cv->cv_parent;
if (cv->cv_value == NULL) {
rcv = find_var(rcv0, cv->cv_name);
if (rcv == NULL) {
rcv = cert_var_clone(cv);
rcv->cv_next = rcv0;
rcv0 = rcv;
}
return (rcv0);
}
for (vv = cv->cv_value; vv != NULL; vv = vv->vv_next) {
if (vv->vv_type == VV_VAR) {
rcv0 = add_undefined_deps(vv->vv_var, rcv0);
}
}
return (rcv0);
}
static errf_t *
add_dn_component(const char *attr, const char *val, X509_NAME *name)
{
int rc, nid;
errf_t *err;
char *upattr;
uint i;
nid = OBJ_txt2nid(attr);
if (nid == NID_undef) {
upattr = calloc(strlen(attr) + 1, 1);
for (i = 0; attr[i] != '\0'; ++i)
upattr[i] = toupper(attr[i]);
nid = OBJ_txt2nid(upattr);
free(upattr);
}
if (nid == NID_undef) {
return (errf("UnknownAttributeError", NULL, "Unknown or "
"invalid attribute in DN: '%s'", attr));
}
rc = X509_NAME_add_entry_by_NID(name, nid, MBSTRING_ASC,
(unsigned char *)val, -1, 0, 0);
if (rc != 1) {
/*
* If we failed to add it using MBSTRING_ASC, it might be
* because it's a legacy name component that breaks some rules
* (like country attributes longer than 3 chars). We'll try to
* add these without validation by giving the type argument
* as a specific type.
*/
int type;
switch (nid) {
case NID_domainComponent:
case NID_pkcs9_emailAddress:
type = V_ASN1_IA5STRING;
break;
default:
type = V_ASN1_PRINTABLESTRING;
}
rc = X509_NAME_add_entry_by_NID(name, nid, type,
(unsigned char *)val, -1, 0, 0);
}
if (rc != 1) {
make_sslerrf(err, "X509_NAME_add_entry_by_NID", "adding DN "
"attribute '%s'", attr);
return (err);
}
return (ERRF_OK);
}
static char
from_hex_char(char v)
{
if (v >= 'a' && v <= 'f')
return (0xa + (v - 'a'));
if (v >= 'A' && v <= 'F')
return (0xa + (v - 'A'));
if (v >= '0' && v <= '9')
return (v - '0');
return (0);
}
errf_t *
unparse_dn(X509_NAME *name, char **out)
{
struct sshbuf *buf;
uint i, j, max;
char nmbuf[128];
int rc;
const unsigned char *p;
errf_t *err;
buf = sshbuf_new();
if (buf == NULL)
return (errfno("sshbuf_new", errno, NULL));
max = X509_NAME_entry_count(name);
for (i = max - 1; i < max; --i) {
X509_NAME_ENTRY *ent = X509_NAME_get_entry(name, i);
ASN1_OBJECT *obj = X509_NAME_ENTRY_get_object(ent);
ASN1_STRING *val = X509_NAME_ENTRY_get_data(ent);
int nid;
const char *name = NULL;
nid = OBJ_obj2nid(obj);
if (nid != NID_undef) {
name = OBJ_nid2sn(nid);
if (name == NULL)
name = OBJ_nid2ln(nid);
if (name != NULL)
strlcpy(nmbuf, name, sizeof (nmbuf));
}
if (name == NULL) {
rc = OBJ_obj2txt(nmbuf, sizeof (nmbuf), obj, 0);
if (rc == -1) {
make_sslerrf(err, "OBJ_obj2txt", "Failed to convert "
"DN entry %u", i);
sshbuf_free(buf);
return (err);
}
}
VERIFY0(sshbuf_put(buf, nmbuf, strlen(nmbuf)));
VERIFY0(sshbuf_put_u8(buf, '='));
p = ASN1_STRING_get0_data(val);
for (j = 0; j < ASN1_STRING_length(val); ++j) {
char c = p[j];
switch (c) {
case ',':
case '=':
VERIFY0(sshbuf_put_u8(buf, '\\'));
break;
case '\t':
VERIFY0(sshbuf_put_u8(buf, '\\'));
c = 't';
break;
case '\n':
VERIFY0(sshbuf_put_u8(buf, '\\'));
c = 'n';
break;
}
VERIFY0(sshbuf_put_u8(buf, c));
}
if (i != 0) {
VERIFY0(sshbuf_put_u8(buf, ','));
VERIFY0(sshbuf_put_u8(buf, ' '));
}
}
*out = sshbuf_dup_string(buf);
sshbuf_free(buf);
return (ERRF_OK);
}
errf_t *
parse_dn(const char *dnstr, X509_NAME *name)
{
const char *p, *lp, *pp;
struct sshbuf *buf;
char *attr = NULL, *val = NULL;
char v;
errf_t *err;
buf = sshbuf_new();
if (buf == NULL)
return (errfno("sshbuf_new", errno, NULL));
for (lp = (p = dnstr); *p != '\0'; ++p) {
switch (*p) {
case '\\':
VERIFY0(sshbuf_put(buf, lp, (p - lp)));
v = *(++p);
if (v == 'n') {
v = '\n';
} else if ((v >= 'a' && v <= 'f') ||
(v >= 'A' && v <= 'F') ||
(v >= '0' && v <= '9')) {
v = from_hex_char(v);
v <<= 4;
v |= from_hex_char(*(++p));
if (*p == '\0') {
sshbuf_free(buf);
return (errf("SyntaxError", NULL,
"Reached end of string while "
"processing hex escape in DN near "
"'%s'", lp));
}
}
VERIFY0(sshbuf_put_u8(buf, v));
lp = ++p;
break;
case '=':
if (attr != NULL) {
sshbuf_free(buf);
return (errf("SyntaxError", NULL, "Unexpected "
"EQUALS in DN at '%s'", p));
}
/* Trim any trailing whitespace from the attr */
pp = p;
while (*(pp - 1) == ' ' || *(pp - 1) == '\t')
--pp;
VERIFY0(sshbuf_put(buf, lp, (pp - lp)));
lp = ++p;
/* Skip any leading whitespace after = */
while (*p == ' ' || *p == '\t')
lp = ++p;
attr = sshbuf_dup_string(buf);
sshbuf_reset(buf);
break;
case ',':
if (attr == NULL) {
sshbuf_free(buf);
return (errf("SyntaxError", NULL, "Unexpected "
"COMMA in DN at '%s'", p));
}
/* Trim any trailing whitespace from the value */
pp = p;
while (*(pp - 1) == ' ' || *(pp - 1) == '\t')
--pp;
VERIFY0(sshbuf_put(buf, lp, (pp - lp)));
lp = ++p;
/* Skip any leading whitespace after = */
while (*p == ' ' || *p == '\t')
lp = ++p;
val = sshbuf_dup_string(buf);
sshbuf_reset(buf);
err = add_dn_component(attr, val, name);
if (err != ERRF_OK) {
sshbuf_free(buf);
return (err);
}
free(attr);
free(val);
attr = NULL;
val = NULL;
break;
}
}
if (lp != p) {
if (attr == NULL) {
sshbuf_free(buf);
return (errf("SyntaxError", NULL, "Reached end of "
"string, expected attribute value in DN '%s'",
dnstr));
}
/* Trim any trailing whitespace from the value */
pp = p;
while (*(pp - 1) == ' ' || *(pp - 1) == '\t')
--pp;
VERIFY0(sshbuf_put(buf, lp, (pp - lp)));
lp = ++p;
val = sshbuf_dup_string(buf);
sshbuf_reset(buf);
err = add_dn_component(attr, val, name);
if (err != ERRF_OK)
return (err);
free(attr);
free(val);
}
sshbuf_free(buf);
return (ERRF_OK);
}
void
varval_free(struct varval *vv)
{
struct varval *next;
for (; vv != NULL; vv = next) {
next = vv->vv_next;
if (vv->vv_type == VV_STRING)
free(vv->vv_string);
free(vv);
}
}
char *
varval_unparse(const struct varval *vv0)
{
struct sshbuf *buf;
const struct varval *vv;
char *ret;
buf = sshbuf_new();
if (buf == NULL) {
return (NULL);
}
for (vv = vv0; vv != NULL; vv = vv->vv_next) {
if (vv->vv_type == VV_STRING) {
VERIFY0(sshbuf_put(buf, vv->vv_string,
strlen(vv->vv_string)));
} else if (vv->vv_type == VV_VAR) {
VERIFY0(sshbuf_put_u8(buf, '%'));
VERIFY0(sshbuf_put_u8(buf, '{'));
VERIFY0(sshbuf_put(buf, vv->vv_var->cv_name,
strlen(vv->vv_var->cv_name)));
VERIFY0(sshbuf_put_u8(buf, '}'));
}
}
ret = sshbuf_dup_string(buf);
sshbuf_free(buf);
return (ret);
}
struct varval *
varval_parse(const char *inp)
{
const char *p, *lp;
struct sshbuf *buf;
struct varval *vv0, *vv, *vvn;
vv0 = (vv = calloc(1, sizeof (struct varval)));
if (vv == NULL)
return (NULL);
vv->vv_type = VV_STRING;
buf = sshbuf_new();
if (buf == NULL) {
free(vv);
return (NULL);
}