You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

401 lines
14 KiB

  1. /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
  2. * All rights reserved.
  3. *
  4. * This package is an SSL implementation written
  5. * by Eric Young (eay@cryptsoft.com).
  6. * The implementation was written so as to conform with Netscapes SSL.
  7. *
  8. * This library is free for commercial and non-commercial use as long as
  9. * the following conditions are aheared to. The following conditions
  10. * apply to all code found in this distribution, be it the RC4, RSA,
  11. * lhash, DES, etc., code; not just the SSL code. The SSL documentation
  12. * included with this distribution is covered by the same copyright terms
  13. * except that the holder is Tim Hudson (tjh@cryptsoft.com).
  14. *
  15. * Copyright remains Eric Young's, and as such any Copyright notices in
  16. * the code are not to be removed.
  17. * If this package is used in a product, Eric Young should be given attribution
  18. * as the author of the parts of the library used.
  19. * This can be in the form of a textual message at program startup or
  20. * in documentation (online or textual) provided with the package.
  21. *
  22. * Redistribution and use in source and binary forms, with or without
  23. * modification, are permitted provided that the following conditions
  24. * are met:
  25. * 1. Redistributions of source code must retain the copyright
  26. * notice, this list of conditions and the following disclaimer.
  27. * 2. Redistributions in binary form must reproduce the above copyright
  28. * notice, this list of conditions and the following disclaimer in the
  29. * documentation and/or other materials provided with the distribution.
  30. * 3. All advertising materials mentioning features or use of this software
  31. * must display the following acknowledgement:
  32. * "This product includes cryptographic software written by
  33. * Eric Young (eay@cryptsoft.com)"
  34. * The word 'cryptographic' can be left out if the rouines from the library
  35. * being used are not cryptographic related :-).
  36. * 4. If you include any Windows specific code (or a derivative thereof) from
  37. * the apps directory (application code) you must include an acknowledgement:
  38. * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
  39. *
  40. * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
  41. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  42. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  43. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  44. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  45. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  46. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  47. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  48. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  49. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  50. * SUCH DAMAGE.
  51. *
  52. * The licence and distribution terms for any publically available version or
  53. * derivative of this code cannot be changed. i.e. this code cannot simply be
  54. * copied and put under another distribution licence
  55. * [including the GNU Public Licence.] */
  56. #include <openssl/x509.h>
  57. #include <limits.h>
  58. #include <openssl/asn1.h>
  59. #include <openssl/buf.h>
  60. #include <openssl/digest.h>
  61. #include <openssl/dsa.h>
  62. #include <openssl/evp.h>
  63. #include <openssl/mem.h>
  64. #include <openssl/rsa.h>
  65. #include <openssl/stack.h>
  66. int X509_verify(X509 *a, EVP_PKEY *r)
  67. {
  68. if (X509_ALGOR_cmp(a->sig_alg, a->cert_info->signature)) {
  69. OPENSSL_PUT_ERROR(X509, X509_R_SIGNATURE_ALGORITHM_MISMATCH);
  70. return 0;
  71. }
  72. return (ASN1_item_verify(ASN1_ITEM_rptr(X509_CINF), a->sig_alg,
  73. a->signature, a->cert_info, r));
  74. }
  75. int X509_REQ_verify(X509_REQ *a, EVP_PKEY *r)
  76. {
  77. return (ASN1_item_verify(ASN1_ITEM_rptr(X509_REQ_INFO),
  78. a->sig_alg, a->signature, a->req_info, r));
  79. }
  80. int X509_sign(X509 *x, EVP_PKEY *pkey, const EVP_MD *md)
  81. {
  82. x->cert_info->enc.modified = 1;
  83. return (ASN1_item_sign(ASN1_ITEM_rptr(X509_CINF), x->cert_info->signature,
  84. x->sig_alg, x->signature, x->cert_info, pkey, md));
  85. }
  86. int X509_sign_ctx(X509 *x, EVP_MD_CTX *ctx)
  87. {
  88. x->cert_info->enc.modified = 1;
  89. return ASN1_item_sign_ctx(ASN1_ITEM_rptr(X509_CINF),
  90. x->cert_info->signature,
  91. x->sig_alg, x->signature, x->cert_info, ctx);
  92. }
  93. int X509_REQ_sign(X509_REQ *x, EVP_PKEY *pkey, const EVP_MD *md)
  94. {
  95. return (ASN1_item_sign(ASN1_ITEM_rptr(X509_REQ_INFO), x->sig_alg, NULL,
  96. x->signature, x->req_info, pkey, md));
  97. }
  98. int X509_REQ_sign_ctx(X509_REQ *x, EVP_MD_CTX *ctx)
  99. {
  100. return ASN1_item_sign_ctx(ASN1_ITEM_rptr(X509_REQ_INFO),
  101. x->sig_alg, NULL, x->signature, x->req_info,
  102. ctx);
  103. }
  104. int X509_CRL_sign(X509_CRL *x, EVP_PKEY *pkey, const EVP_MD *md)
  105. {
  106. x->crl->enc.modified = 1;
  107. return (ASN1_item_sign(ASN1_ITEM_rptr(X509_CRL_INFO), x->crl->sig_alg,
  108. x->sig_alg, x->signature, x->crl, pkey, md));
  109. }
  110. int X509_CRL_sign_ctx(X509_CRL *x, EVP_MD_CTX *ctx)
  111. {
  112. x->crl->enc.modified = 1;
  113. return ASN1_item_sign_ctx(ASN1_ITEM_rptr(X509_CRL_INFO),
  114. x->crl->sig_alg, x->sig_alg, x->signature,
  115. x->crl, ctx);
  116. }
  117. int NETSCAPE_SPKI_sign(NETSCAPE_SPKI *x, EVP_PKEY *pkey, const EVP_MD *md)
  118. {
  119. return (ASN1_item_sign(ASN1_ITEM_rptr(NETSCAPE_SPKAC), x->sig_algor, NULL,
  120. x->signature, x->spkac, pkey, md));
  121. }
  122. int NETSCAPE_SPKI_verify(NETSCAPE_SPKI *x, EVP_PKEY *pkey)
  123. {
  124. return (ASN1_item_verify(ASN1_ITEM_rptr(NETSCAPE_SPKAC), x->sig_algor,
  125. x->signature, x->spkac, pkey));
  126. }
  127. #ifndef OPENSSL_NO_FP_API
  128. X509 *d2i_X509_fp(FILE *fp, X509 **x509)
  129. {
  130. return ASN1_item_d2i_fp(ASN1_ITEM_rptr(X509), fp, x509);
  131. }
  132. int i2d_X509_fp(FILE *fp, X509 *x509)
  133. {
  134. return ASN1_item_i2d_fp(ASN1_ITEM_rptr(X509), fp, x509);
  135. }
  136. #endif
  137. X509 *d2i_X509_bio(BIO *bp, X509 **x509)
  138. {
  139. return ASN1_item_d2i_bio(ASN1_ITEM_rptr(X509), bp, x509);
  140. }
  141. int i2d_X509_bio(BIO *bp, X509 *x509)
  142. {
  143. return ASN1_item_i2d_bio(ASN1_ITEM_rptr(X509), bp, x509);
  144. }
  145. #ifndef OPENSSL_NO_FP_API
  146. X509_CRL *d2i_X509_CRL_fp(FILE *fp, X509_CRL **crl)
  147. {
  148. return ASN1_item_d2i_fp(ASN1_ITEM_rptr(X509_CRL), fp, crl);
  149. }
  150. int i2d_X509_CRL_fp(FILE *fp, X509_CRL *crl)
  151. {
  152. return ASN1_item_i2d_fp(ASN1_ITEM_rptr(X509_CRL), fp, crl);
  153. }
  154. #endif
  155. X509_CRL *d2i_X509_CRL_bio(BIO *bp, X509_CRL **crl)
  156. {
  157. return ASN1_item_d2i_bio(ASN1_ITEM_rptr(X509_CRL), bp, crl);
  158. }
  159. int i2d_X509_CRL_bio(BIO *bp, X509_CRL *crl)
  160. {
  161. return ASN1_item_i2d_bio(ASN1_ITEM_rptr(X509_CRL), bp, crl);
  162. }
  163. #ifndef OPENSSL_NO_FP_API
  164. X509_REQ *d2i_X509_REQ_fp(FILE *fp, X509_REQ **req)
  165. {
  166. return ASN1_item_d2i_fp(ASN1_ITEM_rptr(X509_REQ), fp, req);
  167. }
  168. int i2d_X509_REQ_fp(FILE *fp, X509_REQ *req)
  169. {
  170. return ASN1_item_i2d_fp(ASN1_ITEM_rptr(X509_REQ), fp, req);
  171. }
  172. #endif
  173. X509_REQ *d2i_X509_REQ_bio(BIO *bp, X509_REQ **req)
  174. {
  175. return ASN1_item_d2i_bio(ASN1_ITEM_rptr(X509_REQ), bp, req);
  176. }
  177. int i2d_X509_REQ_bio(BIO *bp, X509_REQ *req)
  178. {
  179. return ASN1_item_i2d_bio(ASN1_ITEM_rptr(X509_REQ), bp, req);
  180. }
  181. #ifndef OPENSSL_NO_FP_API
  182. #define IMPLEMENT_D2I_FP(type, name, bio_func) \
  183. type *name(FILE *fp, type **obj) { \
  184. BIO *bio = BIO_new_fp(fp, BIO_NOCLOSE); \
  185. if (bio == NULL) { \
  186. return NULL; \
  187. } \
  188. type *ret = bio_func(bio, obj); \
  189. BIO_free(bio); \
  190. return ret; \
  191. }
  192. #define IMPLEMENT_I2D_FP(type, name, bio_func) \
  193. int name(FILE *fp, type *obj) { \
  194. BIO *bio = BIO_new_fp(fp, BIO_NOCLOSE); \
  195. if (bio == NULL) { \
  196. return 0; \
  197. } \
  198. int ret = bio_func(bio, obj); \
  199. BIO_free(bio); \
  200. return ret; \
  201. }
  202. IMPLEMENT_D2I_FP(RSA, d2i_RSAPrivateKey_fp, d2i_RSAPrivateKey_bio)
  203. IMPLEMENT_I2D_FP(RSA, i2d_RSAPrivateKey_fp, i2d_RSAPrivateKey_bio)
  204. IMPLEMENT_D2I_FP(RSA, d2i_RSAPublicKey_fp, d2i_RSAPublicKey_bio)
  205. IMPLEMENT_I2D_FP(RSA, i2d_RSAPublicKey_fp, i2d_RSAPublicKey_bio)
  206. IMPLEMENT_D2I_FP(RSA, d2i_RSA_PUBKEY_fp, d2i_RSA_PUBKEY_bio)
  207. IMPLEMENT_I2D_FP(RSA, i2d_RSA_PUBKEY_fp, i2d_RSA_PUBKEY_bio)
  208. #endif
  209. #define IMPLEMENT_D2I_BIO(type, name, d2i_func) \
  210. type *name(BIO *bio, type **obj) { \
  211. uint8_t *data; \
  212. size_t len; \
  213. if (!BIO_read_asn1(bio, &data, &len, 100 * 1024)) { \
  214. return NULL; \
  215. } \
  216. const uint8_t *ptr = data; \
  217. type *ret = d2i_func(obj, &ptr, (long)len); \
  218. OPENSSL_free(data); \
  219. return ret; \
  220. }
  221. #define IMPLEMENT_I2D_BIO(type, name, i2d_func) \
  222. int name(BIO *bio, type *obj) { \
  223. uint8_t *data = NULL; \
  224. int len = i2d_func(obj, &data); \
  225. if (len < 0) { \
  226. return 0; \
  227. } \
  228. int ret = BIO_write_all(bio, data, len); \
  229. OPENSSL_free(data); \
  230. return ret; \
  231. }
  232. IMPLEMENT_D2I_BIO(RSA, d2i_RSAPrivateKey_bio, d2i_RSAPrivateKey)
  233. IMPLEMENT_I2D_BIO(RSA, i2d_RSAPrivateKey_bio, i2d_RSAPrivateKey)
  234. IMPLEMENT_D2I_BIO(RSA, d2i_RSAPublicKey_bio, d2i_RSAPublicKey)
  235. IMPLEMENT_I2D_BIO(RSA, i2d_RSAPublicKey_bio, i2d_RSAPublicKey)
  236. IMPLEMENT_D2I_BIO(RSA, d2i_RSA_PUBKEY_bio, d2i_RSA_PUBKEY)
  237. IMPLEMENT_I2D_BIO(RSA, i2d_RSA_PUBKEY_bio, i2d_RSA_PUBKEY)
  238. #ifndef OPENSSL_NO_DSA
  239. # ifndef OPENSSL_NO_FP_API
  240. IMPLEMENT_D2I_FP(DSA, d2i_DSAPrivateKey_fp, d2i_DSAPrivateKey_bio)
  241. IMPLEMENT_I2D_FP(DSA, i2d_DSAPrivateKey_fp, i2d_DSAPrivateKey_bio)
  242. IMPLEMENT_D2I_FP(DSA, d2i_DSA_PUBKEY_fp, d2i_DSA_PUBKEY_bio)
  243. IMPLEMENT_I2D_FP(DSA, i2d_DSA_PUBKEY_fp, i2d_DSA_PUBKEY_bio)
  244. # endif
  245. IMPLEMENT_D2I_BIO(DSA, d2i_DSAPrivateKey_bio, d2i_DSAPrivateKey)
  246. IMPLEMENT_I2D_BIO(DSA, i2d_DSAPrivateKey_bio, i2d_DSAPrivateKey)
  247. IMPLEMENT_D2I_BIO(DSA, d2i_DSA_PUBKEY_bio, d2i_DSA_PUBKEY)
  248. IMPLEMENT_I2D_BIO(DSA, i2d_DSA_PUBKEY_bio, i2d_DSA_PUBKEY)
  249. #endif
  250. #ifndef OPENSSL_NO_FP_API
  251. IMPLEMENT_D2I_FP(EC_KEY, d2i_ECPrivateKey_fp, d2i_ECPrivateKey_bio)
  252. IMPLEMENT_I2D_FP(EC_KEY, i2d_ECPrivateKey_fp, i2d_ECPrivateKey_bio)
  253. IMPLEMENT_D2I_FP(EC_KEY, d2i_EC_PUBKEY_fp, d2i_EC_PUBKEY_bio)
  254. IMPLEMENT_I2D_FP(EC_KEY, i2d_EC_PUBKEY_fp, i2d_EC_PUBKEY_bio)
  255. #endif
  256. IMPLEMENT_D2I_BIO(EC_KEY, d2i_ECPrivateKey_bio, d2i_ECPrivateKey)
  257. IMPLEMENT_I2D_BIO(EC_KEY, i2d_ECPrivateKey_bio, i2d_ECPrivateKey)
  258. IMPLEMENT_D2I_BIO(EC_KEY, d2i_EC_PUBKEY_bio, d2i_EC_PUBKEY)
  259. IMPLEMENT_I2D_BIO(EC_KEY, i2d_EC_PUBKEY_bio, i2d_EC_PUBKEY)
  260. int X509_pubkey_digest(const X509 *data, const EVP_MD *type,
  261. unsigned char *md, unsigned int *len)
  262. {
  263. ASN1_BIT_STRING *key;
  264. key = X509_get0_pubkey_bitstr(data);
  265. if (!key)
  266. return 0;
  267. return EVP_Digest(key->data, key->length, md, len, type, NULL);
  268. }
  269. int X509_digest(const X509 *data, const EVP_MD *type, unsigned char *md,
  270. unsigned int *len)
  271. {
  272. return (ASN1_item_digest
  273. (ASN1_ITEM_rptr(X509), type, (char *)data, md, len));
  274. }
  275. int X509_CRL_digest(const X509_CRL *data, const EVP_MD *type,
  276. unsigned char *md, unsigned int *len)
  277. {
  278. return (ASN1_item_digest
  279. (ASN1_ITEM_rptr(X509_CRL), type, (char *)data, md, len));
  280. }
  281. int X509_REQ_digest(const X509_REQ *data, const EVP_MD *type,
  282. unsigned char *md, unsigned int *len)
  283. {
  284. return (ASN1_item_digest
  285. (ASN1_ITEM_rptr(X509_REQ), type, (char *)data, md, len));
  286. }
  287. int X509_NAME_digest(const X509_NAME *data, const EVP_MD *type,
  288. unsigned char *md, unsigned int *len)
  289. {
  290. return (ASN1_item_digest
  291. (ASN1_ITEM_rptr(X509_NAME), type, (char *)data, md, len));
  292. }
  293. #ifndef OPENSSL_NO_FP_API
  294. IMPLEMENT_D2I_FP(X509_SIG, d2i_PKCS8_fp, d2i_PKCS8_bio)
  295. IMPLEMENT_I2D_FP(X509_SIG, i2d_PKCS8_fp, i2d_PKCS8_bio)
  296. #endif
  297. IMPLEMENT_D2I_BIO(X509_SIG, d2i_PKCS8_bio, d2i_X509_SIG)
  298. IMPLEMENT_I2D_BIO(X509_SIG, i2d_PKCS8_bio, i2d_X509_SIG)
  299. #ifndef OPENSSL_NO_FP_API
  300. IMPLEMENT_D2I_FP(PKCS8_PRIV_KEY_INFO, d2i_PKCS8_PRIV_KEY_INFO_fp,
  301. d2i_PKCS8_PRIV_KEY_INFO_bio)
  302. IMPLEMENT_I2D_FP(PKCS8_PRIV_KEY_INFO, i2d_PKCS8_PRIV_KEY_INFO_fp,
  303. i2d_PKCS8_PRIV_KEY_INFO_bio)
  304. int i2d_PKCS8PrivateKeyInfo_fp(FILE *fp, EVP_PKEY *key)
  305. {
  306. PKCS8_PRIV_KEY_INFO *p8inf;
  307. int ret;
  308. p8inf = EVP_PKEY2PKCS8(key);
  309. if (!p8inf)
  310. return 0;
  311. ret = i2d_PKCS8_PRIV_KEY_INFO_fp(fp, p8inf);
  312. PKCS8_PRIV_KEY_INFO_free(p8inf);
  313. return ret;
  314. }
  315. IMPLEMENT_D2I_FP(EVP_PKEY, d2i_PrivateKey_fp, d2i_PrivateKey_bio)
  316. IMPLEMENT_I2D_FP(EVP_PKEY, i2d_PrivateKey_fp, i2d_PrivateKey_bio)
  317. IMPLEMENT_D2I_FP(EVP_PKEY, d2i_PUBKEY_fp, d2i_PUBKEY_bio)
  318. IMPLEMENT_I2D_FP(EVP_PKEY, i2d_PUBKEY_fp, i2d_PUBKEY_bio)
  319. IMPLEMENT_D2I_BIO(PKCS8_PRIV_KEY_INFO, d2i_PKCS8_PRIV_KEY_INFO_bio,
  320. d2i_PKCS8_PRIV_KEY_INFO)
  321. IMPLEMENT_I2D_BIO(PKCS8_PRIV_KEY_INFO, i2d_PKCS8_PRIV_KEY_INFO_bio,
  322. i2d_PKCS8_PRIV_KEY_INFO)
  323. int i2d_PKCS8PrivateKeyInfo_bio(BIO *bp, EVP_PKEY *key)
  324. {
  325. PKCS8_PRIV_KEY_INFO *p8inf;
  326. int ret;
  327. p8inf = EVP_PKEY2PKCS8(key);
  328. if (!p8inf)
  329. return 0;
  330. ret = i2d_PKCS8_PRIV_KEY_INFO_bio(bp, p8inf);
  331. PKCS8_PRIV_KEY_INFO_free(p8inf);
  332. return ret;
  333. }
  334. #endif
  335. IMPLEMENT_D2I_BIO(EVP_PKEY, d2i_PrivateKey_bio, d2i_AutoPrivateKey)
  336. IMPLEMENT_I2D_BIO(EVP_PKEY, i2d_PrivateKey_bio, i2d_PrivateKey)
  337. IMPLEMENT_D2I_BIO(EVP_PKEY, d2i_PUBKEY_bio, d2i_PUBKEY)
  338. IMPLEMENT_I2D_BIO(EVP_PKEY, i2d_PUBKEY_bio, i2d_PUBKEY)
  339. IMPLEMENT_D2I_BIO(DH, d2i_DHparams_bio, d2i_DHparams)
  340. IMPLEMENT_I2D_BIO(const DH, i2d_DHparams_bio, i2d_DHparams)