1
0

cert.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491
  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 <ldaputil/certmap.h>
  50. #include <ldaputil/errors.h>
  51. #include <ldaputil/cert.h>
  52. #include "ldaputili.h"
  53. #include "slapi-plugin.h"
  54. NSAPI_PUBLIC int ldapu_get_cert (void *SSLendpoint, void **cert)
  55. {
  56. /* TEMPORARY -- not implemented yet*/
  57. return LDAPU_FAILED;
  58. }
  59. NSAPI_PUBLIC int ldapu_get_cert_subject_dn (void *cert_in, char **subjectDN)
  60. {
  61. CERTCertificate *cert = (CERTCertificate *)cert_in;
  62. char *cert_subject = CERT_NameToAscii(&cert->subject);
  63. if (cert_subject != NULL)
  64. *subjectDN = strdup(cert_subject);
  65. else
  66. *subjectDN=NULL;
  67. PR_Free(cert_subject);
  68. return *subjectDN ? LDAPU_SUCCESS : LDAPU_ERR_EXTRACT_SUBJECTDN_FAILED;
  69. }
  70. NSAPI_PUBLIC int ldapu_get_cert_issuer_dn (void *cert_in, char **issuerDN)
  71. {
  72. CERTCertificate *cert = (CERTCertificate *)cert_in;
  73. char *cert_issuer = CERT_NameToAscii(&cert->issuer);
  74. *issuerDN = strdup(cert_issuer);
  75. PR_Free(cert_issuer);
  76. return *issuerDN ? LDAPU_SUCCESS : LDAPU_ERR_EXTRACT_ISSUERDN_FAILED;
  77. }
  78. NSAPI_PUBLIC int ldapu_get_cert_der (void *cert_in, unsigned char **der,
  79. unsigned int *len)
  80. {
  81. CERTCertificate *cert = (CERTCertificate *)cert_in;
  82. SECItem derCert = ((CERTCertificate*)cert)->derCert;
  83. unsigned char *data = derCert.data;
  84. *len = derCert.len;
  85. *der = (unsigned char *)malloc(*len);
  86. if (!*der) return LDAPU_ERR_OUT_OF_MEMORY;
  87. memcpy(*der, data, *len);
  88. return *len ? LDAPU_SUCCESS : LDAPU_ERR_EXTRACT_DERCERT_FAILED;
  89. }
  90. static int certmap_name_to_secoid (const char *str)
  91. {
  92. if (!ldapu_strcasecmp(str, "c")) return SEC_OID_AVA_COUNTRY_NAME;
  93. if (!ldapu_strcasecmp(str, "o")) return SEC_OID_AVA_ORGANIZATION_NAME;
  94. if (!ldapu_strcasecmp(str, "cn")) return SEC_OID_AVA_COMMON_NAME;
  95. if (!ldapu_strcasecmp(str, "l")) return SEC_OID_AVA_LOCALITY;
  96. if (!ldapu_strcasecmp(str, "st")) return SEC_OID_AVA_STATE_OR_PROVINCE;
  97. if (!ldapu_strcasecmp(str, "ou")) return SEC_OID_AVA_ORGANIZATIONAL_UNIT_NAME;
  98. if (!ldapu_strcasecmp(str, "uid")) return SEC_OID_RFC1274_UID;
  99. if (!ldapu_strcasecmp(str, "e")) return SEC_OID_PKCS9_EMAIL_ADDRESS;
  100. if (!ldapu_strcasecmp(str, "mail")) return SEC_OID_RFC1274_MAIL;
  101. if (!ldapu_strcasecmp(str, "dc")) return SEC_OID_AVA_DC;
  102. return SEC_OID_AVA_UNKNOWN; /* return invalid OID */
  103. }
  104. NSAPI_PUBLIC int ldapu_get_cert_ava_val (void *cert_in, int which_dn,
  105. const char *attr, char ***val_out)
  106. {
  107. CERTCertificate *cert = (CERTCertificate *)cert_in;
  108. CERTName *cert_dn;
  109. CERTRDN **rdns;
  110. CERTRDN **rdn;
  111. CERTAVA **avas;
  112. CERTAVA *ava;
  113. int attr_tag = certmap_name_to_secoid(attr);
  114. char **val;
  115. char **ptr;
  116. int rv;
  117. *val_out = 0;
  118. if (attr_tag == SEC_OID_AVA_UNKNOWN) {
  119. return LDAPU_ERR_INVALID_ARGUMENT;
  120. }
  121. if (which_dn == LDAPU_SUBJECT_DN)
  122. cert_dn = &cert->subject;
  123. else if (which_dn == LDAPU_ISSUER_DN)
  124. cert_dn = &cert->issuer;
  125. else
  126. return LDAPU_ERR_INVALID_ARGUMENT;
  127. val = (char **)malloc(32*sizeof(char *));
  128. if (!val) return LDAPU_ERR_OUT_OF_MEMORY;
  129. ptr = val;
  130. rdns = cert_dn->rdns;
  131. if (rdns) {
  132. for (rdn = rdns; *rdn; rdn++) {
  133. avas = (*rdn)->avas;
  134. while ((ava = *avas++) != NULL) {
  135. int tag = CERT_GetAVATag(ava);
  136. if (tag == attr_tag) {
  137. char buf[BIG_LINE];
  138. int lenLen;
  139. int vallen;
  140. /* Found it */
  141. /* Copied from ns/lib/libsec ...
  142. * XXX this code is incorrect in general
  143. * -- should use a DER template.
  144. */
  145. lenLen = 2;
  146. if (ava->value.len >= 128) lenLen = 3;
  147. vallen = ava->value.len - lenLen;
  148. rv = CERT_RFC1485_EscapeAndQuote(buf,
  149. BIG_LINE,
  150. (char*) ava->value.data + lenLen,
  151. vallen);
  152. if (rv == SECSuccess) {
  153. *ptr++ = strdup(buf);
  154. }
  155. break;
  156. }
  157. }
  158. }
  159. }
  160. *ptr = 0;
  161. if (*val) {
  162. /* At least one value found */
  163. *val_out = val;
  164. rv = LDAPU_SUCCESS;
  165. }
  166. else {
  167. free(val);
  168. rv = LDAPU_FAILED;
  169. }
  170. return rv;
  171. }
  172. static void
  173. _rdns_free (char*** rdns)
  174. {
  175. auto char*** rdn;
  176. for (rdn = rdns; *rdn; ++rdn) {
  177. slapi_ldap_value_free (*rdn);
  178. }
  179. free (rdns);
  180. }
  181. static char***
  182. _explode_dn (const char* dn)
  183. {
  184. auto char*** exp = NULL;
  185. if (dn && *dn) {
  186. auto char** rdns = slapi_ldap_explode_dn (dn, 0);
  187. if (rdns) {
  188. auto size_t expLen = 0;
  189. auto char** rdn;
  190. for (rdn = rdns; *rdn; ++rdn) {
  191. auto char** avas = slapi_ldap_explode_rdn (*rdn, 0);
  192. if (avas && *avas) {
  193. exp = (char***) ldapu_realloc (exp, sizeof(char**) * (expLen + 2));
  194. if (exp) {
  195. exp[expLen++] = avas;
  196. } else {
  197. slapi_ldap_value_free (avas);
  198. break;
  199. }
  200. } else { /* parse error */
  201. if (avas) {
  202. slapi_ldap_value_free (avas);
  203. }
  204. if (exp) {
  205. exp[expLen] = NULL;
  206. _rdns_free (exp);
  207. exp = NULL;
  208. }
  209. break;
  210. }
  211. }
  212. if (exp) {
  213. exp[expLen] = NULL;
  214. }
  215. slapi_ldap_value_free (rdns);
  216. }
  217. }
  218. return exp;
  219. }
  220. static size_t
  221. _rdns_count (char*** rdns)
  222. {
  223. auto size_t count = 0;
  224. auto char*** rdn;
  225. for (rdn = rdns; *rdn; ++rdn) {
  226. auto char** ava;
  227. for (ava = *rdn; *ava; ++ava) {
  228. ++count;
  229. }
  230. }
  231. return count;
  232. }
  233. static int
  234. _replaceAVA (char* attr, char** avas)
  235. {
  236. if (attr && avas) {
  237. for (; *avas; ++avas) {
  238. if (!ldapu_strcasecmp (*avas, attr)) {
  239. *avas = attr;
  240. return 1;
  241. }
  242. }
  243. }
  244. return 0;
  245. }
  246. struct _attr_getter_pair {
  247. char* (*getter) (CERTName* dn);
  248. const char* name1;
  249. const char* name2;
  250. } _attr_getter_table[] =
  251. {
  252. {NULL, "OU", "organizationalUnitName"},
  253. {CERT_GetOrgName, "O", "organizationName"},
  254. {CERT_GetCommonName, "CN", "commonName"},
  255. {CERT_GetCertEmailAddress, "E", NULL},
  256. {CERT_GetCertEmailAddress, "MAIL", "rfc822mailbox"},
  257. {CERT_GetCertUid, "uid", NULL},
  258. {CERT_GetCountryName, "C", "country"},
  259. {CERT_GetStateName, "ST", "state"},
  260. {CERT_GetLocalityName, "L", "localityName"},
  261. {CERT_GetDomainComponentName, "DC", "dc"},
  262. {NULL, NULL, NULL}
  263. };
  264. static int
  265. _is_OU (const char* attr)
  266. {
  267. auto struct _attr_getter_pair* descAttr;
  268. for (descAttr = _attr_getter_table; descAttr->name1; ++descAttr) {
  269. if (descAttr->getter == NULL) { /* OU attribute */
  270. if (!ldapu_strcasecmp (attr, descAttr->name1) || (descAttr->name2 &&
  271. !ldapu_strcasecmp (attr, descAttr->name2))) {
  272. return 1;
  273. }
  274. break;
  275. }
  276. }
  277. return 0;
  278. }
  279. static char**
  280. _previous_OU (char** ava, char** avas)
  281. {
  282. while (ava != avas) {
  283. --ava;
  284. if (_is_OU (*ava)) {
  285. return ava;
  286. }
  287. }
  288. return NULL;
  289. }
  290. static char*
  291. _value_normalize (char* value)
  292. /* Remove leading and trailing spaces, and
  293. change consecutive spaces to a single space.
  294. */
  295. {
  296. auto char* t;
  297. auto char* f;
  298. t = f = value;
  299. while (*f == ' ') ++f; /* ignore leading spaces */
  300. for (; *f; ++f) {
  301. if (*f != ' ' || t[-1] != ' ') {
  302. *t++ = *f; /* no consecutive spaces */
  303. }
  304. }
  305. if (t > value && t[-1] == ' ') {
  306. --t; /* ignore trailing space */
  307. }
  308. *t = '\0';
  309. return value;
  310. }
  311. static int
  312. _explode_AVA (char* AVA)
  313. /* Change an attributeTypeAndValue a la <draft-ietf-asid-ldapv3-dn>,
  314. to the type name, followed immediately by the attribute value,
  315. both normalized.
  316. */
  317. {
  318. auto char* value = strchr (AVA, '=');
  319. if (!value) return LDAPU_FAILED;
  320. *value++ = '\0';
  321. _value_normalize (AVA);
  322. _value_normalize (value);
  323. {
  324. auto char* typeEnd = AVA + strlen (AVA);
  325. if ((typeEnd + 1) != value) {
  326. memmove (typeEnd+1, value, strlen(value)+1);
  327. }
  328. }
  329. return LDAPU_SUCCESS;
  330. }
  331. static char*
  332. _AVA_value (char* AVA)
  333. {
  334. return (AVA + strlen (AVA) + 1);
  335. }
  336. static int
  337. _value_match (char* value, char* desc)
  338. {
  339. auto const int result =
  340. !ldapu_strcasecmp (_value_normalize(value), desc);
  341. return result;
  342. }
  343. int
  344. ldapu_member_certificate_match (void* cert, const char* desc)
  345. /*
  346. * Return Values: (same as ldapu_find)
  347. * LDAPU_SUCCESS cert matches desc
  348. * LDAPU_FAILED cert doesn't match desc
  349. * <rv> Something went wrong.
  350. */
  351. {
  352. auto int err = LDAPU_FAILED;
  353. auto char*** descRDNs;
  354. if (!cert || !desc || desc[0] != '{') return LDAPU_FAILED;
  355. if (desc[1] == '\0') return LDAPU_SUCCESS; /* no AVAs */
  356. descRDNs = _explode_dn (desc+1);
  357. if (descRDNs) {
  358. auto char** descAVAs = (char**)ldapu_malloc(sizeof(char*) * (_rdns_count(descRDNs)+1));
  359. if (!descAVAs) {
  360. err = LDAPU_ERR_OUT_OF_MEMORY;
  361. } else {
  362. auto CERTName* subject = &(((CERTCertificate*)cert)->subject);
  363. auto char** descAVA;
  364. err = LDAPU_SUCCESS;
  365. { /* extract all the AVAs, but not duplicate types, except OU */
  366. auto size_t descAVAsLen = 0;
  367. auto char*** descRDN;
  368. descAVAs[0] = NULL;
  369. for (descRDN = descRDNs; err == LDAPU_SUCCESS && *descRDN; ++descRDN) {
  370. for (descAVA = *descRDN; err == LDAPU_SUCCESS && *descAVA; ++descAVA) {
  371. err = _explode_AVA (*descAVA);
  372. if (err == LDAPU_SUCCESS) {
  373. if (_is_OU (*descAVA) ||
  374. !_replaceAVA (*descAVA, descAVAs)) {
  375. descAVAs[descAVAsLen++] = *descAVA;
  376. descAVAs[descAVAsLen] = NULL;
  377. }
  378. }
  379. }
  380. }
  381. }
  382. /* match all the attributes except OU */
  383. for (descAVA = descAVAs; err == LDAPU_SUCCESS && *descAVA; ++descAVA) {
  384. auto struct _attr_getter_pair* descAttr;
  385. err = LDAPU_FAILED; /* if no match */
  386. for (descAttr = _attr_getter_table; descAttr->name1; ++descAttr) {
  387. if (!ldapu_strcasecmp (*descAVA, descAttr->name1) || (descAttr->name2 &&
  388. !ldapu_strcasecmp (*descAVA, descAttr->name2))) {
  389. if (descAttr->getter == NULL) { /* OU attribute */
  390. err = LDAPU_SUCCESS; /* for now */
  391. } else {
  392. auto char* certVal = (*(descAttr->getter))(subject);
  393. if (certVal && _value_match (certVal, _AVA_value (*descAVA))) {
  394. err = LDAPU_SUCCESS;
  395. }
  396. PR_Free (certVal);
  397. }
  398. break;
  399. }
  400. }
  401. }
  402. /* match the OU attributes */
  403. if (err == LDAPU_SUCCESS && descAVA != descAVAs) {
  404. /* Iterate over the OUs in the certificate subject */
  405. auto CERTRDN** certRDN = subject->rdns;
  406. descAVA = _previous_OU (descAVA, descAVAs);
  407. for (; descAVA && *certRDN; ++certRDN) {
  408. auto CERTAVA** certAVA = (*certRDN)->avas;
  409. for (; descAVA && *certAVA; ++certAVA) {
  410. auto const int tag = CERT_GetAVATag (*certAVA);
  411. if (tag == SEC_OID_AVA_ORGANIZATIONAL_UNIT_NAME) {
  412. auto const size_t certValLen =(*certAVA)->value.len;
  413. auto const size_t lenLen = (certValLen < 128) ? 2 : 3;
  414. auto const size_t buflen = certValLen - lenLen;
  415. auto char* buf = (char*)ldapu_malloc(buflen+1);
  416. if (!buf) {
  417. err = LDAPU_ERR_OUT_OF_MEMORY;
  418. descAVA = NULL;
  419. } else {
  420. memcpy (buf, (*certAVA)->value.data+lenLen, buflen);
  421. buf[buflen] = 0;
  422. if (_value_match (buf, _AVA_value (*descAVA))) {
  423. descAVA = _previous_OU (descAVA, descAVAs);
  424. }
  425. free (buf);
  426. }
  427. }
  428. }
  429. }
  430. if (descAVA) {
  431. err = LDAPU_FAILED; /* no match for descAVA in subject */
  432. }
  433. }
  434. free (descAVAs);
  435. }
  436. _rdns_free (descRDNs);
  437. }
  438. return err;
  439. }