cert.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497
  1. /** BEGIN COPYRIGHT BLOCK
  2. * This Program is free software; you can redistribute it and/or modify it under
  3. * the terms of the GNU General Public License as published by the Free Software
  4. * Foundation; version 2 of the License.
  5. *
  6. * This Program is distributed in the hope that it will be useful, but WITHOUT
  7. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  8. * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  9. *
  10. * You should have received a copy of the GNU General Public License along with
  11. * this Program; if not, write to the Free Software Foundation, Inc., 59 Temple
  12. * Place, Suite 330, Boston, MA 02111-1307 USA.
  13. *
  14. * In addition, as a special exception, Red Hat, Inc. gives You the additional
  15. * right to link the code of this Program with code not covered under the GNU
  16. * General Public License ("Non-GPL Code") and to distribute linked combinations
  17. * including the two, subject to the limitations in this paragraph. Non-GPL Code
  18. * permitted under this exception must only link to the code of this Program
  19. * through those well defined interfaces identified in the file named EXCEPTION
  20. * found in the source code files (the "Approved Interfaces"). The files of
  21. * Non-GPL Code may instantiate templates or use macros or inline functions from
  22. * the Approved Interfaces without causing the resulting work to be covered by
  23. * the GNU General Public License. Only Red Hat, Inc. may make changes or
  24. * additions to the list of Approved Interfaces. You must obey the GNU General
  25. * Public License in all respects for all of the Program code and other code used
  26. * in conjunction with the Program except the Non-GPL Code covered by this
  27. * exception. If you modify this file, you may extend this exception to your
  28. * version of the file, but you are not obligated to do so. If you do not wish to
  29. * provide this exception without modification, you must delete this exception
  30. * statement from your version and license this file solely under the GPL without
  31. * exception.
  32. *
  33. *
  34. * Copyright (C) 2001 Sun Microsystems, Inc. Used by permission.
  35. * Copyright (C) 2005 Red Hat, Inc.
  36. * All rights reserved.
  37. * END COPYRIGHT BLOCK **/
  38. #ifdef HAVE_CONFIG_H
  39. # include <config.h>
  40. #endif
  41. #include <string.h>
  42. #include <malloc.h>
  43. /* removed for ns security integration
  44. #include <sec.h>
  45. */
  46. #include "prmem.h"
  47. #include <key.h>
  48. #include <cert.h>
  49. #include <nss.h>
  50. #include <ldaputil/certmap.h>
  51. #include <ldaputil/errors.h>
  52. #include <ldaputil/cert.h>
  53. #include "ldaputili.h"
  54. #include "slapi-plugin.h"
  55. NSAPI_PUBLIC int ldapu_get_cert (void *SSLendpoint, void **cert)
  56. {
  57. /* TEMPORARY -- not implemented yet*/
  58. return LDAPU_FAILED;
  59. }
  60. NSAPI_PUBLIC int ldapu_get_cert_subject_dn (void *cert_in, char **subjectDN)
  61. {
  62. CERTCertificate *cert = (CERTCertificate *)cert_in;
  63. char *cert_subject = CERT_NameToAscii(&cert->subject);
  64. if (cert_subject != NULL)
  65. *subjectDN = strdup(cert_subject);
  66. else
  67. *subjectDN=NULL;
  68. PR_Free(cert_subject);
  69. return *subjectDN ? LDAPU_SUCCESS : LDAPU_ERR_EXTRACT_SUBJECTDN_FAILED;
  70. }
  71. NSAPI_PUBLIC int ldapu_get_cert_issuer_dn (void *cert_in, char **issuerDN)
  72. {
  73. CERTCertificate *cert = (CERTCertificate *)cert_in;
  74. char *cert_issuer = CERT_NameToAscii(&cert->issuer);
  75. *issuerDN = strdup(cert_issuer);
  76. PR_Free(cert_issuer);
  77. return *issuerDN ? LDAPU_SUCCESS : LDAPU_ERR_EXTRACT_ISSUERDN_FAILED;
  78. }
  79. NSAPI_PUBLIC int ldapu_get_cert_der (void *cert_in, unsigned char **der,
  80. unsigned int *len)
  81. {
  82. CERTCertificate *cert = (CERTCertificate *)cert_in;
  83. SECItem derCert = ((CERTCertificate*)cert)->derCert;
  84. unsigned char *data = derCert.data;
  85. *len = derCert.len;
  86. *der = (unsigned char *)malloc(*len);
  87. if (!*der) return LDAPU_ERR_OUT_OF_MEMORY;
  88. memcpy(*der, data, *len);
  89. return *len ? LDAPU_SUCCESS : LDAPU_ERR_EXTRACT_DERCERT_FAILED;
  90. }
  91. static int certmap_name_to_secoid (const char *str)
  92. {
  93. if (!ldapu_strcasecmp(str, "c")) return SEC_OID_AVA_COUNTRY_NAME;
  94. if (!ldapu_strcasecmp(str, "o")) return SEC_OID_AVA_ORGANIZATION_NAME;
  95. if (!ldapu_strcasecmp(str, "cn")) return SEC_OID_AVA_COMMON_NAME;
  96. if (!ldapu_strcasecmp(str, "l")) return SEC_OID_AVA_LOCALITY;
  97. if (!ldapu_strcasecmp(str, "st")) return SEC_OID_AVA_STATE_OR_PROVINCE;
  98. if (!ldapu_strcasecmp(str, "ou")) return SEC_OID_AVA_ORGANIZATIONAL_UNIT_NAME;
  99. if (!ldapu_strcasecmp(str, "uid")) return SEC_OID_RFC1274_UID;
  100. if (!ldapu_strcasecmp(str, "e")) return SEC_OID_PKCS9_EMAIL_ADDRESS;
  101. if (!ldapu_strcasecmp(str, "mail")) return SEC_OID_RFC1274_MAIL;
  102. if (!ldapu_strcasecmp(str, "dc")) return SEC_OID_AVA_DC;
  103. return SEC_OID_AVA_UNKNOWN; /* return invalid OID */
  104. }
  105. NSAPI_PUBLIC int ldapu_get_cert_ava_val (void *cert_in, int which_dn,
  106. const char *attr, char ***val_out)
  107. {
  108. CERTCertificate *cert = (CERTCertificate *)cert_in;
  109. CERTName *cert_dn;
  110. CERTRDN **rdns;
  111. CERTRDN **rdn;
  112. CERTAVA **avas;
  113. CERTAVA *ava;
  114. int attr_tag = certmap_name_to_secoid(attr);
  115. char **val;
  116. char **ptr;
  117. int rv;
  118. *val_out = 0;
  119. if (attr_tag == SEC_OID_AVA_UNKNOWN) {
  120. return LDAPU_ERR_INVALID_ARGUMENT;
  121. }
  122. if (which_dn == LDAPU_SUBJECT_DN)
  123. cert_dn = &cert->subject;
  124. else if (which_dn == LDAPU_ISSUER_DN)
  125. cert_dn = &cert->issuer;
  126. else
  127. return LDAPU_ERR_INVALID_ARGUMENT;
  128. val = (char **)malloc(32*sizeof(char *));
  129. if (!val) return LDAPU_ERR_OUT_OF_MEMORY;
  130. ptr = val;
  131. rdns = cert_dn->rdns;
  132. if (rdns) {
  133. for (rdn = rdns; *rdn; rdn++) {
  134. avas = (*rdn)->avas;
  135. while ((ava = *avas++) != NULL) {
  136. int tag = CERT_GetAVATag(ava);
  137. if (tag == attr_tag) {
  138. char buf[BIG_LINE];
  139. int lenLen;
  140. int vallen;
  141. /* Found it */
  142. /* Copied from ns/lib/libsec ...
  143. * XXX this code is incorrect in general
  144. * -- should use a DER template.
  145. */
  146. lenLen = 2;
  147. if (ava->value.len >= 128) lenLen = 3;
  148. vallen = ava->value.len - lenLen;
  149. rv = CERT_RFC1485_EscapeAndQuote(buf,
  150. BIG_LINE,
  151. (char*) ava->value.data + lenLen,
  152. vallen);
  153. if (rv == SECSuccess) {
  154. *ptr++ = strdup(buf);
  155. }
  156. break;
  157. }
  158. }
  159. }
  160. }
  161. *ptr = 0;
  162. if (*val) {
  163. /* At least one value found */
  164. *val_out = val;
  165. rv = LDAPU_SUCCESS;
  166. }
  167. else {
  168. free(val);
  169. rv = LDAPU_FAILED;
  170. }
  171. return rv;
  172. }
  173. static void
  174. _rdns_free (char*** rdns)
  175. {
  176. auto char*** rdn;
  177. for (rdn = rdns; *rdn; ++rdn) {
  178. slapi_ldap_value_free (*rdn);
  179. }
  180. free (rdns);
  181. }
  182. static char***
  183. _explode_dn (const char* dn)
  184. {
  185. auto char*** exp = NULL;
  186. if (dn && *dn) {
  187. auto char** rdns = slapi_ldap_explode_dn (dn, 0);
  188. if (rdns) {
  189. auto size_t expLen = 0;
  190. auto char** rdn;
  191. for (rdn = rdns; *rdn; ++rdn) {
  192. auto char** avas = slapi_ldap_explode_rdn (*rdn, 0);
  193. if (avas && *avas) {
  194. exp = (char***) ldapu_realloc (exp, sizeof(char**) * (expLen + 2));
  195. if (exp) {
  196. exp[expLen++] = avas;
  197. } else {
  198. slapi_ldap_value_free (avas);
  199. break;
  200. }
  201. } else { /* parse error */
  202. if (avas) {
  203. slapi_ldap_value_free (avas);
  204. }
  205. if (exp) {
  206. exp[expLen] = NULL;
  207. _rdns_free (exp);
  208. exp = NULL;
  209. }
  210. break;
  211. }
  212. }
  213. if (exp) {
  214. exp[expLen] = NULL;
  215. }
  216. slapi_ldap_value_free (rdns);
  217. }
  218. }
  219. return exp;
  220. }
  221. static size_t
  222. _rdns_count (char*** rdns)
  223. {
  224. auto size_t count = 0;
  225. auto char*** rdn;
  226. for (rdn = rdns; *rdn; ++rdn) {
  227. auto char** ava;
  228. for (ava = *rdn; *ava; ++ava) {
  229. ++count;
  230. }
  231. }
  232. return count;
  233. }
  234. static int
  235. _replaceAVA (char* attr, char** avas)
  236. {
  237. if (attr && avas) {
  238. for (; *avas; ++avas) {
  239. if (!ldapu_strcasecmp (*avas, attr)) {
  240. *avas = attr;
  241. return 1;
  242. }
  243. }
  244. }
  245. return 0;
  246. }
  247. struct _attr_getter_pair {
  248. #if NSS_VMAJOR < 3 || (NSS_VMAJOR == 3 && NSS_VMINOR < 15)
  249. char* (*getter) ( CERTName* dn);
  250. #else
  251. /* in 3.15.x "const" was added to the declarations */
  252. char* (*getter) (const CERTName* dn);
  253. #endif
  254. const char* name1;
  255. const char* name2;
  256. } _attr_getter_table[] =
  257. {
  258. {NULL, "OU", "organizationalUnitName"},
  259. {CERT_GetOrgName, "O", "organizationName"},
  260. {CERT_GetCommonName, "CN", "commonName"},
  261. {CERT_GetCertEmailAddress, "E", NULL},
  262. {CERT_GetCertEmailAddress, "MAIL", "rfc822mailbox"},
  263. {CERT_GetCertUid, "uid", NULL},
  264. {CERT_GetCountryName, "C", "country"},
  265. {CERT_GetStateName, "ST", "state"},
  266. {CERT_GetLocalityName, "L", "localityName"},
  267. {CERT_GetDomainComponentName, "DC", "dc"},
  268. {NULL, NULL, NULL}
  269. };
  270. static int
  271. _is_OU (const char* attr)
  272. {
  273. auto struct _attr_getter_pair* descAttr;
  274. for (descAttr = _attr_getter_table; descAttr->name1; ++descAttr) {
  275. if (descAttr->getter == NULL) { /* OU attribute */
  276. if (!ldapu_strcasecmp (attr, descAttr->name1) || (descAttr->name2 &&
  277. !ldapu_strcasecmp (attr, descAttr->name2))) {
  278. return 1;
  279. }
  280. break;
  281. }
  282. }
  283. return 0;
  284. }
  285. static char**
  286. _previous_OU (char** ava, char** avas)
  287. {
  288. while (ava != avas) {
  289. --ava;
  290. if (_is_OU (*ava)) {
  291. return ava;
  292. }
  293. }
  294. return NULL;
  295. }
  296. static char*
  297. _value_normalize (char* value)
  298. /* Remove leading and trailing spaces, and
  299. change consecutive spaces to a single space.
  300. */
  301. {
  302. auto char* t;
  303. auto char* f;
  304. t = f = value;
  305. while (*f == ' ') ++f; /* ignore leading spaces */
  306. for (; *f; ++f) {
  307. if (*f != ' ' || t[-1] != ' ') {
  308. *t++ = *f; /* no consecutive spaces */
  309. }
  310. }
  311. if (t > value && t[-1] == ' ') {
  312. --t; /* ignore trailing space */
  313. }
  314. *t = '\0';
  315. return value;
  316. }
  317. static int
  318. _explode_AVA (char* AVA)
  319. /* Change an attributeTypeAndValue a la <draft-ietf-asid-ldapv3-dn>,
  320. to the type name, followed immediately by the attribute value,
  321. both normalized.
  322. */
  323. {
  324. auto char* value = strchr (AVA, '=');
  325. if (!value) return LDAPU_FAILED;
  326. *value++ = '\0';
  327. _value_normalize (AVA);
  328. _value_normalize (value);
  329. {
  330. auto char* typeEnd = AVA + strlen (AVA);
  331. if ((typeEnd + 1) != value) {
  332. memmove (typeEnd+1, value, strlen(value)+1);
  333. }
  334. }
  335. return LDAPU_SUCCESS;
  336. }
  337. static char*
  338. _AVA_value (char* AVA)
  339. {
  340. return (AVA + strlen (AVA) + 1);
  341. }
  342. static int
  343. _value_match (char* value, char* desc)
  344. {
  345. auto const int result =
  346. !ldapu_strcasecmp (_value_normalize(value), desc);
  347. return result;
  348. }
  349. int
  350. ldapu_member_certificate_match (void* cert, const char* desc)
  351. /*
  352. * Return Values: (same as ldapu_find)
  353. * LDAPU_SUCCESS cert matches desc
  354. * LDAPU_FAILED cert doesn't match desc
  355. * <rv> Something went wrong.
  356. */
  357. {
  358. auto int err = LDAPU_FAILED;
  359. auto char*** descRDNs;
  360. if (!cert || !desc || desc[0] != '{') return LDAPU_FAILED;
  361. if (desc[1] == '\0') return LDAPU_SUCCESS; /* no AVAs */
  362. descRDNs = _explode_dn (desc+1);
  363. if (descRDNs) {
  364. auto char** descAVAs = (char**)ldapu_malloc(sizeof(char*) * (_rdns_count(descRDNs)+1));
  365. if (!descAVAs) {
  366. err = LDAPU_ERR_OUT_OF_MEMORY;
  367. } else {
  368. auto CERTName* subject = &(((CERTCertificate*)cert)->subject);
  369. auto char** descAVA;
  370. err = LDAPU_SUCCESS;
  371. { /* extract all the AVAs, but not duplicate types, except OU */
  372. auto size_t descAVAsLen = 0;
  373. auto char*** descRDN;
  374. descAVAs[0] = NULL;
  375. for (descRDN = descRDNs; err == LDAPU_SUCCESS && *descRDN; ++descRDN) {
  376. for (descAVA = *descRDN; err == LDAPU_SUCCESS && *descAVA; ++descAVA) {
  377. err = _explode_AVA (*descAVA);
  378. if (err == LDAPU_SUCCESS) {
  379. if (_is_OU (*descAVA) ||
  380. !_replaceAVA (*descAVA, descAVAs)) {
  381. descAVAs[descAVAsLen++] = *descAVA;
  382. descAVAs[descAVAsLen] = NULL;
  383. }
  384. }
  385. }
  386. }
  387. }
  388. /* match all the attributes except OU */
  389. for (descAVA = descAVAs; err == LDAPU_SUCCESS && *descAVA; ++descAVA) {
  390. auto struct _attr_getter_pair* descAttr;
  391. err = LDAPU_FAILED; /* if no match */
  392. for (descAttr = _attr_getter_table; descAttr->name1; ++descAttr) {
  393. if (!ldapu_strcasecmp (*descAVA, descAttr->name1) || (descAttr->name2 &&
  394. !ldapu_strcasecmp (*descAVA, descAttr->name2))) {
  395. if (descAttr->getter == NULL) { /* OU attribute */
  396. err = LDAPU_SUCCESS; /* for now */
  397. } else {
  398. auto char* certVal = (*(descAttr->getter))(subject);
  399. if (certVal && _value_match (certVal, _AVA_value (*descAVA))) {
  400. err = LDAPU_SUCCESS;
  401. }
  402. PR_Free (certVal);
  403. }
  404. break;
  405. }
  406. }
  407. }
  408. /* match the OU attributes */
  409. if (err == LDAPU_SUCCESS && descAVA != descAVAs) {
  410. /* Iterate over the OUs in the certificate subject */
  411. auto CERTRDN** certRDN = subject->rdns;
  412. descAVA = _previous_OU (descAVA, descAVAs);
  413. for (; descAVA && *certRDN; ++certRDN) {
  414. auto CERTAVA** certAVA = (*certRDN)->avas;
  415. for (; descAVA && *certAVA; ++certAVA) {
  416. auto const int tag = CERT_GetAVATag (*certAVA);
  417. if (tag == SEC_OID_AVA_ORGANIZATIONAL_UNIT_NAME) {
  418. auto const size_t certValLen =(*certAVA)->value.len;
  419. auto const size_t lenLen = (certValLen < 128) ? 2 : 3;
  420. auto const size_t buflen = certValLen - lenLen;
  421. auto char* buf = (char*)ldapu_malloc(buflen+1);
  422. if (!buf) {
  423. err = LDAPU_ERR_OUT_OF_MEMORY;
  424. descAVA = NULL;
  425. } else {
  426. memcpy (buf, (*certAVA)->value.data+lenLen, buflen);
  427. buf[buflen] = 0;
  428. if (_value_match (buf, _AVA_value (*descAVA))) {
  429. descAVA = _previous_OU (descAVA, descAVAs);
  430. }
  431. free (buf);
  432. }
  433. }
  434. }
  435. }
  436. if (descAVA) {
  437. err = LDAPU_FAILED; /* no match for descAVA in subject */
  438. }
  439. }
  440. free (descAVAs);
  441. }
  442. _rdns_free (descRDNs);
  443. }
  444. return err;
  445. }