evp_asn1.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  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 <openssl/asn1.h>
  58. #include <openssl/bytestring.h>
  59. #include <openssl/err.h>
  60. #include <openssl/obj.h>
  61. #include <openssl/x509.h>
  62. #include "internal.h"
  63. EVP_PKEY *EVP_parse_public_key(CBS *cbs) {
  64. /* Parse the SubjectPublicKeyInfo. */
  65. CBS spki, algorithm, oid, key;
  66. uint8_t padding;
  67. if (!CBS_get_asn1(cbs, &spki, CBS_ASN1_SEQUENCE) ||
  68. !CBS_get_asn1(&spki, &algorithm, CBS_ASN1_SEQUENCE) ||
  69. !CBS_get_asn1(&algorithm, &oid, CBS_ASN1_OBJECT) ||
  70. !CBS_get_asn1(&spki, &key, CBS_ASN1_BITSTRING) ||
  71. CBS_len(&spki) != 0 ||
  72. /* Every key type defined encodes the key as a byte string with the same
  73. * conversion to BIT STRING. */
  74. !CBS_get_u8(&key, &padding) ||
  75. padding != 0) {
  76. OPENSSL_PUT_ERROR(EVP, EVP_R_DECODE_ERROR);
  77. return NULL;
  78. }
  79. /* Set up an |EVP_PKEY| of the appropriate type. */
  80. EVP_PKEY *ret = EVP_PKEY_new();
  81. if (ret == NULL ||
  82. !EVP_PKEY_set_type(ret, OBJ_cbs2nid(&oid))) {
  83. goto err;
  84. }
  85. /* Call into the type-specific SPKI decoding function. */
  86. if (ret->ameth->pub_decode == NULL) {
  87. OPENSSL_PUT_ERROR(EVP, EVP_R_UNSUPPORTED_ALGORITHM);
  88. goto err;
  89. }
  90. if (!ret->ameth->pub_decode(ret, &algorithm, &key)) {
  91. goto err;
  92. }
  93. return ret;
  94. err:
  95. EVP_PKEY_free(ret);
  96. return NULL;
  97. }
  98. int EVP_marshal_public_key(CBB *cbb, const EVP_PKEY *key) {
  99. if (key->ameth->pub_encode == NULL) {
  100. OPENSSL_PUT_ERROR(EVP, EVP_R_UNSUPPORTED_ALGORITHM);
  101. return 0;
  102. }
  103. return key->ameth->pub_encode(cbb, key);
  104. }
  105. EVP_PKEY *EVP_parse_private_key(CBS *cbs) {
  106. /* Parse the PrivateKeyInfo. */
  107. CBS pkcs8, algorithm, oid, key;
  108. uint64_t version;
  109. if (!CBS_get_asn1(cbs, &pkcs8, CBS_ASN1_SEQUENCE) ||
  110. !CBS_get_asn1_uint64(&pkcs8, &version) ||
  111. version != 0 ||
  112. !CBS_get_asn1(&pkcs8, &algorithm, CBS_ASN1_SEQUENCE) ||
  113. !CBS_get_asn1(&algorithm, &oid, CBS_ASN1_OBJECT) ||
  114. !CBS_get_asn1(&pkcs8, &key, CBS_ASN1_OCTETSTRING)) {
  115. OPENSSL_PUT_ERROR(EVP, EVP_R_DECODE_ERROR);
  116. return NULL;
  117. }
  118. /* A PrivateKeyInfo ends with a SET of Attributes which we ignore. */
  119. /* Set up an |EVP_PKEY| of the appropriate type. */
  120. EVP_PKEY *ret = EVP_PKEY_new();
  121. if (ret == NULL ||
  122. !EVP_PKEY_set_type(ret, OBJ_cbs2nid(&oid))) {
  123. goto err;
  124. }
  125. /* Call into the type-specific PrivateKeyInfo decoding function. */
  126. if (ret->ameth->priv_decode == NULL) {
  127. OPENSSL_PUT_ERROR(EVP, EVP_R_UNSUPPORTED_ALGORITHM);
  128. goto err;
  129. }
  130. if (!ret->ameth->priv_decode(ret, &algorithm, &key)) {
  131. goto err;
  132. }
  133. return ret;
  134. err:
  135. EVP_PKEY_free(ret);
  136. return NULL;
  137. }
  138. int EVP_marshal_private_key(CBB *cbb, const EVP_PKEY *key) {
  139. if (key->ameth->priv_encode == NULL) {
  140. OPENSSL_PUT_ERROR(EVP, EVP_R_UNSUPPORTED_ALGORITHM);
  141. return 0;
  142. }
  143. return key->ameth->priv_encode(cbb, key);
  144. }
  145. EVP_PKEY *d2i_PrivateKey(int type, EVP_PKEY **out, const uint8_t **inp,
  146. long len) {
  147. EVP_PKEY *ret;
  148. if (out == NULL || *out == NULL) {
  149. ret = EVP_PKEY_new();
  150. if (ret == NULL) {
  151. OPENSSL_PUT_ERROR(EVP, ERR_R_EVP_LIB);
  152. return NULL;
  153. }
  154. } else {
  155. ret = *out;
  156. }
  157. if (!EVP_PKEY_set_type(ret, type)) {
  158. OPENSSL_PUT_ERROR(EVP, EVP_R_UNKNOWN_PUBLIC_KEY_TYPE);
  159. goto err;
  160. }
  161. const uint8_t *in = *inp;
  162. /* If trying to remove |old_priv_decode|, note that some code depends on this
  163. * function writing into |*out| and the |priv_decode| path doesn't support
  164. * that. */
  165. if (!ret->ameth->old_priv_decode ||
  166. !ret->ameth->old_priv_decode(ret, &in, len)) {
  167. if (ret->ameth->priv_decode) {
  168. /* Reset |in| in case |old_priv_decode| advanced it on error. */
  169. in = *inp;
  170. PKCS8_PRIV_KEY_INFO *p8 = d2i_PKCS8_PRIV_KEY_INFO(NULL, &in, len);
  171. if (!p8) {
  172. goto err;
  173. }
  174. EVP_PKEY_free(ret);
  175. ret = EVP_PKCS82PKEY(p8);
  176. PKCS8_PRIV_KEY_INFO_free(p8);
  177. if (ret == NULL) {
  178. goto err;
  179. }
  180. } else {
  181. OPENSSL_PUT_ERROR(EVP, ERR_R_ASN1_LIB);
  182. goto err;
  183. }
  184. }
  185. if (out != NULL) {
  186. *out = ret;
  187. }
  188. *inp = in;
  189. return ret;
  190. err:
  191. if (out == NULL || *out != ret) {
  192. EVP_PKEY_free(ret);
  193. }
  194. return NULL;
  195. }
  196. EVP_PKEY *d2i_AutoPrivateKey(EVP_PKEY **out, const uint8_t **inp, long len) {
  197. STACK_OF(ASN1_TYPE) *inkey;
  198. const uint8_t *p;
  199. int keytype;
  200. p = *inp;
  201. /* Dirty trick: read in the ASN1 data into out STACK_OF(ASN1_TYPE):
  202. * by analyzing it we can determine the passed structure: this
  203. * assumes the input is surrounded by an ASN1 SEQUENCE. */
  204. inkey = d2i_ASN1_SEQUENCE_ANY(NULL, &p, len);
  205. /* Since we only need to discern "traditional format" RSA and DSA
  206. * keys we can just count the elements. */
  207. if (sk_ASN1_TYPE_num(inkey) == 6) {
  208. keytype = EVP_PKEY_DSA;
  209. } else if (sk_ASN1_TYPE_num(inkey) == 4) {
  210. keytype = EVP_PKEY_EC;
  211. } else if (sk_ASN1_TYPE_num(inkey) == 3) {
  212. /* This seems to be PKCS8, not traditional format */
  213. p = *inp;
  214. PKCS8_PRIV_KEY_INFO *p8 = d2i_PKCS8_PRIV_KEY_INFO(NULL, &p, len);
  215. EVP_PKEY *ret;
  216. sk_ASN1_TYPE_pop_free(inkey, ASN1_TYPE_free);
  217. if (!p8) {
  218. OPENSSL_PUT_ERROR(EVP, EVP_R_UNSUPPORTED_PUBLIC_KEY_TYPE);
  219. return NULL;
  220. }
  221. ret = EVP_PKCS82PKEY(p8);
  222. PKCS8_PRIV_KEY_INFO_free(p8);
  223. if (ret == NULL) {
  224. return NULL;
  225. }
  226. *inp = p;
  227. if (out) {
  228. *out = ret;
  229. }
  230. return ret;
  231. } else {
  232. keytype = EVP_PKEY_RSA;
  233. }
  234. sk_ASN1_TYPE_pop_free(inkey, ASN1_TYPE_free);
  235. return d2i_PrivateKey(keytype, out, inp, len);
  236. }
  237. int i2d_PublicKey(EVP_PKEY *key, uint8_t **outp) {
  238. switch (key->type) {
  239. case EVP_PKEY_RSA:
  240. return i2d_RSAPublicKey(key->pkey.rsa, outp);
  241. case EVP_PKEY_DSA:
  242. return i2d_DSAPublicKey(key->pkey.dsa, outp);
  243. case EVP_PKEY_EC:
  244. return i2o_ECPublicKey(key->pkey.ec, outp);
  245. default:
  246. OPENSSL_PUT_ERROR(EVP, EVP_R_UNSUPPORTED_PUBLIC_KEY_TYPE);
  247. return -1;
  248. }
  249. }