p_x25519_asn1.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. /* Copyright (c) 2019, Google Inc.
  2. *
  3. * Permission to use, copy, modify, and/or distribute this software for any
  4. * purpose with or without fee is hereby granted, provided that the above
  5. * copyright notice and this permission notice appear in all copies.
  6. *
  7. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  8. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  9. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
  10. * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  11. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
  12. * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
  13. * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */
  14. #include <openssl_grpc/evp.h>
  15. #include <openssl_grpc/bytestring.h>
  16. #include <openssl_grpc/curve25519.h>
  17. #include <openssl_grpc/err.h>
  18. #include <openssl_grpc/mem.h>
  19. #include "internal.h"
  20. #include "../internal.h"
  21. static void x25519_free(EVP_PKEY *pkey) {
  22. OPENSSL_free(pkey->pkey.ptr);
  23. pkey->pkey.ptr = NULL;
  24. }
  25. static int x25519_set_priv_raw(EVP_PKEY *pkey, const uint8_t *in, size_t len) {
  26. if (len != 32) {
  27. OPENSSL_PUT_ERROR(EVP, EVP_R_DECODE_ERROR);
  28. return 0;
  29. }
  30. X25519_KEY *key = OPENSSL_malloc(sizeof(X25519_KEY));
  31. if (key == NULL) {
  32. OPENSSL_PUT_ERROR(EVP, ERR_R_MALLOC_FAILURE);
  33. return 0;
  34. }
  35. OPENSSL_memcpy(key->priv, in, 32);
  36. X25519_public_from_private(key->pub, key->priv);
  37. key->has_private = 1;
  38. x25519_free(pkey);
  39. pkey->pkey.ptr = key;
  40. return 1;
  41. }
  42. static int x25519_set_pub_raw(EVP_PKEY *pkey, const uint8_t *in, size_t len) {
  43. if (len != 32) {
  44. OPENSSL_PUT_ERROR(EVP, EVP_R_DECODE_ERROR);
  45. return 0;
  46. }
  47. X25519_KEY *key = OPENSSL_malloc(sizeof(X25519_KEY));
  48. if (key == NULL) {
  49. OPENSSL_PUT_ERROR(EVP, ERR_R_MALLOC_FAILURE);
  50. return 0;
  51. }
  52. OPENSSL_memcpy(key->pub, in, 32);
  53. key->has_private = 0;
  54. x25519_free(pkey);
  55. pkey->pkey.ptr = key;
  56. return 1;
  57. }
  58. static int x25519_get_priv_raw(const EVP_PKEY *pkey, uint8_t *out,
  59. size_t *out_len) {
  60. const X25519_KEY *key = pkey->pkey.ptr;
  61. if (!key->has_private) {
  62. OPENSSL_PUT_ERROR(EVP, EVP_R_NOT_A_PRIVATE_KEY);
  63. return 0;
  64. }
  65. if (out == NULL) {
  66. *out_len = 32;
  67. return 1;
  68. }
  69. if (*out_len < 32) {
  70. OPENSSL_PUT_ERROR(EVP, EVP_R_BUFFER_TOO_SMALL);
  71. return 0;
  72. }
  73. OPENSSL_memcpy(out, key->priv, 32);
  74. *out_len = 32;
  75. return 1;
  76. }
  77. static int x25519_get_pub_raw(const EVP_PKEY *pkey, uint8_t *out,
  78. size_t *out_len) {
  79. const X25519_KEY *key = pkey->pkey.ptr;
  80. if (out == NULL) {
  81. *out_len = 32;
  82. return 1;
  83. }
  84. if (*out_len < 32) {
  85. OPENSSL_PUT_ERROR(EVP, EVP_R_BUFFER_TOO_SMALL);
  86. return 0;
  87. }
  88. OPENSSL_memcpy(out, key->pub, 32);
  89. *out_len = 32;
  90. return 1;
  91. }
  92. static int x25519_pub_decode(EVP_PKEY *out, CBS *params, CBS *key) {
  93. // See RFC 8410, section 4.
  94. // The parameters must be omitted. Public keys have length 32.
  95. if (CBS_len(params) != 0) {
  96. OPENSSL_PUT_ERROR(EVP, EVP_R_DECODE_ERROR);
  97. return 0;
  98. }
  99. return x25519_set_pub_raw(out, CBS_data(key), CBS_len(key));
  100. }
  101. static int x25519_pub_encode(CBB *out, const EVP_PKEY *pkey) {
  102. const X25519_KEY *key = pkey->pkey.ptr;
  103. // See RFC 8410, section 4.
  104. CBB spki, algorithm, oid, key_bitstring;
  105. if (!CBB_add_asn1(out, &spki, CBS_ASN1_SEQUENCE) ||
  106. !CBB_add_asn1(&spki, &algorithm, CBS_ASN1_SEQUENCE) ||
  107. !CBB_add_asn1(&algorithm, &oid, CBS_ASN1_OBJECT) ||
  108. !CBB_add_bytes(&oid, x25519_asn1_meth.oid, x25519_asn1_meth.oid_len) ||
  109. !CBB_add_asn1(&spki, &key_bitstring, CBS_ASN1_BITSTRING) ||
  110. !CBB_add_u8(&key_bitstring, 0 /* padding */) ||
  111. !CBB_add_bytes(&key_bitstring, key->pub, 32) ||
  112. !CBB_flush(out)) {
  113. OPENSSL_PUT_ERROR(EVP, EVP_R_ENCODE_ERROR);
  114. return 0;
  115. }
  116. return 1;
  117. }
  118. static int x25519_pub_cmp(const EVP_PKEY *a, const EVP_PKEY *b) {
  119. const X25519_KEY *a_key = a->pkey.ptr;
  120. const X25519_KEY *b_key = b->pkey.ptr;
  121. return OPENSSL_memcmp(a_key->pub, b_key->pub, 32) == 0;
  122. }
  123. static int x25519_priv_decode(EVP_PKEY *out, CBS *params, CBS *key) {
  124. // See RFC 8410, section 7.
  125. // Parameters must be empty. The key is a 32-byte value wrapped in an extra
  126. // OCTET STRING layer.
  127. CBS inner;
  128. if (CBS_len(params) != 0 ||
  129. !CBS_get_asn1(key, &inner, CBS_ASN1_OCTETSTRING) ||
  130. CBS_len(key) != 0) {
  131. OPENSSL_PUT_ERROR(EVP, EVP_R_DECODE_ERROR);
  132. return 0;
  133. }
  134. return x25519_set_priv_raw(out, CBS_data(&inner), CBS_len(&inner));
  135. }
  136. static int x25519_priv_encode(CBB *out, const EVP_PKEY *pkey) {
  137. X25519_KEY *key = pkey->pkey.ptr;
  138. if (!key->has_private) {
  139. OPENSSL_PUT_ERROR(EVP, EVP_R_NOT_A_PRIVATE_KEY);
  140. return 0;
  141. }
  142. // See RFC 8410, section 7.
  143. CBB pkcs8, algorithm, oid, private_key, inner;
  144. if (!CBB_add_asn1(out, &pkcs8, CBS_ASN1_SEQUENCE) ||
  145. !CBB_add_asn1_uint64(&pkcs8, 0 /* version */) ||
  146. !CBB_add_asn1(&pkcs8, &algorithm, CBS_ASN1_SEQUENCE) ||
  147. !CBB_add_asn1(&algorithm, &oid, CBS_ASN1_OBJECT) ||
  148. !CBB_add_bytes(&oid, x25519_asn1_meth.oid, x25519_asn1_meth.oid_len) ||
  149. !CBB_add_asn1(&pkcs8, &private_key, CBS_ASN1_OCTETSTRING) ||
  150. !CBB_add_asn1(&private_key, &inner, CBS_ASN1_OCTETSTRING) ||
  151. // The PKCS#8 encoding stores only the 32-byte seed which is the first 32
  152. // bytes of the private key.
  153. !CBB_add_bytes(&inner, key->priv, 32) ||
  154. !CBB_flush(out)) {
  155. OPENSSL_PUT_ERROR(EVP, EVP_R_ENCODE_ERROR);
  156. return 0;
  157. }
  158. return 1;
  159. }
  160. static int x25519_size(const EVP_PKEY *pkey) { return 32; }
  161. static int x25519_bits(const EVP_PKEY *pkey) { return 253; }
  162. const EVP_PKEY_ASN1_METHOD x25519_asn1_meth = {
  163. EVP_PKEY_X25519,
  164. {0x2b, 0x65, 0x6e},
  165. 3,
  166. x25519_pub_decode,
  167. x25519_pub_encode,
  168. x25519_pub_cmp,
  169. x25519_priv_decode,
  170. x25519_priv_encode,
  171. x25519_set_priv_raw,
  172. x25519_set_pub_raw,
  173. x25519_get_priv_raw,
  174. x25519_get_pub_raw,
  175. NULL /* pkey_opaque */,
  176. x25519_size,
  177. x25519_bits,
  178. NULL /* param_missing */,
  179. NULL /* param_copy */,
  180. NULL /* param_cmp */,
  181. x25519_free,
  182. };
  183. int EVP_PKEY_set1_tls_encodedpoint(EVP_PKEY *pkey, const uint8_t *in,
  184. size_t len) {
  185. // TODO(davidben): In OpenSSL, this function also works for |EVP_PKEY_EC|
  186. // keys. Add support if it ever comes up.
  187. if (pkey->type != EVP_PKEY_X25519) {
  188. OPENSSL_PUT_ERROR(EVP, EVP_R_UNSUPPORTED_PUBLIC_KEY_TYPE);
  189. return 0;
  190. }
  191. return x25519_set_pub_raw(pkey, in, len);
  192. }
  193. size_t EVP_PKEY_get1_tls_encodedpoint(const EVP_PKEY *pkey, uint8_t **out_ptr) {
  194. // TODO(davidben): In OpenSSL, this function also works for |EVP_PKEY_EC|
  195. // keys. Add support if it ever comes up.
  196. if (pkey->type != EVP_PKEY_X25519) {
  197. OPENSSL_PUT_ERROR(EVP, EVP_R_UNSUPPORTED_PUBLIC_KEY_TYPE);
  198. return 0;
  199. }
  200. const X25519_KEY *key = pkey->pkey.ptr;
  201. if (key == NULL) {
  202. OPENSSL_PUT_ERROR(EVP, EVP_R_NO_KEY_SET);
  203. return 0;
  204. }
  205. *out_ptr = OPENSSL_memdup(key->pub, 32);
  206. return *out_ptr == NULL ? 0 : 32;
  207. }