您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

lax_der_privatekey_parsing.c 5.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. /**********************************************************************
  2. * Copyright (c) 2014, 2015 Pieter Wuille *
  3. * Distributed under the MIT software license, see the accompanying *
  4. * file COPYING or http://www.opensource.org/licenses/mit-license.php.*
  5. **********************************************************************/
  6. #include <string.h>
  7. #include <secp256k1.h>
  8. #include "lax_der_privatekey_parsing.h"
  9. int ec_privkey_import_der(const secp256k1_context* ctx, unsigned char *out32, const unsigned char *privkey, size_t privkeylen) {
  10. const unsigned char *end = privkey + privkeylen;
  11. int lenb = 0;
  12. int len = 0;
  13. memset(out32, 0, 32);
  14. /* sequence header */
  15. if (end < privkey+1 || *privkey != 0x30) {
  16. return 0;
  17. }
  18. privkey++;
  19. /* sequence length constructor */
  20. if (end < privkey+1 || !(*privkey & 0x80)) {
  21. return 0;
  22. }
  23. lenb = *privkey & ~0x80; privkey++;
  24. if (lenb < 1 || lenb > 2) {
  25. return 0;
  26. }
  27. if (end < privkey+lenb) {
  28. return 0;
  29. }
  30. /* sequence length */
  31. len = privkey[lenb-1] | (lenb > 1 ? privkey[lenb-2] << 8 : 0);
  32. privkey += lenb;
  33. if (end < privkey+len) {
  34. return 0;
  35. }
  36. /* sequence element 0: version number (=1) */
  37. if (end < privkey+3 || privkey[0] != 0x02 || privkey[1] != 0x01 || privkey[2] != 0x01) {
  38. return 0;
  39. }
  40. privkey += 3;
  41. /* sequence element 1: octet string, up to 32 bytes */
  42. if (end < privkey+2 || privkey[0] != 0x04 || privkey[1] > 0x20 || end < privkey+2+privkey[1]) {
  43. return 0;
  44. }
  45. memcpy(out32 + 32 - privkey[1], privkey + 2, privkey[1]);
  46. if (!secp256k1_ec_seckey_verify(ctx, out32)) {
  47. memset(out32, 0, 32);
  48. return 0;
  49. }
  50. return 1;
  51. }
  52. int ec_privkey_export_der(const secp256k1_context *ctx, unsigned char *privkey, size_t *privkeylen, const unsigned char *key32, int compressed) {
  53. secp256k1_pubkey pubkey;
  54. size_t pubkeylen = 0;
  55. if (!secp256k1_ec_pubkey_create(ctx, &pubkey, key32)) {
  56. *privkeylen = 0;
  57. return 0;
  58. }
  59. if (compressed) {
  60. static const unsigned char begin[] = {
  61. 0x30,0x81,0xD3,0x02,0x01,0x01,0x04,0x20
  62. };
  63. static const unsigned char middle[] = {
  64. 0xA0,0x81,0x85,0x30,0x81,0x82,0x02,0x01,0x01,0x30,0x2C,0x06,0x07,0x2A,0x86,0x48,
  65. 0xCE,0x3D,0x01,0x01,0x02,0x21,0x00,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
  66. 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
  67. 0xFF,0xFF,0xFE,0xFF,0xFF,0xFC,0x2F,0x30,0x06,0x04,0x01,0x00,0x04,0x01,0x07,0x04,
  68. 0x21,0x02,0x79,0xBE,0x66,0x7E,0xF9,0xDC,0xBB,0xAC,0x55,0xA0,0x62,0x95,0xCE,0x87,
  69. 0x0B,0x07,0x02,0x9B,0xFC,0xDB,0x2D,0xCE,0x28,0xD9,0x59,0xF2,0x81,0x5B,0x16,0xF8,
  70. 0x17,0x98,0x02,0x21,0x00,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
  71. 0xFF,0xFF,0xFF,0xFF,0xFE,0xBA,0xAE,0xDC,0xE6,0xAF,0x48,0xA0,0x3B,0xBF,0xD2,0x5E,
  72. 0x8C,0xD0,0x36,0x41,0x41,0x02,0x01,0x01,0xA1,0x24,0x03,0x22,0x00
  73. };
  74. unsigned char *ptr = privkey;
  75. memcpy(ptr, begin, sizeof(begin)); ptr += sizeof(begin);
  76. memcpy(ptr, key32, 32); ptr += 32;
  77. memcpy(ptr, middle, sizeof(middle)); ptr += sizeof(middle);
  78. pubkeylen = 33;
  79. secp256k1_ec_pubkey_serialize(ctx, ptr, &pubkeylen, &pubkey, SECP256K1_EC_COMPRESSED);
  80. ptr += pubkeylen;
  81. *privkeylen = ptr - privkey;
  82. } else {
  83. static const unsigned char begin[] = {
  84. 0x30,0x82,0x01,0x13,0x02,0x01,0x01,0x04,0x20
  85. };
  86. static const unsigned char middle[] = {
  87. 0xA0,0x81,0xA5,0x30,0x81,0xA2,0x02,0x01,0x01,0x30,0x2C,0x06,0x07,0x2A,0x86,0x48,
  88. 0xCE,0x3D,0x01,0x01,0x02,0x21,0x00,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
  89. 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
  90. 0xFF,0xFF,0xFE,0xFF,0xFF,0xFC,0x2F,0x30,0x06,0x04,0x01,0x00,0x04,0x01,0x07,0x04,
  91. 0x41,0x04,0x79,0xBE,0x66,0x7E,0xF9,0xDC,0xBB,0xAC,0x55,0xA0,0x62,0x95,0xCE,0x87,
  92. 0x0B,0x07,0x02,0x9B,0xFC,0xDB,0x2D,0xCE,0x28,0xD9,0x59,0xF2,0x81,0x5B,0x16,0xF8,
  93. 0x17,0x98,0x48,0x3A,0xDA,0x77,0x26,0xA3,0xC4,0x65,0x5D,0xA4,0xFB,0xFC,0x0E,0x11,
  94. 0x08,0xA8,0xFD,0x17,0xB4,0x48,0xA6,0x85,0x54,0x19,0x9C,0x47,0xD0,0x8F,0xFB,0x10,
  95. 0xD4,0xB8,0x02,0x21,0x00,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
  96. 0xFF,0xFF,0xFF,0xFF,0xFE,0xBA,0xAE,0xDC,0xE6,0xAF,0x48,0xA0,0x3B,0xBF,0xD2,0x5E,
  97. 0x8C,0xD0,0x36,0x41,0x41,0x02,0x01,0x01,0xA1,0x44,0x03,0x42,0x00
  98. };
  99. unsigned char *ptr = privkey;
  100. memcpy(ptr, begin, sizeof(begin)); ptr += sizeof(begin);
  101. memcpy(ptr, key32, 32); ptr += 32;
  102. memcpy(ptr, middle, sizeof(middle)); ptr += sizeof(middle);
  103. pubkeylen = 65;
  104. secp256k1_ec_pubkey_serialize(ctx, ptr, &pubkeylen, &pubkey, SECP256K1_EC_UNCOMPRESSED);
  105. ptr += pubkeylen;
  106. *privkeylen = ptr - privkey;
  107. }
  108. return 1;
  109. }