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/asn1t.h> 13e1051a39Sopenharmony_ci#include <openssl/x509.h> 14e1051a39Sopenharmony_ci#include "crypto/x509.h" 15e1051a39Sopenharmony_ci 16e1051a39Sopenharmony_ci/* Minor tweak to operation: zero private key data */ 17e1051a39Sopenharmony_cistatic int pkey_cb(int operation, ASN1_VALUE **pval, const ASN1_ITEM *it, 18e1051a39Sopenharmony_ci void *exarg) 19e1051a39Sopenharmony_ci{ 20e1051a39Sopenharmony_ci /* Since the structure must still be valid use ASN1_OP_FREE_PRE */ 21e1051a39Sopenharmony_ci if (operation == ASN1_OP_FREE_PRE) { 22e1051a39Sopenharmony_ci PKCS8_PRIV_KEY_INFO *key = (PKCS8_PRIV_KEY_INFO *)*pval; 23e1051a39Sopenharmony_ci if (key->pkey) 24e1051a39Sopenharmony_ci OPENSSL_cleanse(key->pkey->data, key->pkey->length); 25e1051a39Sopenharmony_ci } 26e1051a39Sopenharmony_ci return 1; 27e1051a39Sopenharmony_ci} 28e1051a39Sopenharmony_ci 29e1051a39Sopenharmony_ciASN1_SEQUENCE_cb(PKCS8_PRIV_KEY_INFO, pkey_cb) = { 30e1051a39Sopenharmony_ci ASN1_SIMPLE(PKCS8_PRIV_KEY_INFO, version, ASN1_INTEGER), 31e1051a39Sopenharmony_ci ASN1_SIMPLE(PKCS8_PRIV_KEY_INFO, pkeyalg, X509_ALGOR), 32e1051a39Sopenharmony_ci ASN1_SIMPLE(PKCS8_PRIV_KEY_INFO, pkey, ASN1_OCTET_STRING), 33e1051a39Sopenharmony_ci ASN1_IMP_SET_OF_OPT(PKCS8_PRIV_KEY_INFO, attributes, X509_ATTRIBUTE, 0) 34e1051a39Sopenharmony_ci} ASN1_SEQUENCE_END_cb(PKCS8_PRIV_KEY_INFO, PKCS8_PRIV_KEY_INFO) 35e1051a39Sopenharmony_ci 36e1051a39Sopenharmony_ciIMPLEMENT_ASN1_FUNCTIONS(PKCS8_PRIV_KEY_INFO) 37e1051a39Sopenharmony_ci 38e1051a39Sopenharmony_ciint PKCS8_pkey_set0(PKCS8_PRIV_KEY_INFO *priv, ASN1_OBJECT *aobj, 39e1051a39Sopenharmony_ci int version, 40e1051a39Sopenharmony_ci int ptype, void *pval, unsigned char *penc, int penclen) 41e1051a39Sopenharmony_ci{ 42e1051a39Sopenharmony_ci if (version >= 0) { 43e1051a39Sopenharmony_ci if (!ASN1_INTEGER_set(priv->version, version)) 44e1051a39Sopenharmony_ci return 0; 45e1051a39Sopenharmony_ci } 46e1051a39Sopenharmony_ci if (!X509_ALGOR_set0(priv->pkeyalg, aobj, ptype, pval)) 47e1051a39Sopenharmony_ci return 0; 48e1051a39Sopenharmony_ci if (penc) 49e1051a39Sopenharmony_ci ASN1_STRING_set0(priv->pkey, penc, penclen); 50e1051a39Sopenharmony_ci return 1; 51e1051a39Sopenharmony_ci} 52e1051a39Sopenharmony_ci 53e1051a39Sopenharmony_ciint PKCS8_pkey_get0(const ASN1_OBJECT **ppkalg, 54e1051a39Sopenharmony_ci const unsigned char **pk, int *ppklen, 55e1051a39Sopenharmony_ci const X509_ALGOR **pa, const PKCS8_PRIV_KEY_INFO *p8) 56e1051a39Sopenharmony_ci{ 57e1051a39Sopenharmony_ci if (ppkalg) 58e1051a39Sopenharmony_ci *ppkalg = p8->pkeyalg->algorithm; 59e1051a39Sopenharmony_ci if (pk) { 60e1051a39Sopenharmony_ci *pk = ASN1_STRING_get0_data(p8->pkey); 61e1051a39Sopenharmony_ci *ppklen = ASN1_STRING_length(p8->pkey); 62e1051a39Sopenharmony_ci } 63e1051a39Sopenharmony_ci if (pa) 64e1051a39Sopenharmony_ci *pa = p8->pkeyalg; 65e1051a39Sopenharmony_ci return 1; 66e1051a39Sopenharmony_ci} 67e1051a39Sopenharmony_ci 68e1051a39Sopenharmony_ciconst STACK_OF(X509_ATTRIBUTE) * 69e1051a39Sopenharmony_ciPKCS8_pkey_get0_attrs(const PKCS8_PRIV_KEY_INFO *p8) 70e1051a39Sopenharmony_ci{ 71e1051a39Sopenharmony_ci return p8->attributes; 72e1051a39Sopenharmony_ci} 73e1051a39Sopenharmony_ci 74e1051a39Sopenharmony_ciint PKCS8_pkey_add1_attr_by_NID(PKCS8_PRIV_KEY_INFO *p8, int nid, int type, 75e1051a39Sopenharmony_ci const unsigned char *bytes, int len) 76e1051a39Sopenharmony_ci{ 77e1051a39Sopenharmony_ci if (X509at_add1_attr_by_NID(&p8->attributes, nid, type, bytes, len) != NULL) 78e1051a39Sopenharmony_ci return 1; 79e1051a39Sopenharmony_ci return 0; 80e1051a39Sopenharmony_ci} 81e1051a39Sopenharmony_ci 82e1051a39Sopenharmony_ciint PKCS8_pkey_add1_attr_by_OBJ(PKCS8_PRIV_KEY_INFO *p8, const ASN1_OBJECT *obj, int type, 83e1051a39Sopenharmony_ci const unsigned char *bytes, int len) 84e1051a39Sopenharmony_ci{ 85e1051a39Sopenharmony_ci return (X509at_add1_attr_by_OBJ(&p8->attributes, obj, type, bytes, len) != NULL); 86e1051a39Sopenharmony_ci} 87e1051a39Sopenharmony_ci 88e1051a39Sopenharmony_ciint PKCS8_pkey_add1_attr(PKCS8_PRIV_KEY_INFO *p8, X509_ATTRIBUTE *attr) 89e1051a39Sopenharmony_ci{ 90e1051a39Sopenharmony_ci return (X509at_add1_attr(&p8->attributes, attr) != NULL); 91e1051a39Sopenharmony_ci} 92