@@ -109,6 +109,8 @@ errf_t *validate_cstring(const char *buf, size_t len, size_t maxlen);
109
109
#define PARAM_AD_SID { "ad_sid", 0, "MS AD SID (see KB5014754). Accepts " \
110
110
"string format (S-1-5-...) or base64-encoded " \
111
111
"(AQUAAAA...) SIDs." }
112
+ #define PARAM_POLICIES { "cert_policies", 0, \
113
+ "Certificate policies to apply (as an OpenSSL config expression)" }
112
114
113
115
static errf_t * populate_user_auth (struct cert_var_scope * , X509 * );
114
116
static errf_t * populate_user_key_mgmt (struct cert_var_scope * , X509 * );
@@ -133,6 +135,7 @@ struct cert_tpl cert_templates[] = {
133
135
.ct_params = {
134
136
PARAM_DN ,
135
137
PARAM_LIFETIME ,
138
+ PARAM_POLICIES ,
136
139
PARAM_AD_UPN ,
137
140
PARAM_KRB5_PN ,
138
141
PARAM_AD_SID ,
@@ -152,6 +155,7 @@ struct cert_tpl cert_templates[] = {
152
155
.ct_params = {
153
156
PARAM_DN ,
154
157
PARAM_LIFETIME ,
158
+ PARAM_POLICIES ,
155
159
PARAM_AD_UPN ,
156
160
PARAM_AD_SID ,
157
161
{ NULL }
@@ -166,6 +170,7 @@ struct cert_tpl cert_templates[] = {
166
170
.ct_params = {
167
171
PARAM_DN ,
168
172
PARAM_LIFETIME ,
173
+ PARAM_POLICIES ,
169
174
{ "email" , RQF_CERT | RQF_CERT_REQ , "E-mail address" },
170
175
{ NULL }
171
176
},
@@ -179,6 +184,7 @@ struct cert_tpl cert_templates[] = {
179
184
.ct_params = {
180
185
PARAM_DN ,
181
186
PARAM_LIFETIME ,
187
+ PARAM_POLICIES ,
182
188
{ "dns_name" , RQF_CERT | RQF_CERT_REQ , "DNS domain name" },
183
189
PARAM_AD_UPN ,
184
190
PARAM_KRB5_PN ,
@@ -197,6 +203,7 @@ struct cert_tpl cert_templates[] = {
197
203
.ct_params = {
198
204
PARAM_DN ,
199
205
PARAM_LIFETIME ,
206
+ PARAM_POLICIES ,
200
207
PARAM_AD_UPN ,
201
208
PARAM_KRB5_PN ,
202
209
{ "email" , 0 , "E-mail address" },
@@ -211,6 +218,7 @@ struct cert_tpl cert_templates[] = {
211
218
.ct_params = {
212
219
PARAM_DN ,
213
220
PARAM_LIFETIME ,
221
+ PARAM_POLICIES ,
214
222
{ "ext_key_usage" , 0 , "Extended key usage constraint" },
215
223
{ "path_len" , 0 , "Maximum CA path length" },
216
224
{ "name_constraints" , 0 ,
@@ -1219,11 +1227,12 @@ populate_common(struct cert_var_scope *cs, X509 *cert, char *basic, char *ku,
1219
1227
char * eku )
1220
1228
{
1221
1229
errf_t * err ;
1222
- char * lifetime , * dnstr ;
1230
+ char * lifetime , * dnstr , * policies = NULL ;
1223
1231
unsigned long lifetime_secs ;
1224
1232
X509_EXTENSION * ext ;
1225
1233
X509V3_CTX x509ctx ;
1226
1234
X509_NAME * subj ;
1235
+ CONF * config = NULL ;
1227
1236
1228
1237
err = scope_eval (cs , "lifetime" , & lifetime );
1229
1238
if (err != ERRF_OK ) {
@@ -1259,31 +1268,67 @@ populate_common(struct cert_var_scope *cs, X509 *cert, char *basic, char *ku,
1259
1268
VERIFY (X509_set_subject_name (cert , subj ) == 1 );
1260
1269
X509_NAME_free (subj );
1261
1270
1262
- X509V3_set_ctx_nodb (& x509ctx );
1271
+ err = scope_eval (cs , "cert_policies" , & policies );
1272
+ if (err == ERRF_OK ) {
1273
+ OPENSSL_load_builtin_modules ();
1274
+
1275
+ err = load_ossl_config ("piv_ca" , cs , & config );
1276
+ if (err != ERRF_OK )
1277
+ return (err );
1278
+
1279
+ X509V3_set_nconf (& x509ctx , config );
1280
+ } else {
1281
+ X509V3_set_ctx_nodb (& x509ctx );
1282
+ }
1263
1283
X509V3_set_ctx (& x509ctx , cert , cert , NULL , NULL , 0 );
1264
1284
1265
1285
if (basic != NULL ) {
1266
1286
ext = X509V3_EXT_conf_nid (NULL , & x509ctx , NID_basic_constraints ,
1267
1287
(char * )basic );
1268
- VERIFY (ext != NULL );
1288
+ if (ext == NULL ) {
1289
+ make_sslerrf (err , "X509V3_EXT_conf_nid" ,
1290
+ "parsing basicConstraints extension" );
1291
+ return (err );
1292
+ }
1269
1293
X509_add_ext (cert , ext , -1 );
1270
1294
X509_EXTENSION_free (ext );
1271
1295
}
1272
1296
1273
1297
if (ku != NULL ) {
1274
1298
ext = X509V3_EXT_conf_nid (NULL , & x509ctx , NID_key_usage ,
1275
1299
(char * )ku );
1276
- VERIFY (ext != NULL );
1300
+ if (ext == NULL ) {
1301
+ make_sslerrf (err , "X509V3_EXT_conf_nid" ,
1302
+ "parsing keyUsage extension" );
1303
+ return (err );
1304
+ }
1277
1305
X509_add_ext (cert , ext , -1 );
1278
1306
X509_EXTENSION_free (ext );
1279
1307
}
1280
1308
1281
1309
if (eku != NULL ) {
1282
1310
ext = X509V3_EXT_conf_nid (NULL , & x509ctx , NID_ext_key_usage ,
1283
1311
(char * )eku );
1284
- VERIFY (ext != NULL );
1312
+ if (ext == NULL ) {
1313
+ make_sslerrf (err , "X509V3_EXT_conf_nid" ,
1314
+ "parsing extKeyUsage extension" );
1315
+ return (err );
1316
+ }
1317
+ X509_add_ext (cert , ext , -1 );
1318
+ X509_EXTENSION_free (ext );
1319
+ }
1320
+
1321
+ if (policies != NULL ) {
1322
+ ext = X509V3_EXT_conf_nid (NULL , & x509ctx ,
1323
+ NID_certificate_policies , (char * )policies );
1324
+ if (ext == NULL ) {
1325
+ make_sslerrf (err , "X509V3_EXT_conf_nid" ,
1326
+ "parsing certificatePolicies extension" );
1327
+ return (err );
1328
+ }
1285
1329
X509_add_ext (cert , ext , -1 );
1286
1330
X509_EXTENSION_free (ext );
1331
+ free (policies );
1287
1332
}
1288
1333
1289
1334
return (ERRF_OK );
@@ -2138,10 +2183,11 @@ rpopulate_common(struct cert_var_scope *cs, X509_REQ *req,
2138
2183
STACK_OF (X509_EXTENSION ) * exts , char * basic , char * ku , char * eku )
2139
2184
{
2140
2185
errf_t * err ;
2141
- char * dnstr ;
2186
+ char * dnstr , * policies = NULL ;
2142
2187
X509_EXTENSION * ext ;
2143
2188
X509V3_CTX x509ctx ;
2144
2189
X509_NAME * subj ;
2190
+ CONF * config = NULL ;
2145
2191
2146
2192
subj = X509_NAME_new ();
2147
2193
VERIFY (subj != NULL );
@@ -2164,28 +2210,63 @@ rpopulate_common(struct cert_var_scope *cs, X509_REQ *req,
2164
2210
VERIFY (X509_REQ_set_subject_name (req , subj ) == 1 );
2165
2211
X509_NAME_free (subj );
2166
2212
2167
- X509V3_set_ctx_nodb (& x509ctx );
2213
+ err = scope_eval (cs , "cert_policies" , & policies );
2214
+ if (err == ERRF_OK ) {
2215
+ OPENSSL_load_builtin_modules ();
2216
+
2217
+ err = load_ossl_config ("piv_ca" , cs , & config );
2218
+ if (err != ERRF_OK )
2219
+ return (err );
2220
+
2221
+ X509V3_set_nconf (& x509ctx , config );
2222
+ } else {
2223
+ X509V3_set_ctx_nodb (& x509ctx );
2224
+ }
2168
2225
X509V3_set_ctx (& x509ctx , NULL , NULL , req , NULL , 0 );
2169
2226
2170
2227
if (basic != NULL ) {
2171
2228
ext = X509V3_EXT_conf_nid (NULL , & x509ctx , NID_basic_constraints ,
2172
2229
(char * )basic );
2173
- VERIFY (ext != NULL );
2230
+ if (ext == NULL ) {
2231
+ make_sslerrf (err , "X509V3_EXT_conf_nid" ,
2232
+ "parsing basicConstraints extension" );
2233
+ return (err );
2234
+ }
2174
2235
VERIFY (sk_X509_EXTENSION_push (exts , ext ) != 0 );
2175
2236
}
2176
2237
2177
2238
if (ku != NULL ) {
2178
2239
ext = X509V3_EXT_conf_nid (NULL , & x509ctx , NID_key_usage ,
2179
2240
(char * )ku );
2180
- VERIFY (ext != NULL );
2241
+ if (ext == NULL ) {
2242
+ make_sslerrf (err , "X509V3_EXT_conf_nid" ,
2243
+ "parsing keyUsage extension" );
2244
+ return (err );
2245
+ }
2181
2246
VERIFY (sk_X509_EXTENSION_push (exts , ext ) != 0 );
2182
2247
}
2183
2248
2184
2249
if (eku != NULL ) {
2185
2250
ext = X509V3_EXT_conf_nid (NULL , & x509ctx , NID_ext_key_usage ,
2186
2251
(char * )eku );
2187
- VERIFY (ext != NULL );
2252
+ if (ext == NULL ) {
2253
+ make_sslerrf (err , "X509V3_EXT_conf_nid" ,
2254
+ "parsing extKeyUsage extension" );
2255
+ return (err );
2256
+ }
2257
+ VERIFY (sk_X509_EXTENSION_push (exts , ext ) != 0 );
2258
+ }
2259
+
2260
+ if (policies != NULL ) {
2261
+ ext = X509V3_EXT_conf_nid (NULL , & x509ctx ,
2262
+ NID_certificate_policies , (char * )policies );
2263
+ if (ext == NULL ) {
2264
+ make_sslerrf (err , "X509V3_EXT_conf_nid" ,
2265
+ "parsing certificatePolicies extension" );
2266
+ return (err );
2267
+ }
2188
2268
VERIFY (sk_X509_EXTENSION_push (exts , ext ) != 0 );
2269
+ free (policies );
2189
2270
}
2190
2271
2191
2272
return (ERRF_OK );
0 commit comments