p12_attr.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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/pkcs12.h>
  12. #include "p12_lcl.h"
  13. /* Add a local keyid to a safebag */
  14. int PKCS12_add_localkeyid(PKCS12_SAFEBAG *bag, unsigned char *name,
  15. int namelen)
  16. {
  17. if (X509at_add1_attr_by_NID(&bag->attrib, NID_localKeyID,
  18. V_ASN1_OCTET_STRING, name, namelen))
  19. return 1;
  20. else
  21. return 0;
  22. }
  23. /* Add key usage to PKCS#8 structure */
  24. int PKCS8_add_keyusage(PKCS8_PRIV_KEY_INFO *p8, int usage)
  25. {
  26. unsigned char us_val = (unsigned char)usage;
  27. return PKCS8_pkey_add1_attr_by_NID(p8, NID_key_usage,
  28. V_ASN1_BIT_STRING, &us_val, 1);
  29. }
  30. /* Add a friendlyname to a safebag */
  31. int PKCS12_add_friendlyname_asc(PKCS12_SAFEBAG *bag, const char *name,
  32. int namelen)
  33. {
  34. if (X509at_add1_attr_by_NID(&bag->attrib, NID_friendlyName,
  35. MBSTRING_ASC, (unsigned char *)name, namelen))
  36. return 1;
  37. else
  38. return 0;
  39. }
  40. int PKCS12_add_friendlyname_utf8(PKCS12_SAFEBAG *bag, const char *name,
  41. int namelen)
  42. {
  43. if (X509at_add1_attr_by_NID(&bag->attrib, NID_friendlyName,
  44. MBSTRING_UTF8, (unsigned char *)name, namelen))
  45. return 1;
  46. else
  47. return 0;
  48. }
  49. int PKCS12_add_friendlyname_uni(PKCS12_SAFEBAG *bag,
  50. const unsigned char *name, int namelen)
  51. {
  52. if (X509at_add1_attr_by_NID(&bag->attrib, NID_friendlyName,
  53. MBSTRING_BMP, name, namelen))
  54. return 1;
  55. else
  56. return 0;
  57. }
  58. int PKCS12_add_CSPName_asc(PKCS12_SAFEBAG *bag, const char *name, int namelen)
  59. {
  60. if (X509at_add1_attr_by_NID(&bag->attrib, NID_ms_csp_name,
  61. MBSTRING_ASC, (unsigned char *)name, namelen))
  62. return 1;
  63. else
  64. return 0;
  65. }
  66. ASN1_TYPE *PKCS12_get_attr_gen(const STACK_OF(X509_ATTRIBUTE) *attrs,
  67. int attr_nid)
  68. {
  69. X509_ATTRIBUTE *attrib;
  70. int i;
  71. i = X509at_get_attr_by_NID(attrs, attr_nid, -1);
  72. attrib = X509at_get_attr(attrs, i);
  73. return X509_ATTRIBUTE_get0_type(attrib, 0);
  74. }
  75. char *PKCS12_get_friendlyname(PKCS12_SAFEBAG *bag)
  76. {
  77. const ASN1_TYPE *atype;
  78. if ((atype = PKCS12_SAFEBAG_get0_attr(bag, NID_friendlyName)) == NULL)
  79. return NULL;
  80. if (atype->type != V_ASN1_BMPSTRING)
  81. return NULL;
  82. return OPENSSL_uni2utf8(atype->value.bmpstring->data,
  83. atype->value.bmpstring->length);
  84. }
  85. const STACK_OF(X509_ATTRIBUTE) *
  86. PKCS12_SAFEBAG_get0_attrs(const PKCS12_SAFEBAG *bag)
  87. {
  88. return bag->attrib;
  89. }