2
0

p8_pkey.c 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. /*
  2. * Copyright 1999-2016 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the OpenSSL license (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 "internal/x509_int.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. /* Since the structure must still be valid use ASN1_OP_FREE_PRE */
  19. if (operation == ASN1_OP_FREE_PRE) {
  20. PKCS8_PRIV_KEY_INFO *key = (PKCS8_PRIV_KEY_INFO *)*pval;
  21. if (key->pkey)
  22. OPENSSL_cleanse(key->pkey->data, key->pkey->length);
  23. }
  24. return 1;
  25. }
  26. ASN1_SEQUENCE_cb(PKCS8_PRIV_KEY_INFO, pkey_cb) = {
  27. ASN1_SIMPLE(PKCS8_PRIV_KEY_INFO, version, ASN1_INTEGER),
  28. ASN1_SIMPLE(PKCS8_PRIV_KEY_INFO, pkeyalg, X509_ALGOR),
  29. ASN1_SIMPLE(PKCS8_PRIV_KEY_INFO, pkey, ASN1_OCTET_STRING),
  30. ASN1_IMP_SET_OF_OPT(PKCS8_PRIV_KEY_INFO, attributes, X509_ATTRIBUTE, 0)
  31. } ASN1_SEQUENCE_END_cb(PKCS8_PRIV_KEY_INFO, PKCS8_PRIV_KEY_INFO)
  32. IMPLEMENT_ASN1_FUNCTIONS(PKCS8_PRIV_KEY_INFO)
  33. int PKCS8_pkey_set0(PKCS8_PRIV_KEY_INFO *priv, ASN1_OBJECT *aobj,
  34. int version,
  35. int ptype, void *pval, unsigned char *penc, int penclen)
  36. {
  37. if (version >= 0) {
  38. if (!ASN1_INTEGER_set(priv->version, version))
  39. return 0;
  40. }
  41. if (!X509_ALGOR_set0(priv->pkeyalg, aobj, ptype, pval))
  42. return 0;
  43. if (penc)
  44. ASN1_STRING_set0(priv->pkey, penc, penclen);
  45. return 1;
  46. }
  47. int PKCS8_pkey_get0(const ASN1_OBJECT **ppkalg,
  48. const unsigned char **pk, int *ppklen,
  49. const X509_ALGOR **pa, const PKCS8_PRIV_KEY_INFO *p8)
  50. {
  51. if (ppkalg)
  52. *ppkalg = p8->pkeyalg->algorithm;
  53. if (pk) {
  54. *pk = ASN1_STRING_get0_data(p8->pkey);
  55. *ppklen = ASN1_STRING_length(p8->pkey);
  56. }
  57. if (pa)
  58. *pa = p8->pkeyalg;
  59. return 1;
  60. }
  61. const STACK_OF(X509_ATTRIBUTE) *
  62. PKCS8_pkey_get0_attrs(const PKCS8_PRIV_KEY_INFO *p8)
  63. {
  64. return p8->attributes;
  65. }
  66. int PKCS8_pkey_add1_attr_by_NID(PKCS8_PRIV_KEY_INFO *p8, int nid, int type,
  67. const unsigned char *bytes, int len)
  68. {
  69. if (X509at_add1_attr_by_NID(&p8->attributes, nid, type, bytes, len) != NULL)
  70. return 1;
  71. return 0;
  72. }