1e1051a39Sopenharmony_ci/* 2e1051a39Sopenharmony_ci * Copyright 1999-2020 The OpenSSL Project Authors. All Rights Reserved. 3e1051a39Sopenharmony_ci * 4e1051a39Sopenharmony_ci * Licensed under the Apache License 2.0 (the "License"). You may not use 5e1051a39Sopenharmony_ci * this file except in compliance with the License. You can obtain a copy 6e1051a39Sopenharmony_ci * in the file LICENSE in the source distribution or at 7e1051a39Sopenharmony_ci * https://www.openssl.org/source/license.html 8e1051a39Sopenharmony_ci */ 9e1051a39Sopenharmony_ci 10e1051a39Sopenharmony_ci#include <stdio.h> 11e1051a39Sopenharmony_ci#include "internal/cryptlib.h" 12e1051a39Sopenharmony_ci#include <openssl/pkcs12.h> 13e1051a39Sopenharmony_ci#include "p12_local.h" 14e1051a39Sopenharmony_ci 15e1051a39Sopenharmony_ci/* Add a local keyid to a safebag */ 16e1051a39Sopenharmony_ci 17e1051a39Sopenharmony_ciint PKCS12_add_localkeyid(PKCS12_SAFEBAG *bag, unsigned char *name, 18e1051a39Sopenharmony_ci int namelen) 19e1051a39Sopenharmony_ci{ 20e1051a39Sopenharmony_ci if (X509at_add1_attr_by_NID(&bag->attrib, NID_localKeyID, 21e1051a39Sopenharmony_ci V_ASN1_OCTET_STRING, name, namelen) != NULL) 22e1051a39Sopenharmony_ci return 1; 23e1051a39Sopenharmony_ci else 24e1051a39Sopenharmony_ci return 0; 25e1051a39Sopenharmony_ci} 26e1051a39Sopenharmony_ci 27e1051a39Sopenharmony_ci/* Add key usage to PKCS#8 structure */ 28e1051a39Sopenharmony_ci 29e1051a39Sopenharmony_ciint PKCS8_add_keyusage(PKCS8_PRIV_KEY_INFO *p8, int usage) 30e1051a39Sopenharmony_ci{ 31e1051a39Sopenharmony_ci unsigned char us_val = (unsigned char)usage; 32e1051a39Sopenharmony_ci return PKCS8_pkey_add1_attr_by_NID(p8, NID_key_usage, 33e1051a39Sopenharmony_ci V_ASN1_BIT_STRING, &us_val, 1); 34e1051a39Sopenharmony_ci} 35e1051a39Sopenharmony_ci 36e1051a39Sopenharmony_ci/* Add a friendlyname to a safebag */ 37e1051a39Sopenharmony_ci 38e1051a39Sopenharmony_ciint PKCS12_add_friendlyname_asc(PKCS12_SAFEBAG *bag, const char *name, 39e1051a39Sopenharmony_ci int namelen) 40e1051a39Sopenharmony_ci{ 41e1051a39Sopenharmony_ci if (X509at_add1_attr_by_NID(&bag->attrib, NID_friendlyName, 42e1051a39Sopenharmony_ci MBSTRING_ASC, (unsigned char *)name, namelen) != NULL) 43e1051a39Sopenharmony_ci return 1; 44e1051a39Sopenharmony_ci else 45e1051a39Sopenharmony_ci return 0; 46e1051a39Sopenharmony_ci} 47e1051a39Sopenharmony_ci 48e1051a39Sopenharmony_ciint PKCS12_add_friendlyname_utf8(PKCS12_SAFEBAG *bag, const char *name, 49e1051a39Sopenharmony_ci int namelen) 50e1051a39Sopenharmony_ci{ 51e1051a39Sopenharmony_ci if (X509at_add1_attr_by_NID(&bag->attrib, NID_friendlyName, 52e1051a39Sopenharmony_ci MBSTRING_UTF8, (unsigned char *)name, namelen) != NULL) 53e1051a39Sopenharmony_ci return 1; 54e1051a39Sopenharmony_ci else 55e1051a39Sopenharmony_ci return 0; 56e1051a39Sopenharmony_ci} 57e1051a39Sopenharmony_ci 58e1051a39Sopenharmony_ciint PKCS12_add_friendlyname_uni(PKCS12_SAFEBAG *bag, 59e1051a39Sopenharmony_ci const unsigned char *name, int namelen) 60e1051a39Sopenharmony_ci{ 61e1051a39Sopenharmony_ci if (X509at_add1_attr_by_NID(&bag->attrib, NID_friendlyName, 62e1051a39Sopenharmony_ci MBSTRING_BMP, name, namelen) != NULL) 63e1051a39Sopenharmony_ci return 1; 64e1051a39Sopenharmony_ci else 65e1051a39Sopenharmony_ci return 0; 66e1051a39Sopenharmony_ci} 67e1051a39Sopenharmony_ci 68e1051a39Sopenharmony_ciint PKCS12_add_CSPName_asc(PKCS12_SAFEBAG *bag, const char *name, int namelen) 69e1051a39Sopenharmony_ci{ 70e1051a39Sopenharmony_ci if (X509at_add1_attr_by_NID(&bag->attrib, NID_ms_csp_name, 71e1051a39Sopenharmony_ci MBSTRING_ASC, (unsigned char *)name, namelen) != NULL) 72e1051a39Sopenharmony_ci return 1; 73e1051a39Sopenharmony_ci else 74e1051a39Sopenharmony_ci return 0; 75e1051a39Sopenharmony_ci} 76e1051a39Sopenharmony_ci 77e1051a39Sopenharmony_ciint PKCS12_add1_attr_by_NID(PKCS12_SAFEBAG *bag, int nid, int type, 78e1051a39Sopenharmony_ci const unsigned char *bytes, int len) 79e1051a39Sopenharmony_ci{ 80e1051a39Sopenharmony_ci if (X509at_add1_attr_by_NID(&bag->attrib, nid, type, bytes, len) != NULL) 81e1051a39Sopenharmony_ci return 1; 82e1051a39Sopenharmony_ci else 83e1051a39Sopenharmony_ci return 0; 84e1051a39Sopenharmony_ci} 85e1051a39Sopenharmony_ci 86e1051a39Sopenharmony_ciint PKCS12_add1_attr_by_txt(PKCS12_SAFEBAG *bag, const char *attrname, int type, 87e1051a39Sopenharmony_ci const unsigned char *bytes, int len) 88e1051a39Sopenharmony_ci{ 89e1051a39Sopenharmony_ci if (X509at_add1_attr_by_txt(&bag->attrib, attrname, type, bytes, len) != NULL) 90e1051a39Sopenharmony_ci return 1; 91e1051a39Sopenharmony_ci else 92e1051a39Sopenharmony_ci return 0; 93e1051a39Sopenharmony_ci} 94e1051a39Sopenharmony_ci 95e1051a39Sopenharmony_ciASN1_TYPE *PKCS12_get_attr_gen(const STACK_OF(X509_ATTRIBUTE) *attrs, 96e1051a39Sopenharmony_ci int attr_nid) 97e1051a39Sopenharmony_ci{ 98e1051a39Sopenharmony_ci X509_ATTRIBUTE *attrib; 99e1051a39Sopenharmony_ci int i; 100e1051a39Sopenharmony_ci i = X509at_get_attr_by_NID(attrs, attr_nid, -1); 101e1051a39Sopenharmony_ci attrib = X509at_get_attr(attrs, i); 102e1051a39Sopenharmony_ci return X509_ATTRIBUTE_get0_type(attrib, 0); 103e1051a39Sopenharmony_ci} 104e1051a39Sopenharmony_ci 105e1051a39Sopenharmony_cichar *PKCS12_get_friendlyname(PKCS12_SAFEBAG *bag) 106e1051a39Sopenharmony_ci{ 107e1051a39Sopenharmony_ci const ASN1_TYPE *atype; 108e1051a39Sopenharmony_ci 109e1051a39Sopenharmony_ci if ((atype = PKCS12_SAFEBAG_get0_attr(bag, NID_friendlyName)) == NULL) 110e1051a39Sopenharmony_ci return NULL; 111e1051a39Sopenharmony_ci if (atype->type != V_ASN1_BMPSTRING) 112e1051a39Sopenharmony_ci return NULL; 113e1051a39Sopenharmony_ci return OPENSSL_uni2utf8(atype->value.bmpstring->data, 114e1051a39Sopenharmony_ci atype->value.bmpstring->length); 115e1051a39Sopenharmony_ci} 116e1051a39Sopenharmony_ci 117e1051a39Sopenharmony_ciconst STACK_OF(X509_ATTRIBUTE) * 118e1051a39Sopenharmony_ciPKCS12_SAFEBAG_get0_attrs(const PKCS12_SAFEBAG *bag) 119e1051a39Sopenharmony_ci{ 120e1051a39Sopenharmony_ci return bag->attrib; 121e1051a39Sopenharmony_ci} 122