x509_v3.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. /*
  2. * Copyright 1995-2020 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License 2.0 (the "License"). You may not use
  5. * this file except in compliance with the License. You can obtain a copy
  6. * in the file LICENSE in the source distribution or at
  7. * https://www.openssl.org/source/license.html
  8. */
  9. #include <stdio.h>
  10. #include "internal/cryptlib.h"
  11. #include <openssl/safestack.h>
  12. #include <openssl/asn1.h>
  13. #include <openssl/objects.h>
  14. #include <openssl/evp.h>
  15. #include <openssl/x509.h>
  16. #include <openssl/x509v3.h>
  17. #include "x509_local.h"
  18. int X509v3_get_ext_count(const STACK_OF(X509_EXTENSION) *x)
  19. {
  20. if (x == NULL)
  21. return 0;
  22. return sk_X509_EXTENSION_num(x);
  23. }
  24. int X509v3_get_ext_by_NID(const STACK_OF(X509_EXTENSION) *x, int nid,
  25. int lastpos)
  26. {
  27. ASN1_OBJECT *obj;
  28. obj = OBJ_nid2obj(nid);
  29. if (obj == NULL)
  30. return -2;
  31. return X509v3_get_ext_by_OBJ(x, obj, lastpos);
  32. }
  33. int X509v3_get_ext_by_OBJ(const STACK_OF(X509_EXTENSION) *sk,
  34. const ASN1_OBJECT *obj, int lastpos)
  35. {
  36. int n;
  37. X509_EXTENSION *ex;
  38. if (sk == NULL)
  39. return -1;
  40. lastpos++;
  41. if (lastpos < 0)
  42. lastpos = 0;
  43. n = sk_X509_EXTENSION_num(sk);
  44. for (; lastpos < n; lastpos++) {
  45. ex = sk_X509_EXTENSION_value(sk, lastpos);
  46. if (OBJ_cmp(ex->object, obj) == 0)
  47. return lastpos;
  48. }
  49. return -1;
  50. }
  51. int X509v3_get_ext_by_critical(const STACK_OF(X509_EXTENSION) *sk, int crit,
  52. int lastpos)
  53. {
  54. int n;
  55. X509_EXTENSION *ex;
  56. if (sk == NULL)
  57. return -1;
  58. lastpos++;
  59. if (lastpos < 0)
  60. lastpos = 0;
  61. n = sk_X509_EXTENSION_num(sk);
  62. for (; lastpos < n; lastpos++) {
  63. ex = sk_X509_EXTENSION_value(sk, lastpos);
  64. if (((ex->critical > 0) && crit) || ((ex->critical <= 0) && !crit))
  65. return lastpos;
  66. }
  67. return -1;
  68. }
  69. X509_EXTENSION *X509v3_get_ext(const STACK_OF(X509_EXTENSION) *x, int loc)
  70. {
  71. if (x == NULL || sk_X509_EXTENSION_num(x) <= loc || loc < 0)
  72. return NULL;
  73. else
  74. return sk_X509_EXTENSION_value(x, loc);
  75. }
  76. X509_EXTENSION *X509v3_delete_ext(STACK_OF(X509_EXTENSION) *x, int loc)
  77. {
  78. X509_EXTENSION *ret;
  79. if (x == NULL || sk_X509_EXTENSION_num(x) <= loc || loc < 0)
  80. return NULL;
  81. ret = sk_X509_EXTENSION_delete(x, loc);
  82. return ret;
  83. }
  84. STACK_OF(X509_EXTENSION) *X509v3_add_ext(STACK_OF(X509_EXTENSION) **x,
  85. X509_EXTENSION *ex, int loc)
  86. {
  87. X509_EXTENSION *new_ex = NULL;
  88. int n;
  89. STACK_OF(X509_EXTENSION) *sk = NULL;
  90. if (x == NULL) {
  91. ERR_raise(ERR_LIB_X509, ERR_R_PASSED_NULL_PARAMETER);
  92. goto err;
  93. }
  94. if (*x == NULL) {
  95. if ((sk = sk_X509_EXTENSION_new_null()) == NULL) {
  96. ERR_raise(ERR_LIB_X509, ERR_R_CRYPTO_LIB);
  97. goto err;
  98. }
  99. } else
  100. sk = *x;
  101. n = sk_X509_EXTENSION_num(sk);
  102. if (loc > n)
  103. loc = n;
  104. else if (loc < 0)
  105. loc = n;
  106. if ((new_ex = X509_EXTENSION_dup(ex)) == NULL) {
  107. ERR_raise(ERR_LIB_X509, ERR_R_ASN1_LIB);
  108. goto err;
  109. }
  110. if (!sk_X509_EXTENSION_insert(sk, new_ex, loc)) {
  111. ERR_raise(ERR_LIB_X509, ERR_R_CRYPTO_LIB);
  112. goto err;
  113. }
  114. if (*x == NULL)
  115. *x = sk;
  116. return sk;
  117. err:
  118. X509_EXTENSION_free(new_ex);
  119. if (x != NULL && *x == NULL)
  120. sk_X509_EXTENSION_free(sk);
  121. return NULL;
  122. }
  123. X509_EXTENSION *X509_EXTENSION_create_by_NID(X509_EXTENSION **ex, int nid,
  124. int crit,
  125. ASN1_OCTET_STRING *data)
  126. {
  127. ASN1_OBJECT *obj;
  128. X509_EXTENSION *ret;
  129. obj = OBJ_nid2obj(nid);
  130. if (obj == NULL) {
  131. ERR_raise(ERR_LIB_X509, X509_R_UNKNOWN_NID);
  132. return NULL;
  133. }
  134. ret = X509_EXTENSION_create_by_OBJ(ex, obj, crit, data);
  135. if (ret == NULL)
  136. ASN1_OBJECT_free(obj);
  137. return ret;
  138. }
  139. X509_EXTENSION *X509_EXTENSION_create_by_OBJ(X509_EXTENSION **ex,
  140. const ASN1_OBJECT *obj, int crit,
  141. ASN1_OCTET_STRING *data)
  142. {
  143. X509_EXTENSION *ret;
  144. if ((ex == NULL) || (*ex == NULL)) {
  145. if ((ret = X509_EXTENSION_new()) == NULL) {
  146. ERR_raise(ERR_LIB_X509, ERR_R_ASN1_LIB);
  147. return NULL;
  148. }
  149. } else
  150. ret = *ex;
  151. if (!X509_EXTENSION_set_object(ret, obj))
  152. goto err;
  153. if (!X509_EXTENSION_set_critical(ret, crit))
  154. goto err;
  155. if (!X509_EXTENSION_set_data(ret, data))
  156. goto err;
  157. if ((ex != NULL) && (*ex == NULL))
  158. *ex = ret;
  159. return ret;
  160. err:
  161. if ((ex == NULL) || (ret != *ex))
  162. X509_EXTENSION_free(ret);
  163. return NULL;
  164. }
  165. int X509_EXTENSION_set_object(X509_EXTENSION *ex, const ASN1_OBJECT *obj)
  166. {
  167. if ((ex == NULL) || (obj == NULL))
  168. return 0;
  169. ASN1_OBJECT_free(ex->object);
  170. ex->object = OBJ_dup(obj);
  171. return ex->object != NULL;
  172. }
  173. int X509_EXTENSION_set_critical(X509_EXTENSION *ex, int crit)
  174. {
  175. if (ex == NULL)
  176. return 0;
  177. ex->critical = (crit) ? 0xFF : -1;
  178. return 1;
  179. }
  180. int X509_EXTENSION_set_data(X509_EXTENSION *ex, ASN1_OCTET_STRING *data)
  181. {
  182. int i;
  183. if (ex == NULL)
  184. return 0;
  185. i = ASN1_OCTET_STRING_set(&ex->value, data->data, data->length);
  186. if (!i)
  187. return 0;
  188. return 1;
  189. }
  190. ASN1_OBJECT *X509_EXTENSION_get_object(X509_EXTENSION *ex)
  191. {
  192. if (ex == NULL)
  193. return NULL;
  194. return ex->object;
  195. }
  196. ASN1_OCTET_STRING *X509_EXTENSION_get_data(X509_EXTENSION *ex)
  197. {
  198. if (ex == NULL)
  199. return NULL;
  200. return &ex->value;
  201. }
  202. int X509_EXTENSION_get_critical(const X509_EXTENSION *ex)
  203. {
  204. if (ex == NULL)
  205. return 0;
  206. if (ex->critical > 0)
  207. return 1;
  208. return 0;
  209. }