p12_sbag.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. /*
  2. * Copyright 1999-2018 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/pkcs12.h>
  12. #include "p12_local.h"
  13. #if !OPENSSL_API_1_1_0
  14. ASN1_TYPE *PKCS12_get_attr(const PKCS12_SAFEBAG *bag, int attr_nid)
  15. {
  16. return PKCS12_get_attr_gen(bag->attrib, attr_nid);
  17. }
  18. #endif
  19. const ASN1_TYPE *PKCS12_SAFEBAG_get0_attr(const PKCS12_SAFEBAG *bag,
  20. int attr_nid)
  21. {
  22. return PKCS12_get_attr_gen(bag->attrib, attr_nid);
  23. }
  24. ASN1_TYPE *PKCS8_get_attr(PKCS8_PRIV_KEY_INFO *p8, int attr_nid)
  25. {
  26. return PKCS12_get_attr_gen(PKCS8_pkey_get0_attrs(p8), attr_nid);
  27. }
  28. const PKCS8_PRIV_KEY_INFO *PKCS12_SAFEBAG_get0_p8inf(const PKCS12_SAFEBAG *bag)
  29. {
  30. if (PKCS12_SAFEBAG_get_nid(bag) != NID_keyBag)
  31. return NULL;
  32. return bag->value.keybag;
  33. }
  34. const X509_SIG *PKCS12_SAFEBAG_get0_pkcs8(const PKCS12_SAFEBAG *bag)
  35. {
  36. if (OBJ_obj2nid(bag->type) != NID_pkcs8ShroudedKeyBag)
  37. return NULL;
  38. return bag->value.shkeybag;
  39. }
  40. const STACK_OF(PKCS12_SAFEBAG) *
  41. PKCS12_SAFEBAG_get0_safes(const PKCS12_SAFEBAG *bag)
  42. {
  43. if (OBJ_obj2nid(bag->type) != NID_safeContentsBag)
  44. return NULL;
  45. return bag->value.safes;
  46. }
  47. const ASN1_OBJECT *PKCS12_SAFEBAG_get0_type(const PKCS12_SAFEBAG *bag)
  48. {
  49. return bag->type;
  50. }
  51. int PKCS12_SAFEBAG_get_nid(const PKCS12_SAFEBAG *bag)
  52. {
  53. return OBJ_obj2nid(bag->type);
  54. }
  55. int PKCS12_SAFEBAG_get_bag_nid(const PKCS12_SAFEBAG *bag)
  56. {
  57. int btype = PKCS12_SAFEBAG_get_nid(bag);
  58. if (btype != NID_certBag && btype != NID_crlBag && btype != NID_secretBag)
  59. return -1;
  60. return OBJ_obj2nid(bag->value.bag->type);
  61. }
  62. X509 *PKCS12_SAFEBAG_get1_cert(const PKCS12_SAFEBAG *bag)
  63. {
  64. if (PKCS12_SAFEBAG_get_nid(bag) != NID_certBag)
  65. return NULL;
  66. if (OBJ_obj2nid(bag->value.bag->type) != NID_x509Certificate)
  67. return NULL;
  68. return ASN1_item_unpack(bag->value.bag->value.octet,
  69. ASN1_ITEM_rptr(X509));
  70. }
  71. X509_CRL *PKCS12_SAFEBAG_get1_crl(const PKCS12_SAFEBAG *bag)
  72. {
  73. if (PKCS12_SAFEBAG_get_nid(bag) != NID_crlBag)
  74. return NULL;
  75. if (OBJ_obj2nid(bag->value.bag->type) != NID_x509Crl)
  76. return NULL;
  77. return ASN1_item_unpack(bag->value.bag->value.octet,
  78. ASN1_ITEM_rptr(X509_CRL));
  79. }
  80. PKCS12_SAFEBAG *PKCS12_SAFEBAG_create_cert(X509 *x509)
  81. {
  82. return PKCS12_item_pack_safebag(x509, ASN1_ITEM_rptr(X509),
  83. NID_x509Certificate, NID_certBag);
  84. }
  85. PKCS12_SAFEBAG *PKCS12_SAFEBAG_create_crl(X509_CRL *crl)
  86. {
  87. return PKCS12_item_pack_safebag(crl, ASN1_ITEM_rptr(X509_CRL),
  88. NID_x509Crl, NID_crlBag);
  89. }
  90. /* Turn PKCS8 object into a keybag */
  91. PKCS12_SAFEBAG *PKCS12_SAFEBAG_create0_p8inf(PKCS8_PRIV_KEY_INFO *p8)
  92. {
  93. PKCS12_SAFEBAG *bag = PKCS12_SAFEBAG_new();
  94. if (bag == NULL) {
  95. PKCS12err(PKCS12_F_PKCS12_SAFEBAG_CREATE0_P8INF, ERR_R_MALLOC_FAILURE);
  96. return NULL;
  97. }
  98. bag->type = OBJ_nid2obj(NID_keyBag);
  99. bag->value.keybag = p8;
  100. return bag;
  101. }
  102. /* Turn PKCS8 object into a shrouded keybag */
  103. PKCS12_SAFEBAG *PKCS12_SAFEBAG_create0_pkcs8(X509_SIG *p8)
  104. {
  105. PKCS12_SAFEBAG *bag = PKCS12_SAFEBAG_new();
  106. /* Set up the safe bag */
  107. if (bag == NULL) {
  108. PKCS12err(PKCS12_F_PKCS12_SAFEBAG_CREATE0_PKCS8, ERR_R_MALLOC_FAILURE);
  109. return NULL;
  110. }
  111. bag->type = OBJ_nid2obj(NID_pkcs8ShroudedKeyBag);
  112. bag->value.shkeybag = p8;
  113. return bag;
  114. }
  115. PKCS12_SAFEBAG *PKCS12_SAFEBAG_create_pkcs8_encrypt(int pbe_nid,
  116. const char *pass,
  117. int passlen,
  118. unsigned char *salt,
  119. int saltlen, int iter,
  120. PKCS8_PRIV_KEY_INFO *p8inf)
  121. {
  122. PKCS12_SAFEBAG *bag;
  123. const EVP_CIPHER *pbe_ciph;
  124. X509_SIG *p8;
  125. pbe_ciph = EVP_get_cipherbynid(pbe_nid);
  126. if (pbe_ciph)
  127. pbe_nid = -1;
  128. p8 = PKCS8_encrypt(pbe_nid, pbe_ciph, pass, passlen, salt, saltlen, iter,
  129. p8inf);
  130. if (p8 == NULL)
  131. return NULL;
  132. bag = PKCS12_SAFEBAG_create0_pkcs8(p8);
  133. if (bag == NULL)
  134. X509_SIG_free(p8);
  135. return bag;
  136. }