p8_pkey.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. /*
  2. * Copyright 1999-2025 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License 2.0 (the "License"). You may not use
  5. * this file except in compliance with the License. You can obtain a copy
  6. * in the file LICENSE in the source distribution or at
  7. * https://www.openssl.org/source/license.html
  8. */
  9. #include <stdio.h>
  10. #include "internal/cryptlib.h"
  11. #include <openssl/asn1t.h>
  12. #include <openssl/x509.h>
  13. #include "crypto/x509.h"
  14. /* Minor tweak to operation: zero private key data */
  15. static int pkey_cb(int operation, ASN1_VALUE **pval, const ASN1_ITEM *it,
  16. void *exarg)
  17. {
  18. PKCS8_PRIV_KEY_INFO *key;
  19. int version;
  20. switch (operation) {
  21. case ASN1_OP_FREE_PRE:
  22. /* The structure is still valid during ASN1_OP_FREE_PRE */
  23. key = (PKCS8_PRIV_KEY_INFO *)*pval;
  24. if (key->pkey)
  25. OPENSSL_cleanse(key->pkey->data, key->pkey->length);
  26. break;
  27. case ASN1_OP_D2I_POST:
  28. /* Insist on a valid version now that the structure is decoded */
  29. key = (PKCS8_PRIV_KEY_INFO *)*pval;
  30. version = ASN1_INTEGER_get(key->version);
  31. if (version < 0 || version > 1)
  32. return 0;
  33. if (version == 0 && key->kpub != NULL)
  34. return 0;
  35. break;
  36. }
  37. return 1;
  38. }
  39. ASN1_SEQUENCE_cb(PKCS8_PRIV_KEY_INFO, pkey_cb) = {
  40. ASN1_SIMPLE(PKCS8_PRIV_KEY_INFO, version, ASN1_INTEGER),
  41. ASN1_SIMPLE(PKCS8_PRIV_KEY_INFO, pkeyalg, X509_ALGOR),
  42. ASN1_SIMPLE(PKCS8_PRIV_KEY_INFO, pkey, ASN1_OCTET_STRING),
  43. ASN1_IMP_SET_OF_OPT(PKCS8_PRIV_KEY_INFO, attributes, X509_ATTRIBUTE, 0),
  44. ASN1_IMP_OPT(PKCS8_PRIV_KEY_INFO, kpub, ASN1_BIT_STRING, 1)
  45. } ASN1_SEQUENCE_END_cb(PKCS8_PRIV_KEY_INFO, PKCS8_PRIV_KEY_INFO)
  46. IMPLEMENT_ASN1_FUNCTIONS(PKCS8_PRIV_KEY_INFO)
  47. int PKCS8_pkey_set0(PKCS8_PRIV_KEY_INFO *priv, ASN1_OBJECT *aobj,
  48. int version,
  49. int ptype, void *pval, unsigned char *penc, int penclen)
  50. {
  51. if (version >= 0) {
  52. /* We only support PKCS#8 v1 (0) and v2 (1). */
  53. if (version > 1)
  54. return 0;
  55. if (!ASN1_INTEGER_set(priv->version, version))
  56. return 0;
  57. }
  58. if (!X509_ALGOR_set0(priv->pkeyalg, aobj, ptype, pval))
  59. return 0;
  60. if (penc)
  61. ASN1_STRING_set0(priv->pkey, penc, penclen);
  62. return 1;
  63. }
  64. int PKCS8_pkey_get0(const ASN1_OBJECT **ppkalg,
  65. const unsigned char **pk, int *ppklen,
  66. const X509_ALGOR **pa, const PKCS8_PRIV_KEY_INFO *p8)
  67. {
  68. if (ppkalg)
  69. *ppkalg = p8->pkeyalg->algorithm;
  70. if (pk) {
  71. *pk = ASN1_STRING_get0_data(p8->pkey);
  72. *ppklen = ASN1_STRING_length(p8->pkey);
  73. }
  74. if (pa)
  75. *pa = p8->pkeyalg;
  76. return 1;
  77. }
  78. const STACK_OF(X509_ATTRIBUTE) *
  79. PKCS8_pkey_get0_attrs(const PKCS8_PRIV_KEY_INFO *p8)
  80. {
  81. return p8->attributes;
  82. }
  83. int PKCS8_pkey_add1_attr_by_NID(PKCS8_PRIV_KEY_INFO *p8, int nid, int type,
  84. const unsigned char *bytes, int len)
  85. {
  86. if (X509at_add1_attr_by_NID(&p8->attributes, nid, type, bytes, len) != NULL)
  87. return 1;
  88. return 0;
  89. }
  90. int PKCS8_pkey_add1_attr_by_OBJ(PKCS8_PRIV_KEY_INFO *p8, const ASN1_OBJECT *obj, int type,
  91. const unsigned char *bytes, int len)
  92. {
  93. return (X509at_add1_attr_by_OBJ(&p8->attributes, obj, type, bytes, len) != NULL);
  94. }
  95. int PKCS8_pkey_add1_attr(PKCS8_PRIV_KEY_INFO *p8, X509_ATTRIBUTE *attr)
  96. {
  97. return (X509at_add1_attr(&p8->attributes, attr) != NULL);
  98. }