Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.
 
 
 
 
 
 

388 Zeilen
11 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/evp.h>
  57. #include <string.h>
  58. #include <openssl/bytestring.h>
  59. #include <openssl/dsa.h>
  60. #include <openssl/ec_key.h>
  61. #include <openssl/err.h>
  62. #include <openssl/rsa.h>
  63. #include "internal.h"
  64. #include "../internal.h"
  65. static const EVP_PKEY_ASN1_METHOD *const kASN1Methods[] = {
  66. &rsa_asn1_meth,
  67. &ec_asn1_meth,
  68. &dsa_asn1_meth,
  69. &ed25519_asn1_meth,
  70. };
  71. static int parse_key_type(CBS *cbs, int *out_type) {
  72. CBS oid;
  73. if (!CBS_get_asn1(cbs, &oid, CBS_ASN1_OBJECT)) {
  74. return 0;
  75. }
  76. for (unsigned i = 0; i < OPENSSL_ARRAY_SIZE(kASN1Methods); i++) {
  77. const EVP_PKEY_ASN1_METHOD *method = kASN1Methods[i];
  78. if (CBS_len(&oid) == method->oid_len &&
  79. OPENSSL_memcmp(CBS_data(&oid), method->oid, method->oid_len) == 0) {
  80. *out_type = method->pkey_id;
  81. return 1;
  82. }
  83. }
  84. return 0;
  85. }
  86. EVP_PKEY *EVP_parse_public_key(CBS *cbs) {
  87. // Parse the SubjectPublicKeyInfo.
  88. CBS spki, algorithm, key;
  89. int type;
  90. uint8_t padding;
  91. if (!CBS_get_asn1(cbs, &spki, CBS_ASN1_SEQUENCE) ||
  92. !CBS_get_asn1(&spki, &algorithm, CBS_ASN1_SEQUENCE) ||
  93. !CBS_get_asn1(&spki, &key, CBS_ASN1_BITSTRING) ||
  94. CBS_len(&spki) != 0) {
  95. OPENSSL_PUT_ERROR(EVP, EVP_R_DECODE_ERROR);
  96. return NULL;
  97. }
  98. if (!parse_key_type(&algorithm, &type)) {
  99. OPENSSL_PUT_ERROR(EVP, EVP_R_UNSUPPORTED_ALGORITHM);
  100. return NULL;
  101. }
  102. if (// Every key type defined encodes the key as a byte string with the same
  103. // conversion to BIT STRING.
  104. !CBS_get_u8(&key, &padding) ||
  105. padding != 0) {
  106. OPENSSL_PUT_ERROR(EVP, EVP_R_DECODE_ERROR);
  107. return NULL;
  108. }
  109. // Set up an |EVP_PKEY| of the appropriate type.
  110. EVP_PKEY *ret = EVP_PKEY_new();
  111. if (ret == NULL ||
  112. !EVP_PKEY_set_type(ret, type)) {
  113. goto err;
  114. }
  115. // Call into the type-specific SPKI decoding function.
  116. if (ret->ameth->pub_decode == NULL) {
  117. OPENSSL_PUT_ERROR(EVP, EVP_R_UNSUPPORTED_ALGORITHM);
  118. goto err;
  119. }
  120. if (!ret->ameth->pub_decode(ret, &algorithm, &key)) {
  121. goto err;
  122. }
  123. return ret;
  124. err:
  125. EVP_PKEY_free(ret);
  126. return NULL;
  127. }
  128. int EVP_marshal_public_key(CBB *cbb, const EVP_PKEY *key) {
  129. if (key->ameth == NULL || key->ameth->pub_encode == NULL) {
  130. OPENSSL_PUT_ERROR(EVP, EVP_R_UNSUPPORTED_ALGORITHM);
  131. return 0;
  132. }
  133. return key->ameth->pub_encode(cbb, key);
  134. }
  135. EVP_PKEY *EVP_parse_private_key(CBS *cbs) {
  136. // Parse the PrivateKeyInfo.
  137. CBS pkcs8, algorithm, key;
  138. uint64_t version;
  139. int type;
  140. if (!CBS_get_asn1(cbs, &pkcs8, CBS_ASN1_SEQUENCE) ||
  141. !CBS_get_asn1_uint64(&pkcs8, &version) ||
  142. version != 0 ||
  143. !CBS_get_asn1(&pkcs8, &algorithm, CBS_ASN1_SEQUENCE) ||
  144. !CBS_get_asn1(&pkcs8, &key, CBS_ASN1_OCTETSTRING)) {
  145. OPENSSL_PUT_ERROR(EVP, EVP_R_DECODE_ERROR);
  146. return NULL;
  147. }
  148. if (!parse_key_type(&algorithm, &type)) {
  149. OPENSSL_PUT_ERROR(EVP, EVP_R_UNSUPPORTED_ALGORITHM);
  150. return NULL;
  151. }
  152. // A PrivateKeyInfo ends with a SET of Attributes which we ignore.
  153. // Set up an |EVP_PKEY| of the appropriate type.
  154. EVP_PKEY *ret = EVP_PKEY_new();
  155. if (ret == NULL ||
  156. !EVP_PKEY_set_type(ret, type)) {
  157. goto err;
  158. }
  159. // Call into the type-specific PrivateKeyInfo decoding function.
  160. if (ret->ameth->priv_decode == NULL) {
  161. OPENSSL_PUT_ERROR(EVP, EVP_R_UNSUPPORTED_ALGORITHM);
  162. goto err;
  163. }
  164. if (!ret->ameth->priv_decode(ret, &algorithm, &key)) {
  165. goto err;
  166. }
  167. return ret;
  168. err:
  169. EVP_PKEY_free(ret);
  170. return NULL;
  171. }
  172. int EVP_marshal_private_key(CBB *cbb, const EVP_PKEY *key) {
  173. if (key->ameth == NULL || key->ameth->priv_encode == NULL) {
  174. OPENSSL_PUT_ERROR(EVP, EVP_R_UNSUPPORTED_ALGORITHM);
  175. return 0;
  176. }
  177. return key->ameth->priv_encode(cbb, key);
  178. }
  179. static EVP_PKEY *old_priv_decode(CBS *cbs, int type) {
  180. EVP_PKEY *ret = EVP_PKEY_new();
  181. if (ret == NULL) {
  182. return NULL;
  183. }
  184. switch (type) {
  185. case EVP_PKEY_EC: {
  186. EC_KEY *ec_key = EC_KEY_parse_private_key(cbs, NULL);
  187. if (ec_key == NULL || !EVP_PKEY_assign_EC_KEY(ret, ec_key)) {
  188. EC_KEY_free(ec_key);
  189. goto err;
  190. }
  191. return ret;
  192. }
  193. case EVP_PKEY_DSA: {
  194. DSA *dsa = DSA_parse_private_key(cbs);
  195. if (dsa == NULL || !EVP_PKEY_assign_DSA(ret, dsa)) {
  196. DSA_free(dsa);
  197. goto err;
  198. }
  199. return ret;
  200. }
  201. case EVP_PKEY_RSA: {
  202. RSA *rsa = RSA_parse_private_key(cbs);
  203. if (rsa == NULL || !EVP_PKEY_assign_RSA(ret, rsa)) {
  204. RSA_free(rsa);
  205. goto err;
  206. }
  207. return ret;
  208. }
  209. default:
  210. OPENSSL_PUT_ERROR(EVP, EVP_R_UNKNOWN_PUBLIC_KEY_TYPE);
  211. goto err;
  212. }
  213. err:
  214. EVP_PKEY_free(ret);
  215. return NULL;
  216. }
  217. EVP_PKEY *d2i_PrivateKey(int type, EVP_PKEY **out, const uint8_t **inp,
  218. long len) {
  219. if (len < 0) {
  220. OPENSSL_PUT_ERROR(EVP, EVP_R_DECODE_ERROR);
  221. return NULL;
  222. }
  223. // Parse with the legacy format.
  224. CBS cbs;
  225. CBS_init(&cbs, *inp, (size_t)len);
  226. EVP_PKEY *ret = old_priv_decode(&cbs, type);
  227. if (ret == NULL) {
  228. // Try again with PKCS#8.
  229. ERR_clear_error();
  230. CBS_init(&cbs, *inp, (size_t)len);
  231. ret = EVP_parse_private_key(&cbs);
  232. if (ret == NULL) {
  233. return NULL;
  234. }
  235. if (ret->type != type) {
  236. OPENSSL_PUT_ERROR(EVP, EVP_R_DIFFERENT_KEY_TYPES);
  237. EVP_PKEY_free(ret);
  238. return NULL;
  239. }
  240. }
  241. if (out != NULL) {
  242. EVP_PKEY_free(*out);
  243. *out = ret;
  244. }
  245. *inp = CBS_data(&cbs);
  246. return ret;
  247. }
  248. // num_elements parses one SEQUENCE from |in| and returns the number of elements
  249. // in it. On parse error, it returns zero.
  250. static size_t num_elements(const uint8_t *in, size_t in_len) {
  251. CBS cbs, sequence;
  252. CBS_init(&cbs, in, (size_t)in_len);
  253. if (!CBS_get_asn1(&cbs, &sequence, CBS_ASN1_SEQUENCE)) {
  254. return 0;
  255. }
  256. size_t count = 0;
  257. while (CBS_len(&sequence) > 0) {
  258. if (!CBS_get_any_asn1_element(&sequence, NULL, NULL, NULL)) {
  259. return 0;
  260. }
  261. count++;
  262. }
  263. return count;
  264. }
  265. EVP_PKEY *d2i_AutoPrivateKey(EVP_PKEY **out, const uint8_t **inp, long len) {
  266. if (len < 0) {
  267. OPENSSL_PUT_ERROR(EVP, EVP_R_DECODE_ERROR);
  268. return NULL;
  269. }
  270. // Parse the input as a PKCS#8 PrivateKeyInfo.
  271. CBS cbs;
  272. CBS_init(&cbs, *inp, (size_t)len);
  273. EVP_PKEY *ret = EVP_parse_private_key(&cbs);
  274. if (ret != NULL) {
  275. if (out != NULL) {
  276. EVP_PKEY_free(*out);
  277. *out = ret;
  278. }
  279. *inp = CBS_data(&cbs);
  280. return ret;
  281. }
  282. ERR_clear_error();
  283. // Count the elements to determine the legacy key format.
  284. switch (num_elements(*inp, (size_t)len)) {
  285. case 4:
  286. return d2i_PrivateKey(EVP_PKEY_EC, out, inp, len);
  287. case 6:
  288. return d2i_PrivateKey(EVP_PKEY_DSA, out, inp, len);
  289. default:
  290. return d2i_PrivateKey(EVP_PKEY_RSA, out, inp, len);
  291. }
  292. }
  293. int i2d_PublicKey(const EVP_PKEY *key, uint8_t **outp) {
  294. switch (key->type) {
  295. case EVP_PKEY_RSA:
  296. return i2d_RSAPublicKey(key->pkey.rsa, outp);
  297. case EVP_PKEY_DSA:
  298. return i2d_DSAPublicKey(key->pkey.dsa, outp);
  299. case EVP_PKEY_EC:
  300. return i2o_ECPublicKey(key->pkey.ec, outp);
  301. default:
  302. OPENSSL_PUT_ERROR(EVP, EVP_R_UNSUPPORTED_PUBLIC_KEY_TYPE);
  303. return -1;
  304. }
  305. }
  306. EVP_PKEY *d2i_PublicKey(int type, EVP_PKEY **out, const uint8_t **inp,
  307. long len) {
  308. EVP_PKEY *ret = EVP_PKEY_new();
  309. if (ret == NULL) {
  310. return NULL;
  311. }
  312. CBS cbs;
  313. CBS_init(&cbs, *inp, len < 0 ? 0 : (size_t)len);
  314. switch (type) {
  315. case EVP_PKEY_RSA: {
  316. RSA *rsa = RSA_parse_public_key(&cbs);
  317. if (rsa == NULL || !EVP_PKEY_assign_RSA(ret, rsa)) {
  318. RSA_free(rsa);
  319. goto err;
  320. }
  321. break;
  322. }
  323. // Unlike OpenSSL, we do not support EC keys with this API. The raw EC
  324. // public key serialization requires knowing the group. In OpenSSL, calling
  325. // this function with |EVP_PKEY_EC| and setting |out| to NULL does not work.
  326. // It requires |*out| to include a partially-initiazed |EVP_PKEY| to extract
  327. // the group.
  328. default:
  329. OPENSSL_PUT_ERROR(EVP, EVP_R_UNSUPPORTED_PUBLIC_KEY_TYPE);
  330. goto err;
  331. }
  332. *inp = CBS_data(&cbs);
  333. if (out != NULL) {
  334. EVP_PKEY_free(*out);
  335. *out = ret;
  336. }
  337. return ret;
  338. err:
  339. EVP_PKEY_free(ret);
  340. return NULL;
  341. }