Debian Codegraph @

servers/slapd/overlays/autoca.c

raw
1 /* autoca.c - Automatic Certificate Authority */
2 /* $OpenLDAP$ */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 *
5 * Copyright 2009-2024 The OpenLDAP Foundation.
6 * Copyright 2009-2018 by Howard Chu.
7 * All rights reserved.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted only as authorized by the OpenLDAP
11 * Public License.
12 *
13 * A copy of this license is available in the file LICENSE in the
14 * top-level directory of the distribution or, alternatively, at
15 * <http://www.OpenLDAP.org/license.html>.
16 */
17 /* ACKNOWLEDGEMENTS:
18 * This work was initially developed by Howard Chu for inclusion in
19 * OpenLDAP Software.
20 */
21
22 #include "portable.h"
23
24 #ifdef SLAPD_OVER_AUTOCA
25
26 #include <stdio.h>
27
28 #include <ac/string.h>
29 #include <ac/socket.h>
30
31 #include "lutil.h"
32 #include "slap.h"
33 #include "slap-config.h"
34
35 #include <openssl/x509.h>
36 #include <openssl/x509v3.h>
37 #include <openssl/evp.h>
38 #include <openssl/bn.h>
39
40 /* Starting with OpenSSL 1.1.0, rsa.h is no longer included in
41 * x509.h, so we need to explicitly include it for the
42 * call to EVP_PKEY_CTX_set_rsa_keygen_bits
43 */
44
45 #if OPENSSL_VERSION_NUMBER >= 0x10100000
46 #include <openssl/rsa.h>
47 #define X509_get_notBefore(x) X509_getm_notBefore(x)
48 #define X509_get_notAfter(x) X509_getm_notAfter(x)
49 #endif
50
51 #if OPENSSL_VERSION_MAJOR >= 3
52 #define BN_pseudo_rand(bn, bits, top, bottom) BN_rand(bn, bits, top, bottom)
53 #endif
54
55 /* This overlay implements a certificate authority that can generate
56 * certificates automatically for any entry in the directory.
57 * On startup it generates a self-signed CA cert for the directory's
58 * suffix entry and uses this to sign all other certs that it generates.
59 * User and server certs are generated on demand, using a Search request.
60 */
61
62 #define LBER_TAG_OID ((ber_tag_t) 0x06UL)
63 #define LBER_TAG_UTF8 ((ber_tag_t) 0x0cUL)
64
65 #define KEYBITS 2048
66 #define MIN_KEYBITS 512
67
68 #define ACA_SCHEMA_ROOT "1.3.6.1.4.1.4203.666.11.11"
69
70 #define ACA_SCHEMA_AT ACA_SCHEMA_ROOT ".1"
71 #define ACA_SCHEMA_OC ACA_SCHEMA_ROOT ".2"
72
73 static AttributeDescription *ad_caCert, *ad_caPkey, *ad_usrCert, *ad_usrPkey;
74 static AttributeDescription *ad_mail, *ad_ipaddr;
75 static ObjectClass *oc_caObj, *oc_usrObj;
76
77 static char *aca_attrs[] = {
78 "( " ACA_SCHEMA_AT ".1 NAME 'cAPrivateKey' "
79 "DESC 'X.509 CA private key, use ;binary' "
80 "SUP pKCS8PrivateKey )",
81 "( " ACA_SCHEMA_AT ".2 NAME 'userPrivateKey' "
82 "DESC 'X.509 user private key, use ;binary' "
83 "SUP pKCS8PrivateKey )",
84 NULL
85 };
86
87 static struct {
88 char *at;
89 AttributeDescription **ad;
90 } aca_attr2[] = {
91 { "cACertificate;binary", &ad_caCert },
92 { "cAPrivateKey;binary", &ad_caPkey },
93 { "userCertificate;binary", &ad_usrCert },
94 { "userPrivateKey;binary", &ad_usrPkey },
95 { "mail", &ad_mail },
96 { NULL }
97 };
98
99 static struct {
100 char *ot;
101 ObjectClass **oc;
102 } aca_ocs[] = {
103 { "( " ACA_SCHEMA_OC ".1 NAME 'autoCA' "
104 "DESC 'Automated PKI certificate authority' "
105 "SUP pkiCA AUXILIARY "
106 "MAY cAPrivateKey )", &oc_caObj },
107 { "( " ACA_SCHEMA_OC ".2 NAME 'autoCAuser' "
108 "DESC 'Automated PKI CA user' "
109 "SUP pkiUser AUXILIARY "
110 "MAY userPrivateKey )", &oc_usrObj },
111 { NULL }
112 };
113
114 typedef struct autoca_info {
115 X509 *ai_cert;
116 EVP_PKEY *ai_pkey;
117 ObjectClass *ai_usrclass;
118 ObjectClass *ai_srvclass;
119 struct berval ai_localdn;
120 struct berval ai_localndn;
121 int ai_usrkeybits;
122 int ai_srvkeybits;
123 int ai_cakeybits;
124 int ai_usrdays;
125 int ai_srvdays;
126 int ai_cadays;
127 } autoca_info;
128
129 /* Rewrite an LDAP DN in DER form
130 * Input must be valid DN, therefore no error checking is done here.
131 */
132 static int autoca_dnbv2der( Operation *op, struct berval *bv, struct berval *der )
133 {
134 BerElementBuffer berbuf;
135 BerElement *ber = (BerElement *)&berbuf;
136 LDAPDN dn;
137 LDAPRDN rdn;
138 LDAPAVA *ava;
139 AttributeDescription *ad;
140 int irdn, iava;
141
142 ldap_bv2dn_x( bv, &dn, LDAP_DN_FORMAT_LDAP, op->o_tmpmemctx );
143
144 ber_init2( ber, NULL, LBER_USE_DER );
145 ber_set_option( ber, LBER_OPT_BER_MEMCTX, &op->o_tmpmemctx );
146
147 /* count RDNs, we need them in reverse order */
148 for (irdn = 0; dn[irdn]; irdn++);
149 irdn--;
150
151 /* DN is a SEQuence of RDNs */
152 ber_start_seq( ber, LBER_SEQUENCE );
153 for (; irdn >=0; irdn--)
154 {
155 /* RDN is a SET of AVAs */
156 ber_start_set( ber, LBER_SET );
157 rdn = dn[irdn];
158 for (iava = 0; rdn[iava]; iava++)
159 {
160 const char *text;
161 char oid[1024];
162 struct berval bvo = { sizeof(oid), oid };
163 struct berval bva;
164
165 /* AVA is a SEQuence of attr and value */
166 ber_start_seq( ber, LBER_SEQUENCE );
167 ava = rdn[iava];
168 ad = NULL;
169 slap_bv2ad( &ava->la_attr, &ad, &text );
170 ber_str2bv( ad->ad_type->sat_oid, 0, 0, &bva );
171 ber_encode_oid( &bva, &bvo );
172 ber_put_berval( ber, &bvo, LBER_TAG_OID );
173 ber_put_berval( ber, &ava->la_value, LBER_TAG_UTF8 );
174 ber_put_seq( ber );
175 }
176 ber_put_set( ber );
177 }
178 ber_put_seq( ber );
179 ber_flatten2( ber, der, 0 );
180 ldap_dnfree_x( dn, op->o_tmpmemctx );
181 return 0;
182 }
183
184 static int autoca_genpkey(int bits, EVP_PKEY **pkey)
185 {
186 EVP_PKEY_CTX *kctx;
187 int rc;
188
189 kctx = EVP_PKEY_CTX_new_id(EVP_PKEY_RSA, NULL);
190 if (kctx == NULL)
191 return -1;
192 if (EVP_PKEY_keygen_init(kctx) <= 0)
193 {
194 EVP_PKEY_CTX_free(kctx);
195 return -1;
196 }
197 if (EVP_PKEY_CTX_set_rsa_keygen_bits(kctx, bits) <= 0)
198 {
199 EVP_PKEY_CTX_free(kctx);
200 return -1;
201 }
202 rc = EVP_PKEY_keygen(kctx, pkey);
203 EVP_PKEY_CTX_free(kctx);
204 return rc;
205 }
206
207 static int autoca_signcert(X509 *cert, EVP_PKEY *pkey)
208 {
209 EVP_MD_CTX *ctx = EVP_MD_CTX_create();
210 EVP_PKEY_CTX *pkctx = NULL;
211 int rc = -1;
212
213 if ( ctx == NULL )
214 return -1;
215 if (EVP_DigestSignInit(ctx, &pkctx, NULL, NULL, pkey))
216 {
217 rc = X509_sign_ctx(cert, ctx);
218 }
219 EVP_MD_CTX_destroy(ctx);
220 return rc;
221 }
222
223 #define SERIAL_BITS 64 /* should be less than 160 */
224
225 typedef struct myext {
226 char *name;
227 char *value;
228 } myext;
229
230 static myext CAexts[] = {
231 { "subjectKeyIdentifier", "hash" },
232 { "authorityKeyIdentifier", "keyid:always,issuer" },
233 { "basicConstraints", "critical,CA:true" },
234 { "keyUsage", "digitalSignature,cRLSign,keyCertSign" },
235 { "nsComment", "OpenLDAP automatic certificate" },
236 { NULL }
237 };
238
239 static myext usrExts[] = {
240 { "subjectKeyIdentifier", "hash" },
241 { "authorityKeyIdentifier", "keyid:always,issuer" },
242 { "basicConstraints", "CA:false" },
243 { "keyUsage", "digitalSignature,nonRepudiation,keyEncipherment" },
244 { "extendedKeyUsage", "clientAuth,emailProtection,codeSigning" },
245 { "nsComment", "OpenLDAP automatic certificate" },
246 { NULL }
247 };
248
249 static myext srvExts[] = {
250 { "subjectKeyIdentifier", "hash" },
251 { "authorityKeyIdentifier", "keyid:always,issuer" },
252 { "basicConstraints", "CA:false" },
253 { "keyUsage", "digitalSignature,keyEncipherment" },
254 { "extendedKeyUsage", "serverAuth,clientAuth" },
255 { "nsComment", "OpenLDAP automatic certificate" },
256 { NULL }
257 };
258
259 typedef struct genargs {
260 X509 *issuer_cert;
261 EVP_PKEY *issuer_pkey;
262 struct berval *subjectDN;
263 myext *cert_exts;
264 myext *more_exts;
265 X509 *newcert;
266 EVP_PKEY *newpkey;
267 struct berval dercert;
268 struct berval derpkey;
269 int keybits;
270 int days;
271 } genargs;
272
273 static int autoca_gencert( Operation *op, genargs *args )
274 {
275 X509_NAME *subj_name, *issuer_name;
276 X509 *subj_cert;
277 struct berval derdn;
278 unsigned char *pp;
279 EVP_PKEY *evpk = NULL;
280 int rc;
281
282 if ((subj_cert = X509_new()) == NULL)
283 return -1;
284
285 autoca_dnbv2der( op, args->subjectDN, &derdn );
286 pp = (unsigned char *)derdn.bv_val;
287 subj_name = d2i_X509_NAME( NULL, (const unsigned char **)&pp, derdn.bv_len );
288 op->o_tmpfree( derdn.bv_val, op->o_tmpmemctx );
289 if ( subj_name == NULL )
290 {
291 fail1:
292 X509_free( subj_cert );
293 return -1;
294 }
295
296 rc = autoca_genpkey( args->keybits, &evpk );
297 if ( rc <= 0 )
298 {
299 fail2:
300 if ( subj_name ) X509_NAME_free( subj_name );
301 goto fail1;
302 }
303 /* encode DER in PKCS#8 */
304 {
305 PKCS8_PRIV_KEY_INFO *p8inf;
306 if (( p8inf = EVP_PKEY2PKCS8( evpk )) == NULL )
307 goto fail2;
308 args->derpkey.bv_len = i2d_PKCS8_PRIV_KEY_INFO( p8inf, NULL );
309 args->derpkey.bv_val = op->o_tmpalloc( args->derpkey.bv_len, op->o_tmpmemctx );
310 pp = (unsigned char *)args->derpkey.bv_val;
311 i2d_PKCS8_PRIV_KEY_INFO( p8inf, &pp );
312 PKCS8_PRIV_KEY_INFO_free( p8inf );
313 }
314 args->newpkey = evpk;
315
316 /* set random serial */
317 {
318 BIGNUM *bn = BN_new();
319 if ( bn == NULL )
320 {
321 fail3:
322 EVP_PKEY_free( evpk );
323 goto fail2;
324 }
325 if (!BN_pseudo_rand(bn, SERIAL_BITS, 0, 0))
326 {
327 BN_free( bn );
328 goto fail3;
329 }
330 if (!BN_to_ASN1_INTEGER(bn, X509_get_serialNumber(subj_cert)))
331 {
332 BN_free( bn );
333 goto fail3;
334 }
335 BN_free(bn);
336 }
337 if (args->issuer_cert) {
338 issuer_name = X509_get_subject_name(args->issuer_cert);
339 } else {
340 issuer_name = subj_name;
341 args->issuer_cert = subj_cert;
342 args->issuer_pkey = evpk;
343 }
344 if (!X509_set_version(subj_cert, 2) || /* set version to V3 */
345 !X509_set_issuer_name(subj_cert, issuer_name) ||
346 !X509_set_subject_name(subj_cert, subj_name) ||
347 !X509_gmtime_adj(X509_get_notBefore(subj_cert), 0) ||
348 !X509_time_adj_ex(X509_get_notAfter(subj_cert), args->days, 0, NULL) ||
349 !X509_set_pubkey(subj_cert, evpk))
350 {
351 goto fail3;
352 }
353 X509_NAME_free(subj_name);
354 subj_name = NULL;
355
356 /* set cert extensions */
357 {
358 X509V3_CTX ctx;
359 X509_EXTENSION *ext;
360 int i;
361
362 X509V3_set_ctx(&ctx, args->issuer_cert, subj_cert, NULL, NULL, 0);
363 for (i=0; args->cert_exts[i].name; i++) {
364 ext = X509V3_EXT_nconf(NULL, &ctx, args->cert_exts[i].name, args->cert_exts[i].value);
365 if ( ext == NULL )
366 goto fail3;
367 rc = X509_add_ext(subj_cert, ext, -1);
368 X509_EXTENSION_free(ext);
369 if ( !rc )
370 goto fail3;
371 }
372 if (args->more_exts) {
373 for (i=0; args->more_exts[i].name; i++) {
374 ext = X509V3_EXT_nconf(NULL, &ctx, args->more_exts[i].name, args->more_exts[i].value);
375 if ( ext == NULL )
376 goto fail3;
377 rc = X509_add_ext(subj_cert, ext, -1);
378 X509_EXTENSION_free(ext);
379 if ( !rc )
380 goto fail3;
381 }
382 }
383 }
384 rc = autoca_signcert( subj_cert, args->issuer_pkey );
385 if ( rc < 0 )
386 goto fail3;
387 args->dercert.bv_len = i2d_X509( subj_cert, NULL );
388 args->dercert.bv_val = op->o_tmpalloc( args->dercert.bv_len, op->o_tmpmemctx );
389 pp = (unsigned char *)args->dercert.bv_val;
390 i2d_X509( subj_cert, &pp );
391 args->newcert = subj_cert;
392 return 0;
393 }
394
395 typedef struct saveargs {
396 ObjectClass *oc;
397 struct berval *dercert;
398 struct berval *derpkey;
399 slap_overinst *on;
400 struct berval *dn;
401 struct berval *ndn;
402 int isca;
403 } saveargs;
404
405 static int autoca_savecert( Operation *op, saveargs *args )
406 {
407 Modifications mod[3], *mp = mod;
408 struct berval bvs[6], *bp = bvs;
409 BackendInfo *bi;
410 slap_callback cb = {0};
411 SlapReply rs = {REP_RESULT};
412
413 if ( args->oc ) {
414 mp->sml_numvals = 1;
415 mp->sml_values = bp;
416 mp->sml_nvalues = NULL;
417 mp->sml_desc = slap_schema.si_ad_objectClass;
418 mp->sml_op = LDAP_MOD_ADD;
419 mp->sml_flags = SLAP_MOD_INTERNAL;
420 *bp++ = args->oc->soc_cname;
421 BER_BVZERO( bp );
422 bp++;
423 mp->sml_next = mp+1;
424 mp++;
425 }
426 mp->sml_numvals = 1;
427 mp->sml_values = bp;
428 mp->sml_nvalues = NULL;
429 mp->sml_desc = args->isca ? ad_caCert : ad_usrCert;
430 mp->sml_op = LDAP_MOD_REPLACE;
431 mp->sml_flags = SLAP_MOD_INTERNAL;
432 *bp++ = *args->dercert;
433 BER_BVZERO( bp );
434 bp++;
435 mp->sml_next = mp+1;
436 mp++;
437
438 mp->sml_numvals = 1;
439 mp->sml_values = bp;
440 mp->sml_nvalues = NULL;
441 mp->sml_desc = args->isca ? ad_caPkey : ad_usrPkey;
442 mp->sml_op = LDAP_MOD_ADD;
443 mp->sml_flags = SLAP_MOD_INTERNAL;
444 *bp++ = *args->derpkey;
445 BER_BVZERO( bp );
446 mp->sml_next = NULL;
447
448 cb.sc_response = slap_null_cb;
449 bi = op->o_bd->bd_info;
450 op->o_bd->bd_info = args->on->on_info->oi_orig;
451 op->o_tag = LDAP_REQ_MODIFY;
452 op->o_callback = &cb;
453 op->orm_modlist = mod;
454 op->orm_no_opattrs = 1;
455 op->o_req_dn = *args->dn;
456 op->o_req_ndn = *args->ndn;
457 op->o_bd->be_modify( op, &rs );
458 op->o_bd->bd_info = bi;
459 return rs.sr_err;
460 }
461
462 static const struct berval configDN = BER_BVC("cn=config");
463
464 /* must run as a pool thread to avoid cn=config deadlock */
465 static void *
466 autoca_setca_task( void *ctx, void *arg )
467 {
468 Connection conn = { 0 };
469 OperationBuffer opbuf;
470 Operation *op;
471 struct berval *cacert = arg;
472 Modifications mod;
473 struct berval bvs[2];
474 slap_callback cb = {0};
475 SlapReply rs = {REP_RESULT};
476 const char *text;
477
478 connection_fake_init( &conn, &opbuf, ctx );
479 op = &opbuf.ob_op;
480
481 mod.sml_numvals = 1;
482 mod.sml_values = bvs;
483 mod.sml_nvalues = NULL;
484 mod.sml_desc = NULL;
485 if ( slap_str2ad( "olcTLSCACertificate;binary", &mod.sml_desc, &text ))
486 goto leave;
487 mod.sml_op = LDAP_MOD_REPLACE;
488 mod.sml_flags = SLAP_MOD_INTERNAL;
489 bvs[0] = *cacert;
490 BER_BVZERO( &bvs[1] );
491 mod.sml_next = NULL;
492
493 cb.sc_response = slap_null_cb;
494 op->o_bd = select_backend( (struct berval *)&configDN, 0 );
495 if ( !op->o_bd )
496 goto leave;
497
498 op->o_tag = LDAP_REQ_MODIFY;
499 op->o_callback = &cb;
500 op->orm_modlist = &mod;
501 op->orm_no_opattrs = 1;
502 op->o_req_dn = configDN;
503 op->o_req_ndn = configDN;
504 op->o_dn = op->o_bd->be_rootdn;
505 op->o_ndn = op->o_bd->be_rootndn;
506 op->o_bd->be_modify( op, &rs );
507 leave:
508 ch_free( arg );
509 return NULL;
510 }
511
512 static int
513 autoca_setca( struct berval *cacert )
514 {
515 struct berval *bv = ch_malloc( sizeof(struct berval) + cacert->bv_len );
516 bv->bv_len = cacert->bv_len;
517 bv->bv_val = (char *)(bv+1);
518 AC_MEMCPY( bv->bv_val, cacert->bv_val, bv->bv_len );
519 return ldap_pvt_thread_pool_submit( &connection_pool, autoca_setca_task, bv );
520 }
521
522 static int
523 autoca_setlocal( Operation *op, struct berval *cert, struct berval *pkey )
524 {
525 Modifications mod[2];
526 struct berval bvs[4];
527 slap_callback cb = {0};
528 SlapReply rs = {REP_RESULT};
529 const char *text;
530
531 mod[0].sml_numvals = 1;
532 mod[0].sml_values = bvs;
533 mod[0].sml_nvalues = NULL;
534 mod[0].sml_desc = NULL;
535 if ( slap_str2ad( "olcTLSCertificate;binary", &mod[0].sml_desc, &text ))
536 return -1;
537 mod[0].sml_op = LDAP_MOD_REPLACE;
538 mod[0].sml_flags = SLAP_MOD_INTERNAL;
539 bvs[0] = *cert;
540 BER_BVZERO( &bvs[1] );
541 mod[0].sml_next = &mod[1];
542
543 mod[1].sml_numvals = 1;
544 mod[1].sml_values = &bvs[2];
545 mod[1].sml_nvalues = NULL;
546 mod[1].sml_desc = NULL;
547 if ( slap_str2ad( "olcTLSCertificateKey;binary", &mod[1].sml_desc, &text ))
548 return -1;
549 mod[1].sml_op = LDAP_MOD_REPLACE;
550 mod[1].sml_flags = SLAP_MOD_INTERNAL;
551 bvs[2] = *pkey;
552 BER_BVZERO( &bvs[3] );
553 mod[1].sml_next = NULL;
554
555 cb.sc_response = slap_null_cb;
556 op->o_bd = select_backend( (struct berval *)&configDN, 0 );
557 if ( !op->o_bd )
558 return -1;
559
560 op->o_tag = LDAP_REQ_MODIFY;
561 op->o_callback = &cb;
562 op->orm_modlist = mod;
563 op->orm_no_opattrs = 1;
564 op->o_req_dn = configDN;
565 op->o_req_ndn = configDN;
566 op->o_dn = op->o_bd->be_rootdn;
567 op->o_ndn = op->o_bd->be_rootndn;
568 op->o_bd->be_modify( op, &rs );
569 return rs.sr_err;
570 }
571
572 enum {
573 ACA_USRCLASS = 1,
574 ACA_SRVCLASS,
575 ACA_USRKEYBITS,
576 ACA_SRVKEYBITS,
577 ACA_CAKEYBITS,
578 ACA_USRDAYS,
579 ACA_SRVDAYS,
580 ACA_CADAYS,
581 ACA_LOCALDN
582 };
583
584 static int autoca_cf( ConfigArgs *c )
585 {
586 slap_overinst *on = (slap_overinst *)c->bi;
587 autoca_info *ai = on->on_bi.bi_private;
588 int rc = 0;
589
590 switch( c->op ) {
591 case SLAP_CONFIG_EMIT:
592 switch( c->type ) {
593 case ACA_USRCLASS:
594 if ( ai->ai_usrclass ) {
595 c->value_string = ch_strdup( ai->ai_usrclass->soc_cname.bv_val );
596 } else {
597 rc = 1;
598 }
599 break;
600 case ACA_SRVCLASS:
601 if ( ai->ai_srvclass ) {
602 c->value_string = ch_strdup( ai->ai_srvclass->soc_cname.bv_val );
603 } else {
604 rc = 1;
605 }
606 break;
607 case ACA_USRKEYBITS:
608 c->value_int = ai->ai_usrkeybits;
609 break;
610 case ACA_SRVKEYBITS:
611 c->value_int = ai->ai_srvkeybits;
612 break;
613 case ACA_CAKEYBITS:
614 c->value_int = ai->ai_cakeybits;
615 break;
616 case ACA_USRDAYS:
617 c->value_int = ai->ai_usrdays;
618 break;
619 case ACA_SRVDAYS:
620 c->value_int = ai->ai_srvdays;
621 break;
622 case ACA_CADAYS:
623 c->value_int = ai->ai_cadays;
624 break;
625 case ACA_LOCALDN:
626 if ( !BER_BVISNULL( &ai->ai_localdn )) {
627 rc = value_add_one( &c->rvalue_vals, &ai->ai_localdn );
628 } else {
629 rc = 1;
630 }
631 break;
632 }
633 break;
634 case LDAP_MOD_DELETE:
635 switch( c->type ) {
636 case ACA_USRCLASS:
637 ai->ai_usrclass = NULL;
638 break;
639 case ACA_SRVCLASS:
640 ai->ai_srvclass = NULL;
641 break;
642 case ACA_LOCALDN:
643 if ( ai->ai_localdn.bv_val ) {
644 ch_free( ai->ai_localdn.bv_val );
645 ch_free( ai->ai_localndn.bv_val );
646 BER_BVZERO( &ai->ai_localdn );
647 BER_BVZERO( &ai->ai_localndn );
648 }
649 break;
650 /* single-valued attrs, all no-ops */
651 }
652 break;
653 case SLAP_CONFIG_ADD:
654 case LDAP_MOD_ADD:
655 switch( c->type ) {
656 case ACA_USRCLASS:
657 {
658 ObjectClass *oc = oc_find( c->value_string );
659 if ( oc )
660 ai->ai_usrclass = oc;
661 else
662 rc = 1;
663 }
664 break;
665 case ACA_SRVCLASS:
666 {
667 ObjectClass *oc = oc_find( c->value_string );
668 if ( oc )
669 ai->ai_srvclass = oc;
670 else
671 rc = 1;
672 }
673 break;
674 case ACA_USRKEYBITS:
675 if ( c->value_int < MIN_KEYBITS )
676 rc = 1;
677 else
678 ai->ai_usrkeybits = c->value_int;
679 break;
680 case ACA_SRVKEYBITS:
681 if ( c->value_int < MIN_KEYBITS )
682 rc = 1;
683 else
684 ai->ai_srvkeybits = c->value_int;
685 break;
686 case ACA_CAKEYBITS:
687 if ( c->value_int < MIN_KEYBITS )
688 rc = 1;
689 else
690 ai->ai_cakeybits = c->value_int;
691 break;
692 case ACA_USRDAYS:
693 ai->ai_usrdays = c->value_int;
694 break;
695 case ACA_SRVDAYS:
696 ai->ai_srvdays = c->value_int;
697 break;
698 case ACA_CADAYS:
699 ai->ai_cadays = c->value_int;
700 break;
701 case ACA_LOCALDN:
702 if ( c->be->be_nsuffix == NULL ) {
703 snprintf( c->cr_msg, sizeof( c->cr_msg ),
704 "suffix must be set" );
705 Debug( LDAP_DEBUG_CONFIG, "autoca_config: %s\n",
706 c->cr_msg );
707 rc = ARG_BAD_CONF;
708 break;
709 }
710 if ( !dnIsSuffix( &c->value_ndn, c->be->be_nsuffix )) {
711 snprintf( c->cr_msg, sizeof( c->cr_msg ),
712 "DN is not a subordinate of backend" );
713 Debug( LDAP_DEBUG_CONFIG, "autoca_config: %s\n",
714 c->cr_msg );
715 rc = ARG_BAD_CONF;
716 break;
717 }
718 if ( ai->ai_localdn.bv_val ) {
719 ch_free( ai->ai_localdn.bv_val );
720 ch_free( ai->ai_localndn.bv_val );
721 }
722 ai->ai_localdn = c->value_dn;
723 ai->ai_localndn = c->value_ndn;
724 }
725 }
726 return rc;
727 }
728
729 static ConfigTable autoca_cfg[] = {
730 { "userClass", "objectclass", 2, 2, 0,
731 ARG_STRING|ARG_MAGIC|ACA_USRCLASS, autoca_cf,
732 "( OLcfgOvAt:22.1 NAME 'olcAutoCAuserClass' "
733 "DESC 'ObjectClass of user entries' "
734 "EQUALITY caseIgnoreMatch "
735 "SYNTAX OMsDirectoryString SINGLE-VALUE )", NULL, NULL },
736 { "serverClass", "objectclass", 2, 2, 0,
737 ARG_STRING|ARG_MAGIC|ACA_SRVCLASS, autoca_cf,
738 "( OLcfgOvAt:22.2 NAME 'olcAutoCAserverClass' "
739 "DESC 'ObjectClass of server entries' "
740 "EQUALITY caseIgnoreMatch "
741 "SYNTAX OMsDirectoryString SINGLE-VALUE )", NULL, NULL },
742 { "userKeybits", "integer", 2, 2, 0,
743 ARG_INT|ARG_MAGIC|ACA_USRKEYBITS, autoca_cf,
744 "( OLcfgOvAt:22.3 NAME 'olcAutoCAuserKeybits' "
745 "DESC 'Size of PrivateKey for user entries' "
746 "EQUALITY integerMatch "
747 "SYNTAX OMsInteger SINGLE-VALUE )", NULL, NULL },
748 { "serverKeybits", "integer", 2, 2, 0,
749 ARG_INT|ARG_MAGIC|ACA_SRVKEYBITS, autoca_cf,
750 "( OLcfgOvAt:22.4 NAME 'olcAutoCAserverKeybits' "
751 "DESC 'Size of PrivateKey for server entries' "
752 "EQUALITY integerMatch "
753 "SYNTAX OMsInteger SINGLE-VALUE )", NULL, NULL },
754 { "caKeybits", "integer", 2, 2, 0,
755 ARG_INT|ARG_MAGIC|ACA_CAKEYBITS, autoca_cf,
756 "( OLcfgOvAt:22.5 NAME 'olcAutoCAKeybits' "
757 "DESC 'Size of PrivateKey for CA certificate' "
758 "EQUALITY integerMatch "
759 "SYNTAX OMsInteger SINGLE-VALUE )", NULL, NULL },
760 { "userDays", "integer", 2, 2, 0,
761 ARG_INT|ARG_MAGIC|ACA_USRDAYS, autoca_cf,
762 "( OLcfgOvAt:22.6 NAME 'olcAutoCAuserDays' "
763 "DESC 'Lifetime of user certificates in days' "
764 "EQUALITY integerMatch "
765 "SYNTAX OMsInteger SINGLE-VALUE )", NULL, NULL },
766 { "serverDays", "integer", 2, 2, 0,
767 ARG_INT|ARG_MAGIC|ACA_SRVDAYS, autoca_cf,
768 "( OLcfgOvAt:22.7 NAME 'olcAutoCAserverDays' "
769 "DESC 'Lifetime of server certificates in days' "
770 "EQUALITY integerMatch "
771 "SYNTAX OMsInteger SINGLE-VALUE )", NULL, NULL },
772 { "caDays", "integer", 2, 2, 0,
773 ARG_INT|ARG_MAGIC|ACA_CADAYS, autoca_cf,
774 "( OLcfgOvAt:22.8 NAME 'olcAutoCADays' "
775 "DESC 'Lifetime of CA certificate in days' "
776 "EQUALITY integerMatch "
777 "SYNTAX OMsInteger SINGLE-VALUE )", NULL, NULL },
778 { "localdn", "dn", 2, 2, 0,
779 ARG_DN|ARG_QUOTE|ARG_MAGIC|ACA_LOCALDN, autoca_cf,
780 "( OLcfgOvAt:22.9 NAME 'olcAutoCAlocalDN' "
781 "DESC 'DN of local server cert' "
782 "EQUALITY distinguishedNameMatch "
783 "SYNTAX OMsDN SINGLE-VALUE )", NULL, NULL },
784 { NULL, NULL, 0, 0, 0, ARG_IGNORED }
785 };
786
787 static ConfigOCs autoca_ocs[] = {
788 { "( OLcfgOvOc:22.1 "
789 "NAME 'olcAutoCAConfig' "
790 "DESC 'AutoCA configuration' "
791 "SUP olcOverlayConfig "
792 "MAY ( olcAutoCAuserClass $ olcAutoCAserverClass $ "
793 "olcAutoCAuserKeybits $ olcAutoCAserverKeybits $ olcAutoCAKeyBits $ "
794 "olcAutoCAuserDays $ olcAutoCAserverDays $ olcAutoCADays $ "
795 "olcAutoCAlocalDN ) )",
796 Cft_Overlay, autoca_cfg },
797 { NULL, 0, NULL }
798 };
799
800 static int
801 autoca_op_response(
802 Operation *op,
803 SlapReply *rs
804 )
805 {
806 slap_overinst *on = op->o_callback->sc_private;
807 autoca_info *ai = on->on_bi.bi_private;
808 Attribute *a;
809 int isusr = 0;
810
811 if (rs->sr_type != REP_SEARCH)
812 return SLAP_CB_CONTINUE;
813
814 /* If root or self */
815 if ( !be_isroot( op ) &&
816 !dn_match( &rs->sr_entry->e_nname, &op->o_ndn ))
817 return SLAP_CB_CONTINUE;
818
819 isusr = is_entry_objectclass( rs->sr_entry, ai->ai_usrclass, SLAP_OCF_CHECK_SUP );
820 if ( !isusr )
821 {
822 if (!is_entry_objectclass( rs->sr_entry, ai->ai_srvclass, SLAP_OCF_CHECK_SUP ))
823 return SLAP_CB_CONTINUE;
824 }
825 a = attr_find( rs->sr_entry->e_attrs, ad_usrPkey );
826 if ( !a )
827 {
828 Operation op2;
829 genargs args;
830 saveargs arg2;
831 myext extras[2];
832 int rc;
833
834 args.issuer_cert = ai->ai_cert;
835 args.issuer_pkey = ai->ai_pkey;
836 args.subjectDN = &rs->sr_entry->e_name;
837 args.more_exts = NULL;
838 if ( isusr )
839 {
840 args.cert_exts = usrExts;
841 args.keybits = ai->ai_usrkeybits;
842 args.days = ai->ai_usrdays;
843 a = attr_find( rs->sr_entry->e_attrs, ad_mail );
844 if ( a )
845 {
846 extras[0].name = "subjectAltName";
847 extras[1].name = NULL;
848 extras[0].value = op->o_tmpalloc( sizeof("email:") + a->a_vals[0].bv_len, op->o_tmpmemctx );
849 sprintf(extras[0].value, "email:%s", a->a_vals[0].bv_val);
850 args.more_exts = extras;
851 }
852 } else
853 {
854 args.cert_exts = srvExts;
855 args.keybits = ai->ai_srvkeybits;
856 args.days = ai->ai_srvdays;
857 if ( ad_ipaddr && (a = attr_find( rs->sr_entry->e_attrs, ad_ipaddr )))
858 {
859 extras[0].name = "subjectAltName";
860 extras[1].name = NULL;
861 extras[0].value = op->o_tmpalloc( sizeof("IP:") + a->a_vals[0].bv_len, op->o_tmpmemctx );
862 sprintf(extras[0].value, "IP:%s", a->a_vals[0].bv_val);
863 args.more_exts = extras;
864 }
865 }
866 rc = autoca_gencert( op, &args );
867 if ( rc )
868 return SLAP_CB_CONTINUE;
869 X509_free( args.newcert );
870 EVP_PKEY_free( args.newpkey );
871
872 if ( is_entry_objectclass( rs->sr_entry, oc_usrObj, 0 ))
873 arg2.oc = NULL;
874 else
875 arg2.oc = oc_usrObj;
876 if ( !( rs->sr_flags & REP_ENTRY_MODIFIABLE ))
877 {
878 Entry *e = entry_dup( rs->sr_entry );
879 rs_replace_entry( op, rs, on, e );
880 rs->sr_flags |= REP_ENTRY_MODIFIABLE | REP_ENTRY_MUSTBEFREED;
881 }
882 arg2.dercert = &args.dercert;
883 arg2.derpkey = &args.derpkey;
884 arg2.on = on;
885 arg2.dn = &rs->sr_entry->e_name;
886 arg2.ndn = &rs->sr_entry->e_nname;
887 arg2.isca = 0;
888 op2 = *op;
889 rc = autoca_savecert( &op2, &arg2 );
890 if ( !rc )
891 {
892 /* If this is our cert DN, configure it */
893 if ( dn_match( &rs->sr_entry->e_nname, &ai->ai_localndn ))
894 autoca_setlocal( &op2, &args.dercert, &args.derpkey );
895 attr_merge_one( rs->sr_entry, ad_usrCert, &args.dercert, NULL );
896 attr_merge_one( rs->sr_entry, ad_usrPkey, &args.derpkey, NULL );
897 }
898 op->o_tmpfree( args.dercert.bv_val, op->o_tmpmemctx );
899 op->o_tmpfree( args.derpkey.bv_val, op->o_tmpmemctx );
900 }
901
902 return SLAP_CB_CONTINUE;
903 }
904
905 static int
906 autoca_op_search(
907 Operation *op,
908 SlapReply *rs
909 )
910 {
911 /* we only act on a search that returns just our cert/key attrs */
912 if ( op->ors_attrs && op->ors_attrs[0].an_desc == ad_usrCert &&
913 op->ors_attrs[1].an_desc == ad_usrPkey &&
914 op->ors_attrs[2].an_name.bv_val == NULL )
915 {
916 slap_overinst *on = (slap_overinst *)op->o_bd->bd_info;
917 slap_callback *sc = op->o_tmpcalloc( 1, sizeof(slap_callback), op->o_tmpmemctx );
918 sc->sc_response = autoca_op_response;
919 sc->sc_private = on;
920 sc->sc_next = op->o_callback;
921 op->o_callback = sc;
922 }
923 return SLAP_CB_CONTINUE;
924 }
925
926 static int
927 autoca_db_init(
928 BackendDB *be,
929 ConfigReply *cr
930 )
931 {
932 slap_overinst *on = (slap_overinst *) be->bd_info;
933 autoca_info *ai;
934
935 ai = ch_calloc(1, sizeof(autoca_info));
936 on->on_bi.bi_private = ai;
937
938 /* set defaults */
939 ai->ai_usrclass = oc_find( "person" );
940 ai->ai_srvclass = oc_find( "ipHost" );
941 ai->ai_usrkeybits = KEYBITS;
942 ai->ai_srvkeybits = KEYBITS;
943 ai->ai_cakeybits = KEYBITS;
944 ai->ai_usrdays = 365; /* 1 year */
945 ai->ai_srvdays = 1826; /* 5 years */
946 ai->ai_cadays = 3652; /* 10 years */
947 return 0;
948 }
949
950 static int
951 autoca_db_destroy(
952 BackendDB *be,
953 ConfigReply *cr
954 )
955 {
956 slap_overinst *on = (slap_overinst *) be->bd_info;
957 autoca_info *ai = on->on_bi.bi_private;
958
959 if ( ai->ai_cert )
960 X509_free( ai->ai_cert );
961 if ( ai->ai_pkey )
962 EVP_PKEY_free( ai->ai_pkey );
963 ch_free( ai );
964
965 return 0;
966 }
967
968 static int
969 autoca_db_open(
970 BackendDB *be,
971 ConfigReply *cr
972 )
973 {
974 slap_overinst *on = (slap_overinst *)be->bd_info;
975 autoca_info *ai = on->on_bi.bi_private;
976
977 Connection conn = { 0 };
978 OperationBuffer opbuf;
979 Operation *op;
980 void *thrctx;
981 Entry *e = NULL;
982 Attribute *a;
983 int rc;
984
985 if (slapMode & SLAP_TOOL_MODE)
986 return 0;
987
988 if ( ! *aca_attr2[0].ad ) {
989 int i, code;
990 const char *text;
991
992 for ( i=0; aca_attr2[i].at; i++ ) {
993 code = slap_str2ad( aca_attr2[i].at, aca_attr2[i].ad, &text );
994 if ( code ) return code;
995 }
996
997 /* Schema may not be loaded, ignore if missing */
998 slap_str2ad( "ipHostNumber", &ad_ipaddr, &text );
999
1000 for ( i=0; aca_ocs[i].ot; i++ ) {
1001 code = register_oc( aca_ocs[i].ot, aca_ocs[i].oc, 0 );
1002 if ( code ) return code;
1003 }
1004 }
1005
1006 thrctx = ldap_pvt_thread_pool_context();
1007 connection_fake_init2( &conn, &opbuf, thrctx, 0 );
1008 op = &opbuf.ob_op;
1009 op->o_bd = be;
1010 op->o_dn = be->be_rootdn;
1011 op->o_ndn = be->be_rootndn;
1012 rc = overlay_entry_get_ov( op, be->be_nsuffix, NULL,
1013 NULL, 0, &e, on );
1014
1015 if ( e ) {
1016 int gotoc = 0, gotat = 0;
1017 if ( is_entry_objectclass( e, oc_caObj, 0 )) {
1018 gotoc = 1;
1019 a = attr_find( e->e_attrs, ad_caPkey );
1020 if ( a ) {
1021 const unsigned char *pp;
1022 pp = (unsigned char *)a->a_vals[0].bv_val;
1023 ai->ai_pkey = d2i_AutoPrivateKey( NULL, &pp, a->a_vals[0].bv_len );
1024 if ( ai->ai_pkey )
1025 {
1026 a = attr_find( e->e_attrs, ad_caCert );
1027 if ( a )
1028 {
1029 pp = (unsigned char *)a->a_vals[0].bv_val;
1030 ai->ai_cert = d2i_X509( NULL, &pp, a->a_vals[0].bv_len );
1031 /* If TLS wasn't configured yet, set this as our CA */
1032 if ( !slap_tls_ctx )
1033 autoca_setca( a->a_vals );
1034 }
1035 }
1036 gotat = 1;
1037 }
1038 }
1039 overlay_entry_release_ov( op, e, 0, on );
1040 /* generate attrs, store... */
1041 if ( !gotat ) {
1042 genargs args;
1043 saveargs arg2;
1044
1045 args.issuer_cert = NULL;
1046 args.issuer_pkey = NULL;
1047 args.subjectDN = &be->be_suffix[0];
1048 args.cert_exts = CAexts;
1049 args.more_exts = NULL;
1050 args.keybits = ai->ai_cakeybits;
1051 args.days = ai->ai_cadays;
1052
1053 rc = autoca_gencert( op, &args );
1054 if ( rc )
1055 return -1;
1056
1057 ai->ai_cert = args.newcert;
1058 ai->ai_pkey = args.newpkey;
1059
1060 arg2.dn = be->be_suffix;
1061 arg2.ndn = be->be_nsuffix;
1062 arg2.isca = 1;
1063 if ( !gotoc )
1064 arg2.oc = oc_caObj;
1065 else
1066 arg2.oc = NULL;
1067 arg2.on = on;
1068 arg2.dercert = &args.dercert;
1069 arg2.derpkey = &args.derpkey;
1070
1071 autoca_savecert( op, &arg2 );
1072
1073 /* If TLS wasn't configured yet, set this as our CA */
1074 if ( !slap_tls_ctx )
1075 autoca_setca( &args.dercert );
1076
1077 op->o_tmpfree( args.dercert.bv_val, op->o_tmpmemctx );
1078 op->o_tmpfree( args.derpkey.bv_val, op->o_tmpmemctx );
1079 }
1080 }
1081
1082 return 0;
1083 }
1084
1085 static slap_overinst autoca;
1086
1087 /* This overlay is set up for dynamic loading via moduleload. For static
1088 * configuration, you'll need to arrange for the slap_overinst to be
1089 * initialized and registered by some other function inside slapd.
1090 */
1091
1092 int autoca_initialize() {
1093 int i, code;
1094
1095 autoca.on_bi.bi_type = "autoca";
1096 autoca.on_bi.bi_flags = SLAPO_BFLAG_SINGLE;
1097 autoca.on_bi.bi_db_init = autoca_db_init;
1098 autoca.on_bi.bi_db_destroy = autoca_db_destroy;
1099 autoca.on_bi.bi_db_open = autoca_db_open;
1100 autoca.on_bi.bi_op_search = autoca_op_search;
1101
1102 autoca.on_bi.bi_cf_ocs = autoca_ocs;
1103 code = config_register_schema( autoca_cfg, autoca_ocs );
1104 if ( code ) return code;
1105
1106 for ( i=0; aca_attrs[i]; i++ ) {
1107 code = register_at( aca_attrs[i], NULL, 0 );
1108 if ( code ) return code;
1109 }
1110
1111 return overlay_register( &autoca );
1112 }
1113
1114 #if SLAPD_OVER_AUTOCA == SLAPD_MOD_DYNAMIC
1115 int
1116 init_module( int argc, char *argv[] )
1117 {
1118 return autoca_initialize();
1119 }
1120 #endif
1121
1122 #endif /* defined(SLAPD_OVER_AUTOCA) */