1 /* autoca.c - Automatic Certificate Authority */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 2009-2017 The OpenLDAP Foundation.
6 * Copyright 2009-2017 by Howard Chu.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted only as authorized by the OpenLDAP
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>.
18 * This work was initially developed by Howard Chu for inclusion in
24 #define SLAPD_OVER_AUTOCA SLAPD_MOD_DYNAMIC
26 #ifdef SLAPD_OVER_AUTOCA
30 #include <ac/string.h>
31 #include <ac/socket.h>
37 #include <openssl/x509v3.h>
38 #include <openssl/evp.h>
40 /* This overlay implements a certificate authority that can generate
41 * certificates automatically for any entry in the directory.
42 * On startup it generates a self-signed CA cert for the directory's
43 * suffix entry and uses this to sign all other certs that it generates.
44 * User and server certs are generated on demand, using a Search request.
47 #define LBER_TAG_OID ((ber_tag_t) 0x06UL)
48 #define LBER_TAG_UTF8 ((ber_tag_t) 0x0cUL)
51 #define MIN_KEYBITS 512
53 #define ACA_SCHEMA_ROOT "1.3.6.1.4.1.4203.666.11.11"
55 #define ACA_SCHEMA_AT ACA_SCHEMA_ROOT ".1"
56 #define ACA_SCHEMA_OC ACA_SCHEMA_ROOT ".2"
58 static AttributeDescription *ad_caCert, *ad_caPkey, *ad_usrCert, *ad_usrPkey;
59 static AttributeDescription *ad_mail, *ad_ipaddr;
60 static ObjectClass *oc_caObj, *oc_usrObj;
62 static char *aca_attrs[] = {
63 "( " ACA_SCHEMA_AT ".1 NAME 'cAPrivateKey' "
64 "DESC 'X.509 CA private key, use ;binary' "
65 "SUP x509PrivateKey )",
66 "( " ACA_SCHEMA_AT ".2 NAME 'userPrivateKey' "
67 "DESC 'X.509 user private key, use ;binary' "
68 "SUP x509PrivateKey )",
74 AttributeDescription **ad;
76 { "cACertificate;binary", &ad_caCert },
77 { "cAPrivateKey;binary", &ad_caPkey },
78 { "userCertificate;binary", &ad_usrCert },
79 { "userPrivateKey;binary", &ad_usrPkey },
88 { "( " ACA_SCHEMA_OC ".1 NAME 'autoCA' "
89 "DESC 'Automated PKI certificate authority' "
90 "SUP pkiCA AUXILIARY "
91 "MAY cAPrivateKey )", &oc_caObj },
92 { "( " ACA_SCHEMA_OC ".2 NAME 'autoCAuser' "
93 "DESC 'Automated PKI CA user' "
94 "SUP pkiUser AUXILIARY "
95 "MAY userPrivateKey )", &oc_usrObj },
99 typedef struct autoca_info {
102 ObjectClass *ai_usrclass;
103 ObjectClass *ai_srvclass;
104 struct berval ai_localdn;
105 struct berval ai_localndn;
114 /* Rewrite an LDAP DN in DER form
115 * Input must be valid DN, therefore no error checking is done here.
117 static int autoca_dnbv2der( Operation *op, struct berval *bv, struct berval *der )
119 BerElementBuffer berbuf;
120 BerElement *ber = (BerElement *)&berbuf;
124 AttributeDescription *ad;
127 ldap_bv2dn_x( bv, &dn, LDAP_DN_FORMAT_LDAP, op->o_tmpmemctx );
129 ber_init2( ber, NULL, LBER_USE_DER );
130 ber_set_option( ber, LBER_OPT_BER_MEMCTX, &op->o_tmpmemctx );
132 /* count RDNs, we need them in reverse order */
133 for (irdn = 0; dn[irdn]; irdn++);
136 /* DN is a SEQuence of RDNs */
137 ber_start_seq( ber, LBER_SEQUENCE );
138 for (; irdn >=0; irdn--)
140 /* RDN is a SET of AVAs */
141 ber_start_set( ber, LBER_SET );
143 for (iava = 0; rdn[iava]; iava++)
147 struct berval bvo = { sizeof(oid), oid };
150 /* AVA is a SEQuence of attr and value */
151 ber_start_seq( ber, LBER_SEQUENCE );
154 slap_bv2ad( &ava->la_attr, &ad, &text );
155 ber_str2bv( ad->ad_type->sat_oid, 0, 0, &bva );
156 ber_encode_oid( &bva, &bvo );
157 ber_put_berval( ber, &bvo, LBER_TAG_OID );
158 ber_put_berval( ber, &ava->la_value, LBER_TAG_UTF8 );
164 ber_flatten2( ber, der, 0 );
165 ldap_dnfree_x( dn, op->o_tmpmemctx );
169 static int autoca_genpkey(int bits, EVP_PKEY **pkey)
174 kctx = EVP_PKEY_CTX_new_id(EVP_PKEY_RSA, NULL);
177 if (EVP_PKEY_keygen_init(kctx) <= 0)
179 EVP_PKEY_CTX_free(kctx);
182 if (EVP_PKEY_CTX_set_rsa_keygen_bits(kctx, bits) <= 0)
184 EVP_PKEY_CTX_free(kctx);
187 rc = EVP_PKEY_keygen(kctx, pkey);
188 EVP_PKEY_CTX_free(kctx);
192 static int autoca_signcert(X509 *cert, EVP_PKEY *pkey)
194 EVP_MD_CTX *ctx = EVP_MD_CTX_create();
195 EVP_PKEY_CTX *pkctx = NULL;
200 if (EVP_DigestSignInit(ctx, &pkctx, NULL, NULL, pkey))
202 rc = X509_sign_ctx(cert, ctx);
204 EVP_MD_CTX_destroy(ctx);
208 #define SERIAL_BITS 64 /* should be less than 160 */
210 typedef struct myext {
215 static myext CAexts[] = {
216 { "subjectKeyIdentifier", "hash" },
217 { "authorityKeyIdentifier", "keyid:always,issuer" },
218 { "basicConstraints", "critical,CA:true" },
219 { "keyUsage", "digitalSignature,cRLSign,keyCertSign" },
220 { "nsComment", "OpenLDAP automatic certificate" },
224 static myext usrExts[] = {
225 { "subjectKeyIdentifier", "hash" },
226 { "authorityKeyIdentifier", "keyid:always,issuer" },
227 { "basicConstraints", "CA:false" },
228 { "keyUsage", "digitalSignature,nonRepudiation,keyEncipherment" },
229 { "extendedKeyUsage", "clientAuth,emailProtection,codeSigning" },
230 { "nsComment", "OpenLDAP automatic certificate" },
234 static myext srvExts[] = {
235 { "subjectKeyIdentifier", "hash" },
236 { "authorityKeyIdentifier", "keyid:always,issuer" },
237 { "basicConstraints", "CA:false" },
238 { "keyUsage", "digitalSignature,keyEncipherment" },
239 { "extendedKeyUsage", "serverAuth,clientAuth" },
240 { "nsComment", "OpenLDAP automatic certificate" },
244 typedef struct genargs {
246 EVP_PKEY *issuer_pkey;
247 struct berval *subjectDN;
252 struct berval dercert;
253 struct berval derpkey;
258 static int autoca_gencert( Operation *op, genargs *args )
260 X509_NAME *subj_name, *issuer_name;
263 const unsigned char *p;
264 EVP_PKEY *evpk = NULL;
268 if ((subj_cert = X509_new()) == NULL)
271 autoca_dnbv2der( op, args->subjectDN, &derdn );
272 p = (const unsigned char *)derdn.bv_val;
273 subj_name = d2i_X509_NAME( NULL, &p, derdn.bv_len );
274 op->o_tmpfree( derdn.bv_val, op->o_tmpmemctx );
275 if ( subj_name == NULL )
278 X509_free( subj_cert );
282 rc = autoca_genpkey( args->keybits, &evpk );
286 if ( subj_name ) X509_NAME_free( subj_name );
289 args->derpkey.bv_len = i2d_PrivateKey( evpk, NULL );
290 args->derpkey.bv_val = op->o_tmpalloc( args->derpkey.bv_len, op->o_tmpmemctx );
291 pp = args->derpkey.bv_val;
292 i2d_PrivateKey( evpk, &pp );
293 args->newpkey = evpk;
295 /* set random serial */
297 BIGNUM *bn = BN_new();
301 EVP_PKEY_free( evpk );
304 if (!BN_pseudo_rand(bn, SERIAL_BITS, 0, 0))
309 if (!BN_to_ASN1_INTEGER(bn, X509_get_serialNumber(subj_cert)))
316 if (args->issuer_cert) {
317 issuer_name = X509_get_subject_name(args->issuer_cert);
319 issuer_name = subj_name;
320 args->issuer_cert = subj_cert;
321 args->issuer_pkey = evpk;
323 if (!X509_set_version(subj_cert, 2) || /* set version to V3 */
324 !X509_set_issuer_name(subj_cert, issuer_name) ||
325 !X509_set_subject_name(subj_cert, subj_name) ||
326 !X509_gmtime_adj(X509_get_notBefore(subj_cert), 0) ||
327 !X509_time_adj_ex(X509_get_notAfter(subj_cert), args->days, 0, NULL) ||
328 !X509_set_pubkey(subj_cert, evpk))
332 X509_NAME_free(subj_name);
335 /* set cert extensions */
341 X509V3_set_ctx(&ctx, args->issuer_cert, subj_cert, NULL, NULL, 0);
342 for (i=0; args->cert_exts[i].name; i++) {
343 ext = X509V3_EXT_nconf(NULL, &ctx, args->cert_exts[i].name, args->cert_exts[i].value);
346 rc = X509_add_ext(subj_cert, ext, -1);
347 X509_EXTENSION_free(ext);
351 if (args->more_exts) {
352 for (i=0; args->more_exts[i].name; i++) {
353 ext = X509V3_EXT_nconf(NULL, &ctx, args->more_exts[i].name, args->more_exts[i].value);
356 rc = X509_add_ext(subj_cert, ext, -1);
357 X509_EXTENSION_free(ext);
363 rc = autoca_signcert( subj_cert, args->issuer_pkey );
366 args->dercert.bv_len = i2d_X509( subj_cert, NULL );
367 args->dercert.bv_val = op->o_tmpalloc( args->dercert.bv_len, op->o_tmpmemctx );
368 pp = args->dercert.bv_val;
369 i2d_X509( subj_cert, &pp );
370 args->newcert = subj_cert;
374 typedef struct saveargs {
376 struct berval *dercert;
377 struct berval *derpkey;
384 static int autoca_savecert( Operation *op, saveargs *args )
386 Modifications mod[3], *mp = mod;
387 struct berval bvs[6], *bp = bvs;
389 slap_callback cb = {0};
390 SlapReply rs = {REP_RESULT};
395 mp->sml_nvalues = NULL;
396 mp->sml_desc = slap_schema.si_ad_objectClass;
397 mp->sml_op = LDAP_MOD_ADD;
398 mp->sml_flags = SLAP_MOD_INTERNAL;
399 *bp++ = args->oc->soc_cname;
407 mp->sml_nvalues = NULL;
408 mp->sml_desc = args->isca ? ad_caCert : ad_usrCert;
409 mp->sml_op = LDAP_MOD_REPLACE;
410 mp->sml_flags = SLAP_MOD_INTERNAL;
411 *bp++ = *args->dercert;
419 mp->sml_nvalues = NULL;
420 mp->sml_desc = args->isca ? ad_caPkey : ad_usrPkey;
421 mp->sml_op = LDAP_MOD_ADD;
422 mp->sml_flags = SLAP_MOD_INTERNAL;
423 *bp++ = *args->derpkey;
427 cb.sc_response = slap_null_cb;
428 bi = op->o_bd->bd_info;
429 op->o_bd->bd_info = args->on->on_info->oi_orig;
430 op->o_tag = LDAP_REQ_MODIFY;
431 op->o_callback = &cb;
432 op->orm_modlist = mod;
433 op->orm_no_opattrs = 1;
434 op->o_req_dn = *args->dn;
435 op->o_req_ndn = *args->ndn;
436 op->o_bd->be_modify( op, &rs );
437 op->o_bd->bd_info = bi;
441 static const struct berval configDN = BER_BVC("cn=config");
444 autoca_setca( Operation *op, struct berval *cacert )
448 struct berval bvs[2];
450 slap_callback cb = {0};
451 SlapReply rs = {REP_RESULT};
456 mod.sml_values = bvs;
457 mod.sml_nvalues = NULL;
459 if ( slap_str2ad( "olcTLSCACertificate;binary", &mod.sml_desc, &text ))
461 mod.sml_op = LDAP_MOD_REPLACE;
462 mod.sml_flags = SLAP_MOD_INTERNAL;
464 BER_BVZERO( &bvs[1] );
467 cb.sc_response = slap_null_cb;
468 op2.o_bd = select_backend( (struct berval *)&configDN, 0 );
472 op2.o_tag = LDAP_REQ_MODIFY;
473 op2.o_callback = &cb;
474 op2.orm_modlist = &mod;
475 op2.orm_no_opattrs = 1;
476 op2.o_req_dn = configDN;
477 op2.o_req_ndn = configDN;
478 op2.o_dn = op2.o_bd->be_rootdn;
479 op2.o_ndn = op2.o_bd->be_rootndn;
480 op2.o_bd->be_modify( &op2, &rs );
485 autoca_setlocal( Operation *op, struct berval *cert, struct berval *pkey )
487 Modifications mod[2];
488 struct berval bvs[4];
490 slap_callback cb = {0};
491 SlapReply rs = {REP_RESULT};
493 static const struct berval config = BER_BVC("cn=config");
495 mod[0].sml_numvals = 1;
496 mod[0].sml_values = bvs;
497 mod[0].sml_nvalues = NULL;
498 mod[0].sml_desc = NULL;
499 if ( slap_str2ad( "olcTLSCertificate;binary", &mod[0].sml_desc, &text ))
501 mod[0].sml_op = LDAP_MOD_REPLACE;
502 mod[0].sml_flags = SLAP_MOD_INTERNAL;
504 BER_BVZERO( &bvs[1] );
505 mod[0].sml_next = &mod[1];
507 mod[1].sml_numvals = 1;
508 mod[1].sml_values = &bvs[2];
509 mod[1].sml_nvalues = NULL;
510 mod[1].sml_desc = NULL;
511 if ( slap_str2ad( "olcTLSCertificateKey;binary", &mod[1].sml_desc, &text ))
513 mod[1].sml_op = LDAP_MOD_REPLACE;
514 mod[1].sml_flags = SLAP_MOD_INTERNAL;
516 BER_BVZERO( &bvs[3] );
517 mod[1].sml_next = NULL;
519 cb.sc_response = slap_null_cb;
520 op->o_bd = select_backend( (struct berval *)&configDN, 0 );
524 op->o_tag = LDAP_REQ_MODIFY;
525 op->o_callback = &cb;
526 op->orm_modlist = mod;
527 op->orm_no_opattrs = 1;
528 op->o_req_dn = configDN;
529 op->o_req_ndn = configDN;
530 op->o_dn = op->o_bd->be_rootdn;
531 op->o_ndn = op->o_bd->be_rootndn;
532 op->o_bd->be_modify( op, &rs );
548 static int autoca_cf( ConfigArgs *c )
550 slap_overinst *on = (slap_overinst *)c->bi;
551 autoca_info *ai = on->on_bi.bi_private;
555 case SLAP_CONFIG_EMIT:
558 if ( ai->ai_usrclass ) {
559 c->value_string = ch_strdup( ai->ai_usrclass->soc_cname.bv_val );
565 if ( ai->ai_srvclass ) {
566 c->value_string = ch_strdup( ai->ai_srvclass->soc_cname.bv_val );
572 c->value_int = ai->ai_usrkeybits;
575 c->value_int = ai->ai_srvkeybits;
578 c->value_int = ai->ai_cakeybits;
581 c->value_int = ai->ai_usrdays;
584 c->value_int = ai->ai_srvdays;
587 c->value_int = ai->ai_cadays;
590 if ( !BER_BVISNULL( &ai->ai_localdn )) {
591 rc = value_add_one( &c->rvalue_vals, &ai->ai_localdn );
598 case LDAP_MOD_DELETE:
601 ai->ai_usrclass = NULL;
604 ai->ai_srvclass = NULL;
607 if ( ai->ai_localdn.bv_val ) {
608 ch_free( ai->ai_localdn.bv_val );
609 ch_free( ai->ai_localndn.bv_val );
610 BER_BVZERO( &ai->ai_localdn );
611 BER_BVZERO( &ai->ai_localndn );
614 /* single-valued attrs, all no-ops */
617 case SLAP_CONFIG_ADD:
622 ObjectClass *oc = oc_find( c->value_string );
624 ai->ai_usrclass = oc;
631 ObjectClass *oc = oc_find( c->value_string );
633 ai->ai_srvclass = oc;
638 if ( c->value_int < MIN_KEYBITS )
641 ai->ai_usrkeybits = c->value_int;
644 if ( c->value_int < MIN_KEYBITS )
647 ai->ai_srvkeybits = c->value_int;
650 if ( c->value_int < MIN_KEYBITS )
653 ai->ai_cakeybits = c->value_int;
656 ai->ai_usrdays = c->value_int;
659 ai->ai_srvdays = c->value_int;
662 ai->ai_cadays = c->value_int;
665 if ( c->be->be_nsuffix == NULL ) {
666 snprintf( c->cr_msg, sizeof( c->cr_msg ),
667 "suffix must be set" );
668 Debug( LDAP_DEBUG_CONFIG, "autoca_config: %s\n",
669 c->cr_msg, NULL, NULL );
673 if ( !dnIsSuffix( &c->value_ndn, c->be->be_nsuffix )) {
674 snprintf( c->cr_msg, sizeof( c->cr_msg ),
675 "DN is not a subordinate of backend" );
676 Debug( LDAP_DEBUG_CONFIG, "autoca_config: %s\n",
677 c->cr_msg, NULL, NULL );
681 if ( ai->ai_localdn.bv_val ) {
682 ch_free( ai->ai_localdn.bv_val );
683 ch_free( ai->ai_localndn.bv_val );
685 ai->ai_localdn = c->value_dn;
686 ai->ai_localndn = c->value_ndn;
692 static ConfigTable autoca_cfg[] = {
693 { "userClass", "objectclass", 2, 2, 0,
694 ARG_STRING|ARG_MAGIC|ACA_USRCLASS, autoca_cf,
695 "( OLcfgOvAt:22.1 NAME 'olcACAuserClass' "
696 "DESC 'ObjectClass of user entries' "
697 "SYNTAX OMsDirectoryString SINGLE-VALUE )", NULL, NULL },
698 { "serverClass", "objectclass", 2, 2, 0,
699 ARG_STRING|ARG_MAGIC|ACA_SRVCLASS, autoca_cf,
700 "( OLcfgOvAt:22.2 NAME 'olcACAserverClass' "
701 "DESC 'ObjectClass of server entries' "
702 "SYNTAX OMsDirectoryString SINGLE-VALUE )", NULL, NULL },
703 { "userKeybits", "integer", 2, 2, 0,
704 ARG_INT|ARG_MAGIC|ACA_USRKEYBITS, autoca_cf,
705 "( OLcfgOvAt:22.3 NAME 'olcACAuserKeybits' "
706 "DESC 'Size of PrivateKey for user entries' "
707 "SYNTAX OMsInteger SINGLE-VALUE )", NULL, NULL },
708 { "serverKeybits", "integer", 2, 2, 0,
709 ARG_INT|ARG_MAGIC|ACA_SRVKEYBITS, autoca_cf,
710 "( OLcfgOvAt:22.4 NAME 'olcACAserverKeybits' "
711 "DESC 'Size of PrivateKey for server entries' "
712 "SYNTAX OMsInteger SINGLE-VALUE )", NULL, NULL },
713 { "caKeybits", "integer", 2, 2, 0,
714 ARG_INT|ARG_MAGIC|ACA_CAKEYBITS, autoca_cf,
715 "( OLcfgOvAt:22.5 NAME 'olcACAKeybits' "
716 "DESC 'Size of PrivateKey for CA certificate' "
717 "SYNTAX OMsInteger SINGLE-VALUE )", NULL, NULL },
718 { "userDays", "integer", 2, 2, 0,
719 ARG_INT|ARG_MAGIC|ACA_USRDAYS, autoca_cf,
720 "( OLcfgOvAt:22.6 NAME 'olcACAuserDays' "
721 "DESC 'Lifetime of user certificates in days' "
722 "SYNTAX OMsInteger SINGLE-VALUE )", NULL, NULL },
723 { "serverDays", "integer", 2, 2, 0,
724 ARG_INT|ARG_MAGIC|ACA_SRVDAYS, autoca_cf,
725 "( OLcfgOvAt:22.7 NAME 'olcACAserverDays' "
726 "DESC 'Lifetime of server certificates in days' "
727 "SYNTAX OMsInteger SINGLE-VALUE )", NULL, NULL },
728 { "caDays", "integer", 2, 2, 0,
729 ARG_INT|ARG_MAGIC|ACA_CADAYS, autoca_cf,
730 "( OLcfgOvAt:22.8 NAME 'olcACADays' "
731 "DESC 'Lifetime of CA certificate in days' "
732 "SYNTAX OMsInteger SINGLE-VALUE )", NULL, NULL },
733 { "localdn", "dn", 2, 2, 0,
734 ARG_DN|ARG_MAGIC|ACA_LOCALDN, autoca_cf,
735 "( OLcfgOvAt:22.9 NAME 'olcACAlocalDN' "
736 "DESC 'DN of local server cert' "
737 "SYNTAX OMsDN SINGLE-VALUE )", NULL, NULL },
738 { NULL, NULL, 0, 0, 0, ARG_IGNORED }
741 static ConfigOCs autoca_ocs[] = {
742 { "( OLcfgOvOc:22.1 "
743 "NAME 'olcACAConfig' "
744 "DESC 'AutoCA configuration' "
745 "SUP olcOverlayConfig "
746 "MAY ( olcACAuserClass $ olcACAserverClass $ "
747 "olcACAuserKeybits $ olcACAserverKeybits $ olcACAKeyBits $ "
748 "olcACAuserDays $ olcACAserverDays $ olcACADays $ "
750 Cft_Overlay, autoca_cfg },
760 slap_overinst *on = op->o_callback->sc_private;
761 autoca_info *ai = on->on_bi.bi_private;
765 if (rs->sr_type != REP_SEARCH)
766 return SLAP_CB_CONTINUE;
768 /* If root or self */
769 if ( !be_isroot( op ) &&
770 !dn_match( &rs->sr_entry->e_nname, &op->o_ndn ))
771 return SLAP_CB_CONTINUE;
773 isusr = is_entry_objectclass( rs->sr_entry, ai->ai_usrclass, SLAP_OCF_CHECK_SUP );
776 if (!is_entry_objectclass( rs->sr_entry, ai->ai_srvclass, SLAP_OCF_CHECK_SUP ))
777 return SLAP_CB_CONTINUE;
779 a = attr_find( rs->sr_entry->e_attrs, ad_usrPkey );
788 args.issuer_cert = ai->ai_cert;
789 args.issuer_pkey = ai->ai_pkey;
790 args.subjectDN = &rs->sr_entry->e_name;
791 args.more_exts = NULL;
794 args.cert_exts = usrExts;
795 args.keybits = ai->ai_usrkeybits;
796 args.days = ai->ai_usrdays;
797 a = attr_find( rs->sr_entry->e_attrs, ad_mail );
800 extras[0].name = "subjectAltName";
801 extras[1].name = NULL;
802 extras[0].value = op->o_tmpalloc( sizeof("email:") + a->a_vals[0].bv_len, op->o_tmpmemctx );
803 sprintf(extras[0].value, "email:%s", a->a_vals[0].bv_val);
804 args.more_exts = extras;
808 args.cert_exts = srvExts;
809 args.keybits = ai->ai_srvkeybits;
810 args.days = ai->ai_srvdays;
811 if ( ad_ipaddr && (a = attr_find( rs->sr_entry->e_attrs, ad_ipaddr )))
813 extras[0].name = "subjectAltName";
814 extras[1].name = NULL;
815 extras[0].value = op->o_tmpalloc( sizeof("IP:") + a->a_vals[0].bv_len, op->o_tmpmemctx );
816 sprintf(extras[0].value, "IP:%s", a->a_vals[0].bv_val);
817 args.more_exts = extras;
820 rc = autoca_gencert( op, &args );
822 return SLAP_CB_CONTINUE;
823 X509_free( args.newcert );
824 EVP_PKEY_free( args.newpkey );
826 if ( is_entry_objectclass( rs->sr_entry, oc_usrObj, 0 ))
830 arg2.dercert = &args.dercert;
831 arg2.derpkey = &args.derpkey;
833 arg2.dn = &rs->sr_entry->e_name;
834 arg2.ndn = &rs->sr_entry->e_nname;
837 rc = autoca_savecert( &op2, &arg2 );
840 /* If this is our cert DN, configure it */
841 if ( dn_match( &rs->sr_entry->e_nname, &ai->ai_localndn ))
842 autoca_setlocal( &op2, &args.dercert, &args.derpkey );
843 if ( !( rs->sr_flags & REP_ENTRY_MODIFIABLE ))
845 Entry *e = entry_dup( rs->sr_entry );
846 rs_replace_entry( op, rs, on, e );
847 rs->sr_flags |= REP_ENTRY_MODIFIABLE | REP_ENTRY_MUSTBEFREED;
849 attr_merge_one( rs->sr_entry, ad_usrCert, &args.dercert, NULL );
850 attr_merge_one( rs->sr_entry, ad_usrPkey, &args.derpkey, NULL );
852 op->o_tmpfree( args.dercert.bv_val, op->o_tmpmemctx );
853 op->o_tmpfree( args.derpkey.bv_val, op->o_tmpmemctx );
856 return SLAP_CB_CONTINUE;
865 /* we only act on a search that returns just our cert/key attrs */
866 if ( op->ors_attrs[0].an_desc == ad_usrCert &&
867 op->ors_attrs[1].an_desc == ad_usrPkey &&
868 op->ors_attrs[2].an_name.bv_val == NULL )
870 slap_overinst *on = (slap_overinst *)op->o_bd->bd_info;
871 slap_callback *sc = op->o_tmpcalloc( 1, sizeof(slap_callback), op->o_tmpmemctx );
872 sc->sc_response = autoca_op_response;
874 sc->sc_next = op->o_callback;
877 return SLAP_CB_CONTINUE;
886 slap_overinst *on = (slap_overinst *) be->bd_info;
889 ai = ch_calloc(1, sizeof(autoca_info));
890 on->on_bi.bi_private = ai;
893 ai->ai_usrclass = oc_find( "person" );
894 ai->ai_srvclass = oc_find( "ipHost" );
895 ai->ai_usrkeybits = KEYBITS;
896 ai->ai_srvkeybits = KEYBITS;
897 ai->ai_cakeybits = KEYBITS;
898 ai->ai_usrdays = 365; /* 1 year */
899 ai->ai_srvdays = 1826; /* 5 years */
900 ai->ai_cadays = 3652; /* 10 years */
910 slap_overinst *on = (slap_overinst *) be->bd_info;
911 autoca_info *ai = on->on_bi.bi_private;
914 X509_free( ai->ai_cert );
916 EVP_PKEY_free( ai->ai_pkey );
928 slap_overinst *on = (slap_overinst *)be->bd_info;
929 autoca_info *ai = on->on_bi.bi_private;
931 Connection conn = { 0 };
932 OperationBuffer opbuf;
939 if (slapMode & SLAP_TOOL_MODE)
942 thrctx = ldap_pvt_thread_pool_context();
943 connection_fake_init2( &conn, &opbuf, thrctx, 0 );
946 op->o_dn = be->be_rootdn;
947 op->o_ndn = be->be_rootndn;
948 rc = overlay_entry_get_ov( op, be->be_nsuffix, NULL,
952 int gotoc = 0, gotat = 0;
953 if ( is_entry_objectclass( e, oc_caObj, 0 )) {
955 a = attr_find( e->e_attrs, ad_caPkey );
957 const unsigned char *pp;
958 pp = a->a_vals[0].bv_val;
959 ai->ai_pkey = d2i_AutoPrivateKey( NULL, &pp, a->a_vals[0].bv_len );
962 a = attr_find( e->e_attrs, ad_caCert );
965 pp = a->a_vals[0].bv_val;
966 ai->ai_cert = d2i_X509( NULL, &pp, a->a_vals[0].bv_len );
967 /* If TLS wasn't configured yet, set this as our CA */
969 autoca_setca( op, a->a_vals );
975 overlay_entry_release_ov( op, e, 0, on );
976 /* generate attrs, store... */
981 args.issuer_cert = NULL;
982 args.issuer_pkey = NULL;
983 args.subjectDN = &be->be_suffix[0];
984 args.cert_exts = CAexts;
985 args.more_exts = NULL;
986 args.keybits = ai->ai_cakeybits;
987 args.days = ai->ai_cadays;
989 rc = autoca_gencert( op, &args );
993 ai->ai_cert = args.newcert;
994 ai->ai_pkey = args.newpkey;
996 arg2.dn = be->be_suffix;
997 arg2.ndn = be->be_nsuffix;
1004 arg2.dercert = &args.dercert;
1005 arg2.derpkey = &args.derpkey;
1007 autoca_savecert( op, &arg2 );
1009 /* If TLS wasn't configured yet, set this as our CA */
1010 if ( !slap_tls_ctx )
1011 autoca_setca( op, &args.dercert );
1013 op->o_tmpfree( args.dercert.bv_val, op->o_tmpmemctx );
1014 op->o_tmpfree( args.derpkey.bv_val, op->o_tmpmemctx );
1021 static slap_overinst autoca;
1023 /* This overlay is set up for dynamic loading via moduleload. For static
1024 * configuration, you'll need to arrange for the slap_overinst to be
1025 * initialized and registered by some other function inside slapd.
1028 int autoca_initialize() {
1032 autoca.on_bi.bi_type = "autoca";
1033 autoca.on_bi.bi_db_init = autoca_db_init;
1034 autoca.on_bi.bi_db_destroy = autoca_db_destroy;
1035 autoca.on_bi.bi_db_open = autoca_db_open;
1036 autoca.on_bi.bi_op_search = autoca_op_search;
1038 autoca.on_bi.bi_cf_ocs = autoca_ocs;
1039 code = config_register_schema( autoca_cfg, autoca_ocs );
1040 if ( code ) return code;
1042 for ( i=0; aca_attrs[i]; i++ ) {
1043 code = register_at( aca_attrs[i], NULL, 0 );
1044 if ( code ) return code;
1047 for ( i=0; aca_attr2[i].at; i++ ) {
1048 code = slap_str2ad( aca_attr2[i].at, aca_attr2[i].ad, &text );
1049 if ( code ) return code;
1052 /* Schema may not be loaded, ignore if missing */
1053 slap_str2ad( "ipHostNumber", &ad_ipaddr, &text );
1055 for ( i=0; aca_ocs[i].ot; i++ ) {
1056 code = register_oc( aca_ocs[i].ot, aca_ocs[i].oc, 0 );
1057 if ( code ) return code;
1060 return overlay_register( &autoca );
1063 #if SLAPD_OVER_AUTOCA == SLAPD_MOD_DYNAMIC
1065 init_module( int argc, char *argv[] )
1067 return autoca_initialize();
1071 #endif /* defined(SLAPD_OVER_AUTOCA) */