cert.c 13 KB

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