| 1 |
/* |
| 2 |
* Copyright 1999-2025 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 |
|
| 10 |
#include <stdio.h> |
| 11 |
#include "internal/cryptlib.h" |
| 12 |
#include <openssl/asn1t.h> |
| 13 |
#include <openssl/x509.h> |
| 14 |
#include "crypto/x509.h" |
| 15 |
|
| 16 |
/* Minor tweak to operation: zero private key data */ |
| 17 |
static int pkey_cb(int operation, ASN1_VALUE **pval, const ASN1_ITEM *it, |
| 18 |
void *exarg) |
| 19 |
{ |
| 20 |
PKCS8_PRIV_KEY_INFO *key; |
| 21 |
int version; |
| 22 |
|
| 23 |
switch (operation) { |
| 24 |
case ASN1_OP_FREE_PRE: |
| 25 |
/* The structure is still valid during ASN1_OP_FREE_PRE */ |
| 26 |
key = (PKCS8_PRIV_KEY_INFO *)*pval; |
| 27 |
if (key->pkey) |
| 28 |
OPENSSL_cleanse(key->pkey->data, key->pkey->length); |
| 29 |
break; |
| 30 |
case ASN1_OP_D2I_POST: |
| 31 |
/* Insist on a valid version now that the structure is decoded */ |
| 32 |
key = (PKCS8_PRIV_KEY_INFO *)*pval; |
| 33 |
version = ASN1_INTEGER_get(key->version); |
| 34 |
if (version < 0 || version > 1) |
| 35 |
return 0; |
| 36 |
if (version == 0 && key->kpub != NULL) |
| 37 |
return 0; |
| 38 |
break; |
| 39 |
} |
| 40 |
return 1; |
| 41 |
} |
| 42 |
|
| 43 |
ASN1_SEQUENCE_cb(PKCS8_PRIV_KEY_INFO, pkey_cb) = { |
| 44 |
ASN1_SIMPLE(PKCS8_PRIV_KEY_INFO, version, ASN1_INTEGER), |
| 45 |
ASN1_SIMPLE(PKCS8_PRIV_KEY_INFO, pkeyalg, X509_ALGOR), |
| 46 |
ASN1_SIMPLE(PKCS8_PRIV_KEY_INFO, pkey, ASN1_OCTET_STRING), |
| 47 |
ASN1_IMP_SET_OF_OPT(PKCS8_PRIV_KEY_INFO, attributes, X509_ATTRIBUTE, 0), |
| 48 |
ASN1_IMP_OPT(PKCS8_PRIV_KEY_INFO, kpub, ASN1_BIT_STRING, 1) |
| 49 |
} ASN1_SEQUENCE_END_cb(PKCS8_PRIV_KEY_INFO, PKCS8_PRIV_KEY_INFO) |
| 50 |
|
| 51 |
IMPLEMENT_ASN1_FUNCTIONS(PKCS8_PRIV_KEY_INFO) |
| 52 |
|
| 53 |
int PKCS8_pkey_set0(PKCS8_PRIV_KEY_INFO *priv, ASN1_OBJECT *aobj, |
| 54 |
int version, |
| 55 |
int ptype, void *pval, unsigned char *penc, int penclen) |
| 56 |
{ |
| 57 |
if (version >= 0) { |
| 58 |
/* We only support PKCS#8 v1 (0) and v2 (1). */ |
| 59 |
if (version > 1) |
| 60 |
return 0; |
| 61 |
if (!ASN1_INTEGER_set(priv->version, version)) |
| 62 |
return 0; |
| 63 |
} |
| 64 |
if (!X509_ALGOR_set0(priv->pkeyalg, aobj, ptype, pval)) |
| 65 |
return 0; |
| 66 |
if (penc) |
| 67 |
ASN1_STRING_set0(priv->pkey, penc, penclen); |
| 68 |
return 1; |
| 69 |
} |
| 70 |
|
| 71 |
int PKCS8_pkey_get0(const ASN1_OBJECT **ppkalg, |
| 72 |
const unsigned char **pk, int *ppklen, |
| 73 |
const X509_ALGOR **pa, const PKCS8_PRIV_KEY_INFO *p8) |
| 74 |
{ |
| 75 |
if (ppkalg) |
| 76 |
*ppkalg = p8->pkeyalg->algorithm; |
| 77 |
if (pk) { |
| 78 |
*pk = ASN1_STRING_get0_data(p8->pkey); |
| 79 |
*ppklen = ASN1_STRING_length(p8->pkey); |
| 80 |
} |
| 81 |
if (pa) |
| 82 |
*pa = p8->pkeyalg; |
| 83 |
return 1; |
| 84 |
} |
| 85 |
|
| 86 |
const STACK_OF(X509_ATTRIBUTE) * |
| 87 |
PKCS8_pkey_get0_attrs(const PKCS8_PRIV_KEY_INFO *p8) |
| 88 |
{ |
| 89 |
return p8->attributes; |
| 90 |
} |
| 91 |
|
| 92 |
int PKCS8_pkey_add1_attr_by_NID(PKCS8_PRIV_KEY_INFO *p8, int nid, int type, |
| 93 |
const unsigned char *bytes, int len) |
| 94 |
{ |
| 95 |
if (X509at_add1_attr_by_NID(&p8->attributes, nid, type, bytes, len) != NULL) |
| 96 |
return 1; |
| 97 |
return 0; |
| 98 |
} |
| 99 |
|
| 100 |
int PKCS8_pkey_add1_attr_by_OBJ(PKCS8_PRIV_KEY_INFO *p8, const ASN1_OBJECT *obj, int type, |
| 101 |
const unsigned char *bytes, int len) |
| 102 |
{ |
| 103 |
return (X509at_add1_attr_by_OBJ(&p8->attributes, obj, type, bytes, len) != NULL); |
| 104 |
} |
| 105 |
|
| 106 |
int PKCS8_pkey_add1_attr(PKCS8_PRIV_KEY_INFO *p8, X509_ATTRIBUTE *attr) |
| 107 |
{ |
| 108 |
return (X509at_add1_attr(&p8->attributes, attr) != NULL); |
| 109 |
} |